forked from jriou/coller
44 lines
951 B
Go
44 lines
951 B
Go
package internal
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestEncryptAndDecrypt(t *testing.T) {
|
|
plaintext := "test"
|
|
encryptionKey := "test"
|
|
wrongEncryptionKey := encryptionKey + "wrong"
|
|
|
|
ciphertext, err := Encrypt([]byte(plaintext), encryptionKey)
|
|
if err != nil {
|
|
t.Errorf("unexpected error when encrypting: %v", err)
|
|
return
|
|
}
|
|
|
|
if plaintext == string(ciphertext) {
|
|
t.Errorf("plaintext and ciphertext are equal")
|
|
return
|
|
}
|
|
|
|
cleartext, err := Decrypt(ciphertext, encryptionKey)
|
|
if err != nil {
|
|
t.Errorf("unexpected error when decrypting: %v", err)
|
|
return
|
|
}
|
|
|
|
if string(cleartext) != string(plaintext) {
|
|
t.Errorf("got '%s', expected '%s'", cleartext, plaintext)
|
|
return
|
|
}
|
|
|
|
if encryptionKey == wrongEncryptionKey {
|
|
t.Errorf("encryption keys must be different")
|
|
return
|
|
}
|
|
|
|
_, err = Decrypt(ciphertext, wrongEncryptionKey)
|
|
if err == nil {
|
|
t.Errorf("expected error when decrypting with a wrong encryption key, got none")
|
|
return
|
|
}
|
|
}
|