mirror of https://gitee.com/godoos/godoos.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.6 KiB
66 lines
1.6 KiB
package libs
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestEncodeFile(t *testing.T) {
|
|
password := "testpassword"
|
|
longText := "This is a test message."
|
|
|
|
encryptedData, err := EncodeFile(password, longText)
|
|
if err != nil {
|
|
t.Fatalf("EncodeFile failed: %v", err)
|
|
}
|
|
|
|
if !strings.HasPrefix(encryptedData, "@") {
|
|
t.Errorf("Encoded data does not start with '@': %s", encryptedData)
|
|
}
|
|
|
|
parts := strings.SplitN(encryptedData[1:], "@", 2)
|
|
if len(parts) != 2 {
|
|
t.Errorf("Encoded data format is incorrect: %s", encryptedData)
|
|
}
|
|
if !IsEncryptedFile(encryptedData) {
|
|
t.Errorf("IsEncryptedFile returned false for valid encrypted data: %s", encryptedData)
|
|
}
|
|
}
|
|
|
|
func TestDecodeFile(t *testing.T) {
|
|
password := "testpassword"
|
|
longText := "This is a test message."
|
|
|
|
encryptedData, err := EncodeFile(password, longText)
|
|
if err != nil {
|
|
t.Fatalf("EncodeFile failed: %v", err)
|
|
}
|
|
|
|
decryptedText, err := DecodeFile(password, encryptedData)
|
|
if err != nil {
|
|
t.Fatalf("DecodeFile failed: %v", err)
|
|
}
|
|
|
|
if decryptedText != longText {
|
|
t.Errorf("Decrypted text does not match original text. Expected: %v, Got: %v", longText, decryptedText)
|
|
}
|
|
}
|
|
|
|
func TestIsEncryptedFile(t *testing.T) {
|
|
password := "password"
|
|
longText := "分地方大幅度"
|
|
|
|
encryptedData, err := EncodeFile(password, longText)
|
|
if err != nil {
|
|
t.Fatalf("EncodeFile failed: %v", err)
|
|
}
|
|
|
|
if !IsEncryptedFile(encryptedData) {
|
|
t.Errorf("IsEncryptedFile returned false for valid encrypted data: %s", encryptedData)
|
|
}
|
|
|
|
invalidData := "Invalid@Data"
|
|
if IsEncryptedFile(invalidData) {
|
|
t.Errorf("IsEncryptedFile returned true for invalid encrypted data: %s", invalidData)
|
|
}
|
|
}
|
|
|