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.

49 lines
1.1 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. tmrand "github.com/tendermint/tendermint/libs/rand"
  9. )
  10. func TestLoadOrGenNodeKey(t *testing.T) {
  11. filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
  12. nodeKey, err := LoadOrGenNodeKey(filePath)
  13. assert.Nil(t, err)
  14. nodeKey2, err := LoadOrGenNodeKey(filePath)
  15. assert.Nil(t, err)
  16. assert.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 := LoadNodeKey(filePath)
  21. assert.True(t, os.IsNotExist(err))
  22. _, err = LoadOrGenNodeKey(filePath)
  23. require.NoError(t, err)
  24. nodeKey, err := LoadNodeKey(filePath)
  25. assert.NoError(t, err)
  26. assert.NotNil(t, nodeKey)
  27. }
  28. func TestNodeKeySaveAs(t *testing.T) {
  29. filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
  30. assert.NoFileExists(t, filePath)
  31. nodeKey := GenNodeKey()
  32. err := nodeKey.SaveAs(filePath)
  33. assert.NoError(t, err)
  34. assert.FileExists(t, filePath)
  35. }