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__qcbor_encode_tests__ |
| 34 | #define __QCBOR__qcbor_encode_tests__ |
| 35 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 36 | #include <stdint.h> |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 37 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 38 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 39 | Notes: |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 40 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 41 | - All the functions in qcbor.h are called once in the aggregation of all |
| 42 | the tests below. |
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 | - 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] | 45 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 46 | - There is some hostile input such as invalid lengths and CBOR too complex |
| 47 | and types this parser doesn't handle |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 48 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | |
| 52 | /* |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 53 | Most basic test. |
| 54 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 55 | int32_t BasicEncodeTest(void); |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame] | 56 | |
| 57 | |
| 58 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 59 | Encode lots of integer values, particularly around the boundary and |
| 60 | make sure they Match the expected binary output. Primarily an |
| 61 | encoding test. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 62 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 63 | int32_t IntegerValuesTest1(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 64 | |
| 65 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 66 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 67 | Create nested arrays to the max depth allowed and make sure it |
| 68 | succeeds. This is an encoding test. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 69 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 70 | int32_t ArrayNestingTest1(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 71 | |
| 72 | |
| 73 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 74 | Create nested arrays to one more than the meax depth and make sure it |
| 75 | fails. This is an encoding test. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 76 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 77 | int32_t ArrayNestingTest2(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 78 | |
| 79 | |
| 80 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 81 | Encoding test. Create arrays to max depth and close one extra time |
| 82 | and look for correct error code |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 83 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 84 | int32_t ArrayNestingTest3(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 85 | |
| 86 | |
| 87 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 88 | This tests the QCBOREncode_AddRaw() function by adding two chunks or |
| 89 | RAWCBOR to an array and comparing with expected values. This is an |
| 90 | encoding test. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 91 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 92 | int32_t EncodeRawTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 93 | |
| 94 | |
| 95 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 96 | This creates a somewhat complicated CBOR MAP and verifies it against |
| 97 | expected data. This is an encoding test. |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 98 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 99 | int32_t MapEncodeTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 100 | |
| 101 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 102 | /* |
| 103 | Encodes a goodly number of floats and doubles and checks encoding is right |
| 104 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 105 | int32_t FloatValuesTest1(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 106 | |
| 107 | |
| 108 | /* |
| 109 | Encodes true, false and the like |
| 110 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 111 | int32_t SimpleValuesTest1(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 112 | |
| 113 | |
| 114 | /* |
Jan Jongboom | 47d86c5 | 2019-07-25 08:54:16 +0200 | [diff] [blame] | 115 | Encodes basic maps and arrays with indefinite length |
| 116 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 117 | int32_t SimpleValuesIndefiniteLengthTest1(void); |
Jan Jongboom | 47d86c5 | 2019-07-25 08:54:16 +0200 | [diff] [blame] | 118 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 119 | |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 120 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 121 | Indefinite length arrays and maps use the 'magic' number 31, verify |
| 122 | that everything with length 31 still works properly |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 123 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 124 | int32_t EncodeLengthThirtyoneTest(void); |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 125 | |
Jan Jongboom | 47d86c5 | 2019-07-25 08:54:16 +0200 | [diff] [blame] | 126 | |
| 127 | /* |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 128 | Encodes most data formats that are supported */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 129 | int32_t EncodeDateTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 130 | |
| 131 | |
| 132 | /* |
| 133 | Encodes particular data structure that a particular app will need... |
| 134 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 135 | int32_t RTICResultsTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 136 | |
| 137 | |
| 138 | /* |
| 139 | Calls all public encode methods in qcbor.h once. |
| 140 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 141 | int32_t AllAddMethodsTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | The binary string wrapping of maps and arrays used by COSE |
| 145 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 146 | int32_t BstrWrapTest(void); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 147 | |
Laurence Lundblade | a65b482 | 2019-09-08 12:17:03 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | Test error cases for bstr wrapping encoding such as closing an open |
| 151 | array with CloseBstrWrap |
| 152 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 153 | int32_t BstrWrapErrorTest(void); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 154 | |
Laurence Lundblade | a65b482 | 2019-09-08 12:17:03 -0700 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | Test complicated nested bstr wrapping |
| 158 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 159 | int32_t BstrWrapNestTest(void); |
Laurence Lundblade | 684aec2 | 2018-10-12 19:33:53 +0800 | [diff] [blame] | 160 | |
Laurence Lundblade | a65b482 | 2019-09-08 12:17:03 -0700 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | Test encoding a COSE_Sign1 with bstr wrapping |
| 164 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 165 | int32_t CoseSign1TBSTest(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 166 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 167 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 168 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 169 | /* |
| 170 | Test encoding of decimal fractions and big floats, both of which are |
| 171 | made up of an exponent and mantissa |
| 172 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 173 | int32_t ExponentAndMantissaEncodeTests(void); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 174 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 175 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame^] | 176 | |
Laurence Lundblade | a65b482 | 2019-09-08 12:17:03 -0700 | [diff] [blame] | 177 | /* |
| 178 | Test the error cases when encoding CBOR such as buffer too large, |
| 179 | buffer too small, array nesting too deep. Aims to cover the error |
| 180 | codes returned when encoding CBOR |
| 181 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 182 | int32_t EncodeErrorTests(void); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 183 | |
| 184 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame^] | 185 | /* |
| 186 | Test QCBOREncode_EncodeHead(). This is a minimal test because every other |
| 187 | test here exercises it in some way. |
| 188 | */ |
| 189 | int32_t QCBORHeadTest(void); |
| 190 | |
| 191 | |
| 192 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 193 | #endif /* defined(__QCBOR__qcbor_encode_tests__) */ |