| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
stdjson "encoding/json" |
| 6 |
"math/rand" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
json "github.com/goccy/go-json" |
| 11 |
) |
| 12 |
|
| 13 |
// randomTextString returns a random string of valid UTF-8 with runs of plain bytes of every length between the |
| 14 |
// bytes which need an escape by one option or another. |
| 15 |
func randomTextString(r *rand.Rand, n int) string { |
| 16 |
var b strings.Builder |
| 17 |
for i := 0; i < n; i++ { |
| 18 |
switch k := r.Intn(24); { |
| 19 |
case k == 0: |
| 20 |
b.WriteString([]string{"\n", "\r", "\t", `"`, `\`, "\x00", "\x1f", "\x7f", "\b", "\f"}[r.Intn(10)]) |
| 21 |
case k == 1: |
| 22 |
b.WriteString([]string{"<", ">", "&"}[r.Intn(3)]) |
| 23 |
case k == 2: |
| 24 |
b.WriteString([]string{"é", "あ", "😀", "\u2028", "\u2029"}[r.Intn(5)]) |
| 25 |
default: |
| 26 |
b.WriteByte(byte(' ' + r.Intn(95))) |
| 27 |
} |
| 28 |
} |
| 29 |
return b.String() |
| 30 |
} |
| 31 |
|
| 32 |
func TestEncodeEscapedStrings(t *testing.T) { |
| 33 |
// Strings of every length with bytes to escape anywhere, encoded with every combination of the options of |
| 34 |
// the escapes, as encoding/json does for the ones it has, and decoded back to themselves for the others. |
| 35 |
r := rand.New(rand.NewSource(1)) |
| 36 |
for i := 0; i < 5000; i++ { |
| 37 |
s := randomTextString(r, r.Intn(i%300+1)) |
| 38 |
v := struct{ S string }{s} |
| 39 |
|
| 40 |
want, _ := stdjson.Marshal(v) |
| 41 |
got, err := json.Marshal(v) |
| 42 |
if err != nil || !bytes.Equal(got, want) { |
| 43 |
t.Fatalf("%q:\n got %s %v\nwant %s", s, got, err, want) |
| 44 |
} |
| 45 |
|
| 46 |
var buf bytes.Buffer |
| 47 |
enc := stdjson.NewEncoder(&buf) |
| 48 |
enc.SetEscapeHTML(false) |
| 49 |
enc.Encode(v) |
| 50 |
got, err = json.MarshalWithOption(v, json.DisableHTMLEscape()) |
| 51 |
if err != nil || !bytes.Equal(got, bytes.TrimSuffix(buf.Bytes(), []byte("\n"))) { |
| 52 |
t.Fatalf("%q: without the escape of HTML:\n got %s %v\nwant %s", s, got, err, buf.Bytes()) |
| 53 |
} |
| 54 |
|
| 55 |
for _, opts := range [][]json.EncodeOptionFunc{ |
| 56 |
{json.DisableNormalizeUTF8()}, |
| 57 |
{json.DisableNormalizeUTF8(), json.DisableHTMLEscape()}, |
| 58 |
} { |
| 59 |
got, err := json.MarshalWithOption(v, opts...) |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
var back struct{ S string } |
| 64 |
if err := stdjson.Unmarshal(got, &back); err != nil || back.S != s { |
| 65 |
t.Fatalf("%q: without the normalization of UTF-8: %s decodes to %q %v", s, got, back.S, err) |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func TestEncodeInvalidUTF8(t *testing.T) { |
| 72 |
// A byte of a string which is not valid UTF-8 is replaced by U+FFFD as encoding/json of the running Go does |
| 73 |
// ( escaped before Go 1.27, as it is by Go 1.27 ), in strings of every length and at every position, by the |
| 74 |
// scan of words and of SIMD, in values, in object keys and in the strings of the string option. |
| 75 |
type withString struct { |
| 76 |
S string `json:"s,string"` |
| 77 |
} |
| 78 |
invalid := []string{"\xff", "\xe3\x81", "\xed\xa0\x80"} |
| 79 |
others := []string{"", "<", "
", "あ", `"`} |
| 80 |
for n := 0; n < 100; n++ { |
| 81 |
for pos := 0; pos <= n; pos += 1 + n/16 { |
| 82 |
for _, bad := range invalid { |
| 83 |
for _, other := range others { |
| 84 |
s := strings.Repeat("a", n) |
| 85 |
s = s[:pos] + bad + other + s[pos:] |
| 86 |
values := []any{s, map[string]int{s: 1}, withString{S: s}} |
| 87 |
for _, v := range values { |
| 88 |
want, err := stdjson.Marshal(v) |
| 89 |
if err != nil { |
| 90 |
t.Fatal(err) |
| 91 |
} |
| 92 |
got, err := json.Marshal(v) |
| 93 |
if err != nil || string(got) != string(want) { |
| 94 |
t.Fatalf("Marshal(%q): got %q, %v, want %q", v, got, err, want) |
| 95 |
} |
| 96 |
wantIndent, _ := stdjson.MarshalIndent(v, "", " ") |
| 97 |
gotIndent, err := json.MarshalIndent(v, "", " ") |
| 98 |
if err != nil || string(gotIndent) != string(wantIndent) { |
| 99 |
t.Fatalf("MarshalIndent(%q): got %q, %v, want %q", v, gotIndent, err, wantIndent) |
| 100 |
} |
| 101 |
var wantBuf, gotBuf strings.Builder |
| 102 |
stdEnc := stdjson.NewEncoder(&wantBuf) |
| 103 |
stdEnc.SetEscapeHTML(false) |
| 104 |
_ = stdEnc.Encode(v) |
| 105 |
enc := json.NewEncoder(&gotBuf) |
| 106 |
enc.SetEscapeHTML(false) |
| 107 |
if err := enc.Encode(v); err != nil || gotBuf.String() != wantBuf.String() { |
| 108 |
t.Fatalf("Encode(%q) without HTML escapes: got %q, %v, want %q", v, gotBuf.String(), err, wantBuf.String()) |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |