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.

71 lines
1.7 KiB

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
10 years ago
10 years ago
  1. package account
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/tendermint/ed25519"
  6. "github.com/tendermint/go-wire"
  7. . "github.com/tendermint/go-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. sigEd := sig.(SignatureEd25519)
  22. sigEd[0] ^= byte(0x01)
  23. sig = Signature(sigEd)
  24. if pubKey.VerifyBytes(msg, sig) {
  25. t.Errorf("Account message signature verification should have failed but passed instead")
  26. }
  27. }
  28. func TestBinaryDecode(t *testing.T) {
  29. privAccount := GenPrivAccount()
  30. pubKey := privAccount.PubKey
  31. privKey := privAccount.PrivKey
  32. msg := CRandBytes(128)
  33. sig := privKey.Sign(msg)
  34. t.Logf("msg: %X, sig: %X", msg, sig)
  35. buf, n, err := new(bytes.Buffer), new(int64), new(error)
  36. wire.WriteBinary(sig, buf, n, err)
  37. if *err != nil {
  38. t.Fatalf("Failed to write Signature: %v", err)
  39. }
  40. if len(buf.Bytes()) != ed25519.SignatureSize+1 {
  41. // 1 byte TypeByte, 64 bytes signature bytes
  42. t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
  43. }
  44. if buf.Bytes()[0] != SignatureTypeEd25519 {
  45. t.Fatalf("Unexpected signature type byte")
  46. }
  47. sig2, ok := wire.ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
  48. if !ok || *err != nil {
  49. t.Fatalf("Failed to read Signature: %v", err)
  50. }
  51. // Test the signature
  52. if !pubKey.VerifyBytes(msg, sig2) {
  53. t.Errorf("Account message signature verification failed")
  54. }
  55. }