github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/swarm/storage/localstore_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package storage
    18  
    19  import (
    20  	"context"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	ch "github.com/ethereum/go-ethereum/swarm/chunk"
    26  )
    27  
    28  var (
    29  	hashfunc = MakeHashFunc(DefaultHash)
    30  )
    31  
    32  // tests that the content address validator correctly checks the data
    33  // tests that feed update chunks are passed through content address validator
    34  // the test checking the resouce update validator internal correctness is found in storage/feeds/handler_test.go
    35  func TestValidator(t *testing.T) {
    36  	// set up localstore
    37  	datadir, err := ioutil.TempDir("", "storage-testvalidator")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	defer os.RemoveAll(datadir)
    42  
    43  	params := NewDefaultLocalStoreParams()
    44  	params.Init(datadir)
    45  	store, err := NewLocalStore(params, nil)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	// check puts with no validators, both succeed
    51  	chunks := GenerateRandomChunks(259, 2)
    52  	goodChunk := chunks[0]
    53  	badChunk := chunks[1]
    54  	copy(badChunk.Data(), goodChunk.Data())
    55  
    56  	errs := putChunks(store, goodChunk, badChunk)
    57  	if errs[0] != nil {
    58  		t.Fatalf("expected no error on good content address chunk in spite of no validation, but got: %s", err)
    59  	}
    60  	if errs[1] != nil {
    61  		t.Fatalf("expected no error on bad content address chunk in spite of no validation, but got: %s", err)
    62  	}
    63  
    64  	// add content address validator and check puts
    65  	// bad should fail, good should pass
    66  	store.Validators = append(store.Validators, NewContentAddressValidator(hashfunc))
    67  	chunks = GenerateRandomChunks(ch.DefaultSize, 2)
    68  	goodChunk = chunks[0]
    69  	badChunk = chunks[1]
    70  	copy(badChunk.Data(), goodChunk.Data())
    71  
    72  	errs = putChunks(store, goodChunk, badChunk)
    73  	if errs[0] != nil {
    74  		t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
    75  	}
    76  	if errs[1] == nil {
    77  		t.Fatal("expected error on bad content address chunk with content address validator only, but got nil")
    78  	}
    79  
    80  	// append a validator that always denies
    81  	// bad should fail, good should pass,
    82  	var negV boolTestValidator
    83  	store.Validators = append(store.Validators, negV)
    84  
    85  	chunks = GenerateRandomChunks(ch.DefaultSize, 2)
    86  	goodChunk = chunks[0]
    87  	badChunk = chunks[1]
    88  	copy(badChunk.Data(), goodChunk.Data())
    89  
    90  	errs = putChunks(store, goodChunk, badChunk)
    91  	if errs[0] != nil {
    92  		t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
    93  	}
    94  	if errs[1] == nil {
    95  		t.Fatal("expected error on bad content address chunk with content address validator only, but got nil")
    96  	}
    97  
    98  	// append a validator that always approves
    99  	// all shall pass
   100  	var posV boolTestValidator = true
   101  	store.Validators = append(store.Validators, posV)
   102  
   103  	chunks = GenerateRandomChunks(ch.DefaultSize, 2)
   104  	goodChunk = chunks[0]
   105  	badChunk = chunks[1]
   106  	copy(badChunk.Data(), goodChunk.Data())
   107  
   108  	errs = putChunks(store, goodChunk, badChunk)
   109  	if errs[0] != nil {
   110  		t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
   111  	}
   112  	if errs[1] != nil {
   113  		t.Fatalf("expected no error on bad content address chunk in spite of no validation, but got: %s", err)
   114  	}
   115  
   116  }
   117  
   118  type boolTestValidator bool
   119  
   120  func (self boolTestValidator) Validate(addr Address, data []byte) bool {
   121  	return bool(self)
   122  }
   123  
   124  // putChunks adds chunks  to localstore
   125  // It waits for receive on the stored channel
   126  // It logs but does not fail on delivery error
   127  func putChunks(store *LocalStore, chunks ...Chunk) []error {
   128  	i := 0
   129  	f := func(n int64) Chunk {
   130  		chunk := chunks[i]
   131  		i++
   132  		return chunk
   133  	}
   134  	_, errs := put(store, len(chunks), f)
   135  	return errs
   136  }
   137  
   138  func put(store *LocalStore, n int, f func(i int64) Chunk) (hs []Address, errs []error) {
   139  	for i := int64(0); i < int64(n); i++ {
   140  		chunk := f(ch.DefaultSize)
   141  		err := store.Put(context.TODO(), chunk)
   142  		errs = append(errs, err)
   143  		hs = append(hs, chunk.Address())
   144  	}
   145  	return hs, errs
   146  }