mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-08 14:21:14 +00:00
closes: #5770 closes: #5769 also, include node ID in the output (#5769) and modify NodeKey to use value semantics (it makes perfect sense for NodeKey to not be a pointer).
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package p2p
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
tmrand "github.com/tendermint/tendermint/libs/rand"
|
|
)
|
|
|
|
func TestLoadOrGenNodeKey(t *testing.T) {
|
|
filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
|
|
|
|
nodeKey, err := LoadOrGenNodeKey(filePath)
|
|
assert.Nil(t, err)
|
|
|
|
nodeKey2, err := LoadOrGenNodeKey(filePath)
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, nodeKey, nodeKey2)
|
|
}
|
|
|
|
func TestLoadNodeKey(t *testing.T) {
|
|
filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
|
|
|
|
_, err := LoadNodeKey(filePath)
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
_, err = LoadOrGenNodeKey(filePath)
|
|
require.NoError(t, err)
|
|
|
|
nodeKey, err := LoadNodeKey(filePath)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, nodeKey)
|
|
}
|
|
|
|
func TestNodeKeySaveAs(t *testing.T) {
|
|
filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
|
|
|
|
assert.NoFileExists(t, filePath)
|
|
|
|
nodeKey := GenNodeKey()
|
|
err := nodeKey.SaveAs(filePath)
|
|
assert.NoError(t, err)
|
|
assert.FileExists(t, filePath)
|
|
}
|