#include "srmresourcestrings.h"
#include "doxmresource.h"
#include "srmutility.h"
+#include "ocserverrequest.h"
#include <stdlib.h>
+#ifdef WITH_ARDUINO
#include <string.h>
+#else
+#include <strings.h>
+#endif
#define TAG PCF("SRM-ACL")
OicSecAcl_t *gAcl = NULL;
static OCResourceHandle gAclHandle = NULL;
+/**
+ * This function frees OicSecAcl_t object's fields and object itself.
+ */
+static void FreeACE(OicSecAcl_t *ace)
+{
+ size_t i;
+ if(NULL == ace)
+ {
+ OC_LOG (INFO, TAG, PCF("Invalid Parameter"));
+ return;
+ }
+
+ // Clean Resources
+ for (i = 0; i < ace->resourcesLen; i++)
+ {
+ OICFree(ace->resources[i]);
+ }
+ OICFree(ace->resources);
+
+ //Clean Period & Recurrence
+ for(i = 0; i < ace->prdRecrLen; i++)
+ {
+ OICFree(ace->periods[i]);
+ OICFree(ace->recurrences[i]);
+ }
+ OICFree(ace->periods);
+ OICFree(ace->recurrences);
+
+ // Clean Owners
+ OICFree(ace->owners);
+
+ // Clean ACL node itself
+ OICFree(ace);
+}
+
void DeleteACLList(OicSecAcl_t* acl)
{
if (acl)
OicSecAcl_t *aclTmp1 = NULL, *aclTmp2 = NULL;
LL_FOREACH_SAFE(acl, aclTmp1, aclTmp2)
{
- int i = 0;
-
LL_DELETE(acl, aclTmp1);
-
- // Clean Resources
- for (i = 0; i < aclTmp1->resourcesLen; i++)
- {
- OICFree(aclTmp1->resources[i]);
- }
- OICFree(aclTmp1->resources);
-
- //Clean Period & Recurrence
- for(i = 0; i < aclTmp1->prdRecrLen; i++)
- {
- OICFree(aclTmp1->periods[i]);
- OICFree(aclTmp1->recurrences[i]);
- }
- OICFree(aclTmp1->periods);
- OICFree(aclTmp1->recurrences);
-
- // Clean Owners
- OICFree(aclTmp1->owners);
-
- // Clean ACL node itself
- OICFree(aclTmp1);
+ FreeACE(aclTmp1);
}
}
}
cJSON *jsonRsrcArray = NULL;
cJSON_AddItemToObject (jsonAcl, OIC_JSON_RESOURCES_NAME, jsonRsrcArray = cJSON_CreateArray());
VERIFY_NON_NULL(TAG, jsonRsrcArray, ERROR);
- for (int i = 0; i < acl->resourcesLen; i++)
+ for (size_t i = 0; i < acl->resourcesLen; i++)
{
cJSON_AddItemToArray (jsonRsrcArray, cJSON_CreateString(acl->resources[i]));
}
cJSON *jsonOwnrArray = NULL;
cJSON_AddItemToObject (jsonAcl, OIC_JSON_OWNERS_NAME, jsonOwnrArray = cJSON_CreateArray());
VERIFY_NON_NULL(TAG, jsonOwnrArray, ERROR);
- for (int i = 0; i < acl->ownersLen; i++)
+ for (size_t i = 0; i < acl->ownersLen; i++)
{
outLen = 0;
acl->resources = (char**)OICCalloc(acl->resourcesLen, sizeof(char*));
VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
- int idxx = 0;
+ size_t idxx = 0;
do
{
cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
return headAcl;
}
+static bool UpdatePersistentStorage(const OicSecAcl_t *acl)
+{
+ // Convert ACL data into JSON for update to persistent storage
+ char *jsonStr = BinToAclJSON(acl);
+ if (jsonStr)
+ {
+ cJSON *jsonAcl = cJSON_Parse(jsonStr);
+ OICFree(jsonStr);
+
+ if ((jsonAcl) && (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_ACL_NAME, jsonAcl)))
+ {
+ return true;
+ }
+ cJSON_Delete(jsonAcl);
+ }
+ return false;
+}
+/*
+ * This method removes ACE for the subject and resource from the ACL
+ *
+ * @param subject - subject of the ACE
+ * @param resource - resource of the ACE
+ *
+ * @return
+ * OC_STACK_RESOURCE_DELETED on success
+ * OC_STACK_NO_RESOURC on failure to find the appropriate ACE
+ * OC_STACK_INVALID_PARAM on invalid parameter
+ */
+static OCStackResult RemoveACE(const OicUuid_t * subject,
+ const char * resource)
+{
+ OC_LOG(INFO, TAG, PCF("IN RemoveACE"));
+
+ OicSecAcl_t *acl = NULL;
+ OicSecAcl_t *tempAcl = NULL;
+ bool deleteFlag = false;
+ OCStackResult ret = OC_STACK_NO_RESOURCE;
+
+ if(memcmp(subject->id, &WILDCARD_SUBJECT_ID, sizeof(subject->id)) == 0)
+ {
+ OC_LOG_V (INFO, TAG, PCF("%s received invalid parameter"), __func__ );
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ //If resource is NULL then delete all the ACE for the subject.
+ if(NULL == resource)
+ {
+ LL_FOREACH_SAFE(gAcl, acl, tempAcl)
+ {
+ if(memcmp(acl->subject.id, subject->id, sizeof(subject->id)) == 0)
+ {
+ LL_DELETE(gAcl, acl);
+ FreeACE(acl);
+ deleteFlag = true;
+ }
+ }
+ }
+ else
+ {
+ //Looping through ACL to find the right ACE to delete. If the required resource is the only
+ //resource in the ACE for the subject then delete the whole ACE. If there are more resources
+ //than the required resource in the ACE, for the subject then just delete the resource from
+ //the resource array
+ LL_FOREACH_SAFE(gAcl, acl, tempAcl)
+ {
+ if(memcmp(acl->subject.id, subject->id, sizeof(subject->id)) == 0)
+ {
+ if(1 == acl->resourcesLen && strcmp(acl->resources[0], resource) == 0)
+ {
+ LL_DELETE(gAcl, acl);
+ FreeACE(acl);
+ deleteFlag = true;
+ break;
+ }
+ else
+ {
+ int resPos = -1;
+ size_t i;
+ for(i = 0; i < acl->resourcesLen; i++)
+ {
+ if(strcmp(acl->resources[i], resource) == 0)
+ {
+ resPos = i;
+ break;
+ }
+ }
+ if((0 <= resPos))
+ {
+ OICFree(acl->resources[resPos]);
+ acl->resources[resPos] = NULL;
+ acl->resourcesLen -= 1;
+ for(i = resPos; i < acl->resourcesLen; i++)
+ {
+ acl->resources[i] = acl->resources[i+1];
+ }
+ deleteFlag = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ if(deleteFlag)
+ {
+ if(UpdatePersistentStorage(gAcl))
+ {
+ ret = OC_STACK_RESOURCE_DELETED;
+ }
+ }
+ return ret;
+}
+
static OCEntityHandlerResult HandleACLGetRequest (const OCEntityHandlerRequest * ehRequest)
{
// Convert ACL data into JSON for transmission
// Append the new ACL to existing ACL
LL_APPEND(gAcl, newAcl);
- // Convert ACL data into JSON for update to persistent storage
- char *jsonStr = BinToAclJSON(gAcl);
- if (jsonStr)
+ if(UpdatePersistentStorage(gAcl))
{
- cJSON *jsonAcl = cJSON_Parse(jsonStr);
- OICFree(jsonStr);
-
- if ((jsonAcl) &&
- (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_ACL_NAME, jsonAcl)))
- {
- ehRet = OC_EH_RESOURCE_CREATED;
- }
- cJSON_Delete(jsonAcl);
+ ehRet = OC_EH_RESOURCE_CREATED;
}
}
return ehRet;
}
+static OCEntityHandlerResult HandleACLDeleteRequest(const OCEntityHandlerRequest *ehRequest)
+{
+ OC_LOG (INFO, TAG, PCF("Processing ACLDeleteRequest"));
+ OCEntityHandlerResult ehRet = OC_EH_ERROR;
+
+ if(NULL == ehRequest->query)
+ {
+ return ehRet;
+ }
+ OicParseQueryIter_t parseIter = {.attrPos=NULL};
+ OicUuid_t subject = {.id={0}};
+ char * resource = NULL;
+
+ //Parsing REST query to get subject & resource
+ ParseQueryIterInit((unsigned char *)ehRequest->query, &parseIter);
+
+ while(GetNextQuery(&parseIter))
+ {
+ if(strncasecmp((char *)parseIter.attrPos, OIC_JSON_SUBJECT_NAME, parseIter.attrLen) == 0)
+ {
+ unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
+ uint32_t outLen = 0;
+ B64Result b64Ret = B64_OK;
+
+ b64Ret = b64Decode((char *)parseIter.valPos, parseIter.valLen, base64Buff,
+ sizeof(base64Buff), &outLen);
+
+ VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(subject.id)), ERROR);
+ memcpy(subject.id, base64Buff, outLen);
+ }
+ if(strncasecmp((char *)parseIter.attrPos, OIC_JSON_RESOURCES_NAME, parseIter.attrLen) == 0)
+ {
+ resource = (char *)OICMalloc(parseIter.valLen);
+ VERIFY_NON_NULL(TAG, resource, ERROR);
+ OICStrcpy(resource, sizeof(resource), (char *)parseIter.valPos);
+ }
+ }
+
+ if(OC_STACK_RESOURCE_DELETED == RemoveACE(&subject, resource))
+ {
+ ehRet = OC_EH_RESOURCE_DELETED;
+ }
+ OICFree(resource);
+
+ // Send payload to request originator
+ SendSRMResponse(ehRequest, ehRet, NULL);
+
+exit:
+ return ehRet;
+}
+
/*
* This internal method is the entity handler for ACL resources and
* will handle REST request (GET/PUT/POST/DEL) for them.
OCEntityHandlerRequest * ehRequest,
void* callbackParameter)
{
+ OC_LOG(INFO, TAG, PCF("Received request ACLEntityHandler\n"));
OCEntityHandlerResult ehRet = OC_EH_ERROR;
if (!ehRequest)
ehRet = HandleACLPostRequest(ehRequest);
break;
+ case OC_REST_DELETE:
+ ehRet = HandleACLDeleteRequest(ehRequest);
+ break;
+
default:
ehRet = OC_EH_ERROR;
SendSRMResponse(ehRequest, ehRet, NULL);
{
OCStackResult ret = OC_STACK_ERROR;
- OicUuid_t ownerId = {};
+ OicUuid_t ownerId = {.id={}};
/*
* TODO In future, when new virtual resources will be added in OIC
acl->resources = (char**)OICCalloc(acl->resourcesLen, sizeof(char*));
VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
- for (int i = 0; i < acl->resourcesLen; i++)
+ for (size_t i = 0; i < acl->resourcesLen; i++)
{
size_t len = strlen(rsrcs[i]) + 1;
acl->resources[i] = (char*)OICMalloc(len * sizeof(char));
#include "srmutility.h"
#include "cainterface.h"
#include <stdlib.h>
+#ifdef WITH_ARDUINO
#include <string.h>
+#else
+#include <strings.h>
+#endif
#include <stdint.h>
static OicSecCred_t *gCred = NULL;
static OCResourceHandle gCredHandle = NULL;
-void DeleteCredList(OicSecCred_t* cred)
+/**
+ * This function frees OicSecCred_t object's fields and object itself.
+ */
+static void FreeCred(OicSecCred_t *cred)
{
- if (cred)
+ if(NULL == cred)
{
- OicSecCred_t *credTmp1 = NULL, *credTmp2 = NULL;
- LL_FOREACH_SAFE(cred, credTmp1, credTmp2)
- {
- LL_DELETE(cred, credTmp1);
-
- //Note: Need further clarification on roleID data type
+ OC_LOG (INFO, TAG, PCF("Invalid Parameter"));
+ return;
+ }
+ //Note: Need further clarification on roleID data type
#if 0
- //Clean roleIds
- OICFree(credTmp1->roleIds);
+ //Clean roleIds
+ OICFree(cred->roleIds);
#endif
- //Clean PublicData
- OICFree(credTmp1->publicData.data);
+ //Clean PublicData
+ OICFree(cred->publicData.data);
- //Clean PrivateData
- OICFree(credTmp1->privateData.data);
+ //Clean PrivateData
+ OICFree(cred->privateData.data);
- //Clean Period
- OICFree(credTmp1->period);
+ //Clean Period
+ OICFree(cred->period);
- //Clean Owners
- OICFree(credTmp1->owners);
+ //Clean Owners
+ OICFree(cred->owners);
- //Clean Cred node itself
- OICFree(credTmp1);
+ //Clean Cred node itself
+ OICFree(cred);
+}
+
+void DeleteCredList(OicSecCred_t* cred)
+{
+ if (cred)
+ {
+ OicSecCred_t *credTmp1 = NULL, *credTmp2 = NULL;
+ LL_FOREACH_SAFE(cred, credTmp1, credTmp2)
+ {
+ LL_DELETE(cred, credTmp1);
+ FreeCred(credTmp1);
}
}
}
return cred;
}
-/*
+static bool UpdatePersistentStorage(const OicSecCred_t *cred)
+{
+ bool ret = false;
+
+ // Convert Cred data into JSON for update to persistent storage
+ char *jsonStr = BinToCredJSON(cred);
+ if (jsonStr)
+ {
+ cJSON *jsonCred = cJSON_Parse(jsonStr);
+ OICFree(jsonStr);
+
+ if ((jsonCred) &&
+ (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_CRED_NAME, jsonCred)))
+ {
+ ret = true;
+ }
+ cJSON_Delete(jsonCred );
+ }
+ else //Empty cred list
+ {
+ if (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_CRED_NAME, NULL))
+ {
+ ret = true;
+ }
+ }
+ return ret;
+}
+
+/**
* Compare function used LL_SORT for sorting credentials
*
* @param first pointer to OicSecCred_t struct
exit:
return 0;
}
+
+
/**
* This function adds the new cred to the credential list.
*
//Append the new Cred to existing list
LL_APPEND(gCred, newCred);
- //Convert CredList to JSON and update the persistent Storage
- jsonStr = BinToCredJSON(gCred);
-
- if(jsonStr)
+ if(UpdatePersistentStorage(gCred))
{
- cJSON *jsonCred = cJSON_Parse(jsonStr);
- OICFree(jsonStr);
+ ret = OC_STACK_OK;
+ }
- if((jsonCred) && (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_CRED_NAME, jsonCred)))
+exit:
+ return ret;
+}
+
+OCStackResult RemoveCredential(const OicUuid_t *subject)
+{
+ OCStackResult ret = OC_STACK_ERROR;
+ OicSecCred_t *cred = NULL;
+ OicSecCred_t *tempCred = NULL;
+ bool deleteFlag = false;
+
+ LL_FOREACH_SAFE(gCred, cred, tempCred)
+ {
+ if(memcmp(cred->subject.id, subject->id, sizeof(subject->id)) == 0)
{
- ret = OC_STACK_OK;
+ LL_DELETE(gCred, cred);
+ FreeCred(cred);
+ deleteFlag = 1;
}
- cJSON_Delete(jsonCred);
}
-exit:
+ if(deleteFlag)
+ {
+ if(UpdatePersistentStorage(gCred))
+ {
+ ret = OC_STACK_RESOURCE_DELETED;
+ }
+ }
return ret;
+
}
static OCEntityHandlerResult HandlePostRequest(const OCEntityHandlerRequest * ehRequest)
return ret;
}
+static OCEntityHandlerResult HandleDeleteRequest(const OCEntityHandlerRequest *ehRequest)
+{
+ OC_LOG_V (INFO, TAG, PCF("Processing CredDeleteRequest"));
+
+ OCEntityHandlerResult ehRet = OC_EH_ERROR;
+
+ if(NULL == ehRequest->query)
+ {
+ return ehRet;
+ }
+
+ OicParseQueryIter_t parseIter = {.attrPos=NULL};
+ OicUuid_t subject = {.id={0}};
+
+ //Parsing REST query to get the subject
+ ParseQueryIterInit((unsigned char *)ehRequest->query, &parseIter);
+ while(GetNextQuery(&parseIter))
+ {
+ if(strncasecmp((char *)parseIter.attrPos, OIC_JSON_SUBJECT_NAME,
+ parseIter.attrLen) == 0)
+ {
+ unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
+ uint32_t outLen = 0;
+ B64Result b64Ret = B64_OK;
+
+ b64Ret = b64Decode((char *)parseIter.valPos, parseIter.valLen,
+ base64Buff, sizeof(base64Buff), &outLen);
+
+ VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(subject.id)), ERROR);
+ memcpy(subject.id, base64Buff, outLen);
+ }
+ }
+
+ if(OC_STACK_RESOURCE_DELETED == RemoveCredential(&subject))
+ {
+ ehRet = OC_EH_RESOURCE_DELETED;
+ }
+
+exit:
+ return ehRet;
+}
+
+
/*
* This internal method is the entity handler for Cred resources
* to handle REST request (PUT/POST/DEL)
case OC_REST_POST:
ret = HandlePostRequest(ehRequest);
break;
+ case OC_REST_DELETE:
+ ret = HandleDeleteRequest(ehRequest);
+ break;
default:
ret = OC_EH_ERROR;
break;
{
OicSecCred_t *cred = NULL;
- if ( NULL == subject)
+ if ( NULL == subject)
{
- return NULL;
+ return NULL;
}
LL_FOREACH(gCred, cred)
{
if(memcmp(cred->subject.id, subject->id, sizeof(subject->id)) == 0)
{
- return cred;
+ return cred;
}
}
return NULL;
{
OCStackResult ret = OC_STACK_ERROR;
cJSON *jsonSVRDb = NULL;
+ OCPersistentStorage* ps = NULL;
// Read SVR database from PS
char* jsonSVRDbStr = GetSVRDatabase();
OICFree(jsonSVRDbStr);
jsonSVRDbStr = NULL;
- if (jsonObj->child )
+ //If Cred resource gets updated with empty list then delete the Cred
+ //object from database.
+ if(NULL == jsonObj && (0 == strcmp(rsrcName, OIC_JSON_CRED_NAME)))
+ {
+ cJSON_DeleteItemFromObject(jsonSVRDb, rsrcName);
+ }
+ else if (jsonObj->child )
{
// Create a duplicate of the JSON object which was passed.
cJSON* jsonDuplicateObj = cJSON_Duplicate(jsonObj, 1);
// Replace the modified json object in existing SVR database json
cJSON_ReplaceItemInObject(jsonSVRDb, rsrcName, jsonDuplicateObj->child);
}
+ }
- // Generate string representation of updated SVR database json object
- jsonSVRDbStr = cJSON_PrintUnformatted(jsonSVRDb);
- VERIFY_NON_NULL(TAG,jsonSVRDbStr, ERROR);
+ // Generate string representation of updated SVR database json object
+ jsonSVRDbStr = cJSON_PrintUnformatted(jsonSVRDb);
+ VERIFY_NON_NULL(TAG,jsonSVRDbStr, ERROR);
- // Update the persistent storage with new SVR database
- OCPersistentStorage* ps = SRMGetPersistentStorageHandler();
- if (ps && ps->open)
+ // Update the persistent storage with new SVR database
+ ps = SRMGetPersistentStorageHandler();
+ if (ps && ps->open)
+ {
+ FILE* fp = ps->open(SVR_DB_FILE_NAME, "w");
+ if (fp)
{
- FILE* fp = ps->open(SVR_DB_FILE_NAME, "w");
- if (fp)
+ size_t bytesWritten = ps->write(jsonSVRDbStr, 1, strlen(jsonSVRDbStr), fp);
+ if (bytesWritten == strlen(jsonSVRDbStr))
{
- size_t bytesWritten = ps->write(jsonSVRDbStr, 1, strlen(jsonSVRDbStr), fp);
- if (bytesWritten == strlen(jsonSVRDbStr))
- {
- ret = OC_STACK_OK;
- }
- OC_LOG_V(INFO, TAG, PCF("Written %d bytes into SVR database file"), bytesWritten);
- ps->close(fp);
- fp = NULL;
- }
- else
- {
- OC_LOG (ERROR, TAG, PCF("Unable to open SVR database file!! "));
+ ret = OC_STACK_OK;
}
+ OC_LOG_V(INFO, TAG, PCF("Written %d bytes into SVR database file"), bytesWritten);
+ ps->close(fp);
+ fp = NULL;
+ }
+ else
+ {
+ OC_LOG (ERROR, TAG, PCF("Unable to open SVR database file!! "));
}
}
const char * OIC_SEC_TRUE = "true";
const char * OIC_SEC_FALSE = "false";
-const char * OIC_SEC_REST_QUERY_SEPARATOR = "&";
+const char * OIC_SEC_REST_QUERY_SEPARATOR = ";";
char OIC_SEC_REST_QUERY_DELIMETER = '=';
+
#include "ocstack.h"
#include "ocpayload.h"
#include "oic_malloc.h"
+#include "oic_string.h"
#include "cJSON.h"
#include "cainterface.h"
#include "secureresourcemanager.h"
#include "srmresourcestrings.h"
#include "aclresource.h"
#include "srmtestcommon.h"
+#include "srmutility.h"
+#include "logger.h"
using namespace std;
+#define TAG PCF("SRM-ACL-UT")
+
#ifdef __cplusplus
extern "C" {
#endif
+
extern char * BinToAclJSON(const OicSecAcl_t * acl);
extern OicSecAcl_t * JSONToAclBin(const char * jsonStr);
extern void DeleteACLList(OicSecAcl_t* acl);
#define NUM_ACE_FOR_WILDCARD_IN_ACL1_JSON (2)
-
-void SetPersistentHandler(OCPersistentStorage *ps, bool set)
-{
- if (set)
- {
- ps->open = fopen;
- ps->read = fread;
- ps->write = fwrite;
- ps->close = fclose;
- ps->unlink = unlink;
- }
- else
- {
- memset(ps, 0, sizeof(OCPersistentStorage));
- }
- EXPECT_EQ(OC_STACK_OK,
- OCRegisterPersistentStorageHandler(ps));
-}
-
// JSON Marshalling Tests
TEST(ACLResourceTest, JSONMarshallingTests)
{
OICFree(jsonStr);
}
}
+//'DELETE' ACL test
+TEST(ACLResourceTest, ACLDeleteWithSingleResourceTest)
+{
+ OCEntityHandlerRequest ehReq = {};
+ static OCPersistentStorage ps = {};
+ char *jsonStr = NULL;
+ OicSecAcl_t acl = {};
+ OicSecAcl_t* savePtr = NULL;
+ const OicSecAcl_t* subjectAcl1 = NULL;
+ const OicSecAcl_t* subjectAcl2 = NULL;
+ OCEntityHandlerResult ehRet = OC_EH_ERROR;
+ char query[] = "sub=MjIyMjIyMjIyMjIyMjIyMg==;rsrc=/a/led";
+
+ SetPersistentHandler(&ps, true);
+
+ //ACE to POST
+ memcpy(acl.subject.id, "2222222222222222", sizeof(acl.subject.id));
+ acl.resourcesLen = 1;
+ acl.resources = (char**)OICCalloc(acl.resourcesLen, sizeof(char*));
+ VERIFY_NON_NULL(TAG, acl.resources, ERROR);
+ acl.resources[0] = (char*)OICMalloc(strlen("/a/led")+1);
+ VERIFY_NON_NULL(TAG, acl.resources[0], ERROR);
+ OICStrcpy(acl.resources[0], sizeof(acl.resources[0]), "/a/led");
+ acl.permission = 6;
+ acl.ownersLen = 1;
+ acl.owners = (OicUuid_t*)OICCalloc(acl.ownersLen, sizeof(OicUuid_t));
+ VERIFY_NON_NULL(TAG, acl.owners, ERROR);
+ memcpy(acl.owners->id, "1111111111111111", sizeof(acl.owners->id));
+
+ //GET json POST payload
+ jsonStr = BinToAclJSON(&acl);
+ VERIFY_NON_NULL(TAG, jsonStr, ERROR);
+
+ // Create Entity Handler POST request payload
+ ehReq.method = OC_REST_POST;
+ ehReq.payload = (OCPayload*)OCSecurityPayloadCreate(jsonStr);
+ ehRet = ACLEntityHandler(OC_REQUEST_FLAG, &ehReq);
+ EXPECT_TRUE(OC_EH_ERROR == ehRet);
+
+ // Verify if SRM contains ACE for the subject
+ savePtr = NULL;
+ subjectAcl1 = GetACLResourceData(&acl.subject, &savePtr);
+ EXPECT_TRUE(NULL != subjectAcl1);
+
+ // Create Entity Handler DELETE request
+ ehReq.method = OC_REST_DELETE;
+ ehReq.query = (char*)OICMalloc(strlen(query)+1);
+ VERIFY_NON_NULL(TAG, ehReq.query, ERROR);
+ OICStrcpy(ehReq.query, strlen(query)+1, query);
+ ehRet = ACLEntityHandler(OC_REQUEST_FLAG, &ehReq);
+ EXPECT_TRUE(OC_EH_ERROR == ehRet);
+
+ // Verify if SRM has deleted ACE for the subject
+ savePtr = NULL;
+ subjectAcl2 = GetACLResourceData(&acl.subject, &savePtr);
+ EXPECT_TRUE(NULL == subjectAcl2);
+
+exit:
+ // Perform cleanup
+ if(NULL != subjectAcl1)
+ {
+ DeInitACLResource();
+ }
+ OCPayloadDestroy(ehReq.payload);
+ OICFree(ehReq.query);
+ OICFree(jsonStr);
+
+}
+
+TEST(ACLResourceTest, ACLDeleteWithMultiResourceTest)
+{
+ OCEntityHandlerRequest ehReq = {};
+ static OCPersistentStorage ps = {};
+ OicSecAcl_t acl = {};
+ char *jsonStr = NULL;
+ OicSecAcl_t* savePtr = NULL;
+ const OicSecAcl_t* subjectAcl1 = NULL;
+ const OicSecAcl_t* subjectAcl2 = NULL;
+ OCEntityHandlerResult ehRet = OC_EH_ERROR;
+ char query[] = "sub=MjIyMjIyMjIyMjIyMjIyMg==;rsrc=/a/led";
+
+ SetPersistentHandler(&ps, true);
+
+ memcpy(acl.subject.id, "2222222222222222", sizeof(acl.subject.id));
+ acl.resourcesLen = 2;
+ acl.resources = (char**)OICCalloc(acl.resourcesLen, sizeof(char*));
+ VERIFY_NON_NULL(TAG, acl.resources, ERROR);
+ acl.resources[0] = (char*)OICMalloc(strlen("/a/led")+1);
+ VERIFY_NON_NULL(TAG, acl.resources[0], ERROR);
+ OICStrcpy(acl.resources[0], sizeof(acl.resources[0]), "/a/led");
+ acl.resources[1] = (char*)OICMalloc(strlen("/a/fan")+1);
+ VERIFY_NON_NULL(TAG, acl.resources[1], ERROR);
+ OICStrcpy(acl.resources[1], sizeof(acl.resources[1]), "/a/fan");
+ acl.permission = 6;
+ acl.ownersLen = 1;
+ acl.owners = (OicUuid_t*)OICCalloc(acl.ownersLen, sizeof(OicUuid_t));
+ VERIFY_NON_NULL(TAG, acl.owners, ERROR);
+ memcpy(acl.owners->id, "1111111111111111", sizeof(acl.owners->id));
+
+ jsonStr = BinToAclJSON(&acl);
+ VERIFY_NON_NULL(TAG, jsonStr, ERROR);
+
+ // Create Entity Handler POST request payload
+ ehReq.method = OC_REST_POST;
+ ehReq.payload = (OCPayload*)OCSecurityPayloadCreate(jsonStr);
+ ehRet = ACLEntityHandler(OC_REQUEST_FLAG, &ehReq);
+ EXPECT_TRUE(OC_EH_ERROR == ehRet);
+
+ // Verify if SRM contains ACE for the subject with two resources
+ savePtr = NULL;
+ subjectAcl1 = GetACLResourceData(&acl.subject, &savePtr);
+ EXPECT_TRUE(NULL != subjectAcl1);
+ EXPECT_TRUE(subjectAcl1->resourcesLen == 2);
+
+ // Create Entity Handler DELETE request
+ ehReq.method = OC_REST_DELETE;
+ ehReq.query = (char*)OICMalloc(strlen(query)+1);
+ VERIFY_NON_NULL(TAG, ehReq.query, ERROR);
+ OICStrcpy(ehReq.query, strlen(query)+1, query);
+
+ ehRet = ACLEntityHandler(OC_REQUEST_FLAG, &ehReq);
+ EXPECT_TRUE(OC_EH_ERROR == ehRet);
+
+ // Verify if SRM contains ACL for the subject but only with one resource
+ savePtr = NULL;
+ subjectAcl2 = GetACLResourceData(&acl.subject, &savePtr);
+ EXPECT_TRUE(NULL != subjectAcl2);
+ EXPECT_TRUE(subjectAcl2->resourcesLen == 1);
+
+exit:
+ // Perform cleanup
+ if(NULL != subjectAcl1)
+ {
+ DeInitACLResource();
+ }
+ OCPayloadDestroy(ehReq.payload);
+ OICFree(ehReq.query);
+ OICFree(jsonStr);
+}
#include "gtest/gtest.h"
#include "ocstack.h"
+#include "ocpayload.h"
#include "resourcemanager.h"
#include "securevirtualresourcetypes.h"
#include "credresource.h"
#include "oic_malloc.h"
#include "oic_string.h"
+#include "srmtestcommon.h"
+#include "srmutility.h"
#include "logger.h"
#define TAG PCF("SRM-CRED-UT")
void InitSecCredInstance(OicSecCred_t * cred);
void DeleteCredList(OicSecCred_t* cred);
const OicSecCred_t* GetCredResourceData(const OicUuid_t* subject);
+
#ifdef __cplusplus
}
#endif
+
+
OicSecCred_t * getCredList()
{
- OicSecCred_t * cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
- if(!cred)
- {
- return NULL;
- }
+
+ OicSecCred_t * cred = NULL;
+ size_t sz = 0;
+
+ cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
+ VERIFY_NON_NULL(TAG, cred, ERROR);
cred->credId = 1234;
OICStrcpy((char *)cred->subject.id, sizeof(cred->subject.id), "subject1");
#if 0
cred->roleIdsLen = 2;
cred->roleIds = (OicSecRole_t *)OICCalloc(cred->roleIdsLen, sizeof(OicSecRole_t));
+ VERIFY_NON_NULL(TAG, cred->roleIds, ERROR);
OICStrcpy((char *)cred->roleIds[0].id, sizeof(cred->roleIds[0].id), "role11");
OICStrcpy((char *)cred->roleIds[1].id, sizeof(cred->roleIds[1].id), "role12");
+
#endif
cred->credType = 1;
+ cred->privateData.data = (char *)OICCalloc(1, strlen("My private Key11") + 1);
+ VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
+ strcpy(cred->privateData.data, "My private Key11");
cred->ownersLen = 1;
cred->owners = (OicUuid_t*)OICCalloc(cred->ownersLen, sizeof(OicUuid_t));
- if(!cred->owners)
- {
- OICFree(cred);
- return NULL;
- }
+ VERIFY_NON_NULL(TAG, cred->owners, ERROR);
OICStrcpy((char *)cred->owners[0].id, sizeof(cred->owners[0].id), "ownersId11");
cred->next = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
- if(!cred->next)
- {
- OICFree(cred->owners);
- OICFree(cred);
- return NULL;
- }
+ VERIFY_NON_NULL(TAG, cred->next, ERROR);
cred->next->credId = 5678;
OICStrcpy((char *)cred->next->subject.id, sizeof(cred->next->subject.id), "subject2");
#if 0
cred->next->roleIdsLen = 0;
#endif
cred->next->credType = 1;
- size_t data_size = strlen("My private Key21") + 1;
- cred->next->privateData.data = (char *)OICCalloc(1, data_size);
- if(!cred->next->privateData.data)
- {
- OICFree(cred->next);
- OICFree(cred->owners);
- OICFree(cred);
- return NULL;
- }
- OICStrcpy(cred->next->privateData.data, data_size,"My private Key21");
+ sz = strlen("My private Key21") + 1;
+ cred->next->privateData.data = (char *)OICCalloc(1, sz);
+ VERIFY_NON_NULL(TAG, cred->next->privateData.data, ERROR);
+ OICStrcpy(cred->next->privateData.data, sz,"My private Key21");
#if 0
- cred->next->publicData.data = (char *)OICCalloc(1, strlen("My Public Key123") + 1);
- OICStrcpy(cred->next->publicData.data, sizeof(cred->next->publicData.data),"My Public Key123");
+ sz = strlen("My Public Key123") + 1
+ cred->next->publicData.data = (char *)OICCalloc(1, sz);
+ VERIFY_NON_NULL(TAG, cred->next->publicData.data, ERROR);
+ OICStrcpy(cred->next->publicData.data, sz,"My Public Key123");
#endif
cred->next->ownersLen = 2;
cred->next->owners = (OicUuid_t*)OICCalloc(cred->next->ownersLen, sizeof(OicUuid_t));
- if(!cred->next->owners)
- {
- OICFree(cred->next->privateData.data);
- OICFree(cred->next);
- OICFree(cred->owners);
- OICFree(cred);
- return NULL;
- }
+ VERIFY_NON_NULL(TAG, cred->next->owners, ERROR);
OICStrcpy((char *)cred->next->owners[0].id, sizeof(cred->next->owners[0].id), "ownersId21");
OICStrcpy((char *)cred->next->owners[1].id, sizeof(cred->next->owners[1].id), "ownersId22");
+
+ return cred;
+
+exit:
+ if(cred)
+ {
+ DeleteCredList(cred);
+ cred = NULL;
+ }
return cred;
}
TEST(CredEntityHandlerTest, CredEntityHandlerWithDummyRequest)
{
OCEntityHandlerRequest req;
- EXPECT_EQ(OC_EH_ERROR, CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
+ EXPECT_EQ(OC_EH_ERROR,
+ CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
}
TEST(CredEntityHandlerTest, CredEntityHandlerWithNULLRequest)
{
- EXPECT_EQ(OC_EH_ERROR, CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
+ EXPECT_EQ(OC_EH_ERROR,
+ CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
}
TEST(CredEntityHandlerTest, CredEntityHandlerInvalidFlag)
{
OCEntityHandlerRequest req;
- EXPECT_EQ(OC_EH_ERROR, CredEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
+ EXPECT_EQ(OC_EH_ERROR,
+ CredEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
+}
+
+//Cred DELETE request
+TEST(CredEntityHandlerTest, CredEntityHandlerDeleteTest)
+{
+ OCEntityHandlerRequest ehReq = {};
+ static OCPersistentStorage ps = {};
+ const OicSecCred_t* subjectCred1 = NULL;
+ const OicSecCred_t* subjectCred2 = NULL;
+ char *jsonStr = NULL;
+ OCEntityHandlerResult ehRet = OC_EH_ERROR;
+ char query[] = "sub=c3ViamVjdDE=";
+
+ SetPersistentHandler(&ps, true);
+
+ OicSecCred_t *cred = getCredList();
+ VERIFY_NON_NULL(TAG, cred, ERROR);
+
+ jsonStr = BinToCredJSON(cred);
+ VERIFY_NON_NULL(TAG, jsonStr, ERROR);
+
+ // Create Entity Handler POST request payload
+ ehReq.method = OC_REST_POST;
+ ehReq.payload = (OCPayload*)OCSecurityPayloadCreate(jsonStr);
+ ehRet = CredEntityHandler(OC_REQUEST_FLAG, &ehReq);
+ EXPECT_TRUE(OC_EH_ERROR == ehRet);
+
+ // Verify if SRM contains Credential for the subject
+ subjectCred1 = GetCredResourceData(&cred->subject);
+ EXPECT_TRUE(NULL != subjectCred1);
+
+ // Create Entity Handler DELETE request
+ ehReq.method = OC_REST_DELETE;
+ ehReq.query = (char*)OICMalloc(strlen(query)+1);
+ VERIFY_NON_NULL(TAG, ehReq.query, ERROR);
+ OICStrcpy(ehReq.query, strlen(query)+1, query);
+
+ ehRet = CredEntityHandler(OC_REQUEST_FLAG, &ehReq);
+ EXPECT_TRUE(OC_EH_ERROR == ehRet);
+
+ // Verify if SRM has deleted ACE for the subject
+ subjectCred2 = GetCredResourceData(&cred->subject);
+ EXPECT_TRUE(NULL == subjectCred2);
+
+exit:
+ // Perform cleanup
+ OICFree(ehReq.query);
+ OICFree(jsonStr);
+ OCPayloadDestroy(ehReq.payload);
+ if(NULL != cred)
+ {
+ DeInitCredResource();
+ DeleteCredList(cred);
+ }
}
//BinToCredJSON Tests
json = BinToCredJSON(cred);
+ OC_LOG_V(INFO, TAG, PCF("BinToCredJSON:%s\n"), json);
EXPECT_TRUE(json != NULL);
DeleteCredList(cred);
OICFree(json);
EXPECT_TRUE(json != NULL);
OicSecCred_t *cred2 = JSONToCredBin(json);
- EXPECT_TRUE(cred2 == NULL);
+ EXPECT_TRUE(cred2 != NULL);
DeleteCredList(cred1);
DeleteCredList(cred2);
OICFree(json);
privateKey, 1, owners);
printCred(cred);
+ EXPECT_TRUE(NULL != cred);
DeleteCredList(cred);
}
#include "ocserverrequest.h"
#include "oic_string.h"
#include "oic_malloc.h"
+#include "logger.h"
+
+#define TAG PCF("SRM-DOXM")
#ifdef __cplusplus
extern "C" {
TEST(DoxmEntityHandlerTest, DoxmEntityHandlerValidRequest)
{
EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
- char query[] = "oxm=0&owned=false&owner=owner1";
+ char query[] = "oxm=0;owned=false;owner=owner1";
OCEntityHandlerRequest req = {};
req.method = OC_REST_GET;
req.query = OICStrdup(query);
OicSecDoxm_t * doxm = getBinDoxm();
char * json = BinToDoxmJSON(doxm);
+ OC_LOG_V(INFO, TAG, PCF("BinToDoxmJSON:%s"), json);
EXPECT_TRUE(json != NULL);
DeleteDoxmBinData(doxm);
#include "gtest/gtest.h"
#include "oic_malloc.h"
+#include "ocstack.h"
char* ReadFile(const char* filename)
{
return data;
}
+void SetPersistentHandler(OCPersistentStorage *ps, bool set)
+{
+ if (set)
+ {
+ ps->open = fopen;
+ ps->read = fread;
+ ps->write = fwrite;
+ ps->close = fclose;
+ ps->unlink = unlink;
+ }
+ else
+ {
+ memset(ps, 0, sizeof(OCPersistentStorage));
+ }
+ EXPECT_EQ(OC_STACK_OK,
+ OCRegisterPersistentStorageHandler(ps));
+}
#define IOTVT_SRM_TEST_COMMON_H
char* ReadFile(const char* filename);
+void SetPersistentHandler(OCPersistentStorage *ps, bool set);
#endif //IOTVT_SRM_TEST_COMMON_H
-
#include "srmutility.h"
#include "oic_string.h"
-
//ParseRestQuery Tests
TEST(ParseRestQueryTest, ParseRestQueryEmpty)
{
OICStrcpyPartial(attr, sizeof(attr), (char *)parseIter.attrPos, parseIter.attrLen);
OICStrcpyPartial(val, sizeof(val), (char *)parseIter.valPos, parseIter.valLen);
printf("\nAttribute: %s value: %s\n\n", attr, val);
-
}
TEST(ParseRestQueryTest, ParseRestMultipleQuery)
{
char attr[10], val[10];
- unsigned char query[] = "oxm=0&owned=true&owner=owner1";
+ unsigned char query[] = "oxm=0;owned=true;owner=owner1";
OicParseQueryIter_t parseIter = {};
ParseQueryIterInit(query, &parseIter);
"perms": 2,
"ownrs" : ["MTExMTExMTExMTExMTExMQ=="]
},
+ {
+ "sub": "MjIyMjIyMjIyMjIyMjIyMg==",
+ "rsrc": ["/oic/sec/acl",
+ "/oic/sec/cred"],
+ "perms": 8,
+ "ownrs" : ["MjIyMjIyMjIyMjIyMjIyMg=="]
+ },
{
"sub": "MjIyMjIyMjIyMjIyMjIyMg==",
"rsrc": ["/a/led"],