github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/localstore_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  	"io/ioutil"
    29  	"os"
    30  	"testing"
    31  
    32  	"github.com/ethereum/go-ethereum/swarm/chunk"
    33  )
    34  
    35  var (
    36  	hashfunc = MakeHashFunc(DefaultHash)
    37  )
    38  
    39  //
    40  //
    41  //
    42  func TestValidator(t *testing.T) {
    43  //
    44  	datadir, err := ioutil.TempDir("", "storage-testvalidator")
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	defer os.RemoveAll(datadir)
    49  
    50  	params := NewDefaultLocalStoreParams()
    51  	params.Init(datadir)
    52  	store, err := NewLocalStore(params, nil)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  //
    58  	chunks := GenerateRandomChunks(259, 2)
    59  	goodChunk := chunks[0]
    60  	badChunk := chunks[1]
    61  	copy(badChunk.SData, goodChunk.SData)
    62  
    63  	PutChunks(store, goodChunk, badChunk)
    64  	if err := goodChunk.GetErrored(); err != nil {
    65  		t.Fatalf("expected no error on good content address chunk in spite of no validation, but got: %s", err)
    66  	}
    67  	if err := badChunk.GetErrored(); err != nil {
    68  		t.Fatalf("expected no error on bad content address chunk in spite of no validation, but got: %s", err)
    69  	}
    70  
    71  //
    72  //
    73  	store.Validators = append(store.Validators, NewContentAddressValidator(hashfunc))
    74  	chunks = GenerateRandomChunks(chunk.DefaultSize, 2)
    75  	goodChunk = chunks[0]
    76  	badChunk = chunks[1]
    77  	copy(badChunk.SData, goodChunk.SData)
    78  
    79  	PutChunks(store, goodChunk, badChunk)
    80  	if err := goodChunk.GetErrored(); err != nil {
    81  		t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
    82  	}
    83  	if err := badChunk.GetErrored(); err == nil {
    84  		t.Fatal("expected error on bad content address chunk with content address validator only, but got nil")
    85  	}
    86  
    87  //
    88  //
    89  	var negV boolTestValidator
    90  	store.Validators = append(store.Validators, negV)
    91  
    92  	chunks = GenerateRandomChunks(chunk.DefaultSize, 2)
    93  	goodChunk = chunks[0]
    94  	badChunk = chunks[1]
    95  	copy(badChunk.SData, goodChunk.SData)
    96  
    97  	PutChunks(store, goodChunk, badChunk)
    98  	if err := goodChunk.GetErrored(); err != nil {
    99  		t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
   100  	}
   101  	if err := badChunk.GetErrored(); err == nil {
   102  		t.Fatal("expected error on bad content address chunk with content address validator only, but got nil")
   103  	}
   104  
   105  //
   106  //
   107  	var posV boolTestValidator = true
   108  	store.Validators = append(store.Validators, posV)
   109  
   110  	chunks = GenerateRandomChunks(chunk.DefaultSize, 2)
   111  	goodChunk = chunks[0]
   112  	badChunk = chunks[1]
   113  	copy(badChunk.SData, goodChunk.SData)
   114  
   115  	PutChunks(store, goodChunk, badChunk)
   116  	if err := goodChunk.GetErrored(); err != nil {
   117  		t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
   118  	}
   119  	if err := badChunk.GetErrored(); err != nil {
   120  		t.Fatalf("expected no error on bad content address chunk with content address validator only, but got: %s", err)
   121  	}
   122  }
   123  
   124  type boolTestValidator bool
   125  
   126  func (self boolTestValidator) Validate(addr Address, data []byte) bool {
   127  	return bool(self)
   128  }