filesystem_test.go 5.8 KB

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