filestore_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. )
  25. const testDataSize = 0x1000000
  26. func TestFileStorerandom(t *testing.T) {
  27. testFileStoreRandom(false, t)
  28. testFileStoreRandom(true, t)
  29. }
  30. func testFileStoreRandom(toEncrypt bool, t *testing.T) {
  31. tdb, cleanup, err := newTestDbStore(false, false)
  32. defer cleanup()
  33. if err != nil {
  34. t.Fatalf("init dbStore failed: %v", err)
  35. }
  36. db := tdb.LDBStore
  37. db.setCapacity(50000)
  38. memStore := NewMemStore(NewDefaultStoreParams(), db)
  39. localStore := &LocalStore{
  40. memStore: memStore,
  41. DbStore: db,
  42. }
  43. fileStore := NewFileStore(localStore, NewFileStoreParams())
  44. defer os.RemoveAll("/tmp/bzz")
  45. reader, slice := generateRandomData(testDataSize)
  46. ctx := context.TODO()
  47. key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt)
  48. if err != nil {
  49. t.Errorf("Store error: %v", err)
  50. }
  51. err = wait(ctx)
  52. if err != nil {
  53. t.Fatalf("Store waitt error: %v", err.Error())
  54. }
  55. resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
  56. if isEncrypted != toEncrypt {
  57. t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
  58. }
  59. resultSlice := make([]byte, len(slice))
  60. n, err := resultReader.ReadAt(resultSlice, 0)
  61. if err != io.EOF {
  62. t.Errorf("Retrieve error: %v", err)
  63. }
  64. if n != len(slice) {
  65. t.Errorf("Slice size error got %d, expected %d.", n, len(slice))
  66. }
  67. if !bytes.Equal(slice, resultSlice) {
  68. t.Errorf("Comparison error.")
  69. }
  70. ioutil.WriteFile("/tmp/slice.bzz.16M", slice, 0666)
  71. ioutil.WriteFile("/tmp/result.bzz.16M", resultSlice, 0666)
  72. localStore.memStore = NewMemStore(NewDefaultStoreParams(), db)
  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.Errorf("Retrieve error after removing memStore: %v", err)
  83. }
  84. if n != len(slice) {
  85. t.Errorf("Slice size error after removing memStore got %d, expected %d.", n, len(slice))
  86. }
  87. if !bytes.Equal(slice, resultSlice) {
  88. t.Errorf("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. tdb, cleanup, err := newTestDbStore(false, false)
  97. defer cleanup()
  98. if err != nil {
  99. t.Fatalf("init dbStore failed: %v", err)
  100. }
  101. db := tdb.LDBStore
  102. memStore := NewMemStore(NewDefaultStoreParams(), db)
  103. localStore := &LocalStore{
  104. memStore: memStore,
  105. DbStore: db,
  106. }
  107. fileStore := NewFileStore(localStore, NewFileStoreParams())
  108. reader, slice := generateRandomData(testDataSize)
  109. ctx := context.TODO()
  110. key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt)
  111. if err != nil {
  112. t.Errorf("Store error: %v", err)
  113. }
  114. err = wait(ctx)
  115. if err != nil {
  116. t.Errorf("Store error: %v", err)
  117. }
  118. resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), 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(context.TODO(), 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(context.TODO(), 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. }