44 lines
899 B
Go
44 lines
899 B
Go
package internal
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestEncryptAndDecrypt(t *testing.T) {
|
|
plaintext := "test"
|
|
password := "test"
|
|
wrongPassword := password + "wrong"
|
|
|
|
ciphertext, err := Encrypt([]byte(plaintext), password)
|
|
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, password)
|
|
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 password == wrongPassword {
|
|
t.Errorf("passwords must be different")
|
|
return
|
|
}
|
|
|
|
_, err = Decrypt(ciphertext, wrongPassword)
|
|
if err == nil {
|
|
t.Errorf("expected error when decrypting with a wrong password, got none")
|
|
return
|
|
}
|
|
}
|