Files
cmii-uav-watchdog-project/cmii-uav-watchdog-common/totp_tier_one/crypto_test.go
2025-12-06 11:26:05 +08:00

219 lines
5.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package totp_tier_one
import (
"cmii-uav-watchdog-common/models"
"fmt"
"testing"
)
// TestEncryptDecrypt 测试加密和解密功能
func TestEncryptDecrypt(t *testing.T) {
tests := []struct {
name string
plaintext string
key string
}{
{
name: "正常文本加密解密",
plaintext: "这是一段测试文本",
key: "测试密钥",
},
{
name: "空文本加密解密",
plaintext: "",
key: "测试密钥",
},
{
name: "特殊字符加密解密",
plaintext: "!@#$%^&*()_+{}|:<>?",
key: "!@#$%^&*()_+{}|:<>?",
},
{
name: "长文本加密解密",
plaintext: "这是一段非常长的测试文本,用于测试加密和解密功能是否能够正确处理长文本。这是一段非常长的测试文本,用于测试加密和解密功能是否能够正确处理长文本。",
key: "测试密钥",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// 加密
encrypted, err := Encrypt(tt.plaintext, tt.key)
if err != nil {
t.Fatalf("加密失败: %v", err)
}
// 确保加密后的文本与原始文本不同
if tt.plaintext != "" && encrypted == tt.plaintext {
t.Errorf("加密后的文本与原始文本相同,可能未正确加密")
}
// 解密
decrypted, err := Decrypt(encrypted, tt.key)
if err != nil {
t.Fatalf("解密失败: %v", err)
}
// 确保解密后的文本与原始文本相同
if decrypted != tt.plaintext {
t.Errorf("解密后的文本与原始文本不同,期望:%s实际%s", tt.plaintext, decrypted)
}
})
}
}
// TestDecryptWithWrongKey 测试使用错误密钥解密
func TestDecryptWithWrongKey(t *testing.T) {
plaintext := "这是一段测试文本"
correctKey := "正确密钥"
wrongKey := "错误密钥"
// 使用正确密钥加密
encrypted, err := Encrypt(plaintext, correctKey)
if err != nil {
t.Fatalf("加密失败: %v", err)
}
// 使用错误密钥解密,期望失败
_, err = Decrypt(encrypted, wrongKey)
fmt.Println("解密失败的 error :", err)
if err == nil {
t.Errorf("使用错误密钥解密应该失败,但成功了")
}
}
// TestDecryptInvalidData 测试解密无效的数据
func TestDecryptInvalidData(t *testing.T) {
invalidEncrypted := "这不是有效的加密数据"
key := "测试密钥"
_, err := Decrypt(invalidEncrypted, key)
if err == nil {
t.Errorf("解密无效数据应该失败,但成功了")
}
}
// createMockHostInfo 创建模拟的主机信息用于测试
func createMockHostInfo() models.HostInfo {
return models.HostInfo{
SystemInfo: models.SystemInfo{
MachineID: "test-machine-id",
KernelVersion: "5.10.0-test",
OS: models.OSInfo{
Name: "TestOS",
Version: "1.0",
ID: "test",
IDLike: "test",
VersionID: "1.0",
PrettyName: "Test OS 1.0",
},
},
CPUInfo: models.CPUInfo{
ModelName: "Test CPU",
Cores: 4,
Architecture: "x86_64",
Flags: []string{},
Hypervisor: "",
Virtualization: "",
Frequency: "",
},
DiskInfo: []models.DiskInfo{
{
Device: "/dev/sda1",
Filesystem: "ext4",
Type: "ext4",
Size: 1000000,
Used: 500000,
Available: 500000,
UsePercent: "50%",
MountPoint: "/",
PhysicalDevice: "/dev/sda",
PhysicalSize: 2000000,
},
},
MemoryInfo: models.MemoryInfo{
Total: 8000000,
Free: 4000000,
Available: 4000000,
Used: 4000000,
Buffers: 1000000,
Cached: 1000000,
Shared: 500000,
},
NetInfo: []models.NetworkInterfaceInfo{
{
Name: "eth0",
MACAddress: "00:11:22:33:44:55",
IPAddresses: []string{"192.168.1.100", "fe80::1234"},
},
},
MotherboardInfo: models.MotherboardInfo{
Manufacturer: "Test Manufacturer",
Product: "Test Product",
Version: "1.0",
Serial: "TEST123456",
},
}
}
// TestEncryptDecryptHostInfo 测试主机信息的加密和解密
func TestEncryptDecryptHostInfo(t *testing.T) {
hostInfo := createMockHostInfo()
key := "test-key"
// 加密主机信息
encrypted, err := EncryptHostInfo(hostInfo, key)
fmt.Println("加密后的主机信息:", encrypted)
if err != nil {
t.Fatalf("加密主机信息失败: %v", err)
}
// 确保加密后的文本不为空
if encrypted == "" {
t.Errorf("加密后的主机信息为空")
}
// 解密主机信息
decrypted, err := DecryptHostInfo(encrypted, hostInfo, key)
if err != nil {
t.Fatalf("解密主机信息失败: %v", err)
}
if !decrypted {
t.Errorf("解密后的主机信息与原始主机信息不同")
}
}
// TestEncryptDecryptHostInfoWithModifiedInfo 测试使用修改过的主机信息解密
func TestEncryptDecryptHostInfoWithModifiedInfo(t *testing.T) {
originalHostInfo := createMockHostInfo()
key := "test-key"
// 加密主机信息
encrypted, err := EncryptHostInfo(originalHostInfo, key)
if err != nil {
t.Fatalf("加密主机信息失败: %v", err)
}
t.Logf("加密后的主机信息: %s", encrypted)
// 创建修改过的主机信息
modifiedHostInfo := originalHostInfo
// 加密修改过的主机信息
encryptedModified, err := EncryptHostInfo(modifiedHostInfo, key)
if err != nil {
t.Fatalf("加密修改过的主机信息失败: %v", err)
}
t.Logf("加密修改后的主机信息: %s", encryptedModified)
// 使用修改过的主机信息尝试解密,期望失败
isOK, err := DecryptHostInfo(encryptedModified, originalHostInfo, key)
fmt.Println("解密后的主机信息 error :", err)
if isOK {
t.Errorf("解密后的主机信息与修改后的主机信息相同, 不应该相同")
}
}