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