github.com/bearnetworkchain/go-bearnetwork@v1.10.19-0.20220604150648-d63890c2e42b/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/bearnetworkchain/go-bearnetwork/crypto" 27 "github.com/bearnetworkchain/go-bearnetwork/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("", "") 57 if err != nil { 58 t.Fatalf("failed to create temporary file: %v", err) 59 } 60 defer func() { 61 file.Close() 62 os.Remove(file.Name()) 63 }() 64 65 dir = filepath.Join(file.Name(), "invalid/path") 66 node, err = New(&Config{DataDir: dir}) 67 if err == nil { 68 t.Fatalf("protocol stack created with an invalid datadir") 69 if err := node.Close(); err != nil { 70 t.Fatalf("failed to close node: %v", err) 71 } 72 } 73 } 74 75 // Tests that IPC paths are correctly resolved to valid endpoints of different 76 // platforms. 77 func TestIPCPathResolution(t *testing.T) { 78 var tests = []struct { 79 DataDir string 80 IPCPath string 81 Windows bool 82 Endpoint string 83 }{ 84 {"", "", false, ""}, 85 {"data", "", false, ""}, 86 {"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")}, 87 {"data", "geth.ipc", false, "data/geth.ipc"}, 88 {"data", "./geth.ipc", false, "./geth.ipc"}, 89 {"data", "/geth.ipc", false, "/geth.ipc"}, 90 {"", "", true, ``}, 91 {"data", "", true, ``}, 92 {"", "geth.ipc", true, `\\.\pipe\geth.ipc`}, 93 {"data", "geth.ipc", true, `\\.\pipe\geth.ipc`}, 94 {"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`}, 95 } 96 for i, test := range tests { 97 // Only run when platform/test match 98 if (runtime.GOOS == "windows") == test.Windows { 99 if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint { 100 t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint) 101 } 102 } 103 } 104 } 105 106 // Tests that node keys can be correctly created, persisted, loaded and/or made 107 // ephemeral. 108 func TestNodeKeyPersistency(t *testing.T) { 109 // Create a temporary folder and make sure no key is present 110 dir := t.TempDir() 111 112 keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey) 113 114 // Configure a node with a preset key and ensure it's not persisted 115 key, err := crypto.GenerateKey() 116 if err != nil { 117 t.Fatalf("failed to generate one-shot node key: %v", err) 118 } 119 config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}} 120 config.NodeKey() 121 if _, err := os.Stat(filepath.Join(keyfile)); err == nil { 122 t.Fatalf("one-shot node key persisted to data directory") 123 } 124 125 // Configure a node with no preset key and ensure it is persisted this time 126 config = &Config{Name: "unit-test", DataDir: dir} 127 config.NodeKey() 128 if _, err := os.Stat(keyfile); err != nil { 129 t.Fatalf("node key not persisted to data directory: %v", err) 130 } 131 if _, err = crypto.LoadECDSA(keyfile); err != nil { 132 t.Fatalf("failed to load freshly persisted node key: %v", err) 133 } 134 blob1, err := os.ReadFile(keyfile) 135 if err != nil { 136 t.Fatalf("failed to read freshly persisted node key: %v", err) 137 } 138 139 // Configure a new node and ensure the previously persisted key is loaded 140 config = &Config{Name: "unit-test", DataDir: dir} 141 config.NodeKey() 142 blob2, err := os.ReadFile(filepath.Join(keyfile)) 143 if err != nil { 144 t.Fatalf("failed to read previously persisted node key: %v", err) 145 } 146 if !bytes.Equal(blob1, blob2) { 147 t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1) 148 } 149 150 // Configure ephemeral node and ensure no key is dumped locally 151 config = &Config{Name: "unit-test", DataDir: ""} 152 config.NodeKey() 153 if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil { 154 t.Fatalf("ephemeral node key persisted to disk") 155 } 156 }