| 1 |
package internal |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
type mSA map[string]any // To minimize typing the long name |
| 12 |
|
| 13 |
// implementsUnmarshalJSON is a minimalistic type used to detect that |
| 14 |
// paranoidUnmarshalJSONObject uses the json.Unmarshaler interface of resolved |
| 15 |
// pointers. |
| 16 |
type implementsUnmarshalJSON bool |
| 17 |
|
| 18 |
// Compile-time check that Policy implements json.Unmarshaler. |
| 19 |
var _ json.Unmarshaler = (*implementsUnmarshalJSON)(nil) |
| 20 |
|
| 21 |
func (dest *implementsUnmarshalJSON) UnmarshalJSON(data []byte) error { |
| 22 |
_ = data // We don't care, not really. |
| 23 |
*dest = true // Mark handler as called |
| 24 |
return nil |
| 25 |
} |
| 26 |
|
| 27 |
func TestParanoidUnmarshalJSONObject(t *testing.T) { |
| 28 |
type testStruct struct { |
| 29 |
A string |
| 30 |
B int |
| 31 |
} |
| 32 |
ts := testStruct{} |
| 33 |
var unmarshalJSONCalled implementsUnmarshalJSON |
| 34 |
tsResolver := func(key string) any { |
| 35 |
switch key { |
| 36 |
case "a": |
| 37 |
return &ts.A |
| 38 |
case "b": |
| 39 |
return &ts.B |
| 40 |
case "implementsUnmarshalJSON": |
| 41 |
return &unmarshalJSONCalled |
| 42 |
default: |
| 43 |
return nil |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// Empty object |
| 48 |
ts = testStruct{} |
| 49 |
err := ParanoidUnmarshalJSONObject([]byte(`{}`), tsResolver) |
| 50 |
require.NoError(t, err) |
| 51 |
assert.Equal(t, testStruct{}, ts) |
| 52 |
|
| 53 |
// Success |
| 54 |
ts = testStruct{} |
| 55 |
err = ParanoidUnmarshalJSONObject([]byte(`{"a":"x", "b":2}`), tsResolver) |
| 56 |
require.NoError(t, err) |
| 57 |
assert.Equal(t, testStruct{A: "x", B: 2}, ts) |
| 58 |
|
| 59 |
// json.Unmarshaler is used for decoding values |
| 60 |
ts = testStruct{} |
| 61 |
unmarshalJSONCalled = implementsUnmarshalJSON(false) |
| 62 |
err = ParanoidUnmarshalJSONObject([]byte(`{"implementsUnmarshalJSON":true}`), tsResolver) |
| 63 |
require.NoError(t, err) |
| 64 |
assert.Equal(t, unmarshalJSONCalled, implementsUnmarshalJSON(true)) |
| 65 |
|
| 66 |
// Various kinds of invalid input |
| 67 |
for _, input := range []string{ |
| 68 |
``, // Empty input |
| 69 |
`&`, // Entirely invalid JSON |
| 70 |
`1`, // Not an object |
| 71 |
`{&}`, // Invalid key JSON |
| 72 |
`{1:1}`, // Key not a string |
| 73 |
`{"b":1, "b":1}`, // Duplicate key |
| 74 |
`{"thisdoesnotexist":1}`, // Key rejected by resolver |
| 75 |
`{"a":&}`, // Invalid value JSON |
| 76 |
`{"a":1}`, // Type mismatch |
| 77 |
`{"a":"value"}{}`, // Extra data after object |
| 78 |
} { |
| 79 |
ts = testStruct{} |
| 80 |
err := ParanoidUnmarshalJSONObject([]byte(input), tsResolver) |
| 81 |
assert.Error(t, err, input) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestParanoidUnmarshalJSONObjectExactFields(t *testing.T) { |
| 86 |
var stringValue string |
| 87 |
var float64Value float64 |
| 88 |
var rawValue json.RawMessage |
| 89 |
var unmarshallCalled implementsUnmarshalJSON |
| 90 |
exactFields := map[string]any{ |
| 91 |
"string": &stringValue, |
| 92 |
"float64": &float64Value, |
| 93 |
"raw": &rawValue, |
| 94 |
"unmarshaller": &unmarshallCalled, |
| 95 |
} |
| 96 |
|
| 97 |
// Empty object |
| 98 |
err := ParanoidUnmarshalJSONObjectExactFields([]byte(`{}`), map[string]any{}) |
| 99 |
require.NoError(t, err) |
| 100 |
|
| 101 |
// Success |
| 102 |
err = ParanoidUnmarshalJSONObjectExactFields([]byte(`{"string": "a", "float64": 3.5, "raw": {"a":"b"}, "unmarshaller": true}`), exactFields) |
| 103 |
require.NoError(t, err) |
| 104 |
assert.Equal(t, "a", stringValue) |
| 105 |
assert.Equal(t, 3.5, float64Value) |
| 106 |
assert.Equal(t, json.RawMessage(`{"a":"b"}`), rawValue) |
| 107 |
assert.Equal(t, implementsUnmarshalJSON(true), unmarshallCalled) |
| 108 |
|
| 109 |
// Various kinds of invalid input |
| 110 |
for _, input := range []string{ |
| 111 |
``, // Empty input |
| 112 |
`&`, // Entirely invalid JSON |
| 113 |
`1`, // Not an object |
| 114 |
`{&}`, // Invalid key JSON |
| 115 |
`{1:1}`, // Key not a string |
| 116 |
`{"string": "a", "string": "a", "float64": 3.5, "raw": {"a":"b"}, "unmarshaller": true}`, // Duplicate key |
| 117 |
`{"string": "a", "float64": 3.5, "raw": {"a":"b"}, "unmarshaller": true, "thisisunknown", 1}`, // Unknown key |
| 118 |
`{"string": &, "float64": 3.5, "raw": {"a":"b"}, "unmarshaller": true}`, // Invalid value JSON |
| 119 |
`{"string": 1, "float64": 3.5, "raw": {"a":"b"}, "unmarshaller": true}`, // Type mismatch |
| 120 |
`{"string": "a", "float64": 3.5, "raw": {"a":"b"}, "unmarshaller": true}{}`, // Extra data after object |
| 121 |
} { |
| 122 |
err := ParanoidUnmarshalJSONObjectExactFields([]byte(input), exactFields) |
| 123 |
assert.Error(t, err, input) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Return the result of modifying validJSON with fn |
| 128 |
func modifiedJSON(t *testing.T, validJSON []byte, modifyFn func(mSA)) []byte { |
| 129 |
var tmp mSA |
| 130 |
err := json.Unmarshal(validJSON, &tmp) |
| 131 |
require.NoError(t, err) |
| 132 |
|
| 133 |
modifyFn(tmp) |
| 134 |
|
| 135 |
modifiedJSON, err := json.Marshal(tmp) |
| 136 |
require.NoError(t, err) |
| 137 |
return modifiedJSON |
| 138 |
} |