github.com/aergoio/aergo@v1.3.1/types/p2ptypes_test.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package types 7 8 import ( 9 "github.com/multiformats/go-multiaddr" 10 "net" 11 "reflect" 12 "testing" 13 ) 14 15 func TestParseMultiaddrWithResolve(t *testing.T) { 16 tests := []struct { 17 name string 18 str string 19 wantIps []net.IP // verify one of them 20 wantPort string 21 wantErr bool 22 }{ 23 {"TIP4peerAddr", "/ip4/192.168.0.58/tcp/11002/p2p/16Uiu2HAmHuBgtnisgPLbujFvxPNZw3Qvpk3VLUwTzh5C67LAZSFh", []net.IP{net.ParseIP("192.168.0.58")}, "11002", false}, 24 {"TIP4AndPort", "/ip4/192.168.0.58/tcp/11002", []net.IP{net.ParseIP("192.168.0.58")}, "11002", false}, 25 {"TIP6peerAddr", "/ip6/FE80::0202:B3FF:FE1E:8329/tcp/11003/p2p/16Uiu2HAmHuBgtnisgPLbujFvxPNZw3Qvpk3VLUwTzh5C67LAZSFh", []net.IP{net.ParseIP("FE80::0202:B3FF:FE1E:8329")}, "11003", false}, 26 //FIXME 27 // skip case, since it depend on external environment. uncomment it if really need, and comment back after finishing test 28 //{"TDomainName", "/dns/aergo.io/tcp/11004/p2p/16Uiu2HAmHuBgtnisgPLbujFvxPNZw3Qvpk3VLUwTzh5C67LAZSFh", []net.IP{net.ParseIP("104.20.161.59"), net.ParseIP("104.20.160.59")}, "11004", false}, 29 {"TInvalidDomain", "/dns/nowhere.a.aergo.io/tcp/11004/p2p/16Uiu2HAmHuBgtnisgPLbujFvxPNZw3Qvpk3VLUwTzh5C67LAZSFh", []net.IP{}, "", true}, 30 {"TWrongProto", "/ipx/192.168.0.58/tcp/11004/p2p/16Uiu2HAmHuBgtnisgPLbujFvxPNZw3Qvpk3VLUwTzh5C67LAZSFh", []net.IP{}, "", true}, 31 // TODO: Add test cases. 32 } 33 for _, tt := range tests { 34 t.Run(tt.name, func(t *testing.T) { 35 got, err := ParseMultiaddrWithResolve(tt.str) 36 if (err != nil) != tt.wantErr { 37 t.Errorf("ParseMultiaddrWithResolve() error = %v, wantErr %v", err, tt.wantErr) 38 return 39 } 40 if !tt.wantErr { 41 if got == nil { 42 t.Errorf("ParseMultiaddrWithResolve() is nil , want not nil") 43 } 44 45 ipStr, err := got.ValueForProtocol(multiaddr.P_IP4) 46 if err != nil { 47 ipStr, _ = got.ValueForProtocol(multiaddr.P_IP6) 48 } 49 ip := net.ParseIP(ipStr) 50 found := false 51 for _, wantIp := range tt.wantIps { 52 if reflect.DeepEqual(ip, wantIp) { 53 found = true 54 break 55 } 56 } 57 if !found { 58 t.Errorf("ParseMultiaddrWithResolve() = %v, want %v", ip, tt.wantIps) 59 } 60 port, err := got.ValueForProtocol(multiaddr.P_TCP) 61 if !reflect.DeepEqual(port, tt.wantPort) { 62 t.Errorf("ParseMultiaddrWithResolve() = %v, want %v", port, tt.wantPort) 63 } 64 } 65 }) 66 } 67 }