feat(sense): add reconciliation safety controls [T-012]
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -21,6 +23,38 @@ func (f *fakeMediaMTX) ServeHTTP(writer http.ResponseWriter, request *http.Reque
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
if request.URL.Path == "/v3/config/paths/list" {
|
||||
page, _ := strconv.Atoi(request.URL.Query().Get("page"))
|
||||
limit, _ := strconv.Atoi(request.URL.Query().Get("itemsPerPage"))
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
names := make([]string, 0, len(f.paths))
|
||||
for name := range f.paths {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
start := page * limit
|
||||
if start > len(names) {
|
||||
start = len(names)
|
||||
}
|
||||
end := start + limit
|
||||
if end > len(names) {
|
||||
end = len(names)
|
||||
}
|
||||
items := make([]map[string]any, 0, end-start)
|
||||
for _, name := range names[start:end] {
|
||||
items = append(items, map[string]any{"name": name})
|
||||
}
|
||||
pageCount := 0
|
||||
if len(names) > 0 {
|
||||
pageCount = (len(names) + limit - 1) / limit
|
||||
}
|
||||
_ = json.NewEncoder(writer).Encode(map[string]any{
|
||||
"itemCount": len(names), "pageCount": pageCount, "items": items,
|
||||
})
|
||||
return
|
||||
}
|
||||
prefixes := map[string]string{
|
||||
"/v3/config/paths/get/": "get",
|
||||
"/v3/config/paths/add/": "add",
|
||||
@@ -72,6 +106,54 @@ func (f *fakeMediaMTX) ServeHTTP(writer http.ResponseWriter, request *http.Reque
|
||||
http.NotFound(writer, request)
|
||||
}
|
||||
|
||||
func TestListPathNamesUsesPaginationAndDoesNotReturnSources(t *testing.T) {
|
||||
t.Parallel()
|
||||
paths := make(map[string]string)
|
||||
for index := 0; index < 205; index++ {
|
||||
paths[fmt.Sprintf("camera-%03d", index)] = fmt.Sprintf("rtsp://secret.invalid/%d", index)
|
||||
}
|
||||
fake := &fakeMediaMTX{paths: paths}
|
||||
server := httptest.NewServer(fake)
|
||||
defer server.Close()
|
||||
client, err := NewClient(server.URL, server.Client())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
values, err := client.ListPathNames(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(values) != 205 || values[0] != "camera-000" || values[204] != "camera-204" {
|
||||
t.Fatalf("unexpected path inventory: len=%d first=%q last=%q", len(values), values[0], values[len(values)-1])
|
||||
}
|
||||
for _, value := range values {
|
||||
if strings.Contains(value, "rtsp") || strings.Contains(value, "secret") {
|
||||
t.Fatalf("source leaked from path inventory: %q", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPathNamesRejectsRepeatedPage(t *testing.T) {
|
||||
t.Parallel()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
if request.URL.Path != "/v3/config/paths/list" {
|
||||
http.NotFound(writer, request)
|
||||
return
|
||||
}
|
||||
_, _ = writer.Write([]byte(`{"itemCount":2,"pageCount":2,"items":[{"name":"same"}]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
client, err := NewClient(server.URL, server.Client())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := client.ListPathNames(context.Background()); err == nil ||
|
||||
!strings.Contains(err.Error(), "repeated page") {
|
||||
t.Fatalf("repeated pagination was accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneratedClientCreateReadDeleteMapping(t *testing.T) {
|
||||
t.Parallel()
|
||||
fake := &fakeMediaMTX{paths: make(map[string]string)}
|
||||
|
||||
Reference in New Issue
Block a user