MCPcopy Create free account
hub / github.com/buger/jsonparser / FuzzSequence

Function FuzzSequence

path_fuzz_test.go:541–712  ·  view source on GitHub ↗

FuzzSequence runs multi-operation sequences against a single JSON document. Verifies: SYS-REQ-035 (no-panic on sequences of operations). reqproof:proptest Set, Delete, Get

(f *testing.F)

Source from the content-addressed store, hash-verified

539// Verifies: SYS-REQ-035 (no-panic on sequences of operations).
540// reqproof:proptest Set, Delete, Get
541func FuzzSequence(f *testing.F) {
542 // Seed with valid JSON + valid-shape paths so the sequences have a
543 // well-defined starting state.
544 f.Add([]byte(`{"a":1,"b":2}`), []byte("a"))
545 f.Add([]byte(`{"a":1,"b":2}`), []byte("b"))
546 f.Add([]byte(`{"x":{"y":1}}`), []byte("x/y"))
547 f.Add([]byte(`[1,2,3]`), []byte("[0]"))
548 f.Add([]byte(`{"a":[1,2,3]}`), []byte("a/[1]"))
549 f.Add([]byte(`{}`), []byte("newkey"))
550 f.Add([]byte(`{"a":1}`), []byte("a"))
551
552 f.Fuzz(func(t *testing.T, data []byte, pathBytes []byte) {
553 // Only sequence-test VALID JSON — invalid input has no defined
554 // post-condition invariant.
555 if !json.Valid(data) {
556 return
557 }
558 path := splitPathComponents(pathBytes)
559 // Restrict to paths whose shape matches the data (the documented-open
560 // D6/D9/D10 divergences are out of scope for sequence invariants).
561 if !pathShapeMatchesData(data, path) {
562 return
563 }
564 // Refuse the empty-key hazard (SYS-REQ-111 class).
565 for _, c := range path {
566 if c == "" {
567 return
568 }
569 }
570
571 r := newRandFromSeed(data, pathBytes)
572 setVal := []byte(`42`)
573 setVal2 := []byte(`99`)
574
575 // Pick one of the five sequence shapes at random.
576 switch r.Intn(5) {
577 case 0:
578 // S1: Set → Get round-trip (deeper than single-op gate).
579 //
580 // NOTE: Set at an out-of-range array index appends to the array
581 // (SYS-REQ-110). Get at the same index then returns not-found
582 // because the array doesn't have that many elements. This is
583 // documented behavior; a Get error here is tolerated (matching
584 // the existing round-trip gate in json_fuzz_test.go).
585 out, ok := applySequenceStep(t, data, "set", path, setVal)
586 if !ok {
587 return
588 }
589 assertAfterStep(t, "S1.Set", out, path, func(t *testing.T, state []byte) {
590 got, _, _, gErr := Get(state, path...)
591 if gErr != nil {
592 return // tolerated: OOB index after append-style Set.
593 }
594 // Allow numeric-equivalent representations.
595 if bytes.Equal(got, setVal) {
596 return
597 }
598 if gf, e1 := strconvParseFloat(got); e1 == nil {

Callers

nothing calls this directly

Calls 7

splitPathComponentsFunction · 0.85
pathShapeMatchesDataFunction · 0.85
newRandFromSeedFunction · 0.85
applySequenceStepFunction · 0.85
assertAfterStepFunction · 0.85
GetFunction · 0.85
strconvParseFloatFunction · 0.85

Tested by

no test coverage detected