iris/crypto/gcm/gcm_test.go
Gerasimos (Makis) Maropoulos 9dbb300d9b implement the Iris Crypto Library for Request Authentication and Verification. With Examples and Tests.
Relative to this one as well: https://github.com/kataras/iris/issues/1200


Former-commit-id: 3a29e7398b7fdeb9b48a118b742d419d5681d56b
2019-07-02 19:19:43 +03:00

47 lines
1.1 KiB
Go

package gcm
import (
"bytes"
"testing"
)
var testKey = MustGenerateKey()
func TestEncryptDecrypt(t *testing.T) {
if len(testKey) == 0 {
t.Fatalf("testKey is empty??")
}
tests := []struct {
payload []byte
aData []byte // IV of a random aes-256-cbc, 32 size.
}{
{[]byte("test my content 1"), []byte("FFA0A43EA6B8C829AD403817B2F5B7A2")},
{[]byte("test my content 2"), []byte("364787B9AF1AEE4BE26690EA8CBF4AB7")},
}
for i, tt := range tests {
ciphertext, err := Encrypt(testKey, tt.payload, tt.aData)
if err != nil {
t.Fatalf("[%d] encrypt error: %v", i, err)
}
payload, err := Decrypt(testKey, ciphertext, tt.aData)
if err != nil {
t.Fatalf("[%d] decrypt error: %v", i, err)
}
if !bytes.Equal(payload, tt.payload) {
t.Fatalf("[%d] expected data to be decrypted to: '%s' but got: '%s'", i, tt.payload, payload)
}
// test with other, invalid key, should fail to decrypt.
tempKey := MustGenerateKey()
payload, err = Decrypt(tempKey, ciphertext, tt.aData)
if err == nil || len(payload) > 0 {
t.Fatalf("[%d] verification should fail but passed for '%s'", i, tt.payload)
}
}
}