1 | var dns = require('../dns'),
|
2 | Request = dns.Request,
|
3 | Question = dns.Question,
|
4 | consts = dns.consts,
|
5 | dgram = require('dgram');
|
6 |
|
7 | var q = Question({
|
8 | name: 'www.google.com',
|
9 | type: consts.NAME_TO_QTYPE.A,
|
10 | });
|
11 |
|
12 | var noServer = {
|
13 | address: '127.0.0.1',
|
14 | port: 19999,
|
15 | type: 'udp',
|
16 | };
|
17 |
|
18 | var udpServ = {
|
19 | address: '8.8.8.8',
|
20 | port: 53,
|
21 | type: 'udp',
|
22 | };
|
23 |
|
24 | var tcpServ = {
|
25 | address: '8.8.8.8',
|
26 | port: 53,
|
27 | type: 'tcp',
|
28 | };
|
29 |
|
30 | exports.setUp = function (cb) {
|
31 | cb();
|
32 | };
|
33 |
|
34 | exports.timeout = function (test) {
|
35 | var r = Request({
|
36 | question: q,
|
37 | server: noServer,
|
38 | timeout: 100,
|
39 | });
|
40 |
|
41 | var timedout = false;
|
42 |
|
43 | r.on('timeout', function () {
|
44 | timedout = true;
|
45 | });
|
46 |
|
47 | r.on('end', function () {
|
48 | test.ok(timedout, 'Failed to timeout');
|
49 | test.done();
|
50 | });
|
51 |
|
52 | r.send();
|
53 | };
|
54 |
|
55 | exports.udpResponse = function (test) {
|
56 | var r = Request({
|
57 | question: q,
|
58 | server: udpServ,
|
59 | timeout: 4000,
|
60 | });
|
61 |
|
62 | r.on('message', function (err, answer) {
|
63 | test.ok(!err, 'UDP Request should not error');
|
64 | test.ok(answer, 'No UDP answer provided');
|
65 | test.ok(answer.answer.length > 0, 'No answers found');
|
66 | });
|
67 |
|
68 | r.on('timeout', function () {
|
69 | test.ok(false, 'UDP Requests should not timeout');
|
70 | });
|
71 |
|
72 | r.on('end', function () {
|
73 | test.done();
|
74 | });
|
75 |
|
76 | r.send();
|
77 | };
|
78 |
|
79 | exports.tcpResponse = function (test) {
|
80 | var r = Request({
|
81 | question: q,
|
82 | server: tcpServ,
|
83 | timeout: 4000,
|
84 | });
|
85 |
|
86 | r.on('message', function (err, answer) {
|
87 | test.ok(!err, 'TCP Request should not error');
|
88 | test.ok(answer, 'No TCP answer provided');
|
89 | test.ok(answer.answer.length > 0, 'No answers found');
|
90 | });
|
91 |
|
92 | r.on('timeout', function () {
|
93 | test.ok(false, 'TCP Requests should not timeout');
|
94 | });
|
95 |
|
96 | r.on('end', function () {
|
97 | test.done();
|
98 | });
|
99 |
|
100 | r.send();
|
101 | };
|
102 |
|
103 | exports.serverString = function (test) {
|
104 | var r = Request({
|
105 | question: q,
|
106 | server: '8.8.8.8',
|
107 | });
|
108 |
|
109 | r.on('message', function (err, answer) {
|
110 | test.ok(answer.answer.length > 0, 'no answers found');
|
111 | });
|
112 |
|
113 | r.on('timeout', function () {
|
114 | test.ok(false, 'Should not timeout');
|
115 | });
|
116 |
|
117 | r.on('end', function () {
|
118 | test.done();
|
119 | });
|
120 |
|
121 | r.send();
|
122 | };
|
123 |
|
124 | exports.questionString = function (test) {
|
125 | var r = Request({
|
126 | question: Question({
|
127 | name: 'www.google.com',
|
128 | type: 'a',
|
129 | }),
|
130 | server: '8.8.8.8',
|
131 | });
|
132 |
|
133 | r.on('message', function (err, answer) {
|
134 | test.ok(answer.answer.length > 0, 'no answers found');
|
135 | });
|
136 |
|
137 | r.on('timeout', function () {
|
138 | test.ok(false, 'Should not timeout');
|
139 | });
|
140 |
|
141 | r.on('end', function () {
|
142 | test.done();
|
143 | });
|
144 |
|
145 | r.send();
|
146 | };
|
147 |
|
148 | exports.emptyUdp = function (test) {
|
149 | var socket = dgram.createSocket('udp4');
|
150 | socket.on('listening', function () {
|
151 | var timeout = false;
|
152 | var r = Request({
|
153 | question: q,
|
154 | server: { address: '127.0.0.1', port: socket.address().port, type: 'udp' },
|
155 | timeout: 300,
|
156 | });
|
157 | r.on('message', function () {
|
158 | test.ok(false, 'There should not be a response');
|
159 | });
|
160 | r.on('timeout', function () {
|
161 | timeout = true;
|
162 | });
|
163 | r.on('end', function () {
|
164 | test.ok(timeout, 'This should timeout');
|
165 | socket.close();
|
166 | test.done();
|
167 | });
|
168 | r.send();
|
169 | });
|
170 | socket.on('message', function (msg, remote) {
|
171 | socket.send(new Buffer(1), 0, 1, remote.port, remote.address);
|
172 | });
|
173 | socket.bind();
|
174 | };
|
175 |
|
176 | exports.longName = function (test) {
|
177 | var didErr = false;
|
178 | var r = Request({
|
179 | question: Question({
|
180 | name: '*************'
|
181 | + '***************'
|
182 | + '***************'
|
183 | + '***************'
|
184 | + '***************'
|
185 | + '***************'
|
186 | + '***************'
|
187 | + '***************'
|
188 | + '***************'
|
189 | + '***************'
|
190 | + '***************'
|
191 | + '***************'
|
192 | + '***************'
|
193 | + '***************'
|
194 | + '***************'
|
195 | + '***************'
|
196 | + '***************'
|
197 | + '***************'
|
198 | + '***************'
|
199 | + '***************',
|
200 | }),
|
201 | server: '8.8.8.8',
|
202 | timeout: 1000,
|
203 | });
|
204 | r.on('error', function (err) {
|
205 | test.ok(err, 'We should error because the packet failed to pack');
|
206 | didErr = true;
|
207 | });
|
208 | r.on('end', function () {
|
209 | test.ok(didErr, 'We did not err');
|
210 | test.done();
|
211 | });
|
212 | r.send();
|
213 | };
|
214 |
|
215 | exports.tearDown = function (cb) {
|
216 | cb();
|
217 | };
|