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.

106 lines
3.7 KiB

  1. package types
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "github.com/tendermint/tendermint/crypto/ed25519"
  10. )
  11. func TestGenesisBad(t *testing.T) {
  12. // test some bad ones from raw json
  13. testCases := [][]byte{
  14. []byte{}, // empty
  15. []byte{1, 1, 1, 1, 1}, // junk
  16. []byte(`{}`), // empty
  17. []byte(`{"chain_id":"mychain"}`), // missing validators
  18. []byte(`{"chain_id":"mychain","validators":[]}`), // missing validators
  19. []byte(`{"chain_id":"mychain","validators":[{}]}`), // missing validators
  20. []byte(`{"chain_id":"mychain","validators":null}`), // missing validators
  21. []byte(`{"chain_id":"mychain"}`), // missing validators
  22. []byte(`{"validators":[{"pub_key":{"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},"power":"10","name":""}]}`), // missing chain_id
  23. }
  24. for _, testCase := range testCases {
  25. _, err := GenesisDocFromJSON(testCase)
  26. assert.Error(t, err, "expected error for empty genDoc json")
  27. }
  28. }
  29. func TestGenesisGood(t *testing.T) {
  30. // test a good one by raw json
  31. genDocBytes := []byte(`{"genesis_time":"0001-01-01T00:00:00Z","chain_id":"test-chain-QDKdJr","consensus_params":null,"validators":[{"pub_key":{"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},"power":"10","name":""}],"app_hash":"","app_state":{"account_owner": "Bob"}}`)
  32. _, err := GenesisDocFromJSON(genDocBytes)
  33. assert.NoError(t, err, "expected no error for good genDoc json")
  34. // create a base gendoc from struct
  35. baseGenDoc := &GenesisDoc{
  36. ChainID: "abc",
  37. Validators: []GenesisValidator{{ed25519.GenPrivKey().PubKey(), 10, "myval"}},
  38. }
  39. genDocBytes, err = cdc.MarshalJSON(baseGenDoc)
  40. assert.NoError(t, err, "error marshalling genDoc")
  41. // test base gendoc and check consensus params were filled
  42. genDoc, err := GenesisDocFromJSON(genDocBytes)
  43. assert.NoError(t, err, "expected no error for valid genDoc json")
  44. assert.NotNil(t, genDoc.ConsensusParams, "expected consensus params to be filled in")
  45. // create json with consensus params filled
  46. genDocBytes, err = cdc.MarshalJSON(genDoc)
  47. assert.NoError(t, err, "error marshalling genDoc")
  48. genDoc, err = GenesisDocFromJSON(genDocBytes)
  49. assert.NoError(t, err, "expected no error for valid genDoc json")
  50. // test with invalid consensus params
  51. genDoc.ConsensusParams.BlockSize.MaxBytes = 0
  52. genDocBytes, err = cdc.MarshalJSON(genDoc)
  53. assert.NoError(t, err, "error marshalling genDoc")
  54. genDoc, err = GenesisDocFromJSON(genDocBytes)
  55. assert.Error(t, err, "expected error for genDoc json with block size of 0")
  56. }
  57. func TestGenesisSaveAs(t *testing.T) {
  58. tmpfile, err := ioutil.TempFile("", "genesis")
  59. require.NoError(t, err)
  60. defer os.Remove(tmpfile.Name())
  61. genDoc := randomGenesisDoc()
  62. // save
  63. genDoc.SaveAs(tmpfile.Name())
  64. stat, err := tmpfile.Stat()
  65. require.NoError(t, err)
  66. if err != nil && stat.Size() <= 0 {
  67. t.Fatalf("SaveAs failed to write any bytes to %v", tmpfile.Name())
  68. }
  69. err = tmpfile.Close()
  70. require.NoError(t, err)
  71. // load
  72. genDoc2, err := GenesisDocFromFile(tmpfile.Name())
  73. require.NoError(t, err)
  74. // fails to unknown reason
  75. // assert.EqualValues(t, genDoc2, genDoc)
  76. assert.Equal(t, genDoc2.Validators, genDoc.Validators)
  77. }
  78. func TestGenesisValidatorHash(t *testing.T) {
  79. genDoc := randomGenesisDoc()
  80. assert.NotEmpty(t, genDoc.ValidatorHash())
  81. }
  82. func randomGenesisDoc() *GenesisDoc {
  83. return &GenesisDoc{
  84. GenesisTime: time.Now().UTC(),
  85. ChainID: "abc",
  86. Validators: []GenesisValidator{{ed25519.GenPrivKey().PubKey(), 10, "myval"}},
  87. ConsensusParams: DefaultConsensusParams(),
  88. }
  89. }