Skip to content

Commit 86c579d

Browse files
tbkkathomasvl
authored andcommitted
Add more tests to exercise boundary conditions parsing different size integers
1 parent df784a4 commit 86c579d

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

Tests/SwiftProtobufTests/Test_Map_JSON.swift

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ class Test_Map_JSON: XCTestCase, PBTestHelpers {
4141
// Decode should work same regardless of order
4242
assertJSONDecodeSucceeds("{\"mapInt32Int32\":{\"1\":2, \"3\":4}}") {$0.mapInt32Int32 == [1:2, 3:4]}
4343
assertJSONDecodeSucceeds("{\"mapInt32Int32\":{\"3\":4,\"1\":2}}") {$0.mapInt32Int32 == [1:2, 3:4]}
44+
// In range values succeed
45+
assertJSONDecodeSucceeds("{\"mapInt32Int32\":{\"2147483647\":2147483647}}") {
46+
$0.mapInt32Int32 == [2147483647:2147483647]
47+
}
48+
assertJSONDecodeSucceeds("{\"mapInt32Int32\":{\"-2147483648\":-2147483648}}") {
49+
$0.mapInt32Int32 == [-2147483648:-2147483648]
50+
}
51+
// Out of range values fail
52+
assertJSONDecodeFails("{\"mapInt32Int32\":{\"2147483647\":2147483648}}")
53+
assertJSONDecodeFails("{\"mapInt32Int32\":{\"2147483648\":2147483647}}")
54+
assertJSONDecodeFails("{\"mapInt32Int32\":{\"-2147483649\":2147483647}}")
55+
assertJSONDecodeFails("{\"mapInt32Int32\":{\"2147483647\":-2147483649}}")
4456
// JSON RFC does not allow trailing comma
4557
assertJSONDecodeFails("{\"mapInt32Int32\":{\"3\":4,\"1\":2,}}")
4658
// Int values should support being quoted or unquoted
@@ -61,54 +73,137 @@ class Test_Map_JSON: XCTestCase, PBTestHelpers {
6173
assertJSONEncode("{\"mapInt64Int64\":{\"1\":\"2\"}}") {(o: inout MessageTestType) in
6274
o.mapInt64Int64 = [1:2]
6375
}
76+
assertJSONEncode("{\"mapInt64Int64\":{\"9223372036854775807\":\"-9223372036854775808\"}}") {(o: inout MessageTestType) in
77+
o.mapInt64Int64 = [9223372036854775807: -9223372036854775808]
78+
}
79+
assertJSONDecodeSucceeds("{\"mapInt64Int64\":{\"9223372036854775807\":-9223372036854775808}}") {
80+
$0.mapInt64Int64 == [9223372036854775807: -9223372036854775808]
81+
}
82+
assertJSONDecodeFails("{\"mapInt64Int64\":{\"9223372036854775807\":9223372036854775808}}")
6483
}
6584

6685
func testMapUInt32UInt32() throws {
6786
assertJSONEncode("{\"mapUint32Uint32\":{\"1\":2}}") {(o: inout MessageTestType) in
6887
o.mapUint32Uint32 = [1:2]
6988
}
89+
assertJSONDecodeFails("{\"mapUint32Uint32\":{\"1\":-2}}")
90+
assertJSONDecodeFails("{\"mapUint32Uint32\":{\"-1\":2}}")
91+
assertJSONDecodeFails("{\"mapUint32Uint32\":{1:2}}")
92+
assertJSONDecodeSucceeds("{\"mapUint32Uint32\":{\"1\":\"2\"}}") {
93+
$0.mapUint32Uint32 == [1:2]
94+
}
7095
}
7196

7297
func testMapUInt64UInt64() throws {
7398
assertJSONEncode("{\"mapUint64Uint64\":{\"1\":\"2\"}}") {(o: inout MessageTestType) in
7499
o.mapUint64Uint64 = [1:2]
75100
}
101+
assertJSONEncode("{\"mapUint64Uint64\":{\"1\":\"18446744073709551615\"}}") {(o: inout MessageTestType) in
102+
o.mapUint64Uint64 = [1:18446744073709551615 as UInt64]
103+
}
104+
assertJSONDecodeSucceeds("{\"mapUint64Uint64\":{\"1\":18446744073709551615}}") {
105+
$0.mapUint64Uint64 == [1:18446744073709551615 as UInt64]
106+
}
107+
assertJSONDecodeFails("{\"mapUint64Uint64\":{\"1\":\"18446744073709551616\"}}")
108+
assertJSONDecodeFails("{\"mapUint64Uint64\":{1:\"18446744073709551615\"}}")
76109
}
77110

78111
func testMapSInt32SInt32() throws {
79112
assertJSONEncode("{\"mapSint32Sint32\":{\"1\":2}}") {(o: inout MessageTestType) in
80113
o.mapSint32Sint32 = [1:2]
81114
}
115+
assertJSONDecodeSucceeds("{\"mapSint32Sint32\":{\"1\":\"-2\"}}") {
116+
$0.mapSint32Sint32 == [1:-2]
117+
}
118+
assertJSONDecodeFails("{\"mapSint32Sint32\":{1:-2}}")
119+
// In range values succeed
120+
assertJSONDecodeSucceeds("{\"mapSint32Sint32\":{\"2147483647\":2147483647}}") {
121+
$0.mapSint32Sint32 == [2147483647:2147483647]
122+
}
123+
assertJSONDecodeSucceeds("{\"mapSint32Sint32\":{\"-2147483648\":-2147483648}}") {
124+
$0.mapSint32Sint32 == [-2147483648:-2147483648]
125+
}
126+
// Out of range values fail
127+
assertJSONDecodeFails("{\"mapSint32Sint32\":{\"2147483647\":2147483648}}")
128+
assertJSONDecodeFails("{\"mapSint32Sint32\":{\"2147483648\":2147483647}}")
129+
assertJSONDecodeFails("{\"mapSint32Sint32\":{\"-2147483649\":2147483647}}")
130+
assertJSONDecodeFails("{\"mapSint32Sint32\":{\"2147483647\":-2147483649}}")
82131
}
83132

84133
func testMapSInt64SInt64() throws {
85134
assertJSONEncode("{\"mapSint64Sint64\":{\"1\":\"2\"}}") {(o: inout MessageTestType) in
86135
o.mapSint64Sint64 = [1:2]
87136
}
137+
assertJSONEncode("{\"mapSint64Sint64\":{\"9223372036854775807\":\"-9223372036854775808\"}}") {(o: inout MessageTestType) in
138+
o.mapSint64Sint64 = [9223372036854775807: -9223372036854775808]
139+
}
140+
assertJSONDecodeSucceeds("{\"mapSint64Sint64\":{\"9223372036854775807\":-9223372036854775808}}") {
141+
$0.mapSint64Sint64 == [9223372036854775807: -9223372036854775808]
142+
}
143+
assertJSONDecodeFails("{\"mapSint64Sint64\":{\"9223372036854775807\":9223372036854775808}}")
88144
}
89145

90146
func testFixed32Fixed32() throws {
91147
assertJSONEncode("{\"mapFixed32Fixed32\":{\"1\":2}}") {(o: inout MessageTestType) in
92148
o.mapFixed32Fixed32 = [1:2]
93149
}
150+
assertJSONEncode("{\"mapFixed32Fixed32\":{\"0\":0}}") {(o: inout MessageTestType) in
151+
o.mapFixed32Fixed32 = [0:0]
152+
}
153+
// In range values succeed
154+
assertJSONDecodeSucceeds("{\"mapFixed32Fixed32\":{\"4294967295\":4294967295}}") {
155+
$0.mapFixed32Fixed32 == [4294967295:4294967295]
156+
}
157+
// Out of range values fail
158+
assertJSONDecodeFails("{\"mapFixed32Fixed32\":{\"4294967295\":4294967296}}")
159+
assertJSONDecodeFails("{\"mapFixed32Fixed32\":{\"4294967296\":4294967295}}")
160+
assertJSONDecodeFails("{\"mapFixed32Fixed32\":{\"-1\":4294967295}}")
161+
assertJSONDecodeFails("{\"mapFixed32Fixed32\":{\"4294967295\":-1}}")
94162
}
95163

96164
func testFixed64Fixed64() throws {
97165
assertJSONEncode("{\"mapFixed64Fixed64\":{\"1\":\"2\"}}") {(o: inout MessageTestType) in
98166
o.mapFixed64Fixed64 = [1:2]
99167
}
168+
assertJSONEncode("{\"mapFixed64Fixed64\":{\"1\":\"18446744073709551615\"}}") {(o: inout MessageTestType) in
169+
o.mapFixed64Fixed64 = [1:18446744073709551615 as UInt64]
170+
}
171+
assertJSONDecodeSucceeds("{\"mapFixed64Fixed64\":{\"1\":18446744073709551615}}") {
172+
$0.mapFixed64Fixed64 == [1:18446744073709551615 as UInt64]
173+
}
174+
assertJSONDecodeFails("{\"mapFixed64Fixed64\":{\"1\":\"18446744073709551616\"}}")
175+
assertJSONDecodeFails("{\"mapFixed64Fixed64\":{1:\"18446744073709551615\"}}")
100176
}
101177

102178
func testSFixed32SFixed32() throws {
103179
assertJSONEncode("{\"mapSfixed32Sfixed32\":{\"1\":2}}") {(o: inout MessageTestType) in
104180
o.mapSfixed32Sfixed32 = [1:2]
105181
}
182+
// In range values succeed
183+
assertJSONDecodeSucceeds("{\"mapSfixed32Sfixed32\":{\"2147483647\":2147483647}}") {
184+
$0.mapSfixed32Sfixed32 == [2147483647:2147483647]
185+
}
186+
assertJSONDecodeSucceeds("{\"mapSfixed32Sfixed32\":{\"-2147483648\":-2147483648}}") {
187+
$0.mapSfixed32Sfixed32 == [-2147483648:-2147483648]
188+
}
189+
// Out of range values fail
190+
assertJSONDecodeFails("{\"mapSfixed32Sfixed32\":{\"2147483647\":2147483648}}")
191+
assertJSONDecodeFails("{\"mapSfixed32Sfixed32\":{\"2147483648\":2147483647}}")
192+
assertJSONDecodeFails("{\"mapSfixed32Sfixed32\":{\"-2147483649\":2147483647}}")
193+
assertJSONDecodeFails("{\"mapSfixed32Sfixed32\":{\"2147483647\":-2147483649}}")
106194
}
107195

108196
func testSFixed64SFixed64() throws {
109197
assertJSONEncode("{\"mapSfixed64Sfixed64\":{\"1\":\"2\"}}") {(o: inout MessageTestType) in
110198
o.mapSfixed64Sfixed64 = [1:2]
111199
}
200+
assertJSONEncode("{\"mapSfixed64Sfixed64\":{\"9223372036854775807\":\"-9223372036854775808\"}}") {(o: inout MessageTestType) in
201+
o.mapSfixed64Sfixed64 = [9223372036854775807: -9223372036854775808]
202+
}
203+
assertJSONDecodeSucceeds("{\"mapSfixed64Sfixed64\":{\"9223372036854775807\":-9223372036854775808}}") {
204+
$0.mapSfixed64Sfixed64 == [9223372036854775807: -9223372036854775808]
205+
}
206+
assertJSONDecodeFails("{\"mapSfixed64Sfixed64\":{\"9223372036854775807\":9223372036854775808}}")
112207
}
113208

114209
func test_mapInt32Float() {
@@ -119,6 +214,10 @@ class Test_Map_JSON: XCTestCase, PBTestHelpers {
119214
assertJSONEncode("{\"mapInt32Float\":{\"1\":1}}") {
120215
$0.mapInt32Float[1] = Float(1.0)
121216
}
217+
218+
assertJSONDecodeSucceeds("{\"mapInt32Float\":{\"1\":3.141592}}") {
219+
$0.mapInt32Float[1] == 3.141592 as Float
220+
}
122221
}
123222

124223
func test_mapInt32Double() {
@@ -130,6 +229,9 @@ class Test_Map_JSON: XCTestCase, PBTestHelpers {
130229
$0.mapInt32Double[1] = Double(1.0)
131230
}
132231

232+
assertJSONDecodeSucceeds("{\"mapInt32Double\":{\"1\":3.141592}}") {
233+
$0.mapInt32Double[1] == 3.141592
234+
}
133235
}
134236

135237
func test_mapBoolBool() {

0 commit comments

Comments
 (0)