config_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "path"
  23. "path/filepath"
  24. "runtime"
  25. "testing"
  26. "time"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/plugin"
  29. "github.com/ethereum/go-ethereum/params"
  30. "github.com/stretchr/testify/assert"
  31. "github.com/ethereum/go-ethereum/crypto"
  32. "github.com/ethereum/go-ethereum/p2p"
  33. )
  34. // Tests that datadirs can be successfully created, be them manually configured
  35. // ones or automatically generated temporary ones.
  36. func TestDatadirCreation(t *testing.T) {
  37. // Create a temporary data dir and check that it can be used by a node
  38. dir, err := ioutil.TempDir("", "")
  39. if err != nil {
  40. t.Fatalf("failed to create manual data dir: %v", err)
  41. }
  42. defer os.RemoveAll(dir)
  43. node, err := New(&Config{DataDir: dir})
  44. if err != nil {
  45. t.Fatalf("failed to create stack with existing datadir: %v", err)
  46. }
  47. if err := node.Close(); err != nil {
  48. t.Fatalf("failed to close node: %v", err)
  49. }
  50. // Generate a long non-existing datadir path and check that it gets created by a node
  51. dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f")
  52. node, err = New(&Config{DataDir: dir})
  53. if err != nil {
  54. t.Fatalf("failed to create stack with creatable datadir: %v", err)
  55. }
  56. if err := node.Close(); err != nil {
  57. t.Fatalf("failed to close node: %v", err)
  58. }
  59. if _, err := os.Stat(dir); err != nil {
  60. t.Fatalf("freshly created datadir not accessible: %v", err)
  61. }
  62. // Verify that an impossible datadir fails creation
  63. file, err := ioutil.TempFile("", "")
  64. if err != nil {
  65. t.Fatalf("failed to create temporary file: %v", err)
  66. }
  67. defer os.Remove(file.Name())
  68. dir = filepath.Join(file.Name(), "invalid/path")
  69. node, err = New(&Config{DataDir: dir})
  70. if err == nil {
  71. t.Fatalf("protocol stack created with an invalid datadir")
  72. if err := node.Close(); err != nil {
  73. t.Fatalf("failed to close node: %v", err)
  74. }
  75. }
  76. }
  77. // Tests that IPC paths are correctly resolved to valid endpoints of different
  78. // platforms.
  79. func TestIPCPathResolution(t *testing.T) {
  80. var tests = []struct {
  81. DataDir string
  82. IPCPath string
  83. Windows bool
  84. Endpoint string
  85. }{
  86. {"", "", false, ""},
  87. {"data", "", false, ""},
  88. {"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")},
  89. {"data", "geth.ipc", false, "data/geth.ipc"},
  90. {"data", "./geth.ipc", false, "./geth.ipc"},
  91. {"data", "/geth.ipc", false, "/geth.ipc"},
  92. {"", "", true, ``},
  93. {"data", "", true, ``},
  94. {"", "geth.ipc", true, `\\.\pipe\geth.ipc`},
  95. {"data", "geth.ipc", true, `\\.\pipe\geth.ipc`},
  96. {"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`},
  97. }
  98. for i, test := range tests {
  99. // Only run when platform/test match
  100. if (runtime.GOOS == "windows") == test.Windows {
  101. if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint {
  102. t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
  103. }
  104. }
  105. }
  106. }
  107. // Tests that node keys can be correctly created, persisted, loaded and/or made
  108. // ephemeral.
  109. func TestNodeKeyPersistency(t *testing.T) {
  110. // Create a temporary folder and make sure no key is present
  111. dir, err := ioutil.TempDir("", "node-test")
  112. if err != nil {
  113. t.Fatalf("failed to create temporary data directory: %v", err)
  114. }
  115. defer os.RemoveAll(dir)
  116. keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)
  117. // Configure a node with a preset key and ensure it's not persisted
  118. key, err := crypto.GenerateKey()
  119. if err != nil {
  120. t.Fatalf("failed to generate one-shot node key: %v", err)
  121. }
  122. config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}}
  123. config.NodeKey()
  124. if _, err := os.Stat(filepath.Join(keyfile)); err == nil {
  125. t.Fatalf("one-shot node key persisted to data directory")
  126. }
  127. // Configure a node with no preset key and ensure it is persisted this time
  128. config = &Config{Name: "unit-test", DataDir: dir}
  129. config.NodeKey()
  130. if _, err := os.Stat(keyfile); err != nil {
  131. t.Fatalf("node key not persisted to data directory: %v", err)
  132. }
  133. if _, err = crypto.LoadECDSA(keyfile); err != nil {
  134. t.Fatalf("failed to load freshly persisted node key: %v", err)
  135. }
  136. blob1, err := ioutil.ReadFile(keyfile)
  137. if err != nil {
  138. t.Fatalf("failed to read freshly persisted node key: %v", err)
  139. }
  140. // Configure a new node and ensure the previously persisted key is loaded
  141. config = &Config{Name: "unit-test", DataDir: dir}
  142. config.NodeKey()
  143. blob2, err := ioutil.ReadFile(filepath.Join(keyfile))
  144. if err != nil {
  145. t.Fatalf("failed to read previously persisted node key: %v", err)
  146. }
  147. if !bytes.Equal(blob1, blob2) {
  148. t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1)
  149. }
  150. // Configure ephemeral node and ensure no key is dumped locally
  151. config = &Config{Name: "unit-test", DataDir: ""}
  152. config.NodeKey()
  153. if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil {
  154. t.Fatalf("ephemeral node key persisted to disk")
  155. }
  156. }
  157. func TestConfig_ResolvePluginBaseDir_whenPluginFeatureIsDisabled(t *testing.T) {
  158. testObject := &Config{}
  159. assert.NoError(t, testObject.ResolvePluginBaseDir())
  160. }
  161. func TestConfig_ResolvePluginBaseDir_whenBaseDirDoesNotExist(t *testing.T) {
  162. arbitraryBaseDir := path.Join(os.TempDir(), fmt.Sprintf("foo-%d", time.Now().Unix()))
  163. defer func() {
  164. _ = os.RemoveAll(arbitraryBaseDir)
  165. }()
  166. testObject := &Config{
  167. Plugins: &plugin.Settings{
  168. BaseDir: plugin.EnvironmentAwaredValue(arbitraryBaseDir),
  169. },
  170. }
  171. assert.NoError(t, testObject.ResolvePluginBaseDir())
  172. assert.True(t, common.FileExist(arbitraryBaseDir))
  173. assert.True(t, path.IsAbs(testObject.Plugins.BaseDir.String()))
  174. }
  175. func TestConfig_ResolvePluginBaseDir_whenBaseDirExists(t *testing.T) {
  176. arbitraryBaseDir, err := ioutil.TempDir("", "q-")
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. defer func() {
  181. _ = os.RemoveAll(arbitraryBaseDir)
  182. }()
  183. testObject := &Config{
  184. Plugins: &plugin.Settings{
  185. BaseDir: plugin.EnvironmentAwaredValue(arbitraryBaseDir),
  186. },
  187. }
  188. assert.NoError(t, testObject.ResolvePluginBaseDir())
  189. assert.True(t, path.IsAbs(testObject.Plugins.BaseDir.String()))
  190. }
  191. // Quorum
  192. //
  193. func TestConfig_IsPermissionEnabled_whenTypical(t *testing.T) {
  194. tmpdir, err := ioutil.TempDir("", "q-")
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. defer func() {
  199. _ = os.RemoveAll(tmpdir)
  200. }()
  201. if err := ioutil.WriteFile(path.Join(tmpdir, params.PERMISSION_MODEL_CONFIG), []byte("foo"), 0644); err != nil {
  202. t.Fatal(err)
  203. }
  204. testObject := &Config{
  205. EnableNodePermission: true,
  206. DataDir: tmpdir,
  207. }
  208. assert.True(t, testObject.IsPermissionEnabled())
  209. }
  210. // Quorum
  211. //
  212. func TestConfig_IsPermissionEnabled_whenPermissionedFlagIsFalse(t *testing.T) {
  213. testObject := &Config{
  214. EnableNodePermission: false,
  215. }
  216. assert.False(t, testObject.IsPermissionEnabled())
  217. }
  218. // Quorum
  219. //
  220. func TestConfig_IsPermissionEnabled_whenPermissionConfigIsNotAvailable(t *testing.T) {
  221. testObject := &Config{
  222. EnableNodePermission: true,
  223. DataDir: os.TempDir(),
  224. }
  225. assert.False(t, testObject.IsPermissionEnabled())
  226. }