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