github.com/ConsenSys/Quorum@v20.10.0+incompatible/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@invalid.:3",
    65  		wantError: `no such host`,
    66  	},
    67  	{
    68  		input:     "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:foo",
    69  		wantError: `invalid port`,
    70  	},
    71  	{
    72  		input:     "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:3?discport=foo",
    73  		wantError: `invalid discport in query`,
    74  	},
    75  	{
    76  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150",
    77  		wantResult: NewV4(
    78  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
    79  			net.IP{127, 0, 0, 1},
    80  			52150,
    81  			52150,
    82  		),
    83  	},
    84  	{
    85  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@[::]:52150",
    86  		wantResult: NewV4(
    87  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
    88  			net.ParseIP("::"),
    89  			52150,
    90  			52150,
    91  		),
    92  	},
    93  	{
    94  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@[2001:db8:3c4d:15::abcd:ef12]:52150",
    95  		wantResult: NewV4(
    96  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
    97  			net.ParseIP("2001:db8:3c4d:15::abcd:ef12"),
    98  			52150,
    99  			52150,
   100  		),
   101  	},
   102  	{
   103  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:52150?discport=22334",
   104  		wantResult: NewV4(
   105  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
   106  			net.IP{0x7f, 0x0, 0x0, 0x1},
   107  			52150,
   108  			22334,
   109  		),
   110  	},
   111  	// Incomplete node URLs with no address
   112  	{
   113  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439",
   114  		wantResult: NewV4(
   115  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
   116  			nil, 0, 0,
   117  		),
   118  	},
   119  	// Invalid URLs
   120  	{
   121  		input:     "",
   122  		wantError: errMissingPrefix.Error(),
   123  	},
   124  	{
   125  		input:     "1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439",
   126  		wantError: errMissingPrefix.Error(),
   127  	},
   128  	{
   129  		input:     "01010101",
   130  		wantError: errMissingPrefix.Error(),
   131  	},
   132  	{
   133  		input:     "enode://01010101@123.124.125.126:3",
   134  		wantError: `invalid public key (wrong length, want 128 hex chars)`,
   135  	},
   136  	{
   137  		input:     "enode://01010101",
   138  		wantError: `invalid public key (wrong length, want 128 hex chars)`,
   139  	},
   140  	{
   141  		input:     "http://foobar",
   142  		wantError: errMissingPrefix.Error(),
   143  	},
   144  	{
   145  		input:     "://foo",
   146  		wantError: errMissingPrefix.Error(),
   147  	},
   148  	{
   149  		// Quorum: raft url with invalid hostname (no error, hostname will be saved)
   150  		input:      "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@hostname:3?raftport=50401",
   151  		wantResult: NewV4Hostname(hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), "hostname", 3, 3, 50401),
   152  	},
   153  	{
   154  		// Quorum: raft url with valid hostname
   155  		input:      "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@localhost:3?raftport=50401",
   156  		wantResult: NewV4Hostname(hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), "localhost", 3, 3, 50401),
   157  	},
   158  	{
   159  		// Quorum: raft url with no hostname
   160  		input:     "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@:3?raftport=50401",
   161  		wantError: `empty hostname in raft url`,
   162  	},
   163  }
   164  
   165  func hexPubkey(h string) *ecdsa.PublicKey {
   166  	k, err := parsePubkey(h)
   167  	if err != nil {
   168  		panic(err)
   169  	}
   170  	return k
   171  }
   172  
   173  func TestParseNode(t *testing.T) {
   174  	for _, test := range parseNodeTests {
   175  		n, err := Parse(ValidSchemes, test.input)
   176  		if test.wantError != "" {
   177  			if err == nil {
   178  				t.Errorf("test %q:\n  got nil error, expected %#q", test.input, test.wantError)
   179  				continue
   180  			} else if !strings.Contains(err.Error(), test.wantError) {
   181  				t.Errorf("test %q:\n  got error %#q, expected %#q", test.input, err.Error(), test.wantError)
   182  				continue
   183  			}
   184  		} else {
   185  			if err != nil {
   186  				t.Errorf("test %q:\n  unexpected error: %v", test.input, err)
   187  				continue
   188  			}
   189  			if !reflect.DeepEqual(n, test.wantResult) {
   190  				t.Errorf("test %q:\n  result mismatch:\ngot:  %#v\nwant: %#v", test.input, n, test.wantResult)
   191  			}
   192  		}
   193  	}
   194  }
   195  
   196  func TestNodeString(t *testing.T) {
   197  	for i, test := range parseNodeTests {
   198  		if test.wantError == "" && strings.HasPrefix(test.input, "enode://") {
   199  			str := test.wantResult.String()
   200  			if str != test.input {
   201  				t.Errorf("test %d: Node.String() mismatch:\ngot:  %s\nwant: %s", i, str, test.input)
   202  			}
   203  		}
   204  	}
   205  }