-
Notifications
You must be signed in to change notification settings - Fork 472
/
Copy pathTest_BinaryDecodingOptions.swift
327 lines (302 loc) · 11.8 KB
/
Test_BinaryDecodingOptions.swift
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Tests/SwiftProtobufTests/Test_BinaryDecodingOptions.swift - Various Binary tests
//
// Copyright (c) 2014 - 2017 Apple Inc. and the project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See LICENSE.txt for license information:
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
//
// -----------------------------------------------------------------------------
///
/// Test for the use of BinaryDecodingOptions
///
// -----------------------------------------------------------------------------
import Foundation
import SwiftProtobuf
import XCTest
final class Test_BinaryDecodingOptions: XCTestCase {
func testMessageDepthLimit() throws {
let tests: [([UInt8], any Message.Type, (any ExtensionMap)?, [(Int, Bool)])] = [
// Input, (Limit, success/failure)
// Messages within messages: // outer is msg 1
// child { // sub msg 2
// child { // sub msg 3
// payload { // sub msg 4
// optional_int32: 99
// }
// }
// }
(
[10, 6, 10, 4, 18, 2, 8, 99],
SwiftProtoTesting_NestedTestAllTypes.self,
nil, // No Extensions
[
(10, true),
(4, true),
(3, false),
(2, false),
]
),
// Group within messages: // outer is msg 1
// child { // sub msg 2
// child { // sub msg 3
// payload { // sub msg 4
// OptionalGroup { // sub msg 5
// a: 98
// }
// }
// }
// }
(
[10, 11, 10, 9, 18, 7, 131, 1, 136, 1, 98, 132, 1],
SwiftProtoTesting_NestedTestAllTypes.self,
nil, // No Extensions
[
(10, true),
(5, true),
(4, false),
(3, false),
]
),
// Nesting of unknown groups: // outer is msg 1
// 4 { // sub msg 2
// 4 { // sub msg 3
// 4 { // sub msg 4
// 1: 1
// }
// }
// }
// 35 = 0b100011 -> field 4/start group
// 8, 1 -> field 1/varint, value of 1
// 36 = 0b100100 -> field 4/end group
(
[35, 35, 35, 8, 1, 36, 36, 36],
SwiftProtoTesting_TestEmptyMessage.self,
nil, // No Extensions
[
(10, true),
(4, true),
(3, false),
(2, false),
]
),
// Nested message are on the wire as length delimited, so no depth comes into
// play when they are unknown message fields.
// Limit applies to message extension fields: // outer is msg 1
// [swift_proto_testing.optional_nested_message_extension] { // sub msg 2
// bb: 1
// }
(
[146, 1, 2, 8, 1],
SwiftProtoTesting_TestAllExtensions.self,
SwiftProtoTesting_Unittest_Extensions,
[
(10, true),
(2, true),
(1, false),
]
),
// Limit applies to group extension fields: // outer is msg 1
// [swift_proto_testing.optionalgroup_extension] { // sub msg 2
// a: 1
// }
(
[131, 1, 136, 1, 1, 132, 1],
SwiftProtoTesting_TestAllExtensions.self,
SwiftProtoTesting_Unittest_Extensions,
[
(10, true),
(2, true),
(1, false),
]
),
]
for (i, (binaryInput, messageType, extensions, testCases)) in tests.enumerated() {
for (limit, expectSuccess) in testCases {
do {
var options = BinaryDecodingOptions()
options.messageDepthLimit = limit
let _ = try messageType.init(
serializedBytes: binaryInput,
extensions: extensions,
options: options
)
if !expectSuccess {
XCTFail("Should not have succeed, pass: \(i), limit: \(limit)")
}
} catch BinaryDecodingError.messageDepthLimit {
if expectSuccess {
XCTFail("Decode failed because of limit, but should *NOT* have, pass: \(i), limit: \(limit)")
} else {
// Nothing, this is what was expected.
}
} catch let e {
XCTFail("Decode failed (pass: \(i), limit: \(limit) with unexpected error: \(e)")
}
}
}
}
func testDiscardingUnknownFields() throws {
// All tests decode once confirming things show up in unknowns, then decode
// a second time confirming they were dropped.
var discardOptions = BinaryDecodingOptions()
discardOptions.discardUnknownFields = true
// Unknown fields at the root of a message:
// 2: 1
// 3: 0x0000000000000002
// 4: "\003"
// 5 {
// 7: 4
// }
// 6: 0x00000005
let inputCurrentLevel: [UInt8] = [
// Field 1, varint of 1
16, 1,
// Field 2, fixed64 of 2
25, 2, 0, 0, 0, 0, 0, 0, 0,
// Field 3, length delimited of 3
34, 1, 3,
// Field 4, group (start, field 6 varinit of 4, end)
43, 56, 4, 44,
// Field 5, fixed32 of 5
53, 5, 0, 0, 0,
]
do {
let msg1 = try SwiftProtoTesting_TestEmptyMessage(serializedBytes: inputCurrentLevel)
XCTAssertEqual(Array(msg1.unknownFields.data), inputCurrentLevel)
let msg2 = try SwiftProtoTesting_TestEmptyMessage(
serializedBytes: inputCurrentLevel,
options: discardOptions
)
XCTAssertTrue(msg2.unknownFields.data.isEmpty)
}
// Unknown fields nested within a message field:
// optional_nested_message {
// 2: 1
// 3: 0x0000000000000002
// 4: "\003"
// 5 {
// 7: 4
// }
// 6: 0x00000005
// }
let inputSubMessage: [UInt8] =
[
// Field 18, length of data, plus the data
146, 1, UInt8(inputCurrentLevel.count),
] + inputCurrentLevel
do {
let msg1 = try SwiftProtoTesting_TestAllTypes(serializedBytes: inputSubMessage)
XCTAssertTrue(msg1.unknownFields.data.isEmpty)
XCTAssertEqual(Array(msg1.optionalNestedMessage.unknownFields.data), inputCurrentLevel)
let msg2 = try SwiftProtoTesting_TestAllTypes(
serializedBytes: inputSubMessage,
options: discardOptions
)
XCTAssertTrue(msg2.unknownFields.data.isEmpty)
XCTAssertTrue(msg2.optionalNestedMessage.unknownFields.data.isEmpty)
}
// Unknown fields nested within a message extension field:
// [swift_proto_testing.optional_nested_message_extension] {
// 2: 1
// 3: 0x0000000000000002
// 4: "\003"
// 5 {
// 7: 4
// }
// 6: 0x00000005
// }
do {
let msg1 = try SwiftProtoTesting_TestAllExtensions(
serializedBytes: inputSubMessage,
extensions: SwiftProtoTesting_Unittest_Extensions
)
XCTAssertTrue(msg1.unknownFields.data.isEmpty)
XCTAssertEqual(
Array(msg1.SwiftProtoTesting_optionalNestedMessageExtension.unknownFields.data),
inputCurrentLevel
)
let msg2 = try SwiftProtoTesting_TestAllExtensions(
serializedBytes: inputSubMessage,
extensions: SwiftProtoTesting_Unittest_Extensions,
options: discardOptions
)
XCTAssertTrue(msg2.unknownFields.data.isEmpty)
XCTAssertTrue(msg2.SwiftProtoTesting_optionalNestedMessageExtension.unknownFields.data.isEmpty)
}
// Unknown fields nested within a group field:
// OptionalGroup {
// 2: 1
// 3: 0x0000000000000002
// 4: "\003"
// 5 {
// 7: 4
// }
// 6: 0x00000005
// }
let inputGroup: [UInt8] =
[
// Field 16, start_group
131, 1,
] + inputCurrentLevel + [
// Field 16, end_group
132, 1,
]
do {
let msg1 = try SwiftProtoTesting_TestAllExtensions(
serializedBytes: inputGroup,
extensions: SwiftProtoTesting_Unittest_Extensions
)
XCTAssertTrue(msg1.unknownFields.data.isEmpty)
XCTAssertEqual(
Array(msg1.SwiftProtoTesting_optionalGroupExtension.unknownFields.data),
inputCurrentLevel
)
let msg2 = try SwiftProtoTesting_TestAllExtensions(
serializedBytes: inputGroup,
extensions: SwiftProtoTesting_Unittest_Extensions,
options: discardOptions
)
XCTAssertTrue(msg2.unknownFields.data.isEmpty)
XCTAssertTrue(msg2.SwiftProtoTesting_optionalGroupExtension.unknownFields.data.isEmpty)
}
// Unknown fields nested within a group extension field:
// [swift_proto_testing.optionalgroup_extension] {
// 2: 1
// 3: 0x0000000000000002
// 4: "\003"
// 5 {
// 7: 4
// }
// 6: 0x00000005
// }
do {
let msg1 = try SwiftProtoTesting_TestAllTypes(serializedBytes: inputGroup)
XCTAssertTrue(msg1.unknownFields.data.isEmpty)
XCTAssertEqual(Array(msg1.optionalGroup.unknownFields.data), inputCurrentLevel)
let msg2 = try SwiftProtoTesting_TestAllTypes(
serializedBytes: inputGroup,
options: discardOptions
)
XCTAssertTrue(msg2.unknownFields.data.isEmpty)
XCTAssertTrue(msg2.optionalGroup.unknownFields.data.isEmpty)
}
// An unknown enum value. Closed enums uses a different code path and
// end up in unknown fields, so ensure that is honoring the option.
// Test data:
// optional_nested_enum: 13
let inputUnknownEnum: [UInt8] = [
// Field 21, varint
168, 1, 13,
]
do {
let msg1 = try SwiftProtoTesting_TestAllTypes(serializedBytes: inputUnknownEnum)
XCTAssertEqual(Array(msg1.unknownFields.data), inputUnknownEnum)
let msg2 = try SwiftProtoTesting_TestAllTypes(
serializedBytes: inputUnknownEnum,
options: discardOptions
)
XCTAssertTrue(msg2.unknownFields.data.isEmpty)
}
}
}