You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2 KiB

  1. package p2p
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/crypto/ed25519"
  9. tmrand "github.com/tendermint/tendermint/libs/rand"
  10. )
  11. func TestLoadOrGenNodeKey(t *testing.T) {
  12. filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
  13. nodeKey, err := LoadOrGenNodeKey(filePath)
  14. assert.Nil(t, err)
  15. nodeKey2, err := LoadOrGenNodeKey(filePath)
  16. assert.Nil(t, err)
  17. assert.Equal(t, nodeKey, nodeKey2)
  18. }
  19. func TestLoadNodeKey(t *testing.T) {
  20. filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
  21. _, err := LoadNodeKey(filePath)
  22. assert.True(t, os.IsNotExist(err))
  23. _, err = LoadOrGenNodeKey(filePath)
  24. require.NoError(t, err)
  25. nodeKey, err := LoadNodeKey(filePath)
  26. assert.NoError(t, err)
  27. assert.NotNil(t, nodeKey)
  28. }
  29. func TestNodeKeySaveAs(t *testing.T) {
  30. filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
  31. assert.NoFileExists(t, filePath)
  32. privKey := ed25519.GenPrivKey()
  33. nodeKey := &NodeKey{
  34. PrivKey: privKey,
  35. }
  36. err := nodeKey.SaveAs(filePath)
  37. assert.NoError(t, err)
  38. assert.FileExists(t, filePath)
  39. }