github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/network/node/config_test.go (about) 1 package node 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "runtime" 9 "testing" 10 11 "github.com/neatlab/neatio/network/p2p" 12 "github.com/neatlab/neatio/utilities/crypto" 13 ) 14 15 func TestDatadirCreation(t *testing.T) { 16 17 dir, err := ioutil.TempDir("", "") 18 if err != nil { 19 t.Fatalf("failed to create manual data dir: %v", err) 20 } 21 defer os.RemoveAll(dir) 22 23 if _, err := New(&Config{DataDir: dir}); err != nil { 24 t.Fatalf("failed to create stack with existing datadir: %v", err) 25 } 26 27 dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f") 28 if _, err := New(&Config{DataDir: dir}); err != nil { 29 t.Fatalf("failed to create stack with creatable datadir: %v", err) 30 } 31 if _, err := os.Stat(dir); err != nil { 32 t.Fatalf("freshly created datadir not accessible: %v", err) 33 } 34 35 file, err := ioutil.TempFile("", "") 36 if err != nil { 37 t.Fatalf("failed to create temporary file: %v", err) 38 } 39 defer os.Remove(file.Name()) 40 41 dir = filepath.Join(file.Name(), "invalid/path") 42 if _, err := New(&Config{DataDir: dir}); err == nil { 43 t.Fatalf("protocol stack created with an invalid datadir") 44 } 45 } 46 47 func TestIPCPathResolution(t *testing.T) { 48 var tests = []struct { 49 DataDir string 50 IPCPath string 51 Windows bool 52 Endpoint string 53 }{ 54 {"", "", false, ""}, 55 {"data", "", false, ""}, 56 {"", "neatio.ipc", false, filepath.Join(os.TempDir(), "neatio.ipc")}, 57 {"data", "neatio.ipc", false, "data/neatio.ipc"}, 58 {"data", "./neatio.ipc", false, "./neatio.ipc"}, 59 {"data", "/neatio.ipc", false, "/neatio.ipc"}, 60 {"", "", true, ``}, 61 {"data", "", true, ``}, 62 {"", "neatio.ipc", true, `\\.\pipe\neatio.ipc`}, 63 {"data", "neatio.ipc", true, `\\.\pipe\neatio.ipc`}, 64 {"data", `\\.\pipe\neatio.ipc`, true, `\\.\pipe\neatio.ipc`}, 65 } 66 for i, test := range tests { 67 68 if (runtime.GOOS == "windows") == test.Windows { 69 if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint { 70 t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint) 71 } 72 } 73 } 74 } 75 76 func TestNodeKeyPersistency(t *testing.T) { 77 78 dir, err := ioutil.TempDir("", "node-test") 79 if err != nil { 80 t.Fatalf("failed to create temporary data directory: %v", err) 81 } 82 defer os.RemoveAll(dir) 83 84 keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey) 85 86 key, err := crypto.GenerateKey() 87 if err != nil { 88 t.Fatalf("failed to generate one-shot node key: %v", err) 89 } 90 config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}} 91 config.NodeKey() 92 if _, err := os.Stat(filepath.Join(keyfile)); err == nil { 93 t.Fatalf("one-shot node key persisted to data directory") 94 } 95 96 config = &Config{Name: "unit-test", DataDir: dir} 97 config.NodeKey() 98 if _, err := os.Stat(keyfile); err != nil { 99 t.Fatalf("node key not persisted to data directory: %v", err) 100 } 101 if _, err = crypto.LoadECDSA(keyfile); err != nil { 102 t.Fatalf("failed to load freshly persisted node key: %v", err) 103 } 104 blob1, err := ioutil.ReadFile(keyfile) 105 if err != nil { 106 t.Fatalf("failed to read freshly persisted node key: %v", err) 107 } 108 109 config = &Config{Name: "unit-test", DataDir: dir} 110 config.NodeKey() 111 blob2, err := ioutil.ReadFile(filepath.Join(keyfile)) 112 if err != nil { 113 t.Fatalf("failed to read previously persisted node key: %v", err) 114 } 115 if !bytes.Equal(blob1, blob2) { 116 t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1) 117 } 118 119 config = &Config{Name: "unit-test", DataDir: ""} 120 config.NodeKey() 121 if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil { 122 t.Fatalf("ephemeral node key persisted to disk") 123 } 124 }