Add test coverage
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 95667dc..775d0d4 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -3975,7 +3975,6 @@
{
QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, NULL);
pMe->uLastError = (uint8_t)uErr;
- // TODO: this this on empty map
}
/*
@@ -3994,9 +3993,6 @@
QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, &CallBack);
pMe->uLastError = (uint8_t)uErr;
-
- // TODO: this this on empty map
-
}
@@ -4312,8 +4308,6 @@
if(uErr != QCBOR_SUCCESS) {
goto Done;
}
-
- // TODO: test this on empy map
}
uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache);
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index e167159..c691455 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -7007,6 +7007,53 @@
}
+QCBORError CBTest(void *pCallbackCtx, const QCBORItem *pItem)
+{
+ (void)pCallbackCtx;
+ (void)pItem;
+ return QCBOR_SUCCESS;
+}
+
+
+struct CbTest2Ctx {
+ bool found2;
+ bool found4;
+ bool error;
+};
+
+QCBORError CBTest2(void *pCallbackCtx, const QCBORItem *pItem)
+{
+ struct CbTest2Ctx *pCtx;
+
+ pCtx = (struct CbTest2Ctx *)pCallbackCtx;
+
+ if(pItem->uLabelType != QCBOR_TYPE_INT64) {
+ pCtx->error = true;
+ return QCBOR_SUCCESS;
+ }
+
+ switch(pItem->label.int64) {
+
+ case 2:
+ pCtx->found2 = true;
+ break;
+
+ case 4:
+ pCtx->found4 = true;
+ break;
+
+ case 3:
+ /* To test error return out of callback */
+ return QCBOR_ERR_CALLBACK_FAIL;
+
+ default:
+ pCtx->error = true;
+
+ }
+ return QCBOR_SUCCESS;
+}
+
+
int32_t EnterMapTest(void)
{
QCBORItem Item1;
@@ -7389,6 +7436,134 @@
return 3000;
}
+
+ QCBORItem SearchItems[4];
+
+ /* GetItems on an empty map */
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
+ QCBORDecode_EnterMap(&DCtx, NULL);
+
+ SearchItems[0].uLabelType = QCBOR_TYPE_INT64;
+ SearchItems[0].label.int64 = 0;
+ SearchItems[0].uDataType = QCBOR_TYPE_ANY;
+ SearchItems[1].uLabelType = QCBOR_TYPE_NONE;
+ QCBORDecode_GetItemsInMap(&DCtx, SearchItems);
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
+ return 4000;
+ }
+ if(SearchItems[0].uDataType != QCBOR_TYPE_NONE) {
+ return 4001;
+ }
+
+ /* Get Items with call back on empty map */
+ SearchItems[0].uLabelType = QCBOR_TYPE_INT64;
+ SearchItems[0].label.int64 = 0;
+ SearchItems[0].uDataType = QCBOR_TYPE_ANY;
+ SearchItems[1].uLabelType = QCBOR_TYPE_NONE;
+ QCBORDecode_GetItemsInMapWithCallback(&DCtx, SearchItems, NULL, CBTest);
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
+ return 4002;
+ }
+ if(SearchItems[0].uDataType != QCBOR_TYPE_NONE) {
+ return 4003;
+ }
+
+ /* Test exiting an empty map */
+ QCBORDecode_ExitMap(&DCtx);
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
+ return 4702;
+ }
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
+ QCBORDecode_EnterMap(&DCtx, NULL);
+
+ /* GetItems test */
+ SearchItems[0].uLabelType = QCBOR_TYPE_INT64;
+ SearchItems[0].label.int64 = 3;
+ SearchItems[0].uDataType = QCBOR_TYPE_ANY;
+ SearchItems[1].uLabelType = QCBOR_TYPE_INT64;
+ SearchItems[1].label.int64 = 1;
+ SearchItems[1].uDataType = QCBOR_TYPE_ANY;
+ SearchItems[2].uLabelType = QCBOR_TYPE_INT64;
+ SearchItems[2].label.int64 = 99;
+ SearchItems[2].uDataType = QCBOR_TYPE_ANY;
+ SearchItems[3].uLabelType = QCBOR_TYPE_NONE;
+ QCBORDecode_GetItemsInMap(&DCtx, SearchItems);
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
+ return 4104;
+ }
+ if(SearchItems[0].uDataType != QCBOR_TYPE_INT64 ||
+ SearchItems[1].uDataType != QCBOR_TYPE_INT64 ||
+ SearchItems[0].val.int64 != 3 ||
+ SearchItems[1].val.int64 != 1 ||
+ SearchItems[2].uDataType != QCBOR_TYPE_NONE) {
+ return 4103;
+ }
+
+
+ /* Test callback */
+ SearchItems[0].uLabelType = QCBOR_TYPE_INT64;
+ SearchItems[0].label.int64 = 3;
+ SearchItems[0].uDataType = QCBOR_TYPE_ANY;
+ SearchItems[1].uLabelType = QCBOR_TYPE_INT64;
+ SearchItems[1].label.int64 = 1;
+ SearchItems[1].uDataType = QCBOR_TYPE_ANY;
+ SearchItems[2].uLabelType = QCBOR_TYPE_NONE;
+
+ struct CbTest2Ctx CTX;
+ CTX.error = false;
+ CTX.found2 = false;
+ CTX.found4 = false;
+ QCBORDecode_GetItemsInMapWithCallback(&DCtx, SearchItems, &CTX, CBTest2);
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
+ return 4204;
+ }
+ if(!CTX.found2 || !CTX.found4) {
+ return 4201;
+ }
+
+ if(SearchItems[0].uDataType != QCBOR_TYPE_INT64 || SearchItems[1].uDataType != QCBOR_TYPE_INT64) {
+ return 4203;
+ }
+
+ /* Test error-exit from callback */
+ SearchItems[0].uLabelType = QCBOR_TYPE_INT64;
+ SearchItems[0].label.int64 = 2;
+ SearchItems[0].uDataType = QCBOR_TYPE_ANY;
+ SearchItems[1].uLabelType = QCBOR_TYPE_INT64;
+ SearchItems[1].label.int64 = 1;
+ SearchItems[1].uDataType = QCBOR_TYPE_ANY;
+ SearchItems[2].uLabelType = QCBOR_TYPE_NONE;
+
+ CTX.error = false;
+ CTX.found2 = false;
+ CTX.found4 = false;
+ QCBORDecode_GetItemsInMapWithCallback(&DCtx, SearchItems, &CTX, CBTest2);
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CALLBACK_FAIL) {
+ return 4306;
+ }
+
+ /* Test QCBORDecode_GetItemInMapN (covered indireclty by many other tests) */
+ QCBORItem Item;
+ (void)QCBORDecode_GetAndResetError(&DCtx);
+ QCBORDecode_GetItemInMapN(&DCtx, 1, QCBOR_TYPE_ANY, &Item);
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
+ return 4704;
+ }
+ if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.int64 != 1) {
+ return 4707;
+ }
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
+ QCBORDecode_EnterMap(&DCtx, NULL);
+ QCBORDecode_GetItemInMapSZ(&DCtx, "map in a map", QCBOR_TYPE_ANY, &Item);
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
+ return 4804;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 4) {
+ return 4807;
+ }
+
nReturn = EnterMapCursorTest();
return nReturn;
@@ -10191,8 +10366,6 @@
/* ------ */
- static const uint8_t spEmptyMap[] = "\xA0";
-
QCBORDecode_Init(&DCtx,
UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap),
0);