1
0
Fork 0
forked from jriou/coller
coller/src/internal/encryption_test.go
Julien Riou 8e1dd686d3
feat: Rename password by encryption key
Signed-off-by: Julien Riou <julien@riou.xyz>
2025-09-24 07:09:01 +02:00

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
}
}