github.com/core-coin/go-core/v2@v2.1.9/node/config_test.go (about)

     1  // Copyright 2015 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package node
    18  
    19  import (
    20  	"bytes"
    21  	crand "crypto/rand"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"runtime"
    26  	"testing"
    27  
    28  	"github.com/core-coin/go-core/v2/crypto"
    29  	"github.com/core-coin/go-core/v2/p2p"
    30  )
    31  
    32  // Tests that datadirs can be successfully created, be them manually configured
    33  // ones or automatically generated temporary ones.
    34  func TestDatadirCreation(t *testing.T) {
    35  	// Create a temporary data dir and check that it can be used by a node
    36  	dir, err := ioutil.TempDir("", "")
    37  	if err != nil {
    38  		t.Fatalf("failed to create manual data dir: %v", err)
    39  	}
    40  	defer os.RemoveAll(dir)
    41  
    42  	node, err := New(&Config{DataDir: dir})
    43  	if err != nil {
    44  		t.Fatalf("failed to create stack with existing datadir: %v", err)
    45  	}
    46  	if err := node.Close(); err != nil {
    47  		t.Fatalf("failed to close node: %v", err)
    48  	}
    49  	// Generate a long non-existing datadir path and check that it gets created by a node
    50  	dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f")
    51  	node, err = New(&Config{DataDir: dir})
    52  	if err != nil {
    53  		t.Fatalf("failed to create stack with creatable datadir: %v", err)
    54  	}
    55  	if err := node.Close(); err != nil {
    56  		t.Fatalf("failed to close node: %v", err)
    57  	}
    58  	if _, err := os.Stat(dir); err != nil {
    59  		t.Fatalf("freshly created datadir not accessible: %v", err)
    60  	}
    61  	// Verify that an impossible datadir fails creation
    62  	file, err := ioutil.TempFile("", "")
    63  	if err != nil {
    64  		t.Fatalf("failed to create temporary file: %v", err)
    65  	}
    66  	defer os.Remove(file.Name())
    67  
    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  
    78  // Tests that IPC paths are correctly resolved to valid endpoints of different
    79  // platforms.
    80  func TestIPCPathResolution(t *testing.T) {
    81  	var tests = []struct {
    82  		DataDir  string
    83  		IPCPath  string
    84  		Windows  bool
    85  		Endpoint string
    86  	}{
    87  		{"", "", false, ""},
    88  		{"data", "", false, ""},
    89  		{"", "gocore.ipc", false, filepath.Join(os.TempDir(), "gocore.ipc")},
    90  		{"data", "gocore.ipc", false, "data/gocore.ipc"},
    91  		{"data", "./gocore.ipc", false, "./gocore.ipc"},
    92  		{"data", "/gocore.ipc", false, "/gocore.ipc"},
    93  		{"", "", true, ``},
    94  		{"data", "", true, ``},
    95  		{"", "gocore.ipc", true, `\\.\pipe\gocore.ipc`},
    96  		{"data", "gocore.ipc", true, `\\.\pipe\gocore.ipc`},
    97  		{"data", `\\.\pipe\gocore.ipc`, true, `\\.\pipe\gocore.ipc`},
    98  	}
    99  	for i, test := range tests {
   100  		// Only run when platform/test match
   101  		if (runtime.GOOS == "windows") == test.Windows {
   102  			if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint {
   103  				t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
   104  			}
   105  		}
   106  	}
   107  }
   108  
   109  // Tests that node keys can be correctly created, persisted, loaded and/or made
   110  // ephemeral.
   111  func TestNodeKeyPersistency(t *testing.T) {
   112  	// Create a temporary folder and make sure no key is present
   113  	dir, err := ioutil.TempDir("", "node-test")
   114  	if err != nil {
   115  		t.Fatalf("failed to create temporary data directory: %v", err)
   116  	}
   117  	defer os.RemoveAll(dir)
   118  
   119  	keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)
   120  
   121  	// Configure a node with a preset key and ensure it's not persisted
   122  	key, err := crypto.GenerateKey(crand.Reader)
   123  	if err != nil {
   124  		t.Fatalf("failed to generate one-shot node key: %v", err)
   125  	}
   126  	config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}}
   127  	config.NodeKey()
   128  	if _, err := os.Stat(filepath.Join(keyfile)); err == nil {
   129  		t.Fatalf("one-shot node key persisted to data directory")
   130  	}
   131  
   132  	// Configure a node with no preset key and ensure it is persisted this time
   133  	config = &Config{Name: "unit-test", DataDir: dir}
   134  	config.NodeKey()
   135  	if _, err := os.Stat(keyfile); err != nil {
   136  		t.Fatalf("node key not persisted to data directory: %v", err)
   137  	}
   138  	if _, err = crypto.LoadEDDSA(keyfile); err != nil {
   139  		t.Fatalf("failed to load freshly persisted node key: %v", err)
   140  	}
   141  	blob1, err := ioutil.ReadFile(keyfile)
   142  	if err != nil {
   143  		t.Fatalf("failed to read freshly persisted node key: %v", err)
   144  	}
   145  
   146  	// Configure a new node and ensure the previously persisted key is loaded
   147  	config = &Config{Name: "unit-test", DataDir: dir}
   148  	config.NodeKey()
   149  	blob2, err := ioutil.ReadFile(filepath.Join(keyfile))
   150  	if err != nil {
   151  		t.Fatalf("failed to read previously persisted node key: %v", err)
   152  	}
   153  	if !bytes.Equal(blob1, blob2) {
   154  		t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1)
   155  	}
   156  
   157  	// Configure ephemeral node and ensure no key is dumped locally
   158  	config = &Config{Name: "unit-test", DataDir: ""}
   159  	config.NodeKey()
   160  	if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil {
   161  		t.Fatalf("ephemeral node key persisted to disk")
   162  	}
   163  }