66 lines
2.5 KiB
Go
66 lines
2.5 KiB
Go
package dispatcher
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/configread"
|
|
)
|
|
|
|
func TestMalformedSIPHTTPRejectsOriginalNotificationAndKeepsAdmissionClosed(t *testing.T) {
|
|
for _, publishFails := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "confirmed", true: "confirmation failure"}[publishFails], func(t *testing.T) {
|
|
execute, _, publisher, s := newExecuteFixture(t)
|
|
snapshot := policySnapshot(t)
|
|
id := snapshot.Task.DispatcherID
|
|
if publishFails {
|
|
publisher.err = errors.New("publisher confirmation lost")
|
|
}
|
|
if err := s.NoteSIPChange(id, 2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
original := []byte(`{"event_id":"original-sip-event","event_type":"sip.config","dispatcher_id":"` + id + `","payload":{"trunk_id":"1","change_type":"enabled","revision":2}}`)
|
|
if err := s.RememberSIPSource(id, original); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"resource":"sip_config","private":"PRIVATE-SIP-SOURCE"}`))
|
|
}))
|
|
defer server.Close()
|
|
client, err := configread.NewClient(server.URL, id, "fixture", server.Client())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := &Runtime{Bootstrap: Bootstrap{Store: s, DispatcherID: id, Client: client, VerifySIP: func(context.Context, configread.SIP) error {
|
|
t.Fatal("invalid configuration reached native verifier")
|
|
return nil
|
|
}}, Execute: *execute, Logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
|
|
sip := snapshot.SIP
|
|
if err := r.refreshSIP(context.Background(), &sip); err == nil {
|
|
t.Fatal("malformed configuration hidden by feedback success")
|
|
}
|
|
if len(publisher.bodies) != 1 || !bytes.Contains(publisher.bodies[0], []byte(`"event_id":"original-sip-event"`)) || !bytes.Contains(publisher.bodies[0], []byte(`"status":"rejected"`)) || bytes.Contains(publisher.bodies[0], []byte("PRIVATE-SIP-SOURCE")) {
|
|
t.Fatalf("wrong or unsafe feedback: %s", publisher.bodies)
|
|
}
|
|
_, pending, err := s.SIPState(id)
|
|
if err != nil || pending == 0 {
|
|
t.Fatalf("malformed SIP removed the admission fence: %d %v", pending, err)
|
|
}
|
|
events, err := s.ListPendingOutbox(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if (publishFails && len(events) != 1) || (!publishFails && len(events) != 0) {
|
|
t.Fatalf("failed feedback not retained for retry: %+v", events)
|
|
}
|
|
})
|
|
}
|
|
}
|