node_test.go 4.6 KB

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