github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/cmd/etn-sc/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/electroneum/electroneum-sc/params"
    30  )
    31  
    32  const (
    33  	ipcAPIs  = "admin:1.0 debug:1.0 eth:1.0 istanbul: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  	// --testnet 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{"--testnet", "--networkid", "1337", "--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  	nodekey := "ba6dc97bd7bbd4742adbb808136a96a9d59743fe06d7c0c740f528d36bbc82ac"
    54  	// Coinbase for this nodekey = 0x4c7968f79c1a414c34cd4d3c1ac7a3a8413da50c
    55  
    56  	// Start a geth console, make sure it's cleaned up and terminate the console
    57  	geth := runMinimalGeth(t, "--nodekeyhex", nodekey, "console")
    58  
    59  	// Gather all the infos the welcome message needs to contain
    60  	geth.SetTemplateFunc("goos", func() string { return runtime.GOOS })
    61  	geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
    62  	geth.SetTemplateFunc("gover", runtime.Version)
    63  	geth.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit("", "") })
    64  	geth.SetTemplateFunc("niltime", func() string {
    65  		return time.Unix(1655988353, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
    66  	})
    67  	geth.SetTemplateFunc("apis", func() string { return ipcAPIs })
    68  
    69  	// Verify the actual welcome message to the required template
    70  	geth.Expect(`
    71  Welcome to the ETN-SC JavaScript console!
    72  
    73  instance: etn-sc/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
    74  coinbase: 0x4c7968f79c1a414c34cd4d3c1ac7a3a8413da50c
    75  at block: 0 ({{niltime}})
    76   datadir: {{.Datadir}}
    77   modules: {{apis}}
    78  
    79  To exit, press ctrl-d or type exit
    80  > {{.InputLine "exit"}}
    81  `)
    82  	geth.ExpectExit()
    83  }
    84  
    85  // Tests that a console can be attached to a running node via various means.
    86  func TestAttachWelcome(t *testing.T) {
    87  	var (
    88  		ipc      string
    89  		httpPort string
    90  		wsPort   string
    91  	)
    92  	// Configure the instance for IPC attachment
    93  	if runtime.GOOS == "windows" {
    94  		ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999))
    95  	} else {
    96  		ipc = filepath.Join(t.TempDir(), "etn-sc.ipc")
    97  	}
    98  	// And HTTP + WS attachment
    99  	p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P
   100  	httpPort = strconv.Itoa(p)
   101  	wsPort = strconv.Itoa(p + 1)
   102  	geth := runMinimalGeth(t, "--nodekeyhex", "ba6dc97bd7bbd4742adbb808136a96a9d59743fe06d7c0c740f528d36bbc82ac",
   103  		"--ipcpath", ipc,
   104  		"--http", "--http.port", httpPort,
   105  		"--ws", "--ws.port", wsPort)
   106  	t.Run("ipc", func(t *testing.T) {
   107  		waitForEndpoint(t, ipc, 3*time.Second)
   108  		testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
   109  	})
   110  	t.Run("http", func(t *testing.T) {
   111  		endpoint := "http://127.0.0.1:" + httpPort
   112  		waitForEndpoint(t, endpoint, 3*time.Second)
   113  		testAttachWelcome(t, geth, endpoint, httpAPIs)
   114  	})
   115  	t.Run("ws", func(t *testing.T) {
   116  		endpoint := "ws://127.0.0.1:" + wsPort
   117  		waitForEndpoint(t, endpoint, 3*time.Second)
   118  		testAttachWelcome(t, geth, endpoint, httpAPIs)
   119  	})
   120  	geth.ExpectExit()
   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("niltime", func() string {
   135  		return time.Unix(1655988353, 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 ETN-SC JavaScript console!
   144  
   145  instance: etn-sc/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
   146  coinbase: 0x4c7968f79c1a414c34cd4d3c1ac7a3a8413da50c
   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 cocurrently.
   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  }