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.

68 lines
1.7 KiB

10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package account
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519"
  6. "github.com/tendermint/tendermint/binary"
  7. . "github.com/tendermint/tendermint/common"
  8. )
  9. func TestSignAndValidate(t *testing.T) {
  10. privAccount := GenPrivAccount()
  11. pubKey := privAccount.PubKey
  12. privKey := privAccount.PrivKey
  13. msg := CRandBytes(128)
  14. sig := privKey.Sign(msg)
  15. t.Logf("msg: %X, sig: %X", msg, sig)
  16. // Test the signature
  17. if !pubKey.VerifyBytes(msg, sig) {
  18. t.Errorf("Account message signature verification failed")
  19. }
  20. // Mutate the signature, just one bit.
  21. sig.(SignatureEd25519)[0] ^= byte(0x01)
  22. if pubKey.VerifyBytes(msg, sig) {
  23. t.Errorf("Account message signature verification should have failed but passed instead")
  24. }
  25. }
  26. func TestBinaryDecode(t *testing.T) {
  27. privAccount := GenPrivAccount()
  28. pubKey := privAccount.PubKey
  29. privKey := privAccount.PrivKey
  30. msg := CRandBytes(128)
  31. sig := privKey.Sign(msg)
  32. t.Logf("msg: %X, sig: %X", msg, sig)
  33. buf, n, err := new(bytes.Buffer), new(int64), new(error)
  34. binary.WriteBinary(sig, buf, n, err)
  35. if *err != nil {
  36. t.Fatalf("Failed to write Signature: %v", err)
  37. }
  38. if len(buf.Bytes()) != ed25519.SignatureSize+3 {
  39. // 1 byte TypeByte, 2 bytes length, 64 bytes signature bytes
  40. t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
  41. }
  42. if buf.Bytes()[0] != SignatureTypeEd25519 {
  43. t.Fatalf("Unexpected signature type byte")
  44. }
  45. sig2, ok := binary.ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
  46. if !ok || *err != nil {
  47. t.Fatalf("Failed to read Signature: %v", err)
  48. }
  49. // Test the signature
  50. if !pubKey.VerifyBytes(msg, sig2) {
  51. t.Errorf("Account message signature verification failed")
  52. }
  53. }