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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization.json
import kotlinx.serialization.*
import kotlinx.serialization.test.*
import kotlin.test.*
class JsonParserFailureModesTest : JsonTestBase() {
@Serializable
data class Holder(
val id: Long
)
@Test
fun testFailureModes() = parametrizedTest {
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString(
Holder.serializer(),
"""{"id": "}""",
it
)
}
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString(
Holder.serializer(),
"""{"id": ""}""",
it
)
}
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString(
Holder.serializer(),
"""{"id":a}""",
it
)
}
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString(
Holder.serializer(),
"""{"id":2.0}""",
it
)
}
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString(
Holder.serializer(),
"""{"id2":2}""",
it
)
}
// 9223372036854775807 is Long.MAX_VALUE
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString(
Holder.serializer(),
"""{"id":${Long.MAX_VALUE}""" + "00" + "}",
it
)
}
// -9223372036854775808 is Long.MIN_VALUE
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString(
Holder.serializer(),
"""{"id":9223372036854775808}""",
it
)
}
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString(Holder.serializer(), """{"id"}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString(Holder.serializer(), """{"id}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString(Holder.serializer(), """{"i}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString(Holder.serializer(), """{"}""", it) }
assertFailsWithMissingField { default.decodeFromString(Holder.serializer(), """{}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString(Holder.serializer(), """{""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString(Holder.serializer(), """}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString(Holder.serializer(), """{""", it) }
}
@Serializable
class BooleanHolder(val b: Boolean)
@Test
fun testBoolean() = parametrizedTest {
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString(
BooleanHolder.serializer(),
"""{"b": fals}""",
it
)
}
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString(
BooleanHolder.serializer(),
"""{"b": 123}""",
it
)
}
}
@Serializable
class PrimitiveHolder(
val b: Byte = 0, val s: Short = 0, val i: Int = 0
)
@Test
fun testOverflow() = parametrizedTest {
// Byte overflow
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<PrimitiveHolder>("""{"b": 128}""", it) }
// Short overflow
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<PrimitiveHolder>("""{"s": 32768}""", it) }
// Int overflow
assertFailsWithSerial("JsonDecodingException") {
default.decodeFromString<PrimitiveHolder>(
"""{"i": 2147483648}""",
it
)
}
}
@Test
fun testNoOverflow() = parametrizedTest {
default.decodeFromString<PrimitiveHolder>("""{"b": ${Byte.MAX_VALUE}}""", it)
default.decodeFromString<PrimitiveHolder>("""{"b": ${Byte.MIN_VALUE}}""", it)
default.decodeFromString<PrimitiveHolder>("""{"s": ${Short.MAX_VALUE}}""", it)
default.decodeFromString<PrimitiveHolder>("""{"s": ${Short.MIN_VALUE}}""", it)
default.decodeFromString<PrimitiveHolder>("""{"i": ${Int.MAX_VALUE}}""", it)
default.decodeFromString<PrimitiveHolder>("""{"i": ${Int.MIN_VALUE}}""", it)
default.decodeFromString<Holder>("""{"id": ${Long.MIN_VALUE.toString()}}""", it)
default.decodeFromString<Holder>("""{"id": ${Long.MAX_VALUE.toString()}}""", it)
}
@Test
fun testInvalidNumber() = parametrizedTest {
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<Holder>("""{"id":-}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<Holder>("""{"id":+}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<Holder>("""{"id":--}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<Holder>("""{"id":1-1}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<Holder>("""{"id":0-1}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<Holder>("""{"id":0-}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<Holder>("""{"id":a}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<Holder>("""{"id":-a}""", it) }
}
@Serializable
data class BooleanWrapper(val b: Boolean)
@Serializable
data class StringWrapper(val s: String)
@Test
fun testUnexpectedNull() = parametrizedTest {
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<BooleanWrapper>("""{"b":{"b":"b"}}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<BooleanWrapper>("""{"b":null}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<StringWrapper>("""{"s":{"s":"s"}}""", it) }
assertFailsWithSerial("JsonDecodingException") { default.decodeFromString<StringWrapper>("""{"s":null}""", it) }
}
}
|