config_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "testing"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. )
  25. // Tests that datadirs can be successfully created, be them manually configured
  26. // ones or automatically generated temporary ones.
  27. func TestDatadirCreation(t *testing.T) {
  28. // Create a temporary data dir and check that it can be used by a node
  29. dir, err := ioutil.TempDir("", "")
  30. if err != nil {
  31. t.Fatalf("failed to create manual data dir: %v", err)
  32. }
  33. defer os.RemoveAll(dir)
  34. if _, err := New(&Config{DataDir: dir}); err != nil {
  35. t.Fatalf("failed to create stack with existing datadir: %v", err)
  36. }
  37. // Generate a long non-existing datadir path and check that it gets created by a node
  38. dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f")
  39. if _, err := New(&Config{DataDir: dir}); err != nil {
  40. t.Fatalf("failed to create stack with creatable datadir: %v", err)
  41. }
  42. if _, err := os.Stat(dir); err != nil {
  43. t.Fatalf("freshly created datadir not accessible: %v", err)
  44. }
  45. // Verify that an impossible datadir fails creation
  46. file, err := ioutil.TempFile("", "")
  47. if err != nil {
  48. t.Fatalf("failed to create temporary file: %v", err)
  49. }
  50. defer os.Remove(file.Name())
  51. dir = filepath.Join(file.Name(), "invalid/path")
  52. if _, err := New(&Config{DataDir: dir}); err == nil {
  53. t.Fatalf("protocol stack created with an invalid datadir")
  54. }
  55. }
  56. // Tests that node keys can be correctly created, persisted, loaded and/or made
  57. // ephemeral.
  58. func TestNodeKeyPersistency(t *testing.T) {
  59. // Create a temporary folder and make sure no key is present
  60. dir, err := ioutil.TempDir("", "")
  61. if err != nil {
  62. t.Fatalf("failed to create temporary data directory: %v", err)
  63. }
  64. defer os.RemoveAll(dir)
  65. if _, err := os.Stat(filepath.Join(dir, datadirPrivateKey)); err == nil {
  66. t.Fatalf("non-created node key already exists")
  67. }
  68. // Configure a node with a preset key and ensure it's not persisted
  69. key, err := crypto.GenerateKey()
  70. if err != nil {
  71. t.Fatalf("failed to generate one-shot node key: %v", err)
  72. }
  73. if _, err := New(&Config{DataDir: dir, PrivateKey: key}); err != nil {
  74. t.Fatalf("failed to create empty stack: %v", err)
  75. }
  76. if _, err := os.Stat(filepath.Join(dir, datadirPrivateKey)); err == nil {
  77. t.Fatalf("one-shot node key persisted to data directory")
  78. }
  79. // Configure a node with no preset key and ensure it is persisted this time
  80. if _, err := New(&Config{DataDir: dir}); err != nil {
  81. t.Fatalf("failed to create newly keyed stack: %v", err)
  82. }
  83. if _, err := os.Stat(filepath.Join(dir, datadirPrivateKey)); err != nil {
  84. t.Fatalf("node key not persisted to data directory: %v", err)
  85. }
  86. key, err = crypto.LoadECDSA(filepath.Join(dir, datadirPrivateKey))
  87. if err != nil {
  88. t.Fatalf("failed to load freshly persisted node key: %v", err)
  89. }
  90. blob1, err := ioutil.ReadFile(filepath.Join(dir, datadirPrivateKey))
  91. if err != nil {
  92. t.Fatalf("failed to read freshly persisted node key: %v", err)
  93. }
  94. // Configure a new node and ensure the previously persisted key is loaded
  95. if _, err := New(&Config{DataDir: dir}); err != nil {
  96. t.Fatalf("failed to create previously keyed stack: %v", err)
  97. }
  98. blob2, err := ioutil.ReadFile(filepath.Join(dir, datadirPrivateKey))
  99. if err != nil {
  100. t.Fatalf("failed to read previously persisted node key: %v", err)
  101. }
  102. if bytes.Compare(blob1, blob2) != 0 {
  103. t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1)
  104. }
  105. // Configure ephemeral node and ensure no key is dumped locally
  106. if _, err := New(&Config{DataDir: ""}); err != nil {
  107. t.Fatalf("failed to create ephemeral stack: %v", err)
  108. }
  109. if _, err := os.Stat(filepath.Join(".", datadirPrivateKey)); err == nil {
  110. t.Fatalf("ephemeral node key persisted to disk")
  111. }
  112. }