Encode test coverage increased to 100%
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 3986cbb..aa0b991 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -111,7 +111,7 @@
Nesting_Increment(QCBORTrackNesting *pNesting)
{
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
- if(1 >= QCBOR_MAX_ITEMS_IN_ARRAY - pNesting->pCurrentNesting->uCount) {
+ if(pNesting->pCurrentNesting->uCount >= QCBOR_MAX_ITEMS_IN_ARRAY) {
return QCBOR_ERR_ARRAY_TOO_LONG;
}
#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index 44f97ab..ab2ed90 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -959,10 +959,9 @@
static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
-int32_t SimpleValuesIndefiniteLengthTest1(void)
+int32_t IndefiniteLengthTest(void)
{
QCBOREncodeContext ECtx;
- int nReturn = 0;
QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
@@ -981,13 +980,38 @@
UsefulBufC ECBOR;
if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
- nReturn = -1;
+ return -1;
}
- if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
+ if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength)) {
return -2;
+ }
- return(nReturn);
+
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
+ QCBOREncode_CloseArray(&ECtx);
+ if(QCBOREncode_GetErrorState(&ECtx) != QCBOR_ERR_CLOSE_MISMATCH) {
+ return -3;
+ }
+
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_OpenArray(&ECtx);
+ QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
+ if(QCBOREncode_GetErrorState(&ECtx) != QCBOR_ERR_CLOSE_MISMATCH) {
+ return -3;
+ }
+
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
+ QCBOREncode_CloseMapIndefiniteLength(&ECtx);
+ if(QCBOREncode_GetErrorState(&ECtx) != QCBOR_ERR_CLOSE_MISMATCH) {
+ return -3;
+ }
+#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
+ return 0;
}
#endif
@@ -1905,6 +1929,24 @@
return -11;
}
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
+ // Seventh test, erroneous cancel
+ QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_CancelBstrWrap(&EC);
+ uErr = QCBOREncode_GetErrorState(&EC);
+ if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
+ return -12;
+ }
+
+ QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_CancelBstrWrap(&EC);
+ uErr = QCBOREncode_GetErrorState(&EC);
+ if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
+ return -13;
+ }
+#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
return 0;
}
@@ -2721,6 +2763,24 @@
return -130;
}
+
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
+ /* ------ QCBOR_ERR_ARRAY_TOO_LONG -------- */
+ QCBOREncode_Init(&EC, Large);
+ QCBOREncode_OpenArray(&EC);
+ int i;
+ for(i = 0; i < QCBOR_MAX_ITEMS_IN_ARRAY; i++) {
+ QCBOREncode_AddInt64(&EC, 0);
+ }
+ if(QCBOREncode_GetErrorState(&EC)) {
+ return 250;
+ }
+ QCBOREncode_AddInt64(&EC, 0);
+ if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_ARRAY_TOO_LONG) {
+ return 251;
+ }
+#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
return 0;
}
@@ -2729,26 +2789,34 @@
/*
[
4([-1, 3]),
+ [-1, 4],
4([-20, 4759477275222530853136]),
+ [2, 4759477275222530853136],
4([9223372036854775807, -4759477275222530853137]),
5([300, 100]),
+ [600, 200],
5([-20, 4759477275222530853136]),
- 5([-9223372036854775808, -4759477275222530853137])
- ]
+ [4, 4759477275222530853136],
+ 5([-9223372036854775808, -4759477275222530853137])]
+ ]
*/
static const uint8_t spExpectedExponentAndMantissaArray[] = {
- 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
- 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
- 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
- 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
- 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
- 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
- 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
- 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
-
+ 0x8A, 0xC4, 0x82, 0x20, 0x03, 0x82, 0x20, 0x04,
+ 0xC4, 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03,
+ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x82,
+ 0x02, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
+ 0x06, 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B,
+ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01,
+ 0x2C, 0x18, 0x64, 0x82, 0x19, 0x02, 0x58, 0x18,
+ 0xC8, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02,
+ 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
+ 0x82, 0x04, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
+ 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0xC5, 0x82,
+ 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
+ 0x06, 0x07, 0x08, 0x09, 0x10};
/*
{
@@ -2827,10 +2895,14 @@
QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
QCBOREncode_OpenArray(&EC);
QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
+ QCBOREncode_AddTDecimalFraction(&EC, QCBOR_ENCODE_AS_BORROWED, 4, -1); // 3 * (10 ^ -1)
QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
+ QCBOREncode_AddTDecimalFractionBigNum(&EC, QCBOR_ENCODE_AS_BORROWED, BigNum , false, 2);
QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
QCBOREncode_AddBigFloat(&EC, 100, 300);
+ QCBOREncode_AddTBigFloat(&EC, QCBOR_ENCODE_AS_BORROWED, 200, 600);
QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
+ QCBOREncode_AddTBigFloatBigNum(&EC, QCBOR_ENCODE_AS_BORROWED, BigNum, false, 4);
QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
QCBOREncode_CloseArray(&EC);
diff --git a/test/qcbor_encode_tests.h b/test/qcbor_encode_tests.h
index 43a6290..bc47c55 100644
--- a/test/qcbor_encode_tests.h
+++ b/test/qcbor_encode_tests.h
@@ -108,7 +108,7 @@
/*
Encodes basic maps and arrays with indefinite length
*/
-int32_t SimpleValuesIndefiniteLengthTest1(void);
+int32_t IndefiniteLengthTest(void);
/*
diff --git a/test/run_tests.c b/test/run_tests.c
index 140eb53..81b9053 100644
--- a/test/run_tests.c
+++ b/test/run_tests.c
@@ -146,7 +146,7 @@
TEST_ENTRY_DISABLED(TooLargeInputTest),
TEST_ENTRY(EncodeErrorTests),
#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
- TEST_ENTRY(SimpleValuesIndefiniteLengthTest1),
+ TEST_ENTRY(IndefiniteLengthTest),
#endif
TEST_ENTRY(EncodeLengthThirtyoneTest),
TEST_ENTRY(CBORSequenceDecodeTests),