github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/swarm/export_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2018 Go Ethereum作者 10 //此文件是Go以太坊的一部分。 11 // 12 //Go以太坊是免费软件:您可以重新发布和/或修改它 13 //根据GNU通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊的分布希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU通用公共许可证了解更多详细信息。 21 // 22 //你应该已经收到一份GNU通用公共许可证的副本 23 //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package main 26 27 import ( 28 "bytes" 29 "crypto/md5" 30 "crypto/rand" 31 "io" 32 "io/ioutil" 33 "net/http" 34 "os" 35 "strings" 36 "testing" 37 38 "github.com/ethereum/go-ethereum/swarm" 39 ) 40 41 //testcliswarmexportimport执行以下测试: 42 // 43 // 44 //三。运行本地数据存储的导出 45 // 46 //5。导入导出的数据存储 47 //6。从第二个节点获取上载的随机文件 48 func TestCLISwarmExportImport(t *testing.T) { 49 cluster := newTestCluster(t, 1) 50 51 // 52 f, cleanup := generateRandomFile(t, 10000000) 53 defer cleanup() 54 55 //用“swarm up”上传文件,并期望得到一个哈希值 56 up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", f.Name()) 57 _, matches := up.ExpectRegexp(`[a-f\d]{64}`) 58 up.ExpectExit() 59 hash := matches[0] 60 61 var info swarm.Info 62 if err := cluster.Nodes[0].Client.Call(&info, "bzz_info"); err != nil { 63 t.Fatal(err) 64 } 65 66 cluster.Stop() 67 defer cluster.Cleanup() 68 69 //生成export.tar 70 exportCmd := runSwarm(t, "db", "export", info.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info.BzzKey, "0x")) 71 exportCmd.ExpectExit() 72 73 // 74 cluster2 := newTestCluster(t, 1) 75 76 var info2 swarm.Info 77 if err := cluster2.Nodes[0].Client.Call(&info2, "bzz_info"); err != nil { 78 t.Fatal(err) 79 } 80 81 // 82 cluster2.Stop() 83 defer cluster2.Cleanup() 84 85 //导入export.tar 86 importCmd := runSwarm(t, "db", "import", info2.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info2.BzzKey, "0x")) 87 importCmd.ExpectExit() 88 89 //旋转第二个群集备份 90 cluster2.StartExistingNodes(t, 1, strings.TrimPrefix(info2.BzzAccount, "0x")) 91 92 //尝试获取导入的文件 93 res, err := http.Get(cluster2.Nodes[0].URL + "/bzz:/" + hash) 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 if res.StatusCode != 200 { 99 t.Fatalf("expected HTTP status %d, got %s", 200, res.Status) 100 } 101 102 // 103 mustEqualFiles(t, f, res.Body) 104 } 105 106 func mustEqualFiles(t *testing.T, up io.Reader, down io.Reader) { 107 h := md5.New() 108 upLen, err := io.Copy(h, up) 109 if err != nil { 110 t.Fatal(err) 111 } 112 upHash := h.Sum(nil) 113 h.Reset() 114 downLen, err := io.Copy(h, down) 115 if err != nil { 116 t.Fatal(err) 117 } 118 downHash := h.Sum(nil) 119 120 if !bytes.Equal(upHash, downHash) || upLen != downLen { 121 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) 122 } 123 } 124 125 func generateRandomFile(t *testing.T, size int) (f *os.File, teardown func()) { 126 //创建tmp文件 127 tmp, err := ioutil.TempFile("", "swarm-test") 128 if err != nil { 129 t.Fatal(err) 130 } 131 132 // 133 teardown = func() { 134 tmp.Close() 135 os.Remove(tmp.Name()) 136 } 137 138 // 139 buf := make([]byte, 10000000) 140 _, err = rand.Read(buf) 141 if err != nil { 142 t.Fatal(err) 143 } 144 ioutil.WriteFile(tmp.Name(), buf, 0755) 145 146 return tmp, teardown 147 }