filestore_test.go 6.2 KB

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