Add tests from CAF exactly as they came from CAF. They are not yet being called and run
diff --git a/test/UsefulBuf_Tests.c b/test/UsefulBuf_Tests.c
new file mode 100644
index 0000000..84442f1
--- /dev/null
+++ b/test/UsefulBuf_Tests.c
@@ -0,0 +1,579 @@
+/*==============================================================================
+Copyright (c) 2016-2018, The Linux Foundation. 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 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"
+#include <string.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 * NonAdversarialUOBTest()
+{
+ const char *szReturn = NULL;
+
+ char outbuf[50];
+
+ UsefulOutBuf UOB;
+
+ UsefulOutBuf_Init(&UOB, outbuf, sizeof(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');
+
+
+ UsefulBuf U;
+ int nErr = UsefulOutBuf_OutUBuf(&UOB, &U);
+
+ const char *expected = "heffalump unbounce bluster hunny";
+
+ if(nErr || U.len-1 != strlen(expected) || strcmp(expected, U.ptr) || UsefulOutBuf_GetError(&UOB)) {
+ szReturn = "OutUBuf";
+ }
+
+ char buf[50];
+ size_t xxx;
+ nErr = UsefulOutBuf_CopyOut(&UOB, buf, sizeof(buf), &xxx);
+
+ if(nErr || xxx-1 != strlen(expected) || strcmp(expected, buf)) {
+ 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 *BoundaryConditionsTest()
+{
+ char outbuf[2];
+
+ UsefulOutBuf UOB;
+
+ UsefulOutBuf_Init(&UOB, outbuf, sizeof(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";
+
+
+ char outBuf2[10];
+
+ UsefulOutBuf_Init(&UOB, outBuf2, sizeof(outBuf2));
+
+ UsefulOutBuf_Reset(&UOB);
+ // put data in the buffer
+ UsefulOutBuf_AppendData(&UOB, "abc123", 6);
+
+ UsefulOutBuf_InsertData(&UOB, "xyz*&^", 6, 0);
+
+ if(!UsefulOutBuf_GetError(&UOB)) {
+ return "insert with data should have failed";
+ }
+
+
+ UsefulOutBuf_Init(&UOB, 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, 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, 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()
+{
+ char outbuf[10];
+
+ UsefulOutBuf UOB;
+
+ // First -- make sure that the room left function returns the right amount
+ UsefulOutBuf_Init(&UOB, outbuf, sizeof(outbuf));
+
+ if(UsefulOutBuf_RoomLeft(&UOB) != sizeof(outbuf))
+ return "room left failed";
+
+
+ // 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, sizeof(outbuf));
+
+ UOB.UB.len = UOB.size+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 = SZToUsefulBufC(szFoo);
+ if(Foo.len != 3 || strncmp(Foo.ptr, szFoo, 3))
+ return "SZToUsefulBufC failed";
+
+ UsefulBufC Too = SZLiteralToUsefulBufC("Toooo");
+ if(Too.len != 5 || strncmp(Too.ptr, "Toooo", 5))
+ return "SZLiteralToUsefulBufC failed";
+
+ uint8_t pB[] = {0x42, 0x6f, 0x6f};
+ UsefulBufC Boo = ByteArrayLiteralToUsefulBufC(pB);
+ if(Boo.len != 3 || strncmp(Boo.ptr, "Boo", 3))
+ return "ByteArrayLiteralToUsefulBufC failed";
+
+ char *sz = "not const"; // some data for the test
+ UsefulBuf B = (UsefulBuf){sz, sizeof(sz)};
+ UsefulBufC BC = UsefulBufConst(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";
+ }
+
+ UsefulBufC UBC = UsefulBuf_Const(UB);
+
+ if(!UsefulBuf_IsNULL(UBC)){
+ return "IsNull const failed";
+ }
+
+ if(!UsefulBuf_IsEmpty(UBC)){
+ return "IsEmpty failed";
+ }
+
+ if(!UsefulBuf_IsNULLOrEmpty(UBC)){
+ return "IsNULLOrEmpty failed";
+ }
+
+ UB.ptr = "x"; // just some valid pointer
+
+ if(UsefulBuf_IsNULL(UB)){
+ return "IsNull failed";
+ }
+
+ if(!UsefulBuf_IsEmpty(UBC)){
+ return "IsEmpty failed";
+ }
+
+ // test the Unconst.
+ if(UsefulBufC_Unconst(UBC).ptr != NULL) {
+ return "Unconst failed";
+ }
+
+ // Set 100 bytes of '+'; validated a few tests later
+ MakeUsefulBufOnStack(Temp, 100);
+ UsefulBuf_Set(&Temp, '+');
+
+ // Try to copy into a buf that is too small and see failure
+ MakeUsefulBufOnStack(Temp2, 99);
+ if(!UsefulBuf_Copy(&Temp2, UsefulBuf_Const(Temp))) {
+ return "Copy should have failed";
+ }
+
+ // Try to copy into a NULL/empty buf and see failure
+ UsefulBuf UBNull = NULLUsefulBuf;
+ if(!UsefulBuf_Copy(&UBNull, UsefulBuf_Const(Temp))) {
+ 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
+ MakeUsefulBufOnStack(Temp3, 101);
+ if(UsefulBuf_Copy(&Temp3, UsefulBuf_Const(Temp))) {
+ return "Copy should not have failed";
+ }
+
+ static const uint8_t pExpected[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ };
+ UsefulBufC Expected = ByteArrayLiteralToUsefulBufC(pExpected);
+ // This validates comparison for equality and the UsefulBuf_Set
+ if(UsefulBuf_Compare(Expected, UsefulBuf_Const(Temp))) {
+ 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[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', ',',
+ };
+ UsefulBufC ExpectedBigger = ByteArrayLiteralToUsefulBufC(pExpectedBigger);
+
+ // Expect -1 with the first arg is smaller
+ if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){
+ return "Compare with bigger";
+ }
+
+
+ static const uint8_t pExpectedSmaller[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '*',
+ };
+ UsefulBufC ExpectedSmaller = ByteArrayLiteralToUsefulBufC(pExpectedSmaller);
+ // Expect +1 with the first arg is larger
+ if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){
+ return "Compare with smaller";
+ }
+
+
+ static const uint8_t pExpectedLonger[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
+ };
+ UsefulBufC ExpectedLonger = ByteArrayLiteralToUsefulBufC(pExpectedLonger);
+
+ // Expect -1 with the first arg is smaller
+ if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){
+ return "Compare with longer";
+ }
+
+
+ static const uint8_t pExpectedShorter[] = {
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ '+', '+', '+', '+', '+', '+', '+', '+', '+',
+ };
+ UsefulBufC ExpectedShorter = ByteArrayLiteralToUsefulBufC(pExpectedShorter);
+ // Expect +1 with the first arg is larger
+ if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){
+ return "Compare with shorter";
+ }
+
+
+ if(UsefulBuf_Copy(&Temp, NULLUsefulBufC)) {
+ return "Copy null/empty failed";
+ }
+
+ if(UsefulBuf_Compare(UsefulBuf_Const(Temp), NULLUsefulBufC)) {
+ return "Copy Null 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[] = {'+', '+', '*'};
+ UsefulBufC ToBeFound = ByteArrayLiteralToUsefulBufC(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 * UBIntegerFormatTests()
+{
+ char outbuf[100];
+
+ UsefulOutBuf UOB;
+
+ 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_Init(&UOB, outbuf, sizeof(outbuf));
+
+ UsefulOutBuf_AppendUint32(&UOB, u32);
+ UsefulOutBuf_AppendUint64(&UOB, u64);
+ UsefulOutBuf_AppendUint16(&UOB, u16);
+ UsefulOutBuf_AppendByte(&UOB, u8);
+ UsefulOutBuf_AppendFloat(&UOB, f);
+ UsefulOutBuf_AppendDouble(&UOB, d);
+
+ UsefulBuf O;
+ if(UsefulOutBuf_OutUBuf(&UOB, &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, UsefulBuf_Const(O));
+
+ 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";
+ }
+
+
+ return NULL;
+}
+
+
+
diff --git a/test/UsefulBuf_Tests.h b/test/UsefulBuf_Tests.h
new file mode 100644
index 0000000..e80be69
--- /dev/null
+++ b/test/UsefulBuf_Tests.h
@@ -0,0 +1,46 @@
+/*==============================================================================
+Copyright (c) 2016-2018, The Linux Foundation. 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 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 * NonAdversarialUOBTest();
+
+const char * TestBasicSanity();
+
+const char * BoundaryConditionsTest();
+
+const char * UBMacroConversionsTest();
+
+const char * UBUtilTests();
+
+const char * UBIntegerFormatTests();
+
+
+#endif
diff --git a/test/mempool_test.c b/test/mempool_test.c
new file mode 100644
index 0000000..3eec5e8
--- /dev/null
+++ b/test/mempool_test.c
@@ -0,0 +1,9 @@
+//
+// mempool_test.c
+// QCBOR
+//
+// Created by Laurence Lundblade on 10/8/18.
+// Copyright © 2018 Laurence Lundblade. All rights reserved.
+//
+
+#include "mempool_test.h"
diff --git a/test/mempool_test.h b/test/mempool_test.h
new file mode 100644
index 0000000..02e1cdc
--- /dev/null
+++ b/test/mempool_test.h
@@ -0,0 +1,14 @@
+//
+// mempool_test.h
+// QCBOR
+//
+// Created by Laurence Lundblade on 10/8/18.
+// Copyright © 2018 Laurence Lundblade. All rights reserved.
+//
+
+#ifndef mempool_test_h
+#define mempool_test_h
+
+#include <stdio.h>
+
+#endif /* mempool_test_h */
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
new file mode 100644
index 0000000..2328912
--- /dev/null
+++ b/test/qcbor_decode_tests.c
@@ -0,0 +1,1534 @@
+/*==============================================================================
+Copyright (c) 2016-2018, The Linux Foundation. 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 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_decode_tests.h"
+#include <stdio.h>
+#include <strings.h>
+#include <float.h>
+#include <math.h>
+#include <stdlib.h>
+
+
+// TODO: test other than the normal decoder mode
+
+static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
+{
+ if(szLabel) {
+ printf("%s ", szLabel);
+ }
+
+ int i;
+ for(i = 0; i < nLen; i++) {
+ uint8_t Z = pEncoded[i];
+ printf("%02x ", Z);
+ }
+ printf("\n");
+
+ fflush(stdout);
+}
+
+
+// TODO: -- add a test for counting the top level items and adding it back in with AddRaw()
+
+
+static const uint8_t pExpectedEncodedInts[] = {
+ 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.uint64 != -9223372036854775807LL - 1)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.uint64 != -4294967297)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.uint64 != -4294967296)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.uint64 != -4294967295)
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.uint64 != -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.
+
+ Todo: test values 254, 255 and 256
+
+ */
+
+int IntegerValuesParseTest()
+{
+ int n;
+ QCBORDecodeContext DCtx;
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){pExpectedEncodedInts, sizeof(pExpectedEncodedInts)}, 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 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
+ // calucate the length so buffer can be allocated correctly,
+ // and last with the buffer to do the actual encoding
+ do {
+ QCBOREncode_Init(&ECtx, *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_Finish(&ECtx, pEncodedLen))
+ goto Done;
+
+ if(*pEncoded != NULL) {
+ nReturn = 0;
+ goto Done;
+ }
+ *pEncoded = malloc(*pEncodedLen);
+ if(*pEncoded == NULL) {
+ nReturn = -1;
+ goto Done;
+ }
+
+ } 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, i2;
+ size_t i3, i4;
+ const uint8_t *s3, *s4;
+
+
+ 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 ||
+ bcmp("galactic", s3, 8) !=0 ||
+ bcmp("haven token", s4, 11) !=0) {
+ printf("SimpleArraryTest Failed\n");
+ return(-1);
+ }
+
+ return(0);
+}
+
+
+static uint8_t s_pDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
+
+int ParseDeepArrayTest()
+{
+ QCBORDecodeContext DCtx;
+ int nReturn = 0;
+ int i;
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){s_pDeepArrays, sizeof(s_pDeepArrays)}, 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);
+}
+
+
+
+static uint8_t s_pTooDeepArrays[] = {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, (UsefulBufC){s_pTooDeepArrays, sizeof(s_pTooDeepArrays)}, QCBOR_DECODE_MODE_NORMAL);
+
+ for(i = 0; i < 10; 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);
+}
+
+
+
+
+static uint8_t s_indefiniteLenString[] = { 0x7f, 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x67, 0xff};
+
+
+int UnsupportedCBORDecodeTest()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){s_indefiniteLenString, sizeof(s_indefiniteLenString)}, QCBOR_DECODE_MODE_NORMAL);
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_UNSUPPORTED)
+ return -1;
+
+ return 0;
+}
+
+
+
+int ShortBufferParseTest()
+{
+ int nResult = 0;
+ QCBORDecodeContext DCtx;
+ int num;
+
+ for(num = sizeof(pExpectedEncodedInts)-1; num; num--) {
+ int n;
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){pExpectedEncodedInts, 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);
+ }
+
+ //printencoded(pEncoded, nEncodedLen);
+
+ for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
+ int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
+ if(nResult == 0) {
+ nReturn = -1;
+ }
+ }
+
+ return(nReturn);
+}
+
+
+
+static int ParseMapTest1()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
+
+ 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.label.string.len != 13 ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 42 ||
+ memcmp(Item.label.string.ptr, "first integer", 13))
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 23 ||
+ memcmp(Item.label.string.ptr, "an array of two strings", 23) ||
+ 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.val.string.len != 7 ||
+ memcmp(Item.val.string.ptr, "string1", 7))
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.val.string.len != 7 ||
+ memcmp(Item.val.string.ptr, "string2", 7))
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 12 ||
+ memcmp(Item.label.string.ptr, "map in a map", 12) ||
+ 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 ||
+ Item.label.string.len != 7 ||
+ memcmp(Item.label.string.ptr, "bytes 1", 7)||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ Item.val.string.len != 4 ||
+ memcmp(Item.val.string.ptr, "xxxx", 4))
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 7 ||
+ memcmp(Item.label.string.ptr, "bytes 2", 7) ||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ Item.val.string.len != 4 ||
+ memcmp(Item.val.string.ptr, "yyyy", 4))
+ return -1;
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 11 ||
+ memcmp(Item.label.string.ptr, "another int", 11) ||
+ 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 ||
+ Item.label.string.len != 6 ||
+ memcmp(Item.label.string.ptr, "text 2", 6)||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.val.string.len != 30 ||
+ memcmp(Item.val.string.ptr, "lies, damn lies and statistics", 30))
+ return -1;
+
+ return 0;
+}
+
+
+
+/*
+ This test parses pValidMapEncoded and checks for extra bytes along the way
+ */
+static int ExtraBytesTest()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+
+ QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ Item.val.uCount != 3)
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 13 ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.uCount != 42 ||
+ memcmp(Item.label.string.ptr, "first integer", 13))
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 23 ||
+ memcmp(Item.label.string.ptr, "an array of two strings", 23) ||
+ Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.val.uCount != 2)
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.val.string.len != 7 ||
+ memcmp(Item.val.string.ptr, "string1", 7))
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.val.string.len != 7 ||
+ memcmp(Item.val.string.ptr, "string2", 7))
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 12 ||
+ memcmp(Item.label.string.ptr, "map in a map", 12) ||
+ Item.uDataType != QCBOR_TYPE_MAP ||
+ Item.val.uCount != 4)
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 7 ||
+ memcmp(Item.label.string.ptr, "bytes 1", 7)||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ Item.val.string.len != 4 ||
+ memcmp(Item.val.string.ptr, "xxxx", 4))
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 7 ||
+ memcmp(Item.label.string.ptr, "bytes 2", 7) ||
+ Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
+ Item.val.string.len != 4 ||
+ memcmp(Item.val.string.ptr, "yyyy", 4))
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 11 ||
+ memcmp(Item.label.string.ptr, "another int", 11) ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.val.int64 != 98)
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return nCBORError;
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.label.string.len != 6 ||
+ memcmp(Item.label.string.ptr, "text 2", 6)||
+ Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
+ Item.val.string.len != 30 ||
+ memcmp(Item.val.string.ptr, "lies, damn lies and statistics", 30))
+ return -1;
+
+ if(QCBORDecode_Finish(&DCtx) == QCBOR_ERR_EXTRA_BYTES) {
+ return -1;
+ }
+
+ return 0;
+}
+
+
+
+
+int ParseMapTest()
+{
+ int n = ParseMapTest1();
+
+ if(!n) {
+ n = ExtraBytesTest();
+ }
+
+ return(n);
+}
+
+
+static uint8_t s_pSimpleValues[] = {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, ByteArrayLiteralToUsefulBufC(s_pSimpleValues), 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_UNSUPPORTED)
+ 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
+
+};
+
+
+void Dump(UsefulBufC Input, int x)
+{
+ char label[10];
+
+ sprintf(label, "%d", x);
+
+ printencoded(label, Input.ptr, Input.len);
+}
+
+
+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;
+ // Dump(pF->Input, nCBORError);
+ break;
+ }
+ }
+ }
+
+ {
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ QCBORDecode_Init(&DCtx, ByteArrayLiteralToUsefulBufC(s_pSimpleValues), 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;
+}
+
+
+
+
+static void Recurser(uint8_t *pBuf, int nLen, int nLenMax)
+{
+
+ if(nLen >= nLenMax) {
+ return;
+ }
+
+ //printf("__%d__%d__\n", nLen, nLenMax);
+
+ for(int i = 0; i < 256; i++) {
+ pBuf[nLen] = i;
+
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ UsefulBufC Input = {pBuf, nLen+1};
+
+ QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
+
+ while(1) {
+ nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
+ if(QCBOR_ERR_HIT_END == nCBORError) {
+ break;
+ }
+ if(nCBORError != QCBOR_SUCCESS) {
+ if(nCBORError != QCBOR_ERR_UNSUPPORTED && nCBORError != QCBOR_ERR_HIT_END && nCBORError != QCBOR_ERR_INVALID_CBOR) {
+ //Dump(Input, nCBORError);
+ }
+ break;
+ }
+ }
+ //Dump(Input, -1);
+
+
+ Recurser(pBuf, nLen+1, nLenMax);
+ }
+}
+
+
+/*
+ Runs all possible input strings of a given length. This is set to 3 to make the test
+ run in reasonable time.
+ Main point of this test is to not crash.
+ */
+
+int ComprehensiveInputTest()
+{
+ uint8_t pBuf[3]; // 3 keeps it running in reasonable time. 4 takes tens of minutes.
+
+ Recurser(pBuf, 0, sizeof(pBuf));
+
+ return 0;
+}
+
+
+static uint8_t s_DateTestInput[] = {
+ 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
+
+ 0xc1, 0xcf, 0xd8, 0xee, // Epoch date with extra tags
+ 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, ByteArrayLiteralToUsefulBufC(s_DateTestInput), QCBOR_DECODE_MODE_NORMAL);
+
+ // String date
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
+ UsefulBuf_Compare(Item.val.dateString, SZLiteralToUsefulBufC("1985-04-12"))){
+ return -1;
+ }
+
+ // Epoch date
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
+ Item.val.epochDate.nSeconds != 1400000000 ||
+ Item.val.epochDate.fSecondsFraction != 0 ) {
+ return -1;
+ }
+
+ // Epoch date with extra tags
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
+ Item.val.epochDate.nSeconds != 1400000001 ||
+ Item.val.epochDate.fSecondsFraction != 0 ||
+ Item.uTagBits != (0x02 | (0x01 << 0x0f)) ||
+ Item.uTag != 0xee) {
+ return -1;
+ }
+
+ // Epoch date that is too large for our representation
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
+ return -1;
+ }
+
+ // Epoch date in float format with fractional seconds
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
+ Item.val.epochDate.nSeconds != 1 ||
+ CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
+ return -1;
+ }
+
+ // Epoch date float that is too large for our representation
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
+ return -1;
+ }
+
+ // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
+
+ return 0;
+}
+
+
+static uint8_t s_OptTestInput[] = {
+ 0xd9, 0xd9, 0xf7, // CBOR magic number
+ 0x81,
+ 0xd8, 62, // 62 is decimal intentionally
+ 0x00};
+
+int OptTagParseTest()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+
+ QCBORDecode_Init(&DCtx, ByteArrayLiteralToUsefulBufC(s_OptTestInput), QCBOR_DECODE_MODE_NORMAL);
+
+ //
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_ARRAY ||
+ Item.uTagBits != QCBOR_TAGFLAG_CBOR_MAGIC) {
+ return -1;
+ }
+
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_INT64 ||
+ Item.uTagBits != (0x01LL << 62) ||
+ Item.val.int64 != 0)
+ return -1;
+
+ return 0;
+}
+
+
+
+
+static uint8_t s_BigNumInput[] = {
+ 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 sBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+
+int BignumParseTest()
+{
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ int nCBORError;
+
+ QCBORDecode_Init(&DCtx, ByteArrayLiteralToUsefulBufC(s_BigNumInput), 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, ByteArrayLiteralToUsefulBufC(sBigNum))){
+ return -1;
+ }
+
+ //
+ if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
+ return -1;
+ if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
+ UsefulBuf_Compare(Item.val.bigNum, ByteArrayLiteralToUsefulBufC(sBigNum))){
+ 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, ByteArrayLiteralToUsefulBufC(sBigNum))){
+ 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, ByteArrayLiteralToUsefulBufC(sBigNum))){
+ 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, ByteArrayLiteralToUsefulBufC(sBigNum))){
+ 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, ByteArrayLiteralToUsefulBufC(sBigNum))){
+ return -1;
+ }
+
+ return 0;
+}
+
+
+
+static int CheckItemWithIntLabel(QCBORDecodeContext *pCtx, uint8_t uDataType, uint8_t uNestingLevel, 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 != nLabel) return -1;
+ }
+ }
+ if(Item.uNestingLevel != uNestingLevel) return -1;
+
+ if(pItem) {
+ *pItem = Item;
+ }
+ 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 s_CSRInput[] = {
+ 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, ByteArrayLiteralToUsefulBufC(s_CSRInput), QCBOR_DECODE_MODE_NORMAL);
+
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_MAP, 0, 0, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_MAP, 1, -23, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_MAP, 2, -20, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_TEXT_STRING, 3, -18, NULL)) return -1;
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_TEXT_STRING, 3, -17, NULL)) return -1;
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_TEXT_STRING, 3, -15, NULL)) return -1;
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_TEXT_STRING, 3, -16, NULL)) return -1;
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_TEXT_STRING, 3, -14, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_MAP, 2, -19, NULL)) return -1;
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_MAP, 3, -11, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_INT64, 4, -9, NULL)) return -1;
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_BYTE_STRING, 3, -10, NULL)) return -1;
+
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_MAP, 1, -22, NULL)) return -1;
+ if(CheckItemWithIntLabel(&DCtx, QCBOR_TYPE_INT64, 2, -5, NULL)) return -1;
+
+ return 0;
+}
+
+
+
+
+
+
+
diff --git a/test/qcbor_decode_tests.h b/test/qcbor_decode_tests.h
new file mode 100644
index 0000000..759406f
--- /dev/null
+++ b/test/qcbor_decode_tests.h
@@ -0,0 +1,162 @@
+/*==============================================================================
+Copyright (c) 2016-2018, The Linux Foundation. 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 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();
+
+
+
+
+
+/*
+ Decode a simple CBOR encoded array and make sure it returns all the correct values.
+ This is a decode test.
+ */
+int SimpleArrayTest();
+
+
+/*
+ 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();
+
+
+/*
+ See that the correct error is reported when parsing
+ an array of depth 11, one too large.
+ */
+int ParseTooDeepArrayTest();
+
+
+/*
+ Try to parse some legit CBOR types that this parsers
+ doesn't support.
+ */
+int UnsupportedCBORDecodeTest();
+
+
+/*
+ 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();
+
+
+/*
+ Same as ShortBufferParseTest, but with a different encoded CBOR input.
+ It is another hostile input test
+ */
+int ShortBufferParseTest2();
+
+
+/*
+ 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();
+
+
+
+int FloatValuesTest1();
+
+
+
+int SimpleValuesTest1();
+
+
+
+int ParseSimpleTest();
+
+
+
+/*
+ Tests a number of failure cases on bad CBOR to get the right error code
+ */
+int FailureTests();
+
+
+/*
+ Generate all possible input strings up to length x and tries to parse them completely
+ */
+int ComprehensiveInputTest();
+
+
+/*
+ Thest the date types -- epoch and strings
+ */
+int DateParseTest();
+
+
+/*
+ Test optional tags like the CBOR magic number.
+ */
+int OptTagParseTest();
+
+
+/*
+ Parse some big numbers, positive and negative
+ */
+int BignumParseTest();
+
+
+/*
+ Parse some nested maps
+ */
+int NestedMapTest();
+
+
+
+#endif /* defined(__QCBOR__qcbort_decode_tests__) */
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
new file mode 100644
index 0000000..a486614
--- /dev/null
+++ b/test/qcbor_encode_tests.c
@@ -0,0 +1,987 @@
+/*==============================================================================
+Copyright (c) 2016-2018, The Linux Foundation. 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 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"
+#include <stdio.h>
+#include <strings.h>
+#include <float.h>
+#include <math.h>
+#include <stdlib.h>
+
+
+
+// TODO: -- test on a 32-bit machine)
+
+// TODO: test QCBOR_MAX_ITEMS_IN_ARRAY (this is very large...)
+
+//
+
+#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
+// ifdef these out to not have compiler warnings
+static void printencoded(const uint8_t *pEncoded, size_t nLen)
+{
+ int i;
+ for(i = 0; i < nLen; i++) {
+ uint8_t Z = pEncoded[i];
+ printf("%02x ", Z);
+ }
+ printf("\n");
+
+ fflush(stdout);
+}
+
+
+static void printencodedE(EncodedCBOR E)
+{
+ printf("Num Items: %d\n", E.uItems);
+ printencoded(E.Bytes.ptr, E.Bytes.len);
+}
+#endif
+
+
+#define CheckResults(Enc, Expected) \
+ UsefulBuf_Compare(UsefulBufConst(Enc.Bytes), (UsefulBufC){Expected, sizeof(Expected)})
+
+
+
+static const uint8_t pExpectedEncodedAll[] = {
+
+ 0x98, 0x29, 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, 0x65, 0x4a, 0x61, 0x69, 0x6d, 0x65, 0xd8, 0x58, 0xfa, 0x40, 0x49, 0x0f, 0xd0, 0x66, 0x53, 0x74, 0x72, 0x65, 0x65, 0x74, 0xd8, 0x63, 0xfb, 0x40, 0x21, 0x4f, 0x01, 0x96, 0xd8, 0xf4, 0xf9, 0xfa, 0x3f, 0x80, 0x00, 0x00, 0xfb, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x63, 0x66, 0x6f, 0x6f, 0xfa, 0x45, 0x98, 0xb8, 0x00, 0x39, 0x03, 0xe6, 0xfa, 0x44, 0x79, 0xc0, 0x00, 0x66, 0x66, 0x6f, 0x6f, 0x66, 0x6f, 0x6f, 0xfb, 0x41, 0x58, 0xf7, 0x7d, 0xc0, 0x00, 0x00, 0x00, 0x39, 0xaf, 0xc6, 0xfb, 0x40, 0xf5, 0xba, 0x70, 0x00, 0x00, 0x00, 0x00, 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()
+{
+ QCBOREncodeContext ECtx;
+ int nReturn = 0;
+
+ uint8_t pEncoded[3000];
+ size_t nEncodedLen = sizeof(pEncoded);
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+
+ QCBOREncode_OpenArray(&ECtx);
+
+ // Non-map ints
+ QCBOREncode_AddUInt64_3(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 100, 89989909);
+ QCBOREncode_AddInt64_3(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 76, 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);
+
+ // floats and doubles
+ QCBOREncode_AddFloat_3(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 88, 3.14159);
+ QCBOREncode_AddDouble_3(&ECtx, "Street", QCBOR_NO_INT_LABEL, 99, 8.654309);
+ QCBOREncode_AddFloat(&ECtx, 1);
+ QCBOREncode_AddDouble(&ECtx, 1);
+
+ // floats and doubles that go in map
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddFloatToMap(&ECtx, "foo", 4887);
+ QCBOREncode_AddFloatToMapN(&ECtx, -999, 999);
+ QCBOREncode_AddDoubleToMap(&ECtx, "foofoo", 6544887);
+ QCBOREncode_AddDoubleToMapN(&ECtx, -44999, 88999);
+ QCBOREncode_CloseMap(&ECtx);
+
+ // Epoch Date
+ QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
+
+ // Epoch date with labels
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddDateEpoch_2(&ECtx, "LongLiveDenisRitchie", QCBOR_NO_INT_LABEL, 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_AddBytes_3(&ECtx, "binbin", QCBOR_NO_INT_LABEL, 100000, ((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, SZLiteralToUsefulBufC("bar bar foo bar"));
+ QCBOREncode_AddSZString(&ECtx, "oof\n");
+ QCBOREncode_AddURI(&ECtx, SZLiteralToUsefulBufC("http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8"));
+ QCBOREncode_AddB64Text(&ECtx, SZLiteralToUsefulBufC("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
+ QCBOREncode_AddRegex(&ECtx, SZLiteralToUsefulBufC("[^abc]+"));
+ QCBOREncode_AddMIMEData(&ECtx, SZToUsefulBufC(szMIME));
+
+ // text blobs in maps
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddTextToMap(&ECtx, "#####", SZLiteralToUsefulBufC("foo bar foo foo"));
+ QCBOREncode_AddText_3(&ECtx, "____", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, SZLiteralToUsefulBufC("foo bar"));
+ QCBOREncode_AddSZString_3(&ECtx, "()()()", QCBOR_NO_INT_LABEL, 1000, "rab rab oof");
+ QCBOREncode_AddTextToMapN(&ECtx,22, SZLiteralToUsefulBufC("foo foo foo foo"));
+ QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
+ QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
+ QCBOREncode_AddURIToMap(&ECtx, "RFC", SZLiteralToUsefulBufC("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
+ QCBOREncode_AddURIToMapN(&ECtx, 0x89, SZLiteralToUsefulBufC("http://cbor.me/"));
+ QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", SZLiteralToUsefulBufC("cGxlYXN1cmUu"));
+ QCBOREncode_AddB64TextToMapN(&ECtx, 64, SZLiteralToUsefulBufC("c3VyZS4="));
+ QCBOREncode_AddRegexToMap(&ECtx, "popo", SZLiteralToUsefulBufC("100\\s*mk")); // x code string literal bug
+ QCBOREncode_AddRegexToMapN(&ECtx, -51, SZLiteralToUsefulBufC("perl\\B")); // x code string literal bug
+ QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", SZToUsefulBufC(szMIME));
+ QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, SZToUsefulBufC(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_AddSimple_3(&ECtx, "dare", QCBOR_NO_INT_LABEL, 66, CBOR_SIMPLEV_TRUE);
+ QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_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_OpenArray_3(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL, 1093, 0);
+ 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_OpenMap_3(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL, 9087, 0);
+ QCBOREncode_CloseMap(&ECtx);
+ QCBOREncode_CloseMap(&ECtx);
+ QCBOREncode_CloseMap(&ECtx);
+ QCBOREncode_CloseMap(&ECtx);
+
+ // Extended simple values (these are not standard...)
+ QCBOREncode_OpenMap(&ECtx);
+ QCBOREncode_AddRawSimple_3(&ECtx, "s1", QCBOR_NO_INT_LABEL, 88, 255);
+ QCBOREncode_AddRawSimple_3(&ECtx, "s2", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 0);
+ QCBOREncode_AddRawSimple_3(&ECtx, "s3", QCBOR_NO_INT_LABEL, 88, 33);
+ QCBOREncode_AddRawSimple_3(&ECtx, NULL, 88378374, 88, 255);
+ QCBOREncode_AddRawSimple_3(&ECtx, NULL, 89, 88, 19);
+ QCBOREncode_CloseMap(&ECtx);
+
+
+ // UUIDs
+ static uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
+ UsefulBufC XXUUID = ByteArrayLiteralToUsefulBufC(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 uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ UsefulBufC BIGNUM = ByteArrayLiteralToUsefulBufC(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);
+
+ EncodedCBOR Enc;
+
+ if(QCBOREncode_Finish2(&ECtx, &Enc)) {
+ nReturn = -1;
+ goto Done;
+ }
+
+ //printencodedE(Enc);
+
+ if(CheckResults(Enc,pExpectedEncodedAll))
+ nReturn = -1;
+
+Done:
+ return nReturn;
+}
+
+// todo -- add a test for counting the top level items and adding it back in with AddRaw()
+
+
+static const uint8_t pExpectedEncodedInts[] = {
+ 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;
+
+ uint8_t pEncoded[1000];
+ size_t nEncodedLen = sizeof(pEncoded);
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+ 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);
+
+ EncodedCBOR Enc;
+ if(QCBOREncode_Finish2(&ECtx, &Enc)) {
+ nReturn = -1;
+ }
+
+ if(CheckResults(Enc, pExpectedEncodedInts))
+ return -1;
+
+ if(Enc.Bytes.len != sizeof(pExpectedEncodedInts) || bcmp(pEncoded, pExpectedEncodedInts, Enc.Bytes.len))
+ nReturn = -1;
+
+ //printencoded(pEncoded, nEncodedLen);
+
+ return(nReturn);
+}
+
+
+
+static uint8_t pExpectedEncodedSimple[] = {
+ 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
+
+
+int SimpleValuesTest1()
+{
+ QCBOREncodeContext ECtx;
+ int nReturn = 0;
+
+ uint8_t pEncoded[100];
+ size_t nEncodedLen = sizeof(pEncoded);
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+ 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);
+
+ EncodedCBOR ECBOR;
+ if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
+ nReturn = -1;
+ }
+
+ if(ECBOR.Bytes.len != sizeof(pExpectedEncodedSimple) || bcmp(pEncoded, pExpectedEncodedSimple, ECBOR.Bytes.len))
+ nReturn = -1;
+
+ // printencoded(pEncoded, nEncodedLen);
+
+ return(nReturn);
+}
+
+
+static uint8_t pExpectedEncodedFloat[] = {
+ 0x98, 0x1e, 0xfa, 0x00, 0x00, 0x00, 0x00, 0xfa,
+ 0x3f, 0x80, 0x00, 0x00, 0xfa, 0x3f, 0x8c, 0xcc,
+ 0xcd, 0xfa, 0x3f, 0xc0, 0x00, 0x00, 0xfa, 0x47,
+ 0x7f, 0xe0, 0x00, 0xfa, 0x47, 0xc3, 0x50, 0x00,
+ 0xfa, 0x7f, 0x7f, 0xff, 0xff, 0xfa, 0x7f, 0x80,
+ 0x00, 0x00, 0xfa, 0x33, 0x80, 0x00, 0x00, 0xfa,
+ 0x38, 0x80, 0x00, 0x00, 0xfa, 0xc0, 0x80, 0x00,
+ 0x00, 0xfa, 0xc0, 0x83, 0x33, 0x33, 0xfa, 0x7f,
+ 0xc0, 0x00, 0x00, 0xfa, 0x7f, 0x80, 0x00, 0x00,
+ 0xfa, 0xff, 0x80, 0x00, 0x00, 0xfb, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb, 0x3f,
+ 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb,
+ 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a,
+ 0xfb, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0xfb, 0x40, 0xef, 0xfc, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0xfb, 0x40, 0xf8, 0x6a, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0xfb, 0x47, 0xef, 0xff, 0xff,
+ 0xe0, 0x00, 0x00, 0x00, 0xfb, 0x7e, 0x37, 0xe4,
+ 0x3c, 0x88, 0x00, 0x75, 0x9c, 0xfb, 0x3e, 0x70,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb, 0x3f,
+ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb,
+ 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xfb, 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66,
+ 0x66, 0xfb, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0xfb, 0xff, 0xf0, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00};
+
+
+int FloatValuesTest1()
+{
+ QCBOREncodeContext ECtx;
+ int nReturn = 0;
+
+ uint8_t pEncoded[1000];
+ size_t nEncodedLen = sizeof(pEncoded);
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+ QCBOREncode_OpenArray(&ECtx);
+
+ // These are all samples published
+ // in RFC 7049.
+ QCBOREncode_AddFloat(&ECtx, 0.0);
+ QCBOREncode_AddFloat(&ECtx, 1.0);
+ QCBOREncode_AddFloat(&ECtx, 1.1); // appx
+ QCBOREncode_AddFloat(&ECtx, 1.5);
+ QCBOREncode_AddFloat(&ECtx, 65504.0);
+ QCBOREncode_AddFloat(&ECtx, 100000.0);
+ QCBOREncode_AddFloat(&ECtx, 3.4028234663852886e+38);
+ QCBOREncode_AddFloat(&ECtx, 1.0e+300); // Infinity?
+ QCBOREncode_AddFloat(&ECtx, 5.960464477539063e-8);
+ QCBOREncode_AddFloat(&ECtx, 0.00006103515625);
+ QCBOREncode_AddFloat(&ECtx, -4.0);
+ QCBOREncode_AddFloat(&ECtx, -4.1); // appx
+
+ QCBOREncode_AddFloat(&ECtx, NAN);
+ QCBOREncode_AddFloat(&ECtx, INFINITY);
+ QCBOREncode_AddFloat(&ECtx, -INFINITY);
+
+
+ QCBOREncode_AddDouble(&ECtx, 0.0);
+ QCBOREncode_AddDouble(&ECtx, 1.0);
+ QCBOREncode_AddDouble(&ECtx, 1.1); // appx
+ QCBOREncode_AddDouble(&ECtx, 1.5);
+ QCBOREncode_AddDouble(&ECtx, 65504.0);
+ QCBOREncode_AddDouble(&ECtx, 100000.0);
+ QCBOREncode_AddDouble(&ECtx, 3.4028234663852886e+38);
+ QCBOREncode_AddDouble(&ECtx, 1.0e+300); // Infinity?
+ QCBOREncode_AddDouble(&ECtx, 5.960464477539063e-8);
+ QCBOREncode_AddDouble(&ECtx, 0.00006103515625);
+ QCBOREncode_AddDouble(&ECtx, -4.0);
+ QCBOREncode_AddDouble(&ECtx, -4.1); // appx
+
+ QCBOREncode_AddDouble(&ECtx, NAN);
+ QCBOREncode_AddDouble(&ECtx, INFINITY);
+ QCBOREncode_AddDouble(&ECtx, -INFINITY);
+
+ QCBOREncode_CloseArray(&ECtx);
+ if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
+ nReturn = -1;
+ }
+
+ if(nEncodedLen != sizeof(pExpectedEncodedFloat) || bcmp(pEncoded, pExpectedEncodedFloat, nEncodedLen))
+ nReturn = -1;
+
+ //printencoded(pEncoded, nEncodedLen);
+
+ return(nReturn);
+}
+
+static uint8_t pExpectedEncodedDates[] = {
+ 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;
+
+ uint8_t pEncoded[1000];
+ size_t nEncodedLen = sizeof(pEncoded);
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+
+ 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);
+
+
+ if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
+ nReturn = -1;
+ }
+
+ if(nEncodedLen != sizeof(pExpectedEncodedDates) || bcmp(pEncoded, pExpectedEncodedDates, nEncodedLen))
+ nReturn = -1;
+
+ //printencoded(pEncoded, nEncodedLen);
+
+ return(nReturn);
+
+}
+
+
+int ArrayNestingTest1()
+{
+ QCBOREncodeContext ECtx;
+ int i;
+ int nReturn = 0;
+
+ uint8_t pEncoded[100];
+ size_t nEncodedLen = sizeof(pEncoded);
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+ for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
+ QCBOREncode_OpenArray(&ECtx);
+ }
+ for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
+ QCBOREncode_CloseArray(&ECtx);
+ }
+ if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
+ printf("ArrayNestingTest1 Failed\n");
+ nReturn = -1;
+ }
+ //printencoded(pEncoded, nEncodedLen);
+
+ return(nReturn);
+}
+
+
+
+int ArrayNestingTest2()
+{
+ QCBOREncodeContext ECtx;
+ int i;
+ int nReturn = 0;
+
+ uint8_t pEncoded[100];
+ size_t nEncodedLen = sizeof(pEncoded);
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+ for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
+ QCBOREncode_OpenArray(&ECtx);
+ }
+ for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
+ QCBOREncode_CloseArray(&ECtx);
+ }
+ if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
+ nReturn = -1;
+ }
+
+ return(nReturn);
+}
+
+
+
+int ArrayNestingTest3()
+{
+ QCBOREncodeContext ECtx;
+ int i;
+ int nReturn = 0;
+
+ uint8_t pEncoded[100];
+ size_t nEncodedLen = sizeof(pEncoded);
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+ for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
+ QCBOREncode_OpenArray(&ECtx);
+ }
+ for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
+ QCBOREncode_CloseArray(&ECtx);
+ }
+ if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_TOO_MANY_CLOSES) {
+ nReturn = -1;
+ }
+
+ return(nReturn);
+}
+
+
+
+static uint8_t s_pFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
+
+static int EncodeRaw(uint8_t **pEncoded, size_t *pEncodedLen)
+{
+ QCBOREncodeContext ECtx;
+ int nReturn = -1;
+
+ *pEncoded = NULL;
+ *pEncodedLen = INT32_MAX; // largest buffer this CBOR implementation will deal with
+
+ // 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, *pEncoded, *pEncodedLen);
+ QCBOREncode_OpenArray(&ECtx);
+ QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {s_pFiveArrarys, 5}), 1});
+ QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {pExpectedEncodedInts, sizeof(pExpectedEncodedInts)}), 1});
+ QCBOREncode_CloseArray(&ECtx);
+
+ if(QCBOREncode_Finish(&ECtx, pEncodedLen))
+ goto Done;
+ if(*pEncoded != NULL) {
+ nReturn = 0;
+ goto Done;
+ }
+ *pEncoded = malloc(*pEncodedLen);
+ } while(1);
+
+Done:
+ return(nReturn);
+}
+
+
+// Validated at http://cbor.me and by manually examining its output
+static uint8_t s_pEncodeRawExpected[] = {
+ 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()
+{
+ uint8_t *pEncodedRaw;
+ size_t nEncodedRawLen;
+
+ if(EncodeRaw(&pEncodedRaw, &nEncodedRawLen)) {
+ return(-1);
+ }
+
+ //printencoded(pEncodedRaw, nEncodedRawLen);
+
+ int nReturn = 0;
+ if(nEncodedRawLen != sizeof(s_pEncodeRawExpected) || memcmp(s_pEncodeRawExpected, pEncodedRaw, sizeof(s_pEncodeRawExpected)))
+ nReturn = 1;
+
+ return(nReturn);
+}
+
+
+
+
+
+static int CreateMap(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
+ // calucate the length so buffer can be allocated correctly,
+ // and last with the buffer to do the actual encoding
+ do {
+ QCBOREncode_Init(&ECtx, *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_Finish(&ECtx, pEncodedLen))
+ goto Done;
+ if(*pEncoded != NULL) {
+ nReturn = 0;
+ goto Done;
+ }
+ *pEncoded = malloc(*pEncodedLen);
+ } while(1);
+
+ Done:
+ return(nReturn);
+}
+
+
+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 } ;
+
+
+
+int MapEncodeTest()
+{
+ uint8_t *pEncodedMaps;
+ size_t nEncodedMapLen;
+
+ if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
+ return(-1);
+ }
+
+ //printencoded(pEncodedMaps, nEncodedMapLen);
+
+ int nReturn = 0;
+ if(memcmp(pValidMapEncoded, pEncodedMaps, sizeof(pValidMapEncoded)))
+ nReturn = 1;
+
+ 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.
+
+ */
+
+int FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, uint8_t *pOut, size_t *pnLen)
+{
+ // 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, pOut, *pnLen);
+
+ // 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.
+ const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
+ UsefulBufC WSPV = {pPV, sizeof(pPV)};
+
+ QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
+ }
+ }
+
+ // Close the telemetry map
+ QCBOREncode_CloseMap(&ECtx);
+
+ // Close the map
+ QCBOREncode_CloseMap(&ECtx);
+
+ return QCBOREncode_Finish(&ECtx, pnLen);
+}
+
+
+static const uint8_t pExpectedRTIC[] = {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()
+{
+ uint8_t out[200];
+ size_t len = sizeof(out);
+
+ int nResult = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730, "recent", "0xA1eC5001", out, &len);
+ if(nResult)
+ return -1;
+
+ if(memcmp(pExpectedRTIC, out, sizeof(pExpectedRTIC)))
+ return 1;
+
+ //printencoded(out, len);
+
+ return 0;
+}
+
+static const uint8_t pBStrArrayExpected[] = { 0x45, 0x81, 0x63, 0x66, 0x6F, 0x6F };
+
+static const uint8_t pBStrMapExpected[] = {
+ 0x81, // array of 1
+ 0x58, 0x1e, // bstr wrapper
+ 0xA1, // map of 1
+ 0x13, // label 19
+ 0x78, 0x1a, // text string of 26
+ 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a};
+
+int BStrWrapTests()
+{
+ QCBOREncodeContext ECtx;
+
+ uint8_t pEncoded[100];
+ size_t nEncodedLen = sizeof(pEncoded);
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+
+ QCBOREncode_OpenArray_3(&ECtx, NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 1);
+ QCBOREncode_AddSZString(&ECtx, "foo");
+ QCBOREncode_CloseArray(&ECtx);
+
+ EncodedCBOR E;
+ QCBOREncode_Finish2(&ECtx, &E);
+
+ if(UsefulBuf_Compare(UsefulBuf_Const(E.Bytes), ByteArrayLiteralToUsefulBufC(pBStrArrayExpected) )) {
+ return 1;
+ }
+
+
+ QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
+
+ QCBOREncode_OpenArray(&ECtx);
+
+ QCBOREncode_OpenMap_3(&ECtx, NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 1);
+ QCBOREncode_AddSZStringToMapN(&ECtx, 19, "abcdefghijklmnopqrstuvwxyz");
+ QCBOREncode_CloseMap(&ECtx);
+ QCBOREncode_CloseArray(&ECtx);
+
+ QCBOREncode_Finish2(&ECtx, &E);
+
+ if(UsefulBuf_Compare(UsefulBuf_Const(E.Bytes), ByteArrayLiteralToUsefulBufC(pBStrMapExpected) )) {
+ return 2;
+ }
+
+ return 0;
+}
+
+
+
+
diff --git a/test/qcbor_encode_tests.h b/test/qcbor_encode_tests.h
new file mode 100644
index 0000000..038465c
--- /dev/null
+++ b/test/qcbor_encode_tests.h
@@ -0,0 +1,128 @@
+/*==============================================================================
+Copyright (c) 2016-2018, The Linux Foundation. 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 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
+
+ */
+
+
+/*
+ Encode lots of integer values, particularly around the boundary and make sure they
+ Match the expected binary output. Primarily an encoding test.
+ */
+int IntegerValuesTest1();
+
+
+
+/*
+ Create nested arrays to the max depth allowed and make sure it succeeds.
+ This is an encoding test.
+ */
+int ArrayNestingTest1();
+
+
+/*
+ Create nested arrays to one more than the meax depth and make sure it fails.
+ This is an encoding test.
+ */
+int ArrayNestingTest2();
+
+
+/*
+ Encoding test.
+ Create arrays to max depth and close one extra time and look for correct error code
+ */
+int ArrayNestingTest3();
+
+
+/*
+ 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();
+
+
+/*
+ This creates a somewhat complicated CBOR MAP and verifies it against expected
+ data. This is an encoding test.
+ */
+int MapEncodeTest();
+
+
+
+/*
+ Encodes a goodly number of floats and doubles and checks encoding is right
+ */
+int FloatValuesTest1();
+
+
+/*
+ Encodes true, false and the like
+ */
+int SimpleValuesTest1();
+
+
+/*
+ Encodes most data formats that are supported */
+int EncodeDateTest();
+
+
+/*
+ Encodes particular data structure that a particular app will need...
+ */
+int RTICResultsTest();
+
+
+/*
+ Calls all public encode methods in qcbor.h once.
+ */
+int AllAddMethodsTest();
+
+/*
+ The binary string wrapping of maps and arrays used by COSE
+ */
+int BStrWrapTests();
+
+
+
+#endif /* defined(__QCBOR__qcbor_encode_tests__) */