hasherstore_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "bytes"
  19. "context"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/swarm/storage/encryption"
  22. "github.com/ethereum/go-ethereum/common"
  23. )
  24. func TestHasherStore(t *testing.T) {
  25. var tests = []struct {
  26. chunkLength int
  27. toEncrypt bool
  28. }{
  29. {10, false},
  30. {100, false},
  31. {1000, false},
  32. {4096, false},
  33. {10, true},
  34. {100, true},
  35. {1000, true},
  36. {4096, true},
  37. }
  38. for _, tt := range tests {
  39. chunkStore := NewMapChunkStore()
  40. hasherStore := NewHasherStore(chunkStore, MakeHashFunc(DefaultHash), tt.toEncrypt)
  41. // Put two random chunks into the hasherStore
  42. chunkData1 := GenerateRandomChunk(int64(tt.chunkLength)).SData
  43. key1, err := hasherStore.Put(chunkData1)
  44. if err != nil {
  45. t.Fatalf("Expected no error got \"%v\"", err)
  46. }
  47. chunkData2 := GenerateRandomChunk(int64(tt.chunkLength)).SData
  48. key2, err := hasherStore.Put(chunkData2)
  49. if err != nil {
  50. t.Fatalf("Expected no error got \"%v\"", err)
  51. }
  52. hasherStore.Close()
  53. // Wait until chunks are really stored
  54. err = hasherStore.Wait(context.TODO())
  55. if err != nil {
  56. t.Fatalf("Expected no error got \"%v\"", err)
  57. }
  58. // Get the first chunk
  59. retrievedChunkData1, err := hasherStore.Get(key1)
  60. if err != nil {
  61. t.Fatalf("Expected no error, got \"%v\"", err)
  62. }
  63. // Retrieved data should be same as the original
  64. if !bytes.Equal(chunkData1, retrievedChunkData1) {
  65. t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(retrievedChunkData1))
  66. }
  67. // Get the second chunk
  68. retrievedChunkData2, err := hasherStore.Get(key2)
  69. if err != nil {
  70. t.Fatalf("Expected no error, got \"%v\"", err)
  71. }
  72. // Retrieved data should be same as the original
  73. if !bytes.Equal(chunkData2, retrievedChunkData2) {
  74. t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData2), common.Bytes2Hex(retrievedChunkData2))
  75. }
  76. hash1, encryptionKey1, err := parseReference(key1, hasherStore.hashSize)
  77. if err != nil {
  78. t.Fatalf("Expected no error, got \"%v\"", err)
  79. }
  80. if tt.toEncrypt {
  81. if encryptionKey1 == nil {
  82. t.Fatal("Expected non-nil encryption key, got nil")
  83. } else if len(encryptionKey1) != encryption.KeyLength {
  84. t.Fatalf("Expected encryption key length %v, got %v", encryption.KeyLength, len(encryptionKey1))
  85. }
  86. }
  87. if !tt.toEncrypt && encryptionKey1 != nil {
  88. t.Fatalf("Expected nil encryption key, got key with length %v", len(encryptionKey1))
  89. }
  90. // Check if chunk data in store is encrypted or not
  91. chunkInStore, err := chunkStore.Get(hash1)
  92. if err != nil {
  93. t.Fatalf("Expected no error got \"%v\"", err)
  94. }
  95. chunkDataInStore := chunkInStore.SData
  96. if tt.toEncrypt && bytes.Equal(chunkData1, chunkDataInStore) {
  97. t.Fatalf("Chunk expected to be encrypted but it is stored without encryption")
  98. }
  99. if !tt.toEncrypt && !bytes.Equal(chunkData1, chunkDataInStore) {
  100. t.Fatalf("Chunk expected to be not encrypted but stored content is different. Expected %v got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(chunkDataInStore))
  101. }
  102. }
  103. }