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