Add support for day count dates, RFC 8943 (#106)
This addresses #98
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 236f59c..779b094 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -2576,7 +2576,7 @@
0xc0, // tag for string date
0xa0, // Erroneous empty map as content for date
- 0xa9, // Open a map for tests involving labels.
+ 0xad, // Open a map for tests involving labels.
0x00,
0xc0, // tag for string date
@@ -2615,6 +2615,25 @@
// Untagged half-precision float with value -2
0x09,
0xF9, 0xC0, 0x00,
+
+ /* Tagged date-only date string */
+ 0x63, 0x53, 0x44, 0x53,
+ 0xD9, 0x03, 0xEC,
+ 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
+
+ /* Untagged date-only date string */
+ 0x18, 0x63,
+ 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
+
+ /* Tagged days-count epoch date */
+ 0x63, 0x53, 0x44, 0x45,
+ 0xD8, 0x64, /* tag(100) */
+ 0x39, 0x29, 0xB3, /* -10676 */
+
+ /* Untagged days-count epoch date */
+ 0x11,
+ 0x19, 0x0F, 0x9A /* 3994 */
+
};
int32_t SpiffyDateDecodeTest()
@@ -2623,8 +2642,8 @@
QCBORError uError;
int64_t nEpochDate2, nEpochDate3, nEpochDate5,
nEpochDate4, nEpochDate6, nEpochDateFail,
- nEpochDate1400000000;
- UsefulBufC StringDate1, StringDate2;
+ nEpochDate1400000000, nEpochDays1, nEpochDays2;
+ UsefulBufC StringDate1, StringDate2, StringDays1, StringDays2;
uint64_t uTag1, uTag2;
QCBORDecode_Init(&DC,
@@ -2804,6 +2823,26 @@
&nEpochDate6);
uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
+ /* The days format is much simpler than the date format
+ * because it can't be a floating point value. The test
+ * of the spiffy decode functions sufficiently covers
+ * the test of the non-spiffy decode days date decoding.
+ * There is no full fan out of the error conditions
+ * and decode options as that is implemented by code
+ * that is tested well by the date testing above.
+ */
+ QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
+ &StringDays1);
+
+ QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
+ &StringDays2);
+
+ QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
+ &nEpochDays1);
+
+ QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
+ &nEpochDays2);
+
QCBORDecode_ExitMap(&DC);
QCBORDecode_ExitArray(&DC);
uError = QCBORDecode_Finish(&DC);
@@ -2831,6 +2870,14 @@
return 204;
}
+ if(nEpochDays1 != -10676) {
+ return 205;
+ }
+
+ if(nEpochDays2 != 3994) {
+ return 206;
+ }
+
if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
return 205;
}
@@ -2839,6 +2886,14 @@
return 206;
}
+ if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
+ return 207;
+ }
+
+ if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
+ return 208;
+ }
+
return 0;
}
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index 6655aed..18116cb 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -1170,69 +1170,102 @@
/*
- 83 # array(3)
- C0 # tag(0)
- 74 # text(20)
- 323031332D30332D32315432303A30343A30305A # "2013-03-21T20:04:00Z"
- C1 # tag(1)
- 1A 514B67B0 # unsigned(1363896240)
- A2 # map(2)
- 78 19 # text(25)
- 53616D706C6520446174652066726F6D205246432033333339 # "Sample Date from RFC 3339"
- C0 # tag(0)
- 77 # text(23)
- 313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z"
- 62 # text(2)
- 5344 # "SD"
- C1 # tag(1)
- 19 03E7 # unsigned(999)
+ * [ "2013-03-21T20:04:00Z",
+ * 0("2013-03-21T20:04:00Z"),
+ * 1363896240,
+ * 1(1363896240),
+ * 100(-10676),
+ * 3994,
+ * 1004("1940-10-09"),
+ * "1980-12-08",
+ * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
+ * "SD": 1(999),
+ * "Sample Date from RFC 8943": "1985-04-12",
+ * 42: 1004("1985-04-12T23:20:50.52Z"),
+ * "SY": 100(-10676),
+ * 45: 3994
+ * }
+ * ]
*/
static const uint8_t spExpectedEncodedDates[] = {
- 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33,
- 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a,
- 0x30, 0x30, 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
- 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44,
- 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x52,
- 0x46, 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31,
- 0x39, 0x38, 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
- 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, 0x2e, 0x35,
- 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, 0x19, 0x03, 0xe7
-};
+ 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
+ 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
+ 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
+ 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
+ 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
+ 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
+ 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
+ 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
+ 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
+ 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
+ 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
+ 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
+ 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
+ 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
+ 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
+ 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
+ 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
+ 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
+ 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
+ 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
+ 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
+ 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
+ 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
int32_t EncodeDateTest()
{
QCBOREncodeContext ECtx;
- int nReturn = 0;
QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
QCBOREncode_OpenArray(&ECtx);
-
- QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
- QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
-
+ /* The values are taken from the CBOR RFCs */
+ QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
+ QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
+ QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
+ QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
+ QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
+ QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
+ QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
+ QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
QCBOREncode_OpenMap(&ECtx);
- QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
-
+ QCBOREncode_AddDateStringToMap(&ECtx,
+ "Sample Date from RFC 3339",
+ "1985-04-12T23:20:50.52Z");
QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
+ QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
+ "Sample Date from RFC 8943",
+ QCBOR_ENCODE_AS_BORROWED,
+ "1985-04-12");
+ QCBOREncode_AddTDaysStringToMapN(&ECtx,
+ 42,
+ QCBOR_ENCODE_AS_TAG,
+ "1985-04-12T23:20:50.52Z");
+ QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
+ "SY",
+ QCBOR_ENCODE_AS_TAG,
+ -10676);
+ QCBOREncode_AddTDaysEpochToMapN(&ECtx,
+ 45,
+ QCBOR_ENCODE_AS_BORROWED,
+ 3994);
QCBOREncode_CloseMap(&ECtx);
QCBOREncode_CloseArray(&ECtx);
UsefulBufC ECBOR;
-
if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
- nReturn = -1;
+ return -1;
}
if(CheckResults(ECBOR, spExpectedEncodedDates))
return -2;
- return(nReturn);
+ return 0;
}
diff --git a/test/qcbor_encode_tests.h b/test/qcbor_encode_tests.h
index ae64f7d..69f694a 100644
--- a/test/qcbor_encode_tests.h
+++ b/test/qcbor_encode_tests.h
@@ -125,7 +125,8 @@
/*
- Encodes most data formats that are supported */
+ * Tests Encoding most data formats that are supported.
+ */
int32_t EncodeDateTest(void);