1 | var dns = require('../dns'),
|
2 | Request = dns.Request,
|
3 | Question = dns.Question,
|
4 | consts = dns.consts,
|
5 | dgram = require('dgram');
|
6 |
|
7 | var q = Question({
|
8 | name: 'www.google.com',
|
9 | type: consts.NAME_TO_QTYPE.A,
|
10 | });
|
11 |
|
12 | var noServer = {
|
13 | address: '127.0.0.1',
|
14 | port: 19999,
|
15 | type: 'udp',
|
16 | };
|
17 |
|
18 | var udpServ = {
|
19 | address: '8.8.8.8',
|
20 | port: 53,
|
21 | type: 'udp',
|
22 | };
|
23 |
|
24 | var tcpServ = {
|
25 | address: '8.8.8.8',
|
26 | port: 53,
|
27 | type: 'tcp',
|
28 | };
|
29 |
|
30 | exports.setUp = function (cb) {
|
31 | cb();
|
32 | };
|
33 |
|
34 | exports.timeout = function (test) {
|
35 | var r = Request({
|
36 | question: q,
|
37 | server: noServer,
|
38 | timeout: 100,
|
39 | cache: false,
|
40 | });
|
41 |
|
42 | var timedout = false;
|
43 |
|
44 | r.on('timeout', function () {
|
45 | timedout = true;
|
46 | });
|
47 |
|
48 | r.on('end', function () {
|
49 | test.ok(timedout, 'Failed to timeout');
|
50 | test.done();
|
51 | });
|
52 |
|
53 | r.send();
|
54 | };
|
55 |
|
56 | exports.udpResponse = function (test) {
|
57 | var r = Request({
|
58 | question: q,
|
59 | server: udpServ,
|
60 | timeout: 4000,
|
61 | });
|
62 |
|
63 | r.on('message', function (err, answer) {
|
64 | test.ok(!err, 'UDP Request should not error');
|
65 | test.ok(answer, 'No UDP answer provided');
|
66 | test.ok(answer.answer.length > 0, 'No answers found');
|
67 | });
|
68 |
|
69 | r.on('timeout', function () {
|
70 | test.ok(false, 'UDP Requests should not timeout');
|
71 | });
|
72 |
|
73 | r.on('end', function () {
|
74 | test.done();
|
75 | });
|
76 |
|
77 | r.send();
|
78 | };
|
79 |
|
80 | exports.tcpResponse = function (test) {
|
81 | var r = Request({
|
82 | question: q,
|
83 | server: tcpServ,
|
84 | timeout: 4000,
|
85 | });
|
86 |
|
87 | r.on('message', function (err, answer) {
|
88 | test.ok(!err, 'TCP Request should not error');
|
89 | test.ok(answer, 'No TCP answer provided');
|
90 | test.ok(answer.answer.length > 0, 'No answers found');
|
91 | });
|
92 |
|
93 | r.on('timeout', function () {
|
94 | test.ok(false, 'TCP Requests should not timeout');
|
95 | });
|
96 |
|
97 | r.on('end', function () {
|
98 | test.done();
|
99 | });
|
100 |
|
101 | r.send();
|
102 | };
|
103 |
|
104 | exports.serverString = function (test) {
|
105 | var r = Request({
|
106 | question: q,
|
107 | server: '8.8.8.8',
|
108 | });
|
109 |
|
110 | r.on('message', function (err, answer) {
|
111 | test.ok(answer.answer.length > 0, 'no answers found');
|
112 | });
|
113 |
|
114 | r.on('timeout', function () {
|
115 | test.ok(false, 'Should not timeout');
|
116 | });
|
117 |
|
118 | r.on('end', function () {
|
119 | test.done();
|
120 | });
|
121 |
|
122 | r.send();
|
123 | };
|
124 |
|
125 | exports.questionString = function (test) {
|
126 | var r = Request({
|
127 | question: Question({
|
128 | name: 'www.google.com',
|
129 | type: 'a',
|
130 | }),
|
131 | server: '8.8.8.8',
|
132 | });
|
133 |
|
134 | r.on('message', function (err, answer) {
|
135 | test.ok(answer.answer.length > 0, 'no answers found');
|
136 | });
|
137 |
|
138 | r.on('timeout', function () {
|
139 | test.ok(false, 'Should not timeout');
|
140 | });
|
141 |
|
142 | r.on('end', function () {
|
143 | test.done();
|
144 | });
|
145 |
|
146 | r.send();
|
147 | };
|
148 |
|
149 | exports.emptyUdp = function (test) {
|
150 | var socket = dgram.createSocket('udp4');
|
151 | socket.on('listening', function () {
|
152 | var timeout = false;
|
153 | var r = Request({
|
154 | question: q,
|
155 | server: { address: '127.0.0.1', port: socket.address().port, type: 'udp' },
|
156 | timeout: 300,
|
157 | cache: false,
|
158 | });
|
159 | r.on('message', function () {
|
160 | test.ok(false, 'There should not be a response');
|
161 | });
|
162 | r.on('timeout', function () {
|
163 | timeout = true;
|
164 | });
|
165 | r.on('end', function () {
|
166 | test.ok(timeout, 'This should timeout');
|
167 | socket.close();
|
168 | test.done();
|
169 | });
|
170 | r.send();
|
171 | });
|
172 | socket.on('message', function (msg, remote) {
|
173 | socket.send(new Buffer(1), 0, 1, remote.port, remote.address);
|
174 | });
|
175 | socket.bind();
|
176 | };
|
177 |
|
178 | exports.longName = function (test) {
|
179 | var didErr = false;
|
180 | var r = Request({
|
181 | question: Question({
|
182 | name: '*************'
|
183 | + '***************'
|
184 | + '***************'
|
185 | + '***************'
|
186 | + '***************'
|
187 | + '***************'
|
188 | + '***************'
|
189 | + '***************'
|
190 | + '***************'
|
191 | + '***************'
|
192 | + '***************'
|
193 | + '***************'
|
194 | + '***************'
|
195 | + '***************'
|
196 | + '***************'
|
197 | + '***************'
|
198 | + '***************'
|
199 | + '***************'
|
200 | + '***************'
|
201 | + '***************',
|
202 | }),
|
203 | server: '8.8.8.8',
|
204 | timeout: 1000,
|
205 | });
|
206 | r.on('error', function (err) {
|
207 | test.ok(err, 'We should error because the packet failed to pack');
|
208 | didErr = true;
|
209 | });
|
210 | r.on('end', function () {
|
211 | test.ok(didErr, 'We did not err');
|
212 | test.done();
|
213 | });
|
214 | r.send();
|
215 | };
|
216 |
|
217 | exports.tearDown = function (cb) {
|
218 | cb();
|
219 | };
|