github.com/ethereum/go-ethereum@v1.16.1/node/config_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The go-ethereum library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package node 18 19 import ( 20 "bytes" 21 "os" 22 "path/filepath" 23 "runtime" 24 "testing" 25 26 "github.com/ethereum/go-ethereum/crypto" 27 "github.com/ethereum/go-ethereum/p2p" 28 ) 29 30 // Tests that datadirs can be successfully created, be them manually configured 31 // ones or automatically generated temporary ones. 32 func TestDatadirCreation(t *testing.T) { 33 // Create a temporary data dir and check that it can be used by a node 34 dir := t.TempDir() 35 36 node, err := New(&Config{DataDir: dir}) 37 if err != nil { 38 t.Fatalf("failed to create stack with existing datadir: %v", err) 39 } 40 if err := node.Close(); err != nil { 41 t.Fatalf("failed to close node: %v", err) 42 } 43 // Generate a long non-existing datadir path and check that it gets created by a node 44 dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f") 45 node, err = New(&Config{DataDir: dir}) 46 if err != nil { 47 t.Fatalf("failed to create stack with creatable datadir: %v", err) 48 } 49 if err := node.Close(); err != nil { 50 t.Fatalf("failed to close node: %v", err) 51 } 52 if _, err := os.Stat(dir); err != nil { 53 t.Fatalf("freshly created datadir not accessible: %v", err) 54 } 55 // Verify that an impossible datadir fails creation 56 file, err := os.CreateTemp(t.TempDir(), "") 57 if err != nil { 58 t.Fatalf("failed to create temporary file: %v", err) 59 } 60 defer func() { 61 file.Close() 62 }() 63 64 dir = filepath.Join(file.Name(), "invalid/path") 65 _, err = New(&Config{DataDir: dir}) 66 if err == nil { 67 t.Fatalf("protocol stack created with an invalid datadir") 68 } 69 } 70 71 // Tests that IPC paths are correctly resolved to valid endpoints of different 72 // platforms. 73 func TestIPCPathResolution(t *testing.T) { 74 var tests = []struct { 75 DataDir string 76 IPCPath string 77 Windows bool 78 Endpoint string 79 }{ 80 {"", "", false, ""}, 81 {"data", "", false, ""}, 82 {"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")}, 83 {"data", "geth.ipc", false, "data/geth.ipc"}, 84 {"data", "./geth.ipc", false, "./geth.ipc"}, 85 {"data", "/geth.ipc", false, "/geth.ipc"}, 86 {"", "", true, ``}, 87 {"data", "", true, ``}, 88 {"", "geth.ipc", true, `\\.\pipe\geth.ipc`}, 89 {"data", "geth.ipc", true, `\\.\pipe\geth.ipc`}, 90 {"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`}, 91 } 92 for i, test := range tests { 93 // Only run when platform/test match 94 if (runtime.GOOS == "windows") == test.Windows { 95 if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint { 96 t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint) 97 } 98 } 99 } 100 } 101 102 // Tests that node keys can be correctly created, persisted, loaded and/or made 103 // ephemeral. 104 func TestNodeKeyPersistency(t *testing.T) { 105 // Create a temporary folder and make sure no key is present 106 dir := t.TempDir() 107 108 keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey) 109 110 // Configure a node with a preset key and ensure it's not persisted 111 key, err := crypto.GenerateKey() 112 if err != nil { 113 t.Fatalf("failed to generate one-shot node key: %v", err) 114 } 115 config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}} 116 config.NodeKey() 117 if _, err := os.Stat(keyfile); err == nil { 118 t.Fatalf("one-shot node key persisted to data directory") 119 } 120 121 // Configure a node with no preset key and ensure it is persisted this time 122 config = &Config{Name: "unit-test", DataDir: dir} 123 config.NodeKey() 124 if _, err := os.Stat(keyfile); err != nil { 125 t.Fatalf("node key not persisted to data directory: %v", err) 126 } 127 if _, err = crypto.LoadECDSA(keyfile); err != nil { 128 t.Fatalf("failed to load freshly persisted node key: %v", err) 129 } 130 blob1, err := os.ReadFile(keyfile) 131 if err != nil { 132 t.Fatalf("failed to read freshly persisted node key: %v", err) 133 } 134 135 // Configure a new node and ensure the previously persisted key is loaded 136 config = &Config{Name: "unit-test", DataDir: dir} 137 config.NodeKey() 138 blob2, err := os.ReadFile(keyfile) 139 if err != nil { 140 t.Fatalf("failed to read previously persisted node key: %v", err) 141 } 142 if !bytes.Equal(blob1, blob2) { 143 t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1) 144 } 145 146 // Configure ephemeral node and ensure no key is dumped locally 147 config = &Config{Name: "unit-test", DataDir: ""} 148 config.NodeKey() 149 if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil { 150 t.Fatalf("ephemeral node key persisted to disk") 151 } 152 }