Encode/UB test coverage 100%; fix bugs in UsefulOutBuf_Compare and sorting
UsefulBuf test coverage back up to 100% (filled out coverage for new v2 features)
UsefulOutBuf_Compare() bugs for unequal length comparisons and comparisons off the end of the buffer
Improve UsefulOutBuf magic number check
UsefulOutBuf_OutSubString renamed to UsefulOutBuf_SubString for consistency. Old name still supported.
* Useful test coverage; UsefulOutBuf_Compare fixes
* Improve sort error handling; encode test coverage 100%
* Fix ifdef test fan out
* revert error codes
* Add diverse labels test
---------
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/test/UsefulBuf_Tests.c b/test/UsefulBuf_Tests.c
index ae3862c..385551c 100644
--- a/test/UsefulBuf_Tests.c
+++ b/test/UsefulBuf_Tests.c
@@ -127,7 +127,7 @@
goto Done;
}
- Out = UsefulOutBuf_SubString(&UOB, 10, 8);
+ Out = UsefulOutBuf_OutSubString(&UOB, 10, 8);
if(UsefulBuf_IsNULLC(Out) ||
UsefulBuf_Compare(UsefulBuf_FROM_SZ_LITERAL("unbounce"), Out) ||
UsefulOutBuf_GetError(&UOB)) {
@@ -135,7 +135,7 @@
goto Done;
}
- Out = UsefulOutBuf_SubString(&UOB, 0, Expected.len);
+ Out = UsefulOutBuf_OutSubString(&UOB, 0, Expected.len);
if(UsefulBuf_IsNULLC(Out) ||
UsefulBuf_Compare(Expected, Out) ||
UsefulOutBuf_GetError(&UOB)) {
@@ -143,7 +143,7 @@
goto Done;
}
- Out = UsefulOutBuf_SubString(&UOB, Expected.len, 0);
+ Out = UsefulOutBuf_OutSubString(&UOB, Expected.len, 0);
if(UsefulBuf_IsNULLC(Out) ||
UsefulBuf_Compare(UsefulBuf_FROM_SZ_LITERAL(""), Out) ||
UsefulOutBuf_GetError(&UOB)) {
@@ -232,8 +232,11 @@
const char *UOBTest_BoundaryConditionsTest(void)
{
UsefulBuf_MAKE_STACK_UB(outbuf, 2);
+ UsefulOutBuf UOB_UnInitialized;
+ UsefulOutBuf UOB;
- UsefulOutBuf UOB;
+ /* For test detection of uninitlized UOB (wrong magic number) */
+ memset(&UOB_UnInitialized, 42, sizeof(UsefulInputBuf));
UsefulOutBuf_Init(&UOB, outbuf);
@@ -284,26 +287,31 @@
return "insert with data should have failed";
}
+ UsefulOutBuf_InsertString(&UOB_UnInitialized, "abc123", 0);
+ if(!UsefulOutBuf_GetError(&UOB_UnInitialized)) {
+ return "InsertString failed uninit detect";
+ }
+
UsefulOutBuf_Init(&UOB, outBuf2);
UsefulOutBuf_AppendString(&UOB, "abc123");
- UsefulBufC Out = UsefulOutBuf_SubString(&UOB, 7, 1);
+ UsefulBufC Out = UsefulOutBuf_OutSubString(&UOB, 7, 1);
if(!UsefulBuf_IsNULLC(Out)) {
return "SubString start should fail off end 1";
}
- Out = UsefulOutBuf_SubString(&UOB, 5, 3);
+ Out = UsefulOutBuf_OutSubString(&UOB, 5, 3);
if(!UsefulBuf_IsNULLC(Out)) {
return "SubString len should fail off end 2";
}
- Out = UsefulOutBuf_SubString(&UOB, 0, 7);
+ Out = UsefulOutBuf_OutSubString(&UOB, 0, 7);
if(!UsefulBuf_IsNULLC(Out)) {
return "SubString len should fail off end 3";
}
- Out = UsefulOutBuf_SubString(&UOB, 7, 0);
+ Out = UsefulOutBuf_OutSubString(&UOB, 7, 0);
if(!UsefulBuf_IsNULLC(Out)) {
return "SubString len should fail off end 4";
}
- Out = UsefulOutBuf_SubString(&UOB, 6, 1);
+ Out = UsefulOutBuf_OutSubString(&UOB, 6, 1);
if(!UsefulBuf_IsNULLC(Out)) {
return "SubString len should fail off end 5";
}
@@ -313,6 +321,17 @@
if(UsefulOutBuf_GetError(&UOB)) {
return "insert in huge should have succeeded";
}
+ UsefulOutBuf_Init(&UOB, outBuf2);
+ Out = UsefulOutBuf_OutSubString(&UOB, 0, 1);
+ if(!UsefulBuf_IsNULLC(Out)) {
+ return "SubString len should fail off end 5";
+ }
+ Out = UsefulOutBuf_OutSubString(&UOB_UnInitialized, 0, 1);
+ if(!UsefulBuf_IsNULLC(Out)) {
+ return "SubString failed uninit detect";
+ }
+
+
UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -5);
@@ -1091,13 +1110,18 @@
const char * UOBExtraTests(void)
{
#define COMPARE_TEST_SIZE 10
- UsefulOutBuf_MakeOnStack( UOB, COMPARE_TEST_SIZE);
+ UsefulOutBuf_MakeOnStack( UOB, COMPARE_TEST_SIZE); /* includes initialization */
int nCompare;
UsefulBufC Out;
+ UsefulOutBuf UOB_UnInitialized;
- /* Test UsefulOutBuf_Compare() */
+ /* For test detection of uninitlized UOB (wrong magic number) */
+ memset(&UOB_UnInitialized, 42, sizeof(UsefulInputBuf));
+
+ /* --- Test UsefulOutBuf_Compare() --- */
UsefulOutBuf_AppendString(&UOB, "abcabdefab");
+ /* Tests of equal length, not off the end. */
nCompare = UsefulOutBuf_Compare(&UOB, 0, 2, 8, 2);
if(nCompare != 0) {
return "ab should compare equal";
@@ -1108,6 +1132,11 @@
return "abc should not equal abd";
}
+ nCompare = UsefulOutBuf_Compare(&UOB, 3, 3, 0, 3);
+ if(nCompare != 'c' - 'd') {
+ return "abd should not equal abc";
+ }
+
nCompare = UsefulOutBuf_Compare(&UOB, 3, 2, 8, 2);
if(nCompare != 0) {
return "ab should compare equal";
@@ -1138,41 +1167,94 @@
return "b should compare equal to b";
}
+ /* Tests off end */
nCompare = UsefulOutBuf_Compare(&UOB, 10, 1, 10, 1);
if(nCompare != 0) {
- return "Comparison off the end is equal";
+ return "Comparison off the end is not equal";
}
nCompare = UsefulOutBuf_Compare(&UOB, 0, 1, 100, 1);
- if(nCompare != 0) {
- return "Comparison off the end is equal";
+ if(nCompare != -1) {
+ return "Comparison off end fail 1";
}
nCompare = UsefulOutBuf_Compare(&UOB, 100, 1, 0, 1);
- if(nCompare != 0) {
- return "Comparison off the end is equal";
+ if(nCompare != 1) {
+ return "Comparison off end fail 2";
}
+ nCompare = UsefulOutBuf_Compare(&UOB, 8, 3, 0, 3);
+ if(nCompare != 1) {
+ return "Comparison off end fail 3";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 0, 3, 8, 3);
+ if(nCompare != -1) {
+ return "Comparison off end fail 4";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 10, 1, 5, 1);
+ if(nCompare != 1) {
+ return "Comparison off end fail 5";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 5, 1, 10, 1);
+ if(nCompare != -1) {
+ return "Comparison off end fail 6";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 8, 3, 0, 2);
+ if(nCompare != 0) {
+ return "Comparison off end fail 7";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 0, 2, 8, 100);
+ if(nCompare != 0) {
+ return "Comparison off end fail 8";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 0, 0, 0, 0);
+ if(nCompare != 0) {
+ return "Empty compare failed";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 1, 1, 9, 3);
+ if(nCompare != 0) {
+ return "Unequal lengths, off end";
+ }
+
+ /* Tests of unequal lengths */
nCompare = UsefulOutBuf_Compare(&UOB, 0, 3, 3, 2);
- if(nCompare > 0) {
+ if(nCompare >= 0) {
return "Comparison of unequal lengths incorrect";
}
nCompare = UsefulOutBuf_Compare(&UOB, 8, 2, 0, 3);
- if(nCompare < 0) {
+ if(nCompare <= 0) {
return "Comparison of unequal lengths incorrect 2";
}
nCompare = UsefulOutBuf_Compare(&UOB, 0, 2, 2, 3);
if(nCompare != 'c' - 'a') {
- return "Inequal lengths, but inequal strings";
+ return "Unequal lengths, unequal strings";
}
nCompare = UsefulOutBuf_Compare(&UOB, 1, 3, 4, 2);
if(nCompare != 'd' - 'c') {
- return "Inequal lengths, but inequal strings";
+ return "Unequal lengths, unequal strings";
}
+ nCompare = UsefulOutBuf_Compare(&UOB, 7, 0, 7, 1);
+ if(nCompare != 1) {
+ return "zero length unequal length compare";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 0, 8, 3, 5);
+ if(nCompare != 'd' - 'c') {
+ return "another unequal length compare";
+ }
+
+
/* Test UsefulOutBuf_Swap() */
UsefulOutBuf_Reset(&UOB);
@@ -1286,5 +1368,10 @@
return "GetOutput fail 5";
}
+ Out = UsefulOutBuf_OutUBufOffset(&UOB_UnInitialized, 1);
+ if(!UsefulBuf_IsNULLC(Out)) {
+ return "GetOutput fail 7";
+ }
+
return NULL;
}
diff --git a/test/float_tests.c b/test/float_tests.c
index f32d181..d36ef61 100644
--- a/test/float_tests.c
+++ b/test/float_tests.c
@@ -323,12 +323,17 @@
{"\xFB\x7F\xEF\xFF\xFF\xFF\xFF\xFF\xFF", 9}, {"\xFB\x7F\xEF\xFF\xFF\xFF\xFF\xFF\xFF", 9},
{"\xFB\x7F\xEF\xFF\xFF\xFF\xFF\xFF\xFF", 9}, {"\xFB\x7F\xEF\xFF\xFF\xFF\xFF\xFF\xFF", 9}},
+ /* -18446744073709551616.0 -- largest that encodes into negative uint64 (65-bit neg) */
+ {-18446744073709551616.0, -18446744073709551616.0f,
+ {"\xFA\xDF\x80\x00\x00", 5}, {"\xFB\xC3\xF0\x00\x00\x00\x00\x00\x00", 9},
+ {"\xFA\xDF\x80\x00\x00", 5}, {"\x3B\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9}},
+
/* List terminator */
{0.0, 0.0f, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0} }
};
-/* Can't use types double and float here because there's no way in C to
+/* Can't use the types double and float here because there's no way in C to
* construct arbitrary payloads for those types.
*/
struct NaNTestCase {
@@ -416,7 +421,7 @@
for(uTestIndex = 0; FloatTestCases[uTestIndex].Preferred.len != 0; uTestIndex++) {
pTestCase = &FloatTestCases[uTestIndex];
- if(uTestIndex == 2) {
+ if(uTestIndex == 48) {
uDecoded = 1;
}
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index 8b77a11..af5b124 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -1080,6 +1080,27 @@
}
}
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
+ QCBORError uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
+#else
+ QCBORError uExpectedErr = QCBOR_SUCCESS;
+#endif /* !QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
+ /* Test failure for attempting non-prefered serialiation */
+ QCBOREncode_Init(&Enc, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_Config(&Enc, QCBOR_ENCODE_CONFIG_ONLY_PREFERRED_BIG_NUMBERS);
+ QCBOREncode_AddTBigNumberRaw(&Enc, QCBOR_ENCODE_AS_TAG, false, UsefulBuf_FROM_SZ_LITERAL("\x00"));
+ if(QCBOREncode_GetErrorState(&Enc) != uExpectedErr) {
+ return -1;
+ }
+
+ QCBOREncode_Init(&Enc, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_Config(&Enc, QCBOR_ENCODE_CONFIG_ONLY_PREFERRED_BIG_NUMBERS);
+ QCBOREncode_AddTBigNumberNoPreferred(&Enc, QCBOR_ENCODE_AS_TAG, false, UsefulBuf_FROM_SZ_LITERAL("\x00"));
+ if(QCBOREncode_GetErrorState(&Enc) != uExpectedErr) {
+ return -2;
+ }
+
return 0;
}
@@ -3532,6 +3553,85 @@
}
+#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
+
+struct SortTest {
+ const char *szDescription;
+ UsefulBufC ToBeSorted;
+ UsefulBufC Sorted;
+ QCBORError uError;
+};
+
+static const struct SortTest sSortTests[] =
+{
+ {
+ "Simple Sort Test",
+ {"\x03\x03\x01\x01\x04\x04\x02\x02", 8},
+ {"\xBF\x01\x01\x02\x02\x03\x03\x04\x04\xFF", 10},
+ QCBOR_SUCCESS
+ },
+
+ {
+ "Not well formed label",
+ {"\x1c\x03\x01\x01\x04\x04\x02\x02", 8},
+ NULLUsefulBufC,
+ QCBOR_ERR_UNSUPPORTED
+ },
+
+ {
+ "Not well formed value",
+ {"\x03\x1c\x01\x01\x04\x04\x02\x02", 8},
+ NULLUsefulBufC,
+ QCBOR_ERR_UNSUPPORTED
+ },
+
+ {
+ "Not well formed label at end",
+ {"\x03\x03\x01\x01\x04\x04\x1c\x02", 8},
+ NULLUsefulBufC,
+ QCBOR_ERR_UNSUPPORTED
+ },
+
+ {
+ "Extraneous break",
+ {"\x03\x03\x01\x01\x04\xff\x02\x02", 8},
+ NULLUsefulBufC,
+ QCBOR_ERR_HIT_END
+ },
+
+ {
+ "Off end",
+ {"\x03\x03\x01\x01\x04\x04\x02\x6f\x68\x69", 10},
+ NULLUsefulBufC,
+ QCBOR_ERR_HIT_END
+ },
+
+ {
+ "Indef string chunk in error",
+ {"\x03\x03\x5f\x61\x68\x1c\xff\x01\x04\x04\x02\x02", 12},
+ NULLUsefulBufC,
+ QCBOR_ERR_UNSUPPORTED
+ },
+
+ {
+ "All sorts of labels",
+ {"\x81\x00\x03\xFB\x40\x09\x1E\xB8\x51\xEB\x85\x1F\x01\xf4\x04\xa2"
+ "\x05\x05\x06\x06\x02\xc1\x38\xff\x05\x81\x01\x06\x81\x20\x07\x19"
+ "\x00\x01\x08", 35},
+ {"\xBF\x19\x00\x01\x08\x81\x00\x03\x81\x01\x06\x81\x20\x07\xA2\x05"
+ "\x05\x06\x06\x02\xC1\x38\xFF\x05\xF4\x04\xFB\x40\x09\x1E\xB8\x51"
+ "\xEB\x85\x1F\x01\xFF", 37},
+ QCBOR_SUCCESS
+ },
+
+ {
+ NULL,
+ NULLUsefulBufC,
+ NULLUsefulBufC,
+ QCBOR_SUCCESS
+ }
+};
+#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
int32_t
SortMapTest(void)
@@ -3542,6 +3642,44 @@
QCBORError uErr;
struct UBCompareDiagnostic CompareDiagnostics;
+#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
+
+ for(int nIndex = 0; ; nIndex++) {
+ const struct SortTest *pTest = &sSortTests[nIndex];
+
+ if(pTest->szDescription == NULL) {
+ break;
+ }
+
+ if(nIndex == 7) {
+ uErr = 0; /* For break point */
+ }
+
+ QCBOREncode_Init(&EC, TestBuf);
+ /* Have to use indefinite length map to make test work */
+ QCBOREncode_OpenMapIndefiniteLength(&EC);
+ /* Violating layering here to be able to make test work.
+ * In particular to test error handling when the CBOR
+ * being sorted is not well formed. That should never happen
+ * but it needs to be tested for code safety.
+ */
+ UsefulOutBuf_AppendUsefulBuf(&(EC.OutBuf), pTest->ToBeSorted);
+ QCBOREncode_CloseAndSortMapIndef(&EC);
+ uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
+ if(uErr != pTest->uError) {
+ return MakeTestResultCode((uint32_t)nIndex, 0, uErr);
+ }
+
+ if(uErr == QCBOR_SUCCESS && !UsefulBuf_IsNULLC(pTest->Sorted)) {
+ if(UsefulBuf_Compare(pTest->Sorted, EncodedAndSorted)) {
+ return MakeTestResultCode((uint32_t)nIndex, 1, uErr);
+
+ }
+ }
+ }
+#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
+ // TODO: Move most of the tests below into sSortTests
+
/* --- Basic sort test case --- */
QCBOREncode_Init(&EC, TestBuf);
@@ -4086,12 +4224,10 @@
QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
QCBOREncode_Config(&EC, QCBOR_ENCODE_CONFIG_DCBOR);
QCBOREncode_AddUndef(&EC);
- QCBOREncode_CloseMap(&EC);
if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
return 102;
}
-
/* Improvement: when indefinite length string encoding is supported
* test it here too. */