[6a3a178] | 1 | var mdns = require('./')
|
---|
| 2 | var tape = require('tape')
|
---|
| 3 | var dgram = require('dgram')
|
---|
| 4 |
|
---|
| 5 | var port = function (cb) {
|
---|
| 6 | var s = dgram.createSocket('udp4')
|
---|
| 7 | s.bind(0, function () {
|
---|
| 8 | var port = s.address().port
|
---|
| 9 | s.on('close', function () {
|
---|
| 10 | cb(port)
|
---|
| 11 | })
|
---|
| 12 | s.close()
|
---|
| 13 | })
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | var configs = [
|
---|
| 17 | {ip: '127.0.0.1', multicast: false}
|
---|
| 18 | // {'interface': '127.0.0.1', multicast: true}
|
---|
| 19 | ]
|
---|
| 20 |
|
---|
| 21 | var tests = configs.map(function (config) {
|
---|
| 22 | return function (name, fn) {
|
---|
| 23 | tape(name, function (t) {
|
---|
| 24 | port(function (p) {
|
---|
| 25 | config.port = p
|
---|
| 26 | var dns = mdns(config)
|
---|
| 27 | dns.on('warning', function (e) {
|
---|
| 28 | t.error(e)
|
---|
| 29 | })
|
---|
| 30 | fn(dns, t)
|
---|
| 31 | })
|
---|
| 32 | })
|
---|
| 33 | }
|
---|
| 34 | })
|
---|
| 35 |
|
---|
| 36 | tests.forEach(function (test) {
|
---|
| 37 | test('works', function (dns, t) {
|
---|
| 38 | t.plan(3)
|
---|
| 39 |
|
---|
| 40 | dns.once('query', function (packet) {
|
---|
| 41 | t.same(packet.type, 'query')
|
---|
| 42 | dns.destroy(function () {
|
---|
| 43 | t.ok(true, 'destroys')
|
---|
| 44 | })
|
---|
| 45 | })
|
---|
| 46 |
|
---|
| 47 | dns.query('hello-world', function () {
|
---|
| 48 | t.ok(true, 'flushed')
|
---|
| 49 | })
|
---|
| 50 | })
|
---|
| 51 |
|
---|
| 52 | test('ANY query', function (dns, t) {
|
---|
| 53 | dns.once('query', function (packet) {
|
---|
| 54 | t.same(packet.questions.length, 1, 'one question')
|
---|
| 55 | t.same(packet.questions[0], {name: 'hello-world', type: 'ANY', class: 1})
|
---|
| 56 | dns.destroy(function () {
|
---|
| 57 | t.end()
|
---|
| 58 | })
|
---|
| 59 | })
|
---|
| 60 |
|
---|
| 61 | dns.query('hello-world', 'ANY')
|
---|
| 62 | })
|
---|
| 63 |
|
---|
| 64 | test('A record', function (dns, t) {
|
---|
| 65 | dns.once('query', function (packet) {
|
---|
| 66 | t.same(packet.questions.length, 1, 'one question')
|
---|
| 67 | t.same(packet.questions[0], {name: 'hello-world', type: 'A', class: 1})
|
---|
| 68 | dns.respond([{type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1'}])
|
---|
| 69 | })
|
---|
| 70 |
|
---|
| 71 | dns.once('response', function (packet) {
|
---|
| 72 | t.same(packet.answers.length, 1, 'one answer')
|
---|
| 73 | t.same(packet.answers[0], {type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1', class: 1, flush: false})
|
---|
| 74 | dns.destroy(function () {
|
---|
| 75 | t.end()
|
---|
| 76 | })
|
---|
| 77 | })
|
---|
| 78 |
|
---|
| 79 | dns.query('hello-world', 'A')
|
---|
| 80 | })
|
---|
| 81 |
|
---|
| 82 | test('A record (two questions)', function (dns, t) {
|
---|
| 83 | dns.once('query', function (packet) {
|
---|
| 84 | t.same(packet.questions.length, 2, 'two questions')
|
---|
| 85 | t.same(packet.questions[0], {name: 'hello-world', type: 'A', class: 1})
|
---|
| 86 | t.same(packet.questions[1], {name: 'hej.verden', type: 'A', class: 1})
|
---|
| 87 | dns.respond([{type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1'}, {
|
---|
| 88 | type: 'A',
|
---|
| 89 | name: 'hej.verden',
|
---|
| 90 | ttl: 120,
|
---|
| 91 | data: '127.0.0.2'
|
---|
| 92 | }])
|
---|
| 93 | })
|
---|
| 94 |
|
---|
| 95 | dns.once('response', function (packet) {
|
---|
| 96 | t.same(packet.answers.length, 2, 'one answers')
|
---|
| 97 | t.same(packet.answers[0], {type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1', class: 1, flush: false})
|
---|
| 98 | t.same(packet.answers[1], {type: 'A', name: 'hej.verden', ttl: 120, data: '127.0.0.2', class: 1, flush: false})
|
---|
| 99 | dns.destroy(function () {
|
---|
| 100 | t.end()
|
---|
| 101 | })
|
---|
| 102 | })
|
---|
| 103 |
|
---|
| 104 | dns.query([{name: 'hello-world', type: 'A'}, {name: 'hej.verden', type: 'A'}])
|
---|
| 105 | })
|
---|
| 106 |
|
---|
| 107 | test('AAAA record', function (dns, t) {
|
---|
| 108 | dns.once('query', function (packet) {
|
---|
| 109 | t.same(packet.questions.length, 1, 'one question')
|
---|
| 110 | t.same(packet.questions[0], {name: 'hello-world', type: 'AAAA', class: 1})
|
---|
| 111 | dns.respond([{type: 'AAAA', name: 'hello-world', ttl: 120, data: 'fe80::5ef9:38ff:fe8c:ceaa'}])
|
---|
| 112 | })
|
---|
| 113 |
|
---|
| 114 | dns.once('response', function (packet) {
|
---|
| 115 | t.same(packet.answers.length, 1, 'one answer')
|
---|
| 116 | t.same(packet.answers[0], {
|
---|
| 117 | type: 'AAAA',
|
---|
| 118 | name: 'hello-world',
|
---|
| 119 | ttl: 120,
|
---|
| 120 | data: 'fe80::5ef9:38ff:fe8c:ceaa',
|
---|
| 121 | class: 1,
|
---|
| 122 | flush: false
|
---|
| 123 | })
|
---|
| 124 | dns.destroy(function () {
|
---|
| 125 | t.end()
|
---|
| 126 | })
|
---|
| 127 | })
|
---|
| 128 |
|
---|
| 129 | dns.query('hello-world', 'AAAA')
|
---|
| 130 | })
|
---|
| 131 |
|
---|
| 132 | test('SRV record', function (dns, t) {
|
---|
| 133 | dns.once('query', function (packet) {
|
---|
| 134 | t.same(packet.questions.length, 1, 'one question')
|
---|
| 135 | t.same(packet.questions[0], {name: 'hello-world', type: 'SRV', class: 1})
|
---|
| 136 | dns.respond([{
|
---|
| 137 | type: 'SRV',
|
---|
| 138 | name: 'hello-world',
|
---|
| 139 | ttl: 120,
|
---|
| 140 | data: {port: 11111, target: 'hello.world.com', priority: 10, weight: 12}
|
---|
| 141 | }])
|
---|
| 142 | })
|
---|
| 143 |
|
---|
| 144 | dns.once('response', function (packet) {
|
---|
| 145 | t.same(packet.answers.length, 1, 'one answer')
|
---|
| 146 | t.same(packet.answers[0], {
|
---|
| 147 | type: 'SRV',
|
---|
| 148 | name: 'hello-world',
|
---|
| 149 | ttl: 120,
|
---|
| 150 | data: {port: 11111, target: 'hello.world.com', priority: 10, weight: 12},
|
---|
| 151 | class: 1,
|
---|
| 152 | flush: false
|
---|
| 153 | })
|
---|
| 154 | dns.destroy(function () {
|
---|
| 155 | t.end()
|
---|
| 156 | })
|
---|
| 157 | })
|
---|
| 158 |
|
---|
| 159 | dns.query('hello-world', 'SRV')
|
---|
| 160 | })
|
---|
| 161 |
|
---|
| 162 | test('TXT record', function (dns, t) {
|
---|
| 163 | var data = Buffer.from('black box')
|
---|
| 164 |
|
---|
| 165 | dns.once('query', function (packet) {
|
---|
| 166 | t.same(packet.questions.length, 1, 'one question')
|
---|
| 167 | t.same(packet.questions[0], {name: 'hello-world', type: 'TXT', class: 1})
|
---|
| 168 | dns.respond([{type: 'TXT', name: 'hello-world', ttl: 120, data: data}])
|
---|
| 169 | })
|
---|
| 170 |
|
---|
| 171 | dns.once('response', function (packet) {
|
---|
| 172 | t.same(packet.answers.length, 1, 'one answer')
|
---|
| 173 | t.same(packet.answers[0], {type: 'TXT', name: 'hello-world', ttl: 120, data: data, class: 1, flush: false})
|
---|
| 174 | dns.destroy(function () {
|
---|
| 175 | t.end()
|
---|
| 176 | })
|
---|
| 177 | })
|
---|
| 178 |
|
---|
| 179 | dns.query('hello-world', 'TXT')
|
---|
| 180 | })
|
---|
| 181 |
|
---|
| 182 | test('QU question bit', function (dns, t) {
|
---|
| 183 | dns.once('query', function (packet) {
|
---|
| 184 | t.same(packet.questions, [
|
---|
| 185 | {type: 'A', name: 'foo', class: 1},
|
---|
| 186 | {type: 'A', name: 'bar', class: 1}
|
---|
| 187 | ])
|
---|
| 188 | dns.destroy(function () {
|
---|
| 189 | t.end()
|
---|
| 190 | })
|
---|
| 191 | })
|
---|
| 192 |
|
---|
| 193 | dns.query([
|
---|
| 194 | {type: 'A', name: 'foo', class: 32769},
|
---|
| 195 | {type: 'A', name: 'bar', class: 1}
|
---|
| 196 | ])
|
---|
| 197 | })
|
---|
| 198 |
|
---|
| 199 | test('cache flush bit', function (dns, t) {
|
---|
| 200 | dns.once('query', function (packet) {
|
---|
| 201 | dns.respond({
|
---|
| 202 | answers: [
|
---|
| 203 | {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 1, flush: true},
|
---|
| 204 | {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 1, flush: false}
|
---|
| 205 | ],
|
---|
| 206 | additionals: [
|
---|
| 207 | {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 1, flush: true}
|
---|
| 208 | ]
|
---|
| 209 | })
|
---|
| 210 | })
|
---|
| 211 |
|
---|
| 212 | dns.once('response', function (packet) {
|
---|
| 213 | t.same(packet.answers, [
|
---|
| 214 | {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 1, flush: true},
|
---|
| 215 | {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 1, flush: false}
|
---|
| 216 | ])
|
---|
| 217 | t.same(packet.additionals[0], {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 1, flush: true})
|
---|
| 218 | dns.destroy(function () {
|
---|
| 219 | t.end()
|
---|
| 220 | })
|
---|
| 221 | })
|
---|
| 222 |
|
---|
| 223 | dns.query('foo', 'A')
|
---|
| 224 | })
|
---|
| 225 |
|
---|
| 226 | test('Authoritive Answer bit', function (dns, t) {
|
---|
| 227 | dns.once('query', function (packet) {
|
---|
| 228 | dns.respond([])
|
---|
| 229 | })
|
---|
| 230 |
|
---|
| 231 | dns.once('response', function (packet) {
|
---|
| 232 | t.ok(packet.flag_auth, 'should be set')
|
---|
| 233 | dns.destroy(function () {
|
---|
| 234 | t.end()
|
---|
| 235 | })
|
---|
| 236 | })
|
---|
| 237 |
|
---|
| 238 | dns.query('foo', 'A')
|
---|
| 239 | })
|
---|
| 240 | })
|
---|