config_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "os"
  20. "path/filepath"
  21. "runtime"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/p2p"
  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 := t.TempDir()
  31. node, err := New(&Config{DataDir: dir})
  32. if err != nil {
  33. t.Fatalf("failed to create stack with existing datadir: %v", err)
  34. }
  35. if err := node.Close(); err != nil {
  36. t.Fatalf("failed to close node: %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. node, err = New(&Config{DataDir: dir})
  41. if err != nil {
  42. t.Fatalf("failed to create stack with creatable datadir: %v", err)
  43. }
  44. if err := node.Close(); err != nil {
  45. t.Fatalf("failed to close node: %v", err)
  46. }
  47. if _, err := os.Stat(dir); err != nil {
  48. t.Fatalf("freshly created datadir not accessible: %v", err)
  49. }
  50. // Verify that an impossible datadir fails creation
  51. file, err := os.CreateTemp("", "")
  52. if err != nil {
  53. t.Fatalf("failed to create temporary file: %v", err)
  54. }
  55. defer func() {
  56. file.Close()
  57. os.Remove(file.Name())
  58. }()
  59. dir = filepath.Join(file.Name(), "invalid/path")
  60. _, err = New(&Config{DataDir: dir})
  61. if err == nil {
  62. t.Fatalf("protocol stack created with an invalid datadir")
  63. }
  64. }
  65. // Tests that IPC paths are correctly resolved to valid endpoints of different
  66. // platforms.
  67. func TestIPCPathResolution(t *testing.T) {
  68. var tests = []struct {
  69. DataDir string
  70. IPCPath string
  71. Windows bool
  72. Endpoint string
  73. }{
  74. {"", "", false, ""},
  75. {"data", "", false, ""},
  76. {"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")},
  77. {"data", "geth.ipc", false, "data/geth.ipc"},
  78. {"data", "./geth.ipc", false, "./geth.ipc"},
  79. {"data", "/geth.ipc", false, "/geth.ipc"},
  80. {"", "", true, ``},
  81. {"data", "", true, ``},
  82. {"", "geth.ipc", true, `\\.\pipe\geth.ipc`},
  83. {"data", "geth.ipc", true, `\\.\pipe\geth.ipc`},
  84. {"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`},
  85. }
  86. for i, test := range tests {
  87. // Only run when platform/test match
  88. if (runtime.GOOS == "windows") == test.Windows {
  89. if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint {
  90. t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
  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 := t.TempDir()
  100. keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)
  101. // Configure a node with a preset key and ensure it's not persisted
  102. key, err := crypto.GenerateKey()
  103. if err != nil {
  104. t.Fatalf("failed to generate one-shot node key: %v", err)
  105. }
  106. config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}}
  107. config.NodeKey()
  108. if _, err := os.Stat(keyfile); err == nil {
  109. t.Fatalf("one-shot node key persisted to data directory")
  110. }
  111. // Configure a node with no preset key and ensure it is persisted this time
  112. config = &Config{Name: "unit-test", DataDir: dir}
  113. config.NodeKey()
  114. if _, err := os.Stat(keyfile); err != nil {
  115. t.Fatalf("node key not persisted to data directory: %v", err)
  116. }
  117. if _, err = crypto.LoadECDSA(keyfile); err != nil {
  118. t.Fatalf("failed to load freshly persisted node key: %v", err)
  119. }
  120. blob1, err := os.ReadFile(keyfile)
  121. if err != nil {
  122. t.Fatalf("failed to read freshly persisted node key: %v", err)
  123. }
  124. // Configure a new node and ensure the previously persisted key is loaded
  125. config = &Config{Name: "unit-test", DataDir: dir}
  126. config.NodeKey()
  127. blob2, err := os.ReadFile(keyfile)
  128. if err != nil {
  129. t.Fatalf("failed to read previously persisted node key: %v", err)
  130. }
  131. if !bytes.Equal(blob1, blob2) {
  132. t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1)
  133. }
  134. // Configure ephemeral node and ensure no key is dumped locally
  135. config = &Config{Name: "unit-test", DataDir: ""}
  136. config.NodeKey()
  137. if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil {
  138. t.Fatalf("ephemeral node key persisted to disk")
  139. }
  140. }