UNPKG

8.66 kBJavaScriptView Raw
1/*
2The follwoing code adapted from node's test-dns.js license is as follows
3*/
4
5// Copyright Joyent, Inc. and other Node contributors.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a
8// copy of this software and associated documentation files (the
9// "Software"), to deal in the Software without restriction, including
10// without limitation the rights to use, copy, modify, merge, publish,
11// distribute, sublicense, and/or sell copies of the Software, and to permit
12// persons to whom the Software is furnished to do so, subject to the
13// following conditions:
14//
15// The above copyright notice and this permission notice shall be included
16// in all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
21// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24// USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26var dns = require('../dns'),
27 net = require('net'),
28 isIP = net.isIP,
29 isIPv4 = net.isIPv4,
30 isIPv6 = net.isIPv6;
31
32var platform = require('../lib/platform');
33
34function checkWrap(test, req) {
35 test.ok(typeof req === 'object');
36}
37
38var oldServers;
39var oldPath;
40
41var fixupDns = function (cb) {
42 oldServers = platform.name_servers;
43 oldPath = platform.search_path;
44
45 /* Force queries to google */
46 platform.name_servers = [{
47 address: '8.8.8.8',
48 port: 53,
49 }];
50
51 /* Don't bother trying to search for queries */
52 platform.search_path = [];
53
54 cb();
55};
56
57exports.setUp = function (cb) {
58 /* wait for up to 5 ticks so /etc/resolv.conf can be parsed */
59 var ticks = 5;
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
79exports.tearDown = function (cb) {
80 platform.name_servers = oldServers;
81 platform.search_path = oldPath;
82 cb();
83};
84
85exports.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
102exports.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
119exports.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
137exports.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
155exports.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
173exports.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
197exports.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
216exports.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
242exports.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
261exports.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
273exports.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
286exports.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
299exports.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// TODO XXX FIXME Doesn't google require ipv6 for this test to pass?
313exports.lookup_ipv6_implicit = function (test) {
314 var req = dns.lookup('ipv6.google.com', function(err, ip, family) {
315 test.ifError(err);
316 test.ok(net.isIPv6(ip));
317 test.strictEqual(family, 6);
318
319 test.done();
320 });
321
322 checkWrap(test, req);
323};
324//*/
325
326exports.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
338exports.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
351exports.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//*
365exports.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
379exports.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
392/* Disabled because it appears to be not working on linux. */
393/*
394exports.lookup_localhost_ipv6 = function (test) {
395 var req = dns.lookup('localhost', 6, function(err, ip, family) {
396 test.ifError(err);
397 test.ok(net.isIPv6(ip));
398 test.strictEqual(family, 6);
399
400 test.done();
401 });
402
403 checkWrap(test, req);
404};
405//*/