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.

46 lines
1.1 KiB

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