github.com/susy-go/susy-graviton@v0.0.0-20190614130430-36cddae42305/swarm/storage/hasherstore_test.go (about)

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