config_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. package node
  17. import (
  18. "bytes"
  19. "io/ioutil"
  20. "os"
  21. "path/filepath"
  22. "runtime"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/p2p"
  26. )
  27. // Tests that datadirs can be successfully created, be them manually configured
  28. // ones or automatically generated temporary ones.
  29. func TestDatadirCreation(t *testing.T) {
  30. // Create a temporary data dir and check that it can be used by a node
  31. dir, err := ioutil.TempDir("", "")
  32. if err != nil {
  33. t.Fatalf("failed to create manual data dir: %v", err)
  34. }
  35. defer os.RemoveAll(dir)
  36. node, err := New(&Config{DataDir: dir})
  37. if err != nil {
  38. t.Fatalf("failed to create stack with existing datadir: %v", err)
  39. }
  40. if err := node.Close(); err != nil {
  41. t.Fatalf("failed to close node: %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. node, err = New(&Config{DataDir: dir})
  46. if err != nil {
  47. t.Fatalf("failed to create stack with creatable datadir: %v", err)
  48. }
  49. if err := node.Close(); err != nil {
  50. t.Fatalf("failed to close node: %v", err)
  51. }
  52. if _, err := os.Stat(dir); err != nil {
  53. t.Fatalf("freshly created datadir not accessible: %v", err)
  54. }
  55. // Verify that an impossible datadir fails creation
  56. file, err := ioutil.TempFile("", "")
  57. if err != nil {
  58. t.Fatalf("failed to create temporary file: %v", err)
  59. }
  60. defer os.Remove(file.Name())
  61. dir = filepath.Join(file.Name(), "invalid/path")
  62. node, err = New(&Config{DataDir: dir})
  63. if err == nil {
  64. t.Fatalf("protocol stack created with an invalid datadir")
  65. if err := node.Close(); err != nil {
  66. t.Fatalf("failed to close node: %v", err)
  67. }
  68. }
  69. }
  70. // Tests that IPC paths are correctly resolved to valid endpoints of different
  71. // platforms.
  72. func TestIPCPathResolution(t *testing.T) {
  73. var tests = []struct {
  74. DataDir string
  75. IPCPath string
  76. Windows bool
  77. Endpoint string
  78. }{
  79. {"", "", false, ""},
  80. {"data", "", false, ""},
  81. {"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")},
  82. {"data", "geth.ipc", false, "data/geth.ipc"},
  83. {"data", "./geth.ipc", false, "./geth.ipc"},
  84. {"data", "/geth.ipc", false, "/geth.ipc"},
  85. {"", "", true, ``},
  86. {"data", "", true, ``},
  87. {"", "geth.ipc", true, `\\.\pipe\geth.ipc`},
  88. {"data", "geth.ipc", true, `\\.\pipe\geth.ipc`},
  89. {"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`},
  90. }
  91. for i, test := range tests {
  92. // Only run when platform/test match
  93. if (runtime.GOOS == "windows") == test.Windows {
  94. if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint {
  95. t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
  96. }
  97. }
  98. }
  99. }
  100. // Tests that node keys can be correctly created, persisted, loaded and/or made
  101. // ephemeral.
  102. func TestNodeKeyPersistency(t *testing.T) {
  103. // Create a temporary folder and make sure no key is present
  104. dir, err := ioutil.TempDir("", "node-test")
  105. if err != nil {
  106. t.Fatalf("failed to create temporary data directory: %v", err)
  107. }
  108. defer os.RemoveAll(dir)
  109. keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)
  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}}
  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. // Configure a node with no preset key and ensure it is persisted this time
  121. config = &Config{Name: "unit-test", DataDir: dir}
  122. config.NodeKey()
  123. if _, err := os.Stat(keyfile); err != nil {
  124. t.Fatalf("node key not persisted to data directory: %v", err)
  125. }
  126. if _, err = crypto.LoadECDSA(keyfile); err != nil {
  127. t.Fatalf("failed to load freshly persisted node key: %v", err)
  128. }
  129. blob1, err := ioutil.ReadFile(keyfile)
  130. if err != nil {
  131. t.Fatalf("failed to read freshly persisted node key: %v", err)
  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. // 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. }