72 lines
2.1 KiB
Go
72 lines
2.1 KiB
Go
package ai
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.ipao.vip/rogee/go-sip/contracts"
|
|
"git.ipao.vip/rogee/go-sip/internal/contract"
|
|
)
|
|
|
|
func TestMQConfigurationMatchesOriginalRequestAndCanonicalDigest(t *testing.T) {
|
|
base := "upstream/" + contract.MQSourceCommit + "/examples/"
|
|
requestRaw, err := contracts.Files.ReadFile(base + "ai-config-request.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request, err := contract.DecodeService(requestRaw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := contracts.Files.ReadFile(base + "ai-config-result.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Date(2026, 9, 21, 0, 0, 1, 0, time.UTC)
|
|
snapshot, err := ValidateConfigResponse(raw, request, now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if snapshot.TenantKey != request.TenantKey || snapshot.AgentVersionID == "" {
|
|
t.Fatal("configuration lost request binding")
|
|
}
|
|
for _, field := range []string{"dispatcher_id", "tenant_id", "tenant_key", "correlation_id"} {
|
|
var changed map[string]any
|
|
if err := json.Unmarshal(raw, &changed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
changed[field] = "incorrect"
|
|
encoded, err := json.Marshal(changed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := ValidateConfigResponse(encoded, request, now); err == nil {
|
|
t.Fatalf("mismatched %s accepted", field)
|
|
}
|
|
}
|
|
var changed map[string]any
|
|
if err := json.Unmarshal(raw, &changed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
changed["payload"].(map[string]any)["snapshot"].(map[string]any)["status"] = "reused"
|
|
reused, err := json.Marshal(changed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := ValidateConfigResponse(reused, request, now); err != nil {
|
|
t.Fatalf("published immutable version reuse rejected: %v", err)
|
|
}
|
|
changed["payload"].(map[string]any)["snapshot"].(map[string]any)["content_sha256"] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
encoded, err := json.Marshal(changed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := ValidateConfigResponse(encoded, request, now); err == nil {
|
|
t.Fatal("forged digest accepted")
|
|
}
|
|
if _, err := ValidateConfigResponse(raw, request, now.Add(time.Hour)); err == nil {
|
|
t.Fatal("late response accepted")
|
|
}
|
|
}
|