Skip to content

Commit

Permalink
webcfg_http_request unit test using cmocka
Browse files Browse the repository at this point in the history
  • Loading branch information
suvi-123 committed Nov 9, 2023
1 parent f6f8adb commit a54d57f
Show file tree
Hide file tree
Showing 5 changed files with 632 additions and 67 deletions.
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ add_library(liblibparodus STATIC SHARED IMPORTED)
add_dependencies(liblibparodus libparodus)
endif (FEATURE_SUPPORT_AKER)

if (BUILD_TESTING)
# cmocka external dependency
#-------------------------------------------------------------------------------
ExternalProject_Add(cmocka
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/_prefix/cmocka
GIT_REPOSITORY https://github.com/elventear/cmocka.git
GIT_TAG "b71a3060699bc1a5b00e958be353772f42545ac2"
CMAKE_ARGS += -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR}
)
add_library(libcmocka STATIC SHARED IMPORTED)
add_dependencies(libcmocka cmocka)

endif (BUILD_TESTING)

endif ()
link_directories ( ${LIBRARY_DIR} ${COMMON_LIBRARY_DIR} ${LIBRARY_DIR64} )

Expand Down
1 change: 1 addition & 0 deletions src/webcfg_multipart.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ int get_global_eventFlag(void);
void set_global_eventFlag();
void set_global_ETAG(char *etag);
char *get_global_ETAG(void);
WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id);
#ifdef WAN_FAILOVER_SUPPORTED
void set_global_interface(char * value);
char * get_global_interface(void);
Expand Down
49 changes: 46 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ target_link_libraries (test_webcfgdb gcov -Wl,--no-as-needed )
#-------------------------------------------------------------------------------
add_test(NAME test_multipart_unittest COMMAND ${MEMORY_CHECK} ./test_multipart_unittest)

set(SOURCES test_multipart_unittest.c ../src/webcfg_param.c ../src/webcfg_multipart.c ../src/webcfg_helpers.c ../src/webcfg.c ../src/webcfg_auth.c ../src/webcfg_notify.c ../src/webcfg_db.c ../src/webcfg_pack.c ../src/webcfg_blob.c ../src/webcfg_event.c ../src/webcfg_generic_pc.c ../src/webcfg_metadata.c)
set(SOURCES test_multipart_unittest.c ../src/webcfg_param.c ../src/webcfg_multipart.c ../src/webcfg_helpers.c ../src/webcfg.c ../src/webcfg_auth.c ../src/webcfg_notify.c ../src/webcfg_db.c ../src/webcfg_pack.c ../src/webcfg_blob.c ../src/webcfg_event.c ../src/webcfg_generic_pc.c ../src/webcfg_metadata.c ../src/webcfg_timer.c)

if (WEBCONFIG_BIN_SUPPORT)
set(SOURCES ${SOURCES} ../src/webcfg_rbus.c)
Expand Down Expand Up @@ -343,6 +343,44 @@ endif (WEBCONFIG_BIN_SUPPORT)

target_link_libraries (test_generic_pc gcov -Wl,--no-as-needed )

#-------------------------------------------------------------------------------
# test_blob
#-------------------------------------------------------------------------------
add_test(NAME test_blob COMMAND ${MEMORY_CHECK} ./test_blob)
add_executable(test_blob test_blob.c ../src/webcfg_blob.c)
target_link_libraries (test_blob -lcunit -lmsgpackc -lcimplog -lcjson -ltrower-base64 )

target_link_libraries (test_blob gcov -Wl,--no-as-needed )

#-------------------------------------------------------------------------------
# test_cmoka_multipart.c
#-------------------------------------------------------------------------------
add_test(NAME test_cmocka_multipart COMMAND ${MEMORY_CHECK} ./test_cmocka_multipart)

set(SOURCES test_cmocka_multipart.c ../src/webcfg_param.c ../src/webcfg_multipart.c ../src/webcfg_helpers.c ../src/webcfg.c ../src/webcfg_auth.c ../src/webcfg_notify.c ../src/webcfg_db.c ../src/webcfg_pack.c ../src/webcfg_blob.c ../src/webcfg_event.c ../src/webcfg_generic_pc.c ../src/webcfg_metadata.c ../src/webcfg_timer.c)

if (WEBCONFIG_BIN_SUPPORT)
set(SOURCES ${SOURCES} ../src/webcfg_rbus.c)
endif (WEBCONFIG_BIN_SUPPORT)

if (FEATURE_SUPPORT_AKER)
set(SOURCES ${SOURCES} ../src/webcfg_client.c ../src/webcfg_aker.c)
endif (FEATURE_SUPPORT_AKER)

add_executable(test_cmocka_multipart ${SOURCES})

target_link_libraries (test_cmocka_multipart -lcmocka -lcunit -lmsgpackc -lpthread -lm -luuid -ltrower-base64 -lwdmp-c -lcimplog -lcjson -lwrp-c -lcurl )

if (WEBCONFIG_BIN_SUPPORT)
target_link_libraries (test_cmocka_multipart -lrbus)
endif (WEBCONFIG_BIN_SUPPORT)

if (FEATURE_SUPPORT_AKER)
target_link_libraries (test_cmoka_multipart -llibparodus -lnanomsg)
endif (FEATURE_SUPPORT_AKER)

target_link_libraries (test_cmocka_multipart gcov -Wl,--no-as-needed )

# Code coverage

add_custom_target(coverage
Expand Down Expand Up @@ -375,8 +413,11 @@ ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_timer.dir/__/src --output-file test_
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_notify.dir/__/src --output-file test_notify.info
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_generic.dir/__/src --output-file test_generic_pc.info

${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_generic_pc.dir/__/src --output-file test_generic_pc.info
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_blob.dir/__/src --output-file test_blob.info
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_cmocka_multipart.dir/__/src --output-file test_cmocka_multipart.info

COMMAND lcov
-a test_events_supp.info
Expand All @@ -394,6 +435,8 @@ COMMAND lcov
-a test_timer.info
-a test_notify.info
-a test_generic_pc.info
-a test_blob.info
-a test_cmocka_multipart.info
--output-file coverage.info

COMMAND genhtml coverage.info
Expand Down
286 changes: 286 additions & 0 deletions tests/test_cmocka_multipart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
/**
* Copyright 2019 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <CUnit/Basic.h>
#include <cmocka.h>

#include "../src/webcfg_log.h"
#include "../src/webcfg_param.h"
#include "../src/webcfg.h"
#include "../src/webcfg_multipart.h"
#include "../src/webcfg_helpers.h"
#include "../src/webcfg_db.h"
#include "../src/webcfg_notify.h"
#include "../src/webcfg_metadata.h"
#include "../src/webcfg_generic.h"
#include "../src/webcfg_event.h"
#include "../src/webcfg_auth.h"
#include "../src/webcfg_blob.h"
#include "../src/webcfg_timer.h"

#define UNUSED(x) (void )(x)
int numLoops;

typedef void CURL;

#undef curl_easy_setopt
#undef curl_easy_getinfo

/*long long getRetryExpiryTimeout()
{
return 0;
}
int get_retry_timer()
{
return 0;
}
int updateRetryTimeDiff(long long expiry_time)
{
UNUSED(expiry_time);
return 0;
}
int checkRetryTimer( long long timestamp)
{
return timestamp;
}
char* printTime(long long time)
{
UNUSED(time);
return NULL;
}
int checkMaintenanceTimer()
{
return 0;
}
void initMaintenanceTimer()
{
return;
}
int getMaintenanceSyncSeconds(int maintenance_count)
{
UNUSED(maintenance_count);
return 0;
}
long get_global_retry_timestamp()
{
return 0;
}
int retrySyncSeconds()
{
return 0;
}
void set_retry_timer(int value)
{
UNUSED(value);
return;
}
void set_global_retry_timestamp(long value)
{
UNUSED(value);
return;
}
void set_global_maintenance_time(long value)
{
UNUSED(value);
return;
}
*/

int Get_Webconfig_URL(char *pString)
{
// Set a non-empty value for configURL
strcpy(pString, "http://example.com/config.xml");
return 0; // or whatever the expected return value is
}

CURL *curl_easy_init ()
{
function_called();
return (CURL *) mock();
}

CURLcode curl_easy_perform(CURL *curl)
{
UNUSED(curl);
int rtn;

function_called();
rtn = (int) mock();
return rtn;
}

CURLcode curl_easy_setopt(CURL *easy, CURLoption option, ...)
{
UNUSED (easy);
UNUSED (option);
return CURLE_OK;
}

CURLcode curl_easy_getinfo(CURL *easy, CURLINFO info, ... )
{
UNUSED (easy);
UNUSED (info);
//int rtn;

va_list args;
va_start(args, info);

CURLcode result = CURLE_OK;

function_called();
result = (int) mock();

if (info == CURLINFO_RESPONSE_CODE) {
long* response_code = va_arg(args, long*);
*response_code = 200; // Always return 200 as the response code
} else if (info == CURLINFO_CONTENT_TYPE) {
char** ct = va_arg(args, char**);
*ct = "multipart/mixed"; // Set the content type as "multipart/mixed"
}
return result;
}

void curl_easy_cleanup(CURL *easy)
{
(void) easy;
}

void test_webcfg_http_request_curl_init_fail()
{
char *config = NULL; // Initialize with your data
int r_count = 1; // Set the number of retries as needed
int status = 0; // Set the status as needed
long code = 0; // Response code
char *transaction_id = NULL; // Transaction ID
char contentType[64] = {0}; // Content type
size_t dataSize = 0; // Data size
char docname[64] = "value"; // Document name

will_return (curl_easy_init, NULL);
expect_function_calls (curl_easy_init, 1);

WEBCFG_STATUS result = webcfg_http_request(&config, r_count, status, &code, &transaction_id, contentType, &dataSize, docname);

WebcfgInfo("The result is %d", result);
assert_int_equal (result, 1);

}

//get_global_supplementarySync() == 0
void test_webcfg_http_request_curl_init_success()
{
char *config = NULL; // Initialize with your data
int r_count = 1; // Set the number of retries as needed
int status = 0; // Set the status as needed
long code = 0; // Response code
char *transaction_id = NULL; // Transaction ID
char contentType[64] = {0}; // Content type
size_t dataSize = 0; // Data size
char docname[64] = {0}; // Document name

set_global_supplementarySync(0);

will_return (curl_easy_init, 1);
expect_function_calls (curl_easy_init, 1);

will_return (curl_easy_perform, 0);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 1);
expect_function_calls (curl_easy_getinfo, 1);

WEBCFG_STATUS result = webcfg_http_request(&config, r_count, status, &code, &transaction_id, contentType, &dataSize, docname);

WebcfgInfo("The result is %d", result);

assert_int_equal (result, 0);
}

int Get_Supplementary_URL(char *name, char *pString) {
// Set a non-empty value for configURL
strcpy(pString, "http://example.com/config.xml");
return 0; // or whatever the expected return value is
}

//get_global_supplementarySync() == 1
void test_webcfg_http_request_supp_sync()
{
char *config = NULL; // Initialize with your data
int r_count = 2; // Set the number of retries as needed
int status = 0; // Set the status as needed
long code = 0; // Response code
char *transaction_id = NULL; // Transaction ID
char contentType[64] = {0}; // Content type
size_t dataSize = 0; // Data size
char docname[64] = "value"; // Document name


set_global_supplementarySync(1);
will_return (curl_easy_init, 1);
expect_function_calls (curl_easy_init, 1);

will_return (curl_easy_perform, 0);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

WEBCFG_STATUS result = webcfg_http_request(&config, r_count, status, &code, &transaction_id, contentType, &dataSize, docname);

WebcfgInfo("The result is %d", result);

assert_int_equal (result, 0);
}

int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_webcfg_http_request_curl_init_fail),
cmocka_unit_test(test_webcfg_http_request_curl_init_success),
cmocka_unit_test(test_webcfg_http_request_supp_sync)
};

return cmocka_run_group_tests(tests, NULL, NULL);
}

Loading

0 comments on commit a54d57f

Please sign in to comment.