github.com/ApeGame/aac@v1.9.7/p2p/enode/node_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  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"math/big"
    24  	"testing"
    25  	"testing/quick"
    26  
    27  	"github.com/ethereum/go-ethereum/p2p/enr"
    28  	"github.com/ethereum/go-ethereum/rlp"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  var pyRecord, _ = hex.DecodeString("f884b8407098ad865b00a582051940cb9cf36836572411a47278783077011599ed5cd16b76f2635f4e234738f30813a89eb9137e3e3df5266e3a1f11df72ecf1145ccb9c01826964827634826970847f00000189736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31388375647082765f")
    33  
    34  // TestPythonInterop checks that we can decode and verify a record produced by the Python
    35  // implementation.
    36  func TestPythonInterop(t *testing.T) {
    37  	var r enr.Record
    38  	if err := rlp.DecodeBytes(pyRecord, &r); err != nil {
    39  		t.Fatalf("can't decode: %v", err)
    40  	}
    41  	n, err := New(ValidSchemes, &r)
    42  	if err != nil {
    43  		t.Fatalf("can't verify record: %v", err)
    44  	}
    45  
    46  	var (
    47  		wantID  = HexID("a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7")
    48  		wantSeq = uint64(1)
    49  		wantIP  = enr.IPv4{127, 0, 0, 1}
    50  		wantUDP = enr.UDP(30303)
    51  	)
    52  	if n.Seq() != wantSeq {
    53  		t.Errorf("wrong seq: got %d, want %d", n.Seq(), wantSeq)
    54  	}
    55  	if n.ID() != wantID {
    56  		t.Errorf("wrong id: got %x, want %x", n.ID(), wantID)
    57  	}
    58  	want := map[enr.Entry]interface{}{new(enr.IPv4): &wantIP, new(enr.UDP): &wantUDP}
    59  	for k, v := range want {
    60  		desc := fmt.Sprintf("loading key %q", k.ENRKey())
    61  		if assert.NoError(t, n.Load(k), desc) {
    62  			assert.Equal(t, k, v, desc)
    63  		}
    64  	}
    65  }
    66  
    67  func TestHexID(t *testing.T) {
    68  	ref := ID{0, 0, 0, 0, 0, 0, 0, 128, 106, 217, 182, 31, 165, 174, 1, 67, 7, 235, 220, 150, 66, 83, 173, 205, 159, 44, 10, 57, 42, 161, 26, 188}
    69  	id1 := HexID("0x00000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc")
    70  	id2 := HexID("00000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc")
    71  
    72  	if id1 != ref {
    73  		t.Errorf("wrong id1\ngot  %v\nwant %v", id1[:], ref[:])
    74  	}
    75  	if id2 != ref {
    76  		t.Errorf("wrong id2\ngot  %v\nwant %v", id2[:], ref[:])
    77  	}
    78  }
    79  
    80  func TestID_textEncoding(t *testing.T) {
    81  	ref := ID{
    82  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
    83  		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
    84  		0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
    85  		0x31, 0x32,
    86  	}
    87  	hex := "0102030405060708091011121314151617181920212223242526272829303132"
    88  
    89  	text, err := ref.MarshalText()
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if !bytes.Equal(text, []byte(hex)) {
    94  		t.Fatalf("text encoding did not match\nexpected: %s\ngot:      %s", hex, text)
    95  	}
    96  
    97  	id := new(ID)
    98  	if err := id.UnmarshalText(text); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	if *id != ref {
   102  		t.Fatalf("text decoding did not match\nexpected: %s\ngot:      %s", ref, id)
   103  	}
   104  }
   105  
   106  func TestID_distcmp(t *testing.T) {
   107  	distcmpBig := func(target, a, b ID) int {
   108  		tbig := new(big.Int).SetBytes(target[:])
   109  		abig := new(big.Int).SetBytes(a[:])
   110  		bbig := new(big.Int).SetBytes(b[:])
   111  		return new(big.Int).Xor(tbig, abig).Cmp(new(big.Int).Xor(tbig, bbig))
   112  	}
   113  	if err := quick.CheckEqual(DistCmp, distcmpBig, nil); err != nil {
   114  		t.Error(err)
   115  	}
   116  }
   117  
   118  // The random tests is likely to miss the case where a and b are equal,
   119  // this test checks it explicitly.
   120  func TestID_distcmpEqual(t *testing.T) {
   121  	base := ID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
   122  	x := ID{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
   123  	if DistCmp(base, x, x) != 0 {
   124  		t.Errorf("DistCmp(base, x, x) != 0")
   125  	}
   126  }
   127  
   128  func TestID_logdist(t *testing.T) {
   129  	logdistBig := func(a, b ID) int {
   130  		abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:])
   131  		return new(big.Int).Xor(abig, bbig).BitLen()
   132  	}
   133  	if err := quick.CheckEqual(LogDist, logdistBig, nil); err != nil {
   134  		t.Error(err)
   135  	}
   136  }
   137  
   138  // The random tests is likely to miss the case where a and b are equal,
   139  // this test checks it explicitly.
   140  func TestID_logdistEqual(t *testing.T) {
   141  	x := ID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
   142  	if LogDist(x, x) != 0 {
   143  		t.Errorf("LogDist(x, x) != 0")
   144  	}
   145  }