github.com/phillinzzz/newBsc@v1.1.6/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  	"os"
    23  	"path/filepath"
    24  	"runtime"
    25  	"strconv"
    26  	"strings"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/phillinzzz/newBsc/params"
    31  )
    32  
    33  const (
    34  	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 txpool:1.0 web3:1.0"
    35  	httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0"
    36  )
    37  
    38  // spawns geth with the given command line args, using a set of flags to minimise
    39  // memory and disk IO. If the args don't set --datadir, the
    40  // child g gets a temporary data directory.
    41  func runMinimalGeth(t *testing.T, args ...string) *testgeth {
    42  	// --ropsten to make the 'writing genesis to disk' faster (no accounts)
    43  	// --networkid=1337 to avoid cache bump
    44  	// --syncmode=full to avoid allocating fast sync bloom
    45  	allArgs := []string{"--ropsten", "--networkid", "1337", "--syncmode=full", "--port", "0",
    46  		"--nat", "none", "--nodiscover", "--maxpeers", "0", "--cache", "64"}
    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
    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  		ws := tmpdir(t)
    96  		defer os.RemoveAll(ws)
    97  		ipc = filepath.Join(ws, "geth.ipc")
    98  	}
    99  	// And HTTP + WS attachment
   100  	p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P
   101  	httpPort = strconv.Itoa(p)
   102  	wsPort = strconv.Itoa(p + 1)
   103  	geth := runMinimalGeth(t, "--miner.etherbase", "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182",
   104  		"--ipcpath", ipc,
   105  		"--http", "--http.port", httpPort,
   106  		"--ws", "--ws.port", wsPort)
   107  	t.Run("ipc", func(t *testing.T) {
   108  		waitForEndpoint(t, ipc, 3*time.Second)
   109  		testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
   110  	})
   111  	t.Run("http", func(t *testing.T) {
   112  		endpoint := "http://127.0.0.1:" + httpPort
   113  		waitForEndpoint(t, endpoint, 3*time.Second)
   114  		testAttachWelcome(t, geth, endpoint, httpAPIs)
   115  	})
   116  	t.Run("ws", func(t *testing.T) {
   117  		endpoint := "ws://127.0.0.1:" + wsPort
   118  		waitForEndpoint(t, endpoint, 3*time.Second)
   119  		testAttachWelcome(t, geth, endpoint, httpAPIs)
   120  	})
   121  }
   122  
   123  func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
   124  	// Attach to a running geth 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.VersionWithCommit("", "") })
   134  	attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase })
   135  	attach.SetTemplateFunc("niltime", func() string {
   136  		return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
   137  	})
   138  	attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
   139  	attach.SetTemplateFunc("datadir", func() string { return geth.Datadir })
   140  	attach.SetTemplateFunc("apis", func() string { return apis })
   141  
   142  	// Verify the actual welcome message to the required template
   143  	attach.Expect(`
   144  Welcome to the Geth JavaScript console!
   145  
   146  instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
   147  coinbase: {{etherbase}}
   148  at block: 0 ({{niltime}}){{if ipc}}
   149   datadir: {{datadir}}{{end}}
   150   modules: {{apis}}
   151  
   152  To exit, press ctrl-d
   153  > {{.InputLine "exit" }}
   154  `)
   155  	attach.ExpectExit()
   156  }
   157  
   158  // trulyRandInt generates a crypto random integer used by the console tests to
   159  // not clash network ports with other tests running cocurrently.
   160  func trulyRandInt(lo, hi int) int {
   161  	num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
   162  	return int(num.Int64()) + lo
   163  }