github.com/ethereum/go-ethereum@v1.16.1/p2p/enode/urlv4_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package enode 18 19 import ( 20 "crypto/ecdsa" 21 "net" 22 "reflect" 23 "strings" 24 "testing" 25 26 "github.com/ethereum/go-ethereum/crypto" 27 "github.com/ethereum/go-ethereum/p2p/enr" 28 ) 29 30 var parseNodeTests = []struct { 31 input string 32 wantError string 33 wantResult *Node 34 }{ 35 // Records 36 { 37 input: "enr:-IS4QGrdq0ugARp5T2BZ41TrZOqLc_oKvZoPuZP5--anqWE_J-Tucc1xgkOL7qXl0puJgT7qc2KSvcupc4NCb0nr4tdjgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQM6UUF2Rm-oFe1IH_rQkRCi00T2ybeMHRSvw1HDpRvjPYN1ZHCCdl8", 38 wantResult: func() *Node { 39 testKey, _ := crypto.HexToECDSA("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8") 40 var r enr.Record 41 r.Set(enr.IP{127, 0, 0, 1}) 42 r.Set(enr.UDP(30303)) 43 r.SetSeq(99) 44 SignV4(&r, testKey) 45 n, _ := New(ValidSchemes, &r) 46 return n 47 }(), 48 }, 49 // Invalid Records 50 { 51 input: "enr:", 52 wantError: "EOF", // could be nicer 53 }, 54 { 55 input: "enr:x", 56 wantError: "illegal base64 data at input byte 0", 57 }, 58 { 59 input: "enr:-EmGZm9vYmFyY4JpZIJ2NIJpcIR_AAABiXNlY3AyNTZrMaEDOlFBdkZvqBXtSB_60JEQotNE9sm3jB0Ur8NRw6Ub4z2DdWRwgnZf", 60 wantError: enr.ErrInvalidSig.Error(), 61 }, 62 // Complete node URLs with IP address and ports 63 { 64 input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:foo", 65 wantError: `invalid port`, 66 }, 67 { 68 input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:3?discport=foo", 69 wantError: `invalid discport in query`, 70 }, 71 { 72 input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150", 73 wantResult: NewV4( 74 hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 75 net.IP{127, 0, 0, 1}, 76 52150, 77 52150, 78 ), 79 }, 80 { 81 input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@valid.:3", 82 wantResult: NewV4( 83 hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 84 nil, 85 3, 86 3, 87 ).WithHostname("valid."), 88 }, 89 { 90 input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@[::]:52150", 91 wantResult: NewV4( 92 hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 93 net.ParseIP("::"), 94 52150, 95 52150, 96 ), 97 }, 98 { 99 input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@[2001:db8:3c4d:15::abcd:ef12]:52150", 100 wantResult: NewV4( 101 hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 102 net.ParseIP("2001:db8:3c4d:15::abcd:ef12"), 103 52150, 104 52150, 105 ), 106 }, 107 { 108 input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150?discport=22334", 109 wantResult: NewV4( 110 hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 111 net.IP{0x7f, 0x0, 0x0, 0x1}, 112 52150, 113 22334, 114 ), 115 }, 116 // Incomplete node URLs with no address 117 { 118 input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439", 119 wantResult: NewV4( 120 hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 121 nil, 0, 0, 122 ), 123 }, 124 // Invalid URLs 125 { 126 input: "", 127 wantError: errMissingPrefix.Error(), 128 }, 129 { 130 input: "1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439", 131 wantError: errMissingPrefix.Error(), 132 }, 133 { 134 input: "01010101", 135 wantError: errMissingPrefix.Error(), 136 }, 137 { 138 input: "enode://01010101@123.124.125.126:3", 139 wantError: `invalid public key (wrong length, want 128 hex chars)`, 140 }, 141 { 142 input: "enode://01010101", 143 wantError: `invalid public key (wrong length, want 128 hex chars)`, 144 }, 145 { 146 input: "http://foobar", 147 wantError: errMissingPrefix.Error(), 148 }, 149 { 150 input: "://foo", 151 wantError: errMissingPrefix.Error(), 152 }, 153 } 154 155 func hexPubkey(h string) *ecdsa.PublicKey { 156 k, err := parsePubkey(h) 157 if err != nil { 158 panic(err) 159 } 160 return k 161 } 162 163 func TestParseNode(t *testing.T) { 164 for _, test := range parseNodeTests { 165 n, err := Parse(ValidSchemes, test.input) 166 if test.wantError != "" { 167 if err == nil { 168 t.Errorf("test %q:\n got nil error, expected %#q", test.input, test.wantError) 169 continue 170 } else if !strings.Contains(err.Error(), test.wantError) { 171 t.Errorf("test %q:\n got error %#q, expected %#q", test.input, err.Error(), test.wantError) 172 continue 173 } 174 } else { 175 if err != nil { 176 t.Errorf("test %q:\n unexpected error: %v", test.input, err) 177 continue 178 } 179 if !reflect.DeepEqual(n, test.wantResult) { 180 t.Errorf("test %q:\n result mismatch:\ngot: %#v\nwant: %#v", test.input, n, test.wantResult) 181 } 182 } 183 } 184 } 185 186 func TestNodeString(t *testing.T) { 187 for i, test := range parseNodeTests { 188 if test.wantError == "" && strings.HasPrefix(test.input, "enode://") { 189 str := test.wantResult.String() 190 if str != test.input { 191 t.Errorf("test %d: Node.String() mismatch:\ngot: %s\nwant: %s", i, str, test.input) 192 } 193 } 194 } 195 }