Skip to content

Commit b4ef4ca

Browse files
committed
Add tests
1 parent 23b0485 commit b4ef4ca

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

parser_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package fastjson
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"math"
68
"strings"
@@ -1275,3 +1277,59 @@ func testParseGetSerial(s string) error {
12751277
}
12761278
return nil
12771279
}
1280+
1281+
// Tests for https://github.com/valyala/fastjson/issues/90
1282+
// This was manifesting due to the use of strconv.AppendQuote
1283+
func TestUTF8NonPrintableArtifacts(t *testing.T) {
1284+
testCases := []struct {
1285+
name string
1286+
s string
1287+
}{
1288+
{
1289+
name: "problematic bytes",
1290+
s: "data:\"\\xd6`\\xb76d\\xf6E\U000E8737(\\x91\\xb294\"",
1291+
},
1292+
}
1293+
1294+
for _, tc := range testCases {
1295+
t.Run(tc.name, func(t *testing.T) {
1296+
jsonEnvelope := struct {
1297+
Inner string
1298+
}{
1299+
Inner: tc.s,
1300+
}
1301+
1302+
m, err := json.Marshal(jsonEnvelope) // `m` is a full valid json
1303+
if err != nil {
1304+
t.Fatalf("unexpected error: %s", err)
1305+
}
1306+
1307+
// we will pass that `m` json through a fastjson marshal/unmarshal cycle
1308+
1309+
fastjsonValue, err := ParseBytes(m)
1310+
if err != nil {
1311+
t.Fatalf("unexpected error: %s", err)
1312+
}
1313+
1314+
o, err := fastjsonValue.Object()
1315+
if err != nil {
1316+
t.Fatalf("unexpected error: %s", err)
1317+
}
1318+
1319+
// In order to trigger the bug we need to visit all the keys and call Type() on them
1320+
o.Visit(func(k []byte, v *Value) {
1321+
v.Type()
1322+
})
1323+
1324+
res := fastjsonValue.MarshalTo(nil)
1325+
if !bytes.Equal(res, m) {
1326+
t.Fatalf("unexpected result; got %q; want %q", res, m)
1327+
}
1328+
1329+
err = ValidateBytes(res)
1330+
if err != nil {
1331+
t.Fatalf("unexpected error: %s", err)
1332+
}
1333+
})
1334+
}
1335+
}

0 commit comments

Comments
 (0)