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