diff --git a/src/crud_internal.c b/src/crud_internal.c index 204d14ec..badb1671 100644 --- a/src/crud_internal.c +++ b/src/crud_internal.c @@ -174,8 +174,8 @@ int createObject( wrp_msg_t *reqMsg , wrp_msg_t **response) ParodusInfo( "Number of object level %d\n", objlevel ); - /* Valid request will be mac:14cfexxxx/parodus/tags/${name} which is objlevel 4 */ - if(objlevel == 4) + /* Valid request will be mac:14cfexxxx/parodus/tag/${name} which is objlevel 4 */ + if(objlevel == 4 && ((obj[2] != NULL) && (strcmp(obj[2] , "parodus") == 0) ) && ((obj[3] != NULL) &&(strcmp(obj[3] , "tag") == 0 ))) { if(reqMsg->u.crud.payload != NULL) { @@ -313,7 +313,7 @@ int createObject( wrp_msg_t *reqMsg , wrp_msg_t **response) } else { - // Return error for request format other than /tag/${name} + // Return error for request format other than parodus/tag/${name} ParodusError("Invalid CREATE request\n"); (*response)->u.crud.status = 400; freeObjArray(&obj, objlevel); @@ -584,38 +584,51 @@ int retrieveObject( wrp_msg_t *reqMsg, wrp_msg_t **response ) } else { - //To traverse through total number of test objects in json - for( i = 0 ; i < itemSize ; i++ ) + if ((obj[3] !=NULL) && (strcmp(obj[3] , "tag") == 0)) { - cJSON* subitem = cJSON_GetArrayItem( paramArray, i ); - if( strcmp( cJSON_GetArrayItem( paramArray, i )->string, obj[objlevel] ) == 0 ) + //To traverse through total number of test objects in json + for( i = 0 ; i < itemSize ; i++ ) { - //retrieve test object value - ParodusPrint( " %s : %d \n", cJSON_GetArrayItem( subitem, 0)->string, cJSON_GetArrayItem( subitem, 0 )->valueint ); - cJSON_AddItemToObject( jsonresponse, cJSON_GetArrayItem( subitem, 0 )->string , cJSON_CreateNumber(cJSON_GetArrayItem( subitem, 0 )->valueint)); - ParodusInfo("Retrieve: requested object found \n"); - found = 1; - break; + cJSON* subitem = cJSON_GetArrayItem( paramArray, i ); + if( strcmp( cJSON_GetArrayItem( paramArray, i )->string, obj[objlevel] ) == 0 ) + { + //retrieve test object value + ParodusPrint( " %s : %d \n", cJSON_GetArrayItem( subitem, 0)->string, cJSON_GetArrayItem( subitem, 0 )->valueint ); + cJSON_AddItemToObject( jsonresponse, cJSON_GetArrayItem( subitem, 0 )->string , cJSON_CreateNumber(cJSON_GetArrayItem( subitem, 0 )->valueint)); + ParodusInfo("Retrieve: requested object found \n"); + found = 1; + break; + } + else + { + ParodusPrint("retrieve object not found, traversing\n"); + } } - else + if(!found) { - ParodusPrint("retrieve object not found, traversing\n"); + ParodusError("Unable to retrieve requested object\n"); + (*response)->u.crud.status = 400; + cJSON_Delete( jsonresponse ); + cJSON_Delete( json ); + freeObjArray(&obj, objlevel); + return -1; } + + char *str1 = cJSON_PrintUnformatted( jsonresponse ); + ParodusInfo( "jsonResponse %s\n", str1 ); + (*response)->u.crud.status = 200; + (*response)->u.crud.payload = str1; } - if(!found) + else { - ParodusError("Unable to retrieve requested object\n"); + // Return error for request format other than parodus/tag/${name} + ParodusError("Invalid RETRIEVE request\n"); (*response)->u.crud.status = 400; cJSON_Delete( jsonresponse ); cJSON_Delete( json ); freeObjArray(&obj, objlevel); return -1; } - - char *str1 = cJSON_PrintUnformatted( jsonresponse ); - ParodusInfo( "jsonResponse %s\n", str1 ); - (*response)->u.crud.status = 200; - (*response)->u.crud.payload = str1; } } } @@ -727,8 +740,8 @@ int updateObject( wrp_msg_t *reqMsg, wrp_msg_t **response ) ParodusInfo( "Number of object level %d\n", objlevel ); - /* Valid request will be mac:14cfexxxx/parodus/tags/${name} which is objlevel 4 */ - if(objlevel == 4) + /* Valid request will be mac:14cfexxxx/parodus/tag/${name} which is objlevel 4 */ + if(objlevel == 4 && ((obj[2] != NULL) && (strcmp(obj[2] , "parodus") == 0) ) && ((obj[3] != NULL) &&(strcmp(obj[3] , "tag") == 0 ))) { if(reqMsg->u.crud.payload != NULL) { @@ -847,7 +860,7 @@ int updateObject( wrp_msg_t *reqMsg, wrp_msg_t **response ) } else { - // Return error for request format other than /tag/${name} + // Return error for request format other than parodus/tag/${name} ParodusError("Invalid UPDATE request\n"); (*response)->u.crud.status = 400; freeObjArray(&obj, objlevel); diff --git a/tests/test_crud_internal.c b/tests/test_crud_internal.c index fbfb81d2..d8e1fd62 100644 --- a/tests/test_crud_internal.c +++ b/tests/test_crud_internal.c @@ -212,7 +212,7 @@ void test_createObjectInvalidReq() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag"); respMsg->msg_type = 5; ret = createObject(reqMsg, &respMsg); assert_int_equal (respMsg->u.crud.status, 400); @@ -253,7 +253,7 @@ void test_createObjectInvalid_JsonEmpty() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag"); respMsg->msg_type = 5; ret = createObject(reqMsg, &respMsg); @@ -300,7 +300,7 @@ void test_createObjectInvalid_JsonNonEmpty() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag"); respMsg->msg_type = 5; ret = createObject(reqMsg, &respMsg); @@ -347,7 +347,7 @@ void test_createObjectInvalid_JsonParseErr() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag"); respMsg->msg_type = 5; ret = createObject(reqMsg, &respMsg); @@ -430,7 +430,7 @@ void test_createObject_JsonParse() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test"); respMsg->msg_type = 5; ret = createObject(reqMsg, &respMsg); @@ -525,7 +525,7 @@ void test_createObject_withProperPayload() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 1522451870 }"); respMsg->msg_type = 5; @@ -574,7 +574,7 @@ void test_createObject_withWrongPayload() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 1522451"); respMsg->msg_type = 5; @@ -623,7 +623,7 @@ void test_createObject_multipleObjects() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test2"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test2"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 123 }"); respMsg->msg_type = 5; @@ -672,7 +672,7 @@ void test_createObject_existingObj() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test1"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test1"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 1522451}"); respMsg->msg_type = 5; @@ -728,7 +728,7 @@ void test_createObject_jsonFailure() reqMsg->msg_type = 5; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test1"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test1"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 152}"); respMsg->msg_type = 5; @@ -740,6 +740,49 @@ void test_createObject_jsonFailure() } +void test_createObject_invalid() +{ + int ret = 0; + int write_ret = -1; + FILE *fp; + char *testdata = NULL; + + wrp_msg_t *reqMsg = NULL; + reqMsg = ( wrp_msg_t *)malloc( sizeof( wrp_msg_t ) ); + memset(reqMsg, 0, sizeof(wrp_msg_t)); + wrp_msg_t *respMsg = NULL; + respMsg = ( wrp_msg_t *)malloc( sizeof( wrp_msg_t ) ); + memset(respMsg, 0, sizeof(wrp_msg_t)); + + ParodusCfg cfg; + memset(&cfg,0,sizeof(cfg)); + cfg.crud_config_file = strdup("parodus_cfg.json"); + set_parodus_cfg(&cfg); + testdata=strdup("{ \"expires\" : 1522451870 }"); + write_ret = writeToJSON(testdata); + assert_int_equal (write_ret, 1); + reqMsg->msg_type = 5; + reqMsg->u.crud.transaction_uuid = strdup("1234"); + reqMsg->u.crud.source = strdup("tag-update"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test"); + reqMsg->u.crud.payload = strdup("{ \"expires\" : 1522451}"); + respMsg->msg_type = 5; + ret = createObject(reqMsg, &respMsg); + assert_int_equal (respMsg->u.crud.status, 400); + assert_int_equal (ret, -1); + + fp = fopen(cfg.crud_config_file, "r"); + if (fp != NULL) + { + system("rm parodus_cfg.json"); + fclose(fp); + } + if(cfg.crud_config_file !=NULL) + free(cfg.crud_config_file); + wrp_free_struct(reqMsg); + wrp_free_struct(respMsg); +} + void test_retrieveObject_JsonEmpty() { int ret = 0; @@ -964,7 +1007,7 @@ void test_retrieveObject_withTagsEmpty() reqMsg->msg_type = 6; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag"); respMsg->msg_type = 6; ret = retrieveObject(reqMsg, &respMsg); assert_int_equal (respMsg->u.crud.status, 400); @@ -1005,7 +1048,7 @@ void test_retrieveObject_testObj() reqMsg->msg_type = 6; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test1"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test1"); respMsg->msg_type = 6; ret = retrieveObject(reqMsg, &respMsg); assert_int_equal (respMsg->u.crud.status, 200); @@ -1046,7 +1089,7 @@ void test_retrieveObject_nonexistObj() reqMsg->msg_type = 6; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test"); respMsg->msg_type = 6; ret = retrieveObject(reqMsg, &respMsg); assert_int_equal (respMsg->u.crud.status, 400); @@ -1106,6 +1149,49 @@ void test_retrieveObject_tagsFailure() } +void test_retrieveObject_invalid() +{ + int ret = 0; + int write_ret = -1; + FILE *fp; + char *testdata = NULL; + wrp_msg_t *reqMsg = NULL; + reqMsg = ( wrp_msg_t *)malloc( sizeof( wrp_msg_t ) ); + memset(reqMsg, 0, sizeof(wrp_msg_t)); + wrp_msg_t *respMsg = NULL; + respMsg = ( wrp_msg_t *)malloc( sizeof( wrp_msg_t ) ); + memset(respMsg, 0, sizeof(wrp_msg_t)); + ParodusCfg cfg; + memset(&cfg,0,sizeof(cfg)); + cfg.crud_config_file = strdup("parodus_cfg.json"); + set_parodus_cfg(&cfg); + //testdata=strdup("{\"test\":{}}}"); + testdata=strdup("{\"tags\":{\"test1\":{\"expires\":1522451870}}}"); + write_ret = writeToJSON(testdata); + assert_int_equal (write_ret, 1); + reqMsg->msg_type = 6; + reqMsg->u.crud.transaction_uuid = strdup("1234"); + reqMsg->u.crud.source = strdup("tag-update"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test1"); + respMsg->msg_type = 6; + ret = retrieveObject(reqMsg, &respMsg); + assert_int_equal (respMsg->u.crud.status, 400); + assert_int_equal (ret, -1); + fp = fopen(cfg.crud_config_file, "r"); + if (fp != NULL) + { + system("rm parodus_cfg.json"); + fclose(fp); + } + if(cfg.crud_config_file !=NULL) + free(cfg.crud_config_file); + + wrp_free_struct(reqMsg); + wrp_free_struct(respMsg); + +} + + void test_retrieveObject_readOnlyObj() { int ret = 0; @@ -1212,7 +1298,7 @@ void test_updateObject_JsonEmpty() reqMsg->msg_type = 7; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 1522451}"); respMsg->msg_type = 7; ret = updateObject(reqMsg, &respMsg); @@ -1294,7 +1380,7 @@ void test_updateObjectWithNoConfigJson() reqMsg->msg_type = 7; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test1"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test1"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 1522451}"); respMsg->msg_type = 7; ret = updateObject(reqMsg, &respMsg); @@ -1372,7 +1458,7 @@ void test_updateObject_existingObj() reqMsg->msg_type = 7; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test1"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test1"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 152}"); respMsg->msg_type = 7; ret = updateObject(reqMsg, &respMsg); @@ -1414,7 +1500,7 @@ void test_updateObject_NonExistingObj() reqMsg->msg_type = 7; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test2"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test2"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 15256}"); respMsg->msg_type = 7; ret = updateObject(reqMsg, &respMsg); @@ -1456,7 +1542,7 @@ void test_updateObject_withWrongPayload() reqMsg->msg_type = 7; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 1522451"); respMsg->msg_type = 7; @@ -1551,7 +1637,7 @@ void test_updateObject_jsonFailure() reqMsg->msg_type = 7; reqMsg->u.crud.transaction_uuid = strdup("1234"); reqMsg->u.crud.source = strdup("tag-update"); - reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test1"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tag/test1"); reqMsg->u.crud.payload = strdup("{ \"expires\" : 152}"); respMsg->msg_type = 7; @@ -1563,6 +1649,51 @@ void test_updateObject_jsonFailure() } +void test_updateObject_invalid() +{ + int ret = 0; + int write_ret = -1; + FILE *fp; + char *testdata = NULL; + + wrp_msg_t *reqMsg = NULL; + reqMsg = ( wrp_msg_t *)malloc( sizeof( wrp_msg_t ) ); + memset(reqMsg, 0, sizeof(wrp_msg_t)); + wrp_msg_t *respMsg = NULL; + respMsg = ( wrp_msg_t *)malloc( sizeof( wrp_msg_t ) ); + memset(respMsg, 0, sizeof(wrp_msg_t)); + ParodusCfg cfg; + memset(&cfg,0,sizeof(cfg)); + cfg.crud_config_file = strdup("parodus_cfg.json"); + set_parodus_cfg(&cfg); + testdata=strdup("{ \"expires\" : 1522451870 }"); + write_ret = writeToJSON(testdata); + assert_int_equal (write_ret, 1); + reqMsg->msg_type = 7; + reqMsg->u.crud.transaction_uuid = strdup("1234"); + reqMsg->u.crud.source = strdup("tag-update"); + reqMsg->u.crud.dest = strdup("mac:14xxx/parodus/tags/test"); + reqMsg->u.crud.payload = strdup("{ \"expires\" : 1522451}"); + + respMsg->msg_type = 7; + ret = updateObject(reqMsg, &respMsg); + + assert_int_equal (respMsg->u.crud.status, 400); + assert_int_equal (ret, -1); + + fp = fopen(cfg.crud_config_file, "r"); + if (fp != NULL) + { + system("rm parodus_cfg.json"); + fclose(fp); + } + if(cfg.crud_config_file !=NULL) + free(cfg.crud_config_file); + + wrp_free_struct(reqMsg); + wrp_free_struct(respMsg); +} + void test_deleteObject_JsonEmpty() { int ret = 0; @@ -1955,6 +2086,7 @@ int main(void) cmocka_unit_test(test_createObject_multipleObjects), cmocka_unit_test(test_createObject_existingObj), cmocka_unit_test(test_createObject_jsonFailure), + cmocka_unit_test(test_createObject_invalid), cmocka_unit_test(test_retrieveObject_JsonEmpty), cmocka_unit_test(test_retrieveObject_destNull), @@ -1965,6 +2097,7 @@ int main(void) cmocka_unit_test(test_retrieveObject_testObj), cmocka_unit_test(test_retrieveObject_nonexistObj), cmocka_unit_test(test_retrieveObject_tagsFailure), + cmocka_unit_test(test_retrieveObject_invalid), cmocka_unit_test(test_retrieveObject_readOnlyObj), cmocka_unit_test(test_retrieveObject_readOnlyFailure), @@ -1977,6 +2110,7 @@ int main(void) cmocka_unit_test(test_updateObject_withWrongPayload), cmocka_unit_test(test_updateObject_NullPayload), cmocka_unit_test(test_updateObject_jsonFailure), + cmocka_unit_test(test_updateObject_invalid), cmocka_unit_test(test_deleteObject_JsonEmpty), cmocka_unit_test(test_deleteObject_destNull),