30d706c35e
* p2p/discover, p2p/discv5: add marshaling methods to Node * p2p/netutil: make Netlist decodable from TOML * common/math: encode nil HexOrDecimal256 as 0x0 * cmd/geth: add --config file flag * cmd/geth: add missing license header * eth: prettify Config again, fix tests * eth: use gasprice.Config instead of duplicating its fields * eth/gasprice: hide nil default from dumpconfig output * cmd/geth: hide genesis block in dumpconfig output * node: make tests compile * console: fix tests * cmd/geth: make TOML keys look exactly like Go struct fields * p2p: use discovery by default This makes the zero Config slightly more useful. It also fixes package node tests because Node detects reuse of the datadir through the NodeDatabase. * cmd/geth: make ethstats URL settable through config file * cmd/faucet: fix configuration * cmd/geth: dedup attach tests * eth: add comment for DefaultConfig * eth: pass downloader.SyncMode in Config This removes the FastSync, LightSync flags in favour of a more general SyncMode flag. * cmd/utils: remove jitvm flags * cmd/utils: make mutually exclusive flag error prettier It now reads: Fatal: flags --dev, --testnet can't be used at the same time * p2p: fix typo * node: add DefaultConfig, use it for geth * mobile: add missing NoDiscovery option * cmd/utils: drop MakeNode This exposed a couple of places that needed to be updated to use node.DefaultConfig. * node: fix typo * eth: make fast sync the default mode * cmd/utils: remove IPCApiFlag (unused) * node: remove default IPC path Set it in the frontends instead. * cmd/geth: add --syncmode * cmd/utils: make --ipcdisable and --ipcpath mutually exclusive * cmd/utils: don't enable WS, HTTP when setting addr * cmd/utils: fix --identity
151 lines
5.2 KiB
Go
151 lines
5.2 KiB
Go
// Copyright 2015 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package node
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
)
|
|
|
|
// Tests that datadirs can be successfully created, be them manually configured
|
|
// ones or automatically generated temporary ones.
|
|
func TestDatadirCreation(t *testing.T) {
|
|
// Create a temporary data dir and check that it can be used by a node
|
|
dir, err := ioutil.TempDir("", "")
|
|
if err != nil {
|
|
t.Fatalf("failed to create manual data dir: %v", err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
|
|
if _, err := New(&Config{DataDir: dir}); err != nil {
|
|
t.Fatalf("failed to create stack with existing datadir: %v", err)
|
|
}
|
|
// Generate a long non-existing datadir path and check that it gets created by a node
|
|
dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f")
|
|
if _, err := New(&Config{DataDir: dir}); err != nil {
|
|
t.Fatalf("failed to create stack with creatable datadir: %v", err)
|
|
}
|
|
if _, err := os.Stat(dir); err != nil {
|
|
t.Fatalf("freshly created datadir not accessible: %v", err)
|
|
}
|
|
// Verify that an impossible datadir fails creation
|
|
file, err := ioutil.TempFile("", "")
|
|
if err != nil {
|
|
t.Fatalf("failed to create temporary file: %v", err)
|
|
}
|
|
defer os.Remove(file.Name())
|
|
|
|
dir = filepath.Join(file.Name(), "invalid/path")
|
|
if _, err := New(&Config{DataDir: dir}); err == nil {
|
|
t.Fatalf("protocol stack created with an invalid datadir")
|
|
}
|
|
}
|
|
|
|
// Tests that IPC paths are correctly resolved to valid endpoints of different
|
|
// platforms.
|
|
func TestIPCPathResolution(t *testing.T) {
|
|
var tests = []struct {
|
|
DataDir string
|
|
IPCPath string
|
|
Windows bool
|
|
Endpoint string
|
|
}{
|
|
{"", "", false, ""},
|
|
{"data", "", false, ""},
|
|
{"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")},
|
|
{"data", "geth.ipc", false, "data/geth.ipc"},
|
|
{"data", "./geth.ipc", false, "./geth.ipc"},
|
|
{"data", "/geth.ipc", false, "/geth.ipc"},
|
|
{"", "", true, ``},
|
|
{"data", "", true, ``},
|
|
{"", "geth.ipc", true, `\\.\pipe\geth.ipc`},
|
|
{"data", "geth.ipc", true, `\\.\pipe\geth.ipc`},
|
|
{"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`},
|
|
}
|
|
for i, test := range tests {
|
|
// Only run when platform/test match
|
|
if (runtime.GOOS == "windows") == test.Windows {
|
|
if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint {
|
|
t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Tests that node keys can be correctly created, persisted, loaded and/or made
|
|
// ephemeral.
|
|
func TestNodeKeyPersistency(t *testing.T) {
|
|
// Create a temporary folder and make sure no key is present
|
|
dir, err := ioutil.TempDir("", "node-test")
|
|
if err != nil {
|
|
t.Fatalf("failed to create temporary data directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
|
|
keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)
|
|
|
|
// Configure a node with a preset key and ensure it's not persisted
|
|
key, err := crypto.GenerateKey()
|
|
if err != nil {
|
|
t.Fatalf("failed to generate one-shot node key: %v", err)
|
|
}
|
|
config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}}
|
|
config.NodeKey()
|
|
if _, err := os.Stat(filepath.Join(keyfile)); err == nil {
|
|
t.Fatalf("one-shot node key persisted to data directory")
|
|
}
|
|
|
|
// Configure a node with no preset key and ensure it is persisted this time
|
|
config = &Config{Name: "unit-test", DataDir: dir}
|
|
config.NodeKey()
|
|
if _, err := os.Stat(keyfile); err != nil {
|
|
t.Fatalf("node key not persisted to data directory: %v", err)
|
|
}
|
|
if _, err = crypto.LoadECDSA(keyfile); err != nil {
|
|
t.Fatalf("failed to load freshly persisted node key: %v", err)
|
|
}
|
|
blob1, err := ioutil.ReadFile(keyfile)
|
|
if err != nil {
|
|
t.Fatalf("failed to read freshly persisted node key: %v", err)
|
|
}
|
|
|
|
// Configure a new node and ensure the previously persisted key is loaded
|
|
config = &Config{Name: "unit-test", DataDir: dir}
|
|
config.NodeKey()
|
|
blob2, err := ioutil.ReadFile(filepath.Join(keyfile))
|
|
if err != nil {
|
|
t.Fatalf("failed to read previously persisted node key: %v", err)
|
|
}
|
|
if !bytes.Equal(blob1, blob2) {
|
|
t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1)
|
|
}
|
|
|
|
// Configure ephemeral node and ensure no key is dumped locally
|
|
config = &Config{Name: "unit-test", DataDir: ""}
|
|
config.NodeKey()
|
|
if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil {
|
|
t.Fatalf("ephemeral node key persisted to disk")
|
|
}
|
|
}
|