+//******************************************************************
+//
// Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "gtest/gtest.h"
-#include "ocstack.h"
+#include "logger.h"
#include "ocpayload.h"
-#include "resourcemanager.h"
-#include "securevirtualresourcetypes.h"
-#include "credresource.h"
+#include "ocstack.h"
#include "oic_malloc.h"
#include "oic_string.h"
+#include "resourcemanager.h"
+#include "credresource.h"
+#include "securevirtualresourcetypes.h"
#include "srmtestcommon.h"
#include "srmutility.h"
-#include "logger.h"
-
-#define TAG PCF("SRM-CRED-UT")
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-//Declare Cred resource methods for testing
-OCStackResult CreateCredResource();
-OCEntityHandlerResult CredEntityHandler (OCEntityHandlerFlag flag,
- OCEntityHandlerRequest * ehRequest);
-char * BinToCredJSON(const OicSecCred_t * pstat);
-OicSecCred_t * JSONToCredBin(const char * jsonStr);
-void InitSecCredInstance(OicSecCred_t * cred);
-void DeleteCredList(OicSecCred_t* cred);
-const OicSecCred_t* GetCredResourceData(const OicUuid_t* subject);
-
-#ifdef __cplusplus
-}
-#endif
+#include "psinterface.h"
+#include "security_internals.h"
+#define TAG "SRM-CRED-UT"
OicSecCred_t * getCredList()
{
-
- OicSecCred_t * cred = NULL;
size_t sz = 0;
-
- cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
+ OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
VERIFY_NON_NULL(TAG, cred, ERROR);
cred->credId = 1234;
- OICStrcpy((char *)cred->subject.id, sizeof(cred->subject.id), "subject1");
+ OICStrcpy((char *)cred->subject.id, sizeof(cred->subject.id)+1, "1111111111111111");
#if 0
cred->roleIdsLen = 2;
#endif
- cred->credType = 1;
- cred->privateData.data = (char *)OICCalloc(1, strlen("My private Key11") + 1);
+ cred->credType = SYMMETRIC_PAIR_WISE_KEY;
+ cred->privateData.data = (uint8_t *)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));
- 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));
+ OICStrcpy((char *)cred->privateData.data, strlen("My private Key11")+1,"My private Key11");
+ OICStrcpy((char *)cred->rownerID.id, sizeof(cred->rownerID.id), "aaaaaaaaaaaaaaaa");
+ cred->next = (OicSecCred_t*)OICCalloc(1, sizeof(*cred->next));
VERIFY_NON_NULL(TAG, cred->next, ERROR);
cred->next->credId = 5678;
- OICStrcpy((char *)cred->next->subject.id, sizeof(cred->next->subject.id), "subject2");
+ OICStrcpy((char *)cred->next->subject.id, sizeof(cred->next->subject.id)+1, "2222222222222222");
#if 0
cred->next->roleIdsLen = 0;
#endif
- cred->next->credType = 1;
+ cred->next->credType = SYMMETRIC_PAIR_WISE_KEY;
sz = strlen("My private Key21") + 1;
- cred->next->privateData.data = (char *)OICCalloc(1, sz);
+ cred->next->privateData.data = (uint8_t *)OICCalloc(1, sz);
VERIFY_NON_NULL(TAG, cred->next->privateData.data, ERROR);
- OICStrcpy(cred->next->privateData.data, sz,"My private Key21");
+ OICStrcpy((char *)cred->next->privateData.data, sz, "My private Key21");
#if 0
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));
- 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");
+ OICStrcpy((char *)cred->next->rownerID.id, sizeof(cred->next->rownerID.id), "bbbbbbbbbbbbbbbb");
return cred;
EXPECT_TRUE(NULL != cred);
const OicSecCred_t *credTmp1 = NULL;
- for(credTmp1 = cred; credTmp1; credTmp1 = credTmp1->next)
+ for (credTmp1 = cred; credTmp1; credTmp1 = credTmp1->next)
{
- OC_LOG_V(INFO, TAG, PCF("\ncred->credId = %d"), credTmp1->credId);
- OC_LOG_V(INFO, TAG, PCF("cred->subject.id = %s"), credTmp1->subject.id);
- OC_LOG_V(INFO, TAG, PCF("cred->credType = %d"), credTmp1->credType);
+ OIC_LOG_V(INFO, TAG, "\ncred->credId = %d", credTmp1->credId);
+ OIC_LOG_V(INFO, TAG, "cred->subject.id = %s", credTmp1->subject.id);
+ OIC_LOG_V(INFO, TAG, "cred->credType = %d", credTmp1->credType);
if(credTmp1->privateData.data)
{
- OC_LOG_V(INFO, TAG, PCF("cred->privateData.data = %s"), credTmp1->privateData.data);
+ OIC_LOG_V(INFO, TAG, "cred->privateData.data = %s", credTmp1->privateData.data);
}
+#ifdef __WITH_X509__
if(credTmp1->publicData.data)
{
- OC_LOG_V(INFO, TAG, PCF("cred->publicData.data = %s"), credTmp1->publicData.data);
- }
- OC_LOG_V(INFO, TAG, PCF("cred->ownersLen = %zd"), credTmp1->ownersLen);
- for(size_t i = 0; i < cred->ownersLen; i++)
- {
- OC_LOG_V(INFO, TAG, PCF("cred->owners[%zd].id = %s"), i, credTmp1->owners[i].id);
+ OIC_LOG_V(INFO, TAG, "cred->publicData.data = %s", credTmp1->publicData.data);
}
+#endif /* __WITH_X509__ */
+ OIC_LOG_V(INFO, TAG, "cred->rownerID = %s", credTmp1->rownerID.id);
}
}
//InitCredResource Tests
-TEST(InitCredResourceTest, InitCredResource)
+TEST(CredResourceTest, InitCredResource)
{
EXPECT_EQ(OC_STACK_INVALID_PARAM, InitCredResource());
}
//DeInitCredResource Tests
-TEST(DeInitCredResourceTest, DeInitCredResource)
+TEST(CredResourceTest, DeInitCredResource)
{
EXPECT_EQ(OC_STACK_INVALID_PARAM, DeInitCredResource());
}
//CreateCredResource Tests
-TEST(CreateCredResourceTest, CreateCredResource)
+TEST(CredResourceTest, CreateCredResource)
{
EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateCredResource());
}
//CredEntityHandler Tests
-TEST(CredEntityHandlerTest, CredEntityHandlerWithDummyRequest)
+TEST(CredResourceTest, CredEntityHandlerWithDummyRequest)
{
- OCEntityHandlerRequest req;
+ OCEntityHandlerRequest req = OCEntityHandlerRequest();
EXPECT_EQ(OC_EH_ERROR,
- CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
+ CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req, NULL));
}
-TEST(CredEntityHandlerTest, CredEntityHandlerWithNULLRequest)
+TEST(CredResourceTest, CredEntityHandlerWithNULLRequest)
{
EXPECT_EQ(OC_EH_ERROR,
- CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
+ CredEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL, NULL));
}
-TEST(CredEntityHandlerTest, CredEntityHandlerInvalidFlag)
+TEST(CredResourceTest, CredEntityHandlerInvalidFlag)
{
- OCEntityHandlerRequest req;
+ OCEntityHandlerRequest req = OCEntityHandlerRequest();
EXPECT_EQ(OC_EH_ERROR,
- CredEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
+ CredEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req, NULL));
}
//Cred DELETE request
-TEST(CredEntityHandlerTest, CredEntityHandlerDeleteTest)
+TEST(CredResourceTest, CredEntityHandlerDeleteTest)
{
OCEntityHandlerRequest ehReq = OCEntityHandlerRequest();
static OCPersistentStorage ps = OCPersistentStorage();
const OicSecCred_t* subjectCred1 = NULL;
const OicSecCred_t* subjectCred2 = NULL;
- char *jsonStr = NULL;
OCEntityHandlerResult ehRet = OC_EH_ERROR;
- char query[] = "sub=c3ViamVjdDE=";
+ char query[] = "subjectuuid=31313131-3131-3131-3131-313131313131"; //canonical uuid of subject1
SetPersistentHandler(&ps, true);
OicSecCred_t *cred = getCredList();
- VERIFY_NON_NULL(TAG, cred, ERROR);
-
- jsonStr = BinToCredJSON(cred);
- VERIFY_NON_NULL(TAG, jsonStr, ERROR);
+ ASSERT_TRUE(NULL != cred);
+ uint8_t *payload = NULL;
+ size_t size = 0;
+ EXPECT_EQ(OC_STACK_OK, CredToCBORPayload(cred, &payload, &size));
+ if (!payload)
+ {
+ DeleteCredList(cred);
+ }
+ ASSERT_TRUE(NULL != payload);
// 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);
+ ehReq.payload = (OCPayload *)OCSecurityPayloadCBORCreate(payload, size);
+ if (!ehReq.payload)
+ {
+ OICFree(payload);
+ DeleteCredList(cred);
+ }
+ ASSERT_TRUE( NULL != ehReq.payload);
+ EXPECT_EQ(OC_EH_ERROR, CredEntityHandler(OC_REQUEST_FLAG, &ehReq, NULL));
// 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);
+ // Create Entity Handler DELETE request
+ ehReq.method = OC_REST_DELETE;
+ ehReq.query = (char *)OICCalloc(1, strlen(query)+1);
+ if (!ehReq.query)
+ {
+ OICFree(payload);
+ DeleteCredList(cred);
+ }
+ ASSERT_TRUE(NULL != ehReq.query);
+ OICStrcpy(ehReq.query, strlen(query)+1, query);
- ehRet = CredEntityHandler(OC_REQUEST_FLAG, &ehReq);
- EXPECT_TRUE(OC_EH_ERROR == ehRet);
+ ehRet = CredEntityHandler(OC_REQUEST_FLAG, &ehReq, NULL);
+ EXPECT_EQ(OC_EH_ERROR, ehRet);
- // Verify if SRM has deleted ACE for the subject
- subjectCred2 = GetCredResourceData(&cred->subject);
- EXPECT_TRUE(NULL == subjectCred2);
+ // 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);
- }
+ // Perform cleanup
+ OICFree(ehReq.query);
+ OICFree(payload);
+ DeInitCredResource();
+ DeleteCredList(cred);
+ OCPayloadDestroy((OCPayload *)ehReq.payload);
}
-//BinToCredJSON Tests
-TEST(BinToCredJSONTest, BinToCredJSONNullCred)
+TEST(CredResourceTest, CredToCBORPayloadNULL)
{
- char* value = BinToCredJSON(NULL);
- EXPECT_TRUE(value == NULL);
+ OicSecCred_t *cred = getCredList();
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CredToCBORPayload(NULL, NULL, 0));
+ size_t size = 0;
+ uint8_t *cborPayload = (uint8_t *) OICCalloc(1, 10);
+ if (!cborPayload)
+ {
+ DeleteCredList(cred);
+ }
+ ASSERT_TRUE(NULL != cborPayload);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CredToCBORPayload(cred, &cborPayload, &size));
+ OICFree(cborPayload);
+ cborPayload = NULL;
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CredToCBORPayload(NULL, &cborPayload, &size));
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CredToCBORPayload(cred, &cborPayload, 0));
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CredToCBORPayload(cred, NULL, &size));
+ DeleteCredList(cred);
}
-TEST(BinToCredJSONTest, BinToCredJSONValidCred)
+TEST(CredResourceTest, CredToCBORPayloadVALID)
{
- char* json = NULL;
- OicSecCred_t * cred = getCredList();
+ uint8_t* payload = NULL;
+ size_t size = 0;
+ OicSecCred_t *cred = getCredList();
- json = BinToCredJSON(cred);
+ EXPECT_EQ(OC_STACK_OK, CredToCBORPayload(cred, &payload, &size));
+ if (!payload)
+ {
+ DeleteCredList(cred);
+ }
+ ASSERT_TRUE(NULL != payload);
- OC_LOG_V(INFO, TAG, PCF("BinToCredJSON:%s\n"), json);
- EXPECT_TRUE(json != NULL);
DeleteCredList(cred);
- OICFree(json);
+ OICFree(payload);
}
-//JSONToCredBin Tests
-TEST(JSONToCredBinTest, JSONToCredBinValidJSON)
+TEST(CredResourceTest, CBORPayloadToCredVALID)
{
- OicSecCred_t* cred1 = getCredList();
- char* json = BinToCredJSON(cred1);
+ OicSecCred_t *cred1 = getCredList();
- EXPECT_TRUE(json != NULL);
- OicSecCred_t *cred2 = JSONToCredBin(json);
- EXPECT_TRUE(cred2 != NULL);
+ uint8_t *payload = NULL;
+ size_t size = 0;
+ EXPECT_EQ(OC_STACK_OK, CredToCBORPayload(cred1, &payload, &size));
DeleteCredList(cred1);
+ ASSERT_TRUE(NULL != payload);
+
+ OicSecCred_t *cred2 = NULL;
+ EXPECT_EQ(OC_STACK_OK, CBORPayloadToCred(payload, size, &cred2));
+ OICFree(payload);
+ ASSERT_TRUE(cred2 != NULL);
DeleteCredList(cred2);
- OICFree(json);
}
-TEST(JSONToCredBinTest, JSONToCredBinNullJSON)
+TEST(CredResourceTest, CBORPayloadToCredNULL)
{
- OicSecCred_t *cred = JSONToCredBin(NULL);
- EXPECT_TRUE(cred == NULL);
+ OicSecCred_t *cred = NULL;
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToCred(NULL, 0, NULL));
+ uint8_t *cborPayload = (uint8_t *) OICCalloc(1, 10);
+ ASSERT_TRUE(NULL != cborPayload);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToCred(NULL, 0, &cred));
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToCred(cborPayload, 0, NULL));
+ cred = getCredList();
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToCred(cborPayload, 0, &cred));
+ DeleteCredList(cred);
+ OICFree(cborPayload);
}
//GetCredResourceData Test
-TEST(CredGetResourceDataTest, GetCredResourceDataNULLSubject)
+TEST(CredResourceTest, GetCredResourceDataNULLSubject)
{
- EXPECT_TRUE(NULL == GetCredResourceData(NULL));
+ EXPECT_EQ(NULL, GetCredResourceData(NULL));
}
-TEST(CredGenerateCredentialTest, GenerateCredentialValidInput)
+TEST(CredResourceTest, GenerateCredentialValidInput)
{
- OicUuid_t owners[1];
- OICStrcpy((char *)owners[0].id, sizeof(owners[0].id), "ownersId21");
+ OicUuid_t rownerID = {{0}};
+ OICStrcpy((char *)rownerID.id, strlen("ownersId21"), "ownersId21");
- OicUuid_t subject = {0};
- OICStrcpy((char *)subject.id, sizeof(subject.id), "subject11");
+ OicUuid_t subject = {{0}};
+ OICStrcpy((char *)subject.id, strlen("subject11"), "subject11");
- char privateKey[] = "My private Key11";
+ uint8_t privateKey[] = "My private Key11";
+ OicSecKey_t key = {privateKey, sizeof(privateKey)};
OicSecCred_t * cred = NULL;
-
cred = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
- privateKey, 1, owners);
+ &key, &rownerID);
printCred(cred);
- EXPECT_TRUE(NULL != cred);
+ ASSERT_TRUE(NULL != cred);
DeleteCredList(cred);
}
-TEST(GenerateAndAddCredentialTest, GenerateAndAddCredentialValidInput)
+TEST(CredResourceTest, GenerateAndAddCredentialValidInput)
{
- OicUuid_t owners[1];
- OICStrcpy((char *)owners[0].id, sizeof(owners[0].id), "ownersId11");
+ OicUuid_t rownerID = {{0}};
+ OICStrcpy((char *)rownerID.id, sizeof(rownerID.id), "ownersId11");
- OicUuid_t subject = {0};
+ OicUuid_t subject = {{0}};
OICStrcpy((char *)subject.id, sizeof(subject.id), "subject11");
- char privateKey[] = "My private Key11";
+ uint8_t privateKey[] = "My private Key11";
+ OicSecKey_t key = {privateKey, sizeof(privateKey)};
- OicSecCred_t * cred1 = NULL;
- OicSecCred_t * headCred = NULL;
+ OicSecCred_t *cred1 = NULL;
+ OicSecCred_t *headCred = NULL;
cred1 = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
- privateKey, 1, owners);
+ &key, &rownerID);
- EXPECT_EQ(OC_STACK_ERROR, AddCredential(cred1));
+ EXPECT_EQ(OC_STACK_OK, AddCredential(cred1));
headCred = cred1;
- OICStrcpy((char *)owners[0].id, sizeof(owners[0].id), "ownersId22");
+ OICStrcpy((char *)rownerID.id, sizeof(rownerID.id), "ownersId22");
OICStrcpy((char *)subject.id, sizeof(subject.id), "subject22");
cred1 = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
- privateKey, 1, owners);
- EXPECT_EQ(OC_STACK_ERROR, AddCredential(cred1));
+ &key, &rownerID);
+ EXPECT_EQ(OC_STACK_OK, AddCredential(cred1));
- OICStrcpy((char *)owners[0].id, sizeof(owners[0].id), "ownersId33");
+ OICStrcpy((char *)rownerID.id, sizeof(rownerID.id), "ownersId33");
OICStrcpy((char *)subject.id, sizeof(subject.id), "subject33");
cred1 = GenerateCredential(&subject, SYMMETRIC_PAIR_WISE_KEY, NULL,
- privateKey, 1, owners);
- EXPECT_EQ(OC_STACK_ERROR, AddCredential(cred1));
+ &key, &rownerID);
+ EXPECT_EQ(OC_STACK_OK, AddCredential(cred1));
const OicSecCred_t* credList = GetCredResourceData(&headCred->subject);
printCred(credList);
DeleteCredList(headCred);
-
}
#if 0
EXPECT_TRUE(NULL != GetCredResourceData(cred->subject));
}
#endif
-
-