github.com/Elemental-core/elementalcore@v0.0.0-20191206075037-63891242267a/cmd/geth/consolecmd_test.go (about)

     1  // Copyright 2016 The elementalcore Authors
     2  // This file is part of elementalcore.
     3  //
     4  // elementalcore 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  // elementalcore 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 elementalcore. 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/Elemental-core/elementalcore/params"
    31  )
    32  
    33  const (
    34  	ipcAPIs  = "admin:1.0 debug:1.0 dpos: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"
    35  	httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0"
    36  )
    37  
    38  // Tests that a node embedded within a console can be started up properly and
    39  // then terminated by closing the input stream.
    40  func TestConsoleWelcome(t *testing.T) {
    41  	validator := "0x34d4a8d9f6b53a8f5e674516cb8ad66c843b2801"
    42  	coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
    43  
    44  	// Start a geth console, make sure it's cleaned up and terminate the console
    45  	geth := runGeth(t,
    46  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
    47  		"--coinbase", coinbase, "--validator", validator, "--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("niltime", func() string { return time.Unix(1522052340, 0).Format(time.RFC1123) })
    56  	geth.SetTemplateFunc("apis", func() string { return ipcAPIs })
    57  
    58  	// Verify the actual welcome message to the required template
    59  	geth.Expect(`
    60  Welcome to the Geth JavaScript console!
    61  
    62   instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
    63  validator: {{.Validator}}
    64   coinbase: {{.Coinbase}}
    65   at block: 0 ({{niltime}})
    66    datadir: {{.Datadir}}
    67    modules: {{apis}}
    68  
    69  > {{.InputLine "exit"}}
    70  `)
    71  	geth.ExpectExit()
    72  }
    73  
    74  // Tests that a console can be attached to a running node via various means.
    75  func TestIPCAttachWelcome(t *testing.T) {
    76  	// Configure the instance for IPC attachement
    77  	validator := "0x34d4a8d9f6b53a8f5e674516cb8ad66c843b2801"
    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, "geth.ipc")
    86  	}
    87  	// Note: we need --shh because testAttachWelcome checks for default
    88  	// list of ipc modules and shh is included there.
    89  	geth := runGeth(t,
    90  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
    91  		"--coinbase", coinbase, "--validator", validator, "--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  	validator := "0x34d4a8d9f6b53a8f5e674516cb8ad66c843b2801"
   102  	coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
   103  	port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
   104  	geth := runGeth(t,
   105  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
   106  		"--coinbase", coinbase, "--validator", validator, "--rpc", "--rpcport", port)
   107  
   108  	time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
   109  	testAttachWelcome(t, geth, "http://localhost:"+port, httpAPIs)
   110  
   111  	geth.Interrupt()
   112  	geth.ExpectExit()
   113  }
   114  
   115  func TestWSAttachWelcome(t *testing.T) {
   116  	validator := "0x34d4a8d9f6b53a8f5e674516cb8ad66c843b2801"
   117  	coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
   118  	port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
   119  
   120  	geth := runGeth(t,
   121  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
   122  		"--coinbase", coinbase, "--validator", validator, "--ws", "--wsport", port)
   123  
   124  	time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
   125  	testAttachWelcome(t, geth, "ws://localhost:"+port, httpAPIs)
   126  
   127  	geth.Interrupt()
   128  	geth.ExpectExit()
   129  }
   130  
   131  func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
   132  	// Attach to a running geth note and terminate immediately
   133  	attach := runGeth(t, "attach", endpoint)
   134  	defer attach.ExpectExit()
   135  	attach.CloseStdin()
   136  
   137  	// Gather all the infos the welcome message needs to contain
   138  	attach.SetTemplateFunc("goos", func() string { return runtime.GOOS })
   139  	attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
   140  	attach.SetTemplateFunc("gover", runtime.Version)
   141  	attach.SetTemplateFunc("gethver", func() string { return params.Version })
   142  	attach.SetTemplateFunc("validator", func() string { return geth.Validator })
   143  	attach.SetTemplateFunc("coinbase", func() string { return geth.Coinbase })
   144  	attach.SetTemplateFunc("niltime", func() string { return time.Unix(1522052340, 0).Format(time.RFC1123) })
   145  	attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
   146  	attach.SetTemplateFunc("datadir", func() string { return geth.Datadir })
   147  	attach.SetTemplateFunc("apis", func() string { return apis })
   148  
   149  	// Verify the actual welcome message to the required template
   150  	attach.Expect(`
   151  Welcome to the Geth JavaScript console!
   152  
   153   instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
   154  validator: {{validator}}
   155   coinbase: {{coinbase}}
   156   at block: 0 ({{niltime}}){{if ipc}}
   157    datadir: {{datadir}}{{end}}
   158    modules: {{apis}}
   159  
   160  > {{.InputLine "exit" }}
   161  `)
   162  	attach.ExpectExit()
   163  }
   164  
   165  // trulyRandInt generates a crypto random integer used by the console tests to
   166  // not clash network ports with other tests running cocurrently.
   167  func trulyRandInt(lo, hi int) int {
   168  	num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
   169  	return int(num.Int64()) + lo
   170  }