PageRenderTime 220ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/p2p/enr/enr_test.go

https://gitlab.com/Adi128wiraguna/go-pulse
Go | 319 lines | 240 code | 47 blank | 32 comment | 26 complexity | 14b7b2fe8012c9e349b2b5fbcd711c7a MD5 | raw file
  1. // Copyright 2017 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 enr
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "math/rand"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. "github.com/stretchr/testify/assert"
  26. "github.com/stretchr/testify/require"
  27. )
  28. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  29. func randomString(strlen int) string {
  30. b := make([]byte, strlen)
  31. rnd.Read(b)
  32. return string(b)
  33. }
  34. // TestGetSetID tests encoding/decoding and setting/getting of the ID key.
  35. func TestGetSetID(t *testing.T) {
  36. id := ID("someid")
  37. var r Record
  38. r.Set(id)
  39. var id2 ID
  40. require.NoError(t, r.Load(&id2))
  41. assert.Equal(t, id, id2)
  42. }
  43. // TestGetSetIP4 tests encoding/decoding and setting/getting of the IP key.
  44. func TestGetSetIPv4(t *testing.T) {
  45. ip := IPv4{192, 168, 0, 3}
  46. var r Record
  47. r.Set(ip)
  48. var ip2 IPv4
  49. require.NoError(t, r.Load(&ip2))
  50. assert.Equal(t, ip, ip2)
  51. }
  52. // TestGetSetIP6 tests encoding/decoding and setting/getting of the IP6 key.
  53. func TestGetSetIPv6(t *testing.T) {
  54. ip := IPv6{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}
  55. var r Record
  56. r.Set(ip)
  57. var ip2 IPv6
  58. require.NoError(t, r.Load(&ip2))
  59. assert.Equal(t, ip, ip2)
  60. }
  61. // TestGetSetUDP tests encoding/decoding and setting/getting of the UDP key.
  62. func TestGetSetUDP(t *testing.T) {
  63. port := UDP(30309)
  64. var r Record
  65. r.Set(port)
  66. var port2 UDP
  67. require.NoError(t, r.Load(&port2))
  68. assert.Equal(t, port, port2)
  69. }
  70. func TestLoadErrors(t *testing.T) {
  71. var r Record
  72. ip4 := IPv4{127, 0, 0, 1}
  73. r.Set(ip4)
  74. // Check error for missing keys.
  75. var udp UDP
  76. err := r.Load(&udp)
  77. if !IsNotFound(err) {
  78. t.Error("IsNotFound should return true for missing key")
  79. }
  80. assert.Equal(t, &KeyError{Key: udp.ENRKey(), Err: errNotFound}, err)
  81. // Check error for invalid keys.
  82. var list []uint
  83. err = r.Load(WithEntry(ip4.ENRKey(), &list))
  84. kerr, ok := err.(*KeyError)
  85. if !ok {
  86. t.Fatalf("expected KeyError, got %T", err)
  87. }
  88. assert.Equal(t, kerr.Key, ip4.ENRKey())
  89. assert.Error(t, kerr.Err)
  90. if IsNotFound(err) {
  91. t.Error("IsNotFound should return false for decoding errors")
  92. }
  93. }
  94. // TestSortedGetAndSet tests that Set produced a sorted pairs slice.
  95. func TestSortedGetAndSet(t *testing.T) {
  96. type pair struct {
  97. k string
  98. v uint32
  99. }
  100. for _, tt := range []struct {
  101. input []pair
  102. want []pair
  103. }{
  104. {
  105. input: []pair{{"a", 1}, {"c", 2}, {"b", 3}},
  106. want: []pair{{"a", 1}, {"b", 3}, {"c", 2}},
  107. },
  108. {
  109. input: []pair{{"a", 1}, {"c", 2}, {"b", 3}, {"d", 4}, {"a", 5}, {"bb", 6}},
  110. want: []pair{{"a", 5}, {"b", 3}, {"bb", 6}, {"c", 2}, {"d", 4}},
  111. },
  112. {
  113. input: []pair{{"c", 2}, {"b", 3}, {"d", 4}, {"a", 5}, {"bb", 6}},
  114. want: []pair{{"a", 5}, {"b", 3}, {"bb", 6}, {"c", 2}, {"d", 4}},
  115. },
  116. } {
  117. var r Record
  118. for _, i := range tt.input {
  119. r.Set(WithEntry(i.k, &i.v))
  120. }
  121. for i, w := range tt.want {
  122. // set got's key from r.pair[i], so that we preserve order of pairs
  123. got := pair{k: r.pairs[i].k}
  124. assert.NoError(t, r.Load(WithEntry(w.k, &got.v)))
  125. assert.Equal(t, w, got)
  126. }
  127. }
  128. }
  129. // TestDirty tests record signature removal on setting of new key/value pair in record.
  130. func TestDirty(t *testing.T) {
  131. var r Record
  132. if _, err := rlp.EncodeToBytes(r); err != errEncodeUnsigned {
  133. t.Errorf("expected errEncodeUnsigned, got %#v", err)
  134. }
  135. require.NoError(t, signTest([]byte{5}, &r))
  136. if len(r.signature) == 0 {
  137. t.Error("record is not signed")
  138. }
  139. _, err := rlp.EncodeToBytes(r)
  140. assert.NoError(t, err)
  141. r.SetSeq(3)
  142. if len(r.signature) != 0 {
  143. t.Error("signature still set after modification")
  144. }
  145. if _, err := rlp.EncodeToBytes(r); err != errEncodeUnsigned {
  146. t.Errorf("expected errEncodeUnsigned, got %#v", err)
  147. }
  148. }
  149. func TestSeq(t *testing.T) {
  150. var r Record
  151. assert.Equal(t, uint64(0), r.Seq())
  152. r.Set(UDP(1))
  153. assert.Equal(t, uint64(0), r.Seq())
  154. signTest([]byte{5}, &r)
  155. assert.Equal(t, uint64(0), r.Seq())
  156. r.Set(UDP(2))
  157. assert.Equal(t, uint64(1), r.Seq())
  158. }
  159. // TestGetSetOverwrite tests value overwrite when setting a new value with an existing key in record.
  160. func TestGetSetOverwrite(t *testing.T) {
  161. var r Record
  162. ip := IPv4{192, 168, 0, 3}
  163. r.Set(ip)
  164. ip2 := IPv4{192, 168, 0, 4}
  165. r.Set(ip2)
  166. var ip3 IPv4
  167. require.NoError(t, r.Load(&ip3))
  168. assert.Equal(t, ip2, ip3)
  169. }
  170. // TestSignEncodeAndDecode tests signing, RLP encoding and RLP decoding of a record.
  171. func TestSignEncodeAndDecode(t *testing.T) {
  172. var r Record
  173. r.Set(UDP(30303))
  174. r.Set(IPv4{127, 0, 0, 1})
  175. require.NoError(t, signTest([]byte{5}, &r))
  176. blob, err := rlp.EncodeToBytes(r)
  177. require.NoError(t, err)
  178. var r2 Record
  179. require.NoError(t, rlp.DecodeBytes(blob, &r2))
  180. assert.Equal(t, r, r2)
  181. blob2, err := rlp.EncodeToBytes(r2)
  182. require.NoError(t, err)
  183. assert.Equal(t, blob, blob2)
  184. }
  185. // TestRecordTooBig tests that records bigger than SizeLimit bytes cannot be signed.
  186. func TestRecordTooBig(t *testing.T) {
  187. var r Record
  188. key := randomString(10)
  189. // set a big value for random key, expect error
  190. r.Set(WithEntry(key, randomString(SizeLimit)))
  191. if err := signTest([]byte{5}, &r); err != errTooBig {
  192. t.Fatalf("expected to get errTooBig, got %#v", err)
  193. }
  194. // set an acceptable value for random key, expect no error
  195. r.Set(WithEntry(key, randomString(100)))
  196. require.NoError(t, signTest([]byte{5}, &r))
  197. }
  198. // This checks that incomplete RLP inputs are handled correctly.
  199. func TestDecodeIncomplete(t *testing.T) {
  200. type decTest struct {
  201. input []byte
  202. err error
  203. }
  204. tests := []decTest{
  205. {[]byte{0xC0}, errIncompleteList},
  206. {[]byte{0xC1, 0x1}, errIncompleteList},
  207. {[]byte{0xC2, 0x1, 0x2}, nil},
  208. {[]byte{0xC3, 0x1, 0x2, 0x3}, errIncompletePair},
  209. {[]byte{0xC4, 0x1, 0x2, 0x3, 0x4}, nil},
  210. {[]byte{0xC5, 0x1, 0x2, 0x3, 0x4, 0x5}, errIncompletePair},
  211. }
  212. for _, test := range tests {
  213. var r Record
  214. err := rlp.DecodeBytes(test.input, &r)
  215. if err != test.err {
  216. t.Errorf("wrong error for %X: %v", test.input, err)
  217. }
  218. }
  219. }
  220. // TestSignEncodeAndDecodeRandom tests encoding/decoding of records containing random key/value pairs.
  221. func TestSignEncodeAndDecodeRandom(t *testing.T) {
  222. var r Record
  223. // random key/value pairs for testing
  224. pairs := map[string]uint32{}
  225. for i := 0; i < 10; i++ {
  226. key := randomString(7)
  227. value := rnd.Uint32()
  228. pairs[key] = value
  229. r.Set(WithEntry(key, &value))
  230. }
  231. require.NoError(t, signTest([]byte{5}, &r))
  232. _, err := rlp.EncodeToBytes(r)
  233. require.NoError(t, err)
  234. for k, v := range pairs {
  235. desc := fmt.Sprintf("key %q", k)
  236. var got uint32
  237. buf := WithEntry(k, &got)
  238. require.NoError(t, r.Load(buf), desc)
  239. require.Equal(t, v, got, desc)
  240. }
  241. }
  242. type testSig struct{}
  243. type testID []byte
  244. func (id testID) ENRKey() string { return "testid" }
  245. func signTest(id []byte, r *Record) error {
  246. r.Set(ID("test"))
  247. r.Set(testID(id))
  248. return r.SetSig(testSig{}, makeTestSig(id, r.Seq()))
  249. }
  250. func makeTestSig(id []byte, seq uint64) []byte {
  251. sig := make([]byte, 8, len(id)+8)
  252. binary.BigEndian.PutUint64(sig[:8], seq)
  253. sig = append(sig, id...)
  254. return sig
  255. }
  256. func (testSig) Verify(r *Record, sig []byte) error {
  257. var id []byte
  258. if err := r.Load((*testID)(&id)); err != nil {
  259. return err
  260. }
  261. if !bytes.Equal(sig, makeTestSig(id, r.Seq())) {
  262. return ErrInvalidSig
  263. }
  264. return nil
  265. }
  266. func (testSig) NodeAddr(r *Record) []byte {
  267. var id []byte
  268. if err := r.Load((*testID)(&id)); err != nil {
  269. return nil
  270. }
  271. return id
  272. }