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.

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