blob: e87490e9b473e5ecdbc2a081fd76b2152278c380 [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 Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, 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 Lundblade2ded3d92018-10-09 21:36:11 +080041 - All the functions in qcbor.h are called once in the aggregation of all the tests below.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080043 - All the types that are supported are given as input and parsed by these tests
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080044
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045 - There is some hostile input such as invalid lengths and CBOR too complex
46 and types this parser doesn't handle
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080047
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080048 */
49
50
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080051/*
52 Parse a well-known set of integers including those around the boundaries and
53 make sure the expected values come out
54 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080055int32_t IntegerValuesParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056
57
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058/*
59 Decode a simple CBOR encoded array and make sure it returns all the correct values.
60 This is a decode test.
61 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080062int32_t SimpleArrayTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080063
Laurence Lundbladea65b4822019-09-08 12:17:03 -070064
Laurence Lundblade9916b1b2019-09-07 22:33:25 -070065/*
66 Tests with empty maps and arrays
67 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080068int32_t EmptyMapsAndArraysTest(void);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -070069
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070
71/*
72 Make sure a maximally deep array can be parsed and that the
73 reported nesting level is correct. This uses test vector
74 of CBOR encoded data with a depth of 10. This a parse test.
75 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080076int32_t ParseDeepArrayTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080077
78
79/*
80 See that the correct error is reported when parsing
81 an array of depth 11, one too large.
82 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080083int32_t ParseTooDeepArrayTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080084
85
86/*
87 Try to parse some legit CBOR types that this parsers
88 doesn't support.
89 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080090int32_t UnsupportedCBORDecodeTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080091
92
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080093/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080094 This takes the encoded CBOR integers used in the above test and parses
95 it over and over with one more byte less each time. It should fail
96 every time on incorrect CBOR input. This is a hostile input decode test.
97 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -080098int32_t ShortBufferParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080099
100
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800101/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102 Same as ShortBufferParseTest, but with a different encoded CBOR input.
103 It is another hostile input test
104 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800105int32_t ShortBufferParseTest2(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800106
107
108/*
109 Parses the somewhat complicated CBOR MAP and makes sure all the correct
110 values parse out. About 15 values are tested. This is a decode test.
111 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800112int32_t ParseMapTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113
114
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900115/*
Laurence Lundbladea65b4822019-09-08 12:17:03 -0700116Test the decoder mode where maps are treated as arrays.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900117 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800118int32_t ParseMapAsArrayTest(void);
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900119
120
Laurence Lundbladea65b4822019-09-08 12:17:03 -0700121/*
122 Test parsing of some simple values like true, false, null...
123 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800124int32_t ParseSimpleTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125
126
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700127/*
128 This tests all the not-well-formed CBOR from the CBOR RFC.
129 (This is the CBORbis RFC which is not yet published at the
130 time this test was added).
131 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800132int32_t NotWellFormedTests(void);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700133
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800134
135/*
136 Tests a number of failure cases on bad CBOR to get the right error code
137 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800138int32_t DecodeFailureTests(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800139
140
141/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800142 Parses all possible inputs that are two bytes long. Main point
143 is that the test doesn't crash as it doesn't evaluate the
144 input for correctness in any way.
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800145
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800146 (Parsing all possible 3 byte strings takes too long on all but
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800147 very fast machines).
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800148 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800149int32_t ComprehensiveInputTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150
151
152/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800153 Parses all possible inputs that are four bytes long. Main point
154 is that the test doesn't crash as it doesn't evaluate the
155 input for correctness in any way. This runs very slow, so it
156 is only practical as a once-in-a-while regression test on
157 fast machines.
158 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800159int32_t BigComprehensiveInputTest(void);
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800160
161
162/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800163 Thest the date types -- epoch and strings
164 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800165int32_t DateParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800166
167
168/*
169 Test optional tags like the CBOR magic number.
170 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800171int32_t OptTagParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800172
173
174/*
175 Parse some big numbers, positive and negative
176 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800177int32_t BignumParseTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800178
179
Laurence Lundbladea65b4822019-09-08 12:17:03 -0700180/*
181 Test of mode where only string labels are allowed
182 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800183int32_t StringDecoderModeFailTest(void);
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800184
185
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800186/*
187 Parse some nested maps
188 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800189int32_t NestedMapTest(void);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800190
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530191
192/*
193 Parse maps with indefinite lengths
194 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800195int32_t NestedMapTestIndefLen(void);
Laurence Lundblade742df4a2018-10-13 20:07:17 +0800196
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530198/*
199 Parse some maps and arrays with indefinite lengths.
200 Includes some error cases.
201 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800202int32_t IndefiniteLengthArrayMapTest(void);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800203
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530204
205/*
206 Parse indefinite length strings. Uses
207 MemPool. Includes error cases.
208 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800209int32_t IndefiniteLengthStringTest(void);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800210
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530211
212/*
213 Test deep nesting of indefinite length
214 maps and arrays including too deep.
215 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800216int32_t IndefiniteLengthNestTest(void);
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530217
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530218
219/*
220 Test parsing strings were all strings, not
221 just indefinite length strings, are
222 allocated. Includes error test cases.
223 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800224int32_t AllocAllStringsTest(void);
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530225
Laurence Lundblade0155b622018-10-12 20:04:37 +0800226
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530227/*
228 Direct test of MemPool string allocator
229 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800230int32_t MemPoolTest(void);
Laurence Lundblade0155b622018-10-12 20:04:37 +0800231
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800232
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800233/*
234 Test the setting up of an external string allocator.
235 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800236int32_t SetUpAllocatorTest(void);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800237
238
Laurence Lundblade59289e52019-12-30 13:44:37 -0800239#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
240/*
241 Test decoding of decimal fractions and big floats, both of which are
242 made up of an exponent and mantissa.
243 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800244int32_t ExponentAndMantissaDecodeTests(void);
Laurence Lundblade59289e52019-12-30 13:44:37 -0800245
246
247/*
248 Hostile input tests for decimal fractions and big floats.
249 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800250int32_t ExponentAndMantissaDecodeFailTests(void);
Laurence Lundblade59289e52019-12-30 13:44:37 -0800251#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
252
253
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800254#endif /* defined(__QCBOR__qcbort_decode_tests__) */