MCPcopy
hub / github.com/tinylib/msgp / TestCopyJSONNegativeUTF8

Function TestCopyJSONNegativeUTF8

msgp/json_test.go:104–128  ·  view source on GitHub ↗

Encoder should generate valid utf-8 even if passed bad input

(t *testing.T)

Source from the content-addressed store, hash-verified

102
103// Encoder should generate valid utf-8 even if passed bad input
104func TestCopyJSONNegativeUTF8(t *testing.T) {
105 // Single string with non-compliant utf-8 byte
106 stringWithBadUTF8 := []byte{
107 0xa1, 0xe0,
108 }
109
110 src := bytes.NewBuffer(stringWithBadUTF8)
111
112 var js bytes.Buffer
113 _, err := CopyToJSON(&js, src)
114 if err != nil {
115 t.Fatal(err)
116 }
117
118 // Even though we provided bad input, should have escaped the naughty character
119 if !utf8.Valid(js.Bytes()) {
120 t.Errorf("Expected JSON to be valid utf-8 even when provided bad input")
121 }
122
123 // Expect a bad character string
124 expected := `"\ufffd"`
125 if js.String() != expected {
126 t.Errorf("Expected: '%s', got: '%s'", expected, js.String())
127 }
128}
129
130func BenchmarkCopyToJSON(b *testing.B) {
131 var buf bytes.Buffer

Callers

nothing calls this directly

Calls 2

CopyToJSONFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…