github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/p2p/discover/v4wire/v4wire_test.go (about)

     1  // Copyright 2020 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 v4wire
    18  
    19  import (
    20  	"encoding/hex"
    21  	"net"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/davecgh/go-spew/spew"
    26  	"github.com/electroneum/electroneum-sc/crypto"
    27  	"github.com/electroneum/electroneum-sc/rlp"
    28  )
    29  
    30  // EIP-8 test vectors.
    31  var testPackets = []struct {
    32  	input      string
    33  	wantPacket interface{}
    34  }{
    35  	{
    36  		input: "aa1087571ea53b96967a6599082e711d0c414562c0bba65755b1879ef00b47346193cf3ad7800f584c21bfbedf8256d40d2faefcf47b47d02e36fa7806634957649ed794a3df42be6df709c3817cf0986a38ce39303344711227142e184cb3aa011fea04cb847f000001820cfa8215a8d790000000000000000000000000000000018208ae820d058443b9a355",
    37  		wantPacket: &Ping{
    38  			Version:    4,
    39  			From:       Endpoint{net.ParseIP("127.0.0.1").To4(), 3322, 5544},
    40  			To:         Endpoint{net.ParseIP("::1"), 2222, 3333},
    41  			Expiration: 1136239445,
    42  		},
    43  	},
    44  	{
    45  		input: "9b89f307f14c906a26dda6d0b6b18c46a429da177edec5376efcf135af8d9b916b05e2be910ce64211120e1e445dce033114585e81828b5da8841303930becef41ffd18006749210c0f641eea072c81860fa8ecf49006fed32562957555025b5011fec04cb847f000001820cfa8215a8d790000000000000000000000000000000018208ae820d058443b9a3550102",
    46  		wantPacket: &Ping{
    47  			Version:    4,
    48  			From:       Endpoint{net.ParseIP("127.0.0.1").To4(), 3322, 5544},
    49  			To:         Endpoint{net.ParseIP("::1"), 2222, 3333},
    50  			Expiration: 1136239445,
    51  			ENRSeq:     1,
    52  			Rest:       []rlp.RawValue{{0x02}},
    53  		},
    54  	},
    55  	{
    56  		input: "a505b6d76fd0e547153b73acc2ff3d9dc3fef7a410e1322a2ce9d285c1e56023dd230f82e538d19ac619b392d70fdd3714258523ffad30c532461b1e3219eb6452d2d4491f57886648cff8e2a1e0481809fd0e42dff5fc1fb57ff45ef5b0b6650121f84eb840ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f8443b9a35582999983999999",
    57  		wantPacket: &Findnode{
    58  			Target:     hexPubkey("ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f"),
    59  			Expiration: 1136239445,
    60  			Rest:       []rlp.RawValue{{0x82, 0x99, 0x99}, {0x83, 0x99, 0x99, 0x99}},
    61  		},
    62  	},
    63  	{
    64  		input: "c3bc42bfa7884f543112c69ff89ea602933482524f873c6099149f3c2722f67f7ea9fc33769ce44b4127fa83c71fc913757095d3f3b1c658a352bcc228e375b70d664cf15a844239e9212850200a500fbfbfa460e93bf6547942d5d04ee5cac20022f9015bf90150f84d846321163782115c82115db8403155e1427f85f10a5c9a7755877748041af1bcd8d474ec065eb33df57a97babf54bfd2103575fa829115d224c523596b401065a97f74010610fce76382c0bf32f84984010203040101b840312c55512422cf9b8a4097e9a6ad79402e87a15ae909a4bfefa22398f03d20951933beea1e4dfa6f968212385e829f04c2d314fc2d4e255e0d3bc08792b069dbf8599020010db83c4d001500000000abcdef12820d05820d05b84038643200b172dcfef857492156971f0e6aa2c538d8b74010f8e140811d53b98c765dd2d96126051913f44582e8c199ad7c6d6819e9a56483f637feaac9448aacf8599020010db885a308d313198a2e037073488203e78203e8b8408dcab8618c3253b558d459da53bd8fa68935a719aff8b811197101a4b2b47dd2d47295286fc00cc081bb542d760717d1bdd6bec2c37cd72eca367d6dd3b9df738443b9a355010203",
    65  		wantPacket: &Neighbors{
    66  			Nodes: []Node{
    67  				{
    68  					ID:  hexPubkey("3155e1427f85f10a5c9a7755877748041af1bcd8d474ec065eb33df57a97babf54bfd2103575fa829115d224c523596b401065a97f74010610fce76382c0bf32"),
    69  					IP:  net.ParseIP("99.33.22.55").To4(),
    70  					UDP: 4444,
    71  					TCP: 4445,
    72  				},
    73  				{
    74  					ID:  hexPubkey("312c55512422cf9b8a4097e9a6ad79402e87a15ae909a4bfefa22398f03d20951933beea1e4dfa6f968212385e829f04c2d314fc2d4e255e0d3bc08792b069db"),
    75  					IP:  net.ParseIP("1.2.3.4").To4(),
    76  					UDP: 1,
    77  					TCP: 1,
    78  				},
    79  				{
    80  					ID:  hexPubkey("38643200b172dcfef857492156971f0e6aa2c538d8b74010f8e140811d53b98c765dd2d96126051913f44582e8c199ad7c6d6819e9a56483f637feaac9448aac"),
    81  					IP:  net.ParseIP("2001:db8:3c4d:15::abcd:ef12"),
    82  					UDP: 3333,
    83  					TCP: 3333,
    84  				},
    85  				{
    86  					ID:  hexPubkey("8dcab8618c3253b558d459da53bd8fa68935a719aff8b811197101a4b2b47dd2d47295286fc00cc081bb542d760717d1bdd6bec2c37cd72eca367d6dd3b9df73"),
    87  					IP:  net.ParseIP("2001:db8:85a3:8d3:1319:8a2e:370:7348"),
    88  					UDP: 999,
    89  					TCP: 1000,
    90  				},
    91  			},
    92  			Expiration: 1136239445,
    93  			Rest:       []rlp.RawValue{{0x01}, {0x02}, {0x03}},
    94  		},
    95  	},
    96  }
    97  
    98  // This test checks that the decoder accepts packets according to EIP-8.
    99  func TestForwardCompatibility(t *testing.T) {
   100  	testkey, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
   101  	wantNodeKey := EncodePubkey(&testkey.PublicKey)
   102  
   103  	for _, test := range testPackets {
   104  		input, err := hex.DecodeString(test.input)
   105  		if err != nil {
   106  			t.Fatalf("invalid hex: %s", test.input)
   107  		}
   108  		packet, nodekey, _, err := Decode(input)
   109  		if err != nil {
   110  			t.Errorf("did not accept packet %s\n%v", test.input, err)
   111  			continue
   112  		}
   113  		if !reflect.DeepEqual(packet, test.wantPacket) {
   114  			t.Errorf("got %s\nwant %s", spew.Sdump(packet), spew.Sdump(test.wantPacket))
   115  		}
   116  		if nodekey != wantNodeKey {
   117  			t.Errorf("got id %v\nwant id %v", nodekey, wantNodeKey)
   118  		}
   119  	}
   120  }
   121  
   122  func hexPubkey(h string) (ret Pubkey) {
   123  	b, err := hex.DecodeString(h)
   124  	if err != nil {
   125  		panic(err)
   126  	}
   127  	if len(b) != len(ret) {
   128  		panic("invalid length")
   129  	}
   130  	copy(ret[:], b)
   131  	return ret
   132  }