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