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.

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