github.com/core-coin/go-core/v2@v2.1.9/p2p/enode/node_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  	"bytes"
    21  	"math/big"
    22  	"testing"
    23  	"testing/quick"
    24  )
    25  
    26  func TestHexID(t *testing.T) {
    27  	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}
    28  	id1 := HexID("0x00000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc")
    29  	id2 := HexID("00000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc")
    30  
    31  	if id1 != ref {
    32  		t.Errorf("wrong id1\ngot  %v\nwant %v", id1[:], ref[:])
    33  	}
    34  	if id2 != ref {
    35  		t.Errorf("wrong id2\ngot  %v\nwant %v", id2[:], ref[:])
    36  	}
    37  }
    38  
    39  func TestID_textEncoding(t *testing.T) {
    40  	ref := ID{
    41  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
    42  		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
    43  		0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
    44  		0x31, 0x32,
    45  	}
    46  	hex := "0102030405060708091011121314151617181920212223242526272829303132"
    47  
    48  	text, err := ref.MarshalText()
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	if !bytes.Equal(text, []byte(hex)) {
    53  		t.Fatalf("text encoding did not match\nexpected: %s\ngot:      %s", hex, text)
    54  	}
    55  
    56  	id := new(ID)
    57  	if err := id.UnmarshalText(text); err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if *id != ref {
    61  		t.Fatalf("text decoding did not match\nexpected: %s\ngot:      %s", ref, id)
    62  	}
    63  }
    64  
    65  func TestID_distcmp(t *testing.T) {
    66  	distcmpBig := func(target, a, b ID) int {
    67  		tbig := new(big.Int).SetBytes(target[:])
    68  		abig := new(big.Int).SetBytes(a[:])
    69  		bbig := new(big.Int).SetBytes(b[:])
    70  		return new(big.Int).Xor(tbig, abig).Cmp(new(big.Int).Xor(tbig, bbig))
    71  	}
    72  	if err := quick.CheckEqual(DistCmp, distcmpBig, nil); err != nil {
    73  		t.Error(err)
    74  	}
    75  }
    76  
    77  // The random tests is likely to miss the case where a and b are equal,
    78  // this test checks it explicitly.
    79  func TestID_distcmpEqual(t *testing.T) {
    80  	base := ID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    81  	x := ID{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
    82  	if DistCmp(base, x, x) != 0 {
    83  		t.Errorf("DistCmp(base, x, x) != 0")
    84  	}
    85  }
    86  
    87  func TestID_logdist(t *testing.T) {
    88  	logdistBig := func(a, b ID) int {
    89  		abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:])
    90  		return new(big.Int).Xor(abig, bbig).BitLen()
    91  	}
    92  	if err := quick.CheckEqual(LogDist, logdistBig, nil); err != nil {
    93  		t.Error(err)
    94  	}
    95  }
    96  
    97  // The random tests is likely to miss the case where a and b are equal,
    98  // this test checks it explicitly.
    99  func TestID_logdistEqual(t *testing.T) {
   100  	x := ID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
   101  	if LogDist(x, x) != 0 {
   102  		t.Errorf("LogDist(x, x) != 0")
   103  	}
   104  }