Example #1
0
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);
    }
}
Example #2
0
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 of default ACL provisioning.
 *
 * @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 ProvisionDefaultACLCB(void *ctx, OCDoHandle UNUSED,
                                                       OCClientResponse *clientResponse)
{
    OC_LOG_V(INFO, TAG, "IN ProvisionDefaultACLCB.");

    VERIFY_NON_NULL(TAG, clientResponse, ERROR);
    VERIFY_NON_NULL(TAG, ctx, ERROR);

    OTMContext_t* otmCtx = (OTMContext_t*) ctx;
    (void)UNUSED;

    if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
    {
        OC_LOG_V(INFO, TAG, "Staring commit hash task.");
        // TODO hash currently have fixed value 0.
        uint16_t aclHash = 0;
        otmCtx->selectedDeviceInfo->pstat->commitHash = aclHash;
        otmCtx->selectedDeviceInfo->pstat->tm = NORMAL;
        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 = BinToPstatJSON(otmCtx->selectedDeviceInfo->pstat);
        if (NULL == secPayload->securityData)
        {
            OICFree(secPayload);
            SetResult(otmCtx, OC_STACK_INVALID_JSON);
            return OC_STACK_DELETE_TRANSACTION;
        }
        OC_LOG_V(INFO, TAG, "Created payload for commit hash: %s",secPayload->securityData);

        char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
        if(!PMGenerateQuery(true,
                            otmCtx->selectedDeviceInfo->endpoint.addr,
                            otmCtx->selectedDeviceInfo->securePort,
                            otmCtx->selectedDeviceInfo->connType,
                            query, sizeof(query), OIC_RSRC_PSTAT_URI))
        {
            OC_LOG(ERROR, TAG, "ProvisionDefaultACLCB : 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 = &FinalizeProvisioningCB;
        cbData.context = (void*)otmCtx;
        cbData.cd = NULL;
        OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
                otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
        OC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
        if (ret != OC_STACK_OK)
        {
            OC_LOG(ERROR, TAG, "OCStack resource error");
            SetResult(otmCtx, ret);
        }
    }
static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx,
                                    OicSecDpom_t selectedOperationMode)
{
    OC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");

    if(!otmCtx || !otmCtx->selectedDeviceInfo)
    {
        return OC_STACK_INVALID_PARAM;
    }

    OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
    char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
    if(!PMGenerateQuery(false,
                        deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
                        deviceInfo->connType,
                        query, sizeof(query), OIC_RSRC_PSTAT_URI))
    {
        OC_LOG(ERROR, TAG, "PutUpdateOperationMode : Failed to generate query");
        return OC_STACK_ERROR;
    }
    OC_LOG_V(DEBUG, TAG, "Query=%s", query);

    deviceInfo->pstat->om = selectedOperationMode;

    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 = BinToPstatJSON(deviceInfo->pstat);
    if (NULL == secPayload->securityData)
    {
        OICFree(secPayload);
        OC_LOG(ERROR, TAG, "Error while converting pstat bin to json");
        return OC_STACK_INVALID_PARAM;
    }

    OCCallbackData cbData;
    cbData.cb = &OperationModeUpdateHandler;
    cbData.context = (void *)otmCtx;
    cbData.cd = NULL;
    OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
                                     deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
    if (res != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack resource error");
    }

    OC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");

    return res;
}
Example #5
0
/**
 * The entity handler determines how to process a GET request.
 */
static OCEntityHandlerResult HandlePstatGetRequest (const OCEntityHandlerRequest * ehRequest)
{
    // Convert ACL data into JSON for transmission
    char* jsonStr = BinToPstatJSON(gPstat);

    // A device should always have a default pstat. Therefore, jsonStr should never be NULL.
    OCEntityHandlerResult ehRet = (jsonStr ? OC_EH_OK : OC_EH_ERROR);

    // Send response payload to request originator
    SendSRMResponse(ehRequest, ehRet, jsonStr);
    OICFree(jsonStr);
    return ehRet;
}
Example #6
0
/**
 * Function to update persistent storage
 */
static bool UpdatePersistentStorage(OicSecPstat_t * pstat)
{
    bool bRet = false;

    if (pstat)
    {
        // Convert pstat data into JSON for update to persistent storage
        char *jsonStr = BinToPstatJSON(pstat);
        if (jsonStr)
        {
            cJSON *jsonPstat = cJSON_Parse(jsonStr);
            OICFree(jsonStr);

            if (jsonPstat &&
                (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_PSTAT_NAME, jsonPstat)))
            {
                bRet = true;
            }
            cJSON_Delete(jsonPstat);
        }
    }

    return bRet;
}
Example #7
0
/**
 * The entity handler determines how to process a POST request.
 * Per the REST paradigm, POST can also be used to update representation of existing
 * resource or create a new resource.
 * For pstat, it updates only tm and om.
 */
static OCEntityHandlerResult HandlePstatPutRequest(const OCEntityHandlerRequest *ehRequest)
{
    OCEntityHandlerResult ehRet = OC_EH_ERROR;
    cJSON *postJson = NULL;
    OC_LOG (INFO, TAG, "HandlePstatPutRequest  processing PUT request");

    if (ehRequest->resource)
    {
        postJson = cJSON_Parse(((OCSecurityPayload*)ehRequest->payload)->securityData);
        VERIFY_NON_NULL(TAG, postJson, INFO);

        cJSON *commitHashJson = cJSON_GetObjectItem(postJson, OIC_JSON_COMMIT_HASH_NAME);
        uint16_t commitHash = 0;
        if (commitHashJson)
        {
            commitHash = commitHashJson->valueint;
        }
        cJSON *tmJson = cJSON_GetObjectItem(postJson, OIC_JSON_TM_NAME);
        if (tmJson && gPstat)
        {
            gPstat->tm = (OicSecDpm_t)tmJson->valueint;
            if(0 == tmJson->valueint && gPstat->commitHash == commitHash)
            {
                gPstat->isOp = true;
                gPstat->cm = NORMAL;
                OC_LOG (INFO, TAG, "CommitHash is valid and isOp is TRUE");
            }
            else
            {
                OC_LOG (INFO, TAG, "CommitHash is not valid");
            }
        }
        cJSON *omJson = cJSON_GetObjectItem(postJson, OIC_JSON_OM_NAME);
        if (omJson && gPstat)
        {
            /*
             * Check if the operation mode is in the supported provisioning services
             * operation mode list.
             */
            for(size_t i=0; i< gPstat->smLen; i++)
            {
                if(gPstat->sm[i] == (unsigned int)omJson->valueint)
                {
                    gPstat->om = (OicSecDpom_t)omJson->valueint;
                    break;
                }
            }
        }
        // Convert pstat data into JSON for update to persistent storage
        char *jsonStr = BinToPstatJSON(gPstat, true);
        if (jsonStr)
        {
            cJSON *jsonPstat = cJSON_Parse(jsonStr);
            OICFree(jsonStr);
            if (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_PSTAT_NAME, jsonPstat))
            {
                ehRet = OC_EH_OK;
            }
        }
    }
 exit:
    //Send payload to request originator
    if(OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL))
    {
        OC_LOG (ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
    }
    cJSON_Delete(postJson);
    return ehRet;
}
Example #8
0
//BinToJSON Tests
TEST(BinToJSONTest, BinToNullJSON)
{
    char* value = BinToPstatJSON(NULL);
    EXPECT_TRUE(value == NULL);
}