2019-05-14 16:08:25 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
"github.com/ProtonMail/gopenpgp/v2/constants"
|
2019-05-14 16:08:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
const signedPlainText = "Signed message\n"
|
2019-05-14 16:08:25 +00:00
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
var textSignature, binSignature *PGPSignature
|
|
|
|
|
var message *PlainMessage
|
|
|
|
|
var signatureTest = regexp.MustCompile("(?s)^-----BEGIN PGP SIGNATURE-----.*-----END PGP SIGNATURE-----$")
|
2019-05-14 16:08:25 +00:00
|
|
|
|
|
|
|
|
func TestSignTextDetached(t *testing.T) {
|
2019-06-03 17:00:01 +02:00
|
|
|
var err error
|
2019-05-14 16:08:25 +00:00
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
message = NewPlainMessageFromString(signedPlainText)
|
2019-12-27 19:35:43 +01:00
|
|
|
textSignature, err = keyRingTestPrivate.SignDetached(message)
|
2019-05-14 16:08:25 +00:00
|
|
|
if err != nil {
|
2019-06-03 17:00:01 +02:00
|
|
|
t.Fatal("Cannot generate signature:", err)
|
2019-05-14 16:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
armoredSignature, err := textSignature.GetArmored()
|
2019-05-14 16:08:25 +00:00
|
|
|
if err != nil {
|
2019-06-03 17:00:01 +02:00
|
|
|
t.Fatal("Cannot armor signature:", err)
|
2019-05-14 16:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
assert.Regexp(t, signatureTest, armoredSignature)
|
2019-05-14 16:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVerifyTextDetachedSig(t *testing.T) {
|
2019-12-27 19:35:43 +01:00
|
|
|
verificationError := keyRingTestPublic.VerifyDetached(message, textSignature, testTime)
|
2019-07-02 07:36:02 -07:00
|
|
|
if verificationError != nil {
|
2019-12-27 19:35:43 +01:00
|
|
|
t.Fatal("Cannot verify plaintext signature:", verificationError)
|
2019-05-14 16:08:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVerifyTextDetachedSigWrong(t *testing.T) {
|
2019-06-03 17:00:01 +02:00
|
|
|
fakeMessage := NewPlainMessageFromString("wrong text")
|
2019-12-27 19:35:43 +01:00
|
|
|
verificationError := keyRingTestPublic.VerifyDetached(fakeMessage, textSignature, testTime)
|
2019-07-02 07:36:02 -07:00
|
|
|
|
|
|
|
|
assert.EqualError(t, verificationError, "Signature Verification Error: Invalid signature")
|
2019-05-14 16:08:25 +00:00
|
|
|
|
2019-07-02 07:36:02 -07:00
|
|
|
err, _ := verificationError.(SignatureVerificationError)
|
|
|
|
|
assert.Exactly(t, constants.SIGNATURE_FAILED, err.Status)
|
2019-06-03 17:00:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSignBinDetached(t *testing.T) {
|
|
|
|
|
var err error
|
|
|
|
|
|
2020-10-12 21:24:33 +02:00
|
|
|
message = NewPlainMessage([]byte(signedPlainText))
|
|
|
|
|
binSignature, err = keyRingTestPrivate.SignDetached(message)
|
2019-06-03 17:00:01 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Cannot generate signature:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
armoredSignature, err := binSignature.GetArmored()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Cannot armor signature:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Regexp(t, signatureTest, armoredSignature)
|
2019-05-14 16:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVerifyBinDetachedSig(t *testing.T) {
|
2019-12-27 19:35:43 +01:00
|
|
|
verificationError := keyRingTestPublic.VerifyDetached(message, binSignature, testTime)
|
2019-07-02 07:36:02 -07:00
|
|
|
if verificationError != nil {
|
2019-12-27 19:35:43 +01:00
|
|
|
t.Fatal("Cannot verify binary signature:", verificationError)
|
2019-05-14 16:08:25 +00:00
|
|
|
}
|
|
|
|
|
}
|