214 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			214 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
|  | package crypto | |||
|  | 
 | |||
|  | import ( | |||
|  | 	"testing" | |||
|  | ) | |||
|  | 
 | |||
|  | func TestWestDexEncryptDecrypt(t *testing.T) { | |||
|  | 	testCases := []struct { | |||
|  | 		name      string | |||
|  | 		data      string | |||
|  | 		secretKey string | |||
|  | 	}{ | |||
|  | 		{ | |||
|  | 			name:      "简单文本", | |||
|  | 			data:      "hello world", | |||
|  | 			secretKey: "mySecretKey123", | |||
|  | 		}, | |||
|  | 		{ | |||
|  | 			name:      "中文文本", | |||
|  | 			data:      "你好世界", | |||
|  | 			secretKey: "中文密钥", | |||
|  | 		}, | |||
|  | 		{ | |||
|  | 			name:      "JSON数据", | |||
|  | 			data:      `{"name":"张三","age":30,"city":"北京"}`, | |||
|  | 			secretKey: "jsonSecretKey", | |||
|  | 		}, | |||
|  | 		{ | |||
|  | 			name:      "长文本", | |||
|  | 			data:      "这是一个很长的文本,用来测试加密解密功能是否正常工作。包含各种字符:123456789!@#$%^&*()_+-=[]{}|;':\",./<>?", | |||
|  | 			secretKey: "longTextKey", | |||
|  | 		}, | |||
|  | 		{ | |||
|  | 			name:      "空字符串", | |||
|  | 			data:      "", | |||
|  | 			secretKey: "emptyDataKey", | |||
|  | 		}, | |||
|  | 	} | |||
|  | 
 | |||
|  | 	for _, tc := range testCases { | |||
|  | 		t.Run(tc.name, func(t *testing.T) { | |||
|  | 			// 加密 | |||
|  | 			encrypted, err := WestDexEncrypt(tc.data, tc.secretKey) | |||
|  | 			if err != nil { | |||
|  | 				t.Fatalf("加密失败: %v", err) | |||
|  | 			} | |||
|  | 
 | |||
|  | 			t.Logf("原始数据: %s", tc.data) | |||
|  | 			t.Logf("密钥: %s", tc.secretKey) | |||
|  | 			t.Logf("加密结果: %s", encrypted) | |||
|  | 
 | |||
|  | 			// 解密 | |||
|  | 			decrypted, err := WestDexDecrypt(encrypted, tc.secretKey) | |||
|  | 			if err != nil { | |||
|  | 				t.Fatalf("解密失败: %v", err) | |||
|  | 			} | |||
|  | 
 | |||
|  | 			decryptedStr := string(decrypted) | |||
|  | 			t.Logf("解密结果: %s", decryptedStr) | |||
|  | 
 | |||
|  | 			// 验证解密结果是否与原始数据一致 | |||
|  | 			if decryptedStr != tc.data { | |||
|  | 				t.Errorf("解密结果不匹配: 期望 %s, 实际 %s", tc.data, decryptedStr) | |||
|  | 			} | |||
|  | 		}) | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | func TestWestDexDecryptOutput(t *testing.T) { | |||
|  | 	// 专门用来查看解密结果的测试 | |||
|  | 	testData := []struct { | |||
|  | 		name        string | |||
|  | 		data        string | |||
|  | 		secretKey   string | |||
|  | 		encryptedData string // 预设的加密数据 | |||
|  | 	}{ | |||
|  | 		{ | |||
|  | 			name:      "测试数据1", | |||
|  | 			data:      "DLrbtEki5o/5yTvQWR+dWWUZYEo5s58D8LTnhhlAl99SwZbECa34KpStmR+Qr0gbbKzh3y4t5+/vbFFZgv03DtnYlLQcQt+rSgtxkCN/PCBPaFE0QZRTufd7djJfUww0Eh6DMHD7NS9pcuCa0PHGVoE+Vwo2YSwOnh2gtx3Bt0Qhs+w76tfCwIeufZ8tcpFs/nb84HIZxk+0cH1bTfNE6VsXI6vMpKvnS02O3oE2642ozeHgglCNuiOFMcCL8Erw4FKPnfRCUYdeKc2dZ7OF2IZqt0t4WiJBxjB/6k4tgAj/HepE2gaulWU8RVvAF+vPF5i3ekHHq8T7226rNlVfuagodaRXiOqO5E1h6Mx9ygcDL0HXvQKsxxJdl/bUP+t/+rOjA+k/IR/vF1UJGrGrkSJVfkcWXPP85cgws18gE9rIs2Ji1HGjvOmnez370L0+", | |||
|  | 			secretKey: "121a1e41fc1690dd6b90afbcacd80cf4", | |||
|  | 		}, | |||
|  | 		{ | |||
|  | 			name:      "中文数据", | |||
|  | 			data:      "用户数据", | |||
|  | 			secretKey: "密钥123", | |||
|  | 		}, | |||
|  | 		{ | |||
|  | 			name:      "API数据", | |||
|  | 			data:      "api_call_data", | |||
|  | 			secretKey: "production_key", | |||
|  | 		}, | |||
|  | 		{ | |||
|  | 			name:      "JSON格式", | |||
|  | 			data:      `{"user_id":12345,"name":"张三","status":"active"}`, | |||
|  | 			secretKey: "json_key", | |||
|  | 		}, | |||
|  | 	} | |||
|  | 
 | |||
|  | 	for i, td := range testData { | |||
|  | 		decrypted, err := WestDexDecrypt(td.data, td.secretKey) | |||
|  | 		if err != nil { | |||
|  | 			t.Fatalf("解密失败: %v", err) | |||
|  | 		} | |||
|  | 
 | |||
|  | 		t.Logf("测试 %d - %s:", i+1, td.name) | |||
|  | 		t.Logf("  原始数据: %s", td.data) | |||
|  | 		t.Logf("  使用密钥: %s", td.secretKey) | |||
|  | 		t.Logf("  解密结果: %s", string(decrypted)) | |||
|  | 		t.Logf("  解密正确: %v", string(decrypted) == td.data) | |||
|  | 		t.Log("---") | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | func TestSpecificDecrypt(t *testing.T) { | |||
|  | 	// 如果你有特定的加密数据想要解密,可以在这里测试 | |||
|  | 	specificTests := []struct { | |||
|  | 		name          string | |||
|  | 		encryptedData string | |||
|  | 		secretKey     string | |||
|  | 		expectedData  string // 如果知道预期结果的话 | |||
|  | 	}{ | |||
|  | 		// 示例:如果你有具体的加密数据想要解密,可以添加到这里 | |||
|  | 		// { | |||
|  | 		//     name:          "特定数据解密", | |||
|  | 		//     encryptedData: "你的加密数据", | |||
|  | 		//     secretKey:     "你的密钥", | |||
|  | 		//     expectedData:  "预期的解密结果", | |||
|  | 		// }, | |||
|  | 	} | |||
|  | 
 | |||
|  | 	t.Log("=== 特定数据解密测试 ===") | |||
|  | 	for _, test := range specificTests { | |||
|  | 		decrypted, err := WestDexDecrypt(test.encryptedData, test.secretKey) | |||
|  | 		if err != nil { | |||
|  | 			t.Logf("%s - 解密失败: %v", test.name, err) | |||
|  | 			continue | |||
|  | 		} | |||
|  | 
 | |||
|  | 		result := string(decrypted) | |||
|  | 		t.Logf("%s:", test.name) | |||
|  | 		t.Logf("  加密数据: %s", test.encryptedData) | |||
|  | 		t.Logf("  使用密钥: %s", test.secretKey) | |||
|  | 		t.Logf("  解密结果: %s", result) | |||
|  | 		 | |||
|  | 		if test.expectedData != "" { | |||
|  | 			t.Logf("  预期结果: %s", test.expectedData) | |||
|  | 			t.Logf("  解密正确: %v", result == test.expectedData) | |||
|  | 		} | |||
|  | 		t.Log("---") | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | func TestWestDexDecryptWithWrongKey(t *testing.T) { | |||
|  | 	// 测试用错误密钥解密 | |||
|  | 	data := "sensitive data" | |||
|  | 	correctKey := "correct_key" | |||
|  | 	wrongKey := "wrong_key" | |||
|  | 
 | |||
|  | 	// 用正确密钥加密 | |||
|  | 	encrypted, err := WestDexEncrypt(data, correctKey) | |||
|  | 	if err != nil { | |||
|  | 		t.Fatalf("加密失败: %v", err) | |||
|  | 	} | |||
|  | 
 | |||
|  | 	// 用错误密钥解密 | |||
|  | 	decrypted, err := WestDexDecrypt(encrypted, wrongKey) | |||
|  | 	if err != nil { | |||
|  | 		t.Logf("用错误密钥解密失败(这是预期的): %v", err) | |||
|  | 		return | |||
|  | 	} | |||
|  | 
 | |||
|  | 	decryptedStr := string(decrypted) | |||
|  | 	t.Logf("原始数据: %s", data) | |||
|  | 	t.Logf("用错误密钥解密结果: %s", decryptedStr) | |||
|  | 	 | |||
|  | 	// 验证解密结果应该与原始数据不同 | |||
|  | 	if decryptedStr == data { | |||
|  | 		t.Error("用错误密钥解密不应该得到正确结果") | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | // 基准测试 | |||
|  | func BenchmarkWestDexEncrypt(b *testing.B) { | |||
|  | 	data := "这是一个用于基准测试的数据字符串" | |||
|  | 	secretKey := "benchmarkKey" | |||
|  | 	 | |||
|  | 	b.ResetTimer() | |||
|  | 	for i := 0; i < b.N; i++ { | |||
|  | 		_, err := WestDexEncrypt(data, secretKey) | |||
|  | 		if err != nil { | |||
|  | 			b.Fatalf("加密失败: %v", err) | |||
|  | 		} | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | func BenchmarkWestDexDecrypt(b *testing.B) { | |||
|  | 	data := "这是一个用于基准测试的数据字符串" | |||
|  | 	secretKey := "benchmarkKey" | |||
|  | 	 | |||
|  | 	// 先加密一次获得密文 | |||
|  | 	encrypted, err := WestDexEncrypt(data, secretKey) | |||
|  | 	if err != nil { | |||
|  | 		b.Fatalf("预加密失败: %v", err) | |||
|  | 	} | |||
|  | 	 | |||
|  | 	b.ResetTimer() | |||
|  | 	for i := 0; i < b.N; i++ { | |||
|  | 		_, err := WestDexDecrypt(encrypted, secretKey) | |||
|  | 		if err != nil { | |||
|  | 			b.Fatalf("解密失败: %v", err) | |||
|  | 		} | |||
|  | 	} | |||
|  | } |