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.

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