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.

61 lines
2.6 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/tendermint/tendermint/crypto"
  6. )
  7. func TestGenesisBad(t *testing.T) {
  8. // test some bad ones from raw json
  9. testCases := [][]byte{
  10. []byte{}, // empty
  11. []byte{1, 1, 1, 1, 1}, // junk
  12. []byte(`{}`), // empty
  13. []byte(`{"chain_id":"mychain"}`), // missing validators
  14. []byte(`{"chain_id":"mychain","validators":[]}`), // missing validators
  15. []byte(`{"chain_id":"mychain","validators":[{}]}`), // missing validators
  16. []byte(`{"chain_id":"mychain","validators":null}`), // missing validators
  17. []byte(`{"chain_id":"mychain"}`), // missing validators
  18. []byte(`{"validators":[{"pub_key":{"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},"power":"10","name":""}]}`), // missing chain_id
  19. }
  20. for _, testCase := range testCases {
  21. _, err := GenesisDocFromJSON(testCase)
  22. assert.Error(t, err, "expected error for empty genDoc json")
  23. }
  24. }
  25. func TestGenesisGood(t *testing.T) {
  26. // test a good one by raw json
  27. 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"}}`)
  28. _, err := GenesisDocFromJSON(genDocBytes)
  29. assert.NoError(t, err, "expected no error for good genDoc json")
  30. // create a base gendoc from struct
  31. baseGenDoc := &GenesisDoc{
  32. ChainID: "abc",
  33. Validators: []GenesisValidator{{crypto.GenPrivKeyEd25519().PubKey(), 10, "myval"}},
  34. }
  35. genDocBytes, err = cdc.MarshalJSON(baseGenDoc)
  36. assert.NoError(t, err, "error marshalling genDoc")
  37. // test base gendoc and check consensus params were filled
  38. genDoc, err := GenesisDocFromJSON(genDocBytes)
  39. assert.NoError(t, err, "expected no error for valid genDoc json")
  40. assert.NotNil(t, genDoc.ConsensusParams, "expected consensus params to be filled in")
  41. // create json with consensus params filled
  42. genDocBytes, err = cdc.MarshalJSON(genDoc)
  43. assert.NoError(t, err, "error marshalling genDoc")
  44. genDoc, err = GenesisDocFromJSON(genDocBytes)
  45. assert.NoError(t, err, "expected no error for valid genDoc json")
  46. // test with invalid consensus params
  47. genDoc.ConsensusParams.BlockSize.MaxBytes = 0
  48. genDocBytes, err = cdc.MarshalJSON(genDoc)
  49. assert.NoError(t, err, "error marshalling genDoc")
  50. genDoc, err = GenesisDocFromJSON(genDocBytes)
  51. assert.Error(t, err, "expected error for genDoc json with block size of 0")
  52. }