2023-09-19 18:08:40 +02:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateKey(t *testing.T) {
|
|
|
|
testData := "exampl@netbird.io"
|
|
|
|
key, err := GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate key: %s", err)
|
|
|
|
}
|
2023-09-23 10:47:49 +02:00
|
|
|
ee, err := NewFieldEncrypt(key)
|
2023-09-19 18:08:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to init email encryption: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-09-11 19:09:57 +02:00
|
|
|
encrypted, err := ee.Encrypt(testData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to encrypt data: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-09-19 18:08:40 +02:00
|
|
|
if encrypted == "" {
|
|
|
|
t.Fatalf("invalid encrypted text")
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypted, err := ee.Decrypt(encrypted)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to decrypt data: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if decrypted != testData {
|
|
|
|
t.Fatalf("decrypted data is not match with test data: %s, %s", testData, decrypted)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-11 19:09:57 +02:00
|
|
|
func TestGenerateKeyLegacy(t *testing.T) {
|
|
|
|
testData := "exampl@netbird.io"
|
|
|
|
key, err := GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate key: %s", err)
|
|
|
|
}
|
|
|
|
ee, err := NewFieldEncrypt(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to init email encryption: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
encrypted := ee.LegacyEncrypt(testData)
|
|
|
|
if encrypted == "" {
|
|
|
|
t.Fatalf("invalid encrypted text")
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypted, err := ee.LegacyDecrypt(encrypted)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to decrypt data: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if decrypted != testData {
|
|
|
|
t.Fatalf("decrypted data is not match with test data: %s, %s", testData, decrypted)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 18:08:40 +02:00
|
|
|
func TestCorruptKey(t *testing.T) {
|
|
|
|
testData := "exampl@netbird.io"
|
|
|
|
key, err := GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate key: %s", err)
|
|
|
|
}
|
2023-09-23 10:47:49 +02:00
|
|
|
ee, err := NewFieldEncrypt(key)
|
2023-09-19 18:08:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to init email encryption: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-09-11 19:09:57 +02:00
|
|
|
encrypted, err := ee.Encrypt(testData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to encrypt data: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-09-19 18:08:40 +02:00
|
|
|
if encrypted == "" {
|
|
|
|
t.Fatalf("invalid encrypted text")
|
|
|
|
}
|
|
|
|
|
|
|
|
newKey, err := GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate key: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-09-23 10:47:49 +02:00
|
|
|
ee, err = NewFieldEncrypt(newKey)
|
2023-09-19 18:08:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to init email encryption: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-09-23 10:47:49 +02:00
|
|
|
res, _ := ee.Decrypt(encrypted)
|
|
|
|
if res == testData {
|
2023-09-19 18:08:40 +02:00
|
|
|
t.Fatalf("incorrect decryption, the result is: %s", res)
|
|
|
|
}
|
|
|
|
}
|