Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1 | /*============================================================================== |
| 2 | Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
| 3 | |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions are |
| 6 | met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above |
| 10 | copyright notice, this list of conditions and the following |
| 11 | disclaimer in the documentation and/or other materials provided |
| 12 | with the distribution. |
| 13 | * Neither the name of The Linux Foundation nor the names of its |
| 14 | contributors may be used to endorse or promote products derived |
| 15 | from this software without specific prior written permission. |
| 16 | |
| 17 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | ==============================================================================*/ |
| 29 | |
| 30 | #ifndef __QCBOR__qcbort_decode_tests__ |
| 31 | #define __QCBOR__qcbort_decode_tests__ |
| 32 | |
| 33 | #include "qcbor.h" |
| 34 | |
| 35 | |
| 36 | /* |
| 37 | Notes: |
| 38 | |
| 39 | - All the functions in qcbor.h are called once in the aggregation of all the tests below. |
| 40 | |
| 41 | - All the types that are supported are given as input and parsed by these tests |
| 42 | |
| 43 | - There is some hostile input such as invalid lengths and CBOR too complex |
| 44 | and types this parser doesn't handle |
| 45 | |
| 46 | */ |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | Parse a well-known set of integers including those around the boundaries and |
| 53 | make sure the expected values come out |
| 54 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 55 | int IntegerValuesParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | /* |
| 62 | Decode a simple CBOR encoded array and make sure it returns all the correct values. |
| 63 | This is a decode test. |
| 64 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 65 | int SimpleArrayTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 66 | |
| 67 | |
| 68 | /* |
| 69 | Make sure a maximally deep array can be parsed and that the |
| 70 | reported nesting level is correct. This uses test vector |
| 71 | of CBOR encoded data with a depth of 10. This a parse test. |
| 72 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 73 | int ParseDeepArrayTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 74 | |
| 75 | |
| 76 | /* |
| 77 | See that the correct error is reported when parsing |
| 78 | an array of depth 11, one too large. |
| 79 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 80 | int ParseTooDeepArrayTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 81 | |
| 82 | |
| 83 | /* |
| 84 | Try to parse some legit CBOR types that this parsers |
| 85 | doesn't support. |
| 86 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 87 | int UnsupportedCBORDecodeTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 88 | |
| 89 | |
| 90 | /* |
| 91 | This takes the encoded CBOR integers used in the above test and parses |
| 92 | it over and over with one more byte less each time. It should fail |
| 93 | every time on incorrect CBOR input. This is a hostile input decode test. |
| 94 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 95 | int ShortBufferParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 96 | |
| 97 | |
| 98 | /* |
| 99 | Same as ShortBufferParseTest, but with a different encoded CBOR input. |
| 100 | It is another hostile input test |
| 101 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 102 | int ShortBufferParseTest2(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 103 | |
| 104 | |
| 105 | /* |
| 106 | Parses the somewhat complicated CBOR MAP and makes sure all the correct |
| 107 | values parse out. About 15 values are tested. This is a decode test. |
| 108 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 109 | int ParseMapTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 110 | |
| 111 | |
| 112 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 113 | int FloatValuesTest1(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 114 | |
| 115 | |
| 116 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 117 | int SimpleValuesTest1(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 118 | |
| 119 | |
| 120 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 121 | int ParseSimpleTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 122 | |
| 123 | |
| 124 | |
| 125 | /* |
| 126 | Tests a number of failure cases on bad CBOR to get the right error code |
| 127 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 128 | int FailureTests(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 129 | |
| 130 | |
| 131 | /* |
| 132 | Generate all possible input strings up to length x and tries to parse them completely |
| 133 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 134 | int ComprehensiveInputTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 135 | |
| 136 | |
| 137 | /* |
| 138 | Thest the date types -- epoch and strings |
| 139 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 140 | int DateParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 141 | |
| 142 | |
| 143 | /* |
| 144 | Test optional tags like the CBOR magic number. |
| 145 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 146 | int OptTagParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 147 | |
| 148 | |
| 149 | /* |
| 150 | Parse some big numbers, positive and negative |
| 151 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 152 | int BignumParseTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 153 | |
| 154 | |
| 155 | /* |
| 156 | Parse some nested maps |
| 157 | */ |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 158 | int NestedMapTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 159 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | Parse maps with indefinite lengths |
| 163 | */ |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 164 | int NestedMapTestIndefLen(void); |
| 165 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 166 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 167 | /* |
| 168 | Parse some maps and arrays with indefinite lengths. |
| 169 | Includes some error cases. |
| 170 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 171 | int IndefiniteLengthArrayMapTest(void); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 172 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | Parse indefinite length strings. Uses |
| 176 | MemPool. Includes error cases. |
| 177 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 178 | int IndefiniteLengthStringTest(void); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 179 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | Test deep nesting of indefinite length |
| 183 | maps and arrays including too deep. |
| 184 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 185 | int IndefiniteLengthNestTest(void); |
| 186 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | Test parsing strings were all strings, not |
| 190 | just indefinite length strings, are |
| 191 | allocated. Includes error test cases. |
| 192 | */ |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 193 | int AllocAllStringsTest(void); |
| 194 | |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 195 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 196 | /* |
| 197 | Direct test of MemPool string allocator |
| 198 | */ |
| 199 | int MemPoolTest(void); |
Laurence Lundblade | 0155b62 | 2018-10-12 20:04:37 +0800 | [diff] [blame] | 200 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 201 | |
| 202 | #endif /* defined(__QCBOR__qcbort_decode_tests__) */ |