github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/libp2p/static_resolver_test.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package libp2p_test 6 7 import ( 8 "net" 9 "testing" 10 11 "github.com/ethersphere/bee/v2/pkg/p2p/libp2p" 12 ma "github.com/multiformats/go-multiaddr" 13 ) 14 15 func TestStaticAddressResolver(t *testing.T) { 16 t.Parallel() 17 18 for _, tc := range []struct { 19 name string 20 natAddr string 21 observableAddress string 22 want string 23 }{ 24 { 25 name: "replace port", 26 natAddr: ":30123", 27 observableAddress: "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 28 want: "/ip4/127.0.0.1/tcp/30123/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 29 }, 30 { 31 name: "replace ip v4", 32 natAddr: "192.168.1.34:", 33 observableAddress: "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 34 want: "/ip4/192.168.1.34/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 35 }, 36 { 37 name: "replace ip v6", 38 natAddr: "[2001:db8::8a2e:370:1111]:", 39 observableAddress: "/ip6/2001:db8::8a2e:370:7334/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 40 want: "/ip6/2001:db8::8a2e:370:1111/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 41 }, 42 { 43 name: "replace ip v4 with ip v6", 44 natAddr: "[2001:db8::8a2e:370:1111]:", 45 observableAddress: "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 46 want: "/ip6/2001:db8::8a2e:370:1111/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 47 }, 48 { 49 name: "replace ip v6 with ip v4", 50 natAddr: "192.168.1.34:", 51 observableAddress: "/ip6/2001:db8::8a2e:370:7334/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 52 want: "/ip4/192.168.1.34/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 53 }, 54 { 55 name: "replace ip and port", 56 natAddr: "192.168.1.34:30777", 57 observableAddress: "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 58 want: "/ip4/192.168.1.34/tcp/30777/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 59 }, 60 { 61 name: "replace ip v4 and port with ip v6", 62 natAddr: "[2001:db8::8a2e:370:1111]:30777", 63 observableAddress: "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 64 want: "/ip6/2001:db8::8a2e:370:1111/tcp/30777/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 65 }, 66 { 67 name: "replace ip v6 and port with ip v4", 68 natAddr: "192.168.1.34:30777", 69 observableAddress: "/ip6/2001:db8::8a2e:370:7334/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 70 want: "/ip4/192.168.1.34/tcp/30777/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 71 }, 72 { 73 name: "replace ip v6 and port with dns v4", 74 natAddr: "ipv4.com:30777", 75 observableAddress: "/ip6/2001:db8::8a2e:370:7334/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 76 want: "/dns4/ipv4.com/tcp/30777/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 77 }, 78 { 79 name: "replace ip v4 and port with dns", 80 natAddr: "ipv4and6.com:30777", 81 observableAddress: "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 82 want: "/dns/ipv4and6.com/tcp/30777/p2p/16Uiu2HAkyyGKpjBiCkVqCKoJa6RzzZw9Nr7hGogsMPcdad1KyMmd", 83 }, 84 } { 85 tc := tc 86 t.Run(tc.name, func(t *testing.T) { 87 t.Parallel() 88 89 r, err := libp2p.NewStaticAddressResolver(tc.natAddr, func(host string) ([]net.IP, error) { 90 hosts := map[string][]net.IP{ 91 "ipv4.com": { 92 net.ParseIP("192.168.1.34"), 93 }, 94 "ipv4and6.com": { 95 net.ParseIP("192.168.1.34"), 96 net.ParseIP("2001:db8::8a2e:370:1111"), 97 }, 98 } 99 return hosts[host], nil 100 }) 101 if err != nil { 102 t.Fatal(err) 103 } 104 observableAddress, err := ma.NewMultiaddr(tc.observableAddress) 105 if err != nil { 106 t.Fatal(err) 107 } 108 got, err := r.Resolve(observableAddress) 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 if got.String() != tc.want { 114 t.Errorf("got %s, want %s", got, tc.want) 115 } 116 }) 117 } 118 }