-
Notifications
You must be signed in to change notification settings - Fork 29
/
test.js
1030 lines (807 loc) · 24.4 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const {V4MAPPED, ADDRCONFIG, ALL} = require('dns');
const {Resolver: AsyncResolver} = require('dns').promises;
const {promisify} = require('util');
const http = require('http');
const test = require('ava');
const Keyv = require('keyv');
const proxyquire = require('proxyquire');
const QuickLRU = require('quick-lru');
const makeRequest = options => new Promise((resolve, reject) => {
http.get(options, resolve).once('error', reject);
});
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const mockedInterfaces = options => {
const createInterfaces = (options = {}) => {
const interfaces = {
lo: [
{
internal: true
}
],
eth0: []
};
if (options.has4) {
interfaces.eth0.push({
address: '192.168.0.111',
netmask: '255.255.255.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: false,
cidr: '192.168.0.111/24'
});
}
if (options.has6) {
interfaces.eth0.push({
address: 'fe80::c962:2946:a4e2:9f05',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 8,
internal: false,
cidr: 'fe80::c962:2946:a4e2:9f05/64'
});
}
return interfaces;
};
let interfaces = createInterfaces(options);
const _updateInterfaces = (options = {}) => {
interfaces = createInterfaces(options);
};
const result = proxyquire('../source', {
os: {
networkInterfaces: () => interfaces
}
});
result._updateInterfaces = _updateInterfaces;
return result;
};
const createResolver = () => {
let counter = {
4: 0,
6: 0,
lookup: 0
};
const resolver = {
servers: ['127.0.0.1'],
getServers() {
return [...resolver.servers];
},
setServers(servers) {
resolver.servers = [...servers];
},
resolve: (hostname, options, callback) => {
let data;
for (const server of resolver.servers) {
if (resolver.data[server][hostname]) {
data = resolver.data[server][hostname];
break;
}
}
if (hostname === 'econnrefused') {
const error = new Error(`ECONNREFUSED ${hostname}`);
error.code = 'ECONNREFUSED';
callback(error);
return;
}
if (!data) {
const error = new Error(`ENOTFOUND ${hostname}`);
error.code = 'ENOTFOUND';
callback(error);
return;
}
if (data.length === 0) {
const error = new Error(`ENODATA ${hostname}`);
error.code = 'ENODATA';
callback(error);
return;
}
if (options.family === 4 || options.family === 6) {
data = data.filter(entry => entry.family === options.family);
}
callback(null, JSON.parse(JSON.stringify(data)));
},
resolve4: (hostname, options, callback) => {
counter[4]++;
return resolver.resolve(hostname, {...options, family: 4}, callback);
},
resolve6: (hostname, options, callback) => {
counter[6]++;
return resolver.resolve(hostname, {...options, family: 6}, callback);
},
lookup: (hostname, options, callback) => {
// No need to implement hints yet
counter.lookup++;
if (!resolver.lookupData[hostname]) {
const error = new Error(`ENOTFOUND ${hostname}`);
error.code = 'ENOTFOUND';
error.hostname = hostname;
callback(error);
return;
}
let entries = resolver.lookupData[hostname];
if (options.family === 4 || options.family === 6) {
entries = entries.filter(entry => entry.family === options.family);
}
if (options.all) {
callback(null, entries);
return;
}
callback(null, entries[0]);
},
data: {
'127.0.0.1': {
localhost: [
{address: '127.0.0.1', family: 4, ttl: 60},
{address: '::ffff:127.0.0.2', family: 6, ttl: 60}
],
example: [
{address: '127.0.0.127', family: 4, ttl: 60}
],
temporary: [
{address: '127.0.0.1', family: 4, ttl: 1}
],
twoSeconds: [
{address: '127.0.0.1', family: 4, ttl: 2}
],
ttl: [
{address: '127.0.0.1', family: 4, ttl: 1}
],
maxTtl: [
{address: '127.0.0.1', family: 4, ttl: 60}
],
static4: [
{address: '127.0.0.1', family: 4, ttl: 1}
],
zeroTtl: [
{address: '127.0.0.127', family: 4, ttl: 0}
],
multiple: [
{address: '127.0.0.127', family: 4, ttl: 0},
{address: '127.0.0.128', family: 4, ttl: 0}
],
outdated: [
{address: '127.0.0.1', family: 4, ttl: 1}
]
},
'192.168.0.100': {
unique: [
{address: '127.0.0.1', family: 4, ttl: 60}
]
}
},
lookupData: {
osHostname: [
{address: '127.0.0.1', family: 4},
{address: '127.0.0.2', family: 4}
],
outdated: [
{address: '127.0.0.127', family: 4}
]
},
get counter() {
return counter;
},
resetCounter() {
counter = {
4: 0,
6: 0,
lookup: 0
};
}
};
return resolver;
};
const resolver = createResolver();
const CacheableLookup = proxyquire('../source', {
dns: {
lookup: resolver.lookup
}
});
const verify = (t, entry, value) => {
if (Array.isArray(value)) {
// eslint-disable-next-line guard-for-in
for (const key in value) {
t.true(typeof entry[key].expires === 'number' && entry[key].expires >= Date.now() - 1000);
t.true(typeof entry[key].ttl === 'number' && entry[key].ttl >= 0);
if (!('ttl' in value[key]) && 'ttl' in entry[key]) {
value[key].ttl = entry[key].ttl;
}
if (!('expires' in value[key]) && 'expires' in entry[key]) {
value[key].expires = entry[key].expires;
}
}
} else {
t.true(typeof entry.expires === 'number' && entry.expires >= Date.now() - 1000);
t.true(typeof entry.ttl === 'number' && entry.ttl >= 0);
if (!('ttl' in value)) {
value.ttl = entry.ttl;
}
if (!('expires' in value)) {
value.expires = entry.expires;
}
}
t.deepEqual(entry, value);
};
test('options.family', async t => {
const cacheable = new CacheableLookup({resolver});
// IPv4
let entry = await cacheable.lookupAsync('localhost', {family: 4});
verify(t, entry, {
address: '127.0.0.1',
family: 4
});
// IPv6
entry = await cacheable.lookupAsync('localhost', {family: 6});
verify(t, entry, {
address: '::ffff:127.0.0.2',
family: 6
});
});
test('options.all', async t => {
const cacheable = new CacheableLookup({resolver});
const entries = await cacheable.lookupAsync('localhost', {all: true});
verify(t, entries, [
{address: '127.0.0.1', family: 4},
{address: '::ffff:127.0.0.2', family: 6}
]);
});
test('options.all mixed with options.family', async t => {
const cacheable = new CacheableLookup({resolver});
// IPv4
let entries = await cacheable.lookupAsync('localhost', {all: true, family: 4});
verify(t, entries, [
{address: '127.0.0.1', family: 4}
]);
// IPv6
entries = await cacheable.lookupAsync('localhost', {all: true, family: 6});
verify(t, entries, [
{address: '::ffff:127.0.0.2', family: 6}
]);
});
test('V4MAPPED hint', async t => {
const cacheable = new CacheableLookup({resolver});
// Make sure default behavior is right
await t.throwsAsync(cacheable.lookupAsync('static4', {family: 6}), {code: 'ENOTFOUND'});
// V4MAPPED
{
const entries = await cacheable.lookupAsync('static4', {family: 6, hints: V4MAPPED});
verify(t, entries, {address: '::ffff:127.0.0.1', family: 6});
}
{
const entries = await cacheable.lookupAsync('localhost', {family: 6, hints: V4MAPPED});
verify(t, entries, {address: '::ffff:127.0.0.2', family: 6});
}
});
if (process.versions.node.split('.')[0] >= 14) {
test('ALL hint', async t => {
const cacheable = new CacheableLookup({resolver});
// ALL
const entries = await cacheable.lookupAsync('localhost', {family: 6, hints: V4MAPPED | ALL, all: true});
verify(t, entries, [
{address: '::ffff:127.0.0.1', family: 6, ttl: 60},
{address: '::ffff:127.0.0.2', family: 6, ttl: 60}
]);
});
}
test('ADDRCONFIG hint', async t => {
//=> has6 = false, family = 6
{
const CacheableLookup = mockedInterfaces({has4: true, has6: false});
const cacheable = new CacheableLookup({resolver});
await t.throwsAsync(cacheable.lookupAsync('localhost', {family: 6, hints: ADDRCONFIG}), {code: 'ENOTFOUND'});
}
//=> has6 = true, family = 6
{
const CacheableLookup = mockedInterfaces({has4: true, has6: true});
const cacheable = new CacheableLookup({resolver});
verify(t, await cacheable.lookupAsync('localhost', {family: 6, hints: ADDRCONFIG}), {
address: '::ffff:127.0.0.2',
family: 6
});
}
//=> has4 = false, family = 4
{
const CacheableLookup = mockedInterfaces({has4: false, has6: true});
const cacheable = new CacheableLookup({resolver});
await t.throwsAsync(cacheable.lookupAsync('localhost', {family: 4, hints: ADDRCONFIG}), {code: 'ENOTFOUND'});
}
//=> has4 = true, family = 4
{
const CacheableLookup = mockedInterfaces({has4: true, has6: true});
const cacheable = new CacheableLookup({resolver});
verify(t, await cacheable.lookupAsync('localhost', {family: 4, hints: ADDRCONFIG}), {
address: '127.0.0.1',
family: 4
});
}
// Update interface info
{
const CacheableLookup = mockedInterfaces({has4: false, has6: true});
const cacheable = new CacheableLookup({resolver});
await t.throwsAsync(cacheable.lookupAsync('localhost', {family: 4, hints: ADDRCONFIG}), {code: 'ENOTFOUND'});
//=> has4 = true, family = 4
CacheableLookup._updateInterfaces({has4: true, has6: true}); // Override os.networkInterfaces()
cacheable.updateInterfaceInfo();
verify(t, await cacheable.lookupAsync('localhost', {family: 4, hints: ADDRCONFIG}), {
address: '127.0.0.1',
family: 4
});
}
});
test.serial('caching works', async t => {
const cacheable = new CacheableLookup({resolver});
// Make sure default behavior is right
let entries = await cacheable.lookupAsync('temporary', {all: true, family: 4});
verify(t, entries, [
{address: '127.0.0.1', family: 4}
]);
// Update DNS data
const resovlerEntry = resolver.data['127.0.0.1'].temporary[0];
const {address: resolverAddress} = resovlerEntry;
resovlerEntry.address = '127.0.0.2';
// Lookup again
entries = await cacheable.lookupAsync('temporary', {all: true, family: 4});
verify(t, entries, [
{address: '127.0.0.1', family: 4}
]);
// Restore back
resovlerEntry.address = resolverAddress;
});
test('respects ttl', async t => {
const cacheable = new CacheableLookup({resolver});
// Make sure default behavior is right
let entries = await cacheable.lookupAsync('ttl', {all: true, family: 4});
verify(t, entries, [
{address: '127.0.0.1', family: 4}
]);
// Update DNS data
const resolverEntry = resolver.data['127.0.0.1'].ttl[0];
const {address: resolverAddress} = resolverEntry;
resolverEntry.address = '127.0.0.2';
// Wait until it expires
await sleep((resolverEntry.ttl * 1000) + 1);
// Lookup again
entries = await cacheable.lookupAsync('ttl', {all: true, family: 4});
verify(t, entries, [
{address: '127.0.0.2', family: 4}
]);
// Restore back
resolverEntry.address = resolverAddress;
});
test('throw when there are entries available but not for the requested family', async t => {
const cacheable = new CacheableLookup({resolver});
await t.throwsAsync(cacheable.lookupAsync('static4', {family: 6}), {code: 'ENOTFOUND'});
});
test('custom servers', async t => {
const cacheable = new CacheableLookup({resolver: createResolver()});
// .servers (get)
t.deepEqual(cacheable.servers, ['127.0.0.1']);
await t.throwsAsync(cacheable.lookupAsync('unique'), {code: 'ENOTFOUND'});
// .servers (set)
cacheable.servers = ['127.0.0.1', '192.168.0.100'];
verify(t, await cacheable.lookupAsync('unique'), {
address: '127.0.0.1',
family: 4
});
// Verify
t.deepEqual(cacheable.servers, ['127.0.0.1', '192.168.0.100']);
});
test('callback style', async t => {
const cacheable = new CacheableLookup({resolver});
// Custom promise for this particular test
const lookup = (...args) => new Promise((resolve, reject) => {
cacheable.lookup(...args, (error, ...data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
// Without options
let result = await lookup('localhost');
t.is(result.length, 4);
t.is(result[0], '127.0.0.1');
t.is(result[1], 4);
t.true(typeof result[2] === 'number' && result[2] >= Date.now() - 1000);
t.true(typeof result[3] === 'number' && result[3] >= 0);
// With options
result = await lookup('localhost', {family: 6, all: true});
t.is(result.length, 1);
verify(t, result[0], [{address: '::ffff:127.0.0.2', family: 6}]);
});
test('works', async t => {
const cacheable = new CacheableLookup({resolver});
verify(t, await cacheable.lookupAsync('localhost'), {
address: '127.0.0.1',
family: 4
});
});
test('options.maxTtl', async t => {
//=> maxTtl = 1
{
const cacheable = new CacheableLookup({resolver, maxTtl: 1});
// Make sure default behavior is right
verify(t, await cacheable.lookupAsync('maxTtl'), {
address: '127.0.0.1',
family: 4
});
// Update DNS data
const resolverEntry = resolver.data['127.0.0.1'].maxTtl[0];
resolverEntry.address = '127.0.0.2';
// Wait until it expires
await sleep((cacheable.maxTtl * 1000) + 1);
// Lookup again
verify(t, await cacheable.lookupAsync('maxTtl'), {
address: '127.0.0.2',
family: 4
});
// Reset
resolverEntry.address = '127.0.0.1';
}
//=> maxTtl = 0
{
const cacheable = new CacheableLookup({resolver, maxTtl: 0});
// Make sure default behavior is right
verify(t, await cacheable.lookupAsync('maxTtl'), {
address: '127.0.0.1',
family: 4
});
// Update DNS data
const resolverEntry = resolver.data['127.0.0.1'].maxTtl[0];
resolverEntry.address = '127.0.0.2';
// Wait until it expires
await sleep((cacheable.maxTtl * 1000) + 1);
// Lookup again
verify(t, await cacheable.lookupAsync('maxTtl'), {
address: '127.0.0.2',
family: 4
});
// Reset
resolverEntry.address = '127.0.0.1';
}
});
test('entry with 0 ttl', async t => {
const cacheable = new CacheableLookup({resolver});
// Make sure default behavior is right
verify(t, await cacheable.lookupAsync('zeroTtl'), {
address: '127.0.0.127',
family: 4
});
// Update DNS data
resolver.data['127.0.0.1'].zeroTtl[0].address = '127.0.0.1';
// Lookup again
verify(t, await cacheable.lookupAsync('zeroTtl'), {
address: '127.0.0.1',
family: 4
});
});
test('http example', async t => {
const cacheable = new CacheableLookup({resolver});
const options = {
hostname: 'example',
port: 9999,
lookup: cacheable.lookup
};
await t.throwsAsync(makeRequest(options), {
message: 'connect ECONNREFUSED 127.0.0.127:9999'
});
});
test('.lookup() and .lookupAsync() are automatically bounded', async t => {
const cacheable = new CacheableLookup({resolver});
await t.notThrowsAsync(cacheable.lookupAsync('localhost'));
await t.notThrowsAsync(promisify(cacheable.lookup)('localhost'));
t.throws(() => cacheable.lookup('localhost'), {
message: 'Callback must be a function.'
});
});
test('works (Internet connection)', async t => {
const cacheable = new CacheableLookup();
const {address, family} = await cacheable.lookupAsync('1dot1dot1dot1.cloudflare-dns.com');
t.true(address === '1.1.1.1' || address === '1.0.0.1');
t.is(family, 4);
});
test.serial('install & uninstall', async t => {
const cacheable = new CacheableLookup({resolver});
cacheable.install(http.globalAgent);
const options = {
hostname: 'example',
port: 9999
};
await t.throwsAsync(makeRequest(options), {
message: 'connect ECONNREFUSED 127.0.0.127:9999'
});
cacheable.uninstall(http.globalAgent);
await t.throwsAsync(makeRequest(options), {
message: /^getaddrinfo (?:ENOTFOUND|EAI_AGAIN) example/
});
http.globalAgent.destroy();
});
test('`.install()` throws if no Agent provided', t => {
const cacheable = new CacheableLookup();
t.throws(() => cacheable.install(), {
message: 'Expected an Agent instance as the first argument'
});
t.throws(() => cacheable.install(1), {
message: 'Expected an Agent instance as the first argument'
});
});
test('`.uninstall()` throws if no Agent provided', t => {
const cacheable = new CacheableLookup();
t.throws(() => cacheable.uninstall(), {
message: 'Expected an Agent instance as the first argument'
});
t.throws(() => cacheable.uninstall(1), {
message: 'Expected an Agent instance as the first argument'
});
});
test.serial('`.uninstall()` does not alter unmodified Agents', t => {
const cacheable = new CacheableLookup();
const {createConnection} = http.globalAgent;
cacheable.uninstall(http.globalAgent);
t.is(createConnection, http.globalAgent.createConnection);
});
test.serial('throws if double-installing CacheableLookup', t => {
const cacheable = new CacheableLookup();
cacheable.install(http.globalAgent);
t.throws(() => cacheable.install(http.globalAgent), {
message: 'CacheableLookup has been already installed'
});
cacheable.uninstall(http.globalAgent);
});
test.serial('install - providing custom lookup function anyway', async t => {
const a = new CacheableLookup();
const b = new CacheableLookup({resolver});
a.install(http.globalAgent);
const options = {
hostname: 'example',
port: 9999,
lookup: b.lookup
};
await t.throwsAsync(makeRequest(options), {
message: 'connect ECONNREFUSED 127.0.0.127:9999'
});
a.uninstall(http.globalAgent);
});
test.serial('throws when calling `.uninstall()` on the wrong instance', t => {
const a = new CacheableLookup();
const b = new CacheableLookup({resolver});
a.install(http.globalAgent);
t.throws(() => b.uninstall(http.globalAgent), {
message: 'The agent is not owned by this CacheableLookup instance'
});
a.uninstall(http.globalAgent);
});
test('async resolver (Internet connection)', async t => {
const cacheable = new CacheableLookup({resolver: new AsyncResolver()});
t.is(typeof cacheable._resolve4, 'function');
t.is(typeof cacheable._resolve6, 'function');
const {address} = await cacheable.lookupAsync('1dot1dot1dot1.cloudflare-dns.com');
t.true(address === '1.1.1.1' || address === '1.0.0.1');
});
test('clear() works', async t => {
const cacheable = new CacheableLookup({resolver});
await cacheable.lookupAsync('localhost');
t.is(cacheable._cache.size, 1);
cacheable.clear();
t.is(cacheable._cache.size, 0);
});
test('ttl works', async t => {
const cacheable = new CacheableLookup({resolver});
await Promise.all([cacheable.lookupAsync('temporary'), cacheable.lookupAsync('ttl')]);
t.is(cacheable._cache.size, 2);
await sleep(2001);
t.is(cacheable._cache.size, 0);
});
test('custom cache support', async t => {
const cache = new Keyv();
const cacheable = new CacheableLookup({
resolver,
cache
});
await cacheable.lookupAsync('temporary');
const [entry] = await cache.get('temporary');
t.is(entry.address, '127.0.0.1');
t.is(entry.family, 4);
t.is(entry.ttl, 1);
await sleep(entry.ttl * 1001);
const newEntry = await cache.get('temporary');
t.is(newEntry, undefined);
});
test.serial('fallback works', async t => {
const cacheable = new CacheableLookup({resolver, fallbackDuration: 3600});
resolver.resetCounter();
const entries = await cacheable.lookupAsync('osHostname', {all: true});
t.is(entries.length, 2);
t.is(entries[0].address, '127.0.0.1');
t.is(entries[0].family, 4);
t.is(entries[1].address, '127.0.0.2');
t.is(entries[1].family, 4);
t.is(cacheable._cache.size, 0);
await cacheable.lookupAsync('osHostname', {all: true});
t.deepEqual(resolver.counter, {
6: 1,
4: 1,
lookup: 2
});
});
test.serial('fallback works if ip change', async t => {
const cacheable = new CacheableLookup({resolver, fallbackDuration: 3600});
resolver.resetCounter();
resolver.lookupData.osHostnameChange = [
{address: '127.0.0.1', family: 4},
{address: '127.0.0.2', family: 4}
];
// First call: do not enter in `if (this._hostnamesToFallback.has(hostname)) {`
const entries = await cacheable.query('osHostnameChange', {all: true});
t.is(entries.length, 2);
t.is(entries[0].address, '127.0.0.1');
t.is(entries[0].family, 4);
t.is(entries[1].address, '127.0.0.2');
t.is(entries[1].family, 4);
t.is(cacheable._cache.size, 0);
// Second call: enter in `if (this._hostnamesToFallback.has(hostname)) {`
// And use _dnsLookup
// This call is used to ensure that this._pending is cleaned up when the promise is resolved
await cacheable.query('osHostnameChange', {all: true});
// Third call: enter in `if (this._hostnamesToFallback.has(hostname)) {`
// And use _dnsLookup
// Address should be different
resolver.lookupData.osHostnameChange = [
{address: '127.0.0.3', family: 4},
{address: '127.0.0.4', family: 4}
];
const entries2 = await cacheable.query('osHostnameChange', {all: true});
t.is(entries2.length, 2);
t.is(entries2[0].address, '127.0.0.3');
t.is(entries2[0].family, 4);
t.is(entries2[1].address, '127.0.0.4');
t.is(entries2[1].family, 4);
t.is(cacheable._cache.size, 0);
delete resolver.lookupData.osHostnameChange;
});
test('real DNS queries first', async t => {
const resolver = createResolver({delay: 0});
const cacheable = new CacheableLookup({
resolver,
fallbackDuration: 3600,
lookup: resolver.lookup
});
{
const entries = await cacheable.lookupAsync('outdated', {all: true});
verify(t, entries, [
{address: '127.0.0.1', family: 4}
]);
}
await new Promise(resolve => setTimeout(resolve, 100));
{
const entries = await cacheable.lookupAsync('outdated', {all: true});
verify(t, entries, [
{address: '127.0.0.1', family: 4}
]);
}
});
test('fallback can be turned off', async t => {
const cacheable = new CacheableLookup({resolver, fallbackDuration: 0});
await t.throwsAsync(cacheable.lookupAsync('osHostname', {all: true}), {
message: 'cacheableLookup ENOTFOUND osHostname'
});
});
test('errors are cached', async t => {
const cacheable = new CacheableLookup({resolver, errorTtl: 0.1});
await t.throwsAsync(cacheable.lookupAsync('doesNotExist'), {
code: 'ENOTFOUND'
});
t.is(cacheable._cache.size, 1);
await sleep((cacheable.errorTtl * 1000) + 1);
t.is(cacheable._cache.size, 0);
});
test('passing family as options', async t => {
const cacheable = new CacheableLookup({resolver});
const promisified = promisify(cacheable.lookup);
const entry = await cacheable.lookupAsync('localhost', 6);
t.is(entry.address, '::ffff:127.0.0.2');
t.is(entry.family, 6);
const address = await promisified('localhost', 6);
t.is(address, '::ffff:127.0.0.2');
});
test('clear(hostname) works', async t => {
const cacheable = new CacheableLookup({resolver});
await cacheable.lookupAsync('localhost');
await cacheable.lookupAsync('temporary');
cacheable.clear('localhost');
t.is(cacheable._cache.size, 1);
});
test('prevents overloading DNS', async t => {
const resolver = createResolver();
const {lookupAsync} = new CacheableLookup({
resolver,
lookup: resolver.lookup
});
await Promise.all([lookupAsync('localhost'), lookupAsync('localhost')]);
t.deepEqual(resolver.counter, {
4: 1,
6: 1,
lookup: 0
});
});
test('returns IPv6 if no other entries available', async t => {
const CacheableLookup = mockedInterfaces({has4: false, has6: true});
const cacheable = new CacheableLookup({resolver});
verify(t, await cacheable.lookupAsync('localhost', {hints: ADDRCONFIG}), {
address: '::ffff:127.0.0.2',
family: 6
});
});
test('throws when no internet connection', async t => {
const cacheable = new CacheableLookup({resolver});
await t.throwsAsync(cacheable.lookupAsync('econnrefused'), {
code: 'ECONNREFUSED'
});
});
test('full-featured custom cache', async t => {
const cache = new QuickLRU({maxSize: 1});
const cacheable = new CacheableLookup({
resolver,
cache
});
{
const entry = await cacheable.lookupAsync('localhost');
verify(t, entry, {
address: '127.0.0.1',
family: 4
});
}
t.is(cache.size, 1);
{
const entry = await cacheable.lookupAsync('localhost');
verify(t, entry, {
address: '127.0.0.1',
family: 4
});
}
t.is(cache.size, 1);
{
const entry = await cacheable.lookupAsync('temporary');
verify(t, entry, {
address: '127.0.0.1',
family: 4
});
}
t.is(cache.size, 1);
});
test('throws when the cache instance is broken', async t => {
const cacheable = new CacheableLookup({
resolver,
cache: {
get: () => {},
set: () => {
throw new Error('Something broke.');
}
}
});
await t.notThrowsAsync(cacheable.lookupAsync('localhost'));