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.6 KiB

9 years ago
9 years ago
9 years ago
  1. package crypto
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/tendermint/ed25519"
  6. . "github.com/tendermint/go-common"
  7. "github.com/tendermint/go-wire"
  8. )
  9. func TestSignAndValidate(t *testing.T) {
  10. privKey := GenPrivKeyEd25519()
  11. pubKey := privKey.PubKey()
  12. msg := CRandBytes(128)
  13. sig := privKey.Sign(msg)
  14. t.Logf("msg: %X, sig: %X", msg, sig)
  15. // Test the signature
  16. if !pubKey.VerifyBytes(msg, sig) {
  17. t.Errorf("Account message signature verification failed")
  18. }
  19. // Mutate the signature, just one bit.
  20. sigEd := sig.(SignatureEd25519)
  21. sigEd[0] ^= byte(0x01)
  22. sig = Signature(sigEd)
  23. if pubKey.VerifyBytes(msg, sig) {
  24. t.Errorf("Account message signature verification should have failed but passed instead")
  25. }
  26. }
  27. func TestBinaryDecode(t *testing.T) {
  28. privKey := GenPrivKeyEd25519()
  29. pubKey := privKey.PubKey()
  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(int), new(error)
  34. wire.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+1 {
  39. // 1 byte TypeByte, 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 := wire.ReadBinary(SignatureEd25519{}, buf, 0, 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. }