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