/** * Initialize pstat resource by loading data from persistent storage. * * @retval OC_STACK_OK for Success, otherwise some error value */ OCStackResult InitPstatResource() { OCStackResult ret = OC_STACK_ERROR; // Read Pstat resource from PS char* jsonSVRDatabase = GetSVRDatabase(); if (jsonSVRDatabase) { // Convert JSON Pstat into binary format gPstat = JSONToPstatBin(jsonSVRDatabase, true); } /* * If SVR database in persistent storage got corrupted or * is not available for some reason, a default pstat is created * which allows user to initiate pstat provisioning again. */ if(!jsonSVRDatabase || !gPstat) { gPstat = GetPstatDefault(); } // Instantiate 'oic.sec.pstat' ret = CreatePstatResource(); OICFree(jsonSVRDatabase); return ret; }
TEST(PstatTests, JSONMarshalliingTests) { char *jsonStr1 = ReadFile(UNIT_TEST_JSON_FILE_NAME); if (NULL != jsonStr1) { cJSON_Minify(jsonStr1); /* Workaround : cJSON_Minify does not remove all the unwanted characters from the end. Here is an attempt to remove those characters */ int len = strlen(jsonStr1); while (len > 0) { if (jsonStr1[--len] == '}') { break; } } jsonStr1[len + 1] = 0; OicSecPstat_t* pstat = JSONToPstatBin(jsonStr1); EXPECT_TRUE(NULL != pstat); char* jsonStr2 = BinToPstatJSON(pstat); printf("BinToPstatJSON Dump:\n%s\n\n", jsonStr2); EXPECT_STRNE(jsonStr1, jsonStr2); OICFree(jsonStr1); OICFree(jsonStr2); OICFree(pstat); } else { printf("Please copy %s into unittest folder\n", UNIT_TEST_JSON_FILE_NAME); } }
TEST(MarshalingAndUnMarshalingTest, BinToPstatJSONAndJSONToPstatBin) { const char* id = "ZGV2aWNlaWQAAAAAABhanw=="; OicSecPstat_t pstat; pstat.cm = NORMAL; pstat.commitHash = 0; uint32_t outLen = 0; unsigned char base64Buff[sizeof(((OicUuid_t*) 0)->id)] = {}; EXPECT_EQ(B64_OK, b64Decode(id, strlen(id), base64Buff, sizeof(base64Buff), &outLen)); memcpy(pstat.deviceID.id, base64Buff, outLen); pstat.isOp = true; pstat.tm = NORMAL; pstat.om = SINGLE_SERVICE_CLIENT_DRIVEN; pstat.smLen = 2; pstat.sm = (OicSecDpom_t*)OICCalloc(pstat.smLen, sizeof(OicSecDpom_t)); pstat.sm[0] = SINGLE_SERVICE_CLIENT_DRIVEN; pstat.sm[1] = SINGLE_SERVICE_SERVER_DRIVEN; char* jsonPstat = BinToPstatJSON(&pstat); printf("BinToJSON Dump:\n%s\n\n", jsonPstat); EXPECT_TRUE(jsonPstat != NULL); OicSecPstat_t *pstat1 = JSONToPstatBin(jsonPstat); EXPECT_TRUE(pstat1 != NULL); OICFree(pstat1->sm); OICFree(pstat1); OICFree(jsonPstat); OICFree(pstat.sm); }
/** * Callback handler for ProvisioningStatusResouceHandler API. * * @param[in] ctx ctx value passed to callback from calling function. * @param[in] UNUSED handle to an invocation * @param[in] clientResponse Response from queries to remote servers. * @return OC_STACK_DELETE_TRANSACTION to delete the transaction * and OC_STACK_KEEP_TRANSACTION to keep it. */ static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED, OCClientResponse *clientResponse) { OC_LOG(DEBUG, TAG, "IN ListMethodsHandler"); VERIFY_NON_NULL(TAG, clientResponse, WARNING); VERIFY_NON_NULL(TAG, ctx, WARNING); OTMContext_t* otmCtx = (OTMContext_t*)ctx; (void)UNUSED; if (OC_STACK_OK == clientResponse->result) { if (NULL == clientResponse->payload) { OC_LOG(INFO, TAG, "Skiping Null payload"); SetResult(otmCtx, OC_STACK_ERROR); return OC_STACK_DELETE_TRANSACTION; } if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type) { OC_LOG(INFO, TAG, "Unknown payload type"); SetResult(otmCtx, OC_STACK_ERROR); return OC_STACK_DELETE_TRANSACTION; } OicSecPstat_t* pstat = JSONToPstatBin( ((OCSecurityPayload*)clientResponse->payload)->securityData); if(NULL == pstat) { OC_LOG(ERROR, TAG, "Error while converting json to pstat bin"); SetResult(otmCtx, OC_STACK_ERROR); return OC_STACK_DELETE_TRANSACTION; } otmCtx->selectedDeviceInfo->pstat = pstat; //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only) OicSecDpom_t selectedOperationMode; SelectOperationMode(otmCtx->selectedDeviceInfo, &selectedOperationMode); //Send request : PUT /oic/sec/pstat [{"OM":"0x11", .. }] OCStackResult res = PutUpdateOperationMode(otmCtx, selectedOperationMode); if (OC_STACK_OK != res) { OC_LOG(ERROR, TAG, "Error while updating operation mode."); SetResult(otmCtx, res); } } else { OC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d", clientResponse->result); SetResult(otmCtx, clientResponse->result); } OC_LOG(DEBUG, TAG, "OUT ListMethodsHandler"); exit: return OC_STACK_DELETE_TRANSACTION; }
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 ; }
TEST(JSONToBinTest, NullJSONToBin) { OicSecPstat_t *pstat1 = JSONToPstatBin(NULL); EXPECT_TRUE(pstat1 == NULL); }