node_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 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 trie
  17. import (
  18. "bytes"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/rlp"
  21. )
  22. func newTestFullNode(v []byte) []interface{} {
  23. fullNodeData := []interface{}{}
  24. for i := 0; i < 16; i++ {
  25. k := bytes.Repeat([]byte{byte(i + 1)}, 32)
  26. fullNodeData = append(fullNodeData, k)
  27. }
  28. fullNodeData = append(fullNodeData, v)
  29. return fullNodeData
  30. }
  31. func TestDecodeNestedNode(t *testing.T) {
  32. fullNodeData := newTestFullNode([]byte("fullnode"))
  33. data := [][]byte{}
  34. for i := 0; i < 16; i++ {
  35. data = append(data, nil)
  36. }
  37. data = append(data, []byte("subnode"))
  38. fullNodeData[15] = data
  39. buf := bytes.NewBuffer([]byte{})
  40. rlp.Encode(buf, fullNodeData)
  41. if _, err := decodeNode([]byte("testdecode"), buf.Bytes()); err != nil {
  42. t.Fatalf("decode nested full node err: %v", err)
  43. }
  44. }
  45. func TestDecodeFullNodeWrongSizeChild(t *testing.T) {
  46. fullNodeData := newTestFullNode([]byte("wrongsizechild"))
  47. fullNodeData[0] = []byte("00")
  48. buf := bytes.NewBuffer([]byte{})
  49. rlp.Encode(buf, fullNodeData)
  50. _, err := decodeNode([]byte("testdecode"), buf.Bytes())
  51. if _, ok := err.(*decodeError); !ok {
  52. t.Fatalf("decodeNode returned wrong err: %v", err)
  53. }
  54. }
  55. func TestDecodeFullNodeWrongNestedFullNode(t *testing.T) {
  56. fullNodeData := newTestFullNode([]byte("fullnode"))
  57. data := [][]byte{}
  58. for i := 0; i < 16; i++ {
  59. data = append(data, []byte("123456"))
  60. }
  61. data = append(data, []byte("subnode"))
  62. fullNodeData[15] = data
  63. buf := bytes.NewBuffer([]byte{})
  64. rlp.Encode(buf, fullNodeData)
  65. _, err := decodeNode([]byte("testdecode"), buf.Bytes())
  66. if _, ok := err.(*decodeError); !ok {
  67. t.Fatalf("decodeNode returned wrong err: %v", err)
  68. }
  69. }
  70. func TestDecodeFullNode(t *testing.T) {
  71. fullNodeData := newTestFullNode([]byte("decodefullnode"))
  72. buf := bytes.NewBuffer([]byte{})
  73. rlp.Encode(buf, fullNodeData)
  74. _, err := decodeNode([]byte("testdecode"), buf.Bytes())
  75. if err != nil {
  76. t.Fatalf("decode full node err: %v", err)
  77. }
  78. }