140 lines
4.9 KiB
Go
140 lines
4.9 KiB
Go
package contract
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/go-sip/contracts"
|
|
)
|
|
|
|
func TestValidateStaticArtifactBindsCellAndTrunks(t *testing.T) {
|
|
raw, err := contracts.Read("examples/static-cell-artifact.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
artifact, err := ValidateStaticArtifact(raw, StaticArtifactExpectation{
|
|
CellID: "cell-a",
|
|
Mode: "mock",
|
|
SourceRelease: "management-snapshot-1",
|
|
SourceDigest: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
ConfigSHA256: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
|
MinimumRevision: 1,
|
|
AllowedEgressPoolIDs: []string{"egress-mock"},
|
|
RequiredTrunkIDs: []string{"trunk-mock"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("valid artifact rejected: %v", err)
|
|
}
|
|
if artifact.CellID != "cell-a" || artifact.Revision != 1 || len(artifact.Trunks) != 1 {
|
|
t.Fatalf("unexpected artifact: %+v", artifact)
|
|
}
|
|
}
|
|
|
|
func TestValidateRealStaticArtifact(t *testing.T) {
|
|
raw, err := contracts.Read("examples/static-cell-artifact-real-v1.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
artifact, err := ValidateStaticArtifact(raw, StaticArtifactExpectation{
|
|
CellID: "cell-single",
|
|
Mode: "real",
|
|
SourceRelease: "asterisk-22.10.1-native-v1",
|
|
SourceDigest: "68006a1a8efed288be4ca4a2ae3cb9554a31d733eac08eaacf4c646c95faf74d",
|
|
ConfigSHA256: "89d2686d0d1ca60159c3c6bd725dc9e6f511cbdb56bf6ce7b65ca7d4dc3f2d60",
|
|
AllowedEgressPoolIDs: []string{"egress-single"},
|
|
RequiredTrunkIDs: []string{"provider-second"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("valid real artifact rejected: %v", err)
|
|
}
|
|
if artifact.ARI == nil || artifact.Media == nil || artifact.Recording == nil {
|
|
t.Fatalf("real artifact lost runtime sections: %+v", artifact)
|
|
}
|
|
if artifact.Media.Format != "alaw" || artifact.Media.SampleRateHz != 8000 || artifact.Media.PayloadType != 8 || artifact.Recording.Format != "wav" {
|
|
t.Fatalf("unexpected real media/recording contract: %+v %+v", artifact.Media, artifact.Recording)
|
|
}
|
|
}
|
|
|
|
func TestValidateStaticArtifactRejectsBindingViolations(t *testing.T) {
|
|
raw, err := contracts.Read("examples/static-cell-artifact.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
base := func() StaticArtifactExpectation {
|
|
return StaticArtifactExpectation{
|
|
CellID: "cell-a",
|
|
Mode: "mock",
|
|
SourceRelease: "management-snapshot-1",
|
|
SourceDigest: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
ConfigSHA256: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
|
AllowedEgressPoolIDs: []string{"egress-mock"},
|
|
}
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
expected StaticArtifactExpectation
|
|
mutate func(*StaticCellArtifact)
|
|
}{
|
|
{name: "wrong cell", expected: func() StaticArtifactExpectation { e := base(); e.CellID = "cell-b"; return e }()},
|
|
{name: "wrong source digest", expected: func() StaticArtifactExpectation {
|
|
e := base()
|
|
e.SourceDigest = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
|
|
return e
|
|
}()},
|
|
{name: "old revision", expected: func() StaticArtifactExpectation { e := base(); e.MinimumRevision = 2; return e }()},
|
|
{name: "disallowed egress", expected: func() StaticArtifactExpectation {
|
|
e := base()
|
|
e.AllowedEgressPoolIDs = []string{"egress-other"}
|
|
return e
|
|
}()},
|
|
{name: "missing required trunk", expected: func() StaticArtifactExpectation {
|
|
e := base()
|
|
e.RequiredTrunkIDs = []string{"trunk-required"}
|
|
return e
|
|
}()},
|
|
{name: "disabled required trunk", expected: func() StaticArtifactExpectation { e := base(); e.RequiredTrunkIDs = []string{"trunk-mock"}; return e }(), mutate: func(a *StaticCellArtifact) { a.Trunks[0].Enabled = false }},
|
|
{name: "duplicate trunk", expected: base(), mutate: func(a *StaticCellArtifact) { a.Trunks = append(a.Trunks, a.Trunks[0]) }},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
candidate := raw
|
|
if test.mutate != nil {
|
|
var artifact StaticCellArtifact
|
|
if err := json.Unmarshal(raw, &artifact); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
test.mutate(&artifact)
|
|
candidate, err = json.Marshal(artifact)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := ValidateStaticArtifact(candidate, test.expected); err == nil {
|
|
t.Fatal("expected static artifact validation to fail")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateStaticArtifactAlwaysChecksSourceSchema(t *testing.T) {
|
|
raw, err := contracts.Read("examples/static-cell-artifact.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var value map[string]any
|
|
if err := json.Unmarshal(raw, &value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value["unexpected"] = true
|
|
candidate, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := ValidateStaticArtifact(candidate, StaticArtifactExpectation{}); err == nil {
|
|
t.Fatal("expected schema validation failure")
|
|
}
|
|
}
|