|
| 1 | +'use strict'; |
| 2 | +var common = require('../common'); |
| 3 | +var assert = require('assert'), |
| 4 | + dns = require('dns'), |
| 5 | + net = require('net'), |
| 6 | + isIP = net.isIP, |
| 7 | + isIPv4 = net.isIPv4; |
| 8 | +var util = require('util'); |
| 9 | + |
| 10 | +var expected = 0, |
| 11 | + completed = 0, |
| 12 | + running = false, |
| 13 | + queue = []; |
| 14 | + |
| 15 | +function TEST(f) { |
| 16 | + function next() { |
| 17 | + var f = queue.shift(); |
| 18 | + if (f) { |
| 19 | + running = true; |
| 20 | + console.log(f.name); |
| 21 | + f(done); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + function done() { |
| 26 | + running = false; |
| 27 | + completed++; |
| 28 | + process.nextTick(next); |
| 29 | + } |
| 30 | + |
| 31 | + expected++; |
| 32 | + queue.push(f); |
| 33 | + |
| 34 | + if (!running) { |
| 35 | + next(); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function checkWrap(req) { |
| 40 | + assert.ok(typeof req === 'object'); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(function test_resolve4(done) { |
| 44 | + var req = dns.resolve4('www.google.com', function(err, ips) { |
| 45 | + if (err) throw err; |
| 46 | + |
| 47 | + assert.ok(ips.length > 0); |
| 48 | + |
| 49 | + for (var i = 0; i < ips.length; i++) { |
| 50 | + assert.ok(isIPv4(ips[i])); |
| 51 | + } |
| 52 | + |
| 53 | + done(); |
| 54 | + }); |
| 55 | + |
| 56 | + checkWrap(req); |
| 57 | +}); |
| 58 | + |
| 59 | +TEST(function test_reverse_ipv4(done) { |
| 60 | + var req = dns.reverse('8.8.8.8', function(err, domains) { |
| 61 | + if (err) throw err; |
| 62 | + |
| 63 | + assert.ok(domains.length > 0); |
| 64 | + |
| 65 | + for (var i = 0; i < domains.length; i++) { |
| 66 | + assert.ok(domains[i]); |
| 67 | + assert.ok(typeof domains[i] === 'string'); |
| 68 | + } |
| 69 | + |
| 70 | + done(); |
| 71 | + }); |
| 72 | + |
| 73 | + checkWrap(req); |
| 74 | +}); |
| 75 | + |
| 76 | +TEST(function test_lookup_ipv4_explicit(done) { |
| 77 | + var req = dns.lookup('www.google.com', 4, function(err, ip, family) { |
| 78 | + if (err) throw err; |
| 79 | + assert.ok(net.isIPv4(ip)); |
| 80 | + assert.strictEqual(family, 4); |
| 81 | + |
| 82 | + done(); |
| 83 | + }); |
| 84 | + |
| 85 | + checkWrap(req); |
| 86 | +}); |
| 87 | + |
| 88 | +TEST(function test_lookup_ipv4_implicit(done) { |
| 89 | + var req = dns.lookup('www.google.com', function(err, ip, family) { |
| 90 | + if (err) throw err; |
| 91 | + assert.ok(net.isIPv4(ip)); |
| 92 | + assert.strictEqual(family, 4); |
| 93 | + |
| 94 | + done(); |
| 95 | + }); |
| 96 | + |
| 97 | + checkWrap(req); |
| 98 | +}); |
| 99 | + |
| 100 | +TEST(function test_lookup_ipv4_explicit_object(done) { |
| 101 | + var req = dns.lookup('www.google.com', { |
| 102 | + family: 4 |
| 103 | + }, function(err, ip, family) { |
| 104 | + if (err) throw err; |
| 105 | + assert.ok(net.isIPv4(ip)); |
| 106 | + assert.strictEqual(family, 4); |
| 107 | + |
| 108 | + done(); |
| 109 | + }); |
| 110 | + |
| 111 | + checkWrap(req); |
| 112 | +}); |
| 113 | + |
| 114 | +TEST(function test_lookup_ipv4_hint_addrconfig(done) { |
| 115 | + var req = dns.lookup('www.google.com', { |
| 116 | + hints: dns.ADDRCONFIG |
| 117 | + }, function(err, ip, family) { |
| 118 | + if (err) throw err; |
| 119 | + assert.ok(net.isIPv4(ip)); |
| 120 | + assert.strictEqual(family, 4); |
| 121 | + |
| 122 | + done(); |
| 123 | + }); |
| 124 | + |
| 125 | + checkWrap(req); |
| 126 | +}); |
| 127 | + |
| 128 | +TEST(function test_lookup_ip_ipv4(done) { |
| 129 | + var req = dns.lookup('127.0.0.1', function(err, ip, family) { |
| 130 | + if (err) throw err; |
| 131 | + assert.strictEqual(ip, '127.0.0.1'); |
| 132 | + assert.strictEqual(family, 4); |
| 133 | + |
| 134 | + done(); |
| 135 | + }); |
| 136 | + |
| 137 | + checkWrap(req); |
| 138 | +}); |
| 139 | + |
| 140 | +TEST(function test_lookup_localhost_ipv4(done) { |
| 141 | + var req = dns.lookup('localhost', 4, function(err, ip, family) { |
| 142 | + if (err) throw err; |
| 143 | + assert.strictEqual(ip, '127.0.0.1'); |
| 144 | + assert.strictEqual(family, 4); |
| 145 | + |
| 146 | + done(); |
| 147 | + }); |
| 148 | + |
| 149 | + checkWrap(req); |
| 150 | +}); |
| 151 | + |
| 152 | +TEST(function test_lookup_all_ipv4(done) { |
| 153 | + var req = dns.lookup('www.google.com', {all: true, family: 4}, |
| 154 | + function(err, ips) { |
| 155 | + if (err) throw err; |
| 156 | + assert.ok(Array.isArray(ips)); |
| 157 | + assert.ok(ips.length > 0); |
| 158 | + |
| 159 | + ips.forEach(function(ip) { |
| 160 | + assert.ok(isIPv4(ip.address)); |
| 161 | + assert.strictEqual(ip.family, 4); |
| 162 | + }); |
| 163 | + |
| 164 | + done(); |
| 165 | + }); |
| 166 | + |
| 167 | + checkWrap(req); |
| 168 | +}); |
| 169 | + |
| 170 | +TEST(function test_lookupservice_ip_ipv4(done) { |
| 171 | + var req = dns.lookupService('127.0.0.1', 80, function(err, host, service) { |
| 172 | + if (err) throw err; |
| 173 | + assert.equal(typeof host, 'string'); |
| 174 | + assert(host); |
| 175 | + |
| 176 | + /* |
| 177 | + * Retrieve the actual HTTP service name as setup on the host currently |
| 178 | + * running the test by reading it from /etc/services. This is not ideal, |
| 179 | + * as the service name lookup could use another mechanism (e.g nscd), but |
| 180 | + * it's already better than hardcoding it. |
| 181 | + */ |
| 182 | + var httpServiceName = common.getServiceName(80, 'tcp'); |
| 183 | + if (!httpServiceName) { |
| 184 | + /* |
| 185 | + * Couldn't find service name, reverting to the most sensible default |
| 186 | + * for port 80. |
| 187 | + */ |
| 188 | + httpServiceName = 'http'; |
| 189 | + } |
| 190 | + |
| 191 | + assert.strictEqual(service, httpServiceName); |
| 192 | + |
| 193 | + done(); |
| 194 | + }); |
| 195 | + |
| 196 | + checkWrap(req); |
| 197 | +}); |
| 198 | + |
| 199 | +process.on('exit', function() { |
| 200 | + console.log(completed + ' tests completed'); |
| 201 | + assert.equal(running, false); |
| 202 | + assert.strictEqual(expected, completed); |
| 203 | +}); |
0 commit comments