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.

62 lines
2.5 KiB

  1. package types
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. crypto "github.com/tendermint/go-crypto"
  7. )
  8. func TestGenesis(t *testing.T) {
  9. // test some bad ones from raw json
  10. testCases := [][]byte{
  11. []byte{}, // empty
  12. []byte{1, 1, 1, 1, 1}, // junk
  13. []byte(`{}`), // empty
  14. []byte(`{"chain_id": "mychain"}`), // missing validators
  15. []byte(`{"chain_id": "mychain", "validators": []`), // missing validators
  16. []byte(`{"chain_id": "mychain", "validators": [{}]`), // missing validators
  17. []byte(`{"validators":[{"pub_key":
  18. {"type":"ed25519","data":"961EAB8752E51A03618502F55C2B6E09C38C65635C64CCF3173ED452CF86C957"},
  19. "power":10,"name":""}]}`), // missing chain_id
  20. }
  21. for _, testCase := range testCases {
  22. _, err := GenesisDocFromJSON(testCase)
  23. assert.Error(t, err, "expected error for empty genDoc json")
  24. }
  25. // test a good one by raw json
  26. genDocBytes := []byte(`{"genesis_time":"0001-01-01T00:00:00Z","chain_id":"test-chain-QDKdJr","consensus_params":null,"validators":[{"pub_key":{"type":"ed25519","data":"961EAB8752E51A03618502F55C2B6E09C38C65635C64CCF3173ED452CF86C957"},"power":10,"name":""}],"app_hash":"","app_options":{"account_owner": "Bob"}}`)
  27. _, err := GenesisDocFromJSON(genDocBytes)
  28. assert.NoError(t, err, "expected no error for good genDoc json")
  29. // create a base gendoc from struct
  30. baseGenDoc := &GenesisDoc{
  31. ChainID: "abc",
  32. Validators: []GenesisValidator{{crypto.GenPrivKeyEd25519().PubKey(), 10, "myval"}},
  33. }
  34. genDocBytes, err = json.Marshal(baseGenDoc)
  35. assert.NoError(t, err, "error marshalling genDoc")
  36. // test base gendoc and check consensus params were filled
  37. genDoc, err := GenesisDocFromJSON(genDocBytes)
  38. assert.NoError(t, err, "expected no error for valid genDoc json")
  39. assert.NotNil(t, genDoc.ConsensusParams, "expected consensus params to be filled in")
  40. // create json with consensus params filled
  41. genDocBytes, err = json.Marshal(genDoc)
  42. assert.NoError(t, err, "error marshalling genDoc")
  43. genDoc, err = GenesisDocFromJSON(genDocBytes)
  44. assert.NoError(t, err, "expected no error for valid genDoc json")
  45. // test with invalid consensus params
  46. genDoc.ConsensusParams.BlockSizeParams.MaxBytes = 0
  47. genDocBytes, err = json.Marshal(genDoc)
  48. assert.NoError(t, err, "error marshalling genDoc")
  49. genDoc, err = GenesisDocFromJSON(genDocBytes)
  50. assert.Error(t, err, "expected error for genDoc json with block size of 0")
  51. }