UNPKG

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