filesystem_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 api
  17. import (
  18. "bytes"
  19. "context"
  20. "io/ioutil"
  21. "os"
  22. "path/filepath"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/swarm/chunk"
  26. "github.com/ethereum/go-ethereum/swarm/storage"
  27. )
  28. var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test")
  29. func testFileSystem(t *testing.T, f func(*FileSystem, bool)) {
  30. testAPI(t, func(api *API, _ *chunk.Tags, toEncrypt bool) {
  31. f(NewFileSystem(api), toEncrypt)
  32. })
  33. }
  34. func readPath(t *testing.T, parts ...string) string {
  35. file := filepath.Join(parts...)
  36. content, err := ioutil.ReadFile(file)
  37. if err != nil {
  38. t.Fatalf("unexpected error reading '%v': %v", file, err)
  39. }
  40. return string(content)
  41. }
  42. func TestApiDirUpload0(t *testing.T) {
  43. testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
  44. api := fs.api
  45. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
  46. if err != nil {
  47. t.Fatalf("unexpected error: %v", err)
  48. }
  49. content := readPath(t, "testdata", "test0", "index.html")
  50. resp := testGet(t, api, bzzhash, "index.html")
  51. exp := expResponse(content, "text/html; charset=utf-8", 0)
  52. checkResponse(t, resp, exp)
  53. content = readPath(t, "testdata", "test0", "index.css")
  54. resp = testGet(t, api, bzzhash, "index.css")
  55. exp = expResponse(content, "text/css; charset=utf-8", 0)
  56. checkResponse(t, resp, exp)
  57. addr := storage.Address(common.Hex2Bytes(bzzhash))
  58. _, _, _, _, err = api.Get(context.TODO(), NOOPDecrypt, addr, "")
  59. if err == nil {
  60. t.Fatalf("expected error: %v", err)
  61. }
  62. downloadDir := filepath.Join(testDownloadDir, "test0")
  63. defer os.RemoveAll(downloadDir)
  64. err = fs.Download(bzzhash, downloadDir)
  65. if err != nil {
  66. t.Fatalf("unexpected error: %v", err)
  67. }
  68. newbzzhash, err := fs.Upload(downloadDir, "", toEncrypt)
  69. if err != nil {
  70. t.Fatalf("unexpected error: %v", err)
  71. }
  72. // TODO: currently the hash is not deterministic in the encrypted case
  73. if !toEncrypt && bzzhash != newbzzhash {
  74. t.Fatalf("download %v reuploaded has incorrect hash, expected %v, got %v", downloadDir, bzzhash, newbzzhash)
  75. }
  76. })
  77. }
  78. func TestApiDirUploadModify(t *testing.T) {
  79. testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
  80. api := fs.api
  81. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
  82. if err != nil {
  83. t.Errorf("unexpected error: %v", err)
  84. return
  85. }
  86. addr := storage.Address(common.Hex2Bytes(bzzhash))
  87. addr, err = api.Modify(context.TODO(), addr, "index.html", "", "")
  88. if err != nil {
  89. t.Errorf("unexpected error: %v", err)
  90. return
  91. }
  92. index, err := ioutil.ReadFile(filepath.Join("testdata", "test0", "index.html"))
  93. if err != nil {
  94. t.Errorf("unexpected error: %v", err)
  95. return
  96. }
  97. ctx := context.TODO()
  98. hash, wait, err := api.Store(ctx, bytes.NewReader(index), int64(len(index)), toEncrypt)
  99. if err != nil {
  100. t.Errorf("unexpected error: %v", err)
  101. return
  102. }
  103. err = wait(ctx)
  104. if err != nil {
  105. t.Errorf("unexpected error: %v", err)
  106. return
  107. }
  108. addr, err = api.Modify(context.TODO(), addr, "index2.html", hash.Hex(), "text/html; charset=utf-8")
  109. if err != nil {
  110. t.Errorf("unexpected error: %v", err)
  111. return
  112. }
  113. addr, err = api.Modify(context.TODO(), addr, "img/logo.png", hash.Hex(), "text/html; charset=utf-8")
  114. if err != nil {
  115. t.Errorf("unexpected error: %v", err)
  116. return
  117. }
  118. bzzhash = addr.Hex()
  119. content := readPath(t, "testdata", "test0", "index.html")
  120. resp := testGet(t, api, bzzhash, "index2.html")
  121. exp := expResponse(content, "text/html; charset=utf-8", 0)
  122. checkResponse(t, resp, exp)
  123. resp = testGet(t, api, bzzhash, "img/logo.png")
  124. exp = expResponse(content, "text/html; charset=utf-8", 0)
  125. checkResponse(t, resp, exp)
  126. content = readPath(t, "testdata", "test0", "index.css")
  127. resp = testGet(t, api, bzzhash, "index.css")
  128. exp = expResponse(content, "text/css; charset=utf-8", 0)
  129. checkResponse(t, resp, exp)
  130. _, _, _, _, err = api.Get(context.TODO(), nil, addr, "")
  131. if err == nil {
  132. t.Errorf("expected error: %v", err)
  133. }
  134. })
  135. }
  136. func TestApiDirUploadWithRootFile(t *testing.T) {
  137. testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
  138. api := fs.api
  139. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html", toEncrypt)
  140. if err != nil {
  141. t.Errorf("unexpected error: %v", err)
  142. return
  143. }
  144. content := readPath(t, "testdata", "test0", "index.html")
  145. resp := testGet(t, api, bzzhash, "")
  146. exp := expResponse(content, "text/html; charset=utf-8", 0)
  147. checkResponse(t, resp, exp)
  148. })
  149. }
  150. func TestApiFileUpload(t *testing.T) {
  151. testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
  152. api := fs.api
  153. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "", toEncrypt)
  154. if err != nil {
  155. t.Errorf("unexpected error: %v", err)
  156. return
  157. }
  158. content := readPath(t, "testdata", "test0", "index.html")
  159. resp := testGet(t, api, bzzhash, "index.html")
  160. exp := expResponse(content, "text/html; charset=utf-8", 0)
  161. checkResponse(t, resp, exp)
  162. })
  163. }
  164. func TestApiFileUploadWithRootFile(t *testing.T) {
  165. testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
  166. api := fs.api
  167. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html", toEncrypt)
  168. if err != nil {
  169. t.Errorf("unexpected error: %v", err)
  170. return
  171. }
  172. content := readPath(t, "testdata", "test0", "index.html")
  173. resp := testGet(t, api, bzzhash, "")
  174. exp := expResponse(content, "text/html; charset=utf-8", 0)
  175. checkResponse(t, resp, exp)
  176. })
  177. }