github.com/bcnmy/go-ethereum@v1.10.27/cmd/geth/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  	"path/filepath"
    23  	"runtime"
    24  	"strconv"
    25  	"strings"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/ethereum/go-ethereum/params"
    30  )
    31  
    32  const (
    33  	ipcAPIs  = "admin:1.0 debug:1.0 engine:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0"
    34  	httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0"
    35  )
    36  
    37  // spawns geth with the given command line args, using a set of flags to minimise
    38  // memory and disk IO. If the args don't set --datadir, the
    39  // child g gets a temporary data directory.
    40  func runMinimalGeth(t *testing.T, args ...string) *testgeth {
    41  	// --ropsten to make the 'writing genesis to disk' faster (no accounts)
    42  	// --networkid=1337 to avoid cache bump
    43  	// --syncmode=full to avoid allocating fast sync bloom
    44  	allArgs := []string{"--ropsten", "--networkid", "1337", "--authrpc.port", "0", "--syncmode=full", "--port", "0",
    45  		"--nat", "none", "--nodiscover", "--maxpeers", "0", "--cache", "64",
    46  		"--datadir.minfreedisk", "0"}
    47  	return runGeth(t, append(allArgs, args...)...)
    48  }
    49  
    50  // Tests that a node embedded within a console can be started up properly and
    51  // then terminated by closing the input stream.
    52  func TestConsoleWelcome(t *testing.T) {
    53  	coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
    54  
    55  	// Start a geth console, make sure it's cleaned up and terminate the console
    56  	geth := runMinimalGeth(t, "--miner.etherbase", coinbase, "console")
    57  
    58  	// Gather all the infos the welcome message needs to contain
    59  	geth.SetTemplateFunc("goos", func() string { return runtime.GOOS })
    60  	geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
    61  	geth.SetTemplateFunc("gover", runtime.Version)
    62  	geth.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit("", "") })
    63  	geth.SetTemplateFunc("niltime", func() string {
    64  		return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
    65  	})
    66  	geth.SetTemplateFunc("apis", func() string { return ipcAPIs })
    67  
    68  	// Verify the actual welcome message to the required template
    69  	geth.Expect(`
    70  Welcome to the Geth JavaScript console!
    71  
    72  instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
    73  coinbase: {{.Etherbase}}
    74  at block: 0 ({{niltime}})
    75   datadir: {{.Datadir}}
    76   modules: {{apis}}
    77  
    78  To exit, press ctrl-d or type exit
    79  > {{.InputLine "exit"}}
    80  `)
    81  	geth.ExpectExit()
    82  }
    83  
    84  // Tests that a console can be attached to a running node via various means.
    85  func TestAttachWelcome(t *testing.T) {
    86  	var (
    87  		ipc      string
    88  		httpPort string
    89  		wsPort   string
    90  	)
    91  	// Configure the instance for IPC attachment
    92  	if runtime.GOOS == "windows" {
    93  		ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999))
    94  	} else {
    95  		ipc = filepath.Join(t.TempDir(), "geth.ipc")
    96  	}
    97  	// And HTTP + WS attachment
    98  	p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P
    99  	httpPort = strconv.Itoa(p)
   100  	wsPort = strconv.Itoa(p + 1)
   101  	geth := runMinimalGeth(t, "--miner.etherbase", "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182",
   102  		"--ipcpath", ipc,
   103  		"--http", "--http.port", httpPort,
   104  		"--ws", "--ws.port", wsPort)
   105  	t.Run("ipc", func(t *testing.T) {
   106  		waitForEndpoint(t, ipc, 3*time.Second)
   107  		testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
   108  	})
   109  	t.Run("http", func(t *testing.T) {
   110  		endpoint := "http://127.0.0.1:" + httpPort
   111  		waitForEndpoint(t, endpoint, 3*time.Second)
   112  		testAttachWelcome(t, geth, endpoint, httpAPIs)
   113  	})
   114  	t.Run("ws", func(t *testing.T) {
   115  		endpoint := "ws://127.0.0.1:" + wsPort
   116  		waitForEndpoint(t, endpoint, 3*time.Second)
   117  		testAttachWelcome(t, geth, endpoint, httpAPIs)
   118  	})
   119  	geth.ExpectExit()
   120  }
   121  
   122  func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
   123  	// Attach to a running geth note and terminate immediately
   124  	attach := runGeth(t, "attach", endpoint)
   125  	defer attach.ExpectExit()
   126  	attach.CloseStdin()
   127  
   128  	// Gather all the infos the welcome message needs to contain
   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.VersionWithCommit("", "") })
   133  	attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase })
   134  	attach.SetTemplateFunc("niltime", func() string {
   135  		return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
   136  	})
   137  	attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
   138  	attach.SetTemplateFunc("datadir", func() string { return geth.Datadir })
   139  	attach.SetTemplateFunc("apis", func() string { return apis })
   140  
   141  	// Verify the actual welcome message to the required template
   142  	attach.Expect(`
   143  Welcome to the Geth JavaScript console!
   144  
   145  instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
   146  coinbase: {{etherbase}}
   147  at block: 0 ({{niltime}}){{if ipc}}
   148   datadir: {{datadir}}{{end}}
   149   modules: {{apis}}
   150  
   151  To exit, press ctrl-d or type exit
   152  > {{.InputLine "exit" }}
   153  `)
   154  	attach.ExpectExit()
   155  }
   156  
   157  // trulyRandInt generates a crypto random integer used by the console tests to
   158  // not clash network ports with other tests running concurrently.
   159  func trulyRandInt(lo, hi int) int {
   160  	num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
   161  	return int(num.Int64()) + lo
   162  }