-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrypto_test.go
More file actions
104 lines (86 loc) 路 2.56 KB
/
Copy pathcrypto_test.go
File metadata and controls
104 lines (86 loc) 路 2.56 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gouse
import (
"bytes"
"os"
"testing"
)
func TestEncodeAndDecodeData(t *testing.T) {
originalData := []byte("This is test data!")
invalidData := []byte("Invalid base64 data!!!")
encodedData, err := EncodeData(originalData)
if err != nil {
t.Errorf("EncodeData returned an error: %v", err)
}
if bytes.Equal(encodedData, originalData) {
t.Errorf("Encoded data should not match the original data")
}
decodedData, err := DecodeData(encodedData)
if err != nil {
t.Errorf("DecodeData returned an error: %v", err)
}
if !bytes.Equal(decodedData, originalData) {
t.Errorf("Decoded data does not match the original data")
}
_, err = DecodeData(invalidData)
if err == nil {
t.Errorf("DecodeData should fail with invalid base64 data")
}
}
func TestEncryptAndDecryptFile(t *testing.T) {
// Test data
originalContent := []byte("This is the content of the file.")
testFile := "mockdata/crypto_test.txt"
password := []byte("strongpassword123")
err := os.WriteFile(testFile, originalContent, 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer os.Remove(testFile)
func() {
defer func() {
if r := recover(); r != nil {
t.Fatalf("EncryptFile panicked: %v", r)
}
}()
EncryptFile(testFile, password)
}()
encryptedContent, err := os.ReadFile(testFile)
if err != nil {
t.Fatalf("Failed to read encrypted file: %v", err)
}
if bytes.Equal(encryptedContent, originalContent) {
t.Error("File content should be encrypted, but it matches the original content")
}
func() {
defer func() {
if r := recover(); r != nil {
t.Fatalf("DecryptFile panicked: %v", r)
}
}()
DecryptFile(testFile, password)
}()
decryptedContent, err := os.ReadFile(testFile)
if err != nil {
t.Fatalf("Failed to read decrypted file: %v", err)
}
if !bytes.Equal(decryptedContent, originalContent) {
t.Error("Decrypted content does not match the original content")
}
}
func TestEncryptAndComparePassword(t *testing.T) {
originalPassword := "securePassword123"
wrongPassword := "wrongPassword456"
hashedPassword, err := EncryptPassword(originalPassword)
if err != nil {
t.Errorf("EncryptPassword returned an error: %v", err)
}
if hashedPassword == originalPassword {
t.Errorf("Hashed password should not match the original password")
}
if err := ComparePassword(hashedPassword, originalPassword); err != nil {
t.Errorf("ComparePassword failed with the correct password: %v", err)
}
if err := ComparePassword(hashedPassword, wrongPassword); err == nil {
t.Errorf("ComparePassword should fail with the wrong password")
}
}