1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var dns = require('../dns'),
|
27 | net = require('net'),
|
28 | isIP = net.isIP,
|
29 | isIPv4 = net.isIPv4,
|
30 | isIPv6 = net.isIPv6;
|
31 |
|
32 | var platform = require('../lib/platform');
|
33 |
|
34 | function checkWrap(test, req) {
|
35 | test.ok(typeof req === 'object');
|
36 | }
|
37 |
|
38 | var oldServers;
|
39 | var oldPath;
|
40 |
|
41 | var fixupDns = function (cb) {
|
42 | oldServers = platform.name_servers;
|
43 | oldPath = platform.search_path;
|
44 |
|
45 |
|
46 | platform.name_servers = [{
|
47 | address: '8.8.8.8',
|
48 | port: 53,
|
49 | }];
|
50 |
|
51 |
|
52 | platform.search_path = [];
|
53 |
|
54 | cb();
|
55 | };
|
56 |
|
57 | exports.setUp = function (cb) {
|
58 |
|
59 | var ticks = 100;
|
60 |
|
61 | var checkReady = function () {
|
62 | process.nextTick(function () {
|
63 | if (platform.ready) {
|
64 | fixupDns(cb);
|
65 | } else {
|
66 | ticks -= 1;
|
67 | if (ticks > 0)
|
68 | checkReady();
|
69 | }
|
70 | })
|
71 | }
|
72 |
|
73 | if (platform.ready)
|
74 | fixupDns(cb);
|
75 | else
|
76 | checkReady();
|
77 | };
|
78 |
|
79 | exports.tearDown = function (cb) {
|
80 | platform.name_servers = oldServers;
|
81 | platform.search_path = oldPath;
|
82 | cb();
|
83 | };
|
84 |
|
85 | exports.resolve4 = function (test) {
|
86 | var req = dns.resolve4('www.google.com', function(err, ips) {
|
87 | test.ifError(err);
|
88 |
|
89 | test.ok(ips.length > 0);
|
90 |
|
91 | for (var i = 0; i < ips.length; i++) {
|
92 | test.ok(isIPv4(ips[i]));
|
93 | }
|
94 |
|
95 | test.done();
|
96 | });
|
97 |
|
98 | checkWrap(test, req);
|
99 | };
|
100 |
|
101 |
|
102 | exports.resolve6 = function (test) {
|
103 | var req = dns.resolve6('ipv6.google.com', function(err, ips) {
|
104 | test.ifError(err);
|
105 |
|
106 | test.ok(ips.length > 0);
|
107 |
|
108 | for (var i = 0; i < ips.length; i++) {
|
109 | test.ok(isIPv6(ips[i]));
|
110 | }
|
111 |
|
112 | test.done();
|
113 | });
|
114 |
|
115 | checkWrap(test, req);
|
116 | };
|
117 |
|
118 |
|
119 | exports.reverse_ipv4 = function (test) {
|
120 | var req = dns.reverse('8.8.8.8', function(err, domains) {
|
121 | test.ifError(err);
|
122 |
|
123 | test.ok(domains.length > 0);
|
124 |
|
125 | for (var i = 0; i < domains.length; i++) {
|
126 | test.ok(domains[i]);
|
127 | test.ok(typeof domains[i] === 'string');
|
128 | }
|
129 |
|
130 | test.done();
|
131 | });
|
132 |
|
133 | checkWrap(test, req);
|
134 | };
|
135 |
|
136 |
|
137 | exports.reverse_ipv6 = function (test) {
|
138 | var req = dns.reverse('2001:4860:4860::8888', function(err, domains) {
|
139 | test.ifError(err);
|
140 |
|
141 | test.ok(domains.length > 0);
|
142 |
|
143 | for (var i = 0; i < domains.length; i++) {
|
144 | test.ok(domains[i]);
|
145 | test.ok(typeof domains[i] === 'string');
|
146 | }
|
147 |
|
148 | test.done();
|
149 | });
|
150 |
|
151 | checkWrap(test, req);
|
152 | };
|
153 |
|
154 |
|
155 | exports.reverse_bogus = function (test) {
|
156 | var error;
|
157 |
|
158 | try {
|
159 | var req = dns.reverse('bogus ip', function() {
|
160 | test.ok(false);
|
161 | });
|
162 | } catch (e) {
|
163 | error = e;
|
164 | }
|
165 |
|
166 | test.ok(error instanceof Error);
|
167 | test.strictEqual(error.errno, 'ENOTIMP');
|
168 |
|
169 | test.done();
|
170 | };
|
171 |
|
172 |
|
173 | exports.resolveMx = function (test) {
|
174 | var req = dns.resolveMx('gmail.com', function(err, result) {
|
175 | test.ifError(err);
|
176 |
|
177 | test.ok(result.length > 0);
|
178 |
|
179 | for (var i = 0; i < result.length; i++) {
|
180 | var item = result[i];
|
181 | test.ok(item);
|
182 | test.ok(typeof item === 'object');
|
183 |
|
184 | test.ok(item.exchange);
|
185 | test.ok(typeof item.exchange === 'string');
|
186 |
|
187 | test.ok(typeof item.priority === 'number');
|
188 | }
|
189 |
|
190 | test.done();
|
191 | });
|
192 |
|
193 | checkWrap(test, req);
|
194 | };
|
195 |
|
196 |
|
197 | exports.resolveNs = function (test) {
|
198 | var req = dns.resolveNs('rackspace.com', function(err, names) {
|
199 | test.ifError(err);
|
200 |
|
201 | test.ok(names.length > 0);
|
202 |
|
203 | for (var i = 0; i < names.length; i++) {
|
204 | var name = names[i];
|
205 | test.ok(name);
|
206 | test.ok(typeof name === 'string');
|
207 | }
|
208 |
|
209 | test.done();
|
210 | });
|
211 |
|
212 | checkWrap(test, req);
|
213 | };
|
214 |
|
215 |
|
216 | exports.resolveSrv = function (test) {
|
217 | var req = dns.resolveSrv('_jabber._tcp.google.com', function(err, result) {
|
218 | test.ifError(err);
|
219 |
|
220 | test.ok(result.length > 0);
|
221 |
|
222 | for (var i = 0; i < result.length; i++) {
|
223 | var item = result[i];
|
224 | test.ok(item);
|
225 | test.ok(typeof item === 'object');
|
226 |
|
227 | test.ok(item.name);
|
228 | test.ok(typeof item.name === 'string');
|
229 |
|
230 | test.ok(typeof item.port === 'number');
|
231 | test.ok(typeof item.priority === 'number');
|
232 | test.ok(typeof item.weight === 'number');
|
233 | }
|
234 |
|
235 | test.done();
|
236 | });
|
237 |
|
238 | checkWrap(test, req);
|
239 | };
|
240 |
|
241 |
|
242 | exports.resolveCname = function (test) {
|
243 | var req = dns.resolveCname('www.google.com', function(err, names) {
|
244 | test.ifError(err);
|
245 |
|
246 | test.ok(names.length > 0);
|
247 |
|
248 | for (var i = 0; i < names.length; i++) {
|
249 | var name = names[i];
|
250 | test.ok(name);
|
251 | test.ok(typeof name === 'string');
|
252 | }
|
253 |
|
254 | test.done();
|
255 | });
|
256 |
|
257 | checkWrap(test, req);
|
258 | };
|
259 |
|
260 |
|
261 | exports.resolveTxt = function (test) {
|
262 | var req = dns.resolveTxt('google.com', function(err, records) {
|
263 | test.ifError(err);
|
264 | test.equal(records.length, 1);
|
265 | test.equal(records[0].indexOf('v=spf1'), 0);
|
266 | test.done();
|
267 | });
|
268 |
|
269 | checkWrap(test, req);
|
270 | };
|
271 |
|
272 |
|
273 | exports.lookup_ipv4_explicit = function (test) {
|
274 | var req = dns.lookup('www.google.com', 4, function(err, ip, family) {
|
275 | test.ifError(err);
|
276 | test.ok(net.isIPv4(ip));
|
277 | test.strictEqual(family, 4);
|
278 |
|
279 | test.done();
|
280 | });
|
281 |
|
282 | checkWrap(test, req);
|
283 | };
|
284 |
|
285 |
|
286 | exports.lookup_ipv4_implicit = function (test) {
|
287 | var req = dns.lookup('www.google.com', function(err, ip, family) {
|
288 | test.ifError(err);
|
289 | test.ok(net.isIPv4(ip));
|
290 | test.strictEqual(family, 4);
|
291 |
|
292 | test.done();
|
293 | });
|
294 |
|
295 | checkWrap(test, req);
|
296 | };
|
297 |
|
298 |
|
299 | exports.lookup_ipv6_explicit = function (test) {
|
300 | var req = dns.lookup('ipv6.google.com', 6, function(err, ip, family) {
|
301 | test.ifError(err);
|
302 | test.ok(net.isIPv6(ip));
|
303 | test.strictEqual(family, 6);
|
304 |
|
305 | test.done();
|
306 | });
|
307 |
|
308 | checkWrap(test, req);
|
309 | };
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | exports.lookup_failure = function (test) {
|
327 | var req = dns.lookup('does.not.exist', 4, function(err, ip, family) {
|
328 | test.ok(err instanceof Error);
|
329 | test.strictEqual(err.errno, dns.NOTFOUND);
|
330 | test.strictEqual(err.errno, 'ENOTFOUND');
|
331 |
|
332 | test.done();
|
333 | });
|
334 |
|
335 | checkWrap(test, req);
|
336 | };
|
337 |
|
338 | exports.lookup_null = function (test) {
|
339 | var req = dns.lookup(null, function(err, ip, family) {
|
340 | test.ifError(err);
|
341 | test.strictEqual(ip, null);
|
342 | test.strictEqual(family, 4);
|
343 |
|
344 | test.done();
|
345 | });
|
346 |
|
347 | checkWrap(test, req);
|
348 | };
|
349 |
|
350 |
|
351 | exports.lookup_ip_ipv4 = function (test) {
|
352 | var req = dns.lookup('127.0.0.1', function(err, ip, family) {
|
353 | test.ifError(err);
|
354 | test.strictEqual(ip, '127.0.0.1');
|
355 | test.strictEqual(family, 4);
|
356 |
|
357 | test.done();
|
358 | });
|
359 |
|
360 | checkWrap(test, req);
|
361 | };
|
362 |
|
363 |
|
364 |
|
365 | exports.lookup_ip_ipv6 = function (test) {
|
366 | var req = dns.lookup('::1', function(err, ip, family) {
|
367 | test.ifError(err);
|
368 | test.ok(net.isIPv6(ip));
|
369 | test.strictEqual(family, 6);
|
370 |
|
371 | test.done();
|
372 | });
|
373 |
|
374 | checkWrap(test, req);
|
375 | };
|
376 |
|
377 |
|
378 |
|
379 | exports.lookup_localhost_ipv4 = function (test) {
|
380 | var req = dns.lookup('localhost', 4, function(err, ip, family) {
|
381 | test.ifError(err);
|
382 | test.strictEqual(ip, '127.0.0.1');
|
383 | test.strictEqual(family, 4);
|
384 |
|
385 | test.done();
|
386 | });
|
387 |
|
388 | checkWrap(test, req);
|
389 | };
|
390 |
|
391 | exports.lookup_longname = function (test) {
|
392 | var name, request;
|
393 |
|
394 | var name = '************';
|
395 | name += name + '***************'
|
396 | name += name + '***************'
|
397 | name += name + '***************'
|
398 | name += name + '***************'
|
399 | name += name + '***************'
|
400 |
|
401 | request = dns.lookup(name, 4, function (err, ip, family) {
|
402 | test.ok(err, "we should fail");
|
403 | test.strictEqual(err.errno, dns.NOTFOUND);
|
404 |
|
405 | test.done();
|
406 | });
|
407 | checkWrap(test, request);
|
408 | };
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|