177 lines
4.5 KiB
Go
177 lines
4.5 KiB
Go
package crypto
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestEncryptDecrypt(t *testing.T) {
|
|
// Генерируем тестовый ключ
|
|
key := make([]byte, 32)
|
|
for i := range key {
|
|
key[i] = byte(i)
|
|
}
|
|
|
|
encryptor, err := NewEncryptor(key)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create encryptor: %v", err)
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
plaintext string
|
|
}{
|
|
{"Empty string", ""},
|
|
{"Simple text", "Hello, World!"},
|
|
{"Russian text", "Привет, мир!"},
|
|
{"Long text", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."},
|
|
{"Special chars", "!@#$%^&*()_+-=[]{}|;:',.<>?/`~"},
|
|
{"Emoji", "Hello 👋 World 🌍"},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
// Шифруем
|
|
encrypted, err := encryptor.EncryptString(tc.plaintext)
|
|
if err != nil {
|
|
t.Fatalf("Encryption failed: %v", err)
|
|
}
|
|
|
|
// Проверяем, что зашифрованный текст не пустой (для непустого plaintext)
|
|
if tc.plaintext != "" && encrypted == "" {
|
|
t.Error("Encrypted text is empty for non-empty plaintext")
|
|
}
|
|
|
|
// Расшифровываем
|
|
decrypted, err := encryptor.DecryptString(encrypted)
|
|
if err != nil {
|
|
t.Fatalf("Decryption failed: %v", err)
|
|
}
|
|
|
|
// Проверяем соответствие
|
|
if decrypted != tc.plaintext {
|
|
t.Errorf("Decrypted text doesn't match original.\nExpected: %s\nGot: %s", tc.plaintext, decrypted)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEncryptDecryptBinary(t *testing.T) {
|
|
key := make([]byte, 32)
|
|
for i := range key {
|
|
key[i] = byte(i)
|
|
}
|
|
|
|
encryptor, err := NewEncryptor(key)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create encryptor: %v", err)
|
|
}
|
|
|
|
// Тестируем бинарные данные
|
|
binaryData := []byte{0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD}
|
|
|
|
encrypted, err := encryptor.Encrypt(binaryData)
|
|
if err != nil {
|
|
t.Fatalf("Encryption failed: %v", err)
|
|
}
|
|
|
|
decrypted, err := encryptor.Decrypt(encrypted)
|
|
if err != nil {
|
|
t.Fatalf("Decryption failed: %v", err)
|
|
}
|
|
|
|
if len(decrypted) != len(binaryData) {
|
|
t.Errorf("Length mismatch: expected %d, got %d", len(binaryData), len(decrypted))
|
|
}
|
|
|
|
for i := range binaryData {
|
|
if decrypted[i] != binaryData[i] {
|
|
t.Errorf("Byte mismatch at index %d: expected %d, got %d", i, binaryData[i], decrypted[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInvalidKey(t *testing.T) {
|
|
// Тестируем ключ неправильной длины
|
|
invalidKey := make([]byte, 16)
|
|
_, err := NewEncryptor(invalidKey)
|
|
if err == nil {
|
|
t.Error("Expected error for invalid key length, got nil")
|
|
}
|
|
}
|
|
|
|
func TestDecryptInvalidData(t *testing.T) {
|
|
key := make([]byte, 32)
|
|
encryptor, _ := NewEncryptor(key)
|
|
|
|
// Тестируем расшифровку некорректных данных
|
|
invalidData := "not-a-valid-base64"
|
|
_, err := encryptor.DecryptString(invalidData)
|
|
if err == nil {
|
|
t.Error("Expected error for invalid base64, got nil")
|
|
}
|
|
|
|
// Тестируем расшифровку слишком коротких данных
|
|
shortData := "aGVsbG8=" // "hello" в base64
|
|
_, err = encryptor.DecryptString(shortData)
|
|
if err == nil {
|
|
t.Error("Expected error for too short ciphertext, got nil")
|
|
}
|
|
}
|
|
|
|
func TestGenerateRandomKey(t *testing.T) {
|
|
key1, err := GenerateRandomKey()
|
|
if err != nil {
|
|
t.Fatalf("Failed to generate key: %v", err)
|
|
}
|
|
|
|
if len(key1) != 32 {
|
|
t.Errorf("Expected key length 32, got %d", len(key1))
|
|
}
|
|
|
|
key2, err := GenerateRandomKey()
|
|
if err != nil {
|
|
t.Fatalf("Failed to generate second key: %v", err)
|
|
}
|
|
|
|
// Проверяем, что ключи разные
|
|
same := true
|
|
for i := range key1 {
|
|
if key1[i] != key2[i] {
|
|
same = false
|
|
break
|
|
}
|
|
}
|
|
|
|
if same {
|
|
t.Error("Generated keys are identical")
|
|
}
|
|
}
|
|
|
|
func BenchmarkEncrypt(b *testing.B) {
|
|
key := make([]byte, 32)
|
|
encryptor, _ := NewEncryptor(key)
|
|
plaintext := "This is a test message that will be encrypted repeatedly"
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_, err := encryptor.EncryptString(plaintext)
|
|
if err != nil {
|
|
b.Fatalf("Encryption failed: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkDecrypt(b *testing.B) {
|
|
key := make([]byte, 32)
|
|
encryptor, _ := NewEncryptor(key)
|
|
plaintext := "This is a test message that will be encrypted repeatedly"
|
|
encrypted, _ := encryptor.EncryptString(plaintext)
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_, err := encryptor.DecryptString(encrypted)
|
|
if err != nil {
|
|
b.Fatalf("Decryption failed: %v", err)
|
|
}
|
|
}
|
|
} |