github.com/aergoio/aergo@v1.3.1/internal/network/address_test.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package network 7 8 import ( 9 "net" 10 "reflect" 11 "testing" 12 ) 13 14 func TestResolveHostDomain(t *testing.T) { 15 type args struct { 16 domainName string 17 } 18 tests := []struct { 19 name string 20 args args 21 exist bool 22 wantErr bool 23 }{ 24 {"TSucc", args{"www.google.com"}, true, false}, 25 {"TNowhere", args{"not.in.my.aergo.io"}, false, true}, 26 {"TWrongName", args{"!#@doigjw"}, false, true}, 27 } 28 for _, tt := range tests { 29 t.Run(tt.name, func(t *testing.T) { 30 got, err := ResolveHostDomain(tt.args.domainName) 31 if (err != nil) != tt.wantErr { 32 t.Errorf("ResolveHostDomain() error = %v, wantErr %v", err, tt.wantErr) 33 return 34 } 35 if (len(got) > 0) != tt.exist { 36 t.Errorf("ResolveHostDomain() = %v, want %v", got, tt.exist) 37 } 38 }) 39 } 40 } 41 42 func TestResolveHostDomainLocal(t *testing.T) { 43 t.Skip("skip env dependent test") 44 type args struct { 45 domainName string 46 } 47 tests := []struct { 48 name string 49 args args 50 want []net.IP 51 wantErr bool 52 }{ 53 {"TPrivate", args{"devuntu31"}, []net.IP{net.ParseIP("192.168.0.215")}, false}, 54 {"TPrivate", args{"devuntu31.blocko.io"}, []net.IP{net.ParseIP("192.168.0.215")}, false}, 55 {"TPrivate", args{"devuntu31ss"}, nil, true}, 56 } 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 got, err := ResolveHostDomain(tt.args.domainName) 60 if (err != nil) != tt.wantErr { 61 t.Errorf("ResolveHostDomain() error = %v, wantErr %v", err, tt.wantErr) 62 return 63 } 64 if !reflect.DeepEqual(got, tt.want) { 65 t.Errorf("ResolveHostDomain() = %v, want %v", got, tt.want) 66 } 67 }) 68 } 69 } 70 71 func TestParseAddress(t *testing.T) { 72 tests := []struct { 73 name string 74 75 in string 76 77 wantErr bool 78 wantHost string 79 wantPort string 80 }{ 81 {"TIP4", "211.34.56.78", false, "211.34.56.78", ""}, 82 {"TIP6", "fe80::dcbf:beff:fe87:e30a", false, "fe80::dcbf:beff:fe87:e30a", ""}, 83 {"TIP6_2", "::ffff:192.0.1.2", false, "::ffff:192.0.1.2", ""}, 84 {"TFQDN", "iparkmac.aergo.io", false, "iparkmac.aergo.io", ""}, 85 {"TIP4WithPort", "211.34.56.78:1234", true, "211.34.56.78", "1234"}, 86 {"TIP6WithPort", "[fe80::dcbf:beff:fe87:e30a]:1234", true, "fe80::dcbf:beff:fe87:e30a", "1234"}, 87 {"TFQDNWithPort", "iparkmac.aergo.io:1234", true, "iparkmac.aergo.io", "1234"}, 88 // TODO: test cases 89 } 90 for _, test := range tests { 91 t.Run(test.name, func(t *testing.T) { 92 got, err := CheckAddress(test.in) 93 if (err != nil) != test.wantErr { 94 t.Errorf("CheckAddress() error = %v, wantErr %v", err, test.wantErr) 95 return 96 } 97 if !test.wantErr { 98 if got != test.wantHost { 99 t.Errorf("CheckAddress() = host %v, wantHost %v", got, test.wantHost) 100 } 101 } 102 }) 103 } 104 } 105 106 func TestCheckAddressType(t *testing.T) { 107 tests := []struct { 108 name string 109 110 in string 111 112 want AddressType 113 }{ 114 {"TIP4", "211.34.56.78", AddressTypeIP}, 115 {"TIP6", "fe80::dcbf:beff:fe87:e30a", AddressTypeIP}, 116 {"TIP6_2", "::ffff:192.0.1.2", AddressTypeIP}, 117 {"TFQDN", "iparkmac.aergo.io", AddressTypeFQDN}, 118 {"TFQDN_2", "3com.com", AddressTypeFQDN}, 119 {"TWrongDN", "3com!.com", AddressTypeError}, 120 {"TIP4withPort", "211.34.56.78:1234", AddressTypeError}, 121 {"TIP6withPort", "[fe80::dcbf:beff:fe87:e30a]:1234", AddressTypeError}, 122 {"TFQDNwithPort", "iparkmac.aergo.io:1234", AddressTypeError}, 123 // TODO: test cases 124 } 125 for _, test := range tests { 126 t.Run(test.name, func(t *testing.T) { 127 got := CheckAddressType(test.in) 128 if got != test.want { 129 t.Errorf("CheckAddressType() = type %v, wantType %v", got, test.want) 130 } 131 }) 132 } 133 }