filestore_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. package storage
  17. import (
  18. "bytes"
  19. "context"
  20. "io"
  21. "io/ioutil"
  22. "os"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/swarm/testutil"
  25. )
  26. const testDataSize = 0x0001000
  27. func TestFileStorerandom(t *testing.T) {
  28. testFileStoreRandom(false, t)
  29. testFileStoreRandom(true, t)
  30. }
  31. func testFileStoreRandom(toEncrypt bool, t *testing.T) {
  32. tdb, cleanup, err := newTestDbStore(false, false)
  33. defer cleanup()
  34. if err != nil {
  35. t.Fatalf("init dbStore failed: %v", err)
  36. }
  37. db := tdb.LDBStore
  38. db.setCapacity(50000)
  39. memStore := NewMemStore(NewDefaultStoreParams(), db)
  40. localStore := &LocalStore{
  41. memStore: memStore,
  42. DbStore: db,
  43. }
  44. fileStore := NewFileStore(localStore, NewFileStoreParams())
  45. defer os.RemoveAll("/tmp/bzz")
  46. slice := testutil.RandomBytes(1, testDataSize)
  47. ctx := context.TODO()
  48. key, wait, err := fileStore.Store(ctx, bytes.NewReader(slice), testDataSize, toEncrypt)
  49. if err != nil {
  50. t.Fatalf("Store error: %v", err)
  51. }
  52. err = wait(ctx)
  53. if err != nil {
  54. t.Fatalf("Store waitt error: %v", err.Error())
  55. }
  56. resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
  57. if isEncrypted != toEncrypt {
  58. t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
  59. }
  60. resultSlice := make([]byte, testDataSize)
  61. n, err := resultReader.ReadAt(resultSlice, 0)
  62. if err != io.EOF {
  63. t.Fatalf("Retrieve error: %v", err)
  64. }
  65. if n != testDataSize {
  66. t.Fatalf("Slice size error got %d, expected %d.", n, testDataSize)
  67. }
  68. if !bytes.Equal(slice, resultSlice) {
  69. t.Fatalf("Comparison error.")
  70. }
  71. ioutil.WriteFile("/tmp/slice.bzz.16M", slice, 0666)
  72. ioutil.WriteFile("/tmp/result.bzz.16M", resultSlice, 0666)
  73. localStore.memStore = NewMemStore(NewDefaultStoreParams(), db)
  74. resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
  75. if isEncrypted != toEncrypt {
  76. t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
  77. }
  78. for i := range resultSlice {
  79. resultSlice[i] = 0
  80. }
  81. n, err = resultReader.ReadAt(resultSlice, 0)
  82. if err != io.EOF {
  83. t.Fatalf("Retrieve error after removing memStore: %v", err)
  84. }
  85. if n != len(slice) {
  86. t.Fatalf("Slice size error after removing memStore got %d, expected %d.", n, len(slice))
  87. }
  88. if !bytes.Equal(slice, resultSlice) {
  89. t.Fatalf("Comparison error after removing memStore.")
  90. }
  91. }
  92. func TestFileStoreCapacity(t *testing.T) {
  93. testFileStoreCapacity(false, t)
  94. testFileStoreCapacity(true, t)
  95. }
  96. func testFileStoreCapacity(toEncrypt bool, t *testing.T) {
  97. tdb, cleanup, err := newTestDbStore(false, false)
  98. defer cleanup()
  99. if err != nil {
  100. t.Fatalf("init dbStore failed: %v", err)
  101. }
  102. db := tdb.LDBStore
  103. memStore := NewMemStore(NewDefaultStoreParams(), db)
  104. localStore := &LocalStore{
  105. memStore: memStore,
  106. DbStore: db,
  107. }
  108. fileStore := NewFileStore(localStore, NewFileStoreParams())
  109. slice := testutil.RandomBytes(1, testDataSize)
  110. ctx := context.TODO()
  111. key, wait, err := fileStore.Store(ctx, bytes.NewReader(slice), testDataSize, toEncrypt)
  112. if err != nil {
  113. t.Errorf("Store error: %v", err)
  114. }
  115. err = wait(ctx)
  116. if err != nil {
  117. t.Fatalf("Store error: %v", err)
  118. }
  119. resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
  120. if isEncrypted != toEncrypt {
  121. t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
  122. }
  123. resultSlice := make([]byte, len(slice))
  124. n, err := resultReader.ReadAt(resultSlice, 0)
  125. if err != io.EOF {
  126. t.Fatalf("Retrieve error: %v", err)
  127. }
  128. if n != len(slice) {
  129. t.Fatalf("Slice size error got %d, expected %d.", n, len(slice))
  130. }
  131. if !bytes.Equal(slice, resultSlice) {
  132. t.Fatalf("Comparison error.")
  133. }
  134. // Clear memStore
  135. memStore.setCapacity(0)
  136. // check whether it is, indeed, empty
  137. fileStore.ChunkStore = memStore
  138. resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
  139. if isEncrypted != toEncrypt {
  140. t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
  141. }
  142. if _, err = resultReader.ReadAt(resultSlice, 0); err == nil {
  143. t.Fatalf("Was able to read %d bytes from an empty memStore.", len(slice))
  144. }
  145. // check how it works with localStore
  146. fileStore.ChunkStore = localStore
  147. // localStore.dbStore.setCapacity(0)
  148. resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
  149. if isEncrypted != toEncrypt {
  150. t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
  151. }
  152. for i := range resultSlice {
  153. resultSlice[i] = 0
  154. }
  155. n, err = resultReader.ReadAt(resultSlice, 0)
  156. if err != io.EOF {
  157. t.Fatalf("Retrieve error after clearing memStore: %v", err)
  158. }
  159. if n != len(slice) {
  160. t.Fatalf("Slice size error after clearing memStore got %d, expected %d.", n, len(slice))
  161. }
  162. if !bytes.Equal(slice, resultSlice) {
  163. t.Fatalf("Comparison error after clearing memStore.")
  164. }
  165. }
  166. // TestGetAllReferences only tests that GetAllReferences returns an expected
  167. // number of references for a given file
  168. func TestGetAllReferences(t *testing.T) {
  169. tdb, cleanup, err := newTestDbStore(false, false)
  170. defer cleanup()
  171. if err != nil {
  172. t.Fatalf("init dbStore failed: %v", err)
  173. }
  174. db := tdb.LDBStore
  175. memStore := NewMemStore(NewDefaultStoreParams(), db)
  176. localStore := &LocalStore{
  177. memStore: memStore,
  178. DbStore: db,
  179. }
  180. fileStore := NewFileStore(localStore, NewFileStoreParams())
  181. // testRuns[i] and expectedLen[i] are dataSize and expected length respectively
  182. testRuns := []int{1024, 8192, 16000, 30000, 1000000}
  183. expectedLens := []int{1, 3, 5, 9, 248}
  184. for i, r := range testRuns {
  185. slice := testutil.RandomBytes(1, r)
  186. addrs, err := fileStore.GetAllReferences(context.Background(), bytes.NewReader(slice), false)
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. if len(addrs) != expectedLens[i] {
  191. t.Fatalf("Expected reference array length to be %d, but is %d", expectedLens[i], len(addrs))
  192. }
  193. }
  194. }