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.

65 lines
2.6 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. crypto "github.com/tendermint/go-crypto"
  6. wire "github.com/tendermint/tendermint/wire"
  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. /* TODO WIRE enable json ...
  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":"ed25519","data":"961EAB8752E51A03618502F55C2B6E09C38C65635C64CCF3173ED452CF86C957"},"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. */
  31. var genDocBytes []byte
  32. var err error
  33. // create a base gendoc from struct
  34. baseGenDoc := &GenesisDoc{
  35. ChainID: "abc",
  36. Validators: []GenesisValidator{{crypto.GenPrivKeyEd25519().PubKey(), 10, "myval"}},
  37. }
  38. genDocBytes, err = wire.MarshalJSON(baseGenDoc)
  39. assert.NoError(t, err, "error marshalling genDoc")
  40. // test base gendoc and check consensus params were filled
  41. genDoc, err := GenesisDocFromJSON(genDocBytes)
  42. assert.NoError(t, err, "expected no error for valid genDoc json")
  43. assert.NotNil(t, genDoc.ConsensusParams, "expected consensus params to be filled in")
  44. // create json with consensus params filled
  45. genDocBytes, err = wire.MarshalJSON(genDoc)
  46. assert.NoError(t, err, "error marshalling genDoc")
  47. genDoc, err = GenesisDocFromJSON(genDocBytes)
  48. assert.NoError(t, err, "expected no error for valid genDoc json")
  49. // test with invalid consensus params
  50. genDoc.ConsensusParams.BlockSize.MaxBytes = 0
  51. genDocBytes, err = wire.MarshalJSON(genDoc)
  52. assert.NoError(t, err, "error marshalling genDoc")
  53. genDoc, err = GenesisDocFromJSON(genDocBytes)
  54. assert.Error(t, err, "expected error for genDoc json with block size of 0")
  55. }