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