github.com/btcsuite/btcd@v0.24.0/wire/netaddressv2_test.go (about) 1 package wire 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 "time" 8 ) 9 10 // TestNetAddressV2FromBytes tests that NetAddressV2FromBytes works as 11 // expected. 12 func TestNetAddressV2FromBytes(t *testing.T) { 13 tests := []struct { 14 addrBytes []byte 15 expectedString string 16 expectedNetwork string 17 }{ 18 // Ipv4 encoding 19 { 20 []byte{0x7f, 0x00, 0x00, 0x01}, 21 "127.0.0.1", 22 string(ipv4), 23 }, 24 25 // Ipv6 encoding 26 { 27 []byte{ 28 0x20, 0x01, 0x09, 0xe8, 0x26, 0x15, 0x73, 0x00, 29 0x09, 0x54, 0x12, 0x63, 0xef, 0xc8, 0x2e, 0x34, 30 }, 31 "2001:9e8:2615:7300:954:1263:efc8:2e34", 32 string(ipv6), 33 }, 34 35 // OnionCat encoding 36 { 37 []byte{ 38 0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43, 0xff, 0xfe, 39 0xcc, 0x39, 0xa8, 0x73, 0x69, 0x15, 0xff, 0xff, 40 }, 41 "777myonionurl777.onion", 42 string(torv2), 43 }, 44 45 // Torv2 encoding 46 { 47 []byte{ 48 0xff, 0xfe, 0xcc, 0x39, 0xa8, 0x73, 0x69, 0x15, 49 0xff, 0xff, 50 }, 51 "777myonionurl777.onion", 52 string(torv2), 53 }, 54 55 // Torv3 encoding 56 { 57 []byte{ 58 0xca, 0xd2, 0xd3, 0xc8, 0xdc, 0x9c, 0xc4, 0xd3, 59 0x70, 0x33, 0x30, 0xc5, 0x23, 0xaf, 0x02, 0xed, 60 0xc4, 0x9d, 0xf8, 0xc6, 0xb0, 0x4e, 0x74, 0x6d, 61 0x3b, 0x51, 0x57, 0xa7, 0x15, 0xfe, 0x98, 0x35, 62 }, 63 "zljnhsg4ttcng4btgdcshlyc5xcj36ggwbhhi3j3kfl2ofp6ta26jlid.onion", 64 string(torv3), 65 }, 66 } 67 68 t.Logf("Running %d tests", len(tests)) 69 for i, test := range tests { 70 na := NetAddressV2FromBytes(time.Time{}, 0, test.addrBytes, 0) 71 72 if test.expectedNetwork != string(torv3) { 73 if na.ToLegacy() == nil { 74 t.Errorf("Test #%d has nil legacy encoding", i) 75 } 76 } else { 77 if !na.IsTorV3() { 78 t.Errorf("Test #%d is not torv3 address", i) 79 } 80 } 81 82 if na.Addr.String() != test.expectedString { 83 t.Errorf("Test #%d did not match expected string", i) 84 } 85 86 if na.Addr.Network() != test.expectedNetwork { 87 t.Errorf("Test #%d did not match expected network", i) 88 } 89 90 var b bytes.Buffer 91 if err := writeNetAddressV2(&b, 0, na); err != nil { 92 t.Errorf("Test #%d failed writing address %v", i, err) 93 } 94 95 // Assert that the written netID is equivalent to the above. 96 if string(b.Bytes()[5]) != test.expectedNetwork { 97 t.Errorf("Test #%d did not match expected network", i) 98 } 99 } 100 } 101 102 // TestReadNetAddressV2 tests that readNetAddressV2 behaves as expected in 103 // different scenarios. 104 func TestReadNetAddressV2(t *testing.T) { 105 tests := []struct { 106 buf []byte 107 expectedNetwork string 108 expectedError error 109 }{ 110 // Invalid address size for unknown netID. 111 { 112 []byte{ 113 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfd, 0xff, 114 0xff, 115 }, 116 "", 117 ErrInvalidAddressSize, 118 }, 119 120 // Valid address size for unknown netID. 121 { 122 []byte{ 123 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x20, 0x10, 124 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 125 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 126 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 127 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x22, 128 0x22, 129 }, 130 "", 131 ErrSkippedNetworkID, 132 }, 133 134 // Invalid ipv4 size. 135 { 136 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x05}, 137 "", 138 ErrInvalidAddressSize, 139 }, 140 141 // Valid ipv4 encoding. 142 { 143 []byte{ 144 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x7f, 145 0x00, 0x00, 0x01, 0x22, 0x22, 146 }, 147 string(ipv4), 148 nil, 149 }, 150 151 // Invalid ipv6 size. 152 { 153 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfc}, 154 "", 155 ErrInvalidAddressSize, 156 }, 157 158 // OnionCat encoding is skipped. 159 { 160 []byte{ 161 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x10, 0xfd, 162 0x87, 0xd8, 0x7e, 0xeb, 0x43, 0xff, 0xfe, 0xcc, 163 0x39, 0xa8, 0x73, 0x69, 0x15, 0xff, 0xff, 0x22, 164 0x22, 165 }, 166 "", 167 ErrSkippedNetworkID, 168 }, 169 170 // Valid ipv6 encoding. 171 { 172 []byte{ 173 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x10, 0xff, 174 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 175 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x22, 176 0x22, 177 }, 178 string(ipv6), 179 nil, 180 }, 181 182 // Invalid torv2 size. 183 { 184 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02}, 185 "", 186 ErrInvalidAddressSize, 187 }, 188 189 // Valid torv2 encoding. 190 { 191 []byte{ 192 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0a, 0x20, 193 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 194 0x20, 0x22, 0x22, 195 }, 196 string(torv2), 197 nil, 198 }, 199 200 // Invalid torv3 size. 201 { 202 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x02}, 203 "", 204 ErrInvalidAddressSize, 205 }, 206 207 // Valid torv3 encoding. 208 { 209 []byte{ 210 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x10, 211 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 212 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 213 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 214 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x22, 215 0x22, 216 }, 217 string(torv3), 218 nil, 219 }, 220 221 // Invalid i2p size. 222 { 223 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x02}, 224 "", 225 ErrInvalidAddressSize, 226 }, 227 228 // Valid i2p encoding. 229 { 230 []byte{ 231 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x10, 232 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 233 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 234 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 235 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x22, 236 0x22, 237 }, 238 string(i2p), 239 ErrSkippedNetworkID, 240 }, 241 242 // Invalid cjdns size. 243 { 244 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x02}, 245 "", 246 ErrInvalidAddressSize, 247 }, 248 249 // Valid cjdns encoding. 250 { 251 []byte{ 252 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x20, 253 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 254 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x22, 255 0x22, 256 }, 257 string(cjdns), 258 ErrSkippedNetworkID, 259 }, 260 } 261 262 t.Logf("Running %d tests", len(tests)) 263 for i, test := range tests { 264 r := bytes.NewReader(test.buf) 265 na := &NetAddressV2{} 266 267 err := readNetAddressV2(r, 0, na) 268 if err != test.expectedError { 269 t.Errorf("Test #%d had unexpected error %v", i, err) 270 } else if err != nil { 271 continue 272 } 273 274 // Trying to read more should give EOF. 275 var b [1]byte 276 if _, err := r.Read(b[:]); err != io.EOF { 277 t.Errorf("Test #%d did not cleanly finish reading", i) 278 } 279 280 if na.Addr.Network() != test.expectedNetwork { 281 t.Errorf("Test #%d had unexpected network %v", i, 282 na.Addr.Network()) 283 } 284 } 285 }