config_test.go 5.4 KB

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