github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/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/common"
    25  	"github.com/ethereum/go-ethereum/swarm/storage/encryption"
    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)).Data()
    49  		ctx, cancel := context.WithTimeout(context.Background(), getTimeout)
    50  		defer cancel()
    51  		key1, err := hasherStore.Put(ctx, chunkData1)
    52  		if err != nil {
    53  			t.Fatalf("Expected no error got \"%v\"", err)
    54  		}
    55  
    56  		chunkData2 := GenerateRandomChunk(int64(tt.chunkLength)).Data()
    57  		key2, err := hasherStore.Put(ctx, chunkData2)
    58  		if err != nil {
    59  			t.Fatalf("Expected no error got \"%v\"", err)
    60  		}
    61  
    62  		hasherStore.Close()
    63  
    64  		// Wait until chunks are really stored
    65  		err = hasherStore.Wait(ctx)
    66  		if err != nil {
    67  			t.Fatalf("Expected no error got \"%v\"", err)
    68  		}
    69  
    70  		// Get the first chunk
    71  		retrievedChunkData1, err := hasherStore.Get(ctx, key1)
    72  		if err != nil {
    73  			t.Fatalf("Expected no error, got \"%v\"", err)
    74  		}
    75  
    76  		// Retrieved data should be same as the original
    77  		if !bytes.Equal(chunkData1, retrievedChunkData1) {
    78  			t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(retrievedChunkData1))
    79  		}
    80  
    81  		// Get the second chunk
    82  		retrievedChunkData2, err := hasherStore.Get(ctx, key2)
    83  		if err != nil {
    84  			t.Fatalf("Expected no error, got \"%v\"", err)
    85  		}
    86  
    87  		// Retrieved data should be same as the original
    88  		if !bytes.Equal(chunkData2, retrievedChunkData2) {
    89  			t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData2), common.Bytes2Hex(retrievedChunkData2))
    90  		}
    91  
    92  		hash1, encryptionKey1, err := parseReference(key1, hasherStore.hashSize)
    93  		if err != nil {
    94  			t.Fatalf("Expected no error, got \"%v\"", err)
    95  		}
    96  
    97  		if tt.toEncrypt {
    98  			if encryptionKey1 == nil {
    99  				t.Fatal("Expected non-nil encryption key, got nil")
   100  			} else if len(encryptionKey1) != encryption.KeyLength {
   101  				t.Fatalf("Expected encryption key length %v, got %v", encryption.KeyLength, len(encryptionKey1))
   102  			}
   103  		}
   104  		if !tt.toEncrypt && encryptionKey1 != nil {
   105  			t.Fatalf("Expected nil encryption key, got key with length %v", len(encryptionKey1))
   106  		}
   107  
   108  		// Check if chunk data in store is encrypted or not
   109  		chunkInStore, err := chunkStore.Get(ctx, hash1)
   110  		if err != nil {
   111  			t.Fatalf("Expected no error got \"%v\"", err)
   112  		}
   113  
   114  		chunkDataInStore := chunkInStore.Data()
   115  
   116  		if tt.toEncrypt && bytes.Equal(chunkData1, chunkDataInStore) {
   117  			t.Fatalf("Chunk expected to be encrypted but it is stored without encryption")
   118  		}
   119  		if !tt.toEncrypt && !bytes.Equal(chunkData1, chunkDataInStore) {
   120  			t.Fatalf("Chunk expected to be not encrypted but stored content is different. Expected %v got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(chunkDataInStore))
   121  		}
   122  	}
   123  }