-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencryption_test.go
More file actions
89 lines (74 loc) · 2.22 KB
/
encryption_test.go
File metadata and controls
89 lines (74 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package githosts
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncryptDecryptFile(t *testing.T) {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "encryption-test")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
// Create a test file
testFile := filepath.Join(tempDir, "test.bundle")
testContent := []byte("This is a test bundle content")
err = os.WriteFile(testFile, testContent, 0o644)
require.NoError(t, err)
// Test encryption
passphrase := "test-passphrase-123"
encryptedFile := testFile + ".age"
err = encryptFile(testFile, encryptedFile, passphrase)
assert.NoError(t, err)
// Verify encrypted file exists
_, err = os.Stat(encryptedFile)
assert.NoError(t, err)
// Test decryption
decryptedFile := filepath.Join(tempDir, "decrypted.bundle")
err = decryptFile(encryptedFile, decryptedFile, passphrase)
assert.NoError(t, err)
// Verify decrypted content matches original
decryptedContent, err := os.ReadFile(decryptedFile)
assert.NoError(t, err)
assert.Equal(t, testContent, decryptedContent)
// Test wrong passphrase
wrongPassphrase := "wrong-passphrase"
wrongDecryptedFile := filepath.Join(tempDir, "wrong-decrypted.bundle")
err = decryptFile(encryptedFile, wrongDecryptedFile, wrongPassphrase)
assert.Error(t, err)
}
func TestIsEncryptedBundle(t *testing.T) {
tests := []struct {
path string
expected bool
}{
{"repo.20240101000000.bundle", false},
{"repo.20240101000000.bundle.age", true},
{"repo.bundle", false},
{"repo.bundle.age", true},
{"not-a-bundle.txt", false},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
result := isEncryptedBundle(tt.path)
assert.Equal(t, tt.expected, result)
})
}
}
func TestGetOriginalBundleName(t *testing.T) {
tests := []struct {
encrypted string
expected string
}{
{"repo.20240101000000.bundle.age", "repo.20240101000000.bundle"},
{"repo.20240101000000.bundle", "repo.20240101000000.bundle"},
{"test.bundle.age", "test.bundle"},
}
for _, tt := range tests {
t.Run(tt.encrypted, func(t *testing.T) {
result := getOriginalBundleName(tt.encrypted)
assert.Equal(t, tt.expected, result)
})
}
}