gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/node/config_test.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package node 18 19 import ( 20 "bytes" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "runtime" 25 "testing" 26 27 "gitlab.com/aquachain/aquachain/crypto" 28 "gitlab.com/aquachain/aquachain/p2p" 29 ) 30 31 var testp2p = p2p.Config{ChainId: 10101} 32 33 // Tests that datadirs can be successfully created, be them manually configured 34 // ones or automatically generated temporary ones. 35 func TestDatadirCreation(t *testing.T) { 36 // Create a temporary data dir and check that it can be used by a node 37 dir, err := ioutil.TempDir("", "") 38 if err != nil { 39 t.Fatalf("failed to create manual data dir: %v", err) 40 } 41 defer os.RemoveAll(dir) 42 43 if _, err := New(&Config{DataDir: dir, P2P: testp2p}); err != nil { 44 t.Fatalf("failed to create stack with existing datadir: %v", err) 45 } 46 // Generate a long non-existing datadir path and check that it gets created by a node 47 dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f") 48 if _, err := New(&Config{DataDir: dir, P2P: testp2p}); err != nil { 49 t.Fatalf("failed to create stack with creatable datadir: %v", err) 50 } 51 if _, err := os.Stat(dir); err != nil { 52 t.Fatalf("freshly created datadir not accessible: %v", err) 53 } 54 // Verify that an impossible datadir fails creation 55 file, err := ioutil.TempFile("", "") 56 if err != nil { 57 t.Fatalf("failed to create temporary file: %v", err) 58 } 59 defer os.Remove(file.Name()) 60 61 dir = filepath.Join(file.Name(), "invalid/path") 62 if _, err := New(&Config{DataDir: dir, P2P: testp2p}); err == nil { 63 t.Fatalf("protocol stack created with an invalid datadir") 64 } 65 } 66 67 // Tests that IPC paths are correctly resolved to valid endpoints of different 68 // platforms. 69 func TestIPCPathResolution(t *testing.T) { 70 var tests = []struct { 71 DataDir string 72 IPCPath string 73 Windows bool 74 Endpoint string 75 }{ 76 {"", "", false, ""}, 77 {"data", "", false, ""}, 78 {"", "aquachain.ipc", false, filepath.Join(os.TempDir(), "aquachain.ipc")}, 79 {"data", "aquachain.ipc", false, "data/aquachain.ipc"}, 80 {"data", "./aquachain.ipc", false, "./aquachain.ipc"}, 81 {"data", "/aquachain.ipc", false, "/aquachain.ipc"}, 82 {"", "", true, ``}, 83 {"data", "", true, ``}, 84 {"", "aquachain.ipc", true, `\\.\pipe\aquachain.ipc`}, 85 {"data", "aquachain.ipc", true, `\\.\pipe\aquachain.ipc`}, 86 {"data", `\\.\pipe\aquachain.ipc`, true, `\\.\pipe\aquachain.ipc`}, 87 } 88 for i, test := range tests { 89 // Only run when platform/test match 90 if (runtime.GOOS == "windows") == test.Windows { 91 if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath, P2P: testp2p}).IPCEndpoint(); endpoint != test.Endpoint { 92 t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint) 93 } 94 } 95 } 96 } 97 98 // Tests that node keys can be correctly created, persisted, loaded and/or made 99 // ephemeral. 100 func TestNodeKeyPersistency(t *testing.T) { 101 // Create a temporary folder and make sure no key is present 102 dir, err := ioutil.TempDir("", "node-test") 103 if err != nil { 104 t.Fatalf("failed to create temporary data directory: %v", err) 105 } 106 defer os.RemoveAll(dir) 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, ChainId: 10101}} 116 config.NodeKey() 117 if _, err := os.Stat(filepath.Join(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, P2P: testp2p} 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 := ioutil.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 := ioutil.ReadFile(filepath.Join(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 }