client_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 != "text/plain" {
  84. t.Fatalf("expected downloaded file to have type %q, got %q", "text/plain", 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. paths = append(paths, list.CommonPrefixes...)
  210. for _, entry := range list.Entries {
  211. paths = append(paths, entry.Path)
  212. }
  213. sort.Strings(paths)
  214. return paths
  215. }
  216. tests := map[string][]string{
  217. "": {"dir1/", "dir2/", "file1.txt", "file2.txt"},
  218. "file": {"file1.txt", "file2.txt"},
  219. "file1": {"file1.txt"},
  220. "file2.txt": {"file2.txt"},
  221. "file12": {},
  222. "dir": {"dir1/", "dir2/"},
  223. "dir1": {"dir1/"},
  224. "dir1/": {"dir1/file3.txt", "dir1/file4.txt"},
  225. "dir1/file": {"dir1/file3.txt", "dir1/file4.txt"},
  226. "dir1/file3.txt": {"dir1/file3.txt"},
  227. "dir1/file34": {},
  228. "dir2/": {"dir2/dir3/", "dir2/dir4/", "dir2/file5.txt"},
  229. "dir2/file": {"dir2/file5.txt"},
  230. "dir2/dir": {"dir2/dir3/", "dir2/dir4/"},
  231. "dir2/dir3/": {"dir2/dir3/file6.txt"},
  232. "dir2/dir4/": {"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"},
  233. "dir2/dir4/file": {"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"},
  234. "dir2/dir4/file7.txt": {"dir2/dir4/file7.txt"},
  235. "dir2/dir4/file78": {},
  236. }
  237. for prefix, expected := range tests {
  238. actual := ls(prefix)
  239. if !reflect.DeepEqual(actual, expected) {
  240. t.Fatalf("expected prefix %q to return %v, got %v", prefix, expected, actual)
  241. }
  242. }
  243. }
  244. // TestClientMultipartUpload tests uploading files to swarm using a multipart
  245. // upload
  246. func TestClientMultipartUpload(t *testing.T) {
  247. srv := testutil.NewTestSwarmServer(t)
  248. defer srv.Close()
  249. // define an uploader which uploads testDirFiles with some data
  250. data := []byte("some-data")
  251. uploader := UploaderFunc(func(upload UploadFn) error {
  252. for _, name := range testDirFiles {
  253. file := &File{
  254. ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
  255. ManifestEntry: api.ManifestEntry{
  256. Path: name,
  257. ContentType: "text/plain",
  258. Size: int64(len(data)),
  259. },
  260. }
  261. if err := upload(file); err != nil {
  262. return err
  263. }
  264. }
  265. return nil
  266. })
  267. // upload the files as a multipart upload
  268. client := NewClient(srv.URL)
  269. hash, err := client.MultipartUpload("", uploader)
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. // check we can download the individual files
  274. checkDownloadFile := func(path string) {
  275. file, err := client.Download(hash, path)
  276. if err != nil {
  277. t.Fatal(err)
  278. }
  279. defer file.Close()
  280. gotData, err := ioutil.ReadAll(file)
  281. if err != nil {
  282. t.Fatal(err)
  283. }
  284. if !bytes.Equal(gotData, data) {
  285. t.Fatalf("expected data to be %q, got %q", data, gotData)
  286. }
  287. }
  288. for _, file := range testDirFiles {
  289. checkDownloadFile(file)
  290. }
  291. }