blob: 4c4c0a6a88023fb8e15dd5b4326d0c512f6fc4a0 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003 Copyright (c) 2018-2021, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 ============================================================================*/
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080032
33#ifndef __QCBOR__qcbort_decode_tests__
34#define __QCBOR__qcbort_decode_tests__
35
Laurence Lundbladec5fef682020-01-25 11:38:45 -080036#include <stdint.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080037
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080038/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080039 Notes:
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080040
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080041 - All the functions in qcbor_decode.h are called once in the aggregation
42 of all the tests below.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080043
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080044 - All the types that are supported are given as input and parsed by these tests
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080045
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080046 - There is some hostile input such as invalid lengths and CBOR too complex
47 and types this parser doesn't handle
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080048
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080049 */
50
51
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052/*
53 Parse a well-known set of integers including those around the boundaries and
54 make sure the expected values come out
55 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080056int32_t IntegerValuesParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057
58
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080059/*
60 Decode a simple CBOR encoded array and make sure it returns all the correct values.
61 This is a decode test.
62 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080063int32_t SimpleArrayTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064
Laurence Lundbladea65b4822019-09-08 12:17:03 -070065
Laurence Lundblade9916b1b2019-09-07 22:33:25 -070066/*
67 Tests with empty maps and arrays
68 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080069int32_t EmptyMapsAndArraysTest(void);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -070070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080071
72/*
73 Make sure a maximally deep array can be parsed and that the
74 reported nesting level is correct. This uses test vector
75 of CBOR encoded data with a depth of 10. This a parse test.
76 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080077int32_t ParseDeepArrayTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080078
79
80/*
81 See that the correct error is reported when parsing
82 an array of depth 11, one too large.
83 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080084int32_t ParseTooDeepArrayTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080085
86
87/*
88 Try to parse some legit CBOR types that this parsers
89 doesn't support.
90 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080091int32_t UnsupportedCBORDecodeTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092
93
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080094/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080095 This takes the encoded CBOR integers used in the above test and parses
96 it over and over with one more byte less each time. It should fail
97 every time on incorrect CBOR input. This is a hostile input decode test.
98 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080099int32_t ShortBufferParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800100
101
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800102/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800103 Same as ShortBufferParseTest, but with a different encoded CBOR input.
104 It is another hostile input test
105 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800106int32_t ShortBufferParseTest2(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800107
108
109/*
110 Parses the somewhat complicated CBOR MAP and makes sure all the correct
111 values parse out. About 15 values are tested. This is a decode test.
112 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800113int32_t ParseMapTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114
Michael Richardson87de9af2021-02-18 23:13:31 -0500115/*
116 Parses a map that contains a zero-length map as value.
117*/
118int32_t ParseEmptyMapInMapTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900120/*
Laurence Lundbladea65b4822019-09-08 12:17:03 -0700121Test the decoder mode where maps are treated as arrays.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900122 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800123int32_t ParseMapAsArrayTest(void);
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900124
125
Laurence Lundbladea65b4822019-09-08 12:17:03 -0700126/*
127 Test parsing of some simple values like true, false, null...
128 */
Laurence Lundblade3888f002024-06-12 21:20:56 -0700129int32_t SimpleValueDecodeTests(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800130
131
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700132/*
133 This tests all the not-well-formed CBOR from the CBOR RFC.
134 (This is the CBORbis RFC which is not yet published at the
135 time this test was added).
136 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800137int32_t NotWellFormedTests(void);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700138
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800139
140/*
141 Tests a number of failure cases on bad CBOR to get the right error code
142 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800143int32_t DecodeFailureTests(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144
145
146/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800147 Parses all possible inputs that are two bytes long. Main point
148 is that the test doesn't crash as it doesn't evaluate the
149 input for correctness in any way.
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800150
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800151 (Parsing all possible 3 byte strings takes too long on all but
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800152 very fast machines).
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800153 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800154int32_t ComprehensiveInputTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800155
156
157/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800158 Parses all possible inputs that are four bytes long. Main point
159 is that the test doesn't crash as it doesn't evaluate the
160 input for correctness in any way. This runs very slow, so it
161 is only practical as a once-in-a-while regression test on
162 fast machines.
163 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800164int32_t BigComprehensiveInputTest(void);
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800165
166
167/*
Laurence Lundbladec7114722020-08-13 05:11:40 -0700168 Test the date types -- epoch and strings
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800170int32_t DateParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171
172
173/*
Laurence Lundbladec7114722020-08-13 05:11:40 -0700174 Test spiffy date decoding functions
175 */
176int32_t SpiffyDateDecodeTest(void);
177
178
179/*
Laurence Lundblade9b334962020-08-27 10:55:53 -0700180 Test decode of CBOR tagging like the CBOR magic number and many others.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800182int32_t OptTagParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183
184
185/*
186 Parse some big numbers, positive and negative
187 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800188int32_t BignumParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189
190
Laurence Lundbladea65b4822019-09-08 12:17:03 -0700191/*
192 Test of mode where only string labels are allowed
193 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800194int32_t StringDecoderModeFailTest(void);
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800195
196
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197/*
198 Parse some nested maps
199 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800200int32_t NestedMapTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530202
203/*
204 Parse maps with indefinite lengths
205 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800206int32_t NestedMapTestIndefLen(void);
Laurence Lundblade742df4a2018-10-13 20:07:17 +0800207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530209/*
210 Parse some maps and arrays with indefinite lengths.
211 Includes some error cases.
212 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800213int32_t IndefiniteLengthArrayMapTest(void);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800214
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530215
216/*
217 Parse indefinite length strings. Uses
218 MemPool. Includes error cases.
219 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800220int32_t IndefiniteLengthStringTest(void);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800221
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530222
223/*
224 Test deep nesting of indefinite length
225 maps and arrays including too deep.
226 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800227int32_t IndefiniteLengthNestTest(void);
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530228
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530229
230/*
231 Test parsing strings were all strings, not
232 just indefinite length strings, are
233 allocated. Includes error test cases.
234 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800235int32_t AllocAllStringsTest(void);
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530236
Laurence Lundblade0155b622018-10-12 20:04:37 +0800237
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530238/*
239 Direct test of MemPool string allocator
240 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800241int32_t MemPoolTest(void);
Laurence Lundblade0155b622018-10-12 20:04:37 +0800242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800244/*
245 Test the setting up of an external string allocator.
246 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800247int32_t SetUpAllocatorTest(void);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800248
249
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -0700250#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -0800251/*
252 Test decoding of decimal fractions and big floats, both of which are
253 made up of an exponent and mantissa.
254 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800255int32_t ExponentAndMantissaDecodeTests(void);
Laurence Lundblade59289e52019-12-30 13:44:37 -0800256
257
258/*
259 Hostile input tests for decimal fractions and big floats.
260 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800261int32_t ExponentAndMantissaDecodeFailTests(void);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -0700262#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade59289e52019-12-30 13:44:37 -0800263
264
Laurence Lundbladebb87be22020-04-09 19:15:32 -0700265int32_t EnterMapTest(void);
266
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700267int32_t IntegerConvertTest(void);
Laurence Lundbladee3553422020-05-02 11:11:17 -0700268/*
269 Tests decoding of CBOR Sequences defined in RFC 8742
270 */
271int32_t CBORSequenceDecodeTests(void);
272
Laurence Lundbladebb87be22020-04-09 19:15:32 -0700273
Laurence Lundbladee15326f2020-06-15 15:50:23 -0700274/*
275Tests for functions to safely convert integer types.
276*/
277int32_t IntToTests(void);
278
Laurence Lundblade0750fc42020-06-20 21:02:34 -0700279
280/*
281 Test the decoding of bstr-wrapped CBOR.
282 */
283int32_t EnterBstrTest(void);
284
285
Laurence Lundblade37f46e52020-08-04 03:32:14 -0700286/*
287 Test decoding of tagged types like UUID
288 */
289int32_t DecodeTaggedTypeTests(void);
290
Laurence Lundbladea4308a82020-10-03 18:08:57 -0700291
292/*
293 Test the detection of input that is too large. Requires
294 a special build that makes QCBOR_MAX_DECODE_INPUT_SIZE small.
295 */
296int32_t TooLargeInputTest(void);
297
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -0700298
299/*
300 Test spiffy decoding of indefinite length strings.
301 */
302int32_t SpiffyIndefiniteLengthStringsTests(void);
303
304
Laurence Lundblade2a26abb2020-11-05 19:06:54 -0800305/*
306 Test PeekNext().
307 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -0700308int32_t PeekAndRewindTest(void);
Laurence Lundblade2a26abb2020-11-05 19:06:54 -0800309
310
Laurence Lundblade9f9c3732021-03-23 09:38:46 -0700311/*
312Test decoding of booleans
313*/
314int32_t BoolTest(void);
315
David Navarro9123e5b2022-03-28 16:04:03 +0200316/*
317Test GitHub issue #134: decode an indefinite-length string with a zero-length first chunk.
318*/
319int32_t CBORTestIssue134(void);
Laurence Lundblade2a26abb2020-11-05 19:06:54 -0800320
Laurence Lundbladef00b8be2024-03-08 10:34:33 -0800321
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700322int32_t PreciseNumbersTest(void);
Laurence Lundbladef00b8be2024-03-08 10:34:33 -0800323
324int32_t ErrorHandlingTests(void);
325
326
Laurence Lundbladea29f45a2024-05-14 15:55:19 -0700327/*
328 * Test QCBORDecode_GetArray and QCBORDecode_GetMap
329 */
330int32_t GetMapAndArrayTest(void);
331
Laurence Lundblade11654912024-05-09 11:49:24 -0700332int32_t TellTests(void);
333
334
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800335#endif /* defined(__QCBOR__qcbort_decode_tests__) */