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