github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/hasherstore_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package storage
    26  
    27  import (
    28  	"bytes"
    29  	"context"
    30  	"testing"
    31  
    32  	"github.com/ethereum/go-ethereum/swarm/storage/encryption"
    33  
    34  	"github.com/ethereum/go-ethereum/common"
    35  )
    36  
    37  func TestHasherStore(t *testing.T) {
    38  	var tests = []struct {
    39  		chunkLength int
    40  		toEncrypt   bool
    41  	}{
    42  		{10, false},
    43  		{100, false},
    44  		{1000, false},
    45  		{4096, false},
    46  		{10, true},
    47  		{100, true},
    48  		{1000, true},
    49  		{4096, true},
    50  	}
    51  
    52  	for _, tt := range tests {
    53  		chunkStore := NewMapChunkStore()
    54  		hasherStore := NewHasherStore(chunkStore, MakeHashFunc(DefaultHash), tt.toEncrypt)
    55  
    56  //
    57  		chunkData1 := GenerateRandomChunk(int64(tt.chunkLength)).SData
    58  		key1, err := hasherStore.Put(context.TODO(), chunkData1)
    59  		if err != nil {
    60  			t.Fatalf("Expected no error got \"%v\"", err)
    61  		}
    62  
    63  		chunkData2 := GenerateRandomChunk(int64(tt.chunkLength)).SData
    64  		key2, err := hasherStore.Put(context.TODO(), chunkData2)
    65  		if err != nil {
    66  			t.Fatalf("Expected no error got \"%v\"", err)
    67  		}
    68  
    69  		hasherStore.Close()
    70  
    71  //
    72  		err = hasherStore.Wait(context.TODO())
    73  		if err != nil {
    74  			t.Fatalf("Expected no error got \"%v\"", err)
    75  		}
    76  
    77  //
    78  		retrievedChunkData1, err := hasherStore.Get(context.TODO(), key1)
    79  		if err != nil {
    80  			t.Fatalf("Expected no error, got \"%v\"", err)
    81  		}
    82  
    83  //
    84  		if !bytes.Equal(chunkData1, retrievedChunkData1) {
    85  			t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(retrievedChunkData1))
    86  		}
    87  
    88  //
    89  		retrievedChunkData2, err := hasherStore.Get(context.TODO(), key2)
    90  		if err != nil {
    91  			t.Fatalf("Expected no error, got \"%v\"", err)
    92  		}
    93  
    94  //
    95  		if !bytes.Equal(chunkData2, retrievedChunkData2) {
    96  			t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData2), common.Bytes2Hex(retrievedChunkData2))
    97  		}
    98  
    99  		hash1, encryptionKey1, err := parseReference(key1, hasherStore.hashSize)
   100  		if err != nil {
   101  			t.Fatalf("Expected no error, got \"%v\"", err)
   102  		}
   103  
   104  		if tt.toEncrypt {
   105  			if encryptionKey1 == nil {
   106  				t.Fatal("Expected non-nil encryption key, got nil")
   107  			} else if len(encryptionKey1) != encryption.KeyLength {
   108  				t.Fatalf("Expected encryption key length %v, got %v", encryption.KeyLength, len(encryptionKey1))
   109  			}
   110  		}
   111  		if !tt.toEncrypt && encryptionKey1 != nil {
   112  			t.Fatalf("Expected nil encryption key, got key with length %v", len(encryptionKey1))
   113  		}
   114  
   115  //
   116  		chunkInStore, err := chunkStore.Get(context.TODO(), hash1)
   117  		if err != nil {
   118  			t.Fatalf("Expected no error got \"%v\"", err)
   119  		}
   120  
   121  		chunkDataInStore := chunkInStore.SData
   122  
   123  		if tt.toEncrypt && bytes.Equal(chunkData1, chunkDataInStore) {
   124  			t.Fatalf("Chunk expected to be encrypted but it is stored without encryption")
   125  		}
   126  		if !tt.toEncrypt && !bytes.Equal(chunkData1, chunkDataInStore) {
   127  			t.Fatalf("Chunk expected to be not encrypted but stored content is different. Expected %v got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(chunkDataInStore))
   128  		}
   129  	}
   130  }