446 lines
10 KiB
Go
446 lines
10 KiB
Go
package main
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"os"
|
||
"strconv"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/tjfoc/gmsm/sm2"
|
||
)
|
||
|
||
func TestGenerateKeyPair(t *testing.T) {
|
||
privateKey, err := sm2.GenerateKey(nil)
|
||
if err != nil {
|
||
t.Fatalf("生成密钥对失败: %v", err)
|
||
}
|
||
|
||
privateKeyHex := fmt.Sprintf("%x", privateKey.D.Bytes())
|
||
publicKeyHex := fmt.Sprintf("%x", privateKey.PublicKey.X.Bytes()) + fmt.Sprintf("%x", privateKey.PublicKey.Y.Bytes())
|
||
|
||
t.Logf("私钥长度: %d", len(privateKeyHex))
|
||
t.Logf("公钥长度: %d", len(publicKeyHex))
|
||
|
||
if len(privateKeyHex) != 64 {
|
||
t.Errorf("私钥长度应为64,实际为%d", len(privateKeyHex))
|
||
}
|
||
|
||
if len(publicKeyHex) != 128 {
|
||
t.Errorf("公钥长度应为128,实际为%d", len(publicKeyHex))
|
||
}
|
||
}
|
||
|
||
func TestSignData(t *testing.T) {
|
||
privateKey, _ := sm2.GenerateKey(nil)
|
||
privateKeyHex := fmt.Sprintf("%x", privateKey.D.Bytes())
|
||
|
||
client, err := NewTaxHealthClient("test_user", privateKeyHex)
|
||
if err != nil {
|
||
t.Fatalf("创建客户端失败: %v", err)
|
||
}
|
||
|
||
testData := []byte("test data")
|
||
signature, err := client.signData(testData)
|
||
if err != nil {
|
||
t.Fatalf("签名失败: %v", err)
|
||
}
|
||
|
||
t.Logf("签名结果: %s", signature)
|
||
|
||
if signature == "" {
|
||
t.Error("签名结果为空")
|
||
}
|
||
}
|
||
|
||
func TestBuildSignature(t *testing.T) {
|
||
privateKey, _ := sm2.GenerateKey(nil)
|
||
privateKeyHex := fmt.Sprintf("%x", privateKey.D.Bytes())
|
||
|
||
client, err := NewTaxHealthClient("test_user", privateKeyHex)
|
||
if err != nil {
|
||
t.Fatalf("创建客户端失败: %v", err)
|
||
}
|
||
|
||
tests := []struct {
|
||
name string
|
||
nonce string
|
||
ts string
|
||
uri string
|
||
body []byte
|
||
wantErr bool
|
||
}{
|
||
{"完整参数", "abc123", "1694400142", "/api/test", []byte(`{"test":"data"}`), false},
|
||
{"空body", "abc123", "1694400142", "/api/test", nil, false},
|
||
{"空nonce", "", "1694400142", "/api/test", nil, false},
|
||
{"空uri", "abc123", "1694400142", "", nil, false},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
sig, err := client.buildSignature(tt.nonce, tt.ts, tt.uri, tt.body)
|
||
if (err != nil) != tt.wantErr {
|
||
t.Errorf("buildSignature() error = %v, wantErr %v", err, tt.wantErr)
|
||
return
|
||
}
|
||
if !tt.wantErr && sig == "" {
|
||
t.Error("buildSignature() 返回空签名")
|
||
}
|
||
t.Logf("签名: %s", sig)
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestValidateTaxEvalRequest(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
req TaxEvalRequest
|
||
wantErr bool
|
||
}{
|
||
{
|
||
"完整请求",
|
||
TaxEvalRequest{
|
||
ReqNo: "REQ001",
|
||
Detail: TaxEvalDetail{
|
||
CompanyName: "测试公司",
|
||
Period: "年度",
|
||
Year: "2024",
|
||
FileURL: "http://example.com/test.xlsx",
|
||
CIT: 100.0,
|
||
VAT: 50.0,
|
||
InputVAT: 30.0,
|
||
OutputVAT: 80.0,
|
||
PreCIT: 90.0,
|
||
PreVAT: 45.0,
|
||
PreInputVAT: 25.0,
|
||
PreOutputVAT: 70.0,
|
||
},
|
||
},
|
||
false,
|
||
},
|
||
{
|
||
"使用data字段",
|
||
TaxEvalRequest{
|
||
ReqNo: "REQ002",
|
||
Detail: TaxEvalDetail{
|
||
CompanyName: "测试公司",
|
||
Period: "年度",
|
||
Year: "2024",
|
||
Data: map[string]interface{}{"key": "value"},
|
||
CIT: 100.0,
|
||
VAT: 50.0,
|
||
InputVAT: 30.0,
|
||
OutputVAT: 80.0,
|
||
PreCIT: 90.0,
|
||
PreVAT: 45.0,
|
||
PreInputVAT: 25.0,
|
||
PreOutputVAT: 70.0,
|
||
},
|
||
},
|
||
false,
|
||
},
|
||
{
|
||
"缺少req_no",
|
||
TaxEvalRequest{
|
||
Detail: TaxEvalDetail{
|
||
CompanyName: "测试公司",
|
||
Period: "年度",
|
||
Year: "2024",
|
||
FileURL: "http://example.com/test.xlsx",
|
||
},
|
||
},
|
||
true,
|
||
},
|
||
{
|
||
"缺少company_name",
|
||
TaxEvalRequest{
|
||
ReqNo: "REQ003",
|
||
Detail: TaxEvalDetail{
|
||
Period: "年度",
|
||
Year: "2024",
|
||
FileURL: "http://example.com/test.xlsx",
|
||
},
|
||
},
|
||
true,
|
||
},
|
||
{
|
||
"缺少period",
|
||
TaxEvalRequest{
|
||
ReqNo: "REQ004",
|
||
Detail: TaxEvalDetail{
|
||
CompanyName: "测试公司",
|
||
Year: "2024",
|
||
FileURL: "http://example.com/test.xlsx",
|
||
},
|
||
},
|
||
true,
|
||
},
|
||
{
|
||
"缺少year",
|
||
TaxEvalRequest{
|
||
ReqNo: "REQ005",
|
||
Detail: TaxEvalDetail{
|
||
CompanyName: "测试公司",
|
||
Period: "年度",
|
||
FileURL: "http://example.com/test.xlsx",
|
||
},
|
||
},
|
||
true,
|
||
},
|
||
{
|
||
"file_url和data都为空",
|
||
TaxEvalRequest{
|
||
ReqNo: "REQ006",
|
||
Detail: TaxEvalDetail{
|
||
CompanyName: "测试公司",
|
||
Period: "年度",
|
||
Year: "2024",
|
||
},
|
||
},
|
||
true,
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
err := ValidateTaxEvalRequest(tt.req)
|
||
if (err != nil) != tt.wantErr {
|
||
t.Errorf("ValidateTaxEvalRequest() error = %v, wantErr %v", err, tt.wantErr)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestGenerateNonce(t *testing.T) {
|
||
privateKey, _ := sm2.GenerateKey(nil)
|
||
privateKeyHex := fmt.Sprintf("%x", privateKey.D.Bytes())
|
||
|
||
client, _ := NewTaxHealthClient("test_user", privateKeyHex)
|
||
|
||
nonces := make(map[string]bool)
|
||
for i := 0; i < 100; i++ {
|
||
nonce := client.generateNonce()
|
||
if nonces[nonce] {
|
||
t.Errorf("生成了重复的nonce: %s", nonce)
|
||
}
|
||
nonces[nonce] = true
|
||
t.Logf("nonce[%d]: %s", i, nonce)
|
||
}
|
||
}
|
||
|
||
func TestGenerateTimestamp(t *testing.T) {
|
||
privateKey, _ := sm2.GenerateKey(nil)
|
||
privateKeyHex := fmt.Sprintf("%x", privateKey.D.Bytes())
|
||
|
||
client, _ := NewTaxHealthClient("test_user", privateKeyHex)
|
||
|
||
ts := client.generateTimestamp()
|
||
t.Logf("时间戳: %s", ts)
|
||
|
||
_, err := time.Parse("20060102150405", ts)
|
||
if err != nil {
|
||
_, err = strconv.ParseInt(ts, 10, 64)
|
||
if err != nil {
|
||
t.Errorf("时间戳格式不正确: %s", ts)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestJSONMarshalConsistency(t *testing.T) {
|
||
req := TaxEvalRequest{
|
||
ReqNo: "REQ001",
|
||
Detail: TaxEvalDetail{
|
||
CompanyName: "测试公司",
|
||
Period: "年度",
|
||
Year: "2024",
|
||
Data: map[string]interface{}{
|
||
"LC0000003E_T0I1231": 155341.51,
|
||
"LC0000003E_T0I0101": 103743.54,
|
||
},
|
||
CIT: 100.0,
|
||
VAT: 50.0,
|
||
InputVAT: 30.0,
|
||
OutputVAT: 80.0,
|
||
PreCIT: 90.0,
|
||
PreVAT: 45.0,
|
||
PreInputVAT: 25.0,
|
||
PreOutputVAT: 70.0,
|
||
},
|
||
}
|
||
|
||
json1, err := json.Marshal(req)
|
||
if err != nil {
|
||
t.Fatalf("第一次序列化失败: %v", err)
|
||
}
|
||
|
||
json2, err := json.Marshal(req)
|
||
if err != nil {
|
||
t.Fatalf("第二次序列化失败: %v", err)
|
||
}
|
||
|
||
if string(json1) != string(json2) {
|
||
t.Errorf("JSON序列化不一致\n第一次: %s\n第二次: %s", json1, json2)
|
||
}
|
||
|
||
t.Logf("JSON长度: %d", len(json1))
|
||
t.Logf("JSON内容: %s", json1)
|
||
}
|
||
|
||
func TestTaxEvalIntegration(t *testing.T) {
|
||
skIP := os.Getenv("TAX_HEALTH_SKIP_INTEGRATION")
|
||
if skIP == "true" {
|
||
t.Skip("跳过集成测试")
|
||
}
|
||
|
||
privateKey, _ := sm2.GenerateKey(nil)
|
||
privateKeyHex := fmt.Sprintf("%x", privateKey.D.Bytes())
|
||
|
||
client, err := NewTaxHealthClient("test_user", privateKeyHex)
|
||
if err != nil {
|
||
t.Fatalf("创建客户端失败: %v", err)
|
||
}
|
||
|
||
req := TaxEvalRequest{
|
||
ReqNo: "TEST_REQ_" + fmt.Sprintf("%d", time.Now().Unix()),
|
||
Detail: TaxEvalDetail{
|
||
CompanyName: "测试公司",
|
||
Period: "年度",
|
||
Year: "2024",
|
||
Data: map[string]interface{}{
|
||
"LC0000003E_T0I1231": 155341.51,
|
||
"LC0000003E_T0I0101": 103743.54,
|
||
},
|
||
CIT: 100.0,
|
||
VAT: 50.0,
|
||
InputVAT: 30.0,
|
||
OutputVAT: 80.0,
|
||
PreCIT: 90.0,
|
||
PreVAT: 45.0,
|
||
PreInputVAT: 25.0,
|
||
PreOutputVAT: 70.0,
|
||
},
|
||
}
|
||
|
||
err = ValidateTaxEvalRequest(req)
|
||
if err != nil {
|
||
t.Fatalf("验证请求失败: %v", err)
|
||
}
|
||
|
||
t.Log("发送税务健康测评请求...")
|
||
resp, err := client.TaxEval(req)
|
||
if err != nil {
|
||
t.Logf("API调用失败(可能是预期的): %v", err)
|
||
t.Skip("API调用失败,跳过后续测试")
|
||
}
|
||
|
||
t.Logf("响应: code=%d, req_no=%s, order_no=%s", resp.Code, resp.Data.ReqNo, resp.Data.OrderNo)
|
||
|
||
if resp.Code != 200 {
|
||
t.Errorf("期望状态码200,实际为%d", resp.Code)
|
||
}
|
||
|
||
if resp.Data.ReqNo != req.ReqNo {
|
||
t.Errorf("req_no不匹配: 期望%s, 实际%s", req.ReqNo, resp.Data.ReqNo)
|
||
}
|
||
|
||
if resp.Data.OrderNo == "" {
|
||
t.Error("order_no为空")
|
||
}
|
||
|
||
t.Log("查询测评结果...")
|
||
queryResp, err := client.QueryResult(resp.Data.OrderNo)
|
||
if err != nil {
|
||
t.Logf("查询结果失败(可能是预期的): %v", err)
|
||
return
|
||
}
|
||
|
||
t.Logf("查询响应: code=%d, status=%d", queryResp.Code, queryResp.Data.Status)
|
||
|
||
if queryResp.Code != 200 {
|
||
t.Errorf("期望状态码200,实际为%d", queryResp.Code)
|
||
}
|
||
}
|
||
|
||
func TestQueryCompanyInfoIntegration(t *testing.T) {
|
||
skIP := os.Getenv("TAX_HEALTH_SKIP_INTEGRATION")
|
||
if skIP == "true" {
|
||
t.Skip("跳过集成测试")
|
||
}
|
||
|
||
privateKey, _ := sm2.GenerateKey(nil)
|
||
privateKeyHex := fmt.Sprintf("%x", privateKey.D.Bytes())
|
||
|
||
client, err := NewTaxHealthClient("test_user", privateKeyHex)
|
||
if err != nil {
|
||
t.Fatalf("创建客户端失败: %v", err)
|
||
}
|
||
|
||
t.Log("查询公司信息...")
|
||
resp, err := client.QueryCompanyInfo("深圳市致远速联信息技术有限公司", []string{"p_info", "p_biddings_all"})
|
||
if err != nil {
|
||
t.Logf("API调用失败(可能是预期的): %v", err)
|
||
t.Skip("API调用失败")
|
||
}
|
||
|
||
t.Logf("响应: code=%d", resp.Code)
|
||
|
||
if resp.Code != 0 {
|
||
t.Errorf("期望状态码0,实际为%d", resp.Code)
|
||
}
|
||
}
|
||
|
||
func BenchmarkSignData(b *testing.B) {
|
||
privateKey, _ := sm2.GenerateKey(nil)
|
||
privateKeyHex := fmt.Sprintf("%x", privateKey.D.Bytes())
|
||
|
||
client, _ := NewTaxHealthClient("test_user", privateKeyHex)
|
||
|
||
testData := []byte("test data for benchmark")
|
||
|
||
b.ResetTimer()
|
||
for i := 0; i < b.N; i++ {
|
||
client.signData(testData)
|
||
}
|
||
}
|
||
|
||
func BenchmarkBuildSignature(b *testing.B) {
|
||
privateKey, _ := sm2.GenerateKey(nil)
|
||
privateKeyHex := fmt.Sprintf("%x", privateKey.D.Bytes())
|
||
|
||
client, _ := NewTaxHealthClient("test_user", privateKeyHex)
|
||
|
||
b.ResetTimer()
|
||
for i := 0; i < b.N; i++ {
|
||
client.buildSignature("abc123", "1694400142", "/api/test", []byte(`{"test":"data"}`))
|
||
}
|
||
}
|
||
|
||
func BenchmarkJSONMarshal(b *testing.B) {
|
||
req := TaxEvalRequest{
|
||
ReqNo: "REQ001",
|
||
Detail: TaxEvalDetail{
|
||
CompanyName: "测试公司",
|
||
Period: "年度",
|
||
Year: "2024",
|
||
Data: map[string]interface{}{
|
||
"LC0000003E_T0I1231": 155341.51,
|
||
"LC0000033E_T0I1231": 1000000.00,
|
||
},
|
||
CIT: 100.0,
|
||
VAT: 50.0,
|
||
InputVAT: 30.0,
|
||
OutputVAT: 80.0,
|
||
PreCIT: 90.0,
|
||
PreVAT: 45.0,
|
||
PreInputVAT: 25.0,
|
||
PreOutputVAT: 70.0,
|
||
},
|
||
}
|
||
|
||
b.ResetTimer()
|
||
for i := 0; i < b.N; i++ {
|
||
json.Marshal(req)
|
||
}
|
||
}
|