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. |
| 3 | Copyright (c) 2018, Laurence Lundblade. |
| 4 | All rights reserved. |
| 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 | d92a616 | 2018-11-01 11:38:35 +0700 | [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 | d92a616 | 2018-11-01 11:38:35 +0700 | [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 | |
| 36 | #include "qcbor.h" |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | Notes: |
| 41 | |
| 42 | - All the functions in qcbor.h are called once in the aggregation of all the tests below. |
| 43 | |
| 44 | - All the types that are supported are given as input and parsed by these tests |
| 45 | |
| 46 | - There is some hostile input such as invalid lengths and CBOR too complex |
| 47 | and types this parser doesn't handle |
| 48 | |
| 49 | */ |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | /* |
| 55 | Parse a well-known set of integers including those around the boundaries and |
| 56 | make sure the expected values come out |
| 57 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 58 | int IntegerValuesParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | Decode a simple CBOR encoded array and make sure it returns all the correct values. |
| 66 | This is a decode test. |
| 67 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 68 | int SimpleArrayTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 69 | |
| 70 | |
| 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 Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 76 | int ParseDeepArrayTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 77 | |
| 78 | |
| 79 | /* |
| 80 | See that the correct error is reported when parsing |
| 81 | an array of depth 11, one too large. |
| 82 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 83 | int ParseTooDeepArrayTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 84 | |
| 85 | |
| 86 | /* |
| 87 | Try to parse some legit CBOR types that this parsers |
| 88 | doesn't support. |
| 89 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 90 | int UnsupportedCBORDecodeTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 91 | |
| 92 | |
| 93 | /* |
| 94 | 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 Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 98 | int ShortBufferParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 99 | |
| 100 | |
| 101 | /* |
| 102 | Same as ShortBufferParseTest, but with a different encoded CBOR input. |
| 103 | It is another hostile input test |
| 104 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 105 | int ShortBufferParseTest2(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 106 | |
| 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 Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 112 | int ParseMapTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 113 | |
| 114 | |
| 115 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 116 | int FloatValuesTest1(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 117 | |
| 118 | |
| 119 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 120 | int SimpleValuesTest1(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 121 | |
| 122 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 123 | /* |
| 124 | |
| 125 | */ |
| 126 | int ParseMapAsArrayTest(void); |
| 127 | |
| 128 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 129 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 130 | int ParseSimpleTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 131 | |
| 132 | |
| 133 | |
| 134 | /* |
| 135 | Tests a number of failure cases on bad CBOR to get the right error code |
| 136 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 137 | int FailureTests(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 138 | |
| 139 | |
| 140 | /* |
| 141 | Generate all possible input strings up to length x and tries to parse them completely |
| 142 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 143 | int ComprehensiveInputTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 144 | |
| 145 | |
| 146 | /* |
| 147 | Thest the date types -- epoch and strings |
| 148 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 149 | int DateParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 150 | |
| 151 | |
| 152 | /* |
| 153 | Test optional tags like the CBOR magic number. |
| 154 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 155 | int OptTagParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 156 | |
| 157 | |
| 158 | /* |
| 159 | Parse some big numbers, positive and negative |
| 160 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 161 | int BignumParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 162 | |
| 163 | |
Laurence Lundblade | ea567ac | 2018-12-09 14:03:21 -0800 | [diff] [blame^] | 164 | int StringDecoderModeFailTest(void); |
| 165 | |
| 166 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 167 | /* |
| 168 | Parse some nested maps |
| 169 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 170 | int NestedMapTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 171 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | Parse maps with indefinite lengths |
| 175 | */ |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 176 | int NestedMapTestIndefLen(void); |
| 177 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 178 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 179 | /* |
| 180 | Parse some maps and arrays with indefinite lengths. |
| 181 | Includes some error cases. |
| 182 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 183 | int IndefiniteLengthArrayMapTest(void); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 184 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | Parse indefinite length strings. Uses |
| 188 | MemPool. Includes error cases. |
| 189 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 190 | int IndefiniteLengthStringTest(void); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 191 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | Test deep nesting of indefinite length |
| 195 | maps and arrays including too deep. |
| 196 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 197 | int IndefiniteLengthNestTest(void); |
| 198 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | Test parsing strings were all strings, not |
| 202 | just indefinite length strings, are |
| 203 | allocated. Includes error test cases. |
| 204 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 205 | int AllocAllStringsTest(void); |
| 206 | |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 207 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 208 | /* |
| 209 | Direct test of MemPool string allocator |
| 210 | */ |
| 211 | int MemPoolTest(void); |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 212 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 213 | |
| 214 | #endif /* defined(__QCBOR__qcbort_decode_tests__) */ |