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.

118 lines
4.0 KiB

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