File: json_test.go

package info (click to toggle)
golang-github-gabriel-vasile-mimetype 1.4.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 9,720 kB
  • sloc: javascript: 3; makefile: 3; tcl: 1; php: 1; python: 1; perl: 1
file content (97 lines) | stat: -rw-r--r-- 2,572 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package json

import (
	"strings"
	"testing"
)

func TestScan(t *testing.T) {
	tCases := []struct {
		data   string
		length int
		ok     bool
	}{
		{`foo`, 2, false},
		{`}{`, 1, false},
		{`{]`, 2, false},
		{`{}`, 2, true},
		{`[]`, 2, true},
		{`"foo"`, 5, true},
		{`120`, 3, true},
		{`1e20`, 4, true},
		{`0.120`, 5, true},
		{`null`, 4, true},
		{`{"foo":"bar"}`, 13, true},
		{`{"foo":"21\t\u0009 \u1234","bar":{"baz":["qux"]}`, 48, false},
		{`{"foo":"bar","bar":{"baz":["qux"]}}`, 35, true},
		{`{"foo":-1,"bar":{"baz":[true, false, null, 100, 0.123]}}`, 56, true},
		{`{"foo":-1,"bar":{"baz":[tru]}}`, 28, false},
		{`{"foo":-1,"bar":{"baz":[nul]}}`, 28, false},
		{`{"foo":-1,"bar":{"baz":[314e+1]}}`, 33, true},
	}
	for _, st := range tCases {
		scanned, err := Scan([]byte(st.data))
		if scanned != st.length {
			t.Errorf("Scan length error: expected: %d; got: %d; input: %s",
				st.length, scanned, st.data)
		}

		if err != nil && st.ok {
			t.Errorf("Scan failed with err: %s; input: %s", err, st.data)
		}

		if err == nil && !st.ok {
			t.Errorf("Scan should fail for input: %s", st.data)
		}
	}
}

func TestScannerMaxDepth(t *testing.T) {
	tCases := []struct {
		name        string
		data        string
		errMaxDepth bool
	}{
		{
			name:        "ArrayUnderMaxNestingDepth",
			data:        `{"a":` + strings.Repeat(`[`, 10000-1) + strings.Repeat(`]`, 10000-1) + `}`,
			errMaxDepth: false,
		},
		{
			name:        "ArrayOverMaxNestingDepth",
			data:        `{"a":` + strings.Repeat(`[`, 10000) + strings.Repeat(`]`, 10000) + `}`,
			errMaxDepth: true,
		},
		{
			name:        "ObjectUnderMaxNestingDepth",
			data:        `{"a":` + strings.Repeat(`{"a":`, 10000-1) + `0` + strings.Repeat(`}`, 10000-1) + `}`,
			errMaxDepth: false,
		},
		{
			name:        "ObjectOverMaxNestingDepth",
			data:        `{"a":` + strings.Repeat(`{"a":`, 10000) + `0` + strings.Repeat(`}`, 10000) + `}`,
			errMaxDepth: true,
		},
	}

	for _, tt := range tCases {
		t.Run(tt.name, func(t *testing.T) {
			_, err := Scan([]byte(tt.data))
			if !tt.errMaxDepth {
				if err != nil {
					t.Errorf("unexpected error: %v", err)
				}
			} else {
				if err == nil {
					t.Errorf("expected error containing 'exceeded max depth', got none")
				} else if !strings.Contains(err.Error(), "exceeded max depth") {
					t.Errorf("expected error containing 'exceeded max depth', got: %v", err)
				}
			}
		})
	}
}