@@ -257,97 +257,146 @@ describe('Address Manager', () => {
257
257
} )
258
258
259
259
it ( 'should add an IPv4 DNS mapping' , ( ) => {
260
+ const transportManager = stubInterface < TransportManager > ( )
261
+
260
262
const am = new AddressManager ( {
261
263
peerId,
262
- transportManager : stubInterface < TransportManager > ( {
263
- getAddrs : ( ) => [ ]
264
- } ) ,
264
+ transportManager,
265
265
peerStore,
266
266
events,
267
267
logger : defaultLogger ( )
268
268
} )
269
269
270
- expect ( am . getAddresses ( ) ) . to . be . empty ( )
271
-
272
- const externalIp = '81.12.12.1'
273
- const externalAddress = multiaddr ( `/ip4/${ externalIp } /tcp/1234` )
270
+ const internalIp = '192.168.1.123'
271
+ const internalPort = 1234
272
+ const protocol = 'tcp'
274
273
275
- am . confirmObservedAddr ( externalAddress )
274
+ // one loopback, one LAN address
275
+ transportManager . getAddrs . returns ( [
276
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } ` ) ,
277
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } ` )
278
+ ] )
276
279
277
- expect ( am . getAddresses ( ) ) . to . deep . equal ( [ externalAddress . encapsulate ( `/p2p/${ peerId . toString ( ) } ` ) ] )
280
+ expect ( am . getAddresses ( ) ) . to . deep . equal ( [
281
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
282
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } /p2p/${ peerId } ` )
283
+ ] )
278
284
279
285
const domain = 'example.com'
286
+ const externalIp = '81.12.12.1'
287
+ const externalPort = 4566
280
288
281
289
am . addDNSMapping ( domain , [ externalIp ] )
282
290
291
+ // have not verified DNS mapping so it is not included
283
292
expect ( am . getAddresses ( ) ) . to . deep . equal ( [
284
- externalAddress . encapsulate ( `/p2p/${ peerId . toString ( ) } ` ) ,
285
- multiaddr ( `/dns4/${ domain } /tcp/1234/p2p/${ peerId . toString ( ) } ` )
293
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
294
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } /p2p/${ peerId } ` )
295
+ ] )
296
+
297
+ // public address mapping confirms DNS mapping
298
+ am . addPublicAddressMapping ( internalIp , internalPort , externalIp , externalPort , protocol )
299
+
300
+ expect ( am . getAddresses ( ) ) . to . deep . equal ( [
301
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
302
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
303
+ multiaddr ( `/ip4/${ externalIp } /tcp/${ externalPort } /p2p/${ peerId } ` ) ,
304
+ multiaddr ( `/dns4/${ domain } /tcp/${ externalPort } /p2p/${ peerId } ` )
286
305
] )
287
306
} )
288
307
289
308
it ( 'should add an IPv6 DNS mapping' , ( ) => {
309
+ const transportManager = stubInterface < TransportManager > ( )
310
+
290
311
const am = new AddressManager ( {
291
312
peerId,
292
- transportManager : stubInterface < TransportManager > ( {
293
- getAddrs : ( ) => [ ]
294
- } ) ,
313
+ transportManager,
295
314
peerStore,
296
315
events,
297
316
logger : defaultLogger ( )
298
317
} )
299
318
300
- expect ( am . getAddresses ( ) ) . to . be . empty ( )
301
-
302
- const externalIp = 'fe80::7c98:a9ff:fe94'
303
- const externalAddress = multiaddr ( `/ip6/${ externalIp } /tcp/1234` )
319
+ const internalIp = '192.168.1.123'
320
+ const internalPort = 1234
321
+ const protocol = 'tcp'
304
322
305
- am . confirmObservedAddr ( externalAddress )
323
+ // one loopback, one LAN address
324
+ transportManager . getAddrs . returns ( [
325
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } ` ) ,
326
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } ` )
327
+ ] )
306
328
307
- expect ( am . getAddresses ( ) ) . to . deep . equal ( [ externalAddress . encapsulate ( `/p2p/${ peerId . toString ( ) } ` ) ] )
329
+ expect ( am . getAddresses ( ) ) . to . deep . equal ( [
330
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
331
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } /p2p/${ peerId } ` )
332
+ ] )
308
333
309
334
const domain = 'example.com'
335
+ const externalIp = '2a00:23c6:14b1:7e00:c010:8ecf:2a25:dcd1'
336
+ const externalPort = 4566
310
337
311
338
am . addDNSMapping ( domain , [ externalIp ] )
312
339
340
+ // have not verified DNS mapping so it is not included
313
341
expect ( am . getAddresses ( ) ) . to . deep . equal ( [
314
- externalAddress . encapsulate ( `/p2p/${ peerId . toString ( ) } ` ) ,
315
- multiaddr ( `/dns6/${ domain } /tcp/1234/p2p/${ peerId . toString ( ) } ` )
342
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
343
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } /p2p/${ peerId } ` )
344
+ ] )
345
+
346
+ // public address mapping confirms DNS mapping
347
+ am . addPublicAddressMapping ( internalIp , internalPort , externalIp , externalPort , protocol )
348
+
349
+ expect ( am . getAddresses ( ) ) . to . deep . equal ( [
350
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
351
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
352
+ multiaddr ( `/ip6/${ externalIp } /tcp/${ externalPort } /p2p/${ peerId } ` ) ,
353
+ multiaddr ( `/dns6/${ domain } /tcp/${ externalPort } /p2p/${ peerId } ` )
316
354
] )
317
355
} )
318
356
319
- it ( 'should remove add a DNS mapping' , ( ) => {
357
+ it ( 'should remove a DNS mapping' , ( ) => {
358
+ const transportManager = stubInterface < TransportManager > ( )
359
+
320
360
const am = new AddressManager ( {
321
361
peerId,
322
- transportManager : stubInterface < TransportManager > ( {
323
- getAddrs : ( ) => [ ]
324
- } ) ,
362
+ transportManager,
325
363
peerStore,
326
364
events,
327
365
logger : defaultLogger ( )
328
366
} )
329
367
330
- expect ( am . getAddresses ( ) ) . to . be . empty ( )
331
-
332
- const externalIp = '81.12.12.1'
333
- const externalAddress = multiaddr ( `/ip4/${ externalIp } /tcp/1234` )
334
-
335
- am . confirmObservedAddr ( externalAddress )
368
+ const internalIp = '192.168.1.123'
369
+ const internalPort = 1234
370
+ const protocol = 'tcp'
336
371
337
- expect ( am . getAddresses ( ) ) . to . deep . equal ( [ externalAddress . encapsulate ( `/p2p/${ peerId . toString ( ) } ` ) ] )
372
+ // one loopback, one LAN address
373
+ transportManager . getAddrs . returns ( [
374
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } ` ) ,
375
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } ` )
376
+ ] )
338
377
339
378
const domain = 'example.com'
379
+ const externalIp = '81.12.12.1'
380
+ const externalPort = 4566
340
381
341
382
am . addDNSMapping ( domain , [ externalIp ] )
383
+ am . addPublicAddressMapping ( internalIp , internalPort , externalIp , externalPort , protocol )
342
384
343
385
expect ( am . getAddresses ( ) ) . to . deep . equal ( [
344
- externalAddress . encapsulate ( `/p2p/${ peerId . toString ( ) } ` ) ,
345
- multiaddr ( `/dns4/${ domain } /tcp/1234/p2p/${ peerId . toString ( ) } ` )
386
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
387
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
388
+ multiaddr ( `/ip4/${ externalIp } /tcp/${ externalPort } /p2p/${ peerId } ` ) ,
389
+ multiaddr ( `/dns4/${ domain } /tcp/${ externalPort } /p2p/${ peerId } ` )
346
390
] )
347
391
392
+ // public address mapping confirms DNS mapping
348
393
am . removeDNSMapping ( domain )
349
394
350
- expect ( am . getAddresses ( ) ) . to . deep . equal ( [ externalAddress . encapsulate ( `/p2p/${ peerId . toString ( ) } ` ) ] )
395
+ expect ( am . getAddresses ( ) ) . to . deep . equal ( [
396
+ multiaddr ( `/ip4/127.0.0.1/${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
397
+ multiaddr ( `/ip4/${ internalIp } /${ protocol } /${ internalPort } /p2p/${ peerId } ` ) ,
398
+ multiaddr ( `/ip4/${ externalIp } /tcp/${ externalPort } /p2p/${ peerId } ` )
399
+ ] )
351
400
} )
352
401
353
402
it ( 'should add a public IPv4 address mapping' , ( ) => {
0 commit comments