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