config_test.go 5.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// 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"
24
	"runtime"
25 26 27
	"testing"

	"github.com/ethereum/go-ethereum/crypto"
28
	"github.com/ethereum/go-ethereum/p2p"
29 30 31 32 33 34 35 36 37 38 39 40
)

// 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)

41 42
	node, err := New(&Config{DataDir: dir})
	if err != nil {
43 44
		t.Fatalf("failed to create stack with existing datadir: %v", err)
	}
45 46 47
	if err := node.Close(); err != nil {
		t.Fatalf("failed to close node: %v", err)
	}
48 49
	// 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")
50 51
	node, err = New(&Config{DataDir: dir})
	if err != nil {
52 53
		t.Fatalf("failed to create stack with creatable datadir: %v", err)
	}
54 55 56
	if err := node.Close(); err != nil {
		t.Fatalf("failed to close node: %v", err)
	}
57 58 59 60 61 62 63 64 65 66 67
	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")
68 69
	node, err = New(&Config{DataDir: dir})
	if err == nil {
70
		t.Fatalf("protocol stack created with an invalid datadir")
71 72 73
		if err := node.Close(); err != nil {
			t.Fatalf("failed to close node: %v", err)
		}
74 75 76
	}
}

77 78
// Tests that IPC paths are correctly resolved to valid endpoints of different
// platforms.
79
func TestIPCPathResolution(t *testing.T) {
80 81
	var tests = []struct {
		DataDir  string
82
		IPCPath  string
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		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 {
101
			if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint {
102 103 104 105 106 107
				t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
			}
		}
	}
}

108 109 110 111
// 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
112
	dir, err := ioutil.TempDir("", "node-test")
113 114 115 116 117
	if err != nil {
		t.Fatalf("failed to create temporary data directory: %v", err)
	}
	defer os.RemoveAll(dir)

118 119
	keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)

120 121 122 123 124
	// 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)
	}
125
	config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}}
126 127
	config.NodeKey()
	if _, err := os.Stat(filepath.Join(keyfile)); err == nil {
128 129
		t.Fatalf("one-shot node key persisted to data directory")
	}
130

131
	// Configure a node with no preset key and ensure it is persisted this time
132 133 134
	config = &Config{Name: "unit-test", DataDir: dir}
	config.NodeKey()
	if _, err := os.Stat(keyfile); err != nil {
135 136
		t.Fatalf("node key not persisted to data directory: %v", err)
	}
137
	if _, err = crypto.LoadECDSA(keyfile); err != nil {
138 139
		t.Fatalf("failed to load freshly persisted node key: %v", err)
	}
140
	blob1, err := ioutil.ReadFile(keyfile)
141 142 143
	if err != nil {
		t.Fatalf("failed to read freshly persisted node key: %v", err)
	}
144

145
	// Configure a new node and ensure the previously persisted key is loaded
146 147 148
	config = &Config{Name: "unit-test", DataDir: dir}
	config.NodeKey()
	blob2, err := ioutil.ReadFile(filepath.Join(keyfile))
149 150 151
	if err != nil {
		t.Fatalf("failed to read previously persisted node key: %v", err)
	}
152
	if !bytes.Equal(blob1, blob2) {
153 154
		t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1)
	}
155

156
	// Configure ephemeral node and ensure no key is dumped locally
157 158 159
	config = &Config{Name: "unit-test", DataDir: ""}
	config.NodeKey()
	if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil {
160 161 162
		t.Fatalf("ephemeral node key persisted to disk")
	}
}