mirror of
https://codeberg.org/Kankys/doxa.git
synced 2026-11-11 06:44:36 +01:00
92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestCipherGenerationAndSymmetry(t *testing.T) {
|
|
// Create a temp directory for tests
|
|
tempDir, err := os.MkdirTemp("", "doxa_secret_test")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp dir: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
// Create a cipher mappings map manually or call a test helper
|
|
charsToMap := []string{"a", "b", "c", " ", ",", ".", "z", "\n"}
|
|
mappings := make(map[string]string)
|
|
used := make(map[string]bool)
|
|
|
|
for _, ch := range charsToMap {
|
|
code, err := generateRandomCode(used)
|
|
if err != nil {
|
|
t.Fatalf("Failed to generate random code: %v", err)
|
|
}
|
|
mappings[ch] = code
|
|
}
|
|
|
|
config := &CipherConfig{
|
|
Version: "1.0",
|
|
Mappings: mappings,
|
|
}
|
|
|
|
// Verify all code lengths
|
|
for ch, code := range mappings {
|
|
if len(code) != 5 {
|
|
t.Errorf("Code for '%s' is not 5 characters long: %s (len: %d)", ch, code, len(code))
|
|
}
|
|
}
|
|
|
|
// Test encryption & decryption symmetry
|
|
originalText := "a b, c. z\n"
|
|
encrypted := EncryptMessage(originalText, config)
|
|
|
|
// Since length of originalText is 10, and each character is mapped to 5 chars (except if unmapped, but all are mapped),
|
|
// the encrypted string should be exactly 10 * 5 = 50 characters long.
|
|
expectedLen := 10 * 5
|
|
if len([]rune(encrypted)) != expectedLen {
|
|
t.Errorf("Expected encrypted length %d, got %d", expectedLen, len([]rune(encrypted)))
|
|
}
|
|
|
|
decrypted := DecryptMessage(encrypted, config)
|
|
if decrypted != originalText {
|
|
t.Errorf("Symmetry test failed!\nOriginal: %q\nDecrypted: %q", originalText, decrypted)
|
|
}
|
|
}
|
|
|
|
func TestUnmappedCharacters(t *testing.T) {
|
|
mappings := map[string]string{
|
|
"a": "12345",
|
|
"b": "67890",
|
|
}
|
|
config := &CipherConfig{
|
|
Version: "1.0",
|
|
Mappings: mappings,
|
|
}
|
|
|
|
// 'c' is not mapped
|
|
original := "abc"
|
|
encrypted := EncryptMessage(original, config)
|
|
|
|
// 'a' -> "12345", 'b' -> "67890", 'c' -> "c"
|
|
expectedEncrypted := "1234567890c"
|
|
if encrypted != expectedEncrypted {
|
|
t.Errorf("Expected encrypted %q, got %q", expectedEncrypted, encrypted)
|
|
}
|
|
|
|
decrypted := DecryptMessage(encrypted, config)
|
|
if decrypted != original {
|
|
t.Errorf("Expected decrypted %q, got %q", original, decrypted)
|
|
}
|
|
}
|
|
|
|
func TestRemoveDiacritics(t *testing.T) {
|
|
input := "Příliš žluťoučký kůň úpěl ďábelské ódy."
|
|
expected := "Prilis zlutoucky kun upel dabelske ody."
|
|
output := RemoveDiacritics(input)
|
|
if output != expected {
|
|
t.Errorf("Expected %q, got %q", expected, output)
|
|
}
|
|
}
|