export_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bytes"
  19. "crypto/md5"
  20. "io"
  21. "net/http"
  22. "os"
  23. "runtime"
  24. "strings"
  25. "testing"
  26. "github.com/ethereum/go-ethereum/swarm"
  27. "github.com/ethereum/go-ethereum/swarm/testutil"
  28. )
  29. // TestCLISwarmExportImport perform the following test:
  30. // 1. runs swarm node
  31. // 2. uploads a random file
  32. // 3. runs an export of the local datastore
  33. // 4. runs a second swarm node
  34. // 5. imports the exported datastore
  35. // 6. fetches the uploaded random file from the second node
  36. func TestCLISwarmExportImport(t *testing.T) {
  37. if runtime.GOOS == "windows" {
  38. t.Skip()
  39. }
  40. cluster := newTestCluster(t, 1)
  41. // generate random 10mb file
  42. content := testutil.RandomBytes(1, 10000000)
  43. fileName := testutil.TempFileWithContent(t, string(content))
  44. defer os.Remove(fileName)
  45. // upload the file with 'swarm up' and expect a hash
  46. up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", fileName)
  47. _, matches := up.ExpectRegexp(`[a-f\d]{64}`)
  48. up.ExpectExit()
  49. hash := matches[0]
  50. var info swarm.Info
  51. if err := cluster.Nodes[0].Client.Call(&info, "bzz_info"); err != nil {
  52. t.Fatal(err)
  53. }
  54. cluster.Stop()
  55. defer cluster.Cleanup()
  56. // generate an export.tar
  57. exportCmd := runSwarm(t, "db", "export", info.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info.BzzKey, "0x"))
  58. exportCmd.ExpectExit()
  59. // start second cluster
  60. cluster2 := newTestCluster(t, 1)
  61. var info2 swarm.Info
  62. if err := cluster2.Nodes[0].Client.Call(&info2, "bzz_info"); err != nil {
  63. t.Fatal(err)
  64. }
  65. // stop second cluster, so that we close LevelDB
  66. cluster2.Stop()
  67. defer cluster2.Cleanup()
  68. // import the export.tar
  69. importCmd := runSwarm(t, "db", "import", info2.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info2.BzzKey, "0x"))
  70. importCmd.ExpectExit()
  71. // spin second cluster back up
  72. cluster2.StartExistingNodes(t, 1, strings.TrimPrefix(info2.BzzAccount, "0x"))
  73. // try to fetch imported file
  74. res, err := http.Get(cluster2.Nodes[0].URL + "/bzz:/" + hash)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if res.StatusCode != 200 {
  79. t.Fatalf("expected HTTP status %d, got %s", 200, res.Status)
  80. }
  81. // compare downloaded file with the generated random file
  82. mustEqualFiles(t, bytes.NewReader(content), res.Body)
  83. }
  84. func mustEqualFiles(t *testing.T, up io.Reader, down io.Reader) {
  85. h := md5.New()
  86. upLen, err := io.Copy(h, up)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. upHash := h.Sum(nil)
  91. h.Reset()
  92. downLen, err := io.Copy(h, down)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. downHash := h.Sum(nil)
  97. if !bytes.Equal(upHash, downHash) || upLen != downLen {
  98. t.Fatalf("downloaded imported file md5=%x (length %v) is not the same as the generated one mp5=%x (length %v)", downHash, downLen, upHash, upLen)
  99. }
  100. }