//JSONToCredBin Tests TEST(JSONToCredBinTest, JSONToCredBinValidJSON) { OicSecCred_t* cred1 = getCredList(); char* json = BinToCredJSON(cred1); EXPECT_TRUE(json != NULL); OicSecCred_t *cred2 = JSONToCredBin(json); EXPECT_TRUE(cred2 != NULL); DeleteCredList(cred1); DeleteCredList(cred2); OICFree(json); }
TEST(BinToCredJSONTest, BinToCredJSONValidCred) { char* json = NULL; OicSecCred_t * cred = getCredList(); json = BinToCredJSON(cred); OIC_LOG_V(INFO, TAG, "BinToCredJSON:%s\n", json); EXPECT_TRUE(json != NULL); DeleteCredList(cred); OICFree(json); }
//Cred DELETE request TEST(CredEntityHandlerTest, 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="; 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); } }
/** * Internal function for handling credential generation and sending credential to resource server. * * @param[in] cred Instance of cred resource. * @param[in] deviceInfo information about device to which credential is to be provisioned. * @param[in] responseHandler callbak called by OC stack when request API receives response. * @return OC_STACK_OK in case of success and other value otherwise. */ static OCStackResult provisionCredentials(const OicSecCred_t *cred, const OCProvisionDev_t *deviceInfo, CredentialData_t *credData, OCClientResponseHandler responseHandler) { OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload)); if(!secPayload) { OC_LOG(ERROR, TAG, "Failed to memory allocation"); return OC_STACK_NO_MEMORY; } secPayload->base.type = PAYLOAD_TYPE_SECURITY; secPayload->securityData = BinToCredJSON(cred); if(NULL == secPayload->securityData) { OICFree(secPayload); OC_LOG(ERROR, TAG, "Failed to BinToCredJSON"); return OC_STACK_NO_MEMORY; } OC_LOG_V(INFO, TAG, "Credential for provisioning : %s",secPayload->securityData); char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0}; if(!PMGenerateQuery(true, deviceInfo->endpoint.addr, deviceInfo->securePort, deviceInfo->connType, query, sizeof(query), OIC_RSRC_CRED_URI)) { OC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query"); return OC_STACK_ERROR; } OC_LOG_V(DEBUG, TAG, "Query=%s", query); OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL}; cbData.cb = responseHandler; cbData.context = (void *) credData; cbData.cd = NULL; OCDoHandle handle = NULL; OCMethod method = OC_REST_POST; OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload, deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0); OC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret); if (ret != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack resource error"); return ret; } return OC_STACK_OK; }
//BinToCredJSON Tests TEST(BinToCredJSONTest, BinToCredJSONNullCred) { char* value = BinToCredJSON(NULL); EXPECT_TRUE(value == NULL); }