summaryrefslogtreecommitdiff
path: root/network/p2p/peerID_test.go
blob: 9d7729d59386265c36fc3eed95cfaab3a50a1120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package p2p

import (
	"os"
	"path"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/test/partitiontest"
)

func TestGetPrivKeyUserSupplied(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()
	tempdir := t.TempDir()
	cfg := config.GetDefaultLocal()
	customPath := path.Join(tempdir, "foobar.pem")
	// generate a new private key
	privKey, err := generatePrivKey()
	require.NoError(t, err)
	// write it to our custom path
	err = writePrivateKeyToFile(customPath, privKey)
	require.NoError(t, err)
	cfg.P2PPrivateKeyLocation = customPath
	// make sure GetPrivKey loads our custom key
	loadedPrivKey, err := GetPrivKey(cfg, tempdir)
	assert.NoError(t, err)
	assert.Equal(t, privKey, loadedPrivKey)
}

func TestGetPrivKeyUserSuppliedDoesNotExistErrors(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()
	tempdir := t.TempDir()
	cfg := config.GetDefaultLocal()
	cfg.P2PPrivateKeyLocation = path.Join(tempdir, "foobar.pem")
	_, err := GetPrivKey(cfg, tempdir)
	assert.True(t, os.IsNotExist(err))
}

func TestGetPrivKeyDefault(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()
	tempdir := t.TempDir()
	cfg := config.GetDefaultLocal()

	// generate a new private key
	privKey, err := generatePrivKey()
	require.NoError(t, err)
	// write it to the default path
	err = writePrivateKeyToFile(path.Join(tempdir, DefaultPrivKeyPath), privKey)
	require.NoError(t, err)
	// fetch the default private key
	loadedPrivKey, err := GetPrivKey(cfg, tempdir)
	assert.NoError(t, err)
	assert.Equal(t, privKey, loadedPrivKey)
}

func TestGetPrivKeyUserGeneratedPersisted(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()
	tempdir := t.TempDir()
	cfg := config.GetDefaultLocal()
	cfg.P2PPersistPeerID = true
	// get a generated private key
	privKey, err := GetPrivKey(cfg, tempdir)
	require.NoError(t, err)
	// make sure it was persisted
	loadedPrivKey, err := loadPrivateKeyFromFile(path.Join(tempdir, DefaultPrivKeyPath))
	assert.NoError(t, err)
	assert.Equal(t, privKey, loadedPrivKey)
}

func TestGetPrivKeyUserGeneratedEphemeral(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()
	tempdir := t.TempDir()
	cfg := config.GetDefaultLocal()
	cfg.P2PPersistPeerID = false
	// get a generated private key
	_, err := GetPrivKey(cfg, tempdir)
	require.NoError(t, err)
	// make sure it was not persisted
	_, err = loadPrivateKeyFromFile(path.Join(tempdir, DefaultPrivKeyPath))
	assert.True(t, os.IsNotExist(err))
}