filesystem_test.go 5.4 KB

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