secure_trie_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2015 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/common"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. )
  24. func newEmptySecure() *SecureTrie {
  25. db, _ := ethdb.NewMemDatabase()
  26. trie, _ := NewSecure(common.Hash{}, db)
  27. return trie
  28. }
  29. func TestSecureDelete(t *testing.T) {
  30. trie := newEmptySecure()
  31. vals := []struct{ k, v string }{
  32. {"do", "verb"},
  33. {"ether", "wookiedoo"},
  34. {"horse", "stallion"},
  35. {"shaman", "horse"},
  36. {"doge", "coin"},
  37. {"ether", ""},
  38. {"dog", "puppy"},
  39. {"shaman", ""},
  40. }
  41. for _, val := range vals {
  42. if val.v != "" {
  43. trie.Update([]byte(val.k), []byte(val.v))
  44. } else {
  45. trie.Delete([]byte(val.k))
  46. }
  47. }
  48. hash := trie.Hash()
  49. exp := common.HexToHash("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d")
  50. if hash != exp {
  51. t.Errorf("expected %x got %x", exp, hash)
  52. }
  53. }
  54. func TestSecureGetKey(t *testing.T) {
  55. trie := newEmptySecure()
  56. trie.Update([]byte("foo"), []byte("bar"))
  57. key := []byte("foo")
  58. value := []byte("bar")
  59. seckey := crypto.Keccak256(key)
  60. if !bytes.Equal(trie.Get(key), value) {
  61. t.Errorf("Get did not return bar")
  62. }
  63. if k := trie.GetKey(seckey); !bytes.Equal(k, key) {
  64. t.Errorf("GetKey returned %q, want %q", k, key)
  65. }
  66. }