localstore_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 storage
  17. import (
  18. "io/ioutil"
  19. "os"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/swarm/chunk"
  22. )
  23. var (
  24. hashfunc = MakeHashFunc(DefaultHash)
  25. )
  26. // tests that the content address validator correctly checks the data
  27. // tests that resource update chunks are passed through content address validator
  28. // the test checking the resouce update validator internal correctness is found in resource_test.go
  29. func TestValidator(t *testing.T) {
  30. // set up localstore
  31. datadir, err := ioutil.TempDir("", "storage-testvalidator")
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. defer os.RemoveAll(datadir)
  36. params := NewDefaultLocalStoreParams()
  37. params.Init(datadir)
  38. store, err := NewLocalStore(params, nil)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. // check puts with no validators, both succeed
  43. chunks := GenerateRandomChunks(259, 2)
  44. goodChunk := chunks[0]
  45. badChunk := chunks[1]
  46. copy(badChunk.SData, goodChunk.SData)
  47. PutChunks(store, goodChunk, badChunk)
  48. if err := goodChunk.GetErrored(); err != nil {
  49. t.Fatalf("expected no error on good content address chunk in spite of no validation, but got: %s", err)
  50. }
  51. if err := badChunk.GetErrored(); err != nil {
  52. t.Fatalf("expected no error on bad content address chunk in spite of no validation, but got: %s", err)
  53. }
  54. // add content address validator and check puts
  55. // bad should fail, good should pass
  56. store.Validators = append(store.Validators, NewContentAddressValidator(hashfunc))
  57. chunks = GenerateRandomChunks(chunk.DefaultSize, 2)
  58. goodChunk = chunks[0]
  59. badChunk = chunks[1]
  60. copy(badChunk.SData, goodChunk.SData)
  61. PutChunks(store, goodChunk, badChunk)
  62. if err := goodChunk.GetErrored(); err != nil {
  63. t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
  64. }
  65. if err := badChunk.GetErrored(); err == nil {
  66. t.Fatal("expected error on bad content address chunk with content address validator only, but got nil")
  67. }
  68. // append a validator that always denies
  69. // bad should fail, good should pass,
  70. var negV boolTestValidator
  71. store.Validators = append(store.Validators, negV)
  72. chunks = GenerateRandomChunks(chunk.DefaultSize, 2)
  73. goodChunk = chunks[0]
  74. badChunk = chunks[1]
  75. copy(badChunk.SData, goodChunk.SData)
  76. PutChunks(store, goodChunk, badChunk)
  77. if err := goodChunk.GetErrored(); err != nil {
  78. t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
  79. }
  80. if err := badChunk.GetErrored(); err == nil {
  81. t.Fatal("expected error on bad content address chunk with content address validator only, but got nil")
  82. }
  83. // append a validator that always approves
  84. // all shall pass
  85. var posV boolTestValidator = true
  86. store.Validators = append(store.Validators, posV)
  87. chunks = GenerateRandomChunks(chunk.DefaultSize, 2)
  88. goodChunk = chunks[0]
  89. badChunk = chunks[1]
  90. copy(badChunk.SData, goodChunk.SData)
  91. PutChunks(store, goodChunk, badChunk)
  92. if err := goodChunk.GetErrored(); err != nil {
  93. t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
  94. }
  95. if err := badChunk.GetErrored(); err != nil {
  96. t.Fatalf("expected no error on bad content address chunk with content address validator only, but got: %s", err)
  97. }
  98. }
  99. type boolTestValidator bool
  100. func (self boolTestValidator) Validate(addr Address, data []byte) bool {
  101. return bool(self)
  102. }