|
1 | 1 | package fastjson
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "encoding/json" |
4 | 6 | "fmt"
|
5 | 7 | "math"
|
6 | 8 | "strings"
|
@@ -1275,3 +1277,59 @@ func testParseGetSerial(s string) error {
|
1275 | 1277 | }
|
1276 | 1278 | return nil
|
1277 | 1279 | }
|
| 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