github.com/divan/go-ethereum@v1.8.14-0.20180820134928-1de9ada4016d/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  	"io/ioutil"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/swarm/chunk"
    25  )
    26  
    27  var (
    28  	hashfunc = MakeHashFunc(DefaultHash)
    29  )
    30  
    31  // tests that the content address validator correctly checks the data
    32  // tests that resource update chunks are passed through content address validator
    33  // the test checking the resouce update validator internal correctness is found in resource_test.go
    34  func TestValidator(t *testing.T) {
    35  	// set up localstore
    36  	datadir, err := ioutil.TempDir("", "storage-testvalidator")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	defer os.RemoveAll(datadir)
    41  
    42  	params := NewDefaultLocalStoreParams()
    43  	params.Init(datadir)
    44  	store, err := NewLocalStore(params, nil)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	// check puts with no validators, both succeed
    50  	chunks := GenerateRandomChunks(259, 2)
    51  	goodChunk := chunks[0]
    52  	badChunk := chunks[1]
    53  	copy(badChunk.SData, goodChunk.SData)
    54  
    55  	PutChunks(store, goodChunk, badChunk)
    56  	if err := goodChunk.GetErrored(); err != nil {
    57  		t.Fatalf("expected no error on good content address chunk in spite of no validation, but got: %s", err)
    58  	}
    59  	if err := badChunk.GetErrored(); err != nil {
    60  		t.Fatalf("expected no error on bad content address chunk in spite of no validation, but got: %s", err)
    61  	}
    62  
    63  	// add content address validator and check puts
    64  	// bad should fail, good should pass
    65  	store.Validators = append(store.Validators, NewContentAddressValidator(hashfunc))
    66  	chunks = GenerateRandomChunks(chunk.DefaultSize, 2)
    67  	goodChunk = chunks[0]
    68  	badChunk = chunks[1]
    69  	copy(badChunk.SData, goodChunk.SData)
    70  
    71  	PutChunks(store, goodChunk, badChunk)
    72  	if err := goodChunk.GetErrored(); err != nil {
    73  		t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
    74  	}
    75  	if err := badChunk.GetErrored(); err == nil {
    76  		t.Fatal("expected error on bad content address chunk with content address validator only, but got nil")
    77  	}
    78  
    79  	// append a validator that always denies
    80  	// bad should fail, good should pass,
    81  	var negV boolTestValidator
    82  	store.Validators = append(store.Validators, negV)
    83  
    84  	chunks = GenerateRandomChunks(chunk.DefaultSize, 2)
    85  	goodChunk = chunks[0]
    86  	badChunk = chunks[1]
    87  	copy(badChunk.SData, goodChunk.SData)
    88  
    89  	PutChunks(store, goodChunk, badChunk)
    90  	if err := goodChunk.GetErrored(); err != nil {
    91  		t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
    92  	}
    93  	if err := badChunk.GetErrored(); err == nil {
    94  		t.Fatal("expected error on bad content address chunk with content address validator only, but got nil")
    95  	}
    96  
    97  	// append a validator that always approves
    98  	// all shall pass
    99  	var posV boolTestValidator = true
   100  	store.Validators = append(store.Validators, posV)
   101  
   102  	chunks = GenerateRandomChunks(chunk.DefaultSize, 2)
   103  	goodChunk = chunks[0]
   104  	badChunk = chunks[1]
   105  	copy(badChunk.SData, goodChunk.SData)
   106  
   107  	PutChunks(store, goodChunk, badChunk)
   108  	if err := goodChunk.GetErrored(); err != nil {
   109  		t.Fatalf("expected no error on good content address chunk with content address validator only, but got: %s", err)
   110  	}
   111  	if err := badChunk.GetErrored(); err != nil {
   112  		t.Fatalf("expected no error on bad content address chunk with content address validator only, but got: %s", err)
   113  	}
   114  }
   115  
   116  type boolTestValidator bool
   117  
   118  func (self boolTestValidator) Validate(addr Address, data []byte) bool {
   119  	return bool(self)
   120  }