Add memory testing for Mac0 message
diff --git a/src/Cose.c b/src/Cose.c
index a26d139..0b91912 100644
--- a/src/Cose.c
+++ b/src/Cose.c
@@ -53,7 +53,6 @@
 	return true;
 
 errorReturn:
-	_COSE_Release(pobj);
 	return false;
 }
 
@@ -111,7 +110,6 @@
 	return true;
 
 errorReturn:
-	_COSE_Release(pobj);
 	return false;
 }
 
diff --git a/src/MacMessage.c b/src/MacMessage.c
index 258f352..b00f6df 100644
--- a/src/MacMessage.c
+++ b/src/MacMessage.c
@@ -34,7 +34,10 @@
 	return (HCOSE_MAC)pobj;
 
 errorReturn:
-	if (pobj != NULL) COSE_Mac_Free((HCOSE_MAC)pobj);
+	if (pobj != NULL) {
+		_COSE_Mac_Release(pobj);
+		COSE_FREE(pobj, context);
+	}
 	return NULL;
 }
 
@@ -50,7 +53,12 @@
 	if (pobj == NULL) {
 		perr->err = COSE_ERR_OUT_OF_MEMORY;
 	errorReturn:
-		if ((pIn == NULL) && (pobj != NULL)) COSE_FREE(pobj, context);
+		if (pobj != NULL) {
+			_COSE_Mac_Release(pobj);
+			if (pIn == NULL) {
+				COSE_FREE(pobj, context);
+			}
+		}
 		return NULL;
 	}
 
diff --git a/src/MacMessage0.c b/src/MacMessage0.c
index 78f804a..1d04e93 100644
--- a/src/MacMessage0.c
+++ b/src/MacMessage0.c
@@ -11,15 +11,14 @@
 
 byte RgbDontUse2[8 * 1024];   //  Remove this array when we can compute the size of a cbor serialization without this hack.
 
+COSE * Mac0Root = NULL;
 
 bool IsValidMac0Handle(HCOSE_MAC0 h)
 {
 	COSE_Mac0Message * p = (COSE_Mac0Message *)h;
-	if (p == NULL) return false;
-	return true;
+	return _COSE_IsInList(Mac0Root, &p->m_message);
 }
 
-
 HCOSE_MAC0 COSE_Mac0_Init(CBOR_CONTEXT_COMMA cose_errback * perr)
 {
 	COSE_Mac0Message * pobj = (COSE_Mac0Message *)COSE_CALLOC(1, sizeof(COSE_Mac0Message), context);
@@ -29,10 +28,15 @@
 		goto errorReturn;
 	}
 
+	_COSE_InsertInList(&Mac0Root, &pobj->m_message);
+
 	return (HCOSE_MAC0)pobj;
 
 errorReturn:
-	if (pobj != NULL) COSE_Mac0_Free((HCOSE_MAC0)pobj);
+	if (pobj != NULL) {
+		_COSE_Mac0_Release(pobj);
+		COSE_FREE(pobj, context);
+	}
 	return NULL;
 }
 
@@ -48,7 +52,12 @@
 	if (pobj == NULL) {
 		perr->err = COSE_ERR_OUT_OF_MEMORY;
 	errorReturn:
-		if ((pIn == NULL) && (pobj != NULL)) COSE_FREE(pobj, context);
+		if (pobj != NULL) {
+			_COSE_Mac0_Release(pobj);
+			if (pIn == NULL) {
+				COSE_FREE(pobj, context);
+			}
+		}
 		return NULL;
 	}
 
@@ -59,6 +68,8 @@
 	pRecipients = _COSE_arrayget_int(&pobj->m_message, INDEX_MAC_RECIPIENTS);
 	CHECK_CONDITION(pRecipients == NULL, COSE_ERR_INVALID_PARAMETER);
 
+	_COSE_InsertInList(&Mac0Root, &pobj->m_message);
+
 	return(HCOSE_MAC0)pobj;
 }
 
@@ -76,13 +87,15 @@
 		return true;
 	}
 
+	_COSE_RemoveFromList(&Mac0Root, &p->m_message);
+
 #ifdef USE_CBOR_CONTEXT
-	context = ((COSE_Mac0Message *)h)->m_message.m_allocContext;
+	context = p->m_message.m_allocContext;
 #endif
 
-	_COSE_Mac0_Release((COSE_Mac0Message *)h);
+	_COSE_Mac0_Release(p);
 
-	COSE_FREE((COSE_Mac0Message *)h, &context);
+	COSE_FREE(p, &context);
 
 	return true;
 }
diff --git a/src/cose_int.h b/src/cose_int.h
index 2b5f0ed..0de593f 100644
--- a/src/cose_int.h
+++ b/src/cose_int.h
@@ -199,7 +199,8 @@
 //
 //  Debugging Items
 
-#define DO_ASSERT assert(false);
+//#define DO_ASSERT assert(false);
+#define DO_ASSERT
 #define CHECK_CONDITION(condition, error) { if (!(condition)) { DO_ASSERT; if (perr != NULL) {perr->err = error;} goto errorReturn;}}
 #define FAIL_CONDITION(error) { DO_ASSERT; if (perr != NULL) {perr->err = error;} goto errorReturn;}
 #define CHECK_CONDITION_CBOR(condition, error) { if (!(condition)) { DO_ASSERT; if (perr != NULL) {perr->err = _MapFromCBOR(error);} goto errorReturn;}}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 54eb6a5..1fdafaf 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -58,4 +58,5 @@
 
 add_test (NAME corner-cases WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --corners )
 add_test (NAME Memory-mac WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --memory Examples/hmac-examples/HMac-01.json )
+add_test (NAME Memory-mac0 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --memory Examples/hmac-examples/HMac-enc-01.json )
 
diff --git a/test/mac_test.c b/test/mac_test.c
index 22641d2..afb28d2 100644
--- a/test/mac_test.c
+++ b/test/mac_test.c
@@ -239,14 +239,14 @@
 	}
 
 	hMAC = (HCOSE_MAC0)COSE_Decode(pbEncoded, cbEncoded, &type, COSE_mac0_object, CBOR_CONTEXT_PARAM_COMMA NULL);
-	if (hMAC == NULL) exit(1);
+	if (hMAC == NULL) goto errorReturn;
 
-	if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) exit(1);
+	if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) goto errorReturn;
 	pMac = cn_cbor_mapget_string(pInput, "mac0");
-	if ((pMac == NULL) || (pMac->type != CN_CBOR_MAP)) exit(1);
+	if ((pMac == NULL) || (pMac->type != CN_CBOR_MAP)) goto errorReturn;
 
 	pRecipients = cn_cbor_mapget_string(pMac, "recipients");
-	if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) exit(1);
+	if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) goto errorReturn;
 
 	pRecipients = pRecipients->first_child;
 
@@ -276,6 +276,10 @@
 	exitHere:
 	if (fFail) CFails += 1;
 	return 0;
+
+errorReturn:
+	CFails += 1;
+	return 0;
 }
 
 int ValidateMac0(const cn_cbor * pControl)
@@ -299,9 +303,9 @@
 	HCOSE_MAC0 hMacObj = COSE_Mac0_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	const cn_cbor * pInputs = cn_cbor_mapget_string(pControl, "input");
-	if (pInputs == NULL) exit(1);
+	if (pInputs == NULL) goto returnError;
 	const cn_cbor * pMac = cn_cbor_mapget_string(pInputs, "mac0");
-	if (pMac == NULL) exit(1);
+	if (pMac == NULL) goto returnError;
 
 	const cn_cbor * pContent = cn_cbor_mapget_string(pInputs, "plaintext");
 	if (!COSE_Mac0_SetContent(hMacObj, pContent->v.bytes, pContent->length, NULL)) goto returnError;
@@ -312,16 +316,16 @@
 	const cn_cbor * pAlg = COSE_Mac0_map_get_int(hMacObj, 1, COSE_BOTH, NULL);
 
 	const cn_cbor * pRecipients = cn_cbor_mapget_string(pMac, "recipients");
-	if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) exit(1);
+	if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) goto returnError;
 
 	pRecipients = pRecipients->first_child;
 
 	cn_cbor * pkey = BuildKey(cn_cbor_mapget_string(pRecipients, "key"));
-		if (pkey == NULL) exit(1);
+		if (pkey == NULL) goto returnError;
 
 		cn_cbor * k = cn_cbor_mapget_int(pkey, -1);
 
-	if (!COSE_Mac0_encrypt(hMacObj, k->v.bytes, k->length, NULL)) exit(1);
+	if (!COSE_Mac0_encrypt(hMacObj, k->v.bytes, k->length, NULL)) goto returnError;
 
 	size_t cb = COSE_Encode((HCOSE)hMacObj, NULL, 0, 0) + 1;
 	byte * rgb = (byte *)malloc(cb);
diff --git a/test/test.c b/test/test.c
index 1822659..78676a6 100644
--- a/test/test.c
+++ b/test/test.c
@@ -328,6 +328,8 @@
 	unsigned char * pb;
 	size_t cb;
 
+	if (pKeyOut == NULL) return NULL;
+
 	if ((pKty == NULL) || (pKty->type != CN_CBOR_TEXT)) return NULL;
 	if (pKty->length == 2) {
 		if (strncmp(pKty->v.str, "EC", 2) == 0) kty = 2;
@@ -353,16 +355,22 @@
 					((RgStringKeys[i].kty == 0) || (RgStringKeys[i].kty == kty))) {
 					switch (RgStringKeys[i].operation) {
 					case OPERATION_NONE:
-						cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, cn_cbor_clone(pValue, CBOR_CONTEXT_PARAM_COMMA NULL), CBOR_CONTEXT_PARAM_COMMA NULL);
+						p = cn_cbor_clone(pValue, CBOR_CONTEXT_PARAM_COMMA NULL);
+						if (p == NULL) return NULL;
+						if (!cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL;
 						break;
 
 					case OPERATION_BASE64:
 						pb = base64_decode(pValue->v.str, pValue->length, &cb);
-						cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, cn_cbor_data_create(pb, (int) cb, CBOR_CONTEXT_PARAM_COMMA NULL), CBOR_CONTEXT_PARAM_COMMA NULL);
+						p = cn_cbor_data_create(pb, (int)cb, CBOR_CONTEXT_PARAM_COMMA NULL);
+						if (p == NULL) return NULL;
+						if (!cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL;
 						break;
 
 					case OPERATION_STRING:
-						cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, cn_cbor_int_create(MapName(pValue, RgCurveNames, _countof(RgCurveNames)), CBOR_CONTEXT_PARAM_COMMA NULL), CBOR_CONTEXT_PARAM_COMMA NULL);
+						p = cn_cbor_int_create(MapName(pValue, RgCurveNames, _countof(RgCurveNames)), CBOR_CONTEXT_PARAM_COMMA NULL);
+						if (p == NULL) return NULL;
+						if (!cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL;
 						break;
 					}
 					i = 99;
@@ -457,6 +465,21 @@
 				if (CFails == 0) fBuildDone = true;
 			}
 		}
+		else if (cn_cbor_mapget_string(pInput, "mac0") != NULL) {
+			if (!fValidateDone) {
+				allocator = CreateContext(iFail);
+				CFails = 0;
+				ValidateMac0(pControl);
+				if (CFails == 0) fValidateDone = true;
+			}
+
+			if (!fBuildDone) {
+				allocator = CreateContext(iFail);
+				CFails = 0;
+				BuildMac0Message(pControl);
+				if (CFails == 0) fBuildDone = true;
+			}
+		}
 	}
 	CFails = 0;
 	allocator = NULL;