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.

45 lines
1.0 KiB

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