github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/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 "github.com/ethereumproject/go-ethereum/crypto" 22 "os" 23 "path/filepath" 24 "runtime" 25 "testing" 26 27 "github.com/spf13/afero" 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 memFS := &fs{afero.NewMemMapFs()} 35 36 dir := filepath.Join("path", "to", "datadir") 37 err := memFS.MkdirAll(dir, os.ModePerm) // just in mem... heh 38 if err != nil { 39 t.Fatalf("failed to create manual data dir: %v", err) 40 } 41 42 if _, err := New(&Config{DataDir: dir, fs: memFS}); err != nil { 43 t.Fatalf("failed to create stack with existing datadir: %v", err) 44 } 45 // Generate a long non-existing datadir path and check that it gets created by a node 46 dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f") 47 if _, err := New(&Config{DataDir: dir, fs: memFS}); err != nil { 48 t.Fatalf("failed to create stack with creatable datadir: %v", err) 49 } 50 if _, err := memFS.Stat(dir); err != nil { 51 t.Fatalf("freshly created datadir not accessible: %v", err) 52 } 53 // Verify that an impossible datadir fails creation 54 // Impossible here means that it would attempt to be created on top of an already existing file 55 osFS := &fs{afero.NewOsFs()} // work around https://github.com/spf13/afero/issues/164 56 file, err := afero.TempFile(osFS, "", "") 57 if err != nil { 58 t.Fatalf("failed to create temporary file: %v", err) 59 } 60 defer osFS.Remove(file.Name()) 61 62 dir = filepath.Join(file.Name(), "invalid/path") 63 if n, err := New(&Config{DataDir: dir, fs: osFS}); err == nil { 64 t.Fatalf("protocol stack created with an invalid datadir: %s / %s\nabove file=%s", dir, n.DataDir(), file.Name()) 65 } 66 } 67 68 // Tests that IPC paths are correctly resolved to valid endpoints of different 69 // platforms. 70 func TestIPCPathResolution(t *testing.T) { 71 72 var tests = []struct { 73 DataDir string 74 IPCPath string 75 Windows bool 76 Endpoint string 77 }{ 78 {"", "", false, ""}, 79 {"data", "", false, ""}, 80 {"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")}, // test 2 81 {"data", "geth.ipc", false, "data/geth.ipc"}, 82 {"data", "./geth.ipc", false, "./geth.ipc"}, 83 {"data", "/geth.ipc", false, "/geth.ipc"}, 84 {"", "", true, ``}, 85 {"data", "", true, ``}, 86 {"", "geth.ipc", true, `\\.\pipe\geth.ipc`}, 87 {"data", "geth.ipc", true, `\\.\pipe\geth.ipc`}, 88 {"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`}, 89 } 90 for i, test := range tests { 91 // Only run when platform/test match 92 if (runtime.GOOS == "windows") == test.Windows { 93 if gotEndPoint := (&Config{ 94 DataDir: test.DataDir, 95 IPCPath: test.IPCPath, 96 fs: &fs{afero.NewMemMapFs()}, 97 }).IPCEndpoint(); gotEndPoint != test.Endpoint { 98 t.Errorf("test %d: IPC endpoint mismatch: got: %s, want: %s", i, gotEndPoint, test.Endpoint) 99 } 100 } 101 } 102 } 103 104 // Tests that node keys can be correctly created, persisted, loaded and/or made 105 // ephemeral. 106 func TestNodeKeyPersistency(t *testing.T) { 107 dir := os.TempDir() 108 memFS := &fs{afero.NewMemMapFs()} 109 110 if _, err := memFS.Stat(filepath.Join(dir, datadirPrivateKey)); err == nil { 111 t.Fatalf("non-created node key already exists") 112 } 113 // Configure a node with a preset key and ensure it's not persisted 114 key, err := crypto.GenerateKey() 115 if err != nil { 116 t.Fatalf("failed to generate one-shot node key: %v", err) 117 } 118 if _, err := New(&Config{DataDir: dir, PrivateKey: key, fs: memFS}); err != nil { 119 t.Fatalf("failed to create empty stack: %v", err) 120 } 121 if _, err := memFS.Stat(filepath.Join(dir, datadirPrivateKey)); err == nil { 122 t.Fatalf("one-shot node key persisted to data directory") 123 } 124 // Configure a node with no preset key and ensure it is persisted this time 125 if _, err := New(&Config{DataDir: dir, fs: memFS}); err != nil { 126 t.Fatalf("failed to create newly keyed stack: %v", err) 127 } 128 if _, err := memFS.Stat(filepath.Join(dir, datadirPrivateKey)); err != nil { 129 t.Fatalf("node key not persisted to data directory: %v", err) 130 } 131 blob1, err := afero.ReadFile(memFS, filepath.Join(dir, datadirPrivateKey)) 132 if err != nil { 133 t.Fatalf("failed to read freshly persisted node key: %v", err) 134 } 135 f, err := memFS.Open(filepath.Join(dir, datadirPrivateKey)) 136 if err != nil { 137 t.Fatalf("failed to open private key file: %v", err) 138 } 139 key, err = crypto.LoadECDSA(f) 140 if err != nil { 141 t.Fatalf("failed to load freshly persisted node key: %v", err) 142 } 143 if err := f.Close(); err != nil { 144 t.Fatalf("failed to close node key: %v", err) 145 } 146 // Configure a new node and ensure the previously persisted key is loaded 147 if _, err := New(&Config{DataDir: dir, fs: memFS}); err != nil { 148 t.Fatalf("failed to create previously keyed stack: %v", err) 149 } 150 blob2, err := afero.ReadFile(memFS, filepath.Join(dir, datadirPrivateKey)) 151 if err != nil { 152 t.Fatalf("failed to read previously persisted node key: %v", err) 153 } 154 if bytes.Compare(blob1, blob2) != 0 { 155 t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1) 156 } 157 // Configure ephemeral node and ensure no key is dumped locally 158 if _, err := New(&Config{DataDir: "", fs: memFS}); err != nil { 159 t.Fatalf("failed to create ephemeral stack: %v", err) 160 } 161 if _, err := memFS.Stat(filepath.Join(".", datadirPrivateKey)); err == nil { 162 t.Fatalf("ephemeral node key persisted to disk") 163 } 164 }