github.com/gobitfly/go-ethereum@v1.8.12/swarm/storage/filestore_test.go (about) 1 // Copyright 2016 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 "bytes" 21 "io" 22 "io/ioutil" 23 "os" 24 "testing" 25 ) 26 27 const testDataSize = 0x1000000 28 29 func TestFileStorerandom(t *testing.T) { 30 testFileStoreRandom(false, t) 31 testFileStoreRandom(true, t) 32 } 33 34 func testFileStoreRandom(toEncrypt bool, t *testing.T) { 35 tdb, cleanup, err := newTestDbStore(false, false) 36 defer cleanup() 37 if err != nil { 38 t.Fatalf("init dbStore failed: %v", err) 39 } 40 db := tdb.LDBStore 41 db.setCapacity(50000) 42 memStore := NewMemStore(NewDefaultStoreParams(), db) 43 localStore := &LocalStore{ 44 memStore: memStore, 45 DbStore: db, 46 } 47 48 fileStore := NewFileStore(localStore, NewFileStoreParams()) 49 defer os.RemoveAll("/tmp/bzz") 50 51 reader, slice := generateRandomData(testDataSize) 52 key, wait, err := fileStore.Store(reader, testDataSize, toEncrypt) 53 if err != nil { 54 t.Errorf("Store error: %v", err) 55 } 56 wait() 57 resultReader, isEncrypted := fileStore.Retrieve(key) 58 if isEncrypted != toEncrypt { 59 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 60 } 61 resultSlice := make([]byte, len(slice)) 62 n, err := resultReader.ReadAt(resultSlice, 0) 63 if err != io.EOF { 64 t.Errorf("Retrieve error: %v", err) 65 } 66 if n != len(slice) { 67 t.Errorf("Slice size error got %d, expected %d.", n, len(slice)) 68 } 69 if !bytes.Equal(slice, resultSlice) { 70 t.Errorf("Comparison error.") 71 } 72 ioutil.WriteFile("/tmp/slice.bzz.16M", slice, 0666) 73 ioutil.WriteFile("/tmp/result.bzz.16M", resultSlice, 0666) 74 localStore.memStore = NewMemStore(NewDefaultStoreParams(), db) 75 resultReader, isEncrypted = fileStore.Retrieve(key) 76 if isEncrypted != toEncrypt { 77 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 78 } 79 for i := range resultSlice { 80 resultSlice[i] = 0 81 } 82 n, err = resultReader.ReadAt(resultSlice, 0) 83 if err != io.EOF { 84 t.Errorf("Retrieve error after removing memStore: %v", err) 85 } 86 if n != len(slice) { 87 t.Errorf("Slice size error after removing memStore got %d, expected %d.", n, len(slice)) 88 } 89 if !bytes.Equal(slice, resultSlice) { 90 t.Errorf("Comparison error after removing memStore.") 91 } 92 } 93 94 func TestFileStoreCapacity(t *testing.T) { 95 testFileStoreCapacity(false, t) 96 testFileStoreCapacity(true, t) 97 } 98 99 func testFileStoreCapacity(toEncrypt bool, t *testing.T) { 100 tdb, cleanup, err := newTestDbStore(false, false) 101 defer cleanup() 102 if err != nil { 103 t.Fatalf("init dbStore failed: %v", err) 104 } 105 db := tdb.LDBStore 106 memStore := NewMemStore(NewDefaultStoreParams(), db) 107 localStore := &LocalStore{ 108 memStore: memStore, 109 DbStore: db, 110 } 111 fileStore := NewFileStore(localStore, NewFileStoreParams()) 112 reader, slice := generateRandomData(testDataSize) 113 key, wait, err := fileStore.Store(reader, testDataSize, toEncrypt) 114 if err != nil { 115 t.Errorf("Store error: %v", err) 116 } 117 wait() 118 resultReader, isEncrypted := fileStore.Retrieve(key) 119 if isEncrypted != toEncrypt { 120 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 121 } 122 resultSlice := make([]byte, len(slice)) 123 n, err := resultReader.ReadAt(resultSlice, 0) 124 if err != io.EOF { 125 t.Errorf("Retrieve error: %v", err) 126 } 127 if n != len(slice) { 128 t.Errorf("Slice size error got %d, expected %d.", n, len(slice)) 129 } 130 if !bytes.Equal(slice, resultSlice) { 131 t.Errorf("Comparison error.") 132 } 133 // Clear memStore 134 memStore.setCapacity(0) 135 // check whether it is, indeed, empty 136 fileStore.ChunkStore = memStore 137 resultReader, isEncrypted = fileStore.Retrieve(key) 138 if isEncrypted != toEncrypt { 139 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 140 } 141 if _, err = resultReader.ReadAt(resultSlice, 0); err == nil { 142 t.Errorf("Was able to read %d bytes from an empty memStore.", len(slice)) 143 } 144 // check how it works with localStore 145 fileStore.ChunkStore = localStore 146 // localStore.dbStore.setCapacity(0) 147 resultReader, isEncrypted = fileStore.Retrieve(key) 148 if isEncrypted != toEncrypt { 149 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 150 } 151 for i := range resultSlice { 152 resultSlice[i] = 0 153 } 154 n, err = resultReader.ReadAt(resultSlice, 0) 155 if err != io.EOF { 156 t.Errorf("Retrieve error after clearing memStore: %v", err) 157 } 158 if n != len(slice) { 159 t.Errorf("Slice size error after clearing memStore got %d, expected %d.", n, len(slice)) 160 } 161 if !bytes.Equal(slice, resultSlice) { 162 t.Errorf("Comparison error after clearing memStore.") 163 } 164 }