Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 2 | Copyright (c) 2016-2018, The Linux Foundation. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame^] | 3 | Copyright (c) 2018-2020, Laurence Lundblade. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 4 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
| 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 Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 19 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 20 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame^] | 31 | ============================================================================*/ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 32 | |
| 33 | #ifndef __QCBOR__qcbort_decode_tests__ |
| 34 | #define __QCBOR__qcbort_decode_tests__ |
| 35 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 36 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 37 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 38 | Notes: |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 39 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 40 | - All the functions in qcbor.h are called once in the aggregation of all the tests below. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 41 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 42 | - All the types that are supported are given as input and parsed by these tests |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 43 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 44 | - There is some hostile input such as invalid lengths and CBOR too complex |
| 45 | and types this parser doesn't handle |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 46 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 47 | */ |
| 48 | |
| 49 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 50 | /* |
| 51 | Parse a well-known set of integers including those around the boundaries and |
| 52 | make sure the expected values come out |
| 53 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 54 | int IntegerValuesParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 55 | |
| 56 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 57 | /* |
| 58 | Decode a simple CBOR encoded array and make sure it returns all the correct values. |
| 59 | This is a decode test. |
| 60 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 61 | int SimpleArrayTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 62 | |
Laurence Lundblade | a65b482 | 2019-09-08 12:17:03 -0700 | [diff] [blame] | 63 | |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 64 | /* |
| 65 | Tests with empty maps and arrays |
| 66 | */ |
| 67 | int EmptyMapsAndArraysTest(void); |
| 68 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | Make sure a maximally deep array can be parsed and that the |
| 72 | reported nesting level is correct. This uses test vector |
| 73 | of CBOR encoded data with a depth of 10. This a parse test. |
| 74 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 75 | int ParseDeepArrayTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /* |
| 79 | See that the correct error is reported when parsing |
| 80 | an array of depth 11, one too large. |
| 81 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 82 | int ParseTooDeepArrayTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 83 | |
| 84 | |
| 85 | /* |
| 86 | Try to parse some legit CBOR types that this parsers |
| 87 | doesn't support. |
| 88 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 89 | int UnsupportedCBORDecodeTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 90 | |
| 91 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 92 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 93 | This takes the encoded CBOR integers used in the above test and parses |
| 94 | it over and over with one more byte less each time. It should fail |
| 95 | every time on incorrect CBOR input. This is a hostile input decode test. |
| 96 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 97 | int ShortBufferParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 98 | |
| 99 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 100 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 101 | Same as ShortBufferParseTest, but with a different encoded CBOR input. |
| 102 | It is another hostile input test |
| 103 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 104 | int ShortBufferParseTest2(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 105 | |
| 106 | |
| 107 | /* |
| 108 | Parses the somewhat complicated CBOR MAP and makes sure all the correct |
| 109 | values parse out. About 15 values are tested. This is a decode test. |
| 110 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 111 | int ParseMapTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 112 | |
| 113 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 114 | /* |
Laurence Lundblade | a65b482 | 2019-09-08 12:17:03 -0700 | [diff] [blame] | 115 | Test the decoder mode where maps are treated as arrays. |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 116 | */ |
| 117 | int ParseMapAsArrayTest(void); |
| 118 | |
| 119 | |
Laurence Lundblade | a65b482 | 2019-09-08 12:17:03 -0700 | [diff] [blame] | 120 | /* |
| 121 | Test parsing of some simple values like true, false, null... |
| 122 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 123 | int ParseSimpleTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 124 | |
| 125 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 126 | /* |
| 127 | This tests all the not-well-formed CBOR from the CBOR RFC. |
| 128 | (This is the CBORbis RFC which is not yet published at the |
| 129 | time this test was added). |
| 130 | */ |
| 131 | int NotWellFormedTests(void); |
| 132 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | Tests a number of failure cases on bad CBOR to get the right error code |
| 136 | */ |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 137 | int DecodeFailureTests(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 138 | |
| 139 | |
| 140 | /* |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 141 | Parses all possible inputs that are two bytes long. Main point |
| 142 | is that the test doesn't crash as it doesn't evaluate the |
| 143 | input for correctness in any way. |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 144 | |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 145 | (Parsing all possible 3 byte strings takes too long on all but |
Laurence Lundblade | df1c1cf | 2019-01-17 11:55:05 -0800 | [diff] [blame] | 146 | very fast machines). |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 147 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 148 | int ComprehensiveInputTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 149 | |
| 150 | |
| 151 | /* |
Laurence Lundblade | a2e2907 | 2018-12-30 09:20:06 -0800 | [diff] [blame] | 152 | Parses all possible inputs that are four bytes long. Main point |
| 153 | is that the test doesn't crash as it doesn't evaluate the |
| 154 | input for correctness in any way. This runs very slow, so it |
| 155 | is only practical as a once-in-a-while regression test on |
| 156 | fast machines. |
| 157 | */ |
| 158 | int BigComprehensiveInputTest(void); |
| 159 | |
| 160 | |
| 161 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 162 | Thest the date types -- epoch and strings |
| 163 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 164 | int DateParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 165 | |
| 166 | |
| 167 | /* |
| 168 | Test optional tags like the CBOR magic number. |
| 169 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 170 | int OptTagParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 171 | |
| 172 | |
| 173 | /* |
| 174 | Parse some big numbers, positive and negative |
| 175 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 176 | int BignumParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 177 | |
| 178 | |
Laurence Lundblade | a65b482 | 2019-09-08 12:17:03 -0700 | [diff] [blame] | 179 | /* |
| 180 | Test of mode where only string labels are allowed |
| 181 | */ |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame] | 182 | int StringDecoderModeFailTest(void); |
| 183 | |
| 184 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 185 | /* |
| 186 | Parse some nested maps |
| 187 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 188 | int NestedMapTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 189 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | Parse maps with indefinite lengths |
| 193 | */ |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 194 | int NestedMapTestIndefLen(void); |
| 195 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 196 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 197 | /* |
| 198 | Parse some maps and arrays with indefinite lengths. |
| 199 | Includes some error cases. |
| 200 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 201 | int IndefiniteLengthArrayMapTest(void); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 202 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | Parse indefinite length strings. Uses |
| 206 | MemPool. Includes error cases. |
| 207 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 208 | int IndefiniteLengthStringTest(void); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 209 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | Test deep nesting of indefinite length |
| 213 | maps and arrays including too deep. |
| 214 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 215 | int IndefiniteLengthNestTest(void); |
| 216 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | Test parsing strings were all strings, not |
| 220 | just indefinite length strings, are |
| 221 | allocated. Includes error test cases. |
| 222 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 223 | int AllocAllStringsTest(void); |
| 224 | |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 225 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 226 | /* |
| 227 | Direct test of MemPool string allocator |
| 228 | */ |
| 229 | int MemPoolTest(void); |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 230 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 231 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 232 | /* |
| 233 | Test the setting up of an external string allocator. |
| 234 | */ |
| 235 | int SetUpAllocatorTest(void); |
| 236 | |
| 237 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 238 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 239 | /* |
| 240 | Test decoding of decimal fractions and big floats, both of which are |
| 241 | made up of an exponent and mantissa. |
| 242 | */ |
| 243 | int ExponentAndMantissaDecodeTests(void); |
| 244 | |
| 245 | |
| 246 | /* |
| 247 | Hostile input tests for decimal fractions and big floats. |
| 248 | */ |
| 249 | int ExponentAndMantissaDecodeFailTests(void); |
| 250 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 251 | |
| 252 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 253 | #endif /* defined(__QCBOR__qcbort_decode_tests__) */ |