hasherstore_test.go 4.0 KB

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