filesystem_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "sync"
  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)) {
  29. testApi(t, func(api *Api) {
  30. f(NewFileSystem(api))
  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) {
  43. api := fs.api
  44. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "")
  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", 0)
  55. checkResponse(t, resp, exp)
  56. key := storage.Key(common.Hex2Bytes(bzzhash))
  57. _, _, _, err = api.Get(key, "")
  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, "")
  68. if err != nil {
  69. t.Fatalf("unexpected error: %v", err)
  70. }
  71. if 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) {
  78. api := fs.api
  79. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "")
  80. if err != nil {
  81. t.Errorf("unexpected error: %v", err)
  82. return
  83. }
  84. key := storage.Key(common.Hex2Bytes(bzzhash))
  85. key, err = api.Modify(key, "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. wg := &sync.WaitGroup{}
  96. hash, err := api.Store(bytes.NewReader(index), int64(len(index)), wg)
  97. wg.Wait()
  98. if err != nil {
  99. t.Errorf("unexpected error: %v", err)
  100. return
  101. }
  102. key, err = api.Modify(key, "index2.html", hash.Hex(), "text/html; charset=utf-8")
  103. if err != nil {
  104. t.Errorf("unexpected error: %v", err)
  105. return
  106. }
  107. key, err = api.Modify(key, "img/logo.png", hash.Hex(), "text/html; charset=utf-8")
  108. if err != nil {
  109. t.Errorf("unexpected error: %v", err)
  110. return
  111. }
  112. bzzhash = key.String()
  113. content := readPath(t, "testdata", "test0", "index.html")
  114. resp := testGet(t, api, bzzhash, "index2.html")
  115. exp := expResponse(content, "text/html; charset=utf-8", 0)
  116. checkResponse(t, resp, exp)
  117. resp = testGet(t, api, bzzhash, "img/logo.png")
  118. exp = expResponse(content, "text/html; charset=utf-8", 0)
  119. checkResponse(t, resp, exp)
  120. content = readPath(t, "testdata", "test0", "index.css")
  121. resp = testGet(t, api, bzzhash, "index.css")
  122. exp = expResponse(content, "text/css", 0)
  123. checkResponse(t, resp, exp)
  124. _, _, _, err = api.Get(key, "")
  125. if err == nil {
  126. t.Errorf("expected error: %v", err)
  127. }
  128. })
  129. }
  130. func TestApiDirUploadWithRootFile(t *testing.T) {
  131. testFileSystem(t, func(fs *FileSystem) {
  132. api := fs.api
  133. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html")
  134. if err != nil {
  135. t.Errorf("unexpected error: %v", err)
  136. return
  137. }
  138. content := readPath(t, "testdata", "test0", "index.html")
  139. resp := testGet(t, api, bzzhash, "")
  140. exp := expResponse(content, "text/html; charset=utf-8", 0)
  141. checkResponse(t, resp, exp)
  142. })
  143. }
  144. func TestApiFileUpload(t *testing.T) {
  145. testFileSystem(t, func(fs *FileSystem) {
  146. api := fs.api
  147. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "")
  148. if err != nil {
  149. t.Errorf("unexpected error: %v", err)
  150. return
  151. }
  152. content := readPath(t, "testdata", "test0", "index.html")
  153. resp := testGet(t, api, bzzhash, "index.html")
  154. exp := expResponse(content, "text/html; charset=utf-8", 0)
  155. checkResponse(t, resp, exp)
  156. })
  157. }
  158. func TestApiFileUploadWithRootFile(t *testing.T) {
  159. testFileSystem(t, func(fs *FileSystem) {
  160. api := fs.api
  161. bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html")
  162. if err != nil {
  163. t.Errorf("unexpected error: %v", err)
  164. return
  165. }
  166. content := readPath(t, "testdata", "test0", "index.html")
  167. resp := testGet(t, api, bzzhash, "")
  168. exp := expResponse(content, "text/html; charset=utf-8", 0)
  169. checkResponse(t, resp, exp)
  170. })
  171. }