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.

66 lines
1.6 KiB

  1. package files
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. cmn "github.com/tendermint/tmlibs/common"
  9. "github.com/tendermint/tendermint/lite"
  10. )
  11. func tmpFile() string {
  12. suffix := cmn.RandStr(16)
  13. return filepath.Join(os.TempDir(), "fc-test-"+suffix)
  14. }
  15. func TestSerializeFullCommits(t *testing.T) {
  16. assert, require := assert.New(t), require.New(t)
  17. // some constants
  18. appHash := []byte("some crazy thing")
  19. chainID := "ser-ial"
  20. h := int64(25)
  21. // build a fc
  22. keys := lite.GenValKeys(5)
  23. vals := keys.ToValidators(10, 0)
  24. fc := keys.GenFullCommit(chainID, h, nil, vals, appHash, 0, 5)
  25. require.Equal(h, fc.Height())
  26. require.Equal(vals.Hash(), fc.ValidatorsHash())
  27. // try read/write with json
  28. jfile := tmpFile()
  29. defer os.Remove(jfile)
  30. jseed, err := LoadFullCommitJSON(jfile)
  31. assert.NotNil(err)
  32. err = SaveFullCommitJSON(fc, jfile)
  33. require.Nil(err)
  34. jseed, err = LoadFullCommitJSON(jfile)
  35. assert.Nil(err, "%+v", err)
  36. assert.Equal(h, jseed.Height())
  37. assert.Equal(vals.Hash(), jseed.ValidatorsHash())
  38. // try read/write with binary
  39. bfile := tmpFile()
  40. defer os.Remove(bfile)
  41. bseed, err := LoadFullCommit(bfile)
  42. assert.NotNil(err)
  43. err = SaveFullCommit(fc, bfile)
  44. require.Nil(err)
  45. bseed, err = LoadFullCommit(bfile)
  46. assert.Nil(err, "%+v", err)
  47. assert.Equal(h, bseed.Height())
  48. assert.Equal(vals.Hash(), bseed.ValidatorsHash())
  49. // make sure they don't read the other format (different)
  50. _, err = LoadFullCommit(jfile)
  51. assert.NotNil(err)
  52. _, err = LoadFullCommitJSON(bfile)
  53. assert.NotNil(err)
  54. }