github.com/core-coin/go-core/v2@v2.1.9/p2p/enode/urlv4_test.go (about)

     1  // Copyright 2018 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package enode
    18  
    19  import (
    20  	"errors"
    21  	"net"
    22  	"reflect"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/core-coin/go-core/v2/crypto"
    27  	"github.com/core-coin/go-core/v2/p2p/enr"
    28  )
    29  
    30  func init() {
    31  	lookupIPFunc = func(name string) ([]net.IP, error) {
    32  		if name == "node.example.org" {
    33  			return []net.IP{{33, 44, 55, 66}}, nil
    34  		}
    35  		return nil, errors.New("no such host")
    36  	}
    37  }
    38  
    39  var parseNodeTests = []struct {
    40  	input      string
    41  	wantError  string
    42  	wantResult *Node
    43  }{
    44  	// Records
    45  	{
    46  		input: "enr:-QEIuKuk5dq-y6V-njXo1mgo6ETegrTMMdpiN7snT31ebR2RbMp68L5qzSi3AOFPaE70U5gXyaqnxSXEuwCrW-9ElZ4GjrTXOK_-ovGusc5_npVF4ITl6Z6hbOqIG4yV0_VdT9Gmwf1BUh4Ylggtf0Z6jjJ9GAAC_L2OxBWwhT3M3rdzJE9s8bVcyfOEk3tXJwwQQLlCFQGnGRF3DIr82yGNZQcHrBESaKfilTyQJ4BjgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxuDkC_L2OxBWwhT3M3rdzJE9s8bVcyfOEk3tXJwwQQLlCFQGnGRF3DIr82yGNZQcHrBESaKfilTyQJ4CDdWRwgnZc",
    47  		wantResult: func() *Node {
    48  			testKey, _ := crypto.UnmarshalPrivateKeyHex("07e988804055546babfb00e34d015314a21a76a1cb049cad4adeb3d931af355f2393ba45bfda9aeb7ca40c1e0a4e63ba4639e43957a54109f2")
    49  			var r enr.Record
    50  			r.Set(enr.IP{127, 0, 0, 1})
    51  			r.Set(enr.UDP(30300))
    52  			r.SetSeq(99)
    53  			SignV4(&r, testKey)
    54  			n, _ := New(ValidSchemes, &r)
    55  			return n
    56  		}(),
    57  	},
    58  	// Invalid Records
    59  	{
    60  		input:     "enr:",
    61  		wantError: "EOF", // could be nicer
    62  	},
    63  	{
    64  		input:     "enr:x",
    65  		wantError: "illegal base64 data at input byte 0",
    66  	},
    67  	{
    68  		input:     "enr:-EOAY4JpZIRudWxsgmlwhH8AAAGIbnVsbGFkZHKgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACDdWRwgnZf",
    69  		wantError: enr.ErrInvalidSig.Error(),
    70  	},
    71  	// Complete node URLs with IP address and ports
    72  	{
    73  		input:     "enode://767b2d3eb5828a9c2e11d1e9aa515f0435ee7ce80f3749ed33f462921587bc9bb55a7231b2e79ae6ce86e8ff4f83e9a151e855d6bbe87838aa@invalid.:3",
    74  		wantError: `no such host`,
    75  	},
    76  	{
    77  		input:     "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa@127.0.0.1:foo",
    78  		wantError: `invalid port`,
    79  	},
    80  	{
    81  		input:     "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa@127.0.0.1:3?discport=foo",
    82  		wantError: `invalid discport in query`,
    83  	},
    84  	{
    85  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa@127.0.0.1:52150",
    86  		wantResult: NewV4(
    87  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa"),
    88  			net.IP{127, 0, 0, 1},
    89  			52150,
    90  			52150,
    91  		),
    92  	},
    93  	{
    94  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa@[::]:52150",
    95  		wantResult: NewV4(
    96  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa"),
    97  			net.ParseIP("::"),
    98  			52150,
    99  			52150,
   100  		),
   101  	},
   102  	{
   103  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa@[2001:db8:3c4d:15::abcd:ef12]:52150",
   104  		wantResult: NewV4(
   105  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa"),
   106  			net.ParseIP("2001:db8:3c4d:15::abcd:ef12"),
   107  			52150,
   108  			52150,
   109  		),
   110  	},
   111  	{
   112  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa@127.0.0.1:52150?discport=22334",
   113  		wantResult: NewV4(
   114  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa"),
   115  			net.IP{0x7f, 0x0, 0x0, 0x1},
   116  			52150,
   117  			22334,
   118  		),
   119  	},
   120  	// Incomplete node URLs with no address
   121  	{
   122  		input: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa",
   123  		wantResult: NewV4(
   124  			hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa"),
   125  			nil, 0, 0,
   126  		),
   127  	},
   128  	// Invalid URLs
   129  	{
   130  		input:     "",
   131  		wantError: errMissingPrefix.Error(),
   132  	},
   133  	{
   134  		input:     "1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f632aa",
   135  		wantError: errMissingPrefix.Error(),
   136  	},
   137  	{
   138  		input:     "01010101",
   139  		wantError: errMissingPrefix.Error(),
   140  	},
   141  	{
   142  		input:     "enode://01010101@123.124.125.126:3",
   143  		wantError: `invalid public key (wrong length, want 114 hex chars)`,
   144  	},
   145  	{
   146  		input:     "enode://01010101",
   147  		wantError: `invalid public key (wrong length, want 114 hex chars)`,
   148  	},
   149  	{
   150  		input:     "http://foobar",
   151  		wantError: errMissingPrefix.Error(),
   152  	},
   153  	{
   154  		input:     "://foo",
   155  		wantError: errMissingPrefix.Error(),
   156  	},
   157  }
   158  
   159  func hexPubkey(h string) *crypto.PublicKey {
   160  	k, err := parsePubkey(h)
   161  	if err != nil {
   162  		panic(err)
   163  	}
   164  	return k
   165  }
   166  
   167  func TestParseNode(t *testing.T) {
   168  	for _, test := range parseNodeTests {
   169  		n, err := Parse(ValidSchemes, test.input)
   170  		if test.wantError != "" {
   171  			if err == nil {
   172  				t.Errorf("test %q:\n  got nil error, expected %#q", test.input, test.wantError)
   173  				continue
   174  			} else if !strings.Contains(err.Error(), test.wantError) {
   175  				t.Errorf("test %q:\n  got error %#q, expected %#q", test.input, err.Error(), test.wantError)
   176  				continue
   177  			}
   178  		} else {
   179  			if err != nil {
   180  				t.Errorf("test %q:\n  unexpected error: %v", test.input, err)
   181  				continue
   182  			}
   183  			if !reflect.DeepEqual(n, test.wantResult) {
   184  				t.Errorf("test %q:\n  result mismatch:\ngot:  %#v\nwant: %#v", test.input, n, test.wantResult)
   185  			}
   186  		}
   187  	}
   188  }
   189  
   190  func TestNodeString(t *testing.T) {
   191  	for i, test := range parseNodeTests {
   192  		if test.wantError == "" && strings.HasPrefix(test.input, "enode://") {
   193  			str := test.wantResult.String()
   194  			if str != test.input {
   195  				t.Errorf("test %d: Node.String() mismatch:\ngot:  %s\nwant: %s", i, str, test.input)
   196  			}
   197  		}
   198  	}
   199  }