github.com/hyperion-hyn/go-ethereum@v2.4.0+incompatible/node/config_test.go (about)

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