filesystem_test.go 6.1 KB

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