TEST(CredResourceTest, CredToCBORPayloadNULL) { 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(CredResourceTest, CBORPayloadToCredVALID) { OicSecCred_t *cred1 = getCredList(); 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); }
TEST(CredResourceTest, CredToCBORPayloadVALID) { uint8_t* payload = NULL; size_t size = 0; OicSecCred_t *cred = getCredList(); EXPECT_EQ(OC_STACK_OK, CredToCBORPayload(cred, &payload, &size)); if (!payload) { DeleteCredList(cred); } ASSERT_TRUE(NULL != payload); DeleteCredList(cred); OICFree(payload); }
static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName) { char *jsonStr = NULL; FILE *fp = NULL; FILE *fp1 = NULL; uint8_t *aclCbor = NULL; uint8_t *pstatCbor = NULL; uint8_t *doxmCbor = NULL; uint8_t *amaclCbor = NULL; uint8_t *svcCbor = NULL; uint8_t *credCbor = NULL; cJSON *jsonRoot = NULL; OCStackResult ret = OC_STACK_ERROR; size_t size = GetJSONFileSize(jsonFileName); if (0 == size) { OIC_LOG (ERROR, TAG, "Failed converting to JSON"); return; } jsonStr = (char *)OICMalloc(size + 1); VERIFY_NON_NULL(TAG, jsonStr, FATAL); fp = fopen(jsonFileName, "r"); if (fp) { size_t bytesRead = fread(jsonStr, 1, size, fp); jsonStr[bytesRead] = '\0'; OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead); fclose(fp); fp = NULL; } else { OIC_LOG (ERROR, TAG, "Unable to open JSON file!!"); goto exit; } jsonRoot = cJSON_Parse(jsonStr); cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME); //printf("ACL json : \n%s\n", cJSON_PrintUnformatted(value)); size_t aclCborSize = 0; if (NULL != value) { OicSecAcl_t *acl = JSONToAclBin(jsonStr); VERIFY_NON_NULL(TAG, acl, FATAL); ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload"); DeleteACLList(acl); goto exit; } printf("ACL Cbor Size: %zd\n", aclCborSize); DeleteACLList(acl); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME); size_t pstatCborSize = 0; if (NULL != value) { OicSecPstat_t *pstat = JSONToPstatBin(jsonStr); VERIFY_NON_NULL(TAG, pstat, FATAL); ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload"); DeletePstatBinData(pstat); goto exit; } printf("PSTAT Cbor Size: %zd\n", pstatCborSize); DeletePstatBinData(pstat); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME); size_t doxmCborSize = 0; if (NULL != value) { OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr); VERIFY_NON_NULL(TAG, doxm, FATAL); ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload"); DeleteDoxmBinData(doxm); goto exit; } printf("DOXM Cbor Size: %zd\n", doxmCborSize); DeleteDoxmBinData(doxm); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME); size_t amaclCborSize = 0; if (NULL != value) { OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr); VERIFY_NON_NULL(TAG, amacl, FATAL); ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload"); DeleteAmaclList(amacl); goto exit; } printf("AMACL Cbor Size: %zd\n", amaclCborSize); DeleteAmaclList(amacl); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME); size_t svcCborSize = 0; if (NULL != value) { OicSecSvc_t *svc = JSONToSvcBin(jsonStr); VERIFY_NON_NULL(TAG, svc, FATAL); ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload"); DeleteSVCList(svc); goto exit; } printf("SVC Cbor Size: %zd\n", svcCborSize); DeleteSVCList(svc); } value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME); //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value)); size_t credCborSize = 0; int secureFlag = 0; if (NULL != value) { OicSecCred_t *cred = JSONToCredBin(jsonStr); VERIFY_NON_NULL(TAG, cred, FATAL); ret = CredToCBORPayload(cred, &credCbor, &credCborSize, secureFlag); if(OC_STACK_OK != ret) { OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload"); DeleteCredList(cred); goto exit; } printf("CRED Cbor Size: %zd\n", credCborSize); DeleteCredList(cred); } CborEncoder encoder; size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize; printf("Total Cbor Size : %zd\n", cborSize); cborSize += 255; // buffer margin for adding map and byte string uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize); VERIFY_NON_NULL(TAG, outPayload, ERROR); cbor_encoder_init(&encoder, outPayload, cborSize, 0); CborEncoder map; CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map."); if (aclCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name."); cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value."); } if (pstatCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name."); cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value."); } if (doxmCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name."); cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value."); } if (amaclCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name."); cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value."); } if (svcCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name."); cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value."); } if (credCborSize > 0) { cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME)); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name."); cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value."); } cborEncoderResult = cbor_encoder_close_container(&encoder, &map); VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container."); size_t s = encoder.ptr - outPayload; OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s); fp1 = fopen(cborFileName, "w"); if (fp1) { size_t bytesWritten = fwrite(outPayload, 1, s, fp1); if (bytesWritten == s) { OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten); } else { OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s); } fclose(fp1); fp1 = NULL; } exit: cJSON_Delete(jsonRoot); OICFree(aclCbor); OICFree(doxmCbor); OICFree(pstatCbor); OICFree(amaclCbor); OICFree(svcCbor); OICFree(credCbor); OICFree(jsonStr); return ; }
//Cred DELETE request TEST(CredResourceTest, CredEntityHandlerDeleteTest) { OCEntityHandlerRequest ehReq = OCEntityHandlerRequest(); static OCPersistentStorage ps = OCPersistentStorage(); const OicSecCred_t* subjectCred1 = NULL; const OicSecCred_t* subjectCred2 = NULL; OCEntityHandlerResult ehRet = OC_EH_ERROR; char query[] = "subjectuuid=31313131-3131-3131-3131-313131313131"; //canonical uuid of subject1 SetPersistentHandler(&ps, true); OicSecCred_t *cred = getCredList(); 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(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 *)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, NULL); EXPECT_EQ(OC_EH_ERROR, ehRet); // Verify if SRM has deleted ACE for the subject subjectCred2 = GetCredResourceData(&cred->subject); EXPECT_TRUE(NULL == subjectCred2); // Perform cleanup OICFree(ehReq.query); OICFree(payload); DeInitCredResource(); DeleteCredList(cred); OCPayloadDestroy((OCPayload *)ehReq.payload); }