QCBOR: Add CBOR encoder / decoder library
QCBOR supports encoding and decoding of most
of the CBOR standard, RFC 7049. QCBOR is open
source maintained at
https://github.com/laurencelundblade/QCBOR
Change-Id: I5632379e4a1fdb16e0df7f03dfa2374160b7ed7f
Signed-off-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/lib/ext/qcbor/test/UsefulBuf_Tests.c b/lib/ext/qcbor/test/UsefulBuf_Tests.c
new file mode 100644
index 0000000..cfa1262
--- /dev/null
+++ b/lib/ext/qcbor/test/UsefulBuf_Tests.c
@@ -0,0 +1,700 @@
+/*==============================================================================
+ Copyright (c) 2016-2018, The Linux Foundation.
+ Copyright (c) 2018-2019, Laurence Lundblade.
+ All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of The Linux Foundation nor the names of its
+ contributors, nor the name "Laurence Lundblade" may be used to
+ endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ ==============================================================================*/
+
+#include "UsefulBuf.h"
+
+
+/* Basic exercise...
+
+ Call all the main public functions.
+
+ Binary compare the result to the expected.
+
+ There is nothing adversarial in this test
+ */
+const char * UOBTest_NonAdversarial()
+{
+ const char *szReturn = NULL;
+
+ UsefulBuf_MAKE_STACK_UB(outbuf,50);
+
+ UsefulOutBuf UOB;
+
+ UsefulOutBuf_Init(&UOB, outbuf);
+
+ if(!UsefulOutBuf_AtStart(&UOB)) {
+ szReturn = "Not at start";
+ goto Done;
+ }
+
+ // Put 7 bytes at beginning of buf
+ UsefulOutBuf_AppendData(&UOB, "bluster", 7);
+
+ if(UsefulOutBuf_AtStart(&UOB)) {
+ szReturn = "At start";
+ goto Done;
+ }
+
+ // add a space to end
+ UsefulOutBuf_AppendByte(&UOB, ' ');
+
+ // Add 5 bytes to the end
+ UsefulBufC UBC = {"hunny", 5};
+ UsefulOutBuf_AppendUsefulBuf(&UOB, UBC);
+
+ // Insert 9 bytes at the beginning, slide the previous stuff right
+ UsefulOutBuf_InsertData(&UOB, "heffalump", 9, 0);
+ UsefulOutBuf_InsertByte(&UOB, ' ', 9);
+
+ // Put 9 bytes in at position 10 -- just after "heffalump "
+ UsefulBufC UBC2 = {"unbounce ", 9};
+ UsefulOutBuf_InsertUsefulBuf(&UOB, UBC2, 10);
+
+ // Make it a null terminated string (because all the appends and inserts above not strcpy !)
+ UsefulOutBuf_AppendByte(&UOB, '\0');
+
+
+ UsefulBufC U = UsefulOutBuf_OutUBuf(&UOB);
+
+ const char *expected = "heffalump unbounce bluster hunny";
+
+ if(UsefulBuf_IsNULLC(U) || U.len-1 != strlen(expected) || strcmp(expected, U.ptr) || UsefulOutBuf_GetError(&UOB)) {
+ szReturn = "OutUBuf";
+ }
+
+ UsefulBuf_MAKE_STACK_UB(buf, 50);
+ UsefulBufC Out = UsefulOutBuf_CopyOut(&UOB, buf);
+
+ if(UsefulBuf_IsNULLC(Out) || Out.len-1 != strlen(expected) || strcmp(expected, Out.ptr)) {
+ szReturn = "CopyOut";
+ }
+
+Done:
+ return szReturn;
+}
+
+
+/*
+ Append test utility.
+ pUOB is the buffer to append too
+ num is the amount to append
+ expected is the expected return code, 0 or 1
+
+ returns 0 if test passed
+
+ */
+static int AppendTest(UsefulOutBuf *pUOB, size_t num, int expected)
+{
+ //reset
+ UsefulOutBuf_Reset(pUOB);
+
+ // check status first
+ if(UsefulOutBuf_GetError(pUOB))
+ return 1;
+
+ // append the bytes
+ UsefulOutBuf_AppendData(pUOB, (const uint8_t *)"bluster", num);
+
+ // check error status after
+ if(UsefulOutBuf_GetError(pUOB) != expected)
+ return 1;
+
+ return 0;
+}
+
+
+/*
+ Same as append, but takes a position param too
+ */
+static int InsertTest(UsefulOutBuf *pUOB, size_t num, size_t pos, int expected)
+{
+ // reset
+ UsefulOutBuf_Reset(pUOB);
+
+ // check
+ if(UsefulOutBuf_GetError(pUOB))
+ return 1;
+
+ UsefulOutBuf_InsertData(pUOB, (const uint8_t *)"bluster", num, pos);
+
+ if(UsefulOutBuf_GetError(pUOB) != expected)
+ return 1;
+
+ return 0;
+}
+
+
+/*
+ Boundary conditions to test
+ - around 0
+ - around the buffer size
+ - around MAX size_t
+
+
+ Test these for the buffer size and the cursor, the insert amount, the append amount and the insert position
+
+ */
+
+const char *UOBTest_BoundaryConditionsTest()
+{
+ UsefulBuf_MAKE_STACK_UB(outbuf,2);
+
+ UsefulOutBuf UOB;
+
+ UsefulOutBuf_Init(&UOB, outbuf);
+
+ // append 0 byte to a 2 byte buffer --> success
+ if(AppendTest(&UOB, 0, 0))
+ return "Append 0 bytes failed";
+
+ // append 1 byte to a 2 byte buffer --> success
+ if(AppendTest(&UOB, 1, 0))
+ return "Append of 1 byte failed";
+
+ // append 2 byte to a 2 byte buffer --> success
+ if(AppendTest(&UOB, 2, 0))
+ return "Append to fill buffer failed";
+
+ // append 3 bytes to a 2 byte buffer --> failure
+ if(AppendTest(&UOB, 3, 1))
+ return "Overflow of buffer not caught";
+
+ // append max size_t to a 2 byte buffer --> failure
+ if(AppendTest(&UOB, SIZE_MAX, 1))
+ return "Append of SIZE_MAX error not caught";
+
+ if(InsertTest(&UOB, 1, 0, 0))
+ return "Insert 1 byte at start failed";
+
+ if(InsertTest(&UOB, 2, 0, 0))
+ return "Insert 2 bytes at start failed";
+
+ if(InsertTest(&UOB, 3, 0, 1))
+ return "Insert overflow not caught";
+
+ if(InsertTest(&UOB, 1, 1, 1))
+ return "Bad insertion point not caught";
+
+
+ UsefulBuf_MAKE_STACK_UB(outBuf2,10);
+
+ UsefulOutBuf_Init(&UOB, outBuf2);
+
+ UsefulOutBuf_Reset(&UOB);
+ // put data in the buffer
+ UsefulOutBuf_AppendString(&UOB, "abc123");
+
+ UsefulOutBuf_InsertString(&UOB, "xyz*&^", 0);
+
+ if(!UsefulOutBuf_GetError(&UOB)) {
+ return "insert with data should have failed";
+ }
+
+
+ UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
+ UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -6);
+ if(UsefulOutBuf_GetError(&UOB)) {
+ return "insert in huge should have succeeded";
+ }
+
+ UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
+ UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -5);
+ if(UsefulOutBuf_GetError(&UOB)) {
+ return "insert in huge should have succeeded";
+ }
+
+ UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
+ UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX - 4);
+ if(!UsefulOutBuf_GetError(&UOB)) {
+ return "lengths near max size";
+ }
+
+ return NULL;
+}
+
+
+
+
+
+// Test function to get size and magic number check
+
+const char *TestBasicSanity()
+{
+ UsefulBuf_MAKE_STACK_UB(outbuf,10);
+
+ UsefulOutBuf UOB;
+
+ // First -- make sure that the room left function returns the right amount
+ UsefulOutBuf_Init(&UOB, outbuf);
+
+ if(UsefulOutBuf_RoomLeft(&UOB) != 10)
+ return "room left failed";
+
+ if(!UsefulOutBuf_WillItFit(&UOB, 9)) {
+ return "it did not fit";
+ }
+
+ if(UsefulOutBuf_WillItFit(&UOB, 11)) {
+ return "it should have not fit";
+ }
+
+
+ // Next -- make sure that the magic number checking is working right
+ UOB.magic = 8888; // make magic bogus
+
+ UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
+
+ if(!UsefulOutBuf_GetError(&UOB))
+ return "magic corruption check failed";
+
+
+
+ // Next make sure that the valid data length check is working right
+ UsefulOutBuf_Init(&UOB, outbuf);
+
+ UOB.data_len = UOB.UB.len+1; // make size bogus
+
+ UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
+ if(!UsefulOutBuf_GetError(&UOB))
+ return "valid data check failed";
+
+ return NULL;
+}
+
+
+
+const char *UBMacroConversionsTest()
+{
+ char *szFoo = "foo";
+
+ UsefulBufC Foo = UsefulBuf_FromSZ(szFoo);
+ if(Foo.len != 3 || strncmp(Foo.ptr, szFoo, 3))
+ return "SZToUsefulBufC failed";
+
+ UsefulBufC Too = UsefulBuf_FROM_SZ_LITERAL("Toooo");
+ if(Too.len != 5 || strncmp(Too.ptr, "Toooo", 5))
+ return "UsefulBuf_FROM_SZ_LITERAL failed";
+
+ uint8_t pB[] = {0x42, 0x6f, 0x6f};
+ UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);
+ if(Boo.len != 3 || strncmp(Boo.ptr, "Boo", 3))
+ return "UsefulBuf_FROM_BYTE_ARRAY_LITERAL failed";
+
+ char *sz = "not const"; // some data for the test
+ UsefulBuf B = (UsefulBuf){sz, sizeof(sz)};
+ UsefulBufC BC = UsefulBuf_Const(B);
+ if(BC.len != sizeof(sz) || BC.ptr != sz)
+ return "UsefulBufConst failed";
+
+ return NULL;
+}
+
+
+const char *UBUtilTests()
+{
+ UsefulBuf UB = NULLUsefulBuf;
+
+ if(!UsefulBuf_IsNULL(UB)){
+ return "IsNull failed";
+ }
+
+ if(!UsefulBuf_IsEmpty(UB)){
+ return "IsEmpty failed";
+ }
+
+ if(!UsefulBuf_IsNULLOrEmpty(UB)) {
+ return "IsNULLOrEmpty failed";
+ }
+
+ const UsefulBufC UBC = UsefulBuf_Const(UB);
+
+ if(!UsefulBuf_IsNULLC(UBC)){
+ return "IsNull const failed";
+ }
+
+ if(!UsefulBuf_IsEmptyC(UBC)){
+ return "IsEmptyC failed";
+ }
+
+ if(!UsefulBuf_IsNULLOrEmptyC(UBC)){
+ return "IsNULLOrEmptyC failed";
+ }
+
+ const UsefulBuf UB2 = UsefulBuf_Unconst(UBC);
+ if(!UsefulBuf_IsEmpty(UB2)) {
+ return "Back to UB is Empty failed";
+ }
+
+ UB.ptr = "x"; // just some valid pointer
+
+ if(UsefulBuf_IsNULL(UB)){
+ return "IsNull failed";
+ }
+
+ if(!UsefulBuf_IsEmptyC(UBC)){
+ return "IsEmpty failed";
+ }
+
+ // test the Unconst.
+ if(UsefulBuf_Unconst(UBC).ptr != NULL) {
+ return "Unconst failed";
+ }
+
+ // Set 100 bytes of '+'; validated a few tests later
+ UsefulBuf_MAKE_STACK_UB(Temp, 100);
+ const UsefulBufC TempC = UsefulBuf_Set(Temp, '+');
+
+ // Try to copy into a buf that is too small and see failure
+ UsefulBuf_MAKE_STACK_UB(Temp2, 99);
+ if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp2, TempC))) {
+ return "Copy should have failed";
+ }
+
+ if(UsefulBuf_IsNULLC(UsefulBuf_CopyPtr(Temp2, "xx", 2))) {
+ return "CopyPtr failed";
+ }
+
+ UsefulBufC xxyy = UsefulBuf_CopyOffset(Temp2, 2, UsefulBuf_FROM_SZ_LITERAL("yy"));
+ if(UsefulBuf_IsNULLC(xxyy)) {
+ return "CopyOffset Failed";
+ }
+
+ if(UsefulBuf_Compare(UsefulBuf_Head(xxyy, 3), UsefulBuf_FROM_SZ_LITERAL("xxy"))) {
+ return "head failed";
+ }
+
+ if(UsefulBuf_Compare(UsefulBuf_Tail(xxyy, 1), UsefulBuf_FROM_SZ_LITERAL("xyy"))) {
+ return "tail failed";
+ }
+
+ if(!UsefulBuf_IsNULLC(UsefulBuf_Head(xxyy, 5))) {
+ return "head should have failed";
+ }
+
+ if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(xxyy, 5))) {
+ return "tail should have failed";
+ }
+
+ if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(NULLUsefulBufC, 0))) {
+ return "tail of NULLUsefulBufC is not NULLUsefulBufC";
+ }
+
+ const UsefulBufC TailResult = UsefulBuf_Tail((UsefulBufC){NULL, 100}, 99);
+ if(TailResult.ptr != NULL || TailResult.len != 1) {
+ return "tail of NULL and length incorrect";
+ }
+
+ if(!UsefulBuf_IsNULLC(UsefulBuf_CopyOffset(Temp2, 100, UsefulBuf_FROM_SZ_LITERAL("yy")))) {
+ return "Copy Offset should have failed";
+ }
+
+ // Try to copy into a NULL/empty buf and see failure
+ const UsefulBuf UBNull = NULLUsefulBuf;
+ if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(UBNull, TempC))) {
+ return "Copy to NULL should have failed";
+ }
+
+
+ // Try to set a NULL/empty buf; nothing should happen
+ UsefulBuf_Set(UBNull, '+'); // This will crash on failure
+
+ // Copy successfully to a buffer
+ UsefulBuf_MAKE_STACK_UB(Temp3, 101);
+ if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp3, TempC))) {
+ return "Copy should not have failed";
+ }
+
+ static const uint8_t pExpected[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ };
+ UsefulBufC Expected = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpected);
+ // This validates comparison for equality and the UsefulBuf_Set
+ if(UsefulBuf_Compare(Expected, TempC)) {
+ return "Set / Copy / Compare failed";
+ }
+
+ // Compare two empties and expect success
+ if(UsefulBuf_Compare(NULLUsefulBufC, NULLUsefulBufC)){
+ return "Compare Empties failed";
+ }
+
+ // Compare with empty and expect the first to be larger
+ if(UsefulBuf_Compare(Expected, NULLUsefulBufC) <= 0){
+ return "Compare with empty failed";
+ }
+
+
+ static const uint8_t pExpectedBigger[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', ',',
+ };
+ const UsefulBufC ExpectedBigger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedBigger);
+
+ // Expect -1 when the first arg is smaller
+ if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){
+ return "Compare with bigger";
+ }
+
+
+ static const uint8_t pExpectedSmaller[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '*',
+ };
+ const UsefulBufC ExpectedSmaller = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedSmaller);
+ // Expect +1 when the first arg is larger
+ if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){
+ return "Compare with smaller";
+ }
+
+
+ static const uint8_t pExpectedLonger[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
+ };
+ const UsefulBufC ExpectedLonger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedLonger);
+
+ // Expect -1 when the first arg is smaller
+ if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){
+ return "Compare with longer";
+ }
+
+
+ static const uint8_t pExpectedShorter[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ };
+ const UsefulBufC ExpectedShorter = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedShorter);
+ // Expect +1 with the first arg is larger
+ if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){
+ return "Compare with shorter";
+ }
+
+
+ if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp, NULLUsefulBufC))) {
+ return "Copy null/empty failed";
+ }
+
+ // Look for +++++... in +++++... and find it at the beginning
+ if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedShorter)){
+ return "Failed to find";
+ }
+
+ // look for ++* in ....++* and find it at the end
+ static const uint8_t pToFind[] = {'+', '+', '*'};
+ const UsefulBufC ToBeFound = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pToFind);
+
+ if(97 != UsefulBuf_FindBytes(ExpectedSmaller, ToBeFound)){
+ return "Failed to find 2";
+ }
+
+ // look for ++* in ....++, and find it near the end
+ if(SIZE_MAX != UsefulBuf_FindBytes(ExpectedBigger, ToBeFound)){
+ return "Failed to not find";
+ }
+
+ // Look for the whole buffer in itself and succeed.
+ if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedLonger)){
+ return "Failed to find 3";
+ }
+
+ return NULL;
+}
+
+
+const char * UIBTest_IntegerFormat()
+{
+ UsefulOutBuf_MakeOnStack(UOB,100);
+
+ const uint32_t u32 = 0x0A0B0C0D; // from https://en.wikipedia.org/wiki/Endianness
+ const uint64_t u64 = 1984738472938472;
+ const uint16_t u16 = 40000;
+ const uint8_t u8 = 9;
+ const float f = (float)314.15;
+ const double d = 2.1e10;
+
+
+ UsefulOutBuf_AppendUint32(&UOB, u32); // Also tests UsefulOutBuf_InsertUint64 and UsefulOutBuf_GetEndPosition
+ UsefulOutBuf_AppendUint64(&UOB, u64); // Also tests UsefulOutBuf_InsertUint32
+ UsefulOutBuf_AppendUint16(&UOB, u16); // Also tests UsefulOutBuf_InsertUint16
+ UsefulOutBuf_AppendByte(&UOB, u8);
+ UsefulOutBuf_AppendFloat(&UOB, f); // Also tests UsefulOutBuf_InsertFloat
+ UsefulOutBuf_AppendDouble(&UOB, d); // Also tests UsefulOutBuf_InsertDouble
+
+ const UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB);
+ if(UsefulBuf_IsNULLC(O))
+ return "Couldn't output integers";
+
+ // from https://en.wikipedia.org/wiki/Endianness
+ const uint8_t pExpectedNetworkOrder[4] = {0x0A, 0x0B, 0x0C, 0x0D};
+ if(memcmp(O.ptr, pExpectedNetworkOrder, 4)) {
+ return "not in network order";
+ }
+
+ UsefulInputBuf UIB;
+
+ UsefulInputBuf_Init(&UIB, O);
+
+ if(UsefulInputBuf_Tell(&UIB) != 0) {
+ return "UsefulInputBuf_Tell failed";
+ }
+
+ if(UsefulInputBuf_GetUint32(&UIB) != u32) {
+ return "u32 out then in failed";
+ }
+ if(UsefulInputBuf_GetUint64(&UIB) != u64) {
+ return "u64 out then in failed";
+ }
+ if(UsefulInputBuf_GetUint16(&UIB) != u16) {
+ return "u16 out then in failed";
+ }
+ if(UsefulInputBuf_GetByte(&UIB) != u8) {
+ return "u8 out then in failed";
+ }
+ if(UsefulInputBuf_GetFloat(&UIB) != f) {
+ return "float out then in failed";
+ }
+ if(UsefulInputBuf_GetDouble(&UIB) != d) {
+ return "double out then in failed";
+ }
+
+ // Reset and go again for a few more tests
+ UsefulInputBuf_Init(&UIB, O);
+
+ const UsefulBufC Four = UsefulInputBuf_GetUsefulBuf(&UIB, 4);
+ if(UsefulBuf_IsNULLC(Four)) {
+ return "Four is NULL";
+ }
+ if(UsefulBuf_Compare(Four, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedNetworkOrder))) {
+ return "Four compare failed";
+ }
+
+ if(UsefulInputBuf_BytesUnconsumed(&UIB) != 23){
+ return "Wrong number of unconsumed bytes";
+ }
+
+ if(!UsefulInputBuf_BytesAvailable(&UIB, 23)){
+ return "Wrong number of bytes available I";
+ }
+
+ if(UsefulInputBuf_BytesAvailable(&UIB, 24)){
+ return "Wrong number of bytes available II";
+ }
+
+ UsefulInputBuf_Seek(&UIB, 0);
+
+ if(UsefulInputBuf_GetError(&UIB)) {
+ return "unexpected error after seek";
+ }
+
+ const uint8_t *pGetBytes = (const uint8_t *)UsefulInputBuf_GetBytes(&UIB, 4);
+ if(pGetBytes == NULL) {
+ return "GetBytes returns NULL";
+ }
+
+ if(memcmp(pGetBytes, pExpectedNetworkOrder, 4)) {
+ return "Got wrong bytes";
+ }
+
+ UsefulInputBuf_Seek(&UIB, 28);
+
+ if(!UsefulInputBuf_GetError(&UIB)) {
+ return "expected error after seek";
+ }
+
+ return NULL;
+}
+
+
+const char *UBUTest_CopyUtil()
+{
+ if(UsefulBufUtil_CopyFloatToUint32(65536.0F) != 0x47800000) {
+ return "CopyFloatToUint32 failed";
+ }
+
+ if(UsefulBufUtil_CopyDoubleToUint64(4e-40F) != 0X37C16C2800000000ULL) {
+ return "CopyDoubleToUint64 failed";
+ }
+
+ if(UsefulBufUtil_CopyUint64ToDouble(0X37C16C2800000000ULL) != 4e-40F) {
+ return "CopyUint64ToDouble failed";
+ }
+
+ if(UsefulBufUtil_CopyUint32ToFloat(0x47800000) != 65536.0F) {
+ return "CopyUint32ToFloat failed";
+ }
+
+ return NULL;
+}
+
+
+
diff --git a/lib/ext/qcbor/test/UsefulBuf_Tests.h b/lib/ext/qcbor/test/UsefulBuf_Tests.h
new file mode 100644
index 0000000..976de62
--- /dev/null
+++ b/lib/ext/qcbor/test/UsefulBuf_Tests.h
@@ -0,0 +1,50 @@
+/*==============================================================================
+ Copyright (c) 2016-2018, The Linux Foundation.
+ Copyright (c) 2018, Laurence Lundblade.
+ All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of The Linux Foundation nor the names of its
+ contributors, nor the name "Laurence Lundblade" may be used to
+ endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ ==============================================================================*/
+
+#ifndef UsefulBuf_UsefulBuf_Tests_h
+#define UsefulBuf_UsefulBuf_Tests_h
+
+const char * UOBTest_NonAdversarial(void);
+
+const char * TestBasicSanity(void);
+
+const char * UOBTest_BoundaryConditionsTest(void);
+
+const char * UBMacroConversionsTest(void);
+
+const char * UBUtilTests(void);
+
+const char * UIBTest_IntegerFormat(void);
+
+const char * UBUTest_CopyUtil(void);
+
+#endif
diff --git a/lib/ext/qcbor/test/float_tests.c b/lib/ext/qcbor/test/float_tests.c
new file mode 100644
index 0000000..eaf75aa
--- /dev/null
+++ b/lib/ext/qcbor/test/float_tests.c
@@ -0,0 +1,474 @@
+/*==============================================================================
+ float_tests.c -- tests for float and conversion to/from half-precision
+
+ Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+
+ SPDX-License-Identifier: BSD-3-Clause
+
+ See BSD-3-Clause license in README.md
+
+ Created on 9/19/18
+ ==============================================================================*/
+
+#include "float_tests.h"
+#include "qcbor.h"
+#include "half_to_double_from_rfc7049.h"
+#include <math.h> // For INFINITY and NAN and isnan()
+
+
+
+static const uint8_t spExpectedHalf[] = {
+ 0xB1,
+ 0x64,
+ 0x7A, 0x65, 0x72, 0x6F,
+ 0xF9, 0x00, 0x00, // 0.000
+ 0x6A,
+ 0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79,
+ 0xF9, 0x7C, 0x00, // Infinity
+ 0x73,
+ 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79,
+ 0xF9, 0xFC, 0x00, // -Inifinity
+ 0x63,
+ 0x4E, 0x61, 0x4E,
+ 0xF9, 0x7E, 0x00, // NaN
+ 0x63,
+ 0x6F, 0x6E, 0x65,
+ 0xF9, 0x3C, 0x00, // 1.0
+ 0x69,
+ 0x6F, 0x6E, 0x65, 0x20, 0x74, 0x68, 0x69, 0x72, 0x64,
+ 0xF9, 0x35, 0x55, // 0.333251953125
+ 0x76,
+ 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E,
+ 0xF9, 0x7B, 0xFF, // 65504.0
+ 0x78, 0x18, 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x20, 0x68, 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E,
+ 0xF9, 0x7C, 0x00, // Infinity
+ 0x72,
+ 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
+ 0xF9, 0x00, 0x01, // 0.000000059604
+ 0x6F,
+ 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
+ 0xF9, 0x03, 0xFF, // 0.0000609755516
+ 0x71,
+ 0x62, 0x69, 0x67, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
+ 0xF9, 0x04, 0x00, // 0.000061988
+ 0x70,
+ 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65,
+ 0xF9, 0x00, 0x00,
+ 0x03,
+ 0xF9, 0xC0, 0x00, // -2
+ 0x04,
+ 0xF9, 0x7E, 0x00, // qNaN
+ 0x05,
+ 0xF9, 0x7C, 0x01, // sNaN
+ 0x06,
+ 0xF9, 0x7E, 0x0F, // qNaN with payload 0x0f
+ 0x07,
+ 0xF9, 0x7C, 0x0F, // sNaN with payload 0x0f
+
+};
+
+
+int HalfPrecisionDecodeBasicTests()
+{
+ UsefulBufC HalfPrecision = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedHalf);
+
+ QCBORDecodeContext DC;
+ QCBORDecode_Init(&DC, HalfPrecision, 0);
+
+ QCBORItem Item;
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_MAP) {
+ return -1;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0F) {
+ return -2;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) {
+ return -3;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -INFINITY) {
+ return -4;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item); // TODO, is this really converting right? It is carrying payload, but this confuses things.
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || !isnan(Item.val.dfnum)) {
+ return -5;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 1.0F) {
+ return -6;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.333251953125F) {
+ return -7;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 65504.0F) {
+ return -8;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) {
+ return -9;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item); // TODO: check this
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0000000596046448F) {
+ return -10;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item); // TODO: check this
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0000609755516F) {
+ return -11;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item); // TODO check this
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0000610351563F) {
+ return -12;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0) {
+ return -13;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -2.0F) {
+ return -14;
+ }
+
+ // TODO: double check these four tests
+ QCBORDecode_GetNext(&DC, &Item); // qNaN
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff8000000000000ULL) {
+ return -15;
+ }
+ QCBORDecode_GetNext(&DC, &Item); // sNaN
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff0000000000001ULL) {
+ return -16;
+ }
+ QCBORDecode_GetNext(&DC, &Item); // qNaN with payload 0x0f
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff800000000000fULL) {
+ return -17;
+ }
+ QCBORDecode_GetNext(&DC, &Item); // sNaN with payload 0x0f
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff000000000000fULL) {
+ return -18;
+ }
+
+ if(QCBORDecode_Finish(&DC)) {
+ return -19;
+ }
+
+ return 0;
+}
+
+
+
+
+int HalfPrecisionAgainstRFCCodeTest()
+{
+ for(uint32_t uHalfP = 0; uHalfP < 0xffff; uHalfP += 60) {
+ unsigned char x[2];
+ x[1] = uHalfP & 0xff;
+ x[0] = uHalfP >> 8;
+ double d = decode_half(x);
+
+ // Contruct the CBOR for the half-precision float by hand
+ UsefulBuf_MAKE_STACK_UB(__xx, 3);
+ UsefulOutBuf UOB;
+ UsefulOutBuf_Init(&UOB, __xx);
+
+ const uint8_t uHalfPrecInitialByte = HALF_PREC_FLOAT + (CBOR_MAJOR_TYPE_SIMPLE << 5); // 0xf9
+ UsefulOutBuf_AppendByte(&UOB, uHalfPrecInitialByte); // The initial byte for a half-precision float
+ UsefulOutBuf_AppendUint16(&UOB, (uint16_t)uHalfP);
+
+ // Now parse the hand-constructed CBOR. This will invoke the conversion to a float
+ QCBORDecodeContext DC;
+ QCBORDecode_Init(&DC, UsefulOutBuf_OutUBuf(&UOB), 0);
+
+ QCBORItem Item;
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE) {
+ return -1;
+ }
+
+ //printf("%04x QCBOR:%15.15f RFC: %15.15f (%8x)\n", uHalfP,Item.val.fnum, d , UsefulBufUtil_CopyFloatToUint32(d));
+
+ if(isnan(d)) {
+ // The RFC code uses the native instructions which may or may not
+ // handle sNaN, qNaN and NaN payloads correctly. This test just
+ // makes sure it is a NaN and doesn't worry about the type of NaN
+ if(!isnan(Item.val.dfnum)) {
+ return -3;
+ }
+ } else {
+ if(Item.val.dfnum != d) {
+ return -2;
+ }
+ }
+ }
+ return 0;
+}
+
+
+/*
+ {"zero": 0.0,
+ "negative zero": -0.0,
+ "infinitity": Infinity,
+ "negative infinitity": -Infinity,
+ "NaN": NaN,
+ "one": 1.0,
+ "one third": 0.333251953125,
+ "largest half-precision": 65504.0,
+ "largest half-precision point one": 65504.1,
+ "too-large half-precision": 65536.0,
+ "smallest subnormal": 5.96046448e-8,
+ "smallest normal": 0.00006103515261202119,
+ "biggest subnormal": 0.00006103515625,
+ "subnormal single": 4.00000646641519e-40,
+ 3: -2.0,
+ "large single exp": 2.5521177519070385e+38,
+ "too-large single exp": 5.104235503814077e+38,
+ "biggest single with prec": 16777216.0,
+ "first single with prec loss": 16777217.0,
+ 1: "fin"}
+ */
+static const uint8_t spExpectedSmallest[] = {
+ 0xB4, 0x64, 0x7A, 0x65, 0x72, 0x6F, 0xF9, 0x00, 0x00, 0x6D,
+ 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x7A,
+ 0x65, 0x72, 0x6F, 0xF9, 0x80, 0x00, 0x6A, 0x69, 0x6E, 0x66,
+ 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79, 0xF9, 0x7C, 0x00,
+ 0x73, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20,
+ 0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79,
+ 0xF9, 0xFC, 0x00, 0x63, 0x4E, 0x61, 0x4E, 0xF9, 0x7E, 0x00,
+ 0x63, 0x6F, 0x6E, 0x65, 0xF9, 0x3C, 0x00, 0x69, 0x6F, 0x6E,
+ 0x65, 0x20, 0x74, 0x68, 0x69, 0x72, 0x64, 0xF9, 0x35, 0x55,
+ 0x76, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68,
+ 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73,
+ 0x69, 0x6F, 0x6E, 0xF9, 0x7B, 0xFF, 0x78, 0x20, 0x6C, 0x61,
+ 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, 0x61, 0x6C, 0x66,
+ 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E,
+ 0x20, 0x70, 0x6F, 0x69, 0x6E, 0x74, 0x20, 0x6F, 0x6E, 0x65,
+ 0xFB, 0x40, 0xEF, 0xFC, 0x03, 0x33, 0x33, 0x33, 0x33, 0x78,
+ 0x18, 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, 0x67, 0x65,
+ 0x20, 0x68, 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63,
+ 0x69, 0x73, 0x69, 0x6F, 0x6E, 0xFA, 0x47, 0x80, 0x00, 0x00,
+ 0x72, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20,
+ 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0xFB,
+ 0x3E, 0x70, 0x00, 0x00, 0x00, 0x1C, 0x5F, 0x68, 0x6F, 0x73,
+ 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, 0x6F,
+ 0x72, 0x6D, 0x61, 0x6C, 0xFA, 0x38, 0x7F, 0xFF, 0xFF, 0x71,
+ 0x62, 0x69, 0x67, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75,
+ 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0xF9, 0x04, 0x00,
+ 0x70, 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
+ 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0xFB, 0x37, 0xC1,
+ 0x6C, 0x28, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF9, 0xC0, 0x00,
+ 0x70, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x20, 0x73, 0x69, 0x6E,
+ 0x67, 0x6C, 0x65, 0x20, 0x65, 0x78, 0x70, 0xFA, 0x7F, 0x40,
+ 0x00, 0x00, 0x74, 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72,
+ 0x67, 0x65, 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20,
+ 0x65, 0x78, 0x70, 0xFB, 0x47, 0xF8, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x78, 0x18, 0x62, 0x69, 0x67, 0x67, 0x65, 0x73,
+ 0x74, 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x77,
+ 0x69, 0x74, 0x68, 0x20, 0x70, 0x72, 0x65, 0x63, 0xFA, 0x4B,
+ 0x80, 0x00, 0x00, 0x78, 0x1B, 0x66, 0x69, 0x72, 0x73, 0x74,
+ 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x77, 0x69,
+ 0x74, 0x68, 0x20, 0x70, 0x72, 0x65, 0x63, 0x20, 0x6C, 0x6F,
+ 0x73, 0x73, 0xFB, 0x41, 0x70, 0x00, 0x00, 0x10, 0x00, 0x00,
+ 0x00, 0x01, 0x63, 0x66, 0x69, 0x6E
+};
+
+
+int DoubleAsSmallestTest()
+{
+ UsefulBuf_MAKE_STACK_UB(EncodedHalfsMem, 420);
+
+#define QCBOREncode_AddDoubleAsSmallestToMap QCBOREncode_AddDoubleToMap
+#define QCBOREncode_AddDoubleAsSmallestToMapN QCBOREncode_AddDoubleToMapN
+
+
+ QCBOREncodeContext EC;
+ QCBOREncode_Init(&EC, EncodedHalfsMem);
+ // These are mostly from https://en.wikipedia.org/wiki/Half-precision_floating-point_format
+ QCBOREncode_OpenMap(&EC);
+ // 64 # text(4)
+ // 7A65726F # "zero"
+ // F9 0000 # primitive(0)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "zero", 0.00);
+
+ // 64 # text(4)
+ // 7A65726F # "negative zero"
+ // F9 8000 # primitive(0)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "negative zero", -0.00);
+
+ // 6A # text(10)
+ // 696E66696E6974697479 # "infinitity"
+ // F9 7C00 # primitive(31744)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "infinitity", INFINITY);
+
+ // 73 # text(19)
+ // 6E6567617469766520696E66696E6974697479 # "negative infinitity"
+ // F9 FC00 # primitive(64512)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "negative infinitity", -INFINITY);
+
+ // 63 # text(3)
+ // 4E614E # "NaN"
+ // F9 7E00 # primitive(32256)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "NaN", NAN);
+
+ // TODO: test a few NaN variants
+
+ // 63 # text(3)
+ // 6F6E65 # "one"
+ // F9 3C00 # primitive(15360)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "one", 1.0);
+
+ // 69 # text(9)
+ // 6F6E65207468697264 # "one third"
+ // F9 3555 # primitive(13653)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "one third", 0.333251953125);
+
+ // 76 # text(22)
+ // 6C6172676573742068616C662D707265636973696F6E # "largest half-precision"
+ // F9 7BFF # primitive(31743)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "largest half-precision",65504.0);
+
+ // 76 # text(22)
+ // 6C6172676573742068616C662D707265636973696F6E # "largest half-precision"
+ // F9 7BFF # primitive(31743)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "largest half-precision point one",65504.1);
+
+ // Float 65536.0F is 0x47800000 in hex. It has an exponent of 16, which is larger than 15, the largest half-precision exponent
+ // 78 18 # text(24)
+ // 746F6F2D6C617267652068616C662D707265636973696F6E # "too-large half-precision"
+ // FA 47800000 # primitive(31743)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "too-large half-precision", 65536.0);
+
+ // The smallest possible half-precision subnormal, but digitis are lost converting
+ // to half, so this turns into a double
+ // 72 # text(18)
+ // 736D616C6C657374207375626E6F726D616C # "smallest subnormal"
+ // FB 3E700000001C5F68 # primitive(4499096027744984936)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "smallest subnormal", 0.0000000596046448);
+
+ // The smallest possible half-precision snormal, but digitis are lost converting
+ // to half, so this turns into a single TODO: confirm this is right
+ // 6F # text(15)
+ // 736D616C6C657374206E6F726D616C # "smallest normal"
+ // FA 387FFFFF # primitive(947912703)
+ // in hex single is 0x387fffff, exponent -15, significand 7fffff
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "smallest normal", 0.0000610351526F);
+
+ // 71 # text(17)
+ // 62696767657374207375626E6F726D616C # "biggest subnormal"
+ // F9 0400 # primitive(1024)
+ // in hex single is 0x38800000, exponent -14, significand 0
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "biggest subnormal", 0.0000610351563F);
+
+ // 70 # text(16)
+ // 7375626E6F726D616C2073696E676C65 # "subnormal single"
+ // FB 37C16C2800000000 # primitive(4017611261645684736)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "subnormal single", 4e-40F);
+
+ // 03 # unsigned(3)
+ // F9 C000 # primitive(49152)
+ QCBOREncode_AddDoubleAsSmallestToMapN(&EC, 3, -2.0);
+
+ // 70 # text(16)
+ // 6C617267652073696E676C6520657870 # "large single exp"
+ // FA 7F400000 # primitive(2134900736)
+ // (0x01LL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) | ((127LL + DOUBLE_EXPONENT_BIAS) << DOUBLE_EXPONENT_SHIFT);
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "large single exp", 2.5521177519070385E+38); // Exponent fits single
+
+ // 74 # text(20)
+ // 746F6F2D6C617267652073696E676C6520657870 # "too-large single exp"
+ // FB 47F8000000000000 # primitive(5185894970917126144)
+ // (0x01LL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) | ((128LL + DOUBLE_EXPONENT_BIAS) << DOUBLE_EXPONENT_SHIFT);
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "too-large single exp", 5.104235503814077E+38); // Exponent too large for single
+
+ // 66 # text(6)
+ // 646664666465 # "dfdfde"
+ // FA 4B800000 # primitive(1266679808)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "biggest single with prec",16777216); // Single with no precision loss
+
+ // 78 18 # text(24)
+ // 626967676573742073696E676C6520776974682070726563 # "biggest single with prec"
+ // FA 4B800000 # primitive(1266679808)
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "first single with prec loss",16777217); // Double becuase of precision loss
+
+ // Just a convenient marker when cutting and pasting encoded CBOR
+ QCBOREncode_AddSZStringToMapN(&EC, 1, "fin");
+
+ QCBOREncode_CloseMap(&EC);
+
+ UsefulBufC EncodedHalfs;
+ int nReturn = QCBOREncode_Finish(&EC, &EncodedHalfs);
+ if(nReturn) {
+ return -1;
+ }
+
+ if(UsefulBuf_Compare(EncodedHalfs, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedSmallest))) {
+ return -3;
+ }
+
+ return 0;
+}
+
+
+
+#ifdef NAN_EXPERIMENT
+/*
+ Code for checking what the double to float cast does with
+ NaNs. Not run as part of tests. Keep it around to
+ be able to check various platforms and CPUs.
+ */
+
+#define DOUBLE_NUM_SIGNIFICAND_BITS (52)
+#define DOUBLE_NUM_EXPONENT_BITS (11)
+#define DOUBLE_NUM_SIGN_BITS (1)
+
+#define DOUBLE_SIGNIFICAND_SHIFT (0)
+#define DOUBLE_EXPONENT_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS)
+#define DOUBLE_SIGN_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS + DOUBLE_NUM_EXPONENT_BITS)
+
+#define DOUBLE_SIGNIFICAND_MASK (0xfffffffffffffULL) // The lower 52 bits
+#define DOUBLE_EXPONENT_MASK (0x7ffULL << DOUBLE_EXPONENT_SHIFT) // 11 bits of exponent
+#define DOUBLE_SIGN_MASK (0x01ULL << DOUBLE_SIGN_SHIFT) // 1 bit of sign
+#define DOUBLE_QUIET_NAN_BIT (0x01ULL << (DOUBLE_NUM_SIGNIFICAND_BITS-1))
+
+
+static int NaNExperiments() {
+ double dqNaN = UsefulBufUtil_CopyUint64ToDouble(DOUBLE_EXPONENT_MASK | DOUBLE_QUIET_NAN_BIT);
+ double dsNaN = UsefulBufUtil_CopyUint64ToDouble(DOUBLE_EXPONENT_MASK | 0x01);
+ double dqNaNPayload = UsefulBufUtil_CopyUint64ToDouble(DOUBLE_EXPONENT_MASK | DOUBLE_QUIET_NAN_BIT | 0xf00f);
+
+ float f1 = (float)dqNaN;
+ float f2 = (float)dsNaN;
+ float f3 = (float)dqNaNPayload;
+
+
+ uint32_t uqNaN = UsefulBufUtil_CopyFloatToUint32((float)dqNaN);
+ uint32_t usNaN = UsefulBufUtil_CopyFloatToUint32((float)dsNaN);
+ uint32_t uqNaNPayload = UsefulBufUtil_CopyFloatToUint32((float)dqNaNPayload);
+
+ // Result of this on x86 is that every NaN is a qNaN. The intel
+ // CVTSD2SS instruction ignores the NaN payload and even converts
+ // a sNaN to a qNaN.
+
+ return 0;
+}
+#endif
+
+
+
diff --git a/lib/ext/qcbor/test/float_tests.h b/lib/ext/qcbor/test/float_tests.h
new file mode 100644
index 0000000..b7174c8
--- /dev/null
+++ b/lib/ext/qcbor/test/float_tests.h
@@ -0,0 +1,23 @@
+/*==============================================================================
+ float_tests.h -- tests for float and conversion to/from half-precision
+
+ Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+
+ SPDX-License-Identifier: BSD-3-Clause
+
+ See BSD-3-Clause license in README.md
+
+ Created on 9/19/18
+ ==============================================================================*/
+
+#ifndef float_tests_h
+#define float_tests_h
+
+int HalfPrecisionDecodeBasicTests(void);
+
+int DoubleAsSmallestTest(void);
+
+int HalfPrecisionAgainstRFCCodeTest(void);
+
+
+#endif /* float_tests_h */
diff --git a/lib/ext/qcbor/test/half_to_double_from_rfc7049.c b/lib/ext/qcbor/test/half_to_double_from_rfc7049.c
new file mode 100644
index 0000000..6380e51
--- /dev/null
+++ b/lib/ext/qcbor/test/half_to_double_from_rfc7049.c
@@ -0,0 +1,45 @@
+/*
+
+ Copyright (c) 2013 IETF Trust and the persons identified as the
+ document authors. All rights reserved.
+
+ This document is subject to BCP 78 and the IETF Trust's Legal
+ Provisions Relating to IETF Documents
+ (http://trustee.ietf.org/license-info) in effect on the date of
+ publication of this document. Please review these documents
+ carefully, as they describe your rights and restrictions with respect
+ to this document. Code Components extracted from this document must
+ include Simplified BSD License text as described in Section 4.e of
+ the Trust Legal Provisions and are provided without warranty as
+ described in the Simplified BSD License.
+
+ */
+
+/*
+ This code is from RFC 7049. It is not used in the main implementation
+ because:
+ a) it adds a dependency on <math.h> and ldexp().
+ b) the license may be an issue
+
+ QCBOR does support half-precision, but rather than using
+ floating point math like this, it does it with bit shifting
+ and masking.
+
+ This code is here to test that code.
+
+ */
+
+#include "half_to_double_from_rfc7049.h"
+
+#include <math.h>
+
+double decode_half(unsigned char *halfp) {
+ int half = (halfp[0] << 8) + halfp[1];
+ int exp = (half >> 10) & 0x1f;
+ int mant = half & 0x3ff;
+ double val;
+ if (exp == 0) val = ldexp(mant, -24);
+ else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
+ else val = mant == 0 ? INFINITY : NAN;
+ return half & 0x8000 ? -val : val;
+}
diff --git a/lib/ext/qcbor/test/half_to_double_from_rfc7049.h b/lib/ext/qcbor/test/half_to_double_from_rfc7049.h
new file mode 100644
index 0000000..9f69e35
--- /dev/null
+++ b/lib/ext/qcbor/test/half_to_double_from_rfc7049.h
@@ -0,0 +1,18 @@
+/*==============================================================================
+ half_to_double_from_rfc7049.h -- interface to IETF float conversion code.
+
+ Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+
+ SPDX-License-Identifier: BSD-3-Clause
+
+ See BSD-3-Clause license in README.md
+
+ Created on 9/23/18
+ ==============================================================================*/
+
+#ifndef half_to_double_from_rfc7049_h
+#define half_to_double_from_rfc7049_h
+
+double decode_half(unsigned char *halfp);
+
+#endif /* half_to_double_from_rfc7049_h */
diff --git a/lib/ext/qcbor/test/qcbor_decode_tests.c b/lib/ext/qcbor/test/qcbor_decode_tests.c
new file mode 100644
index 0000000..38005c0
--- /dev/null
+++ b/lib/ext/qcbor/test/qcbor_decode_tests.c
@@ -0,0 +1,2836 @@
+/*==============================================================================
+ Copyright (c) 2016-2018, The Linux Foundation.
+ Copyright (c) 2018-2019, Laurence Lundblade.
+ All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of The Linux Foundation nor the names of its
+ contributors, nor the name "Laurence Lundblade" may be used to
+ endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ ==============================================================================*/
+
+#include "qcbor_decode_tests.h"
+#include "qcbor.h"
+#include <string.h>
+#include <math.h> // for fabs()
+
+
+#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
+#include <stdio.h>
+
+static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
+{
+ if(szLabel) {
+ printf("%s ", szLabel);
+ }
+
+ size_t i;
+ for(i = 0; i < Buf.len; i++) {
+ uint8_t Z = ((uint8_t *)Buf.ptr)[i];
+ printf("%02x ", Z);
+ }
+ printf("\n");
+
+ fflush(stdout);
+}
+
+/*static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
+{
+ PrintUsefulBufC(szLabel, (UsefulBufC){pEncoded, nLen});
+}*/
+#endif
+
+
+static const uint8_t spExpectedEncodedInts[] = {
+ 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
+ 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
+ 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
+ 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
+ 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
+ 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
+ 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
+ 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
+ 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
+ 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
+ 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
+ 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
+ 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
+ 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
+ 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
+ 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
+ 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
+ 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
+ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
+ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
+ 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff};
+
+
+// return CBOR error or -1 if type of value doesn't match
+
+static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
+{
+ QCBORItem Item;
+ int nCBORError;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_ARRAY)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
+ Item.val.int64 != -9223372036854775807LL - 1)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -4294967297)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -4294967296)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -4294967295)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -4294967294)
+ return -1;
+
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -2147483648)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -2147483647)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -65538)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -65537)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -65536)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -65535)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -65534)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -257)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -256)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -255)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -254)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -25)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -24)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -23)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -1)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 0)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 0)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 1)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 22)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 23)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 24)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 25)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 26)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 254)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 255)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 256)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 257)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 65534)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 65535)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 65536)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 65537)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 65538)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 2147483647)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 2147483647)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 2147483648)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 2147483649)
+ return -1;
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 4294967294)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 4294967295)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 4294967296)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 4294967297)
+ return -1;
+
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 9223372036854775807LL)
+ return -1;
+
+
+ if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_UINT64 ||
+ Item.val.uint64 != 18446744073709551615ULL)
+ return -1;
+
+
+ if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
+ return -1;
+ }
+
+ return 0;
+}
+
+
+/*
+ Tests the decoding of lots of different integers sizes
+ and values.
+ */
+
+int IntegerValuesParseTest()
+{
+ int n;
+ QCBORDecodeContext DCtx;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
+
+ n = IntegerValuesParseTestInternal(&DCtx);
+
+ return(n);
+}
+
+
+/*
+ Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
+ and needs to be freed. This is used by several tests.
+
+ Two of the inputs can be set. Two other items in the array are fixed.
+
+ */
+
+static uint8_t spSimpleArrayBuffer[50];
+
+static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
+{
+ QCBOREncodeContext ECtx;
+ int nReturn = -1;
+
+ *pEncoded = NULL;
+ *pEncodedLen = INT32_MAX;
+
+ // loop runs CBOR encoding twice. First with no buffer to
+ // calculate the length so buffer can be allocated correctly,
+ // and last with the buffer to do the actual encoding
+ do {
+ QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
+ QCBOREncode_OpenArray(&ECtx);
+ QCBOREncode_AddInt64(&ECtx, nInt1);
+ QCBOREncode_AddInt64(&ECtx, nInt2);
+ QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
+ QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
+ QCBOREncode_CloseArray(&ECtx);
+
+ if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
+ goto Done;
+
+ if(*pEncoded != NULL) {
+ nReturn = 0;
+ goto Done;
+ }
+
+ // Use static buffer to avoid dependency on malloc()
+ if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
+ goto Done;
+ }
+ *pEncoded = spSimpleArrayBuffer;
+
+ } while(1);
+
+Done:
+ return nReturn;
+}
+
+
+/*
+ {"first integer": 42,
+ "an array of two strings": [
+ "string1", "string2"
+ ],
+ "map in a map": {
+ "bytes 1": h'78787878',
+ "bytes 2": h'79797979',
+ "another int": 98,
+ "text 2": "lies, damn lies and statistics"
+ }
+ }
+ */
+
+static uint8_t pValidMapEncoded[] = {
+ 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
+ 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
+ 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
+ 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
+ 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
+ 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
+ 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
+ 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
+ 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
+ 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
+ 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
+ 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
+ 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
+ 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
+ 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
+ 0x73 } ;
+
+static int ParseOrderedArray(const uint8_t *pEncoded, size_t nLen, int64_t *pInt1, int64_t *pInt2, const uint8_t **pBuf3, size_t *pBuf3Len, const uint8_t **pBuf4, size_t *pBuf4Len)
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nReturn = -1; // assume error until success
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
+
+ // Make sure the first thing is a map
+ if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
+ goto Done;
+
+ // First integer
+ if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
+ goto Done;
+ *pInt1 = Item.val.int64;
+
+ // Second integer
+ if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
+ goto Done;
+ *pInt2 = Item.val.int64;
+
+ // First string
+ if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
+ goto Done;
+ *pBuf3 = Item.val.string.ptr;
+ *pBuf3Len = Item.val.string.len;
+
+ // Second string
+ if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
+ goto Done;
+ *pBuf4 = Item.val.string.ptr;
+ *pBuf4Len = Item.val.string.len;
+
+ nReturn = 0;
+
+Done:
+ return(nReturn);
+}
+
+
+
+
+int SimpleArrayTest()
+{
+ uint8_t *pEncoded;
+ size_t nEncodedLen;
+
+ int64_t i1=0, i2=0;
+ size_t i3=0, i4=0;
+ const uint8_t *s3= (uint8_t *)"";
+ const uint8_t *s4= (uint8_t *)"";
+
+
+ if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
+ return(-1);
+ }
+
+ ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
+
+ if(i1 != 23 ||
+ i2 != 6000 ||
+ i3 != 8 ||
+ i4 != 11 ||
+ memcmp("galactic", s3, 8) !=0 ||
+ memcmp("haven token", s4, 11) !=0) {
+ return(-1);
+ }
+
+ return(0);
+}
+
+
+
+static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
+
+int ParseDeepArrayTest()
+{
+ QCBORDecodeContext DCtx;
+ int nReturn = 0;
+ int i;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
+
+ for(i = 0; i < 10; i++) {
+ QCBORItem Item;
+
+ if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
+ Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.uNestingLevel != i) {
+ nReturn = -1;
+ break;
+ }
+ }
+
+ return(nReturn);
+}
+
+// Big enough to test nesting to the depth of 24
+static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
+ 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
+ 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
+ 0x80};
+
+int ParseTooDeepArrayTest()
+{
+ QCBORDecodeContext DCtx;
+ int nReturn = 0;
+ int i;
+ QCBORItem Item;
+
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
+
+ for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
+
+ if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
+ Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.uNestingLevel != i) {
+ nReturn = -1;
+ break;
+ }
+ }
+
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
+ nReturn = -1;
+
+ return(nReturn);
+}
+
+
+
+
+int ShortBufferParseTest()
+{
+ int nResult = 0;
+ QCBORDecodeContext DCtx;
+ int num;
+
+ for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
+ int n;
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
+
+ n = IntegerValuesParseTestInternal(&DCtx);
+
+ //printf("Len %d, result: %d\n", num, n);
+
+ if(n != QCBOR_ERR_HIT_END) {
+ nResult = -1;
+ goto Done;
+ }
+ }
+Done:
+ return nResult;
+}
+
+
+
+int ShortBufferParseTest2()
+{
+ uint8_t *pEncoded;
+ int nReturn;
+ size_t nEncodedLen;
+
+ int64_t i1, i2;
+ size_t i3, i4;
+ const uint8_t *s3, *s4;
+
+ nReturn = 0;
+
+ if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
+ return(-1);
+ }
+
+ for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
+ int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
+ if(nResult == 0) {
+ nReturn = -1;
+ }
+ }
+
+ return(nReturn);
+}
+
+/*
+ Decode and thoroughly check a moderately complex
+ set of maps
+ */
+static int ParseMapTest1(QCBORDecodeMode nMode)
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ Item.val.uCount != 3)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 42 ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
+ Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.val.uCount != 2)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
+ Item.uDataType != QCBOR_TYPE_MAP ||
+ Item.val.uCount != 4) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 98)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
+ return -1;
+ }
+
+ return 0;
+}
+
+
+/*
+ Decode and thoroughly check a moderately complex
+ set of maps
+ */
+int ParseMapAsArrayTest()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
+ Item.val.uCount != 6) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ Item.uLabelType != QCBOR_TYPE_NONE ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
+ return -2;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 42 ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc) {
+ return -3;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
+ return -4;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.val.uCount != 2) {
+ return -5;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.val.string.len != 7 ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
+ return -6;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
+ return -7;
+ }
+
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
+ return -8;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
+ Item.val.uCount != 8) {
+ return -9;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc) {
+ return -10;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
+ return -11;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc) {
+ return -12;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
+ return -13;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
+ return -14;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 98) {
+ return -15;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc) {
+ return -16;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_NONE ||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataAlloc ||
+ Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
+ return -17;
+ }
+
+ return 0;
+}
+
+
+/*
+ Fully or partially decode pValidMapEncoded. When
+ partially decoding check for the right error code.
+ How much partial decoding depends on nLevel.
+
+ The partial decodes test error conditions of
+ incomplete encoded input.
+
+ This could be combined with the above test
+ and made prettier and maybe a little more
+ thorough.
+ */
+static int ExtraBytesTest(int nLevel)
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
+
+ if(nLevel < 1) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ Item.val.uCount != 3)
+ return -1;
+
+ if(nLevel < 2) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.uCount != 42 ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
+ return -1;
+ }
+
+ if(nLevel < 3) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
+ Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.val.uCount != 2) {
+ return -1;
+ }
+
+
+ if(nLevel < 4) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
+ return -1;
+ }
+
+ if(nLevel < 5) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
+ return -1;
+ }
+
+ if(nLevel < 6) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
+ Item.uDataType != QCBOR_TYPE_MAP ||
+ Item.val.uCount != 4)
+ return -1;
+
+ if(nLevel < 7) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
+ return -1;
+ }
+
+ if(nLevel < 8) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
+ return -1;
+ }
+
+ if(nLevel < 9) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 98)
+ return -1;
+
+ if(nLevel < 10) {
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
+ return nCBORError;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
+ return -1;
+ }
+
+ if(QCBORDecode_Finish(&DCtx)) {
+ return -1;
+ }
+
+ return 0;
+}
+
+
+
+
+int ParseMapTest()
+{
+ // Parse a moderatly complex map structure very thoroughl
+ int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
+
+ n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
+
+ if(!n) {
+ for(int i = 0; i < 10; i++) {
+ n = ExtraBytesTest(i);
+ if(n) {
+ break;
+ }
+ }
+ }
+
+ return(n);
+}
+
+
+static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
+
+int ParseSimpleTest()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
+
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.val.uCount != 10)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_FALSE)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_TRUE)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_NULL)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_UNDEF)
+ return -1;
+
+ // A break
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
+ return -1;
+
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
+ return -1;
+
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
+ return -1;
+
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
+ return -1;
+
+ return 0;
+
+}
+
+
+struct FailInput {
+ UsefulBufC Input;
+ int nError;
+};
+
+
+struct FailInput Failures[] = {
+ { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
+ { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
+ { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
+ { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
+ { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer
+ { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
+ { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
+ { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
+ { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer
+ { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
+ { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
+ { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string
+ { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
+ { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
+ { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string
+ { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break
+ { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
+ { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
+ { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
+ { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
+ { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
+ { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string
+
+};
+
+
+int FailureTests()
+{
+ int nResult = 0;
+
+ struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput);
+
+ for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
+
+ while(1) {
+ nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
+ if(QCBOR_ERR_HIT_END == nCBORError) {
+ break;
+ }
+ if(nCBORError != pF->nError) {
+ nResult = 1;
+ break;
+ }
+ }
+ }
+
+ {
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.val.uCount != 10)
+ return -1;
+
+ DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf
+
+ nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
+ if(nCBORError != QCBOR_ERR_HIT_END)
+ return -1;
+ }
+
+
+ return nResult;
+}
+
+
+/* Try all 256 values of the byte at nLen including recursing for
+ each of the values to try values at nLen+1 ... up to nLenMax
+ */
+static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax)
+{
+ if(nLen >= nLenMax) {
+ return;
+ }
+
+ for(int inputByte = 0; inputByte < 256; inputByte++) {
+ // Set up the input
+ pBuf[nLen] = inputByte;
+ const UsefulBufC Input = {pBuf, nLen+1};
+
+ // Get ready to parse
+ QCBORDecodeContext DCtx;
+ QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
+
+ // Parse by getting the next item until an error occurs
+ // Just about every possible decoder error can occur here
+ // The goal of this test is not to check for the correct
+ // error since that is not really possible. It is to
+ // see that there is no crash on hostile input.
+ while(1) {
+ QCBORItem Item;
+ QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
+ if(nCBORError != QCBOR_SUCCESS) {
+ break;
+ }
+ }
+
+ ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
+ }
+}
+
+
+/*
+ Public function for initialization. See header qcbor.h
+ */
+int ComprehensiveInputTest()
+{
+ // Size 2 tests 64K inputs and runs quickly
+ uint8_t pBuf[2];
+
+ ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
+
+ return 0;
+}
+
+
+/*
+ Public function for initialization. See header qcbor.h
+ */
+int BigComprehensiveInputTest()
+{
+ // size 3 tests 16 million inputs and runs OK
+ // in seconds on fast machines. Size 4 takes
+ // 10+ minutes and 5 half a day on fast
+ // machines. This test is kept separate from
+ // the others so as to no slow down the use
+ // of them as a very frequent regression.
+ uint8_t pBuf[3]; //
+
+ ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
+
+ return 0;
+}
+
+
+static uint8_t spDateTestInput[] = {
+ 0xc0, // tag for string date
+ 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
+
+ 0xc1, // tag for epoch date
+ 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
+
+ // CBOR_TAG_B64
+ 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
+ 0x1a, 0x53, 0x72, 0x4E, 0x01,
+
+ 0xc1, // tag for epoch date
+ 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
+
+ 0xc1, // tag for epoch date
+ 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
+
+ 0xc1, // tag for epoch date
+ 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
+
+};
+
+
+// have to check float expected only to within an epsilon
+int CHECK_EXPECTED_DOUBLE(double val, double expected) {
+
+ double diff = val - expected;
+
+ diff = fabs(diff);
+
+ return diff > 0.0000001;
+}
+
+
+int DateParseTest()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
+
+ const uint64_t uTags[] = {15};
+ QCBORTagListIn TagList = {1, uTags};
+
+ QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
+
+ // String date
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
+ UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
+ return -2;
+ }
+
+ // Epoch date
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -3;
+ if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
+ Item.val.epochDate.nSeconds != 1400000000 ||
+ Item.val.epochDate.fSecondsFraction != 0 ) {
+ return -4;
+ }
+
+ // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
+ // but want to be sure extra tag doesn't cause a problem
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -5;
+ if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
+ Item.val.epochDate.nSeconds != 1400000001 ||
+ Item.val.epochDate.fSecondsFraction != 0 ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
+ return -6;
+ }
+
+ // Epoch date that is too large for our representation
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
+ return -7;
+ }
+
+ // Epoch date in float format with fractional seconds
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -8;
+ if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
+ Item.val.epochDate.nSeconds != 1 ||
+ CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
+ return -9;
+ }
+
+ // Epoch date float that is too large for our representation
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
+ return -10;
+ }
+
+ // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
+
+ return 0;
+}
+
+// Really simple basic input for tagging test
+static uint8_t spOptTestInput[] = {
+ 0xd9, 0xd9, 0xf7, // CBOR magic number
+ 0x81, // Array of one
+ 0xd8, 0x04, // non-preferred serialization of tag 4
+ 0x82, 0x01, 0x03}; // fraction 1/3
+
+static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
+
+// 0x9192939495969798, 0x88, 0x01, 0x04
+static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
+
+/*
+ The cbor.me parse of this.
+ 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
+ 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
+ 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
+ 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
+ 16(-22): 23({11(8(7(-5))): 8(-3)})})))
+ */
+static uint8_t spCSRWithTags[] = {
+ 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
+ 0xc6, 0xc7, 0x36,
+ 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
+ 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
+ 0xcb, 0xa5,
+ 0xd1, 0x31,
+ 0xd1, 0xd1, 0xd1, 0x6c,
+ 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
+ 0xc9, 0x30,
+ 0xd9, 0x03, 0x05, 0x63,
+ 0x53, 0x53, 0x47,
+ 0x2e,
+ 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
+ 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
+ 0xd1, 0x2f,
+ 0xd1, 0x69,
+ 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
+ 0xd1, 0x2d,
+ 0xd1, 0x62,
+ 0x55, 0x53,
+ 0xd7, 0x32,
+ 0xd3, 0xa2,
+ 0x2a,
+ 0xc9, 0xa1,
+ 0x28,
+ 0x26,
+ 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
+ 0xcc, 0x4a,
+ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
+ 0xd0, 0x35,
+ 0xd7, 0xa1,
+ 0xcb, 0xc8, 0xc7, 0x24,
+ 0xc8, 0x22};
+
+static int CheckCSRMaps(QCBORDecodeContext *pDC);
+
+
+int OptTagParseTest()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
+
+ //-------------------------
+ // This text matches the magic number tag and the fraction tag
+ if(QCBORDecode_GetNext(&DCtx, &Item)) {
+ return -2;
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
+ return -3;
+ }
+
+ if(QCBORDecode_GetNext(&DCtx, &Item)) {
+ return -4;
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
+ Item.val.uCount != 2) {
+ return -5;
+ }
+
+ // --------------------------------
+ // This test decodes the very large tag, but it is not in
+ // any list so it is ignored.
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
+ if(QCBORDecode_GetNext(&DCtx, &Item)) {
+ return -6;
+ }
+ if(Item.uTagBits) {
+ return -7;
+ }
+
+ // ----------------------------------
+ // This test sets up a caller-config list that includes the very large tage and then matches it.
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
+ const uint64_t puList[] = {0x9192939495969798, 257};
+ const QCBORTagListIn TL = {2, puList};
+ QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
+
+ if(QCBORDecode_GetNext(&DCtx, &Item)) {
+ return -8;
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
+ QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
+ QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
+ Item.val.uCount != 0) {
+ return -9;
+ }
+
+ //------------------------
+ // This test sets up a caller-configured list, and looks up something not in it
+ const uint64_t puLongList[17] = {1,2,1};
+ const QCBORTagListIn TLLong = {17, puLongList};
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
+ if(QCBORDecode_GetNext(&DCtx, &Item)) {
+ return -11;
+ }
+
+ // -----------------------
+ // This tests retrievel of the full tag list
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
+ uint64_t puTags[16];
+ QCBORTagListOut Out = {0, 4, puTags};
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -12;
+ }
+ if(puTags[0] != 0x9192939495969798 ||
+ puTags[1] != 0x88 ||
+ puTags[2] != 0x05 ||
+ puTags[3] != 0x04) {
+ return -13;
+ }
+
+ // ----------------------
+ // This text if too small of an out list
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
+ QCBORTagListOut OutSmall = {0, 3, puTags};
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
+ return -14;
+ }
+
+ // ---------------
+ // Parse a version of the "CSR" that has had a ton of tags randomly inserted
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
+ int n = CheckCSRMaps(&DCtx);
+ if(n) {
+ return n-2000;
+ }
+
+ Out = (QCBORTagListOut){0,16, puTags};
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
+
+ const uint64_t puTagList[] = {773, 1, 90599561};
+ const QCBORTagListIn TagList = {3, puTagList};
+ QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
+
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -100;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
+ QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
+ QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
+ Item.val.uCount != 2 ||
+ puTags[0] != CBOR_TAG_CBOR_MAGIC ||
+ puTags[1] != CBOR_TAG_CBOR_MAGIC ||
+ puTags[2] != CBOR_TAG_CBOR_MAGIC ||
+ Out.uNumUsed != 3) {
+ return -101;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -102;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
+ QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
+ QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
+ Item.val.uCount != 2 ||
+ puTags[0] != 5859837686836516696 ||
+ puTags[1] != 7 ||
+ Out.uNumUsed != 2) {
+ return -103;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -104;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ Item.uTagBits ||
+ Item.val.uCount != 5 ||
+ puTags[0] != 0x0b ||
+ Out.uNumUsed != 1) {
+ return -105;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -106;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
+ Item.val.string.len != 12 ||
+ puTags[0] != CBOR_TAG_COSE_MAC0 ||
+ puTags[1] != CBOR_TAG_COSE_MAC0 ||
+ puTags[2] != CBOR_TAG_COSE_MAC0 ||
+ Out.uNumUsed != 3) {
+ return -105;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -107;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
+ Item.val.string.len != 3 ||
+ puTags[0] != 773 ||
+ Out.uNumUsed != 1) {
+ return -108;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -109;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
+ Item.val.string.len != 9 ||
+ puTags[0] != 4 ||
+ puTags[11] != 0x0f ||
+ Out.uNumUsed != 12) {
+ return -110;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -111;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
+ Item.val.string.len != 9 ||
+ puTags[0] != 17 ||
+ Out.uNumUsed != 1) {
+ return -112;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -111;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
+ Item.val.string.len != 2 ||
+ puTags[0] != 17 ||
+ Out.uNumUsed != 1) {
+ return -112;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -113;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
+ Item.val.uCount != 2 ||
+ puTags[0] != 19 ||
+ Out.uNumUsed != 1) {
+ return -114;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -115;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
+ Item.uTagBits ||
+ Item.val.uCount != 1 ||
+ puTags[0] != 9 ||
+ Out.uNumUsed != 1) {
+ return -116;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -116;
+ }
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != -7 ||
+ Item.uTagBits ||
+ Out.uNumUsed != 0) {
+ return -117;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -118;
+ }
+ if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ Item.val.string.len != 10 ||
+ Item.uTagBits ||
+ puTags[0] != 12 ||
+ Out.uNumUsed != 1) {
+ return -119;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -120;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
+ Item.val.uCount != 1 ||
+ puTags[0] != 0x17 ||
+ Out.uNumUsed != 1) {
+ return -121;
+ }
+
+ if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
+ return -122;
+ }
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
+ Item.val.int64 != -3 ||
+ puTags[0] != 8 ||
+ Out.uNumUsed != 1) {
+ return -123;
+ }
+
+ if(QCBORDecode_Finish(&DCtx)) {
+ return -124;
+ }
+
+ return 0;
+}
+
+
+
+
+static uint8_t spBigNumInput[] = {
+ 0x83,
+ 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xA4,
+ 0x63, 0x42, 0x4E, 0x2B,
+ 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x18, 0x40,
+ 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x63, 0x42, 0x4E, 0x2D,
+ 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x38, 0x3F,
+ 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+
+static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+
+int BignumParseTest()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
+
+
+ //
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -1;
+ }
+
+ //
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
+ UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
+ return -1;
+ }
+
+ //
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
+ UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
+ return -1;
+ }
+
+ //
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_MAP) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
+ Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
+ Item.uLabelType != QCBOR_TYPE_INT64 ||
+ Item.label.int64 != 64 ||
+ UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
+ Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
+ Item.uLabelType != QCBOR_TYPE_INT64 ||
+ Item.label.int64 != -64 ||
+ UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
+ return -1;
+ }
+
+ return 0;
+}
+
+
+
+static int CheckItemWithIntLabel(QCBORDecodeContext *pCtx, uint8_t uDataType, uint8_t uNestingLevel, uint8_t uNextNest, int64_t nLabel, QCBORItem *pItem)
+{
+ QCBORItem Item;
+ int nCBORError;
+
+ if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
+ if(Item.uDataType != uDataType) return -1;
+ if(uNestingLevel > 0) {
+ if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
+ if(Item.uLabelType == QCBOR_TYPE_INT64) {
+ if(Item.label.int64 != nLabel) return -1;
+ } else {
+ if(Item.label.uint64 != (uint64_t)nLabel) return -1;
+ }
+ }
+ if(Item.uNestingLevel != uNestingLevel) return -1;
+ if(Item.uNextNestLevel != uNextNest) return -1;
+
+ if(pItem) {
+ *pItem = Item;
+ }
+ return 0;
+}
+
+
+// Same code checks definite and indefinite length versions of the map
+static int CheckCSRMaps(QCBORDecodeContext *pDC)
+{
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
+ if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
+
+ if(QCBORDecode_Finish(pDC)) return -2;
+
+ return 0;
+}
+
+
+/*
+// cbor.me decoded output
+{
+ -23: {
+ -20: {
+ -18: "Organization",
+ -17: "SSG",
+ -15: "Confusion",
+ -16: "San Diego",
+ -14: "US"
+ },
+ -19: {
+ -11: {
+ -9: -7
+ },
+ -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
+ }
+ },
+ -22: {
+ -5: -3
+ }
+}
+ */
+
+
+static uint8_t spCSRInput[] = {
+ 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
+ 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
+ 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
+ 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
+ 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
+ 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
+ 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
+ 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
+
+int NestedMapTest()
+{
+ QCBORDecodeContext DCtx;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
+
+ return CheckCSRMaps(&DCtx);
+}
+
+
+
+int StringDecoderModeFailTest()
+{
+ QCBORDecodeContext DCtx;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
+
+ QCBORItem Item;
+ QCBORError nCBORError;
+
+ if(QCBORDecode_GetNext(&DCtx, &Item)) {
+ return -1;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP) {
+ return -2;
+ }
+
+ nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
+ if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
+ return -3;
+ }
+
+ return 0;
+}
+
+
+// Same map as above, but using indefinite lengths
+static uint8_t spCSRInputIndefLen[] = {
+ 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
+ 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
+ 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
+ 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
+ 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
+ 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
+ 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
+ 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
+ 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
+ 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
+
+int NestedMapTestIndefLen()
+{
+ QCBORDecodeContext DCtx;
+
+ QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
+
+ return CheckCSRMaps(&DCtx);
+}
+
+
+
+static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
+{
+ UsefulOutBuf UOB;
+ UsefulOutBuf_Init(&UOB, Storage);
+
+ int i;
+ for(i = 0; i < n; i++) {
+ UsefulOutBuf_AppendByte(&UOB, 0x9f);
+ }
+
+ for(i = 0; i < n; i++) {
+ UsefulOutBuf_AppendByte(&UOB, 0xff);
+ }
+ return UsefulOutBuf_OutUBuf(&UOB);
+}
+
+
+static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
+{
+ QCBORDecodeContext DC;
+ QCBORDecode_Init(&DC, Nested, 0);
+
+ int j;
+ for(j = 0; j < nNestLevel; j++) {
+ QCBORItem Item;
+ int nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(j >= QCBOR_MAX_ARRAY_NESTING) {
+ // Should be in error
+ if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
+ return -4;
+ } else {
+ return 0; // Decoding doesn't recover after an error
+ }
+ } else {
+ // Should be no error
+ if(nReturn) {
+ return -9; // Should not have got an error
+ }
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -7;
+ }
+ }
+ int nReturn = QCBORDecode_Finish(&DC);
+ if(nReturn) {
+ return -3;
+ }
+ return 0;
+}
+
+
+int IndefiniteLengthNestTest()
+{
+ UsefulBuf_MAKE_STACK_UB(Storage, 50);
+ int i;
+ for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
+ const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
+ int nReturn = parse_indeflen_nested(Nested, i);
+ if(nReturn) {
+ return nReturn;
+ }
+ }
+ return 0;
+}
+
+
+
+static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
+static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
+static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
+static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
+static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
+static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
+
+int IndefiniteLengthArrayMapTest()
+{
+ int nResult;
+ // --- first test -----
+ UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
+
+ // Decode it and see if it is OK
+ UsefulBuf_MAKE_STACK_UB(MemPool, 150);
+ QCBORDecodeContext DC;
+ QCBORItem Item;
+ QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORDecode_SetMemPool(&DC, MemPool, false);
+
+ QCBORDecode_GetNext(&DC, &Item);
+
+ if(Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.uNestingLevel != 0 ||
+ Item.uNextNestLevel != 1) {
+ return -111;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.uNestingLevel != 1 ||
+ Item.uNextNestLevel != 1) {
+ return -2;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.uNestingLevel != 1 ||
+ Item.uNextNestLevel != 2) {
+ return -3;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.uNestingLevel != 2 ||
+ Item.uNextNestLevel != 2) {
+ return -4;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.uNestingLevel != 2 ||
+ Item.uNextNestLevel != 0) {
+ return -5;
+ }
+
+ if(QCBORDecode_Finish(&DC)) {
+ return -6;
+ }
+
+ // --- next test -----
+ IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
+
+ QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORDecode_SetMemPool(&DC, MemPool, false);
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -7;
+ }
+
+ nResult = QCBORDecode_Finish(&DC);
+ if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -8;
+ }
+
+
+ // --- next test -----
+ IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
+
+ QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORDecode_SetMemPool(&DC, MemPool, false);
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -9;
+ }
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -10;
+ }
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
+ return -11;
+ }
+
+ nResult = QCBORDecode_Finish(&DC);
+ if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -12;
+ }
+
+
+ // --- next test -----
+ IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
+
+ QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORDecode_SetMemPool(&DC, MemPool, false);
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -13;
+ }
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult != QCBOR_ERR_BAD_BREAK) {
+ return -14;
+ }
+
+
+ // --- next test -----
+ IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
+
+ QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORDecode_SetMemPool(&DC, MemPool, false);
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -15;
+ }
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -16;
+ }
+
+ nResult = QCBORDecode_Finish(&DC);
+ if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ return -17;
+ }
+
+ // --- next test -----
+ IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
+
+ QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORDecode_SetMemPool(&DC, MemPool, false);
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -18;
+ }
+
+ nResult = QCBORDecode_GetNext(&DC, &Item);
+ if(nResult != QCBOR_ERR_BAD_BREAK) {
+ return -19;
+ }
+
+ return 0;
+}
+
+
+static const uint8_t spIndefiniteLenString[] = {
+ 0x81, // Array of length one
+ 0x7f, // text string marked with indefinite length
+ 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
+ 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
+ 0xff // ending break
+};
+
+static const uint8_t spIndefiniteLenStringBad2[] = {
+ 0x81, // Array of length one
+ 0x7f, // text string marked with indefinite length
+ 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
+ 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
+ 0xff // ending break
+};
+
+static const uint8_t spIndefiniteLenStringBad3[] = {
+ 0x81, // Array of length one
+ 0x7f, // text string marked with indefinite length
+ 0x01, 0x02, // Not a string
+ 0xff // ending break
+};
+
+static const uint8_t spIndefiniteLenStringBad4[] = {
+ 0x81, // Array of length one
+ 0x7f, // text string marked with indefinite length
+ 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
+ 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
+ // missing end of string
+};
+
+static const uint8_t spIndefiniteLenStringLabel[] = {
+ 0xa1, // Array of length one
+ 0x7f, // text string marked with indefinite length
+ 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
+ 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
+ 0xff, // ending break
+ 0x01 // integer being labeled.
+};
+
+static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this
+{
+ UsefulOutBuf UOB;
+
+ UsefulOutBuf_Init(&UOB, Storage);
+ UsefulOutBuf_AppendByte(&UOB, 0x81);
+ UsefulOutBuf_AppendByte(&UOB, 0x5f);
+
+ int i = 0;
+ for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
+ UsefulOutBuf_AppendByte(&UOB, 0x58);
+ UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
+ for(int j = 0; j < nChunkSize; j++ ) {
+ UsefulOutBuf_AppendByte(&UOB, i);
+ i++;
+ }
+ }
+ UsefulOutBuf_AppendByte(&UOB, 0xff);
+
+ return UsefulOutBuf_OutUBuf(&UOB);
+}
+
+static int CheckBigString(UsefulBufC BigString)
+{
+ if(BigString.len != 255) {
+ return 1;
+ }
+
+ for(uint8_t i = 0; i < 255; i++){
+ if(((const uint8_t *)BigString.ptr)[i] != i) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+
+int IndefiniteLengthStringTest()
+{
+ QCBORDecodeContext DC;
+ QCBORItem Item;
+ // big enough for MakeIndefiniteBigBstr() + MemPool overhead
+ UsefulBuf_MAKE_STACK_UB(MemPool, 350);
+
+ // --- Simple normal indefinite length string ------
+ UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
+ QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
+
+ if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
+ return -1;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item)) {
+ return -2;
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
+ return -3;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item)) {
+ return -4;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
+ return -5;
+ }
+ if(QCBORDecode_Finish(&DC)) {
+ return -6;
+ }
+
+ // ----- types mismatch ---
+ QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
+
+ if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
+ return -7;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item)) {
+ return -8;
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -9;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
+ return -10;
+ }
+
+ // ----- not a string ---
+ QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
+
+ if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
+ return -11;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item)) {
+ return -12;
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -13;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
+ return -14;
+ }
+
+ // ----- no end -----
+ QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
+
+ if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
+ return -15;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item)) {
+ return -16;
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -17;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
+ return -18;
+ }
+
+ // ------ Don't set a string allocator and see an error -----
+ QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -19;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
+ return -20;
+ }
+
+ // ----- Mempool is way too small -----
+ UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
+
+ QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
+ if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
+ return -21;
+ }
+
+ // ----- Mempool is way too small -----
+ UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
+ const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
+
+ UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
+
+ QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
+ if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
+ return -22;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -23;
+ }
+ if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
+ return -24;
+ }
+
+ // ---- big bstr -----
+ QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
+
+ if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
+ return -25;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item)) {
+ return -26;
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
+ return -26;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item)) {
+ return -27;
+ }
+ if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
+ return -28;
+ }
+ if(CheckBigString(Item.val.string)) {
+ return -3;
+ }
+ if(QCBORDecode_Finish(&DC)) {
+ return -29;
+ }
+
+ // --- label is an indefinite length string ------
+ QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
+
+ if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
+ return -30;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_MAP) {
+ return -31;
+ }
+
+ if(QCBORDecode_GetNext(&DC, &Item)){
+ return -32;
+ }
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.uDataAlloc || !Item.uLabelAlloc ||
+ UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
+ return -33;
+ }
+
+ if(QCBORDecode_Finish(&DC)) {
+ return -34;
+ }
+
+ return 0;
+}
+
+
+int AllocAllStringsTest()
+{
+ QCBORDecodeContext DC;
+ QCBORError nCBORError;
+
+
+ // First test, use the "CSRMap" as easy input and checking
+ QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
+
+ UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
+
+ nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
+ if(nCBORError) {
+ return -1;
+ }
+
+ if(CheckCSRMaps(&DC)) {
+ return -2;
+ }
+
+ // Next parse, save pointers to a few strings, destroy original and see all is OK.
+ UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
+ const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
+
+ QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
+ UsefulBuf_Set(Pool, '/');
+ QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
+
+ QCBORItem Item1, Item2, Item3, Item4;
+ if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
+ return nCBORError;
+ if(Item1.uDataType != QCBOR_TYPE_MAP ||
+ Item1.val.uCount != 3)
+ return -3;
+ if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
+ return nCBORError;
+ if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
+ return nCBORError;
+ if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
+ return nCBORError;
+ if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
+ return nCBORError;
+
+ UsefulBuf_Set(CopyOfStorage, '_');
+
+ if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item1.uDataType != QCBOR_TYPE_INT64 ||
+ Item1.val.int64 != 42 ||
+ UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
+ return -4;
+ }
+
+
+ if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
+ Item2.uDataType != QCBOR_TYPE_ARRAY ||
+ Item2.val.uCount != 2)
+ return -5;
+
+ if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
+ return -6;
+ }
+
+ if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
+ return -7;
+ }
+
+ // Next parse with a pool that is too small
+ UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
+ QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
+ if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
+ return -8;
+ if(Item1.uDataType != QCBOR_TYPE_MAP ||
+ Item1.val.uCount != 3) {
+ return -9;
+ }
+ if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
+ if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
+ if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
+ nCBORError = QCBORDecode_GetNext(&DC, &Item4);
+ }
+ }
+ }
+ if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
+ return -10;
+ }
+
+ return 0;
+}
+
+// Cheating declaration to get to the special test hook
+size_t MemPoolTestHook_GetPoolSize(void *ctx);
+
+
+int MemPoolTest(void)
+{
+ // Set up the decoder with a tiny bit of CBOR to parse
+ QCBORDecodeContext DC;
+ const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
+ QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
+
+ // Set up an memory pool of 100 bytes
+ UsefulBuf_MAKE_STACK_UB(Pool, 100);
+ QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
+ if(nError) {
+ return -9;
+ }
+
+ // Cheat a little to get to the string allocator object
+ // so we can call it directly to test it
+ QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
+ // Cheat some more to know exactly the
+ size_t uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
+
+ // First test -- ask for too much in one go
+ UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool+1);
+ if(!UsefulBuf_IsNULL(Allocated)) {
+ return -1;
+ }
+
+
+ // Re do the set up for the next test that will do a successful alloc,
+ // a fail, a free and then success
+ // This test should work on 32 and 64-bit machines if the compiler
+ // does the expected thing with pointer sizes for the internal
+ // MemPool implementation leaving 44 or 72 bytes of pool memory.
+ QCBORDecode_SetMemPool(&DC, Pool, 0);
+
+ // Cheat a little to get to the string allocator object
+ // so we can call it directly to test it
+ pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
+ // Cheat some more to know exactly the
+ uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
+
+ Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool-1);
+ if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
+ return -2;
+ }
+ UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
+ if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
+ return -3;
+ }
+ (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
+ Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
+ if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
+ return -4;
+ }
+
+
+ // Re do set up for next test that involves a successful alloc,
+ // and a successful realloc and a failed realloc
+ QCBORDecode_SetMemPool(&DC, Pool, 0);
+
+ // Cheat a little to get to the string allocator object
+ // so we can call it directly to test it
+ pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
+ Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
+ if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
+ return -5;
+ }
+ Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool);
+ if(UsefulBuf_IsNULL(Allocated2)) {
+ return -6;
+ }
+ if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
+ return -7;
+ }
+ UsefulBuf Allocated3 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool+1);
+ if(!UsefulBuf_IsNULL(Allocated3)) { // expected to fail
+ return -8;
+ }
+
+ return 0;
+}
+
diff --git a/lib/ext/qcbor/test/qcbor_decode_tests.h b/lib/ext/qcbor/test/qcbor_decode_tests.h
new file mode 100644
index 0000000..2b09c55
--- /dev/null
+++ b/lib/ext/qcbor/test/qcbor_decode_tests.h
@@ -0,0 +1,229 @@
+/*==============================================================================
+ Copyright (c) 2016-2018, The Linux Foundation.
+ Copyright (c) 2018-2019, Laurence Lundblade.
+ All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of The Linux Foundation nor the names of its
+ contributors, nor the name "Laurence Lundblade" may be used to
+ endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ ==============================================================================*/
+
+#ifndef __QCBOR__qcbort_decode_tests__
+#define __QCBOR__qcbort_decode_tests__
+
+#include "qcbor.h"
+
+
+/*
+ Notes:
+
+ - All the functions in qcbor.h are called once in the aggregation of all the tests below.
+
+ - All the types that are supported are given as input and parsed by these tests
+
+ - There is some hostile input such as invalid lengths and CBOR too complex
+ and types this parser doesn't handle
+
+ */
+
+
+
+
+/*
+ Parse a well-known set of integers including those around the boundaries and
+ make sure the expected values come out
+ */
+int IntegerValuesParseTest(void);
+
+
+
+
+
+/*
+ Decode a simple CBOR encoded array and make sure it returns all the correct values.
+ This is a decode test.
+ */
+int SimpleArrayTest(void);
+
+
+/*
+ Make sure a maximally deep array can be parsed and that the
+ reported nesting level is correct. This uses test vector
+ of CBOR encoded data with a depth of 10. This a parse test.
+ */
+int ParseDeepArrayTest(void);
+
+
+/*
+ See that the correct error is reported when parsing
+ an array of depth 11, one too large.
+ */
+int ParseTooDeepArrayTest(void);
+
+
+/*
+ Try to parse some legit CBOR types that this parsers
+ doesn't support.
+ */
+int UnsupportedCBORDecodeTest(void);
+
+
+/*
+ This takes the encoded CBOR integers used in the above test and parses
+ it over and over with one more byte less each time. It should fail
+ every time on incorrect CBOR input. This is a hostile input decode test.
+ */
+int ShortBufferParseTest(void);
+
+
+/*
+ Same as ShortBufferParseTest, but with a different encoded CBOR input.
+ It is another hostile input test
+ */
+int ShortBufferParseTest2(void);
+
+
+/*
+ Parses the somewhat complicated CBOR MAP and makes sure all the correct
+ values parse out. About 15 values are tested. This is a decode test.
+ */
+int ParseMapTest(void);
+
+
+
+int FloatValuesTest1(void);
+
+
+
+int SimpleValuesTest1(void);
+
+
+/*
+
+ */
+int ParseMapAsArrayTest(void);
+
+
+
+int ParseSimpleTest(void);
+
+
+
+/*
+ Tests a number of failure cases on bad CBOR to get the right error code
+ */
+int FailureTests(void);
+
+
+/*
+ Parses all possible inputs that are two bytes long. Main point
+ is that the test doesn't crash as it doesn't evaluate the
+ input for correctness in any way.
+
+ (Parsing all possible 3 byte strings takes too long on all but
+ very fast machines).
+ */
+int ComprehensiveInputTest(void);
+
+
+/*
+ Parses all possible inputs that are four bytes long. Main point
+ is that the test doesn't crash as it doesn't evaluate the
+ input for correctness in any way. This runs very slow, so it
+ is only practical as a once-in-a-while regression test on
+ fast machines.
+ */
+int BigComprehensiveInputTest(void);
+
+
+/*
+ Thest the date types -- epoch and strings
+ */
+int DateParseTest(void);
+
+
+/*
+ Test optional tags like the CBOR magic number.
+ */
+int OptTagParseTest(void);
+
+
+/*
+ Parse some big numbers, positive and negative
+ */
+int BignumParseTest(void);
+
+
+int StringDecoderModeFailTest(void);
+
+
+/*
+ Parse some nested maps
+ */
+int NestedMapTest(void);
+
+
+/*
+ Parse maps with indefinite lengths
+ */
+int NestedMapTestIndefLen(void);
+
+
+/*
+ Parse some maps and arrays with indefinite lengths.
+ Includes some error cases.
+ */
+int IndefiniteLengthArrayMapTest(void);
+
+
+/*
+ Parse indefinite length strings. Uses
+ MemPool. Includes error cases.
+ */
+int IndefiniteLengthStringTest(void);
+
+
+/*
+ Test deep nesting of indefinite length
+ maps and arrays including too deep.
+ */
+int IndefiniteLengthNestTest(void);
+
+
+/*
+ Test parsing strings were all strings, not
+ just indefinite length strings, are
+ allocated. Includes error test cases.
+ */
+int AllocAllStringsTest(void);
+
+
+/*
+ Direct test of MemPool string allocator
+ */
+int MemPoolTest(void);
+
+
+#endif /* defined(__QCBOR__qcbort_decode_tests__) */
diff --git a/lib/ext/qcbor/test/qcbor_encode_tests.c b/lib/ext/qcbor/test/qcbor_encode_tests.c
new file mode 100644
index 0000000..2a22cf1
--- /dev/null
+++ b/lib/ext/qcbor/test/qcbor_encode_tests.c
@@ -0,0 +1,2007 @@
+/*==============================================================================
+ Copyright (c) 2016-2018, The Linux Foundation.
+ Copyright (c) 2018-2019, Laurence Lundblade.
+ All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of The Linux Foundation nor the names of its
+ contributors, nor the name "Laurence Lundblade" may be used to
+ endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ ==============================================================================*/
+
+#include "qcbor.h"
+#include "qcbor_encode_tests.h"
+
+
+/*
+ This is the test set for CBOR encoding.
+
+ This is largely complete for the implemented.
+
+ A few more things to do include:
+ - Add a test for counting the top level items and adding it back in with AddRaw()
+ - Run on some different CPUs like 32-bit and maybe even 16-bit
+ - Test the large array count limit
+ - Add the CBOR diagnostic output for every expected
+
+ */
+
+//#define PRINT_FUNCTIONS_FOR_DEBUGGINGXX
+
+#ifdef PRINT_FUNCTIONS_FOR_DEBUGGINGXX
+#include <stdio.h>
+
+// ifdef these out to not have compiler warnings
+static void printencoded(const uint8_t *pEncoded, size_t nLen)
+{
+ size_t i;
+ for(i = 0; i < nLen; i++) {
+ uint8_t Z = pEncoded[i];
+ printf("%02x ", Z);
+ }
+ printf("\n");
+
+ fflush(stdout);
+}
+
+
+// Do the comparison and print out where it fails
+static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
+ size_t i;
+ for(i = 0; i < U1.len; i++) {
+ if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
+ printf("Position: %d Actual: 0x%x Expected: 0x%x\n", i, ((uint8_t *)U1.ptr)[i], ((uint8_t *)U2.ptr)[i]);
+ return 1;
+ }
+ }
+ return 0;
+
+}
+
+#define CheckResults(Enc, Expected) \
+ UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
+
+#else
+
+#define CheckResults(Enc, Expected) \
+ UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
+
+#endif
+
+
+
+// One big buffer that is used by all the tests to encode into
+// Putting it in uninitialized data is better than using a lot
+// of stack. The tests should run on small devices too.
+static uint8_t spBigBuf[2200];
+
+
+
+/*
+ Some very minimal tests.
+ */
+int BasicEncodeTest()
+{
+ // Very simple CBOR, a map with one boolean that is true in it
+ QCBOREncodeContext EC;
+
+ QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+
+ QCBOREncode_OpenMap(&EC);
+ QCBOREncode_AddBoolToMapN(&EC, 66, true);
+ QCBOREncode_CloseMap(&EC);
+
+ UsefulBufC Encoded;
+ if(QCBOREncode_Finish(&EC, &Encoded)) {
+ return -1;
+ }
+
+
+ // Decode it and see that is right
+ QCBORDecodeContext DC;
+ QCBORItem Item;
+ QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_MAP) {
+ return -2;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_TRUE) {
+ return -3;
+ }
+
+ if(QCBORDecode_Finish(&DC)) {
+ return -4;
+ }
+
+
+ // Make another encoded message with the CBOR from the previous put into this one
+ UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
+ QCBOREncode_Init(&EC, MemoryForEncoded2);
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_AddUInt64(&EC, 451);
+ QCBOREncode_AddEncoded(&EC, Encoded);
+ QCBOREncode_OpenMap(&EC);
+ QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
+ QCBOREncode_CloseMap(&EC);
+ QCBOREncode_CloseArray(&EC);
+
+ UsefulBufC Encoded2;
+ if(QCBOREncode_Finish(&EC, &Encoded2)) {
+ return -5;
+ }
+ /*
+ [ // 0 1:3
+ 451, // 1 1:2
+ { // 1 1:2 2:1
+ 66: true // 2 1:1
+ },
+ { // 1 1:1 2:1
+ -70000: { // 2 1:1 2:1 3:1
+ 66: true // 3 XXXXXX
+ }
+ }
+ ]
+
+
+
+ 83 # array(3)
+ 19 01C3 # unsigned(451)
+ A1 # map(1)
+ 18 42 # unsigned(66)
+ F5 # primitive(21)
+ A1 # map(1)
+ 3A 0001116F # negative(69999)
+ A1 # map(1)
+ 18 42 # unsigned(66)
+ F5 # primitive(21)
+ */
+
+ // Decode it and see if it is OK
+ QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
+
+ // 0 1:3
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
+ return -6;
+ }
+
+ // 1 1:2
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
+ return -7;
+ }
+
+ // 1 1:2 2:1
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
+ return -8;
+ }
+
+ // 2 1:1
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_TRUE) {
+ return -9;
+ }
+
+ // 1 1:1 2:1
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
+ return -10;
+ }
+
+ // 2 1:1 2:1 3:1
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1 || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != -70000) {
+ return -11;
+ }
+
+ // 3 XXXXXX
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
+ return -12;
+ }
+
+ if(QCBORDecode_Finish(&DC)) {
+ return -13;
+ }
+
+ return 0;
+}
+
+
+
+static const uint8_t spExpectedEncodedAll[] = {
+ 0x98, 0x22, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
+ 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
+ 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
+ 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
+ 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
+ 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
+ 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
+ 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
+ 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
+ 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
+ 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
+ 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
+ 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
+ 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
+ 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
+ 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
+ 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
+ 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
+ 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
+ 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
+ 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
+ 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
+ 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
+ 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
+ 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
+ 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
+ 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
+ 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
+ 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
+ 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
+ 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
+ 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
+ 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
+ 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
+ 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
+ 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8,
+ 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
+ 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
+ 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
+ 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
+ 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
+ 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
+ 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
+ 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
+ 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
+ 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
+ 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
+ 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
+ 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
+ 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
+ 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
+ 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
+ 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
+ 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
+ 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
+ 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
+ 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
+ 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
+ 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
+ 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
+ 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
+ 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
+ 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
+ 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
+ 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
+ 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
+ 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
+ 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
+ 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
+ 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
+ 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
+ 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
+ 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
+ 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
+ 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
+ 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
+ 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
+ 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
+ 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
+ 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
+ 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
+ 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
+ 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
+ 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
+ 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
+ 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
+ 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
+ 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
+ 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
+ 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
+ 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
+ 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
+ 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
+ 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
+ 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
+ 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01,
+ 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
+ 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
+ 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
+ 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
+ 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
+ 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
+ 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
+ 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
+ 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
+ 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
+ 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
+ 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
+ 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
+ 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
+ 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
+ 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
+ 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
+ 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
+ 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
+ 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
+ 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
+ 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
+ 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
+ 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
+ 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
+ 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
+ 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
+ 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
+ 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
+ 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
+ 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
+ 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
+ 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
+ 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
+ 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57,
+ 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
+ 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
+ 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
+ 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
+ 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
+ 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
+ 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
+ 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
+ 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
+ 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
+ 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
+ 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
+ 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
+ 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
+ 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
+ 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
+ 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
+ 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
+ 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
+ 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
+ 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
+ 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
+ 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
+ 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
+ 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
+ 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
+ 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
+ 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
+ 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
+ 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
+ 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
+ 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
+ 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
+ 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
+ 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
+ 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
+ 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
+ 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
+ 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
+ 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
+ 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
+ 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
+ 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
+ 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
+ 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
+ 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
+ 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
+ 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
+ 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
+ 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
+ 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
+ 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
+ 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
+ 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
+ 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
+ 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
+ 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
+ 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
+ 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
+ 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
+ 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
+ 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
+ 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
+ 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
+ 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
+ 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
+ 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
+ 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
+ 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00
+};
+
+
+static const char *szMIME = "\
+MIME-Version: 1.0\n\
+Content-Type: multipart/mixed;\n\
+boundary=\"XXXXboundary text\"\n\
+\n\
+This is a multipart message in MIME format.\n\
+\n\
+--XXXXboundary text\n\
+Content-Type: text/plain\n\
+\n\
+this is the body text\n\
+\n\
+--XXXXboundary text\n\
+Content-Type: text/plain;\n\
+Content-Disposition: attachment;\n\
+filename=\"test.txt\"\n\
+\n\
+this is the attachment text\n\
+\n\
+--XXXXboundary text--";
+
+
+int AllAddMethodsTest()
+{
+ // TODO: this test should be broken down into several so it is more managable. Tags and labels could be more sensible
+ QCBOREncodeContext ECtx;
+ int nReturn = 0;
+
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+
+ QCBOREncode_OpenArray(&ECtx);
+
+ // Some ints that are tagged and have strings preceeding them (not labels becase it is not a map)
+ QCBOREncode_AddSZString(&ECtx, "UINT62");
+ QCBOREncode_AddTag(&ECtx, 100);
+ QCBOREncode_AddUInt64(&ECtx, 89989909);
+ QCBOREncode_AddSZString(&ECtx, "INT64");
+ QCBOREncode_AddTag(&ECtx, 76);
+ QCBOREncode_AddInt64(&ECtx, 77689989909);
+ QCBOREncode_AddUInt64(&ECtx,0);
+ QCBOREncode_AddInt64(&ECtx, -44);
+
+ // ints that go in maps
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
+ QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
+ QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
+ QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
+ QCBOREncode_CloseMap(&ECtx);
+
+ // Epoch Date
+ QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
+
+ // Epoch date with labels
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddDateEpochToMap(&ECtx, "LongLiveDenisRitchie", 1400000000);
+ QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
+ QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
+ QCBOREncode_CloseMap(&ECtx);
+
+ // Binary blobs
+ QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
+
+ // binary blobs in maps
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddSZString(&ECtx, "binbin");
+ QCBOREncode_AddTag(&ECtx, 100000);
+ QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
+ QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
+ QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
+ QCBOREncode_CloseMap(&ECtx);
+
+ // text blobs
+ QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
+ QCBOREncode_AddSZString(&ECtx, "oof\n");
+ QCBOREncode_AddURI(&ECtx, UsefulBuf_FROM_SZ_LITERAL("http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8"));
+ QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
+ QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
+ QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
+
+ // text blobs in maps
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
+ QCBOREncode_AddTextToMap(&ECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
+ QCBOREncode_AddSZString(&ECtx, "()()()");
+ QCBOREncode_AddTag(&ECtx, 1000);
+ QCBOREncode_AddSZString(&ECtx, "rab rab oof");
+ QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
+ QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
+ QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
+ QCBOREncode_AddURIToMap(&ECtx, "RFC", UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
+ QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
+ QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
+ QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
+ QCBOREncode_AddRegexToMap(&ECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
+ QCBOREncode_AddRegexToMapN(&ECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
+ QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
+ QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
+ QCBOREncode_CloseMap(&ECtx);
+
+ // Date strings
+ QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
+ QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
+ QCBOREncode_CloseMap(&ECtx);
+
+ // true / false ...
+ QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddSZString(&ECtx, "dare");
+ QCBOREncode_AddTag(&ECtx, 66);
+ QCBOREncode_AddBool(&ECtx, true);
+ QCBOREncode_AddBoolToMap(&ECtx, "uu", false);
+ QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
+ QCBOREncode_CloseMap(&ECtx);
+
+ // opening an array
+ QCBOREncode_OpenArray(&ECtx);
+ QCBOREncode_CloseArray(&ECtx);
+
+ // opening arrays in a map
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddSZString(&ECtx, "label and tagged empty array");
+ QCBOREncode_AddTag(&ECtx, 1093);
+ QCBOREncode_OpenArray(&ECtx);
+ QCBOREncode_CloseArray(&ECtx);
+ QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
+ QCBOREncode_CloseArray(&ECtx);
+ QCBOREncode_OpenArrayInMapN(&ECtx, 42);
+ QCBOREncode_CloseArray(&ECtx);
+ QCBOREncode_CloseMap(&ECtx);
+
+ // opening maps with labels and tagging
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_OpenMapInMap(&ECtx, "in a map");
+ QCBOREncode_OpenMapInMapN(&ECtx, 5556);
+ QCBOREncode_AddSZString(&ECtx, "in a in a in a");
+ QCBOREncode_AddTag(&ECtx, 9087);
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_CloseMap(&ECtx);
+ QCBOREncode_CloseMap(&ECtx);
+ QCBOREncode_CloseMap(&ECtx);
+ QCBOREncode_CloseMap(&ECtx);
+
+
+ // Extended simple values (these are not standard...)
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddSZString(&ECtx, "s1");
+ QCBOREncode_AddTag(&ECtx, 88);
+ QCBOREncode_AddSimple(&ECtx, 255);
+ QCBOREncode_AddSimpleToMap(&ECtx, "s2", 0);
+ QCBOREncode_AddSZString(&ECtx, "s3");
+ QCBOREncode_AddTag(&ECtx, 88);
+ QCBOREncode_AddSimple(&ECtx, 33);
+ QCBOREncode_AddInt64(&ECtx, 88378374); // label before tag
+ QCBOREncode_AddTag(&ECtx, 88);
+ QCBOREncode_AddSimple(&ECtx, 255);
+ QCBOREncode_AddInt64(&ECtx, 89); // label before tag
+ QCBOREncode_AddTag(&ECtx, 88);
+ QCBOREncode_AddSimple(&ECtx, 19);
+ QCBOREncode_CloseMap(&ECtx);
+
+ // UUIDs
+ static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
+ const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
+ QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
+ QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
+ QCBOREncode_CloseMap(&ECtx);
+
+ // Bool
+ QCBOREncode_AddBool(&ECtx, true);
+ QCBOREncode_AddBool(&ECtx, false);
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
+ QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
+ QCBOREncode_CloseMap(&ECtx);
+
+
+ static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
+ QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
+ QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
+ QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
+ QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
+ QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
+ QCBOREncode_CloseMap(&ECtx);
+
+ QCBOREncode_CloseArray(&ECtx);
+
+ UsefulBufC Enc;
+
+ if(QCBOREncode_Finish(&ECtx, &Enc)) {
+ nReturn = -1;
+ goto Done;
+ }
+
+ if(CheckResults(Enc, spExpectedEncodedAll))
+ nReturn = -2;
+
+Done:
+ return nReturn;
+}
+
+/*
+ 98 2F # array(47)
+ 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
+ 3B 0000000100000000 # negative(4294967296)
+ 3A FFFFFFFF # negative(4294967295)
+ 3A FFFFFFFE # negative(4294967294)
+ 3A FFFFFFFD # negative(4294967293)
+ 3A 7FFFFFFF # negative(2147483647)
+ 3A 7FFFFFFE # negative(2147483646)
+ 3A 00010001 # negative(65537)
+ 3A 00010000 # negative(65536)
+ 39 FFFF # negative(65535)
+ 39 FFFE # negative(65534)
+ 39 FFFD # negative(65533)
+ 39 0100 # negative(256)
+ 38 FF # negative(255)
+ 38 FE # negative(254)
+ 38 FD # negative(253)
+ 38 18 # negative(24)
+ 37 # negative(23)
+ 36 # negative(22)
+ 20 # negative(0)
+ 00 # unsigned(0)
+ 00 # unsigned(0)
+ 01 # unsigned(1)
+ 16 # unsigned(22)
+ 17 # unsigned(23)
+ 18 18 # unsigned(24)
+ 18 19 # unsigned(25)
+ 18 1A # unsigned(26)
+ 18 FE # unsigned(254)
+ 18 FF # unsigned(255)
+ 19 0100 # unsigned(256)
+ 19 0101 # unsigned(257)
+ 19 FFFE # unsigned(65534)
+ 19 FFFF # unsigned(65535)
+ 1A 00010000 # unsigned(65536)
+ 1A 00010001 # unsigned(65537)
+ 1A 00010002 # unsigned(65538)
+ 1A 7FFFFFFF # unsigned(2147483647)
+ 1A 7FFFFFFF # unsigned(2147483647)
+ 1A 80000000 # unsigned(2147483648)
+ 1A 80000001 # unsigned(2147483649)
+ 1A FFFFFFFE # unsigned(4294967294)
+ 1A FFFFFFFF # unsigned(4294967295)
+ 1B 0000000100000000 # unsigned(4294967296)
+ 1B 0000000100000001 # unsigned(4294967297)
+ 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
+ 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
+ */
+static const uint8_t spExpectedEncodedInts[] = {
+ 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
+ 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
+ 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
+ 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
+ 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
+ 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
+ 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
+ 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
+ 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
+ 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
+ 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
+ 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
+ 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
+ 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
+ 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
+ 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
+ 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
+ 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
+ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
+ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
+ 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff};
+
+/*
+
+ Test the generation of integers. This also ends up testing
+ encoding of all the different lengths. It encodes integers
+ of many lengths and values, especially around the boundaries
+ for different types of integers. It compares the output
+ to expected values generated from http://cbor.me.
+
+ */
+int IntegerValuesTest1()
+{
+ QCBOREncodeContext ECtx;
+ int nReturn = 0;
+
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_OpenArray(&ECtx);
+
+ QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
+ QCBOREncode_AddInt64(&ECtx, -4294967297);
+ QCBOREncode_AddInt64(&ECtx, -4294967296);
+ QCBOREncode_AddInt64(&ECtx, -4294967295);
+ QCBOREncode_AddInt64(&ECtx, -4294967294);
+ QCBOREncode_AddInt64(&ECtx, -2147483648);
+ QCBOREncode_AddInt64(&ECtx, -2147483647);
+ QCBOREncode_AddInt64(&ECtx, -65538);
+ QCBOREncode_AddInt64(&ECtx, -65537);
+ QCBOREncode_AddInt64(&ECtx, -65536);
+ QCBOREncode_AddInt64(&ECtx, -65535);
+ QCBOREncode_AddInt64(&ECtx, -65534);
+ QCBOREncode_AddInt64(&ECtx, -257);
+ QCBOREncode_AddInt64(&ECtx, -256);
+ QCBOREncode_AddInt64(&ECtx, -255);
+ QCBOREncode_AddInt64(&ECtx, -254);
+ QCBOREncode_AddInt64(&ECtx, -25);
+ QCBOREncode_AddInt64(&ECtx, -24);
+ QCBOREncode_AddInt64(&ECtx, -23);
+ QCBOREncode_AddInt64(&ECtx, -1);
+ QCBOREncode_AddInt64(&ECtx, 0);
+ QCBOREncode_AddUInt64(&ECtx, 0ULL);
+ QCBOREncode_AddInt64(&ECtx, 1);
+ QCBOREncode_AddInt64(&ECtx, 22);
+ QCBOREncode_AddInt64(&ECtx, 23);
+ QCBOREncode_AddInt64(&ECtx, 24);
+ QCBOREncode_AddInt64(&ECtx, 25);
+ QCBOREncode_AddInt64(&ECtx, 26);
+ QCBOREncode_AddInt64(&ECtx, 254);
+ QCBOREncode_AddInt64(&ECtx, 255);
+ QCBOREncode_AddInt64(&ECtx, 256);
+ QCBOREncode_AddInt64(&ECtx, 257);
+ QCBOREncode_AddInt64(&ECtx, 65534);
+ QCBOREncode_AddInt64(&ECtx, 65535);
+ QCBOREncode_AddInt64(&ECtx, 65536);
+ QCBOREncode_AddInt64(&ECtx, 65537);
+ QCBOREncode_AddInt64(&ECtx, 65538);
+ QCBOREncode_AddInt64(&ECtx, 2147483647);
+ QCBOREncode_AddInt64(&ECtx, 2147483647);
+ QCBOREncode_AddInt64(&ECtx, 2147483648);
+ QCBOREncode_AddInt64(&ECtx, 2147483649);
+ QCBOREncode_AddInt64(&ECtx, 4294967294);
+ QCBOREncode_AddInt64(&ECtx, 4294967295);
+ QCBOREncode_AddInt64(&ECtx, 4294967296);
+ QCBOREncode_AddInt64(&ECtx, 4294967297);
+ QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
+ QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
+
+ QCBOREncode_CloseArray(&ECtx);
+
+ UsefulBufC Enc;
+ if(QCBOREncode_Finish(&ECtx, &Enc)) {
+ nReturn = -1;
+ }
+
+ if(CheckResults(Enc, spExpectedEncodedInts))
+ return -2;
+
+ return(nReturn);
+}
+
+
+/*
+ 85 # array(5)
+ F5 # primitive(21)
+ F4 # primitive(20)
+ F6 # primitive(22)
+ F7 # primitive(23)
+ A1 # map(1)
+ 65 # text(5)
+ 554E446566 # "UNDef"
+ F7 # primitive(23)
+ */
+static const uint8_t spExpectedEncodedSimple[] = {
+ 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
+
+int SimpleValuesTest1()
+{
+ QCBOREncodeContext ECtx;
+ int nReturn = 0;
+
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_OpenArray(&ECtx);
+
+ QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
+ QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
+ QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
+ QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
+
+ QCBOREncode_OpenMap(&ECtx);
+
+ QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
+ QCBOREncode_CloseMap(&ECtx);
+
+ QCBOREncode_CloseArray(&ECtx);
+
+ UsefulBufC ECBOR;
+ if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
+ nReturn = -1;
+ }
+
+ if(CheckResults(ECBOR, spExpectedEncodedSimple))
+ return -2;
+
+ return(nReturn);
+}
+
+
+/*
+ 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)
+ */
+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
+};
+
+int 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
+
+
+ QCBOREncode_OpenMap(&ECtx);
+
+ QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
+
+ QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
+
+ QCBOREncode_CloseMap(&ECtx);
+
+ QCBOREncode_CloseArray(&ECtx);
+
+ UsefulBufC ECBOR;
+
+ if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
+ nReturn = -1;
+ }
+
+ if(CheckResults(ECBOR, spExpectedEncodedDates))
+ return -2;
+
+ return(nReturn);
+}
+
+
+int ArrayNestingTest1()
+{
+ QCBOREncodeContext ECtx;
+ int i;
+ int nReturn = 0;
+
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
+ QCBOREncode_OpenArray(&ECtx);
+ }
+ for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
+ QCBOREncode_CloseArray(&ECtx);
+ }
+ UsefulBufC Encoded;
+ if(QCBOREncode_Finish(&ECtx, &Encoded)) {
+ nReturn = -1;
+ }
+
+ return(nReturn);
+}
+
+
+
+int ArrayNestingTest2()
+{
+ QCBOREncodeContext ECtx;
+ int i;
+ int nReturn = 0;
+
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
+ QCBOREncode_OpenArray(&ECtx);
+ }
+ for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
+ QCBOREncode_CloseArray(&ECtx);
+ }
+
+ UsefulBufC Encoded;
+ if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
+ nReturn = -1;
+ }
+
+ return(nReturn);
+}
+
+
+
+int ArrayNestingTest3()
+{
+ QCBOREncodeContext ECtx;
+ int i;
+ int nReturn = 0;
+
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
+ QCBOREncode_OpenArray(&ECtx);
+ }
+ for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
+ QCBOREncode_CloseArray(&ECtx);
+ }
+ UsefulBufC Encoded;
+ if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
+ nReturn = -1;
+ }
+
+ return(nReturn);
+}
+
+
+/*
+ 81 # array(1)
+ 81 # array(1)
+ 81 # array(1)
+ 81 # array(1)
+ 80 # array(0)
+*/
+static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
+
+// Validated at http://cbor.me and by manually examining its output
+/*
+ 82 # array(2)
+ 81 # array(1)
+ 81 # array(1)
+ 81 # array(1)
+ 81 # array(1)
+ 80 # array(0)
+ 98 2F # array(47)
+ 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
+ 3B 0000000100000000 # negative(4294967296)
+ 3A FFFFFFFF # negative(4294967295)
+ 3A FFFFFFFE # negative(4294967294)
+ 3A FFFFFFFD # negative(4294967293)
+ 3A 7FFFFFFF # negative(2147483647)
+ 3A 7FFFFFFE # negative(2147483646)
+ 3A 00010001 # negative(65537)
+ 3A 00010000 # negative(65536)
+ 39 FFFF # negative(65535)
+ 39 FFFE # negative(65534)
+ 39 FFFD # negative(65533)
+ 39 0100 # negative(256)
+ 38 FF # negative(255)
+ 38 FE # negative(254)
+ 38 FD # negative(253)
+ 38 18 # negative(24)
+ 37 # negative(23)
+ 36 # negative(22)
+ 20 # negative(0)
+ 00 # unsigned(0)
+ 00 # unsigned(0)
+ 01 # unsigned(1)
+ 16 # unsigned(22)
+ 17 # unsigned(23)
+ 18 18 # unsigned(24)
+ 18 19 # unsigned(25)
+ 18 1A # unsigned(26)
+ 18 FE # unsigned(254)
+ 18 FF # unsigned(255)
+ 19 0100 # unsigned(256)
+ 19 0101 # unsigned(257)
+ 19 FFFE # unsigned(65534)
+ 19 FFFF # unsigned(65535)
+ 1A 00010000 # unsigned(65536)
+ 1A 00010001 # unsigned(65537)
+ 1A 00010002 # unsigned(65538)
+ 1A 7FFFFFFF # unsigned(2147483647)
+ 1A 7FFFFFFF # unsigned(2147483647)
+ 1A 80000000 # unsigned(2147483648)
+ 1A 80000001 # unsigned(2147483649)
+ 1A FFFFFFFE # unsigned(4294967294)
+ 1A FFFFFFFF # unsigned(4294967295)
+ 1B 0000000100000000 # unsigned(4294967296)
+ 1B 0000000100000001 # unsigned(4294967297)
+ 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
+ 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
+ */
+static const uint8_t spEncodeRawExpected[] = {
+ 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
+ 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
+ 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
+ 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
+ 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
+ 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
+ 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
+ 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
+ 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
+ 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
+ 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
+ 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
+ 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
+ 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
+ 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
+ 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
+ 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
+ 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
+ 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
+ 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
+
+
+int EncodeRawTest()
+{
+ QCBOREncodeContext ECtx;
+
+ QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_OpenArray(&ECtx);
+ QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
+ QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
+ QCBOREncode_CloseArray(&ECtx);
+
+ UsefulBufC EncodedRawTest;
+
+ if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
+ return -4;
+ }
+
+ if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
+ return -5;
+ }
+
+ return 0;
+}
+
+/*
+ This returns a pointer to spBigBuf
+ */
+static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
+{
+ QCBOREncodeContext ECtx;
+ int nReturn = -1;
+
+ *pEncoded = NULL;
+ *pEncodedLen = INT32_MAX;
+ size_t uFirstSizeEstimate = 0;
+
+ // loop runs CBOR encoding twice. First with no buffer to
+ // calucate the length so buffer can be allocated correctly,
+ // and last with the buffer to do the actual encoding
+ do {
+ QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
+ QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
+ QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
+ QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
+ QCBOREncode_CloseArray(&ECtx);
+ QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
+ QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
+ QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
+ QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
+ QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
+ QCBOREncode_CloseMap(&ECtx);
+ QCBOREncode_CloseMap(&ECtx);
+
+ if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
+ goto Done;
+ if(*pEncoded != NULL) {
+ if(uFirstSizeEstimate != *pEncodedLen) {
+ nReturn = 1;
+ } else {
+ nReturn = 0;
+ }
+ goto Done;
+ }
+ *pEncoded = spBigBuf;
+ uFirstSizeEstimate = *pEncodedLen;
+
+ } while(1);
+
+ Done:
+ return(nReturn);
+}
+
+/*
+ A3 # map(3)
+ 6D # text(13)
+ 666972737420696E7465676572 # "first integer"
+ 18 2A # unsigned(42)
+ 77 # text(23)
+ 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
+ 82 # array(2)
+ 67 # text(7)
+ 737472696E6731 # "string1"
+ 67 # text(7)
+ 737472696E6732 # "string2"
+ 6C # text(12)
+ 6D617020696E2061206D6170 # "map in a map"
+ A4 # map(4)
+ 67 # text(7)
+ 62797465732031 # "bytes 1"
+ 44 # bytes(4)
+ 78787878 # "xxxx"
+ 67 # text(7)
+ 62797465732032 # "bytes 2"
+ 44 # bytes(4)
+ 79797979 # "yyyy"
+ 6B # text(11)
+ 616E6F7468657220696E74 # "another int"
+ 18 62 # unsigned(98)
+ 66 # text(6)
+ 746578742032 # "text 2"
+ 78 1E # text(30)
+ 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
+ */
+static const uint8_t spValidMapEncoded[] = {
+ 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
+ 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
+ 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
+ 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
+ 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
+ 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
+ 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
+ 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
+ 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
+ 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
+ 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
+ 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
+ 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
+ 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
+ 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
+ 0x73 } ;
+
+
+int MapEncodeTest()
+{
+ uint8_t *pEncodedMaps;
+ size_t nEncodedMapLen;
+
+ if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
+ return -1;
+ }
+
+ int nReturn = 0;
+ if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
+ nReturn = 2;
+
+ return(nReturn);
+}
+
+
+/*
+ @brief Encode the RTIC results
+
+ @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
+ @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
+ @param[in] szAlexString Diagnostic code.
+ @param[in[ pOut Buffer to put the result in
+ @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
+
+ @return
+ One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
+
+ The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
+ short an error will be returned. This function will never write off the end
+ of the buffer passed to it.
+
+ If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
+ length of the encoded CBOR.
+
+ */
+
+static UsefulBufC FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, UsefulBuf Storage)
+{
+ // Buffer that the result will be written in to
+ // It is fixed size and small that a stack variable will be fine
+ // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
+
+ // Context for the encoder
+ QCBOREncodeContext ECtx;
+ QCBOREncode_Init(&ECtx, Storage);
+
+ // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
+ // Contents are label / value pairs
+ QCBOREncode_OpenMap(&ECtx);
+
+ { // Brace / indention just to show CBOR encoding nesting
+
+ // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
+ QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
+
+ // Add the diagnostic code
+ QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
+
+ // Add a time stamp
+ if(time) {
+ QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
+ }
+
+ // Add the diagnostic code
+ QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
+
+ // Open a subordinate map for telemtry data
+ QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
+
+ { // Brace / indention just to show CBOR encoding nesting
+
+ // Add a few fake integers and buffers for now.
+ QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
+
+ // Add a few fake integers and buffers for now.
+ QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
+
+ // Add a few fake integers and buffers for now.
+ static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
+ const UsefulBufC WSPV = {pPV, sizeof(pPV)};
+
+ QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
+ }
+ }
+
+ // Close the telemetry map
+ QCBOREncode_CloseMap(&ECtx);
+
+ // Close the map
+ QCBOREncode_CloseMap(&ECtx);
+
+ UsefulBufC Result;
+
+ QCBOREncode_Finish(&ECtx, &Result);
+
+ return Result;
+}
+
+
+/*
+ A5 # map(5)
+ 69 # text(9)
+ 696E74656772697479 # "integrity"
+ F4 # primitive(20)
+ 64 # text(4)
+ 74797065 # "type"
+ 66 # text(6)
+ 726563656E74 # "recent"
+ 64 # text(4)
+ 74696D65 # "time"
+ C1 # tag(1)
+ 1A 580D4172 # unsigned(1477263730)
+ 64 # text(4)
+ 64696167 # "diag"
+ 6A # text(10)
+ 30784131654335303031 # "0xA1eC5001"
+ 69 # text(9)
+ 74656C656D65747279 # "telemetry"
+ A3 # map(3)
+ 69 # text(9)
+ 53686F652053697A65 # "Shoe Size"
+ 0C # unsigned(12)
+ 62 # text(2)
+ 4951 # "IQ"
+ 1A FFFFFFFF # unsigned(4294967295)
+ 77 # text(23)
+ 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
+ 48 # bytes(8)
+ 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
+ */
+static const uint8_t spExpectedRTIC[] = {
+ 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
+ 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
+ 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
+ 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
+ 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
+ 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
+ 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
+ 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
+ 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
+ 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
+ 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
+ 0xaa, 0xbb, 0x01, 0x01};
+
+
+int RTICResultsTest()
+{
+ const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
+ "recent", "0xA1eC5001",
+ UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ if(UsefulBuf_IsNULLC(Encoded)) {
+ return -1;
+ }
+
+ if(CheckResults(Encoded, spExpectedRTIC)) {
+ return -2;
+ }
+
+ return 0;
+}
+
+
+/*
+ 82 # array(2)
+ 19 01C3 # unsigned(451)
+ 43 # bytes(3)
+ 1901D2 # "\x19\x01\xD2"
+*/
+static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
+
+/*
+ Very basic bstr wrapping test
+ */
+int BstrWrapTest()
+{
+ QCBOREncodeContext EC;
+
+ QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_AddUInt64(&EC, 451);
+
+ QCBOREncode_BstrWrap(&EC);
+ QCBOREncode_AddUInt64(&EC, 466);
+
+ UsefulBufC Wrapped;
+ QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
+
+ QCBOREncode_CloseArray(&EC);
+
+ UsefulBufC Encoded;
+ if(QCBOREncode_Finish(&EC, &Encoded)) {
+ return -1;
+ }
+
+ if(CheckResults(Encoded, spExpectedBstrWrap)) {
+ return -2;
+ }
+
+ /* Another test; see about handling length calculation */
+ QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_BstrWrap(&EC);
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_AddNULL(&EC);
+ QCBOREncode_CloseArray(&EC);
+ UsefulBufC BStr;
+ QCBOREncode_CloseBstrWrap(&EC, &BStr);
+ // 2 is one byte for an array of length 1 and 1 byte for a NULL
+ if(BStr.ptr != NULL || BStr.len != 2) {
+ return -5;
+ }
+
+ return 0;
+}
+
+
+
+int BstrWrapErrorTest()
+{
+ // -------------- Test closing a bstrwrap when it is an array that is open -----------
+ QCBOREncodeContext EC;
+
+ QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_AddUInt64(&EC, 451);
+
+ QCBOREncode_BstrWrap(&EC);
+ QCBOREncode_AddUInt64(&EC, 466);
+ QCBOREncode_OpenArray(&EC);
+
+ UsefulBufC Wrapped;
+ QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
+
+ QCBOREncode_CloseArray(&EC);
+
+ UsefulBufC Encoded2;
+ if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
+ return -1;
+ }
+
+ // ----------- test closing a bstrwrap when nothing is open ---------------------
+ QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
+ if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
+ return -2;
+ }
+
+ // --------------- test nesting too deep ----------------------------------
+ QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+ for(int i = 1; i < 18; i++) {
+ QCBOREncode_BstrWrap(&EC);
+ }
+ QCBOREncode_AddBool(&EC, true);
+
+ for(int i = 1; i < 18; i++) {
+ QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
+ }
+
+ if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
+ return -3;
+ }
+
+ return 0;
+}
+
+
+
+// Part of bstr_wrap_nest_test
+/*
+ 83 array with three
+ 53 byte string with 19 bytes
+ 01 #1
+ 50 byte string with 16 bytes
+ 02
+ 4D byte string with 13 bytes
+ 03
+ 4A byte string with 10 bytes
+ 04
+ 47 byte string with 7 bytes
+ 05
+ 44 byte string with 4 bytes
+ 06
+ 41 byte string with 1 byte
+ 07
+ 01
+ 02
+ 03
+ 04
+ 05
+ 06
+ 07
+ A2 map with two items
+ 18 20 label for byte string
+ 54 byte string of length 20
+ 82 Array with two items
+ 10 The integer value 10
+ A2 map with two items
+ 18 21 label for byte string
+ 44 byte string with 4 bytes
+ 81 array with 1 item
+ 11 integer value 11
+ 18 30 integer value 30
+ 18 40 integer label 40
+ 65 68 65 6C 6C 6F text string hello
+ 18 31 integer value 31
+ 18 41 integer label 41
+ 65 68 65 6C 6C 6F text string hello
+
+
+ */
+
+
+/*
+ 83 # array(3)
+ 56 # bytes(22)
+ 00530150024D034A0447054406410700010203040506 # "\x00S\x01P\x02M\x03J\x04G\x05D\x06A\a\x00\x01\x02\x03\x04\x05\x06"
+ 07 # unsigned(7)
+ A2 # map(2)
+ 18 20 # unsigned(32)
+ 54 # bytes(20)
+ 8210A21821448111183018406568656C6C6F1831 # "\x82\x10\xA2\x18!D\x81\x11\x180\x18@ehello\x181"
+ 18 41 # unsigned(65)
+ 65 # text(5)
+ 68656C6C6F # "hello"
+ */
+static const uint8_t spExpectedDeepBstr[] =
+{
+ 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
+ 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
+ 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
+ 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
+ 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
+ 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
+ 0x6F
+};
+
+// Part of bstr_wrap_nest_test
+static int DecodeNextNested(UsefulBufC Wrapped)
+{
+ int nReturn;
+ QCBORDecodeContext DC;
+ QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORItem Item;
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -11;
+ }
+ if(Item.uDataType != QCBOR_TYPE_INT64) {
+ return -12;
+ }
+
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn == QCBOR_ERR_HIT_END) {
+ return 0;
+ }
+ if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
+ return -13;
+ }
+ nReturn = DecodeNextNested(Item.val.string);
+ if(nReturn) {
+ return nReturn;
+ }
+
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -14;
+ }
+ if(Item.uDataType != QCBOR_TYPE_INT64) {
+ return -15;
+ }
+
+ if(QCBORDecode_Finish(&DC)) {
+ return -16;
+ }
+
+ return 0;
+}
+
+// Part of bstr_wrap_nest_test
+static int DecodeNextNested2(UsefulBufC Wrapped)
+{
+ int nReturn;
+ QCBORDecodeContext DC;
+ QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORItem Item;
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -11;
+ }
+ if(Item.uDataType != QCBOR_TYPE_ARRAY) {
+ return -12;
+ }
+
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -11;
+ }
+ if(Item.uDataType != QCBOR_TYPE_INT64) {
+ return -12;
+ }
+
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -11;
+ }
+ if(Item.uDataType != QCBOR_TYPE_MAP) {
+ return 0;
+ }
+
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -11;
+ }
+ if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
+ return -13;
+ }
+ nReturn = DecodeNextNested2(Item.val.string);
+ if(nReturn) {
+ return nReturn;
+ }
+
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -11;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
+ return -12;
+ }
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -11;
+ }
+ if(Item.uDataType != QCBOR_TYPE_INT64) {
+ return -12;
+ }
+
+ if(QCBORDecode_Finish(&DC)) {
+ return -16;
+ }
+
+ return 0;
+}
+
+
+int BstrWrapNestTest()
+{
+ QCBOREncodeContext EC;
+ QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+
+ // ---- Make a complicated nested CBOR structure ---
+#define BSTR_TEST_DEPTH 10
+
+ QCBOREncode_OpenArray(&EC);
+
+ for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) {
+ QCBOREncode_BstrWrap(&EC);
+ QCBOREncode_AddUInt64(&EC, i);
+ }
+
+ for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) {
+ QCBOREncode_CloseBstrWrap(&EC, NULL);
+ QCBOREncode_AddUInt64(&EC, i);
+ }
+
+ for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) {
+ QCBOREncode_OpenMap(&EC);
+ QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_AddUInt64(&EC, i+0x10);
+ }
+
+ for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) {
+ QCBOREncode_CloseArray(&EC);
+ QCBOREncode_AddUInt64(&EC, i+0x30);
+ QCBOREncode_CloseBstrWrap(&EC, NULL);
+ QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
+ QCBOREncode_CloseMap(&EC);
+ }
+ QCBOREncode_CloseArray(&EC);
+
+ UsefulBufC Encoded;
+ if(QCBOREncode_Finish(&EC, &Encoded)) {
+ return -1;
+ }
+
+ // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
+ if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
+ return -25;
+ }
+
+
+ // ---- Decode it and see if it is OK ------
+ QCBORDecodeContext DC;
+ QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
+
+ QCBORItem Item;
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
+ return -2;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
+ return -3;
+ }
+
+ int nReturn = DecodeNextNested(Item.val.string);
+ if(nReturn) {
+ return nReturn;
+ }
+
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -11;
+ }
+ if(Item.uDataType != QCBOR_TYPE_INT64) {
+ return -12;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
+ return -2;
+ }
+
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
+ return -3;
+ }
+ nReturn = DecodeNextNested2(Item.val.string);
+ if(nReturn) {
+ return nReturn;
+ }
+
+ nReturn = QCBORDecode_GetNext(&DC, &Item);
+ if(nReturn) {
+ return -11;
+ }
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
+ return -12;
+ }
+
+ if(QCBORDecode_Finish(&DC)) {
+ return -16;
+ }
+
+ return 0;
+}
+
+
+static const uint8_t spSignature[] = {
+ 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
+ 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
+ 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
+ 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
+ 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
+ 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
+ 0x45, 0xca, 0xcb, 0x36};
+
+/*
+ D2 # tag(18)
+ 84 # array(4)
+ 43 # bytes(3)
+ A10126 # "\xA1\x01&"
+ A1 # map(1)
+ 04 # unsigned(4)
+ 42 # bytes(2)
+ 3131 # "11"
+ 54 # bytes(20)
+ 546869732069732074686520636F6E74656E742E # "This is the content."
+ 58 40 # bytes(64)
+ 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4\xCCk#\xD5\x8F\xEF\\\b1\x06\xC4\xD2Z\x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91\xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01\xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA\xCB6"
+ */
+static const uint8_t spExpected[] = {
+ 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
+ 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
+ 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
+ 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
+ 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
+ 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
+ 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
+ 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
+ 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
+ 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
+
+/*
+ this corresponds exactly to the example in RFC 8152
+ section C.2.1. This doesn't actually verify the signature
+ though that would be nice as it would make the test
+ really good. That would require bring in ECDSA crypto
+ to this test.
+ */
+int CoseSign1TBSTest()
+{
+ // All of this is from RFC 8152 C.2.1
+ const char *szKid = "11";
+ const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
+ const char *szPayload = "This is the content.";
+ const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
+ static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
+ const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
+
+ // It would be good to compare this to the output from
+ // a COSE implementation like COSE-C. It has been checked
+ // against the CBOR playground.
+ const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSignature);
+
+ QCBOREncodeContext EC;
+ QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+
+ // top level array for cose sign1, 18 is the tag for COSE sign
+ QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
+ QCBOREncode_OpenArray(&EC);
+
+ // Add protected headers
+ QCBOREncode_AddBytes(&EC, ProtectedHeaders);
+
+ // Empty map with unprotected headers
+ QCBOREncode_OpenMap(&EC);
+ QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
+ QCBOREncode_CloseMap(&EC);
+
+ // The payload
+ UsefulBufC WrappedPayload;
+ QCBOREncode_BstrWrap(&EC);
+ QCBOREncode_AddEncoded(&EC, Payload); // Payload is not actually CBOR in example C.2.1
+ QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
+
+ // Check we got back the actual payload expected
+ if(UsefulBuf_Compare(WrappedPayload, Payload)) {
+ return -1;
+ }
+
+ // The signature
+ QCBOREncode_AddBytes(&EC, Signature);
+ QCBOREncode_CloseArray(&EC);
+
+ // Finish and check the results
+ UsefulBufC COSE_Sign1;
+ if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
+ return -2;
+ }
+
+ // 98 is the size from RFC 8152 C.2.1
+ if(COSE_Sign1.len != 98) {
+ return -3;
+ }
+
+ if(CheckResults(COSE_Sign1, spExpected)) {
+ return -4;
+ }
+
+ return 0;
+}
+
+
+int EncodeErrorTests()
+{
+ QCBOREncodeContext EC;
+
+
+ // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
+ // Do all of these tests with NULL buffers so no actual large allocations are neccesary
+ UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
+
+ // First verify no error from a big buffer
+ QCBOREncode_Init(&EC, Buffer);
+ QCBOREncode_OpenArray(&EC);
+ // 6 is the CBOR overhead for opening the array and encodng the length
+ // This exactly fills the buffer.
+ QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
+ QCBOREncode_CloseArray(&EC);
+ size_t xx;
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
+ return -1;
+ }
+
+ // Second verify error from an array in encoded output too large
+ QCBOREncode_Init(&EC, Buffer);
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
+ QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
+ QCBOREncode_CloseArray(&EC);
+ QCBOREncode_CloseArray(&EC);
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
+ return -2;
+ }
+
+ // Third, fit an array in exactly at max position allowed
+ QCBOREncode_Init(&EC, Buffer);
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_CloseArray(&EC);
+ QCBOREncode_CloseArray(&EC);
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
+ return -10;
+ }
+
+
+ // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
+ // Work close to the 4GB size limit for a better test
+ const uint32_t uLargeSize = UINT32_MAX - 1024;
+ UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
+
+ QCBOREncode_Init(&EC, Large);
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
+ QCBOREncode_CloseArray(&EC);
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
+ // Making sure it succeeds when it should first
+ return -3;
+ }
+
+ QCBOREncode_Init(&EC, Large);
+ QCBOREncode_OpenArray(&EC);
+ QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
+ QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
+ QCBOREncode_CloseArray(&EC);
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
+ // Now just 1 byte over, see that it fails
+ return -4;
+ }
+
+
+ // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
+ QCBOREncode_Init(&EC, Large);
+ for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
+ QCBOREncode_OpenArray(&EC);
+ }
+ for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
+ QCBOREncode_CloseArray(&EC);
+ }
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
+ // Making sure it succeeds when it should first
+ return -5;
+ }
+
+ QCBOREncode_Init(&EC, Large);
+ for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
+ QCBOREncode_OpenArray(&EC);
+ }
+ for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
+ QCBOREncode_CloseArray(&EC);
+ }
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
+ // One more level to cause error
+ return -6;
+ }
+
+
+ // ------ QCBOR_ERR_TOO_MANY_CLOSES --------
+ QCBOREncode_Init(&EC, Large);
+ for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
+ QCBOREncode_OpenArray(&EC);
+ }
+ for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
+ QCBOREncode_CloseArray(&EC);
+ }
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_TOO_MANY_CLOSES) {
+ // One more level to cause error
+ return -7;
+ }
+
+
+ // ------ QCBOR_ERR_CLOSE_MISMATCH --------
+ QCBOREncode_Init(&EC, Large);
+ QCBOREncode_OpenArray(&EC);
+ UsefulBufC Wrap;
+ QCBOREncode_CloseBstrWrap(&EC, &Wrap);
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_CLOSE_MISMATCH) {
+ return -8;
+ }
+
+
+ // ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN ---------
+ QCBOREncode_Init(&EC, Large);
+ for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
+ QCBOREncode_OpenArray(&EC);
+ }
+ for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
+ QCBOREncode_CloseArray(&EC);
+ }
+ if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
+ // One more level to cause error
+ return -9;
+ }
+
+ /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
+ it would require a 64KB of RAM to test */
+
+ return 0;
+}
+
diff --git a/lib/ext/qcbor/test/qcbor_encode_tests.h b/lib/ext/qcbor/test/qcbor_encode_tests.h
new file mode 100644
index 0000000..33703d8
--- /dev/null
+++ b/lib/ext/qcbor/test/qcbor_encode_tests.h
@@ -0,0 +1,145 @@
+/*==============================================================================
+ Copyright (c) 2016-2018, The Linux Foundation.
+ Copyright (c) 2018-2019, Laurence Lundblade.
+ All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of The Linux Foundation nor the names of its
+ contributors, nor the name "Laurence Lundblade" may be used to
+ endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ ==============================================================================*/
+
+#ifndef __QCBOR__qcbor_encode_tests__
+#define __QCBOR__qcbor_encode_tests__
+
+#include "qcbor.h"
+
+
+/*
+ Notes:
+
+ - All the functions in qcbor.h are called once in the aggregation of all the tests below.
+
+ - All the types that are supported are given as input and parsed by these tests
+
+ - There is some hostile input such as invalid lengths and CBOR too complex
+ and types this parser doesn't handle
+
+ */
+
+
+/*
+ Most basic test.
+ */
+int BasicEncodeTest(void);
+
+
+/*
+ Encode lots of integer values, particularly around the boundary and make sure they
+ Match the expected binary output. Primarily an encoding test.
+ */
+int IntegerValuesTest1(void);
+
+
+
+/*
+ Create nested arrays to the max depth allowed and make sure it succeeds.
+ This is an encoding test.
+ */
+int ArrayNestingTest1(void);
+
+
+/*
+ Create nested arrays to one more than the meax depth and make sure it fails.
+ This is an encoding test.
+ */
+int ArrayNestingTest2(void);
+
+
+/*
+ Encoding test.
+ Create arrays to max depth and close one extra time and look for correct error code
+ */
+int ArrayNestingTest3(void);
+
+
+/*
+ This tests the QCBOREncode_AddRaw() function by adding two chunks or RAWCBOR to an
+ array and comparing with expected values. This is an encoding test.
+ */
+int EncodeRawTest(void);
+
+
+/*
+ This creates a somewhat complicated CBOR MAP and verifies it against expected
+ data. This is an encoding test.
+ */
+int MapEncodeTest(void);
+
+
+
+/*
+ Encodes a goodly number of floats and doubles and checks encoding is right
+ */
+int FloatValuesTest1(void);
+
+
+/*
+ Encodes true, false and the like
+ */
+int SimpleValuesTest1(void);
+
+
+/*
+ Encodes most data formats that are supported */
+int EncodeDateTest(void);
+
+
+/*
+ Encodes particular data structure that a particular app will need...
+ */
+int RTICResultsTest(void);
+
+
+/*
+ Calls all public encode methods in qcbor.h once.
+ */
+int AllAddMethodsTest(void);
+
+/*
+ The binary string wrapping of maps and arrays used by COSE
+ */
+int BstrWrapTest(void);
+
+int BstrWrapErrorTest(void);
+
+int BstrWrapNestTest(void);
+
+int CoseSign1TBSTest(void);
+
+int EncodeErrorTests(void);
+
+
+
+#endif /* defined(__QCBOR__qcbor_encode_tests__) */
diff --git a/lib/ext/qcbor/test/run_tests.c b/lib/ext/qcbor/test/run_tests.c
new file mode 100644
index 0000000..6e35620
--- /dev/null
+++ b/lib/ext/qcbor/test/run_tests.c
@@ -0,0 +1,285 @@
+/*==============================================================================
+ run_tests.c -- test aggregator and results reporting
+
+ Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+
+ SPDX-License-Identifier: BSD-3-Clause
+
+ See BSD-3-Clause license in README.md
+
+ Created on 9/30/18
+ ==============================================================================*/
+
+#include "run_tests.h"
+#include "UsefulBuf.h"
+#include <stdbool.h>
+
+#include "float_tests.h"
+#include "qcbor_decode_tests.h"
+#include "qcbor_encode_tests.h"
+#include "UsefulBuf_Tests.h"
+
+
+
+// Used to test RunTests
+int fail_test()
+{
+ return -44;
+}
+
+
+
+
+/*
+ Convert a number up to 999999999 to a string. This is so sprintf doesn't
+ have to be linked in so as to minimized dependencies even in test code.
+ */
+const char *NumToString(int32_t nNum, UsefulBuf StringMem)
+{
+ const int32_t nMax = 1000000000;
+
+ UsefulOutBuf OutBuf;
+ UsefulOutBuf_Init(&OutBuf, StringMem);
+
+ if(nNum < 0) {
+ UsefulOutBuf_AppendByte(&OutBuf, '-');
+ nNum = -nNum;
+ }
+ if(nNum > nMax-1) {
+ return "XXX";
+ }
+
+ bool bDidSomeOutput = false;
+ for(int n = nMax; n > 0; n/=10) {
+ int x = nNum/n;
+ if(x || bDidSomeOutput){
+ bDidSomeOutput = true;
+ UsefulOutBuf_AppendByte(&OutBuf, '0' + x);
+ nNum -= x * n;
+ }
+ }
+ if(!bDidSomeOutput){
+ UsefulOutBuf_AppendByte(&OutBuf, '0');
+ }
+ UsefulOutBuf_AppendByte(&OutBuf, '\0');
+
+ return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr;
+}
+
+
+
+
+typedef int (test_fun_t)(void);
+typedef const char * (test_fun2_t)(void);
+
+
+#define TEST_ENTRY(test_name) {#test_name, test_name, true}
+#define TEST_ENTRY_DISABLED(test_name) {#test_name, test_name, false}
+
+typedef struct {
+ const char *szTestName;
+ test_fun_t *test_fun;
+ bool bEnabled;
+} test_entry;
+
+typedef struct {
+ const char *szTestName;
+ test_fun2_t *test_fun;
+ bool bEnabled;
+} test_entry2;
+
+test_entry2 s_tests2[] = {
+ TEST_ENTRY(UBUTest_CopyUtil),
+ TEST_ENTRY(UOBTest_NonAdversarial),
+ TEST_ENTRY(TestBasicSanity),
+ TEST_ENTRY(UOBTest_BoundaryConditionsTest),
+ TEST_ENTRY(UBMacroConversionsTest),
+ TEST_ENTRY(UBUtilTests),
+ TEST_ENTRY(UIBTest_IntegerFormat)
+};
+
+
+test_entry s_tests[] = {
+ TEST_ENTRY(ParseMapAsArrayTest),
+ TEST_ENTRY_DISABLED(AllocAllStringsTest),
+ TEST_ENTRY(IndefiniteLengthNestTest),
+ TEST_ENTRY(NestedMapTestIndefLen),
+ TEST_ENTRY(ParseSimpleTest),
+ TEST_ENTRY(EncodeRawTest),
+ TEST_ENTRY(RTICResultsTest),
+ TEST_ENTRY(MapEncodeTest),
+ TEST_ENTRY(ArrayNestingTest1),
+ TEST_ENTRY(ArrayNestingTest2),
+ TEST_ENTRY(ArrayNestingTest3),
+ TEST_ENTRY(EncodeDateTest),
+ TEST_ENTRY(SimpleValuesTest1),
+ TEST_ENTRY(IntegerValuesTest1),
+ TEST_ENTRY(AllAddMethodsTest),
+ TEST_ENTRY(ParseTooDeepArrayTest),
+ TEST_ENTRY(ComprehensiveInputTest),
+ TEST_ENTRY(ParseMapTest),
+ TEST_ENTRY_DISABLED(IndefiniteLengthArrayMapTest),
+ TEST_ENTRY(BasicEncodeTest),
+ TEST_ENTRY(NestedMapTest),
+ TEST_ENTRY(BignumParseTest),
+ TEST_ENTRY(OptTagParseTest),
+ TEST_ENTRY(DateParseTest),
+ TEST_ENTRY(ShortBufferParseTest2),
+ TEST_ENTRY(ShortBufferParseTest),
+ TEST_ENTRY(ParseDeepArrayTest),
+ TEST_ENTRY(SimpleArrayTest),
+ TEST_ENTRY(IntegerValuesParseTest),
+ TEST_ENTRY_DISABLED(MemPoolTest),
+ TEST_ENTRY_DISABLED(IndefiniteLengthStringTest),
+ TEST_ENTRY(HalfPrecisionDecodeBasicTests),
+ TEST_ENTRY(DoubleAsSmallestTest),
+ TEST_ENTRY(HalfPrecisionAgainstRFCCodeTest),
+ TEST_ENTRY(BstrWrapTest),
+ TEST_ENTRY(BstrWrapErrorTest),
+ TEST_ENTRY(BstrWrapNestTest),
+ TEST_ENTRY(CoseSign1TBSTest),
+ TEST_ENTRY(StringDecoderModeFailTest),
+ TEST_ENTRY_DISABLED(BigComprehensiveInputTest),
+ TEST_ENTRY(EncodeErrorTests),
+ //TEST_ENTRY(fail_test),
+};
+
+
+int RunTests(const char *szTestNames[], OutputStringCB pfOutput, void *poutCtx, int *pNumTestsRun)
+{
+ int nTestsFailed = 0;
+ int nTestsRun = 0;
+ UsefulBuf_MAKE_STACK_UB(StringStorage, 5);
+
+ test_entry2 *t2;
+ const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2);
+
+ for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
+ if(szTestNames[0]) {
+ // Some tests have been named
+ const char **szRequestedNames;
+ for(szRequestedNames = szTestNames; *szRequestedNames; szRequestedNames++) {
+ if(!strcmp(t2->szTestName, *szRequestedNames)) {
+ break; // Name matched
+ }
+ }
+ if(*szRequestedNames == NULL) {
+ // Didn't match this test
+ continue;
+ }
+ } else {
+ // no tests named, but don't run "disabled" tests
+ if(!t2->bEnabled) {
+ // Don't run disabled tests when all tests are being run
+ // as indicated by no specific test names being given
+ continue;
+ }
+ }
+
+ const char * szTestResult = (t2->test_fun)();
+ nTestsRun++;
+ if(pfOutput) {
+ (*pfOutput)(t2->szTestName, poutCtx, 0);
+ }
+
+ if(szTestResult) {
+ if(pfOutput) {
+ (*pfOutput)(" FAILED (returned ", poutCtx, 0);
+ (*pfOutput)(szTestResult, poutCtx, 0);
+ (*pfOutput)(")", poutCtx, 1);
+ }
+ nTestsFailed++;
+ } else {
+ if(pfOutput) {
+ (*pfOutput)( " PASSED", poutCtx, 1);
+ }
+ }
+ }
+
+
+ test_entry *t;
+ const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
+
+ for(t = s_tests; t < s_tests_end; t++) {
+ if(szTestNames[0]) {
+ // Some tests have been named
+ const char **szRequestedNames;
+ for(szRequestedNames = szTestNames; *szRequestedNames; szRequestedNames++) {
+ if(!strcmp(t->szTestName, *szRequestedNames)) {
+ break; // Name matched
+ }
+ }
+ if(*szRequestedNames == NULL) {
+ // Didn't match this test
+ continue;
+ }
+ } else {
+ // no tests named, but don't run "disabled" tests
+ if(!t->bEnabled) {
+ // Don't run disabled tests when all tests are being run
+ // as indicated by no specific test names being given
+ continue;
+ }
+ }
+
+ int nTestResult = (t->test_fun)();
+ nTestsRun++;
+ if(pfOutput) {
+ (*pfOutput)(t->szTestName, poutCtx, 0);
+ }
+
+ if(nTestResult) {
+ if(pfOutput) {
+ (*pfOutput)(" FAILED (returned ", poutCtx, 0);
+ (*pfOutput)(NumToString(nTestResult, StringStorage), poutCtx, 0);
+ (*pfOutput)(")", poutCtx, 1);
+ }
+ nTestsFailed++;
+ } else {
+ if(pfOutput) {
+ (*pfOutput)( " PASSED", poutCtx, 1);
+ }
+ }
+ }
+
+ if(pNumTestsRun) {
+ *pNumTestsRun = nTestsRun;
+ }
+
+ if(pfOutput) {
+ (*pfOutput)( "SUMMARY: ", poutCtx, 0);
+ (*pfOutput)( NumToString(nTestsRun, StringStorage), poutCtx, 0);
+ (*pfOutput)( " tests run; ", poutCtx, 0);
+ (*pfOutput)( NumToString(nTestsFailed, StringStorage), poutCtx, 0);
+ (*pfOutput)( " tests failed", poutCtx, 1);
+ }
+
+ return nTestsFailed;
+}
+
+
+
+
+static void PrintSize(const char *szWhat, uint32_t uSize, OutputStringCB pfOutput, void *pOutCtx)
+{
+ UsefulBuf_MAKE_STACK_UB(buffer, 20);
+
+ (*pfOutput)(szWhat, pOutCtx, 0);
+ (*pfOutput)(" ", pOutCtx, 0);
+ (*pfOutput)(NumToString(uSize, buffer), pOutCtx, 0);
+ (*pfOutput)("", pOutCtx, 1);
+}
+
+void PrintSizes(OutputStringCB pfOutput, void *pOutCtx)
+{
+ // Type and size of return from sizeof() varies. These will never be large so cast is safe
+ PrintSize("sizeof(QCBORTrackNesting)", (uint32_t)sizeof(QCBORTrackNesting), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBOREncodeContext)", (uint32_t)sizeof(QCBOREncodeContext), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBORDecodeNesting)", (uint32_t)sizeof(QCBORDecodeNesting), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBORDecodeContext)", (uint32_t)sizeof(QCBORDecodeContext), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBORItem)", (uint32_t)sizeof(QCBORItem), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBORStringAllocator)",(uint32_t)sizeof(QCBORStringAllocator), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBORTagListIn)", (uint32_t)sizeof(QCBORTagListIn), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBORTagListOut)", (uint32_t)sizeof(QCBORTagListOut), pfOutput, pOutCtx);
+ (*pfOutput)("", pOutCtx, 1);
+}
diff --git a/lib/ext/qcbor/test/run_tests.h b/lib/ext/qcbor/test/run_tests.h
new file mode 100644
index 0000000..734d4f8
--- /dev/null
+++ b/lib/ext/qcbor/test/run_tests.h
@@ -0,0 +1,66 @@
+/*==============================================================================
+ run_tests.h -- test aggregator and results reporting
+
+ Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+
+ SPDX-License-Identifier: BSD-3-Clause
+
+ See BSD-3-Clause license in README.md
+
+ Created 9/30/18
+ ==============================================================================*/
+
+/**
+ @file run_tests.h
+*/
+
+/**
+ @brief Type for function to output a text string
+
+ @param[in] szString The string to output
+ @param[in] pOutCtx A context pointer; NULL if not needed
+ @param[in] bNewline If non-zero, output a newline after the string
+
+ This is a prototype of a function to be passed to RunTests() to
+ output text strings.
+
+ This can be implemented with stdio (if available) using a straight
+ call to fputs() where the FILE * is passed as the pOutCtx as shown in
+ the example code below. This code is for Linux where the newline is
+ a \\n. Windows usually prefers \\r\\n.
+
+ @code
+ static void fputs_wrapper(const char *szString, void *pOutCtx, int bNewLine)
+ {
+ fputs(szString, (FILE *)pOutCtx);
+ if(bNewLine) {
+ fputs("\n", pOutCtx);
+ }
+ }
+ @endcode
+*/
+typedef void (*OutputStringCB)(const char *szString, void *pOutCtx, int bNewline);
+
+
+/**
+ @brief Runs the QCBOR tests.
+
+ @param[in] szTestNames An argv-style list of test names to run. If
+ empty, all are run.
+ @param[in] pfOutput Function that is called to output text strings.
+ @param[in] pOutCtx Context pointer passed to output function.
+ @param[out] pNumTestsRun Returns the number of tests run. May be NULL.
+
+ @return The number of tests that failed. Zero means overall success.
+ */
+int RunTests(const char *szTestNames[], OutputStringCB pfOutput, void *pOutCtx, int *pNumTestsRun);
+
+
+/**
+ @brief Print sizes of encoder / decoder contexts.
+
+ @param[in] pfOutput Function that is called to output text strings.
+ @param[in] pOutCtx Context pointer passed to output function.
+ */
+void PrintSizes(OutputStringCB pfOutput, void *pOutCtx);
+