config_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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("", "node-test")
  92. if err != nil {
  93. t.Fatalf("failed to create temporary data directory: %v", err)
  94. }
  95. defer os.RemoveAll(dir)
  96. keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)
  97. // Configure a node with a preset key and ensure it's not persisted
  98. key, err := crypto.GenerateKey()
  99. if err != nil {
  100. t.Fatalf("failed to generate one-shot node key: %v", err)
  101. }
  102. config := &Config{Name: "unit-test", DataDir: dir, PrivateKey: key}
  103. config.NodeKey()
  104. if _, err := os.Stat(filepath.Join(keyfile)); err == nil {
  105. t.Fatalf("one-shot node key persisted to data directory")
  106. }
  107. // Configure a node with no preset key and ensure it is persisted this time
  108. config = &Config{Name: "unit-test", DataDir: dir}
  109. config.NodeKey()
  110. if _, err := os.Stat(keyfile); err != nil {
  111. t.Fatalf("node key not persisted to data directory: %v", err)
  112. }
  113. if _, err = crypto.LoadECDSA(keyfile); err != nil {
  114. t.Fatalf("failed to load freshly persisted node key: %v", err)
  115. }
  116. blob1, err := ioutil.ReadFile(keyfile)
  117. if err != nil {
  118. t.Fatalf("failed to read freshly persisted node key: %v", err)
  119. }
  120. // Configure a new node and ensure the previously persisted key is loaded
  121. config = &Config{Name: "unit-test", DataDir: dir}
  122. config.NodeKey()
  123. blob2, err := ioutil.ReadFile(filepath.Join(keyfile))
  124. if err != nil {
  125. t.Fatalf("failed to read previously persisted node key: %v", err)
  126. }
  127. if !bytes.Equal(blob1, blob2) {
  128. t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1)
  129. }
  130. // Configure ephemeral node and ensure no key is dumped locally
  131. config = &Config{Name: "unit-test", DataDir: ""}
  132. config.NodeKey()
  133. if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil {
  134. t.Fatalf("ephemeral node key persisted to disk")
  135. }
  136. }