github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/geth/consolecmd_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 //版权所有2016 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 "crypto/rand" 29 "math/big" 30 "os" 31 "path/filepath" 32 "runtime" 33 "strconv" 34 "strings" 35 "testing" 36 "time" 37 38 "github.com/ethereum/go-ethereum/params" 39 ) 40 41 const ( 42 ipcAPIs = "admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh:1.0 txpool:1.0 web3:1.0" 43 httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0" 44 ) 45 46 //测试控制台中嵌入的节点是否可以正确启动,以及 47 //然后通过关闭输入流终止。 48 func TestConsoleWelcome(t *testing.T) { 49 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 50 51 //启动一个geth控制台,确保它已清理干净并终止控制台 52 geth := runGeth(t, 53 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 54 "--etherbase", coinbase, "--shh", 55 "console") 56 57 //收集欢迎信息需要包含的所有信息 58 geth.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 59 geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 60 geth.SetTemplateFunc("gover", runtime.Version) 61 geth.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta }) 62 geth.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) 63 geth.SetTemplateFunc("apis", func() string { return ipcAPIs }) 64 65 //验证所需模板的实际欢迎消息 66 geth.Expect(` 67 Welcome to the Geth JavaScript console! 68 69 instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 70 coinbase: {{.Etherbase}} 71 at block: 0 ({{niltime}}) 72 datadir: {{.Datadir}} 73 modules: {{apis}} 74 75 > {{.InputLine "exit"}} 76 `) 77 geth.ExpectExit() 78 } 79 80 //测试控制台是否可以通过各种方式连接到正在运行的节点。 81 func TestIPCAttachWelcome(t *testing.T) { 82 //为IPC附件配置实例 83 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 84 var ipc string 85 if runtime.GOOS == "windows" { 86 ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999)) 87 } else { 88 ws := tmpdir(t) 89 defer os.RemoveAll(ws) 90 ipc = filepath.Join(ws, "geth.ipc") 91 } 92 //注意:我们需要--shh,因为tesattachwelcome检查默认值 93 //其中包括IPC模块和SHH列表。 94 geth := runGeth(t, 95 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 96 "--etherbase", coinbase, "--shh", "--ipcpath", ipc) 97 98 time.Sleep(2 * time.Second) //等待RPC端点打开的简单方法 99 testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs) 100 101 geth.Interrupt() 102 geth.ExpectExit() 103 } 104 105 func TestHTTPAttachWelcome(t *testing.T) { 106 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 107 port := strconv.Itoa(trulyRandInt(1024, 65536)) // 108 geth := runGeth(t, 109 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 110 "--etherbase", coinbase, "--rpc", "--rpcport", port) 111 112 time.Sleep(2 * time.Second) //等待RPC端点打开的简单方法 113 testAttachWelcome(t, geth, "http://本地主机:“+端口,httpapis) 114 115 geth.Interrupt() 116 geth.ExpectExit() 117 } 118 119 func TestWSAttachWelcome(t *testing.T) { 120 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 121 port := strconv.Itoa(trulyRandInt(1024, 65536)) //是的,有时会失败,对不起:P 122 123 geth := runGeth(t, 124 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 125 "--etherbase", coinbase, "--ws", "--wsport", port) 126 127 time.Sleep(2 * time.Second) //等待RPC端点打开的简单方法 128 testAttachWelcome(t, geth, "ws://本地主机:“+端口,httpapis) 129 130 geth.Interrupt() 131 geth.ExpectExit() 132 } 133 134 func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) { 135 //附加到正在运行的geth note并立即终止 136 attach := runGeth(t, "attach", endpoint) 137 defer attach.ExpectExit() 138 attach.CloseStdin() 139 140 //收集欢迎信息需要包含的所有信息 141 attach.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 142 attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 143 attach.SetTemplateFunc("gover", runtime.Version) 144 attach.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta }) 145 attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase }) 146 attach.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) 147 attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") }) 148 attach.SetTemplateFunc("datadir", func() string { return geth.Datadir }) 149 attach.SetTemplateFunc("apis", func() string { return apis }) 150 151 //验证所需模板的实际欢迎消息 152 attach.Expect(` 153 Welcome to the Geth JavaScript console! 154 155 instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 156 coinbase: {{etherbase}} 157 at block: 0 ({{niltime}}){{if ipc}} 158 datadir: {{datadir}}{{end}} 159 modules: {{apis}} 160 161 > {{.InputLine "exit" }} 162 `) 163 attach.ExpectExit() 164 } 165 166 //Trulyrandit生成控制台测试使用的加密随机整数 167 //不会与并行运行的其他测试冲突网络端口。 168 func trulyRandInt(lo, hi int) int { 169 num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) 170 return int(num.Int64()) + lo 171 }