github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/storage/hasherstore_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:49</date>
    10  //</624342681241260032>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package storage
    29  
    30  import (
    31  	"bytes"
    32  	"context"
    33  	"testing"
    34  
    35  	"github.com/ethereum/go-ethereum/swarm/storage/encryption"
    36  
    37  	"github.com/ethereum/go-ethereum/common"
    38  )
    39  
    40  func TestHasherStore(t *testing.T) {
    41  	var tests = []struct {
    42  		chunkLength int
    43  		toEncrypt   bool
    44  	}{
    45  		{10, false},
    46  		{100, false},
    47  		{1000, false},
    48  		{4096, false},
    49  		{10, true},
    50  		{100, true},
    51  		{1000, true},
    52  		{4096, true},
    53  	}
    54  
    55  	for _, tt := range tests {
    56  		chunkStore := NewMapChunkStore()
    57  		hasherStore := NewHasherStore(chunkStore, MakeHashFunc(DefaultHash), tt.toEncrypt)
    58  
    59  //
    60  		chunkData1 := GenerateRandomChunk(int64(tt.chunkLength)).SData
    61  		key1, err := hasherStore.Put(context.TODO(), chunkData1)
    62  		if err != nil {
    63  			t.Fatalf("Expected no error got \"%v\"", err)
    64  		}
    65  
    66  		chunkData2 := GenerateRandomChunk(int64(tt.chunkLength)).SData
    67  		key2, err := hasherStore.Put(context.TODO(), chunkData2)
    68  		if err != nil {
    69  			t.Fatalf("Expected no error got \"%v\"", err)
    70  		}
    71  
    72  		hasherStore.Close()
    73  
    74  //
    75  		err = hasherStore.Wait(context.TODO())
    76  		if err != nil {
    77  			t.Fatalf("Expected no error got \"%v\"", err)
    78  		}
    79  
    80  //
    81  		retrievedChunkData1, err := hasherStore.Get(context.TODO(), key1)
    82  		if err != nil {
    83  			t.Fatalf("Expected no error, got \"%v\"", err)
    84  		}
    85  
    86  //
    87  		if !bytes.Equal(chunkData1, retrievedChunkData1) {
    88  			t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(retrievedChunkData1))
    89  		}
    90  
    91  //
    92  		retrievedChunkData2, err := hasherStore.Get(context.TODO(), key2)
    93  		if err != nil {
    94  			t.Fatalf("Expected no error, got \"%v\"", err)
    95  		}
    96  
    97  //
    98  		if !bytes.Equal(chunkData2, retrievedChunkData2) {
    99  			t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData2), common.Bytes2Hex(retrievedChunkData2))
   100  		}
   101  
   102  		hash1, encryptionKey1, err := parseReference(key1, hasherStore.hashSize)
   103  		if err != nil {
   104  			t.Fatalf("Expected no error, got \"%v\"", err)
   105  		}
   106  
   107  		if tt.toEncrypt {
   108  			if encryptionKey1 == nil {
   109  				t.Fatal("Expected non-nil encryption key, got nil")
   110  			} else if len(encryptionKey1) != encryption.KeyLength {
   111  				t.Fatalf("Expected encryption key length %v, got %v", encryption.KeyLength, len(encryptionKey1))
   112  			}
   113  		}
   114  		if !tt.toEncrypt && encryptionKey1 != nil {
   115  			t.Fatalf("Expected nil encryption key, got key with length %v", len(encryptionKey1))
   116  		}
   117  
   118  //
   119  		chunkInStore, err := chunkStore.Get(context.TODO(), hash1)
   120  		if err != nil {
   121  			t.Fatalf("Expected no error got \"%v\"", err)
   122  		}
   123  
   124  		chunkDataInStore := chunkInStore.SData
   125  
   126  		if tt.toEncrypt && bytes.Equal(chunkData1, chunkDataInStore) {
   127  			t.Fatalf("Chunk expected to be encrypted but it is stored without encryption")
   128  		}
   129  		if !tt.toEncrypt && !bytes.Equal(chunkData1, chunkDataInStore) {
   130  			t.Fatalf("Chunk expected to be not encrypted but stored content is different. Expected %v got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(chunkDataInStore))
   131  		}
   132  	}
   133  }
   134