github.com/nonsense/go-ethereum@v1.9.7/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/filepath"
    24  	"runtime"
    25  	"testing"
    26  
    27  	"github.com/ethereum/go-ethereum/crypto"
    28  	"github.com/ethereum/go-ethereum/p2p"
    29  )
    30  
    31  // Tests that datadirs can be successfully created, be them manually configured
    32  // ones or automatically generated temporary ones.
    33  func TestDatadirCreation(t *testing.T) {
    34  	// Create a temporary data dir and check that it can be used by a node
    35  	dir, err := ioutil.TempDir("", "")
    36  	if err != nil {
    37  		t.Fatalf("failed to create manual data dir: %v", err)
    38  	}
    39  	defer os.RemoveAll(dir)
    40  
    41  	node, err := New(&Config{DataDir: dir})
    42  	if err != nil {
    43  		t.Fatalf("failed to create stack with existing datadir: %v", err)
    44  	}
    45  	if err := node.Close(); err != nil {
    46  		t.Fatalf("failed to close node: %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  	node, err = New(&Config{DataDir: dir})
    51  	if err != nil {
    52  		t.Fatalf("failed to create stack with creatable datadir: %v", err)
    53  	}
    54  	if err := node.Close(); err != nil {
    55  		t.Fatalf("failed to close node: %v", err)
    56  	}
    57  	if _, err := os.Stat(dir); err != nil {
    58  		t.Fatalf("freshly created datadir not accessible: %v", err)
    59  	}
    60  	// Verify that an impossible datadir fails creation
    61  	file, err := ioutil.TempFile("", "")
    62  	if err != nil {
    63  		t.Fatalf("failed to create temporary file: %v", err)
    64  	}
    65  	defer os.Remove(file.Name())
    66  
    67  	dir = filepath.Join(file.Name(), "invalid/path")
    68  	node, err = New(&Config{DataDir: dir})
    69  	if err == nil {
    70  		t.Fatalf("protocol stack created with an invalid datadir")
    71  		if err := node.Close(); err != nil {
    72  			t.Fatalf("failed to close node: %v", err)
    73  		}
    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  
   108  // Tests that node keys can be correctly created, persisted, loaded and/or made
   109  // ephemeral.
   110  func TestNodeKeyPersistency(t *testing.T) {
   111  	// Create a temporary folder and make sure no key is present
   112  	dir, err := ioutil.TempDir("", "node-test")
   113  	if err != nil {
   114  		t.Fatalf("failed to create temporary data directory: %v", err)
   115  	}
   116  	defer os.RemoveAll(dir)
   117  
   118  	keyfile := filepath.Join(dir, "unit-test", datadirPrivateKey)
   119  
   120  	// Configure a node with a preset key and ensure it's not persisted
   121  	key, err := crypto.GenerateKey()
   122  	if err != nil {
   123  		t.Fatalf("failed to generate one-shot node key: %v", err)
   124  	}
   125  	config := &Config{Name: "unit-test", DataDir: dir, P2P: p2p.Config{PrivateKey: key}}
   126  	config.NodeKey()
   127  	if _, err := os.Stat(filepath.Join(keyfile)); err == nil {
   128  		t.Fatalf("one-shot node key persisted to data directory")
   129  	}
   130  
   131  	// Configure a node with no preset key and ensure it is persisted this time
   132  	config = &Config{Name: "unit-test", DataDir: dir}
   133  	config.NodeKey()
   134  	if _, err := os.Stat(keyfile); err != nil {
   135  		t.Fatalf("node key not persisted to data directory: %v", err)
   136  	}
   137  	if _, err = crypto.LoadECDSA(keyfile); err != nil {
   138  		t.Fatalf("failed to load freshly persisted node key: %v", err)
   139  	}
   140  	blob1, err := ioutil.ReadFile(keyfile)
   141  	if err != nil {
   142  		t.Fatalf("failed to read freshly persisted node key: %v", err)
   143  	}
   144  
   145  	// Configure a new node and ensure the previously persisted key is loaded
   146  	config = &Config{Name: "unit-test", DataDir: dir}
   147  	config.NodeKey()
   148  	blob2, err := ioutil.ReadFile(filepath.Join(keyfile))
   149  	if err != nil {
   150  		t.Fatalf("failed to read previously persisted node key: %v", err)
   151  	}
   152  	if !bytes.Equal(blob1, blob2) {
   153  		t.Fatalf("persisted node key mismatch: have %x, want %x", blob2, blob1)
   154  	}
   155  
   156  	// Configure ephemeral node and ensure no key is dumped locally
   157  	config = &Config{Name: "unit-test", DataDir: ""}
   158  	config.NodeKey()
   159  	if _, err := os.Stat(filepath.Join(".", "unit-test", datadirPrivateKey)); err == nil {
   160  		t.Fatalf("ephemeral node key persisted to disk")
   161  	}
   162  }