github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/geth/consolecmd_test.go (about)

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