github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/cmd/platon/consolecmd_test.go (about)

     1  // Copyright 2016 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  
    17  package main
    18  
    19  import (
    20  	"crypto/rand"
    21  	"math/big"
    22  	"runtime"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/PlatONnetwork/PlatON-Go/params"
    28  )
    29  
    30  const (
    31  	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"
    32  	httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0"
    33  )
    34  
    35  // Tests that a node embedded within a console can be started up properly and
    36  // then terminated by closing the input stream.
    37  /*func TestConsoleWelcome(t *testing.T) {
    38  	coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
    39  
    40  	// Start a platon console, make sure it's cleaned up and terminate the console
    41  	geth := runGeth(t,
    42  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
    43  		"--etherbase", coinbase, "--shh",
    44  		"console")
    45  
    46  	// Gather all the infos the welcome message needs to contain
    47  	geth.SetTemplateFunc("goos", func() string { return runtime.GOOS })
    48  	geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
    49  	geth.SetTemplateFunc("gover", runtime.Version)
    50  	geth.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta })
    51  	geth.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
    52  	geth.SetTemplateFunc("apis", func() string { return ipcAPIs })
    53  
    54  	// Verify the actual welcome message to the required template
    55  	geth.Expect(`
    56  Welcome to the PlatON JavaScript console!
    57  
    58  instance: â—ŠPlatONnetwork/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
    59  coinbase: {{.Etherbase}}
    60  at block: 0 ({{niltime}})
    61   datadir: {{.Datadir}}
    62   modules: {{apis}}
    63  
    64  >
    65  `)
    66  	geth.ExpectExit()
    67  }*/
    68  
    69  // Tests that a console can be attached to a running node via various means.
    70  /*func TestIPCAttachWelcome(t *testing.T) {
    71  	// Configure the instance for IPC attachement
    72  	coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
    73  	var ipc string
    74  	if runtime.GOOS == "windows" {
    75  		ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999))
    76  	} else {
    77  		ws := tmpdir(t)
    78  		defer os.RemoveAll(ws)
    79  		ipc = filepath.Join(ws, "platon.ipc")
    80  	}
    81  	// Note: we need --shh because testAttachWelcome checks for default
    82  	// list of ipc modules and shh is included there.
    83  	geth := runGeth(t,
    84  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
    85  		"--etherbase", coinbase, "--shh", "--ipcpath", ipc)
    86  
    87  	time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
    88  	testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
    89  
    90  	geth.Interrupt()
    91  	geth.ExpectExit()
    92  }*/
    93  
    94  /*func TestHTTPAttachWelcome(t *testing.T) {
    95  	coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
    96  	port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
    97  	geth := runGeth(t,
    98  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
    99  		"--etherbase", coinbase, "--rpc", "--rpcport", port)
   100  
   101  	time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
   102  	testAttachWelcome(t, geth, "http://localhost:"+port, httpAPIs)
   103  
   104  	geth.Interrupt()
   105  	geth.ExpectExit()
   106  }*/
   107  
   108  /*func TestWSAttachWelcome(t *testing.T) {
   109  	coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
   110  	port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
   111  
   112  	geth := runGeth(t,
   113  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
   114  		"--etherbase", coinbase, "--ws", "--wsport", port)
   115  
   116  	time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
   117  	testAttachWelcome(t, geth, "ws://localhost:"+port, httpAPIs)
   118  
   119  	geth.Interrupt()
   120  	geth.ExpectExit()
   121  }*/
   122  
   123  func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
   124  	// Attach to a running platon note and terminate immediately
   125  	attach := runGeth(t, "attach", endpoint)
   126  	defer attach.ExpectExit()
   127  	attach.CloseStdin()
   128  
   129  	// Gather all the infos the welcome message needs to contain
   130  	attach.SetTemplateFunc("goos", func() string { return runtime.GOOS })
   131  	attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
   132  	attach.SetTemplateFunc("gover", runtime.Version)
   133  	attach.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta })
   134  	attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase })
   135  	attach.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
   136  	attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
   137  	attach.SetTemplateFunc("datadir", func() string { return geth.Datadir })
   138  	attach.SetTemplateFunc("apis", func() string { return apis })
   139  
   140  	// Verify the actual welcome message to the required template
   141  	attach.Expect(`
   142  Welcome to the PlatON JavaScript console!
   143  
   144  instance: platon/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
   145  coinbase: {{etherbase}}
   146  at block: 0 ({{niltime}}){{if ipc}}
   147   datadir: {{datadir}}{{end}}
   148   modules: {{apis}}
   149  
   150  > {{.InputLine "exit" }}
   151  `)
   152  	attach.ExpectExit()
   153  }
   154  
   155  // trulyRandInt generates a crypto random integer used by the console tests to
   156  // not clash network ports with other tests running cocurrently.
   157  func trulyRandInt(lo, hi int) int {
   158  	num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
   159  	return int(num.Int64()) + lo
   160  }