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