Skip to content

Commit

Permalink
Unit test cases for multipart.c
Browse files Browse the repository at this point in the history
  • Loading branch information
suvi-123 committed Nov 7, 2023
1 parent f6f8adb commit d9da52a
Show file tree
Hide file tree
Showing 5 changed files with 523 additions and 1 deletion.
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,21 @@ 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
7 changes: 7 additions & 0 deletions src/webcfg_multipart.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ int get_global_eventFlag(void);
void set_global_eventFlag();
void set_global_ETAG(char *etag);
char *get_global_ETAG(void);
void line_parser(char *ptr, int no_of_bytes, char **name_space, uint32_t *etag, char **data, size_t *data_size);
void subdoc_parser(char *ptr, int no_of_bytes);
void stripspaces(char *str, char **final_str);
void get_webCfg_interface(char **interface);
size_t headr_callback(char *buffer, size_t size, size_t nitems, void* data);
size_t writer_callback_fn(void *buffer, size_t size, size_t nmemb, void *datain);
WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id);
#ifdef WAN_FAILOVER_SUPPORTED
void set_global_interface(char * value);
char * get_global_interface(void);
Expand Down
33 changes: 32 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,35 @@ endif (WEBCONFIG_BIN_SUPPORT)

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

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

set(SOURCES test_cmoka_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)

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_cmoka_multipart ${SOURCES})

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

if (WEBCONFIG_BIN_SUPPORT)
target_link_libraries (test_cmoka_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_cmoka_multipart gcov -Wl,--no-as-needed )

# Code coverage

add_custom_target(coverage
Expand Down Expand Up @@ -376,7 +405,8 @@ 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

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

COMMAND lcov
-a test_events_supp.info
Expand All @@ -394,6 +424,7 @@ COMMAND lcov
-a test_timer.info
-a test_notify.info
-a test_generic_pc.info
-a test_cmoka_multipart.info
--output-file coverage.info

COMMAND genhtml coverage.info
Expand Down
223 changes: 223 additions & 0 deletions tests/test_cmoka_multipart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/**
* 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()

Check warning on line 46 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L46

Added line #L46 was not covered by tests
{
return 0;

Check warning on line 48 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L48

Added line #L48 was not covered by tests
}

int get_retry_timer()

Check warning on line 51 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L51

Added line #L51 was not covered by tests
{
return 0;

Check warning on line 53 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L53

Added line #L53 was not covered by tests
}

int updateRetryTimeDiff(long long expiry_time)

Check warning on line 56 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L56

Added line #L56 was not covered by tests
{
UNUSED(expiry_time);
return 0;

Check warning on line 59 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L59

Added line #L59 was not covered by tests
}

int checkRetryTimer( long long timestamp)

Check warning on line 62 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L62

Added line #L62 was not covered by tests
{
return timestamp;

Check warning on line 64 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L64

Added line #L64 was not covered by tests
}

char* printTime(long long time)

Check warning on line 67 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L67

Added line #L67 was not covered by tests
{
UNUSED(time);
return NULL;

Check warning on line 70 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L70

Added line #L70 was not covered by tests
}

int checkMaintenanceTimer()

Check warning on line 73 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L73

Added line #L73 was not covered by tests
{
return 0;

Check warning on line 75 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L75

Added line #L75 was not covered by tests
}

void initMaintenanceTimer()

Check warning on line 78 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L78

Added line #L78 was not covered by tests
{
return;

Check warning on line 80 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L80

Added line #L80 was not covered by tests
}

int getMaintenanceSyncSeconds(int maintenance_count)

Check warning on line 83 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L83

Added line #L83 was not covered by tests
{
UNUSED(maintenance_count);
return 0;

Check warning on line 86 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L86

Added line #L86 was not covered by tests
}

long get_global_retry_timestamp()

Check warning on line 89 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L89

Added line #L89 was not covered by tests
{
return 0;

Check warning on line 91 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L91

Added line #L91 was not covered by tests
}

int retrySyncSeconds()

Check warning on line 94 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L94

Added line #L94 was not covered by tests
{
return 0;

Check warning on line 96 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L96

Added line #L96 was not covered by tests
}

void set_retry_timer(int value)

Check warning on line 99 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L99

Added line #L99 was not covered by tests
{
UNUSED(value);
return;

Check warning on line 102 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L102

Added line #L102 was not covered by tests
}

void set_global_retry_timestamp(long value)

Check warning on line 105 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L105

Added line #L105 was not covered by tests
{
UNUSED(value);
return;

Check warning on line 108 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L108

Added line #L108 was not covered by tests
}

void set_global_maintenance_time(long value)

Check warning on line 111 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L111

Added line #L111 was not covered by tests
{
UNUSED(value);
return;

Check warning on line 114 in tests/test_cmoka_multipart.c

View check run for this annotation

Codecov / codecov/patch

tests/test_cmoka_multipart.c#L114

Added line #L114 was not covered by tests
}

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;

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

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

void test_webcfg_http_request_curl_init_fail()
{
// Set up your test data and mock behaviors
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

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);

printf("The result is %d", result);
}

void test_webcfg_http_request_curl_init_success()
{
// Set up your test data and mock behaviors
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

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);

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

printf("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)

};

return cmocka_run_group_tests(tests, NULL, NULL);
}
Loading

0 comments on commit d9da52a

Please sign in to comment.