forked from mafintosh/multicast-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
191 lines (163 loc) · 5.72 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var mdns = require('./')
var tape = require('tape')
var dgram = require('dgram')
var port = function (cb) {
var s = dgram.createSocket('udp4')
s.bind(0, function () {
var port = s.address().port
s.on('close', function () {
cb(port)
})
s.close()
})
}
var test = function (name, fn) {
tape(name, function (t) {
port(function (p) {
var dns = mdns({ip: '127.0.0.1', port: p, multicast: false})
dns.on('warning', function (e) {
t.error(e)
})
fn(dns, t)
})
})
}
test('works', function (dns, t) {
t.plan(3)
dns.once('query', function (packet) {
t.same(packet.type, 'query')
dns.destroy(function () {
t.ok(true, 'destroys')
})
})
dns.query('hello-world', function () {
t.ok(true, 'flushed')
})
})
test('ANY query', function (dns, t) {
dns.once('query', function (packet) {
t.same(packet.questions.length, 1, 'one question')
t.same(packet.questions[0], {name: 'hello-world', type: 'ANY', class: 1})
dns.destroy(function () {
t.end()
})
})
dns.query('hello-world', 'ANY')
})
test('A record', function (dns, t) {
dns.once('query', function (packet) {
t.same(packet.questions.length, 1, 'one question')
t.same(packet.questions[0], {name: 'hello-world', type: 'A', class: 1})
dns.respond([{type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1'}])
})
dns.once('response', function (packet) {
t.same(packet.answers.length, 1, 'one answer')
t.same(packet.answers[0], {type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1', class: 1, flush: false})
dns.destroy(function () {
t.end()
})
})
dns.query('hello-world', 'A')
})
test('A record (two questions)', function (dns, t) {
dns.once('query', function (packet) {
t.same(packet.questions.length, 2, 'two questions')
t.same(packet.questions[0], {name: 'hello-world', type: 'A', class: 1})
t.same(packet.questions[1], {name: 'hej.verden', type: 'A', class: 1})
dns.respond([{type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1'}, {type: 'A', name: 'hej.verden', ttl: 120, data: '127.0.0.2'}])
})
dns.once('response', function (packet) {
t.same(packet.answers.length, 2, 'one answers')
t.same(packet.answers[0], {type: 'A', name: 'hello-world', ttl: 120, data: '127.0.0.1', class: 1, flush: false})
t.same(packet.answers[1], {type: 'A', name: 'hej.verden', ttl: 120, data: '127.0.0.2', class: 1, flush: false})
dns.destroy(function () {
t.end()
})
})
dns.query([{name: 'hello-world', type: 'A'}, {name: 'hej.verden', type: 'A'}])
})
test('AAAA record', function (dns, t) {
dns.once('query', function (packet) {
t.same(packet.questions.length, 1, 'one question')
t.same(packet.questions[0], {name: 'hello-world', type: 'AAAA', class: 1})
dns.respond([{type: 'AAAA', name: 'hello-world', ttl: 120, data: 'fe80::5ef9:38ff:fe8c:ceaa'}])
})
dns.once('response', function (packet) {
t.same(packet.answers.length, 1, 'one answer')
t.same(packet.answers[0], {type: 'AAAA', name: 'hello-world', ttl: 120, data: 'fe80::5ef9:38ff:fe8c:ceaa', class: 1, flush: false})
dns.destroy(function () {
t.end()
})
})
dns.query('hello-world', 'AAAA')
})
test('SRV record', function (dns, t) {
dns.once('query', function (packet) {
t.same(packet.questions.length, 1, 'one question')
t.same(packet.questions[0], {name: 'hello-world', type: 'SRV', class: 1})
dns.respond([{type: 'SRV', name: 'hello-world', ttl: 120, data: {port: 11111, target: 'hello.world.com', priority: 10, weight: 12}}])
})
dns.once('response', function (packet) {
t.same(packet.answers.length, 1, 'one answer')
t.same(packet.answers[0], {type: 'SRV', name: 'hello-world', ttl: 120, data: {port: 11111, target: 'hello.world.com', priority: 10, weight: 12}, class: 1, flush: false})
dns.destroy(function () {
t.end()
})
})
dns.query('hello-world', 'SRV')
})
test('TXT record', function (dns, t) {
var data = new Buffer('black box')
dns.once('query', function (packet) {
t.same(packet.questions.length, 1, 'one question')
t.same(packet.questions[0], {name: 'hello-world', type: 'TXT', class: 1})
dns.respond([{type: 'TXT', name: 'hello-world', ttl: 120, data: data}])
})
dns.once('response', function (packet) {
t.same(packet.answers.length, 1, 'one answer')
t.same(packet.answers[0], {type: 'TXT', name: 'hello-world', ttl: 120, data: data, class: 1, flush: false})
dns.destroy(function () {
t.end()
})
})
dns.query('hello-world', 'TXT')
})
test('QU question bit', function (dns, t) {
dns.once('query', function (packet) {
t.same(packet.questions, [
{type: 'A', name: 'foo', class: 1},
{type: 'A', name: 'bar', class: 1}
])
dns.destroy(function () {
t.end()
})
})
dns.query([
{type: 'A', name: 'foo', class: 32769},
{type: 'A', name: 'bar', class: 1}
])
})
test('cache flush bit', function (dns, t) {
dns.once('query', function (packet) {
dns.respond({
answers: [
{type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 1, flush: true},
{type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 1, flush: false}
],
additionals: [
{type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 1, flush: true}
]
})
})
dns.once('response', function (packet) {
t.same(packet.answers, [
{type: 'A', name: 'foo', ttl: 120, data: '127.0.0.1', class: 1, flush: true},
{type: 'A', name: 'foo', ttl: 120, data: '127.0.0.2', class: 1, flush: false}
])
t.same(packet.additionals[0], {type: 'A', name: 'foo', ttl: 120, data: '127.0.0.3', class: 1, flush: true})
dns.destroy(function () {
t.end()
})
})
dns.query('foo', 'A')
})