Fix GetTextString() and GetByteString() error handling (#303)
GetTextString() and GetByteString() where returning the wrong error code when the input CBOR was the wrong type
* Fix GetTextString() and GetByteString() error handling
* Improve test and doc for GetString
* Miniscule header fix
---------
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 305489c..8ae0e3d 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -9323,7 +9323,7 @@
/*
An array of three map entries
- 1) Indefinite length string label for indefinite lenght byte string
+ 1) Indefinite length string label for indefinite length byte string
2) Indefinite length string label for an integer
3) Indefinite length string label for an indefinite-length negative big num
*/
@@ -9416,6 +9416,58 @@
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
+static const uint8_t spStringsTest[] = {
+ 0x63, 'a', 'b', 'c',
+ 0x43, 'd', 'e', 'f'
+};
+
+static const uint8_t spNotWellFormed[] = {
+ 0xff
+};
+
+int32_t SpiffyStringTest(void)
+{
+ QCBORDecodeContext DC;
+ UsefulBufC String;
+
+ QCBORDecode_Init(&DC, ByteArrayLiteralToUsefulBufC(spStringsTest), 0);
+
+ QCBORDecode_GetTextString(&DC, &String);
+ if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS &&
+ UsefulBuf_Compare(String, SZLiteralToUsefulBufC("abc"))) {
+ return 1;
+ }
+
+ QCBORDecode_GetByteString(&DC, &String);
+ if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS &&
+ UsefulBuf_Compare(String, SZLiteralToUsefulBufC("def"))) {
+ return 2;
+ }
+
+ QCBORDecode_Init(&DC, ByteArrayLiteralToUsefulBufC(spStringsTest), 0);
+ QCBORDecode_GetByteString(&DC, &String);
+ if(QCBORDecode_GetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE &&
+ !UsefulBuf_IsNULLC(String)) {
+ return 3;
+ }
+
+ QCBORDecode_Init(&DC, ByteArrayLiteralToUsefulBufC(spNotWellFormed), 0);
+ QCBORDecode_GetByteString(&DC, &String);
+ if(QCBORDecode_GetError(&DC) != QCBOR_ERR_BAD_BREAK &&
+ !UsefulBuf_IsNULLC(String)) {
+ return 4;
+ }
+
+#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
+ return SpiffyIndefiniteLengthStringsTests();
+#else
+ return 0;
+#endif
+
+}
+
+
+
#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
/*
* An array of an integer and an array. The second array contains
diff --git a/test/qcbor_decode_tests.h b/test/qcbor_decode_tests.h
index b542896..65020ff 100644
--- a/test/qcbor_decode_tests.h
+++ b/test/qcbor_decode_tests.h
@@ -299,7 +299,7 @@
/*
Test spiffy decoding of indefinite length strings.
*/
-int32_t SpiffyIndefiniteLengthStringsTests(void);
+int32_t SpiffyStringTest(void);
/*
diff --git a/test/run_tests.c b/test/run_tests.c
index bcb4540..f186f57 100644
--- a/test/run_tests.c
+++ b/test/run_tests.c
@@ -131,7 +131,7 @@
TEST_ENTRY(MemPoolTest),
TEST_ENTRY(IndefiniteLengthStringTest),
#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
- TEST_ENTRY(SpiffyIndefiniteLengthStringsTests),
+ TEST_ENTRY(SpiffyStringTest),
#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
TEST_ENTRY(SetUpAllocatorTest),
TEST_ENTRY(CBORTestIssue134),