UNPKG

7.57 kBJavaScriptView Raw
1var mdns = require('./')
2var tape = require('tape')
3var dgram = require('dgram')
4
5var 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
16var configs = [
17 {ip: '127.0.0.1', multicast: false}
18 // {'interface': '127.0.0.1', multicast: true}
19]
20
21var 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
36tests.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: 'IN'})
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: 'IN'})
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: 'IN', 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: 'IN'})
86 t.same(packet.questions[1], {name: 'hej.verden', type: 'A', class: 'IN'})
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: 'IN', flush: false})
98 t.same(packet.answers[1], {type: 'A', name: 'hej.verden', ttl: 120, data: '127.0.0.2', class: 'IN', 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: 'IN'})
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: 'IN',
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: 'IN'})
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: 'IN',
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: 'IN'})
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: 'IN', flush: false})
174 dns.destroy(function () {
175 t.end()
176 })
177 })
178
179 dns.query('hello-world', 'TXT')
180 })
181
182 test('TXT array record', function (dns, t) {
183 var data = ['black', 'box']
184
185 dns.once('query', function (packet) {
186 t.same(packet.questions.length, 1, 'one question')
187 t.same(packet.questions[0], {name: 'hello-world', type: 'TXT', class: 'IN'})
188 dns.respond([{type: 'TXT', name: 'hello-world', ttl: 120, data: data}])
189 })
190
191 dns.once('response', function (packet) {
192 t.same(packet.answers.length, 1, 'one answer')
193 t.same(packet.answers[0], {type: 'TXT', name: 'hello-world', ttl: 120, data: data, class: 'IN', flush: false})
194 dns.destroy(function () {
195 t.end()
196 })
197 })
198
199 dns.query('hello-world', 'TXT')
200 })
201
202 test('QU question bit', function (dns, t) {
203 dns.once('query', function (packet) {
204 t.same(packet.questions, [
205 {type: 'A', name: 'foo', class: 'IN'},
206 {type: 'A', name: 'bar', class: 'IN'}
207 ])
208 dns.destroy(function () {
209 t.end()
210 })
211 })
212
213 dns.query([
214 {type: 'A', name: 'foo', class: 'IN'},
215 {type: 'A', name: 'bar', class: 'IN'}
216 ])
217 })
218
219 test('cache flush bit', function (dns, t) {
220 dns.once('query', function (packet) {
221 dns.respond({
222 answers: [
223 {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 'IN', flush: true},
224 {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 'IN', flush: false}
225 ],
226 additionals: [
227 {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 'IN', flush: true}
228 ]
229 })
230 })
231
232 dns.once('response', function (packet) {
233 t.same(packet.answers, [
234 {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 'IN', flush: true},
235 {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 'IN', flush: false}
236 ])
237 t.same(packet.additionals[0], {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 'IN', flush: true})
238 dns.destroy(function () {
239 t.end()
240 })
241 })
242
243 dns.query('foo', 'A')
244 })
245
246 test('Authoritive Answer bit', function (dns, t) {
247 dns.once('query', function (packet) {
248 dns.respond([])
249 })
250
251 dns.once('response', function (packet) {
252 t.ok(packet.flag_aa, 'should be set')
253 dns.destroy(function () {
254 t.end()
255 })
256 })
257
258 dns.query('foo', 'A')
259 })
260})