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