export_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "crypto/rand"
  21. "io"
  22. "io/ioutil"
  23. "net/http"
  24. "os"
  25. "strings"
  26. "testing"
  27. "github.com/ethereum/go-ethereum/swarm"
  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. cluster := newTestCluster(t, 1)
  38. // generate random 10mb file
  39. f, cleanup := generateRandomFile(t, 10000000)
  40. defer cleanup()
  41. // upload the file with 'swarm up' and expect a hash
  42. up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", f.Name())
  43. _, matches := up.ExpectRegexp(`[a-f\d]{64}`)
  44. up.ExpectExit()
  45. hash := matches[0]
  46. var info swarm.Info
  47. if err := cluster.Nodes[0].Client.Call(&info, "bzz_info"); err != nil {
  48. t.Fatal(err)
  49. }
  50. cluster.Stop()
  51. defer cluster.Cleanup()
  52. // generate an export.tar
  53. exportCmd := runSwarm(t, "db", "export", info.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info.BzzKey, "0x"))
  54. exportCmd.ExpectExit()
  55. // start second cluster
  56. cluster2 := newTestCluster(t, 1)
  57. var info2 swarm.Info
  58. if err := cluster2.Nodes[0].Client.Call(&info2, "bzz_info"); err != nil {
  59. t.Fatal(err)
  60. }
  61. // stop second cluster, so that we close LevelDB
  62. cluster2.Stop()
  63. defer cluster2.Cleanup()
  64. // import the export.tar
  65. importCmd := runSwarm(t, "db", "import", info2.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info2.BzzKey, "0x"))
  66. importCmd.ExpectExit()
  67. // spin second cluster back up
  68. cluster2.StartExistingNodes(t, 1, strings.TrimPrefix(info2.BzzAccount, "0x"))
  69. // try to fetch imported file
  70. res, err := http.Get(cluster2.Nodes[0].URL + "/bzz:/" + hash)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if res.StatusCode != 200 {
  75. t.Fatalf("expected HTTP status %d, got %s", 200, res.Status)
  76. }
  77. // compare downloaded file with the generated random file
  78. mustEqualFiles(t, f, res.Body)
  79. }
  80. func mustEqualFiles(t *testing.T, up io.Reader, down io.Reader) {
  81. h := md5.New()
  82. upLen, err := io.Copy(h, up)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. upHash := h.Sum(nil)
  87. h.Reset()
  88. downLen, err := io.Copy(h, down)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. downHash := h.Sum(nil)
  93. if !bytes.Equal(upHash, downHash) || upLen != downLen {
  94. 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)
  95. }
  96. }
  97. func generateRandomFile(t *testing.T, size int) (f *os.File, teardown func()) {
  98. // create a tmp file
  99. tmp, err := ioutil.TempFile("", "swarm-test")
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. // callback for tmp file cleanup
  104. teardown = func() {
  105. tmp.Close()
  106. os.Remove(tmp.Name())
  107. }
  108. // write 10mb random data to file
  109. buf := make([]byte, 10000000)
  110. _, err = rand.Read(buf)
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. ioutil.WriteFile(tmp.Name(), buf, 0755)
  115. return tmp, teardown
  116. }