client_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2017 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 client
  17. import (
  18. "bytes"
  19. "io/ioutil"
  20. "os"
  21. "path/filepath"
  22. "reflect"
  23. "sort"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/swarm/api"
  26. "github.com/ethereum/go-ethereum/swarm/testutil"
  27. )
  28. // TestClientUploadDownloadRaw test uploading and downloading raw data to swarm
  29. func TestClientUploadDownloadRaw(t *testing.T) {
  30. srv := testutil.NewTestSwarmServer(t)
  31. defer srv.Close()
  32. client := NewClient(srv.URL)
  33. // upload some raw data
  34. data := []byte("foo123")
  35. hash, err := client.UploadRaw(bytes.NewReader(data), int64(len(data)))
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. // check we can download the same data
  40. res, err := client.DownloadRaw(hash)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. defer res.Close()
  45. gotData, err := ioutil.ReadAll(res)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. if !bytes.Equal(gotData, data) {
  50. t.Fatalf("expected downloaded data to be %q, got %q", data, gotData)
  51. }
  52. }
  53. // TestClientUploadDownloadFiles test uploading and downloading files to swarm
  54. // manifests
  55. func TestClientUploadDownloadFiles(t *testing.T) {
  56. srv := testutil.NewTestSwarmServer(t)
  57. defer srv.Close()
  58. client := NewClient(srv.URL)
  59. upload := func(manifest, path string, data []byte) string {
  60. file := &File{
  61. ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
  62. ManifestEntry: api.ManifestEntry{
  63. Path: path,
  64. ContentType: "text/plain",
  65. Size: int64(len(data)),
  66. },
  67. }
  68. hash, err := client.Upload(file, manifest)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. return hash
  73. }
  74. checkDownload := func(manifest, path string, expected []byte) {
  75. file, err := client.Download(manifest, path)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. defer file.Close()
  80. if file.Size != int64(len(expected)) {
  81. t.Fatalf("expected downloaded file to be %d bytes, got %d", len(expected), file.Size)
  82. }
  83. if file.ContentType != file.ContentType {
  84. t.Fatalf("expected downloaded file to have type %q, got %q", file.ContentType, file.ContentType)
  85. }
  86. data, err := ioutil.ReadAll(file)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. if !bytes.Equal(data, expected) {
  91. t.Fatalf("expected downloaded data to be %q, got %q", expected, data)
  92. }
  93. }
  94. // upload a file to the root of a manifest
  95. rootData := []byte("some-data")
  96. rootHash := upload("", "", rootData)
  97. // check we can download the root file
  98. checkDownload(rootHash, "", rootData)
  99. // upload another file to the same manifest
  100. otherData := []byte("some-other-data")
  101. newHash := upload(rootHash, "some/other/path", otherData)
  102. // check we can download both files from the new manifest
  103. checkDownload(newHash, "", rootData)
  104. checkDownload(newHash, "some/other/path", otherData)
  105. // replace the root file with different data
  106. newHash = upload(newHash, "", otherData)
  107. // check both files have the other data
  108. checkDownload(newHash, "", otherData)
  109. checkDownload(newHash, "some/other/path", otherData)
  110. }
  111. var testDirFiles = []string{
  112. "file1.txt",
  113. "file2.txt",
  114. "dir1/file3.txt",
  115. "dir1/file4.txt",
  116. "dir2/file5.txt",
  117. "dir2/dir3/file6.txt",
  118. "dir2/dir4/file7.txt",
  119. "dir2/dir4/file8.txt",
  120. }
  121. func newTestDirectory(t *testing.T) string {
  122. dir, err := ioutil.TempDir("", "swarm-client-test")
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. for _, file := range testDirFiles {
  127. path := filepath.Join(dir, file)
  128. if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
  129. os.RemoveAll(dir)
  130. t.Fatalf("error creating dir for %s: %s", path, err)
  131. }
  132. if err := ioutil.WriteFile(path, []byte(file), 0644); err != nil {
  133. os.RemoveAll(dir)
  134. t.Fatalf("error writing file %s: %s", path, err)
  135. }
  136. }
  137. return dir
  138. }
  139. // TestClientUploadDownloadDirectory tests uploading and downloading a
  140. // directory of files to a swarm manifest
  141. func TestClientUploadDownloadDirectory(t *testing.T) {
  142. srv := testutil.NewTestSwarmServer(t)
  143. defer srv.Close()
  144. dir := newTestDirectory(t)
  145. defer os.RemoveAll(dir)
  146. // upload the directory
  147. client := NewClient(srv.URL)
  148. defaultPath := filepath.Join(dir, testDirFiles[0])
  149. hash, err := client.UploadDirectory(dir, defaultPath, "")
  150. if err != nil {
  151. t.Fatalf("error uploading directory: %s", err)
  152. }
  153. // check we can download the individual files
  154. checkDownloadFile := func(path string, expected []byte) {
  155. file, err := client.Download(hash, path)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. defer file.Close()
  160. data, err := ioutil.ReadAll(file)
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. if !bytes.Equal(data, expected) {
  165. t.Fatalf("expected data to be %q, got %q", expected, data)
  166. }
  167. }
  168. for _, file := range testDirFiles {
  169. checkDownloadFile(file, []byte(file))
  170. }
  171. // check we can download the default path
  172. checkDownloadFile("", []byte(testDirFiles[0]))
  173. // check we can download the directory
  174. tmp, err := ioutil.TempDir("", "swarm-client-test")
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. defer os.RemoveAll(tmp)
  179. if err := client.DownloadDirectory(hash, "", tmp); err != nil {
  180. t.Fatal(err)
  181. }
  182. for _, file := range testDirFiles {
  183. data, err := ioutil.ReadFile(filepath.Join(tmp, file))
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. if !bytes.Equal(data, []byte(file)) {
  188. t.Fatalf("expected data to be %q, got %q", file, data)
  189. }
  190. }
  191. }
  192. // TestClientFileList tests listing files in a swarm manifest
  193. func TestClientFileList(t *testing.T) {
  194. srv := testutil.NewTestSwarmServer(t)
  195. defer srv.Close()
  196. dir := newTestDirectory(t)
  197. defer os.RemoveAll(dir)
  198. client := NewClient(srv.URL)
  199. hash, err := client.UploadDirectory(dir, "", "")
  200. if err != nil {
  201. t.Fatalf("error uploading directory: %s", err)
  202. }
  203. ls := func(prefix string) []string {
  204. list, err := client.List(hash, prefix)
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. paths := make([]string, 0, len(list.CommonPrefixes)+len(list.Entries))
  209. for _, prefix := range list.CommonPrefixes {
  210. paths = append(paths, prefix)
  211. }
  212. for _, entry := range list.Entries {
  213. paths = append(paths, entry.Path)
  214. }
  215. sort.Strings(paths)
  216. return paths
  217. }
  218. tests := map[string][]string{
  219. "": []string{"dir1/", "dir2/", "file1.txt", "file2.txt"},
  220. "file": []string{"file1.txt", "file2.txt"},
  221. "file1": []string{"file1.txt"},
  222. "file2.txt": []string{"file2.txt"},
  223. "file12": []string{},
  224. "dir": []string{"dir1/", "dir2/"},
  225. "dir1": []string{"dir1/"},
  226. "dir1/": []string{"dir1/file3.txt", "dir1/file4.txt"},
  227. "dir1/file": []string{"dir1/file3.txt", "dir1/file4.txt"},
  228. "dir1/file3.txt": []string{"dir1/file3.txt"},
  229. "dir1/file34": []string{},
  230. "dir2/": []string{"dir2/dir3/", "dir2/dir4/", "dir2/file5.txt"},
  231. "dir2/file": []string{"dir2/file5.txt"},
  232. "dir2/dir": []string{"dir2/dir3/", "dir2/dir4/"},
  233. "dir2/dir3/": []string{"dir2/dir3/file6.txt"},
  234. "dir2/dir4/": []string{"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"},
  235. "dir2/dir4/file": []string{"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"},
  236. "dir2/dir4/file7.txt": []string{"dir2/dir4/file7.txt"},
  237. "dir2/dir4/file78": []string{},
  238. }
  239. for prefix, expected := range tests {
  240. actual := ls(prefix)
  241. if !reflect.DeepEqual(actual, expected) {
  242. t.Fatalf("expected prefix %q to return %v, got %v", prefix, expected, actual)
  243. }
  244. }
  245. }
  246. // TestClientMultipartUpload tests uploading files to swarm using a multipart
  247. // upload
  248. func TestClientMultipartUpload(t *testing.T) {
  249. srv := testutil.NewTestSwarmServer(t)
  250. defer srv.Close()
  251. // define an uploader which uploads testDirFiles with some data
  252. data := []byte("some-data")
  253. uploader := UploaderFunc(func(upload UploadFn) error {
  254. for _, name := range testDirFiles {
  255. file := &File{
  256. ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
  257. ManifestEntry: api.ManifestEntry{
  258. Path: name,
  259. ContentType: "text/plain",
  260. Size: int64(len(data)),
  261. },
  262. }
  263. if err := upload(file); err != nil {
  264. return err
  265. }
  266. }
  267. return nil
  268. })
  269. // upload the files as a multipart upload
  270. client := NewClient(srv.URL)
  271. hash, err := client.MultipartUpload("", uploader)
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. // check we can download the individual files
  276. checkDownloadFile := func(path string) {
  277. file, err := client.Download(hash, path)
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. defer file.Close()
  282. gotData, err := ioutil.ReadAll(file)
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. if !bytes.Equal(gotData, data) {
  287. t.Fatalf("expected data to be %q, got %q", data, gotData)
  288. }
  289. }
  290. for _, file := range testDirFiles {
  291. checkDownloadFile(file)
  292. }
  293. }