node_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. "net"
  23. "strings"
  24. "testing"
  25. "testing/quick"
  26. "github.com/ethereum/go-ethereum/p2p/enr"
  27. "github.com/ethereum/go-ethereum/rlp"
  28. "github.com/stretchr/testify/assert"
  29. )
  30. var pyRecord, _ = hex.DecodeString("f884b8407098ad865b00a582051940cb9cf36836572411a47278783077011599ed5cd16b76f2635f4e234738f30813a89eb9137e3e3df5266e3a1f11df72ecf1145ccb9c01826964827634826970847f00000189736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31388375647082765f")
  31. // TestPythonInterop checks that we can decode and verify a record produced by the Python
  32. // implementation.
  33. func TestPythonInterop(t *testing.T) {
  34. var r enr.Record
  35. if err := rlp.DecodeBytes(pyRecord, &r); err != nil {
  36. t.Fatalf("can't decode: %v", err)
  37. }
  38. n, err := New(ValidSchemes, &r)
  39. if err != nil {
  40. t.Fatalf("can't verify record: %v", err)
  41. }
  42. var (
  43. wantID = HexID("a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7")
  44. wantSeq = uint64(1)
  45. wantIP = enr.IPv4{127, 0, 0, 1}
  46. wantUDP = enr.UDP(30303)
  47. )
  48. if n.Seq() != wantSeq {
  49. t.Errorf("wrong seq: got %d, want %d", n.Seq(), wantSeq)
  50. }
  51. if n.ID() != wantID {
  52. t.Errorf("wrong id: got %x, want %x", n.ID(), wantID)
  53. }
  54. want := map[enr.Entry]interface{}{new(enr.IPv4): &wantIP, new(enr.UDP): &wantUDP}
  55. for k, v := range want {
  56. desc := fmt.Sprintf("loading key %q", k.ENRKey())
  57. if assert.NoError(t, n.Load(k), desc) {
  58. assert.Equal(t, k, v, desc)
  59. }
  60. }
  61. }
  62. func TestHexID(t *testing.T) {
  63. 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}
  64. id1 := HexID("0x00000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc")
  65. id2 := HexID("00000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc")
  66. if id1 != ref {
  67. t.Errorf("wrong id1\ngot %v\nwant %v", id1[:], ref[:])
  68. }
  69. if id2 != ref {
  70. t.Errorf("wrong id2\ngot %v\nwant %v", id2[:], ref[:])
  71. }
  72. }
  73. func TestID_textEncoding(t *testing.T) {
  74. ref := ID{
  75. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
  76. 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
  77. 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
  78. 0x31, 0x32,
  79. }
  80. hex := "0102030405060708091011121314151617181920212223242526272829303132"
  81. text, err := ref.MarshalText()
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if !bytes.Equal(text, []byte(hex)) {
  86. t.Fatalf("text encoding did not match\nexpected: %s\ngot: %s", hex, text)
  87. }
  88. id := new(ID)
  89. if err := id.UnmarshalText(text); err != nil {
  90. t.Fatal(err)
  91. }
  92. if *id != ref {
  93. t.Fatalf("text decoding did not match\nexpected: %s\ngot: %s", ref, id)
  94. }
  95. }
  96. func TestID_distcmp(t *testing.T) {
  97. distcmpBig := func(target, a, b ID) int {
  98. tbig := new(big.Int).SetBytes(target[:])
  99. abig := new(big.Int).SetBytes(a[:])
  100. bbig := new(big.Int).SetBytes(b[:])
  101. return new(big.Int).Xor(tbig, abig).Cmp(new(big.Int).Xor(tbig, bbig))
  102. }
  103. if err := quick.CheckEqual(DistCmp, distcmpBig, nil); err != nil {
  104. t.Error(err)
  105. }
  106. }
  107. // The random tests is likely to miss the case where a and b are equal,
  108. // this test checks it explicitly.
  109. func TestID_distcmpEqual(t *testing.T) {
  110. base := ID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
  111. x := ID{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
  112. if DistCmp(base, x, x) != 0 {
  113. t.Errorf("DistCmp(base, x, x) != 0")
  114. }
  115. }
  116. func TestID_logdist(t *testing.T) {
  117. logdistBig := func(a, b ID) int {
  118. abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:])
  119. return new(big.Int).Xor(abig, bbig).BitLen()
  120. }
  121. if err := quick.CheckEqual(LogDist, logdistBig, nil); err != nil {
  122. t.Error(err)
  123. }
  124. }
  125. // The random tests is likely to miss the case where a and b are equal,
  126. // this test checks it explicitly.
  127. func TestID_logdistEqual(t *testing.T) {
  128. x := ID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
  129. if LogDist(x, x) != 0 {
  130. t.Errorf("LogDist(x, x) != 0")
  131. }
  132. }
  133. // Quorum
  134. //
  135. // test raft port in node detail
  136. func TestNodeInfoForRaftPort(t *testing.T) {
  137. node := NewV4Hostname(
  138. hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
  139. "192.168.0.1",
  140. 30302,
  141. 30303,
  142. 2021,
  143. )
  144. wantIP := enr.IPv4{192, 168, 0, 1}
  145. wantUdp := 30303
  146. wantTcp := 30302
  147. wantRaftPort := 2021
  148. assert.Equal(t, wantRaftPort, node.RaftPort(), "node raft port mismatch")
  149. assert.Equal(t, net.IP(wantIP), node.IP(), "node ip mismatch")
  150. assert.Equal(t, wantUdp, node.UDP(), "node UDP port mismatch")
  151. assert.Equal(t, wantTcp, node.TCP(), "node TCP port mismatch")
  152. }
  153. // Quorum - test parsing url with hostname (if host is FQDN)
  154. func TestNodeParseUrlWithHostnameForQuorum(t *testing.T) {
  155. var url = "enode://ac6b1096ca56b9f6d004b779ae3728bf83f8e22453404cc3cef16a3d9b96608bc67c4b30db88e0a5a6c6390213f7acbe1153ff6d23ce57380104288ae19373ef@localhost:21000?discport=0&raftport=50401"
  156. n, err := ParseV4(url)
  157. if err != nil {
  158. t.Errorf("parsing host failed %v", err)
  159. }
  160. assert.Equal(t, 50401, n.RaftPort())
  161. url = "enode://ac6b1096ca56b9f6d004b779ae3728bf83f8e22453404cc3cef16a3d9b96608bc67c4b30db88e0a5a6c6390213f7acbe1153ff6d23ce57380104288ae19373ef@localhost1:21000?discport=0&raftport=50401"
  162. _, err = ParseV4(url)
  163. if err != nil {
  164. errMsg := err.Error()
  165. hasError := strings.Contains(errMsg, "no such host")
  166. assert.Equal(t, true, hasError, err.Error())
  167. }
  168. }
  169. // Quorum
  170. // test Incomplete
  171. func TestIncomplete(t *testing.T) {
  172. var testCases = []struct {
  173. n *Node
  174. isIncomplete bool
  175. }{
  176. {
  177. n: NewV4(
  178. hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
  179. net.IP{127, 0, 0, 1},
  180. 52150,
  181. 52150,
  182. ),
  183. isIncomplete: false,
  184. },
  185. {
  186. n: NewV4(hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
  187. net.ParseIP("::"),
  188. 52150,
  189. 52150,
  190. ),
  191. isIncomplete: false,
  192. },
  193. {
  194. n: NewV4(
  195. hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
  196. net.ParseIP("2001:db8:3c4d:15::abcd:ef12"),
  197. 52150,
  198. 52150,
  199. ),
  200. isIncomplete: false,
  201. },
  202. {
  203. n: NewV4(
  204. hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
  205. nil,
  206. 52150,
  207. 52150,
  208. ),
  209. isIncomplete: true,
  210. },
  211. {
  212. n: NewV4Hostname(
  213. hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
  214. "hostname",
  215. 52150,
  216. 52150,
  217. 50400,
  218. ),
  219. isIncomplete: false,
  220. },
  221. {
  222. n: NewV4Hostname(
  223. hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
  224. "hostname",
  225. 52150,
  226. 52150,
  227. 0,
  228. ),
  229. isIncomplete: true,
  230. },
  231. {
  232. n: NewV4Hostname(
  233. hexPubkey("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"),
  234. "",
  235. 52150,
  236. 52150,
  237. 50400,
  238. ),
  239. isIncomplete: true,
  240. },
  241. }
  242. for i, test := range testCases {
  243. if test.n.Incomplete() != test.isIncomplete {
  244. t.Errorf("test %d: Node.Incomplete() mismatch:\ngot: %v\nwant: %v", i, test.n.Incomplete(), test.isIncomplete)
  245. }
  246. }
  247. }