Files
artex/db/jsonb_clean_test.go
T
dela 0335d572de
ci / go (push) Waiting to run
ci / go-db (agent) (push) Waiting to run
ci / go-db (config) (push) Waiting to run
ci / go-db (db) (push) Waiting to run
ci / go-db (evidence) (push) Waiting to run
ci / go-db (llmrec) (push) Waiting to run
ci / go-db (server) (push) Waiting to run
web / web (push) Waiting to run
docs / links (push) Canceled after 0s
detections / detections (push) Canceled after 0s
First Commit
2026-10-09 08:38:16 +08:00

45 lines
1.3 KiB
Go

package db
import (
"encoding/json"
"strings"
"testing"
)
func TestJsonbClean(t *testing.T) {
// A marshaled payload carrying a NUL byte (e.g. captured HTTP/tool output).
b, err := json.Marshal(map[string]string{"body": "ab\x00cd"})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(b), `\u0000`) {
t.Fatalf("precondition: marshaled JSON should contain the NUL escape, got %s", b)
}
cleaned := jsonbClean(b)
if strings.Contains(string(cleaned), `\u0000`) {
t.Fatalf("jsonbClean left a NUL escape jsonb rejects: %s", cleaned)
}
// Result must stay valid JSON with the NUL simply dropped.
var out map[string]string
if err := json.Unmarshal(cleaned, &out); err != nil {
t.Fatalf("cleaned bytes are not valid JSON: %v (%s)", err, cleaned)
}
if out["body"] != "abcd" {
t.Fatalf("expected NUL stripped to \"abcd\", got %q", out["body"])
}
// A NUL escape typed literally in source text (doubled backslash) is preserved.
lit := []byte(`{"body":"\\u0000"}`)
if got := jsonbClean(lit); string(got) != string(lit) {
t.Fatalf("literal \\\\u0000 must be untouched, got %s", got)
}
// No NUL escape → returned unchanged.
plain := []byte(`{"body":"hello"}`)
if got := jsonbClean(plain); string(got) != string(plain) {
t.Fatalf("plain JSON must be untouched, got %s", got)
}
}