attachments_test.go raw
1 package bridge
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 func TestEncryptDecryptAttachment(t *testing.T) {
9 original := []byte("This is a test attachment with some content.")
10
11 ciphertext, keyHex, err := EncryptAttachment(original)
12 if err != nil {
13 t.Fatalf("EncryptAttachment: %v", err)
14 }
15
16 if len(keyHex) != 64 { // 32 bytes = 64 hex chars
17 t.Errorf("key hex length = %d, want 64", len(keyHex))
18 }
19
20 if bytes.Equal(ciphertext, original) {
21 t.Error("ciphertext should differ from plaintext")
22 }
23
24 // Decrypt
25 decrypted, err := DecryptAttachment(ciphertext, keyHex)
26 if err != nil {
27 t.Fatalf("DecryptAttachment: %v", err)
28 }
29
30 if !bytes.Equal(decrypted, original) {
31 t.Error("decrypted content doesn't match original")
32 }
33 }
34
35 func TestEncryptDecryptAttachment_LargeData(t *testing.T) {
36 // 1MB of test data
37 original := make([]byte, 1024*1024)
38 for i := range original {
39 original[i] = byte(i % 256)
40 }
41
42 ciphertext, keyHex, err := EncryptAttachment(original)
43 if err != nil {
44 t.Fatalf("EncryptAttachment: %v", err)
45 }
46
47 decrypted, err := DecryptAttachment(ciphertext, keyHex)
48 if err != nil {
49 t.Fatalf("DecryptAttachment: %v", err)
50 }
51
52 if !bytes.Equal(decrypted, original) {
53 t.Error("large data round-trip failed")
54 }
55 }
56
57 func TestEncryptDecryptAttachment_EmptyData(t *testing.T) {
58 original := []byte{}
59
60 ciphertext, keyHex, err := EncryptAttachment(original)
61 if err != nil {
62 t.Fatalf("EncryptAttachment: %v", err)
63 }
64
65 decrypted, err := DecryptAttachment(ciphertext, keyHex)
66 if err != nil {
67 t.Fatalf("DecryptAttachment: %v", err)
68 }
69
70 if !bytes.Equal(decrypted, original) {
71 t.Error("empty data round-trip failed")
72 }
73 }
74
75 func TestDecryptAttachment_WrongKey(t *testing.T) {
76 original := []byte("secret data")
77
78 ciphertext, _, err := EncryptAttachment(original)
79 if err != nil {
80 t.Fatalf("EncryptAttachment: %v", err)
81 }
82
83 // Use a different key
84 wrongKey := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
85 _, err = DecryptAttachment(ciphertext, wrongKey)
86 if err == nil {
87 t.Error("expected error when decrypting with wrong key")
88 }
89 }
90
91 func TestDecryptAttachment_InvalidKeyHex(t *testing.T) {
92 _, err := DecryptAttachment([]byte("data"), "not-hex")
93 if err == nil {
94 t.Error("expected error for invalid hex key")
95 }
96 }
97
98 func TestDecryptAttachment_ShortCiphertext(t *testing.T) {
99 // Ciphertext shorter than nonce
100 _, err := DecryptAttachment([]byte("short"), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
101 if err == nil {
102 t.Error("expected error for short ciphertext")
103 }
104 }
105
106 func TestEncryptAttachment_DifferentKeys(t *testing.T) {
107 original := []byte("same data")
108
109 _, key1, _ := EncryptAttachment(original)
110 _, key2, _ := EncryptAttachment(original)
111
112 if key1 == key2 {
113 t.Error("encrypting same data twice should produce different keys")
114 }
115 }
116
117 func TestDecryptAttachment_WrongKeyLength(t *testing.T) {
118 // Valid hex but wrong length (16 bytes instead of 32)
119 _, err := DecryptAttachment([]byte("some data here that is long enough"), "aabbccddaabbccddaabbccddaabbccdd")
120 if err == nil {
121 t.Error("expected error for wrong key length")
122 }
123 }
124