2015-11-06 00:57:57 +03:00
|
|
|
// 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"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-02-02 19:06:43 +02:00
|
|
|
"runtime"
|
2015-11-06 00:57:57 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-04-12 17:27:23 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2015-11-06 00:57:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2022-04-08 16:44:55 +03:00
|
|
|
dir := t.TempDir()
|
2015-11-06 00:57:57 +03:00
|
|
|
|
2019-02-07 13:40:36 +03:00
|
|
|
node, err := New(&Config{DataDir: dir})
|
|
|
|
if err != nil {
|
2015-11-06 00:57:57 +03:00
|
|
|
t.Fatalf("failed to create stack with existing datadir: %v", err)
|
|
|
|
}
|
2019-02-07 13:40:36 +03:00
|
|
|
if err := node.Close(); err != nil {
|
|
|
|
t.Fatalf("failed to close node: %v", err)
|
|
|
|
}
|
2015-11-06 00:57:57 +03:00
|
|
|
// 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")
|
2019-02-07 13:40:36 +03:00
|
|
|
node, err = New(&Config{DataDir: dir})
|
|
|
|
if err != nil {
|
2015-11-06 00:57:57 +03:00
|
|
|
t.Fatalf("failed to create stack with creatable datadir: %v", err)
|
|
|
|
}
|
2019-02-07 13:40:36 +03:00
|
|
|
if err := node.Close(); err != nil {
|
|
|
|
t.Fatalf("failed to close node: %v", err)
|
|
|
|
}
|
2015-11-06 00:57:57 +03:00
|
|
|
if _, err := os.Stat(dir); err != nil {
|
|
|
|
t.Fatalf("freshly created datadir not accessible: %v", err)
|
|
|
|
}
|
|
|
|
// Verify that an impossible datadir fails creation
|
2022-05-16 12:59:35 +03:00
|
|
|
file, err := os.CreateTemp("", "")
|
2015-11-06 00:57:57 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create temporary file: %v", err)
|
|
|
|
}
|
2022-04-08 16:44:55 +03:00
|
|
|
defer func() {
|
|
|
|
file.Close()
|
|
|
|
os.Remove(file.Name())
|
|
|
|
}()
|
2015-11-06 00:57:57 +03:00
|
|
|
|
|
|
|
dir = filepath.Join(file.Name(), "invalid/path")
|
2022-06-13 17:24:45 +03:00
|
|
|
_, err = New(&Config{DataDir: dir})
|
2019-02-07 13:40:36 +03:00
|
|
|
if err == nil {
|
2015-11-06 00:57:57 +03:00
|
|
|
t.Fatalf("protocol stack created with an invalid datadir")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 19:06:43 +02:00
|
|
|
// Tests that IPC paths are correctly resolved to valid endpoints of different
|
|
|
|
// platforms.
|
2016-02-09 13:24:42 +02:00
|
|
|
func TestIPCPathResolution(t *testing.T) {
|
2016-02-02 19:06:43 +02:00
|
|
|
var tests = []struct {
|
|
|
|
DataDir string
|
2016-02-09 13:24:42 +02:00
|
|
|
IPCPath string
|
2016-02-02 19:06:43 +02:00
|
|
|
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 {
|
2016-02-09 13:24:42 +02:00
|
|
|
if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint {
|
2016-02-02 19:06:43 +02:00
|
|
|
t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:57:57 +03:00
|
|
|
// 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
|
2022-04-08 16:44:55 +03:00
|
|
|
dir := t.TempDir()
|
2015-11-06 00:57:57 +03:00
|
|
|
|
2016-08-18 14:28:17 +03:00
|
|
|
keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)
|
|
|
|
|
2015-11-06 00:57:57 +03:00
|
|
|
// 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)
|
|
|
|
}
|
2017-04-12 17:27:23 +03:00
|
|
|
config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}}
|
2016-08-18 14:28:17 +03:00
|
|
|
config.NodeKey()
|
2022-08-04 10:42:03 +03:00
|
|
|
if _, err := os.Stat(keyfile); err == nil {
|
2015-11-06 00:57:57 +03:00
|
|
|
t.Fatalf("one-shot node key persisted to data directory")
|
|
|
|
}
|
2016-08-18 14:28:17 +03:00
|
|
|
|
2015-11-06 00:57:57 +03:00
|
|
|
// Configure a node with no preset key and ensure it is persisted this time
|
2016-08-18 14:28:17 +03:00
|
|
|
config = &Config{Name: "unit-test", DataDir: dir}
|
|
|
|
config.NodeKey()
|
|
|
|
if _, err := os.Stat(keyfile); err != nil {
|
2015-11-06 00:57:57 +03:00
|
|
|
t.Fatalf("node key not persisted to data directory: %v", err)
|
|
|
|
}
|
2017-01-09 13:16:06 +03:00
|
|
|
if _, err = crypto.LoadECDSA(keyfile); err != nil {
|
2015-11-06 00:57:57 +03:00
|
|
|
t.Fatalf("failed to load freshly persisted node key: %v", err)
|
|
|
|
}
|
2022-05-16 12:59:35 +03:00
|
|
|
blob1, err := os.ReadFile(keyfile)
|
2015-11-06 00:57:57 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read freshly persisted node key: %v", err)
|
|
|
|
}
|
2016-08-18 14:28:17 +03:00
|
|
|
|
2015-11-06 00:57:57 +03:00
|
|
|
// Configure a new node and ensure the previously persisted key is loaded
|
2016-08-18 14:28:17 +03:00
|
|
|
config = &Config{Name: "unit-test", DataDir: dir}
|
|
|
|
config.NodeKey()
|
2022-08-04 10:42:03 +03:00
|
|
|
blob2, err := os.ReadFile(keyfile)
|
2015-11-06 00:57:57 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read previously persisted node key: %v", err)
|
|
|
|
}
|
2017-01-06 18:44:20 +03:00
|
|
|
if !bytes.Equal(blob1, blob2) {
|
2015-11-06 00:57:57 +03:00
|
|
|
t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1)
|
|
|
|
}
|
2016-08-18 14:28:17 +03:00
|
|
|
|
2015-11-06 00:57:57 +03:00
|
|
|
// Configure ephemeral node and ensure no key is dumped locally
|
2016-08-18 14:28:17 +03:00
|
|
|
config = &Config{Name: "unit-test", DataDir: ""}
|
|
|
|
config.NodeKey()
|
|
|
|
if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil {
|
2015-11-06 00:57:57 +03:00
|
|
|
t.Fatalf("ephemeral node key persisted to disk")
|
|
|
|
}
|
|
|
|
}
|