config_test.go 5.2 KB

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