135 lines
5.0 KiB
Go
135 lines
5.0 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"crypto/ecdsa"
|
|
"crypto/sha256"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"encoding/pem"
|
|
"io"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestIntegrationAuthentication(t *testing.T) {
|
|
r := gin.New()
|
|
registerBusinessRoutes(r, &BusinessAPI{})
|
|
for _, path := range []string{"/api/v1/releases", "/api/v1/integration-kit", "/api/v1/integration-kit/download", "/api/v1/integration-kit/archive"} {
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
|
|
if w.Code != 401 {
|
|
t.Fatalf("%s: %d", path, w.Code)
|
|
}
|
|
}
|
|
}
|
|
func TestIntegrationInputs(t *testing.T) {
|
|
for _, query := range []string{"?language=rust", "?mode=browser", "?language=../../secret"} {
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/"+query, nil)
|
|
(&BusinessAPI{}).integrationKit(c)
|
|
if w.Code != 400 {
|
|
t.Fatalf("%s: %d", query, w.Code)
|
|
}
|
|
}
|
|
for language := range integrationLanguages {
|
|
kit := buildIntegrationKit(language, "service")
|
|
if !strings.Contains(kit.Prompt, languageRequirements(language)) || !strings.Contains(kit.Prompt, defaultIntegrationBaseURL) || !strings.Contains(kit.Protocol, defaultIntegrationBaseURL) || kit.PlatformBaseURL != defaultIntegrationBaseURL || !strings.Contains(kit.Protocol, "DER") || !strings.Contains(kit.Upgrade, "当前能力与实施边界") || !strings.Contains(kit.Prompt, "UpdateClient.listReleases") || !strings.Contains(kit.Upgrade, "/api/v1/portal/snapshot") {
|
|
t.Fatal("incomplete kit")
|
|
}
|
|
}
|
|
content, err := integrationAssets.ReadFile("integration_assets/openapi.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var spec map[string]any
|
|
if err = json.Unmarshal(content, &spec); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if spec["openapi"] != "3.0.3" {
|
|
t.Fatal("invalid OpenAPI")
|
|
}
|
|
}
|
|
func TestIntegrationArchiveAndSignatures(t *testing.T) {
|
|
const public = "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAESr8/VFFwt70o7fwa8HiRNyxWtZEu\nUqzCBfYRG72DazEgeOaSVAdlTv49OujtggXSJzFLOluMENJzaRmp9+iJ0Q==\n-----END PUBLIC KEY-----\n"
|
|
a := &BusinessAPI{keyring: &Keyring{Current: "prod-test", Keys: []VerificationKey{{KeyID: "prod-test", PublicKeyPEM: public}, {KeyID: "retired", PublicKeyPEM: public, Revoked: true}}}}
|
|
md := httptest.NewRecorder()
|
|
mc, _ := gin.CreateTestContext(md)
|
|
mc.Request = httptest.NewRequest("GET", "/?language=java&mode=container", nil)
|
|
a.downloadIntegrationKit(mc)
|
|
if !strings.Contains(md.Body.String(), "SDK 快速接入") || !strings.Contains(md.Body.String(), "UpdateClient.listReleases") || md.Code != 200 || !strings.Contains(md.Body.String(), public) || !strings.Contains(md.Body.String(), "22056bfacc8fe8a240dd2125b629e60ba9f4a5ea0041a1efe034f376a258d562") || strings.Contains(md.Body.String(), "retired") || md.Header().Get("Cache-Control") != "no-store" {
|
|
t.Fatal("markdown lacks trusted deployment key")
|
|
}
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/?language=java&mode=container", nil)
|
|
a.downloadIntegrationArchive(c)
|
|
if w.Code != 200 || w.Header().Get("Content-Type") != "application/zip" {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
archive, err := zip.NewReader(bytes.NewReader(w.Body.Bytes()), int64(w.Body.Len()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(archive.File) != 7 {
|
|
t.Fatal("missing files")
|
|
}
|
|
for _, file := range archive.File {
|
|
reader, err := file.Open()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content, err := io.ReadAll(reader)
|
|
reader.Close()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if file.Name == "openapi.json" && !strings.Contains(string(content), defaultIntegrationBaseURL) {
|
|
t.Fatal("OpenAPI does not use the default platform address")
|
|
}
|
|
if file.Name == "verification-keys.md" && (!strings.Contains(string(content), public) || strings.Contains(string(content), "retired")) {
|
|
t.Fatal("incorrect deployment key in archive")
|
|
}
|
|
if file.Name == "upgrade.md" && !strings.Contains(string(content), "自动升级所需服务端契约") {
|
|
t.Fatal("missing upgrade plan")
|
|
}
|
|
if file.Name != "test-vectors.json" {
|
|
continue
|
|
}
|
|
var vectors struct {
|
|
PinnedKeys []struct {
|
|
PublicKey string `json:"public_key_pem"`
|
|
} `json:"pinned_keys"`
|
|
License, Lease struct{ Signature, Canonical string }
|
|
}
|
|
if err := json.Unmarshal(content, &vectors); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
block, _ := pem.Decode([]byte(vectors.PinnedKeys[0].PublicKey))
|
|
key, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, vector := range []struct{ Signature, Canonical string }{vectors.License, vectors.Lease} {
|
|
sig, err := base64.RawStdEncoding.DecodeString(vector.Signature)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
digest := sha256.Sum256([]byte(vector.Canonical))
|
|
if !ecdsa.VerifyASN1(key.(*ecdsa.PublicKey), digest[:], sig) {
|
|
t.Fatal("bad signature")
|
|
}
|
|
digest = sha256.Sum256([]byte(vector.Canonical + "tampered"))
|
|
if ecdsa.VerifyASN1(key.(*ecdsa.PublicKey), digest[:], sig) {
|
|
t.Fatal("accepted tampering")
|
|
}
|
|
}
|
|
}
|
|
}
|