blob: 6c09b0927bee31809b610cd4708b4fc85b297c19 [file] [log] [blame]
Laurence Lundbladef156fb82018-10-01 09:47:03 -07001/*==============================================================================
2 run_tests.c -- test aggregator and results reporting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004 Created on 9/30/18
5
6 Copyright (c) 2018-2019, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07007 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08008
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07009Redistribution and use in source and binary forms, with or without
10modification, are permitted provided that the following conditions are
11met:
12 * Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above
15 copyright notice, this list of conditions and the following
16 disclaimer in the documentation and/or other materials provided
17 with the distribution.
18 * The name "Laurence Lundblade" may not be used to
19 endorse or promote products derived from this software without
20 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080021
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070022THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
25ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
26BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
Laurence Lundblade88b6ece2018-12-04 12:27:19 +090032IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ==============================================================================*/
Laurence Lundbladef156fb82018-10-01 09:47:03 -070034
35
36#include "run_tests.h"
37#include "UsefulBuf.h"
38#include <stdbool.h>
39
Laurence Lundblade2d85ce42018-10-12 14:12:47 +080040#include "float_tests.h"
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080041#include "qcbor_decode_tests.h"
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053042#include "qcbor_encode_tests.h"
43#include "UsefulBuf_Tests.h"
Laurence Lundbladef156fb82018-10-01 09:47:03 -070044
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080045
46
47// Used to test RunTests
Laurence Lundbladef156fb82018-10-01 09:47:03 -070048int fail_test()
49{
50 return -44;
51}
52
53
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080054
55
Laurence Lundbladef156fb82018-10-01 09:47:03 -070056/*
57 Convert a number up to 999999999 to a string. This is so sprintf doesn't
58 have to be linked in so as to minimized dependencies even in test code.
Laurence Lundblade1544c482018-12-05 20:52:35 +090059 */
Laurence Lundbladef156fb82018-10-01 09:47:03 -070060const char *NumToString(int32_t nNum, UsefulBuf StringMem)
61{
Laurence Lundblade570fab52018-10-13 18:28:27 +080062 const int32_t nMax = 1000000000;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080063
Laurence Lundbladef156fb82018-10-01 09:47:03 -070064 UsefulOutBuf OutBuf;
65 UsefulOutBuf_Init(&OutBuf, StringMem);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080066
Laurence Lundbladef156fb82018-10-01 09:47:03 -070067 if(nNum < 0) {
68 UsefulOutBuf_AppendByte(&OutBuf, '-');
69 nNum = -nNum;
70 }
Laurence Lundblade570fab52018-10-13 18:28:27 +080071 if(nNum > nMax-1) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070072 return "XXX";
73 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080074
Laurence Lundbladef156fb82018-10-01 09:47:03 -070075 bool bDidSomeOutput = false;
Laurence Lundblade570fab52018-10-13 18:28:27 +080076 for(int n = nMax; n > 0; n/=10) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070077 int x = nNum/n;
78 if(x || bDidSomeOutput){
79 bDidSomeOutput = true;
80 UsefulOutBuf_AppendByte(&OutBuf, '0' + x);
81 nNum -= x * n;
82 }
83 }
84 if(!bDidSomeOutput){
85 UsefulOutBuf_AppendByte(&OutBuf, '0');
86 }
87 UsefulOutBuf_AppendByte(&OutBuf, '\0');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080088
Laurence Lundbladef156fb82018-10-01 09:47:03 -070089 return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr;
90}
91
92
93
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080094
Laurence Lundbladef156fb82018-10-01 09:47:03 -070095typedef int (test_fun_t)(void);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053096typedef const char * (test_fun2_t)(void);
97
Laurence Lundbladef156fb82018-10-01 09:47:03 -070098
Laurence Lundbladea2e29072018-12-30 09:20:06 -080099#define TEST_ENTRY(test_name) {#test_name, test_name, true}
100#define TEST_ENTRY_DISABLED(test_name) {#test_name, test_name, false}
101
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700102typedef struct {
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800103 const char *szTestName;
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700104 test_fun_t *test_fun;
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800105 bool bEnabled;
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700106} test_entry;
107
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530108typedef struct {
109 const char *szTestName;
110 test_fun2_t *test_fun;
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800111 bool bEnabled;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530112} test_entry2;
113
114test_entry2 s_tests2[] = {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800115 TEST_ENTRY(UBUTest_CopyUtil),
116 TEST_ENTRY(UOBTest_NonAdversarial),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530117 TEST_ENTRY(TestBasicSanity),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800118 TEST_ENTRY(UOBTest_BoundaryConditionsTest),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530119 TEST_ENTRY(UBMacroConversionsTest),
120 TEST_ENTRY(UBUtilTests),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800121 TEST_ENTRY(UIBTest_IntegerFormat)
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530122};
123
124
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700125test_entry s_tests[] = {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900126 TEST_ENTRY(ParseMapAsArrayTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530127 TEST_ENTRY(AllocAllStringsTest),
128 TEST_ENTRY(IndefiniteLengthNestTest),
129 TEST_ENTRY(NestedMapTestIndefLen),
130 TEST_ENTRY(ParseSimpleTest),
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800131 TEST_ENTRY(EncodeRawTest),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530132 TEST_ENTRY(RTICResultsTest),
133 TEST_ENTRY(MapEncodeTest),
134 TEST_ENTRY(ArrayNestingTest1),
135 TEST_ENTRY(ArrayNestingTest2),
136 TEST_ENTRY(ArrayNestingTest3),
137 TEST_ENTRY(EncodeDateTest),
138 TEST_ENTRY(SimpleValuesTest1),
139 TEST_ENTRY(IntegerValuesTest1),
140 TEST_ENTRY(AllAddMethodsTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800141 TEST_ENTRY(ParseTooDeepArrayTest),
142 TEST_ENTRY(ComprehensiveInputTest),
143 TEST_ENTRY(ParseMapTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530144 TEST_ENTRY(IndefiniteLengthArrayMapTest),
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530145 TEST_ENTRY(BasicEncodeTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800146 TEST_ENTRY(NestedMapTest),
147 TEST_ENTRY(BignumParseTest),
148 TEST_ENTRY(OptTagParseTest),
149 TEST_ENTRY(DateParseTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800150 TEST_ENTRY(ShortBufferParseTest2),
151 TEST_ENTRY(ShortBufferParseTest),
152 TEST_ENTRY(ParseDeepArrayTest),
153 TEST_ENTRY(SimpleArrayTest),
154 TEST_ENTRY(IntegerValuesParseTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530155 TEST_ENTRY(MemPoolTest),
156 TEST_ENTRY(IndefiniteLengthStringTest),
Laurence Lundbladebb474be2018-10-22 11:53:21 +0530157 TEST_ENTRY(HalfPrecisionDecodeBasicTests),
Laurence Lundbladebb474be2018-10-22 11:53:21 +0530158 TEST_ENTRY(DoubleAsSmallestTest),
159 TEST_ENTRY(HalfPrecisionAgainstRFCCodeTest),
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530160 TEST_ENTRY(BstrWrapTest),
161 TEST_ENTRY(BstrWrapErrorTest),
162 TEST_ENTRY(BstrWrapNestTest),
163 TEST_ENTRY(CoseSign1TBSTest),
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800164 TEST_ENTRY(StringDecoderModeFailTest),
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800165 TEST_ENTRY_DISABLED(BigComprehensiveInputTest),
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800166 TEST_ENTRY(EncodeErrorTests),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700167 //TEST_ENTRY(fail_test),
168};
169
170
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800171int RunTests(const char *szTestNames[], OutputStringCB pfOutput, void *poutCtx, int *pNumTestsRun)
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700172{
173 int nTestsFailed = 0;
174 int nTestsRun = 0;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530175 UsefulBuf_MAKE_STACK_UB(StringStorage, 5);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530176
177 test_entry2 *t2;
178 const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800179
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530180 for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800181 if(szTestNames[0]) {
182 // Some tests have been named
183 const char **szRequestedNames;
184 for(szRequestedNames = szTestNames; *szRequestedNames; szRequestedNames++) {
185 if(!strcmp(t2->szTestName, *szRequestedNames)) {
186 break; // Name matched
187 }
188 }
189 if(*szRequestedNames == NULL) {
190 // Didn't match this test
191 continue;
192 }
193 } else {
194 // no tests named, but don't run "disabled" tests
195 if(!t2->bEnabled) {
196 // Don't run disabled tests when all tests are being run
197 // as indicated by no specific test names being given
198 continue;
199 }
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800200 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800201
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800202 const char * szTestResult = (t2->test_fun)();
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530203 nTestsRun++;
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800204 if(pfOutput) {
Laurence Lundbladed396f622019-01-12 17:12:29 -0800205 (*pfOutput)(t2->szTestName, poutCtx, 0);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800207
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800208 if(szTestResult) {
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800209 if(pfOutput) {
Laurence Lundbladed396f622019-01-12 17:12:29 -0800210 (*pfOutput)(" FAILED (returned ", poutCtx, 0);
211 (*pfOutput)(szTestResult, poutCtx, 0);
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800212 (*pfOutput)(")", poutCtx, 1);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530213 }
214 nTestsFailed++;
215 } else {
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800216 if(pfOutput) {
Laurence Lundbladed396f622019-01-12 17:12:29 -0800217 (*pfOutput)( " PASSED", poutCtx, 1);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530218 }
219 }
220 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
222
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700223 test_entry *t;
224 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700226 for(t = s_tests; t < s_tests_end; t++) {
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800227 if(szTestNames[0]) {
228 // Some tests have been named
229 const char **szRequestedNames;
230 for(szRequestedNames = szTestNames; *szRequestedNames; szRequestedNames++) {
231 if(!strcmp(t->szTestName, *szRequestedNames)) {
232 break; // Name matched
233 }
234 }
235 if(*szRequestedNames == NULL) {
236 // Didn't match this test
237 continue;
238 }
239 } else {
240 // no tests named, but don't run "disabled" tests
241 if(!t->bEnabled) {
242 // Don't run disabled tests when all tests are being run
243 // as indicated by no specific test names being given
244 continue;
245 }
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800246 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800247
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800248 int nTestResult = (t->test_fun)();
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700249 nTestsRun++;
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800250 if(pfOutput) {
Laurence Lundbladed396f622019-01-12 17:12:29 -0800251 (*pfOutput)(t->szTestName, poutCtx, 0);
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800254 if(nTestResult) {
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800255 if(pfOutput) {
Laurence Lundbladed396f622019-01-12 17:12:29 -0800256 (*pfOutput)(" FAILED (returned ", poutCtx, 0);
257 (*pfOutput)(NumToString(nTestResult, StringStorage), poutCtx, 0);
258 (*pfOutput)(")", poutCtx, 1);
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700259 }
260 nTestsFailed++;
261 } else {
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800262 if(pfOutput) {
Laurence Lundbladed396f622019-01-12 17:12:29 -0800263 (*pfOutput)( " PASSED", poutCtx, 1);
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700264 }
265 }
266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800267
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700268 if(pNumTestsRun) {
269 *pNumTestsRun = nTestsRun;
270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800271
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800272 if(pfOutput) {
Laurence Lundbladed396f622019-01-12 17:12:29 -0800273 (*pfOutput)( "SUMMARY: ", poutCtx, 0);
274 (*pfOutput)( NumToString(nTestsRun, StringStorage), poutCtx, 0);
275 (*pfOutput)( " tests run; ", poutCtx, 0);
276 (*pfOutput)( NumToString(nTestsFailed, StringStorage), poutCtx, 0);
277 (*pfOutput)( " tests failed", poutCtx, 1);
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800279
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700280 return nTestsFailed;
281}
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800282
283
284
285
286static void PrintSize(const char *szWhat, uint32_t uSize, OutputStringCB pfOutput, void *pOutCtx)
287{
Laurence Lundbladed396f622019-01-12 17:12:29 -0800288 UsefulBuf_MAKE_STACK_UB(buffer, 20);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800289
Laurence Lundbladed396f622019-01-12 17:12:29 -0800290 (*pfOutput)(szWhat, pOutCtx, 0);
291 (*pfOutput)(" ", pOutCtx, 0);
292 (*pfOutput)(NumToString(uSize, buffer), pOutCtx, 0);
293 (*pfOutput)("", pOutCtx, 1);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800294}
295
296void PrintSizes(OutputStringCB pfOutput, void *pOutCtx)
297{
Laurence Lundbladed396f622019-01-12 17:12:29 -0800298 // Type and size of return from sizeof() varies. These will never be large so cast is safe
299 PrintSize("sizeof(QCBORTrackNesting)", (uint32_t)sizeof(QCBORTrackNesting), pfOutput, pOutCtx);
300 PrintSize("sizeof(QCBOREncodeContext)", (uint32_t)sizeof(QCBOREncodeContext), pfOutput, pOutCtx);
301 PrintSize("sizeof(QCBORDecodeNesting)", (uint32_t)sizeof(QCBORDecodeNesting), pfOutput, pOutCtx);
302 PrintSize("sizeof(QCBORDecodeContext)", (uint32_t)sizeof(QCBORDecodeContext), pfOutput, pOutCtx);
303 PrintSize("sizeof(QCBORItem)", (uint32_t)sizeof(QCBORItem), pfOutput, pOutCtx);
304 PrintSize("sizeof(QCBORStringAllocator)",(uint32_t)sizeof(QCBORStringAllocator), pfOutput, pOutCtx);
305 PrintSize("sizeof(QCBORTagListIn)", (uint32_t)sizeof(QCBORTagListIn), pfOutput, pOutCtx);
306 PrintSize("sizeof(QCBORTagListOut)", (uint32_t)sizeof(QCBORTagListOut), pfOutput, pOutCtx);
307 (*pfOutput)("", pOutCtx, 1);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800308}