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.

70 lines
1.7 KiB

  1. package tx
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. crypto "github.com/tendermint/go-crypto"
  7. data "github.com/tendermint/go-wire/data"
  8. "github.com/tendermint/go-crypto/keys/cryptostore"
  9. "github.com/tendermint/go-crypto/keys/storage/memstorage"
  10. )
  11. func TestReader(t *testing.T) {
  12. assert, require := assert.New(t), require.New(t)
  13. algo := crypto.NameEd25519
  14. cstore := cryptostore.New(
  15. cryptostore.SecretBox,
  16. memstorage.New(),
  17. )
  18. type sigs struct{ name, pass string }
  19. u := sigs{"alice", "1234"}
  20. u2 := sigs{"bob", "foobar"}
  21. _, err := cstore.Create(u.name, u.pass, algo)
  22. require.Nil(err, "%+v", err)
  23. _, err = cstore.Create(u2.name, u2.pass, algo)
  24. require.Nil(err, "%+v", err)
  25. cases := []struct {
  26. tx Sig
  27. sigs []sigs
  28. }{
  29. {New([]byte("first")), nil},
  30. {New([]byte("second")), []sigs{u}},
  31. {New([]byte("other")), []sigs{u2}},
  32. {NewMulti([]byte("m-first")), nil},
  33. {NewMulti([]byte("m-second")), []sigs{u}},
  34. {NewMulti([]byte("m-other")), []sigs{u, u2}},
  35. }
  36. for _, tc := range cases {
  37. tx := tc.tx
  38. // make sure json serialization and loading works w/o sigs
  39. var pre Sig
  40. pjs, err := data.ToJSON(tx)
  41. require.Nil(err, "%+v", err)
  42. err = data.FromJSON(pjs, &pre)
  43. require.Nil(err, "%+v", err)
  44. assert.Equal(tx, pre)
  45. for _, s := range tc.sigs {
  46. err = cstore.Sign(s.name, s.pass, tx)
  47. require.Nil(err, "%+v", err)
  48. }
  49. var post Sig
  50. sjs, err := data.ToJSON(tx)
  51. require.Nil(err, "%+v", err)
  52. err = data.FromJSON(sjs, &post)
  53. require.Nil(err, "%+v\n%s", err, string(sjs))
  54. assert.Equal(tx, post)
  55. if len(tc.sigs) > 0 {
  56. assert.NotEqual(pjs, sjs, "%s\n ------ %s", string(pjs), string(sjs))
  57. }
  58. }
  59. }