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