Improve boolean decoding error handling
Also more tests for boolean decoding
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 6e1cc3b..01a87e5 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -7766,3 +7766,70 @@
return 0;
}
+
+
+
+
+static const uint8_t spBooleansInMap[] =
+{
+ 0xa1, 0x08, 0xf5
+};
+
+static const uint8_t spBooleansInMapWrongType[] =
+{
+ 0xa1, 0x08, 0xf6
+};
+
+static const uint8_t spBooleansInMapNWF[] =
+{
+ 0xa1, 0x08, 0x1a
+};
+
+
+int32_t BoolTest(void)
+{
+ QCBORDecodeContext DCtx;
+ bool b;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap), 0);
+
+ QCBORDecode_EnterMap(&DCtx, NULL);
+ QCBORDecode_GetBool(&DCtx, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
+ return 1;
+ }
+
+ QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
+ return 2;
+ }
+
+ QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
+ return 3;
+ }
+
+
+ QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
+ return 4;
+ }
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType), 0);
+
+ QCBORDecode_EnterMap(&DCtx, NULL);
+ QCBORDecode_GetBool(&DCtx, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
+ return 5;
+ }
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF), 0);
+
+ QCBORDecode_EnterMap(&DCtx, NULL);
+ QCBORDecode_GetBool(&DCtx, &b);
+ if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
+ return 6;
+ }
+
+ return 0;
+}
diff --git a/test/qcbor_decode_tests.h b/test/qcbor_decode_tests.h
index 53f6bd2..54e125a 100644
--- a/test/qcbor_decode_tests.h
+++ b/test/qcbor_decode_tests.h
@@ -308,5 +308,10 @@
int32_t PeekAndRewindTest(void);
+/*
+Test decoding of booleans
+*/
+int32_t BoolTest(void);
+
#endif /* defined(__QCBOR__qcbort_decode_tests__) */
diff --git a/test/run_tests.c b/test/run_tests.c
index cc9c70b..53d83f6 100644
--- a/test/run_tests.c
+++ b/test/run_tests.c
@@ -137,6 +137,7 @@
TEST_ENTRY(ExponentAndMantissaEncodeTests),
#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
TEST_ENTRY(ParseEmptyMapInMapTest),
+ TEST_ENTRY(BoolTest)
};