client_test.go 9.8 KB

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