github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/filestore_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package storage 26 27 import ( 28 "bytes" 29 "context" 30 "io" 31 "io/ioutil" 32 "os" 33 "testing" 34 ) 35 36 const testDataSize = 0x1000000 37 38 func TestFileStorerandom(t *testing.T) { 39 testFileStoreRandom(false, t) 40 testFileStoreRandom(true, t) 41 } 42 43 func testFileStoreRandom(toEncrypt bool, t *testing.T) { 44 tdb, cleanup, err := newTestDbStore(false, false) 45 defer cleanup() 46 if err != nil { 47 t.Fatalf("init dbStore failed: %v", err) 48 } 49 db := tdb.LDBStore 50 db.setCapacity(50000) 51 memStore := NewMemStore(NewDefaultStoreParams(), db) 52 localStore := &LocalStore{ 53 memStore: memStore, 54 DbStore: db, 55 } 56 57 fileStore := NewFileStore(localStore, NewFileStoreParams()) 58 defer os.RemoveAll("/tmp/bzz") 59 60 reader, slice := generateRandomData(testDataSize) 61 ctx := context.TODO() 62 key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt) 63 if err != nil { 64 t.Errorf("Store error: %v", err) 65 } 66 err = wait(ctx) 67 if err != nil { 68 t.Fatalf("Store waitt error: %v", err.Error()) 69 } 70 resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key) 71 if isEncrypted != toEncrypt { 72 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 73 } 74 resultSlice := make([]byte, len(slice)) 75 n, err := resultReader.ReadAt(resultSlice, 0) 76 if err != io.EOF { 77 t.Errorf("Retrieve error: %v", err) 78 } 79 if n != len(slice) { 80 t.Errorf("Slice size error got %d, expected %d.", n, len(slice)) 81 } 82 if !bytes.Equal(slice, resultSlice) { 83 t.Errorf("Comparison error.") 84 } 85 ioutil.WriteFile("/tmp/slice.bzz.16M", slice, 0666) 86 ioutil.WriteFile("/tmp/result.bzz.16M", resultSlice, 0666) 87 localStore.memStore = NewMemStore(NewDefaultStoreParams(), db) 88 resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key) 89 if isEncrypted != toEncrypt { 90 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 91 } 92 for i := range resultSlice { 93 resultSlice[i] = 0 94 } 95 n, err = resultReader.ReadAt(resultSlice, 0) 96 if err != io.EOF { 97 t.Errorf("Retrieve error after removing memStore: %v", err) 98 } 99 if n != len(slice) { 100 t.Errorf("Slice size error after removing memStore got %d, expected %d.", n, len(slice)) 101 } 102 if !bytes.Equal(slice, resultSlice) { 103 t.Errorf("Comparison error after removing memStore.") 104 } 105 } 106 107 func TestFileStoreCapacity(t *testing.T) { 108 testFileStoreCapacity(false, t) 109 testFileStoreCapacity(true, t) 110 } 111 112 func testFileStoreCapacity(toEncrypt bool, t *testing.T) { 113 tdb, cleanup, err := newTestDbStore(false, false) 114 defer cleanup() 115 if err != nil { 116 t.Fatalf("init dbStore failed: %v", err) 117 } 118 db := tdb.LDBStore 119 memStore := NewMemStore(NewDefaultStoreParams(), db) 120 localStore := &LocalStore{ 121 memStore: memStore, 122 DbStore: db, 123 } 124 fileStore := NewFileStore(localStore, NewFileStoreParams()) 125 reader, slice := generateRandomData(testDataSize) 126 ctx := context.TODO() 127 key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt) 128 if err != nil { 129 t.Errorf("Store error: %v", err) 130 } 131 err = wait(ctx) 132 if err != nil { 133 t.Errorf("Store error: %v", err) 134 } 135 resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key) 136 if isEncrypted != toEncrypt { 137 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 138 } 139 resultSlice := make([]byte, len(slice)) 140 n, err := resultReader.ReadAt(resultSlice, 0) 141 if err != io.EOF { 142 t.Errorf("Retrieve error: %v", err) 143 } 144 if n != len(slice) { 145 t.Errorf("Slice size error got %d, expected %d.", n, len(slice)) 146 } 147 if !bytes.Equal(slice, resultSlice) { 148 t.Errorf("Comparison error.") 149 } 150 // 151 memStore.setCapacity(0) 152 // 153 fileStore.ChunkStore = memStore 154 resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key) 155 if isEncrypted != toEncrypt { 156 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 157 } 158 if _, err = resultReader.ReadAt(resultSlice, 0); err == nil { 159 t.Errorf("Was able to read %d bytes from an empty memStore.", len(slice)) 160 } 161 // 162 fileStore.ChunkStore = localStore 163 // 164 resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key) 165 if isEncrypted != toEncrypt { 166 t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted) 167 } 168 for i := range resultSlice { 169 resultSlice[i] = 0 170 } 171 n, err = resultReader.ReadAt(resultSlice, 0) 172 if err != io.EOF { 173 t.Errorf("Retrieve error after clearing memStore: %v", err) 174 } 175 if n != len(slice) { 176 t.Errorf("Slice size error after clearing memStore got %d, expected %d.", n, len(slice)) 177 } 178 if !bytes.Equal(slice, resultSlice) { 179 t.Errorf("Comparison error after clearing memStore.") 180 } 181 }