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