From: Seonah Moon Date: Fri, 25 Sep 2020 08:28:01 +0000 (+0900) Subject: Add unittest for line coverage X-Git-Tag: submit/tizen/20201020.080343~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F68%2F244868%2F7;p=platform%2Fcore%2Fapi%2Fhttp.git Add unittest for line coverage Change-Id: Ibb5180e81f5cc5f364e1df4229320430434c8a82 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e9b668..05aa278 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,36 +64,8 @@ CONFIGURE_FILE( ) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) -ADD_SUBDIRECTORY(test) - -IF(UNIX) - -ADD_CUSTOM_TARGET (distclean @echo cleaning for source distribution) -ADD_CUSTOM_COMMAND( - DEPENDS clean - COMMENT "distribution clean" - COMMAND find - ARGS . - -not -name config.cmake -and \( - -name tester.c -or - -name Testing -or - -name CMakeFiles -or - -name cmake.depends -or - -name cmake.check_depends -or - -name CMakeCache.txt -or - -name cmake.check_cache -or - -name *.cmake -or - -name Makefile -or - -name core -or - -name core.* -or - -name gmon.out -or - -name install_manifest.txt -or - -name *.pc -or - -name *~ \) - | grep -v TC | xargs rm -rf - TARGET distclean - VERBATIM -) - -ENDIF(UNIX) +ADD_SUBDIRECTORY(tools) +IF(BUILD_GTESTS) + ADD_SUBDIRECTORY(tests) +ENDIF(BUILD_GTESTS) diff --git a/packaging/capi-network-http.spec b/packaging/capi-network-http.spec index abbb311..69d2868 100644 --- a/packaging/capi-network-http.spec +++ b/packaging/capi-network-http.spec @@ -12,9 +12,11 @@ BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(capi-network-connection) BuildRequires: pkgconfig(libcurl) BuildRequires: pkgconfig(cynara-client) +BuildRequires: pkgconfig(gmock) BuildRequires: cmake %if 0%{?gcov:1} BuildRequires: lcov +BuildRequires: tar %endif Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig @@ -48,19 +50,28 @@ export FFLAGS+=" -fprofile-arcs -ftest-coverage" export LDFLAGS+=" -lgcov" %endif -%cmake . +%cmake -DBUILD_GTESTS=%{?gcov:1}%{!?gcov:0} \ + . + make %{?_smp_mflags} +%install +%make_install + %if 0%{?gcov:1} -mkdir -p gcov-obj -find . -name '*.gcno' -exec cp '{}' gcov-obj ';' +find .. -name '*.gcno' | tar cf %{name}-gcov.tar -T - +install -d -m 755 %{buildroot}%{_datadir}/gcov/obj +tar xf %{name}-gcov.tar -C %{buildroot}%{_datadir}/gcov/obj +%endif + +%check +%if 0%{?gcov:1} +env all_proxy=%{gproxy} tests/http-gtest # http://[proxy addr]:[port] %endif -%install -%make_install %if 0%{?gcov:1} -mkdir -p %{buildroot}%{_datadir}/gcov/obj -install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj +lcov -c --ignore-errors graph --no-external -b . -d . -o %{name}.info +genhtml %{name}.info -o out --legend --show-details %endif %post -p /sbin/ldconfig diff --git a/src/http_transaction.c b/src/http_transaction.c index ef4252e..0e37207 100644 --- a/src/http_transaction.c +++ b/src/http_transaction.c @@ -497,7 +497,10 @@ int _transaction_submit(gpointer user_data) API int http_session_open_transaction(http_session_h http_session, http_method_e method, http_transaction_h *http_transaction) { _retvm_if(_http_is_init() == false, HTTP_ERROR_INVALID_OPERATION, "http isn't initialized"); - _retvm_if(http_session == NULL, HTTP_ERROR_INVALID_PARAMETER, "parameter(http_session) is NULL\n"); + _retvm_if(http_session == NULL, + HTTP_ERROR_INVALID_PARAMETER, "parameter(http_session) is NULL\n"); + _retvm_if(http_transaction == NULL, + HTTP_ERROR_INVALID_PARAMETER, "parameter(http_transaction) is NULL\n"); __http_transaction_h *transaction = NULL; @@ -944,6 +947,8 @@ API int http_transaction_set_timeout(http_transaction_h http_transaction, int ti "http isn't initialized"); _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER, "parameter(http_transaction) is NULL\n"); + _retvm_if(timeout < 0, HTTP_ERROR_INVALID_PARAMETER, + "parameter(timeout) is less than 0\n"); __http_transaction_h *transaction = (__http_transaction_h *)http_transaction; @@ -1024,6 +1029,8 @@ API int http_transaction_get_server_certificate_verification(http_transaction_h "http isn't initialized"); _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER, "parameter(http_transaction) is NULL\n"); + _retvm_if(verify == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(verify) is NULL\n"); __http_transaction_h *transaction = (__http_transaction_h *)http_transaction; @@ -1052,6 +1059,8 @@ API int http_transaction_get_tcp_fastopen(http_transaction_h http_transaction, b "http isn't initialized"); _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER, "parameter(http_transaction) is NULL\n"); + _retvm_if(enable == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(enable) is NULL\n"); #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0) return HTTP_ERROR_NOT_SUPPORTED; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index f9fee27..0000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -SET(fw_test "${fw_name}_test") - -INCLUDE(FindPkgConfig) -pkg_check_modules(${fw_test} REQUIRED glib-2.0) -FOREACH(flag ${${fw_test}_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) - -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -fPIE") -SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") - -aux_source_directory(. sources) -FOREACH(src ${sources}) - GET_FILENAME_COMPONENT(src_name ${src} NAME_WE) - MESSAGE("${src_name}") - ADD_EXECUTABLE(${src_name} ${src}) - TARGET_LINK_LIBRARIES(${src_name} ${fw_name} ${${fw_test}_LDFLAGS}) -ENDFOREACH() - -INSTALL(TARGETS http_test RUNTIME DESTINATION bin/) diff --git a/test/http_test.c b/test/http_test.c deleted file mode 100644 index bb3ae27..0000000 --- a/test/http_test.c +++ /dev/null @@ -1,601 +0,0 @@ -/* - * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd. - * - * 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 -#include -#include -#include -#include - -#include "http.h" -#include "http_internal.h" - -#define ERR(x, y) printf("[ERR] %s(%d)\n", x, y) -#define PRG(x, y) printf("[PRG] %s(%p)\n", x, y) -#define DBG printf -#define MAX_URI_LEN 1024 - -FILE* fp1 = NULL; -FILE* fp2 = NULL; - -http_session_h session = NULL; -int cancel = 0; - -void _register_callbacks(http_transaction_h transaction); -int _write_message_body(http_transaction_h transaction, const char *file_path); - -void __transaction_header_cb(http_transaction_h transaction, char *header, size_t header_len, void *user_data) -{ - PRG("transaction_header_cb", transaction); -} - -void __transaction_body_cb(http_transaction_h transaction, char *body, size_t size, size_t nmemb, void *user_data) -{ - PRG("transaction_body_cb", transaction); - int written = size * nmemb; - DBG("Received length: %d\n", written); - DBG("Received body: %s\n", body); -} - -void __transaction_write_cb(http_transaction_h transaction, int recommended_chunk_size, void *user_data) -{ - PRG("transaction_write_cb", transaction); - DBG("recommended_chunk_size:%d\n", recommended_chunk_size); -} - -void __transaction_completed_cb(http_transaction_h transaction, void *user_data) -{ - PRG("transaction_completed_cb", transaction); - - http_status_code_e status = 0; - int ret; - char *uri = NULL; - char *realm = NULL; - char id[16] = {0, }; - char pw[16] = {0, }; - - ret = http_transaction_request_get_uri(transaction, &uri); - ret = http_transaction_response_get_status_code(transaction, &status); - DBG("%s - status(%d)\n", uri, status); - if (status == HTTP_STATUS_UNAUTHORIZED) { - DBG("Authentication Required\n"); - http_transaction_h http_auth_transaction; - http_auth_scheme_e auth_scheme = HTTP_AUTH_NONE; - - http_transaction_open_authentication(transaction, &http_auth_transaction); - http_transaction_get_http_auth_scheme(http_auth_transaction, &auth_scheme); - - ret = http_transaction_get_realm(http_auth_transaction, &realm); - if (ret != HTTP_ERROR_NONE) - DBG("Fail to get realm\n"); - else - DBG("realm (%s)", realm); - - printf("User ID: "); - ret = scanf("%15s", id); - printf("Password: "); - ret = scanf("%15s", pw); - - http_transaction_set_credentials(http_auth_transaction, id, pw); - _register_callbacks(http_auth_transaction); - http_transaction_submit(http_auth_transaction); - } - - if (realm) - free(realm); - - if (uri) - free(uri); -} - -void __transaction_aborted_cb(http_transaction_h transaction, http_error_code_e reason, void *user_data) -{ - PRG("transaction_aborted_cb", transaction); - DBG("aborted reason: %d\n", reason); - - switch (reason) { - case HTTP_ERROR_OPERATION_FAILED: - DBG("HTTP_ERROR_OPERATION_FAILED\n"); - break; - case HTTP_ERROR_COULDNT_RESOLVE_HOST: - DBG("HTTP_ERROR_COULDNT_RESOLVE_HOST\n"); - break; - case HTTP_ERROR_COULDNT_CONNECT: - DBG("HTTP_ERROR_COULDNT_CONNECT\n"); - break; - case HTTP_ERROR_OPERATION_TIMEDOUT: - DBG("HTTP_ERROR_OPERATION_TIMEDOUT\n"); - break; - case HTTP_ERROR_SSL_CONNECT_ERROR: - DBG("HTTP_ERROR_SSL_CONNECT_ERROR\n"); - break; - case HTTP_ERROR_CANCELED: - DBG("HTTP_ERROR_CANCELED\n"); - break; - default: - DBG("Unknown error\n"); - break; - } - cancel = 0; - - return; -} - -void __transaction_progress_cb(http_transaction_h transaction, double download_total, double download_now, double upload_total, double upload_now, void *user_data) -{ - if (cancel) { - DBG("Cancel"); - http_transaction_cancel(transaction); - } -} - -void _register_callbacks(http_transaction_h transaction) -{ - http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL); - http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL); - http_transaction_set_uploaded_cb(transaction, __transaction_write_cb, NULL); - http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL); - http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL); - http_transaction_set_progress_cb(transaction, __transaction_progress_cb, NULL); -} - -int _write_message_body(http_transaction_h transaction, const char *file_path) -{ - FILE *fp; - size_t chunk_size = 50; - size_t result = 0; - char *buf = malloc(chunk_size + 1); - - fp = fopen(file_path, "rb"); - if (!fp) { - free(buf); - return -1; - } - - while ((result = fread(buf, 1, chunk_size, fp)) > 0) - http_transaction_request_write_body(transaction, strndup(buf, result)); - - free(buf); - fclose(fp); - - return 0; -} - -int test_http_init(void) -{ - int ret = http_init(); - if (ret == HTTP_ERROR_NONE) - return 1; - else return 0; -} - -int test_http_deinit(void) -{ - int ret = http_deinit(); - if (ret == HTTP_ERROR_NONE) - return 1; - else return 0; -} - -int test_http_session_create(void) -{ - int ret; - int session_mode; - - printf("Select session mode(0: NORMAL 1: PIPELINING): "); - ret = scanf("%d", &session_mode); - - ret = http_session_create(session_mode, &session); - if (ret != HTTP_ERROR_NONE) { - ERR("Fail to create session", ret); - return 0; - } - return 1; -} - -int test_http_session_destroy(void) -{ - int ret = http_session_destroy(session); - if (ret != HTTP_ERROR_NONE) { - ERR("Fail to destroy session", ret); - return 0; - } - - session = NULL; - - return 1; -} - -int test_simple_get(void) -{ - char uri[1024]; - int ret; - http_transaction_h transaction = NULL; - http_method_e method; - int tfo = false; - - printf("Input uri: "); - ret = scanf("%1023s", uri); - - ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction); - if (ret != 0) { - ERR("Fail to open transaction", ret); - return 0; - } - - http_transaction_request_get_method(transaction, &method); - ret = http_transaction_request_set_uri(transaction, uri); - if (ret != 0) { - ERR("Fail to set URI", ret); - return 0; - } - - printf("TCP Fast Oepn(0: disable, 1: enable): "); - ret = scanf("%1d", &tfo); - - ret = http_transaction_set_tcp_fastopen(transaction, tfo); - if (ret != 0) { - ERR("Fail to set tfo", ret); - return 0; - } - - _register_callbacks(transaction); - ret = http_transaction_submit(transaction); - if (ret != 0) { - ERR("Fail to submit transaction", ret); - return 0; - } - - return 1; -} - -int test_multiple_get(void) -{ - int ret; - http_transaction_h transaction[10]; - int count; - int i; - - printf("Input count of transactions(1~10): "); - ret = scanf("%d", &count); - - if (count < 0 || count > 10) { - printf("Invalid input!\n"); - return 0; - } - - for (i = 0; i < count; i++) { - char uri[1024]; - printf("Input uri for transaction[%d]: ", i + 1); - ret = scanf("%1023s", uri); - ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction[i]); - if (ret != 0) { - ERR("Fail to open transaction", ret); - return 0; - } - ret = http_transaction_request_set_uri(transaction[i], uri); - if (ret != 0) { - ERR("Fail to set URI", ret); - return 0; - } - _register_callbacks(transaction[i]); - http_transaction_submit(transaction[i]); - } - - return 1; -} - -int test_simple_post(void) -{ - int ret; - http_transaction_h transaction; - const char* post_msg = "name=tizen&project=capi-network-http"; - - ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction); - if (ret != 0) { - ERR("Fail to open transaction", ret); - return 0; - } - ret = http_transaction_request_set_uri(transaction, "http://httpbin.org/post"); - if (ret != 0) { - ERR("Fail to set URI", ret); - return 0; - } - http_transaction_header_add_field(transaction, "Expect", ""); - http_transaction_request_write_body(transaction, post_msg); - - _register_callbacks(transaction); - http_transaction_submit(transaction); - - return 1; -} - -int test_multipart_post(void) -{ - int ret; - http_transaction_h transaction; - char *file_path = "/home/test.txt"; - char *file_name = "test.txt"; - - ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction); - if (ret != 0) { - ERR("Fail to open transaction", ret); - return 0; - } - ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php"); - if (ret != 0) { - ERR("Fail to set URI", ret); - return 0; - } - - ret = http_transaction_request_add_multipart(transaction, HTTP_MULTIPART_FILE, - "sendfile", file_path, NULL); - - ret = http_transaction_request_add_multipart(transaction, HTTP_MULTIPART_CONTENTS, - "filename", file_name, NULL); - - http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL); - http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL); - http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL); - http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL); - http_transaction_set_progress_cb(transaction, __transaction_progress_cb, NULL); - - http_transaction_submit(transaction); - - return 1; -} - -int test_put(void) -{ - http_transaction_h transaction; - const char *file_path = "/home/test.txt"; - int ret = 0; - - ret = http_session_open_transaction(session, HTTP_METHOD_PUT, &transaction); - if (ret != 0) { - ERR("Fail to open transaction", ret); - return 0; - } - - ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php"); - if (ret != 0) { - ERR("Fail to set URI", ret); - return 0; - } - - ret = http_transaction_request_set_upload_file(transaction, file_path); - if (ret != 0) { - ERR("Fail to set file path", ret); - return 0; - } - - _register_callbacks(transaction); - ret = http_transaction_submit(transaction); - if (ret != 0) { - ERR("Fail to submit transaction", ret); - return 0; - } - - return 1; -} - -int test_simple_authentication_get(void) -{ - int ret; - http_transaction_h transaction = NULL; - http_method_e method; - char uri[1024]; - - ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction); - if (ret != 0) { - ERR("Fail to open transaction", ret); - return 0; - } - - printf("Input uri for transaction: "); - ret = scanf("%1023s", uri); - - http_transaction_request_get_method(transaction, &method); - ret = http_transaction_request_set_uri(transaction, uri); - if (ret != 0) { - ERR("Fail to set URI", ret); - return 0; - } - - _register_callbacks(transaction); - ret = http_transaction_submit(transaction); - if (ret != 0) { - ERR("Fail to submit transaction", ret); - return 0; - } - - return 1; -} - -int test_post_chunk(void) -{ - int ret; - http_transaction_h transaction; - char path[1024] = {0, }; - - printf("Input path: "); - ret = scanf("%1023s", path); - - if (strlen(path) < 1) - return 0; - - ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction); - if (ret != 0) { - ERR("Fail to open transaction", ret); - return 0; - } - - ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php"); - if (ret != 0) { - ERR("Fail to set URI", ret); - return 0; - } - - _register_callbacks(transaction); - ret = _write_message_body(transaction, path); - if (ret != 0) { - ERR("Failed write message body", ret); - return 0; - - } - - http_transaction_set_ready_to_write(transaction, TRUE); - http_transaction_header_add_field(transaction, "Transfer-Encoding", "chunked"); - http_transaction_header_add_field(transaction, "Expect", ""); - http_transaction_submit(transaction); - - return 1; -} - -int test_cancel(void) -{ - char uri[1024]; - int ret; - http_transaction_h transaction = NULL; - http_method_e method; - - cancel = 1; - - printf("Input uri: "); - ret = scanf("%1023s", uri); - - ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction); - if (ret != 0) { - ERR("Fail to open transaction", ret); - return 0; - } - - http_transaction_request_get_method(transaction, &method); - ret = http_transaction_request_set_uri(transaction, uri); - if (ret != 0) { - ERR("Fail to set URI", ret); - return 0; - } - - _register_callbacks(transaction); - ret = http_transaction_submit(transaction); - - if (ret != 0) { - ERR("Fail to submit transaction", ret); - return 0; - } - - return 1; -} - -gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) -{ - int rv; - char a[10]; - - printf("Event received from stdin\n"); - - rv = read(0, a, 10); - - if (rv <= 0 || a[0] == '0') - exit(1); - - if (a[0] == '\n' || a[0] == '\r') { - printf("\n\n Network Connection API Test App\n\n"); - printf("Options..\n"); - printf("1 - Initialize\n"); - printf("2 - Deinitialize\n"); - printf("3 - Create session\n"); - printf("4 - Destroy session\n"); - printf("5 - Simple GET\n"); - printf("6 - Multiple GET\n"); - printf("7 - Simple POST\n"); - printf("8 - Multipart POST\n"); - printf("9 - Simple Authentication GET\n"); - printf("a - Upload file (PUT)\n"); - printf("b - Upload file (POST)\n"); - printf("c - Cancel\n"); - printf("0 - Exit \n"); - printf("ENTER - Show options menu.......\n"); - } - - switch (a[0]) { - case '1': - rv = test_http_init(); - break; - case '2': - rv = test_http_deinit(); - break; - case '3': - rv = test_http_session_create(); - break; - case '4': - rv = test_http_session_destroy(); - break; - case '5': - rv = test_simple_get(); - break; - case '6': - rv = test_multiple_get(); - break; - case '7': - rv = test_simple_post(); - break; - case '8': - rv = test_multipart_post(); - break; - case '9': - rv = test_simple_authentication_get(); - break; - case 'a': - rv = test_put(); - break; - case 'b': - rv = test_post_chunk(); - break; - case 'c': - rv = test_cancel(); - break; - } - - if (rv == 1) - printf("Operation succeeded!\n"); - else - printf("Operation failed!\n"); - - return true; -} - -int main(int argc, char **argv) -{ - GMainLoop *mainloop; - -#if !GLIB_CHECK_VERSION(2, 36, 0) - g_type_init(); -#endif - mainloop = g_main_loop_new(NULL, false); - - GIOChannel *channel = g_io_channel_unix_new(0); - g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL); - printf("Test Thread created...\n"); - g_main_loop_run(mainloop); - - return 0; -} - - - diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..d939950 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,20 @@ +LINK_DIRECTORIES(${CMAKE_BINARY_DIR}) +ADD_DEFINITIONS("-DHTTP_GTEST") + +pkg_check_modules(gtest_pkgs REQUIRED gmock glib-2.0) +INCLUDE_DIRECTORIES(${gtest_pkgs_INCLUDE_DIRS}) +LINK_DIRECTORIES(${gtest_pkgs_LIBRARY_DIRS}) + +SET(HTTP_GTEST "http-gtest") +FILE(GLOB GTEST_SRCS *.cpp mocks/*.c ${CMAKE_SOURCE_DIR}/src/*.c) +ADD_EXECUTABLE(${HTTP_GTEST} ${GTEST_SRCS}) +TARGET_LINK_LIBRARIES(${HTTP_GTEST} ${gtest_pkgs_LIBRARIES} ${${fw_name}_LIBRARIES}) +SET_TARGET_PROPERTIES(${HTTP_GTEST} PROPERTIES + COMPILE_FLAGS "-fPIE" + LINK_FLAGS "-Wl,\ +--wrap=malloc,\ +--wrap=fopen,\ +--wrap=fopen64,\ +--wrap=fclose,\ +--wrap=fread,\ +--wrap=cynara_check") diff --git a/tests/http-gtest-async.cpp b/tests/http-gtest-async.cpp new file mode 100755 index 0000000..acb64fc --- /dev/null +++ b/tests/http-gtest-async.cpp @@ -0,0 +1,163 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* 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 +#include + +#include "http.h" + +extern "C" { +#include "mocks/http_cynara.h" +#include "mocks/http_file.h" +#include "mocks/http_memory.h" +} + +#include "http-gtest-util.h" + +static bool _completed_flag; +static bool _canceled_flag; +static bool _paused_flag; +static bool _resumed_flag; + +static void _completed_cb(http_transaction_h http_transaction, void *user_data); +static void _aborted_cb(http_transaction_h http_transaction, http_error_code_e error, void *user_data); +class HttpAsyncTest: public ::testing::Test { + protected: + http_session_h session = NULL; + http_transaction_h transaction = NULL; + + void SetUp() override + { + http_mock_set_memory_result(true); + http_mock_set_cynara_result(true); + http_mock_set_file_result(true); + + _completed_flag = false; + _canceled_flag = false; + _paused_flag = false; + _resumed_flag = false; + + http_init(); + http_session_create(HTTP_SESSION_MODE_NORMAL, &session); + http_session_open_transaction(session, HTTP_METHOD_GET, &transaction); + http_transaction_set_completed_cb(transaction, _completed_cb, NULL); + http_transaction_set_aborted_cb(transaction, _aborted_cb, NULL); + http_transaction_request_set_uri(transaction, "http://www.example.com"); + } + + void TearDown() override + { + http_transaction_destroy(transaction); + http_session_destroy(session); + http_deinit(); + + http_mock_set_file_result(true); + http_mock_set_cynara_result(true); + http_mock_set_memory_result(true); + } +}; + +static void _completed_cb(http_transaction_h http_transaction, void *user_data) +{ + _completed_flag = true; + HttpTestUtil::stopMainLoop(); +} + +static void _aborted_cb(http_transaction_h http_transaction, + http_error_code_e error, void *user_data) +{ + _completed_flag = false; + if (error == HTTP_ERROR_CANCELED) + _canceled_flag = true; + HttpTestUtil::stopMainLoop(); +} + +static void _progress_cancel_cb(http_transaction_h http_transaction, + double download_total, double download_now, + double upload_total, double upload_now, void *user_data) +{ + http_transaction_cancel(http_transaction); +} + +static void _progress_pause_cb(http_transaction_h http_transaction, + double download_total, double download_now, + double upload_total, double upload_now, void *user_data) +{ + int ret = http_transaction_pause(http_transaction, HTTP_PAUSE_ALL); + if (ret == HTTP_ERROR_NONE) + _paused_flag = true; + + http_transaction_cancel(http_transaction); +} + +static void _progress_resume_cb(http_transaction_h http_transaction, + double download_total, double download_now, + double upload_total, double upload_now, void *user_data) +{ + http_transaction_pause(http_transaction, HTTP_PAUSE_ALL); + if (http_transaction_resume(http_transaction) == HTTP_ERROR_NONE) + _resumed_flag = true; +} + +TEST_F(HttpAsyncTest, SubmitN) +{ + http_transaction_request_set_uri(transaction, "http://aaaa.bbbb.cccc"); + EXPECT_EQ(HTTP_ERROR_NONE, http_transaction_submit(transaction)); + + HttpTestUtil::runMainLoop(); + + EXPECT_EQ(false, _completed_flag); +} + +TEST_F(HttpAsyncTest, SubmitP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, http_transaction_submit(transaction)); + + HttpTestUtil::runMainLoop(); + + EXPECT_EQ(true, _completed_flag); +} + +TEST_F(HttpAsyncTest, CancelP) +{ + http_transaction_set_progress_cb(transaction, _progress_cancel_cb, NULL); + http_transaction_submit(transaction); + + HttpTestUtil::runMainLoop(); + + EXPECT_EQ(true, _canceled_flag); +} + +TEST_F(HttpAsyncTest, PauseP) +{ + http_transaction_set_progress_cb(transaction, _progress_pause_cb, NULL); + http_transaction_submit(transaction); + + HttpTestUtil::runMainLoop(); + + EXPECT_EQ(true, _paused_flag); +} + +TEST_F(HttpAsyncTest, ResumeP) +{ + http_transaction_set_progress_cb(transaction, _progress_resume_cb, NULL); + http_transaction_submit(transaction); + + HttpTestUtil::runMainLoop(); + + EXPECT_EQ(true, _resumed_flag); + EXPECT_EQ(true, _completed_flag); +} diff --git a/tests/http-gtest-common.cpp b/tests/http-gtest-common.cpp new file mode 100755 index 0000000..b837b77 --- /dev/null +++ b/tests/http-gtest-common.cpp @@ -0,0 +1,63 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* 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 + +#include "http.h" + +extern "C" { +#include "mocks/http_file.h" +#include "mocks/http_memory.h" +} + +class HttpCommonTest: public ::testing::Test { + protected: + void SetUp() override + { + http_mock_set_memory_result(true); + http_mock_set_file_result(true); + http_init(); + } + + void TearDown() override + { + http_deinit(); + http_mock_set_memory_result(true); + http_mock_set_file_result(true); + } +}; + +TEST_F(HttpCommonTest, InitN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_OPERATION, http_init()); +} + +TEST_F(HttpCommonTest, InitP) +{ + http_deinit(); + EXPECT_EQ(HTTP_ERROR_NONE, http_init()); +} + +TEST_F(HttpCommonTest, DeInitN) +{ + http_deinit(); + EXPECT_EQ(HTTP_ERROR_INVALID_OPERATION, http_deinit()); +} + +TEST_F(HttpCommonTest, DeInitP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, http_deinit()); +} diff --git a/tests/http-gtest-main.cpp b/tests/http-gtest-main.cpp new file mode 100755 index 0000000..4890e9c --- /dev/null +++ b/tests/http-gtest-main.cpp @@ -0,0 +1,23 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* 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 + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tests/http-gtest-session.cpp b/tests/http-gtest-session.cpp new file mode 100755 index 0000000..12cb3a2 --- /dev/null +++ b/tests/http-gtest-session.cpp @@ -0,0 +1,149 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* 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 + +#include "http.h" + +extern "C" { +#include "mocks/http_memory.h" +} + +class HttpSessionTest: public ::testing::Test { + protected: + http_session_h session = NULL; + + void SetUp() override + { + http_mock_set_memory_result(true); + + http_init(); + http_session_create(HTTP_SESSION_MODE_NORMAL, &session); + } + + void TearDown() override + { + http_session_destroy(session); + http_deinit(); + + http_mock_set_memory_result(true); + } +}; + +TEST_F(HttpSessionTest, SessionCreateN) +{ + http_session_h handle; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_session_create(HTTP_SESSION_MODE_NORMAL, NULL)); + + http_mock_set_memory_result(false); + EXPECT_EQ(HTTP_ERROR_OUT_OF_MEMORY, http_session_create(HTTP_SESSION_MODE_NORMAL, &handle)); + http_mock_set_memory_result(true); +} + +TEST_F(HttpSessionTest, SessionCreateP) +{ + http_session_h handle; + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_create(HTTP_SESSION_MODE_NORMAL, &handle)); + http_session_destroy(handle); + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_create(HTTP_SESSION_MODE_PIPELINING, &handle)); + http_session_destroy(handle); +} + +TEST_F(HttpSessionTest, SessionDestroyN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_session_destroy(NULL)); +} + +TEST_F(HttpSessionTest, SessionDestroyP) +{ + http_session_h handle; + + http_session_create(HTTP_SESSION_MODE_NORMAL, &handle); + EXPECT_EQ(HTTP_ERROR_NONE, http_session_destroy(handle)); +} + +TEST_F(HttpSessionTest, SetAutoRedirectionN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_session_set_auto_redirection(NULL, false)); +} + +TEST_F(HttpSessionTest, SetAutoRedirectionP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, http_session_set_auto_redirection(session, false)); + EXPECT_EQ(HTTP_ERROR_NONE, http_session_set_auto_redirection(session, true)); +} + +TEST_F(HttpSessionTest, GetAutoRedirectionN) +{ + bool auto_redirection; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_session_get_auto_redirection(session, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_session_get_auto_redirection(NULL, &auto_redirection)); +} + +TEST_F(HttpSessionTest, GetAutoRedirectionP) +{ + bool auto_redirection; + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_get_auto_redirection(session, &auto_redirection)); +} + +TEST_F(HttpSessionTest, GetActiveTransactionCountN) +{ + int count; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_session_get_active_transaction_count(session, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_session_get_active_transaction_count(NULL, &count)); +} + +TEST_F(HttpSessionTest, GetActiveTransactionCountP) +{ + int count; + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_get_active_transaction_count(session, &count)); +} + +TEST_F(HttpSessionTest, GetMaxTransactionCountN) +{ + int count; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_session_get_max_transaction_count(session, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_session_get_max_transaction_count(NULL, &count)); +} + +TEST_F(HttpSessionTest, GetMaxTransactionCountP) +{ + int count; + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_get_max_transaction_count(session, &count)); +} + +TEST_F(HttpSessionTest, DestroyAllTransactionsN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_session_destroy_all_transactions(NULL)); +} + +TEST_F(HttpSessionTest, DestroyAllTransactionsP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, http_session_destroy_all_transactions(session)); +} diff --git a/tests/http-gtest-transaction.cpp b/tests/http-gtest-transaction.cpp new file mode 100755 index 0000000..026f0dc --- /dev/null +++ b/tests/http-gtest-transaction.cpp @@ -0,0 +1,815 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* 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 + +#include "http.h" + +extern "C" { +#include "mocks/http_file.h" +#include "mocks/http_memory.h" +} + +class HttpTransactionTest: public ::testing::Test { + protected: + http_session_h session = NULL; + http_transaction_h transaction = NULL; + + void SetUp() override + { + http_mock_set_memory_result(true); + http_mock_set_file_result(true); + + http_init(); + http_session_create(HTTP_SESSION_MODE_NORMAL, &session); + http_session_open_transaction(session, HTTP_METHOD_GET, &transaction); + } + + void TearDown() override + { + http_transaction_destroy(transaction); + http_session_destroy(session); + http_deinit(); + + http_mock_set_file_result(true); + http_mock_set_memory_result(true); + } +}; + +static void _header_cb(http_transaction_h http_transaction, + char *header, size_t header_len, void *user_data) +{ +} + +static void _body_cb(http_transaction_h http_transaction, + char *body, size_t size, size_t count, void *user_data) +{ +} + +static void _write_cb(http_transaction_h http_transaction, + int recommended_chunk_size, void *user_data) +{ +} + +static void _completed_cb(http_transaction_h http_transaction, void *user_data) +{ +} + +static void _aborted_cb(http_transaction_h http_transaction, + http_error_code_e error, void *user_data) +{ +} + +static void _progress_cb(http_transaction_h http_transaction, + double download_total, double download_now, + double upload_total, double upload_now, void *user_data) +{ +} + +TEST_F(HttpTransactionTest, OpenTransactionN) +{ + http_transaction_h handle; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_session_open_transaction(NULL, + HTTP_METHOD_GET, &handle)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_session_open_transaction(session, + HTTP_METHOD_GET, NULL)); + + + http_mock_set_memory_result(false); + EXPECT_EQ(HTTP_ERROR_OUT_OF_MEMORY, http_session_open_transaction(session, + HTTP_METHOD_GET, &handle)); + http_mock_set_memory_result(true); +} + +TEST_F(HttpTransactionTest, OpenTransactionP) +{ + http_transaction_h handle; + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_open_transaction(session, + HTTP_METHOD_GET, &handle)); + http_transaction_destroy(handle); + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_open_transaction(session, + HTTP_METHOD_OPTIONS, &handle)); + http_transaction_destroy(handle); + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_open_transaction(session, + HTTP_METHOD_HEAD, &handle)); + http_transaction_destroy(handle); + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_open_transaction(session, + HTTP_METHOD_DELETE, &handle)); + http_transaction_destroy(handle); + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_open_transaction(session, + HTTP_METHOD_TRACE, &handle)); + http_transaction_destroy(handle); + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_open_transaction(session, + HTTP_METHOD_POST, &handle)); + http_transaction_destroy(handle); + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_open_transaction(session, + HTTP_METHOD_PUT, &handle)); + http_transaction_destroy(handle); + + EXPECT_EQ(HTTP_ERROR_NONE, http_session_open_transaction(session, + HTTP_METHOD_CONNECT, &handle)); + http_transaction_destroy(handle); +} + +TEST_F(HttpTransactionTest, TransactionDestroyN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_transaction_destroy(NULL)); +} + +TEST_F(HttpTransactionTest, TransactionDestroyP) +{ + http_transaction_h handle; + + http_session_open_transaction(session, HTTP_METHOD_GET, &handle); + + EXPECT_EQ(HTTP_ERROR_NONE, http_transaction_destroy(handle)); +} + + +TEST_F(HttpTransactionTest, SetReceivedHeaderCbN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_received_header_cb(transaction, NULL, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_received_header_cb(NULL, _header_cb, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_received_header_cb(NULL, NULL, NULL)); +} + +TEST_F(HttpTransactionTest, SetReceivedHeaderCbP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_received_header_cb(transaction, _header_cb, NULL)); +} + +TEST_F(HttpTransactionTest, SetReceivedBodyCbN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_received_body_cb(transaction, NULL, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_received_body_cb(NULL, _body_cb, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_received_body_cb(NULL, NULL, NULL)); +} + +TEST_F(HttpTransactionTest, SetReceivedBodyCbP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_received_body_cb(transaction, _body_cb, NULL)); +} + +TEST_F(HttpTransactionTest, SetUploadedCbN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_uploaded_cb(transaction, NULL, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_uploaded_cb(NULL, _write_cb, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_uploaded_cb(NULL, NULL, NULL)); +} + +TEST_F(HttpTransactionTest, SetUploadedCbP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_uploaded_cb(transaction, _write_cb, NULL)); +} + +TEST_F(HttpTransactionTest, SetCompletedCbN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_completed_cb(transaction, NULL, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_completed_cb(NULL, _completed_cb, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_completed_cb(NULL, NULL, NULL)); +} + +TEST_F(HttpTransactionTest, SetCompletedCbP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_completed_cb(transaction, _completed_cb, NULL)); +} + +TEST_F(HttpTransactionTest, SetAbortedCbN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_aborted_cb(transaction, NULL, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_aborted_cb(NULL, _aborted_cb, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_aborted_cb(NULL, NULL, NULL)); +} + +TEST_F(HttpTransactionTest, SetAbortedCbP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_aborted_cb(transaction, _aborted_cb, NULL)); +} + +TEST_F(HttpTransactionTest, SetProgressCbN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_progress_cb(transaction, NULL, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_progress_cb(NULL, _progress_cb, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_progress_cb(NULL, NULL, NULL)); +} + +TEST_F(HttpTransactionTest, SetProgressCbP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_progress_cb(transaction, _progress_cb, NULL)); +} + +TEST_F(HttpTransactionTest, SetTimeoutN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_timeout(transaction, -1)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_timeout(NULL, 100)); +} + +TEST_F(HttpTransactionTest, SetTimeoutP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, http_transaction_set_timeout(transaction, 100)); +} + +TEST_F(HttpTransactionTest, GetTimeoutN) +{ + int timeout; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_timeout(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_timeout(NULL, &timeout)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_timeout(NULL, NULL)); +} + +TEST_F(HttpTransactionTest, GetTimeoutP) +{ + int timeout; + EXPECT_EQ(HTTP_ERROR_NONE, http_transaction_get_timeout(transaction, &timeout)); +} + +TEST_F(HttpTransactionTest, SetReadyToWriteN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, http_transaction_set_ready_to_write(NULL, true)); +} + +TEST_F(HttpTransactionTest, SetReadyToWriteP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, http_transaction_set_ready_to_write(transaction, true)); +} + +TEST_F(HttpTransactionTest, SetInterfaceNameN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_interface_name(NULL, "wlan0")); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_interface_name(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_interface_name(NULL, NULL)); +} + +TEST_F(HttpTransactionTest, SetInterfaceNameP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_interface_name(transaction, "wlan0")); +} + +TEST_F(HttpTransactionTest, GetInterfaceNameN) +{ + char *iface; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_interface_name(NULL, &iface)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_interface_name(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_interface_name(NULL, NULL)); + EXPECT_EQ(HTTP_ERROR_OUT_OF_MEMORY, + http_transaction_get_interface_name(transaction, &iface)); +} + +TEST_F(HttpTransactionTest, GetInterfaceNameP) +{ + char *iface; + + http_transaction_set_interface_name(transaction, "wlan0"); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_get_interface_name(transaction, &iface)); + free(iface); +} + +TEST_F(HttpTransactionTest, SetServerCertificateVerificationN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_server_certificate_verification(NULL, true)); +} + +TEST_F(HttpTransactionTest, SetServerCertificateVerificationP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_server_certificate_verification(transaction, true)); +} + +TEST_F(HttpTransactionTest, GetServerCertificateVerificationN) +{ + bool verify; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_server_certificate_verification(NULL, &verify)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_server_certificate_verification(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_server_certificate_verification(NULL, NULL)); +} + +TEST_F(HttpTransactionTest, GetServerCertificateVerificationP) +{ + bool verify; + + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_get_server_certificate_verification(transaction, &verify)); +} + +TEST_F(HttpTransactionTest, SetTcpFastOpenN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_tcp_fastopen(NULL, true)); +} + +TEST_F(HttpTransactionTest, SetTcpFastOpenP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_tcp_fastopen(transaction, true)); +} + +TEST_F(HttpTransactionTest, GetTcpFastOpenN) +{ + bool tfo; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_tcp_fastopen(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_tcp_fastopen(NULL, &tfo)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_tcp_fastopen(NULL, NULL)); +} + +TEST_F(HttpTransactionTest, GetTcpFastOpenP) +{ + bool tfo; + + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_get_tcp_fastopen(transaction, &tfo)); +} + +TEST_F(HttpTransactionTest, SetCredentialsN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_credentials(transaction, "user", NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_credentials(transaction, NULL, "password!!")); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_credentials(NULL, "user", "password!!")); +} + +TEST_F(HttpTransactionTest, SetCredentialsP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_credentials(transaction, "user", "password!!")); +} + +TEST_F(HttpTransactionTest, GetCredentialsN) +{ + char *user_name; + char *password; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_credentials(transaction, &user_name, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_credentials(transaction, NULL, &password)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_credentials(NULL, &user_name, &password)); +} + +TEST_F(HttpTransactionTest, GetCredentialsP) +{ + char *user_name; + char *password; + + http_transaction_set_credentials(transaction, "user", "password!!"); + + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_get_credentials(transaction, &user_name, &password)); + + free(user_name); + free(password); +} + +TEST_F(HttpTransactionTest, SetHttpAuthSchemeN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_set_http_auth_scheme(NULL, HTTP_AUTH_NONE)); +} + +TEST_F(HttpTransactionTest, SetHttpAuthSchemeP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_http_auth_scheme(transaction, HTTP_AUTH_NONE)); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_http_auth_scheme(transaction, HTTP_AUTH_PROXY_BASIC)); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_http_auth_scheme(transaction, HTTP_AUTH_PROXY_MD5)); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_http_auth_scheme(transaction, HTTP_AUTH_WWW_BASIC)); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_http_auth_scheme(transaction, HTTP_AUTH_WWW_MD5)); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_http_auth_scheme(transaction, HTTP_AUTH_PROXY_NTLM)); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_http_auth_scheme(transaction, HTTP_AUTH_WWW_NTLM)); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_set_http_auth_scheme(transaction, HTTP_AUTH_WWW_NEGOTIATE)); +} + +TEST_F(HttpTransactionTest, GetHttpAuthSchemeN) +{ + http_auth_scheme_e scheme; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_http_auth_scheme(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_http_auth_scheme(NULL, &scheme)); +} + +TEST_F(HttpTransactionTest, GetHttpAuthSchemeP) +{ + http_auth_scheme_e scheme; + + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_get_http_auth_scheme(transaction, &scheme)); +} + +TEST_F(HttpTransactionTest, GetRealmN) +{ + char *realm; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_realm(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_get_realm(NULL, &realm)); + EXPECT_EQ(HTTP_ERROR_INVALID_OPERATION, + http_transaction_get_realm(transaction, &realm)); +} + +TEST_F(HttpTransactionTest, AddHeaderFieldN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_header_add_field(transaction, "Content-Length", NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_header_add_field(transaction, NULL, "1024")); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_header_add_field(NULL, "Content-Length", "1024")); +} + +TEST_F(HttpTransactionTest, AddHeaderFieldP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_header_add_field(transaction, "Content-Length", "1024")); +} + +TEST_F(HttpTransactionTest, RemoveHeaderFieldN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_header_remove_field(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_header_remove_field(NULL, "Content-Length")); + + EXPECT_EQ(HTTP_ERROR_INVALID_OPERATION, + http_transaction_header_remove_field(transaction, "Content-Length")); + + http_transaction_header_add_field(transaction, "User-Agent", "tizen"); + EXPECT_EQ(HTTP_ERROR_INVALID_OPERATION, + http_transaction_header_remove_field(transaction, "Content-Length")); +} + +TEST_F(HttpTransactionTest, RemoveHeaderFieldP) +{ + http_transaction_header_add_field(transaction, "Content-Length", "1024"); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_header_remove_field(transaction, "Content-Length")); +} + +TEST_F(HttpTransactionTest, GetHeaderFieldValueN) +{ + char *value; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_header_get_field_value(transaction, NULL, &value)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_header_get_field_value(transaction, "Content-Length", NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_header_get_field_value(NULL, "Content-Length", &value)); + + EXPECT_EQ(HTTP_ERROR_INVALID_OPERATION, + http_transaction_header_get_field_value(transaction, "Content-Length", &value)); + + http_transaction_header_add_field(transaction, "User-Agent", "tizen"); + EXPECT_EQ(HTTP_ERROR_INVALID_OPERATION, + http_transaction_header_get_field_value(transaction, "Content-Length", &value)); +} + +TEST_F(HttpTransactionTest, GetHeaderFieldValueP) +{ + char *value; + + http_transaction_header_add_field(transaction, "Content-Length", "1024"); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_header_get_field_value(transaction, "Content-Length", &value)); + + free(value); +} + +TEST_F(HttpTransactionTest, SetRequestMethodN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_method(NULL, HTTP_METHOD_GET)); +} + +TEST_F(HttpTransactionTest, SetRequestMethodP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_set_method(transaction, HTTP_METHOD_GET)); +} + +TEST_F(HttpTransactionTest, GetRequestMethodN) +{ + http_method_e method; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_method(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_method(NULL, &method)); +} + +TEST_F(HttpTransactionTest, GetRequestMethodP) +{ + http_method_e method; + + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_get_method(transaction, &method)); +} + +TEST_F(HttpTransactionTest, SetRequestVersionN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_version(NULL, HTTP_VERSION_1_1)); +} + +TEST_F(HttpTransactionTest, SetRequestVersionP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_set_version(transaction, HTTP_VERSION_1_1)); +} + +TEST_F(HttpTransactionTest, GetRequestVersionN) +{ + http_version_e version; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_version(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_version(NULL, &version)); +} + +TEST_F(HttpTransactionTest, GetRequestVersionP) +{ + http_version_e version; + + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_get_version(transaction, &version)); +} + +TEST_F(HttpTransactionTest, SetRequestUriN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_uri(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_uri(NULL, "www.tizen.org")); +} + +TEST_F(HttpTransactionTest, SetRequestUriP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_set_uri(transaction, "www.tizen.org")); +} + +TEST_F(HttpTransactionTest, GetRequestUriN) +{ + char *uri; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_uri(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_uri(NULL, &uri)); + + EXPECT_EQ(HTTP_ERROR_OUT_OF_MEMORY, + http_transaction_request_get_uri(transaction, &uri)); + +} + +TEST_F(HttpTransactionTest, GetRequestUriP) +{ + char *uri; + + http_transaction_request_set_uri(transaction, "www.tizen.org"); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_get_uri(transaction, &uri)); + + free(uri); +} + +TEST_F(HttpTransactionTest, SetRequestAcceptEncondingN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_accept_encoding(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_accept_encoding(NULL, "")); +} + +TEST_F(HttpTransactionTest, SetRequestAcceptEncondingP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_set_accept_encoding(transaction, "")); +} + +TEST_F(HttpTransactionTest, GetRequestAcceptEncodingN) +{ + char *encoding; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_accept_encoding(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_accept_encoding(NULL, &encoding)); + + EXPECT_EQ(HTTP_ERROR_OUT_OF_MEMORY, + http_transaction_request_get_accept_encoding(transaction, &encoding)); + +} + +TEST_F(HttpTransactionTest, GetRequestAcceptEncodingP) +{ + char *encoding; + + http_transaction_request_set_accept_encoding(transaction, ""); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_get_accept_encoding(transaction, &encoding)); + + free(encoding); +} + +TEST_F(HttpTransactionTest, SetRequestCookieN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_cookie(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_cookie(NULL, "")); +} + +TEST_F(HttpTransactionTest, SetRequestCoockeP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_set_cookie(transaction, "name1=var1; name2=var2;")); +} + +TEST_F(HttpTransactionTest, GetRequestCookieN) +{ + char *cookie; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_cookie(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_get_cookie(NULL, &cookie)); + EXPECT_EQ(HTTP_ERROR_OUT_OF_MEMORY, + http_transaction_request_get_cookie(transaction, &cookie)); +} + +TEST_F(HttpTransactionTest, GetRequestCookieP) +{ + char *cookie; + + http_transaction_request_set_cookie(transaction, "name1=var1; name2=var2;"); + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_get_cookie(transaction, &cookie)); + + free(cookie); +} + +TEST_F(HttpTransactionTest, WriteRequestBodyN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_write_body(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_write_body(NULL, "This is a test message.")); +} + +TEST_F(HttpTransactionTest, WriteRequestBodyP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_write_body(transaction, "This is a test message.")); +} + +TEST_F(HttpTransactionTest, SetRequestUploadFileN) +{ + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_upload_file(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_request_set_upload_file(NULL, "/opt/usr/data/test.txt")); +} + +TEST_F(HttpTransactionTest, SetRequestUploadFileP) +{ + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_request_set_upload_file(transaction, "/opt/usr/data/test.txt")); +} + +TEST_F(HttpTransactionTest, GetResponseStatusTextN) +{ + char *status_text; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_response_get_status_text(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_response_get_status_text(NULL, &status_text)); +} + +TEST_F(HttpTransactionTest, GetResponseStatusTextP) +{ + char *status_text; + + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_response_get_status_text(transaction, &status_text)); + + free(status_text); +} + +TEST_F(HttpTransactionTest, GetResponseStatusCodeN) +{ + http_status_code_e status_code; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_response_get_status_code(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_response_get_status_code(NULL, &status_code)); +} + +TEST_F(HttpTransactionTest, GetResponseStatueCodeP) +{ + http_status_code_e status_code; + + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_response_get_status_code(transaction, &status_code)); +} + +TEST_F(HttpTransactionTest, GetResponseVersionN) +{ + http_version_e version; + + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_response_get_version(transaction, NULL)); + EXPECT_EQ(HTTP_ERROR_INVALID_PARAMETER, + http_transaction_response_get_version(NULL, &version)); +} + +TEST_F(HttpTransactionTest, GetResponseVersionP) +{ + http_version_e version; + + EXPECT_EQ(HTTP_ERROR_NONE, + http_transaction_response_get_version(transaction, &version)); +} diff --git a/tests/http-gtest-util.cpp b/tests/http-gtest-util.cpp new file mode 100755 index 0000000..5fc292f --- /dev/null +++ b/tests/http-gtest-util.cpp @@ -0,0 +1,44 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* 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 "http-gtest-util.h" + +#include +#include + +extern "C" { +}; + +GMainLoop *HttpTestUtil::mainLoop = nullptr; +HttpTestUtil::HttpTestUtil(void) +{ +} + +HttpTestUtil::~HttpTestUtil(void) +{ +} + +void HttpTestUtil::runMainLoop(void) +{ + mainLoop = g_main_loop_new(NULL, false); + g_main_loop_run(mainLoop); +} + +void HttpTestUtil::stopMainLoop(void) +{ + if (mainLoop) + g_main_loop_quit(mainLoop); +} diff --git a/tests/http-gtest-util.h b/tests/http-gtest-util.h new file mode 100755 index 0000000..4196e53 --- /dev/null +++ b/tests/http-gtest-util.h @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* 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. +*/ + +#pragma once + +#include + +class HttpTestUtil +{ +public: + HttpTestUtil(void); + ~HttpTestUtil(); + + static void runMainLoop(void); + static void stopMainLoop(void); + +private: + static GMainLoop *mainLoop; +}; diff --git a/tests/mocks/http_cynara.c b/tests/mocks/http_cynara.c new file mode 100644 index 0000000..4982bb2 --- /dev/null +++ b/tests/mocks/http_cynara.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "http_cynara.h" + +#include +#include +#include +#include + +static bool http_mock_cynara_result; + +void http_mock_set_cynara_result(bool result) +{ + http_mock_cynara_result = result; +} + +int __wrap_cynara_check(cynara *p_cynara, + const char *client, const char *client_session, + const char *user, const char *privilege) +{ + if (http_mock_cynara_result) + return CYNARA_API_ACCESS_ALLOWED; + return CYNARA_API_PERMISSION_DENIED; +} diff --git a/tests/mocks/http_cynara.h b/tests/mocks/http_cynara.h new file mode 100644 index 0000000..20dbd6f --- /dev/null +++ b/tests/mocks/http_cynara.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#pragma once + +#include + +void http_mock_set_cynara_result(bool result); diff --git a/tests/mocks/http_file.c b/tests/mocks/http_file.c new file mode 100644 index 0000000..75c719d --- /dev/null +++ b/tests/mocks/http_file.c @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + * + * httpp://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 "http_file.h" + +#include +#include +#include +#include + +#define HTTP_TEST_FILE_PATH "/proc/self/attr/current" + +static bool http_mock_file_result; +static FILE *dummy_fd = NULL; + +extern FILE *__real_fopen(const char *pathname, const char *mode); +extern int __real_fclose(FILE *stream); +extern size_t __real_fread(void *ptr, size_t size, size_t nmemb, FILE *stream); + +void http_mock_set_file_result(bool result) +{ + http_mock_file_result = result; +} + +FILE *__wrap_fopen(const char *pathname, const char *mode) +{ + if (!http_mock_file_result) + return NULL; + + if (strcmp(pathname, HTTP_TEST_FILE_PATH) != 0) + return __real_fopen(pathname, mode); + + if (dummy_fd) + free(dummy_fd); + + dummy_fd = (FILE *)calloc(1, sizeof(FILE)); + return dummy_fd; +} + +FILE *__wrap_fopen64(const char *pathname, const char *mode) +{ + if (!http_mock_file_result) + return NULL; + + return __wrap_fopen(pathname, mode); +} + +int __wrap_fclose(FILE *stream) +{ + if (stream == dummy_fd) { + free(dummy_fd); + dummy_fd = NULL; + return 0; + } + + return __real_fclose(stream); +} + +size_t __wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + if (stream == dummy_fd) + return 0; + + return __real_fread(ptr, size, nmemb, stream); +} diff --git a/tests/mocks/http_file.h b/tests/mocks/http_file.h new file mode 100644 index 0000000..ec199cc --- /dev/null +++ b/tests/mocks/http_file.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + * + * httpp://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. + */ + +#pragma once + +#include + +void http_mock_set_file_result(bool result); diff --git a/tests/mocks/http_memory.c b/tests/mocks/http_memory.c new file mode 100644 index 0000000..e619d27 --- /dev/null +++ b/tests/mocks/http_memory.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "http_memory.h" + +#include +#include +#include + +static bool http_mock_memory_result; + +extern void *__real_malloc(size_t size); + +void http_mock_set_memory_result(bool result) +{ + http_mock_memory_result = result; +} + +void *__wrap_malloc(size_t size) +{ + if (http_mock_memory_result) + return __real_malloc(size); + + return NULL; +} diff --git a/tests/mocks/http_memory.h b/tests/mocks/http_memory.h new file mode 100644 index 0000000..fcfb60a --- /dev/null +++ b/tests/mocks/http_memory.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#pragma once + +#include + +void http_mock_set_memory_result(bool result); diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt new file mode 100644 index 0000000..f9fee27 --- /dev/null +++ b/tools/CMakeLists.txt @@ -0,0 +1,20 @@ +SET(fw_test "${fw_name}_test") + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_test} REQUIRED glib-2.0) +FOREACH(flag ${${fw_test}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -fPIE") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") + +aux_source_directory(. sources) +FOREACH(src ${sources}) + GET_FILENAME_COMPONENT(src_name ${src} NAME_WE) + MESSAGE("${src_name}") + ADD_EXECUTABLE(${src_name} ${src}) + TARGET_LINK_LIBRARIES(${src_name} ${fw_name} ${${fw_test}_LDFLAGS}) +ENDFOREACH() + +INSTALL(TARGETS http_test RUNTIME DESTINATION bin/) diff --git a/tools/http_test.c b/tools/http_test.c new file mode 100644 index 0000000..bb3ae27 --- /dev/null +++ b/tools/http_test.c @@ -0,0 +1,601 @@ +/* + * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd. + * + * 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 +#include +#include +#include +#include + +#include "http.h" +#include "http_internal.h" + +#define ERR(x, y) printf("[ERR] %s(%d)\n", x, y) +#define PRG(x, y) printf("[PRG] %s(%p)\n", x, y) +#define DBG printf +#define MAX_URI_LEN 1024 + +FILE* fp1 = NULL; +FILE* fp2 = NULL; + +http_session_h session = NULL; +int cancel = 0; + +void _register_callbacks(http_transaction_h transaction); +int _write_message_body(http_transaction_h transaction, const char *file_path); + +void __transaction_header_cb(http_transaction_h transaction, char *header, size_t header_len, void *user_data) +{ + PRG("transaction_header_cb", transaction); +} + +void __transaction_body_cb(http_transaction_h transaction, char *body, size_t size, size_t nmemb, void *user_data) +{ + PRG("transaction_body_cb", transaction); + int written = size * nmemb; + DBG("Received length: %d\n", written); + DBG("Received body: %s\n", body); +} + +void __transaction_write_cb(http_transaction_h transaction, int recommended_chunk_size, void *user_data) +{ + PRG("transaction_write_cb", transaction); + DBG("recommended_chunk_size:%d\n", recommended_chunk_size); +} + +void __transaction_completed_cb(http_transaction_h transaction, void *user_data) +{ + PRG("transaction_completed_cb", transaction); + + http_status_code_e status = 0; + int ret; + char *uri = NULL; + char *realm = NULL; + char id[16] = {0, }; + char pw[16] = {0, }; + + ret = http_transaction_request_get_uri(transaction, &uri); + ret = http_transaction_response_get_status_code(transaction, &status); + DBG("%s - status(%d)\n", uri, status); + if (status == HTTP_STATUS_UNAUTHORIZED) { + DBG("Authentication Required\n"); + http_transaction_h http_auth_transaction; + http_auth_scheme_e auth_scheme = HTTP_AUTH_NONE; + + http_transaction_open_authentication(transaction, &http_auth_transaction); + http_transaction_get_http_auth_scheme(http_auth_transaction, &auth_scheme); + + ret = http_transaction_get_realm(http_auth_transaction, &realm); + if (ret != HTTP_ERROR_NONE) + DBG("Fail to get realm\n"); + else + DBG("realm (%s)", realm); + + printf("User ID: "); + ret = scanf("%15s", id); + printf("Password: "); + ret = scanf("%15s", pw); + + http_transaction_set_credentials(http_auth_transaction, id, pw); + _register_callbacks(http_auth_transaction); + http_transaction_submit(http_auth_transaction); + } + + if (realm) + free(realm); + + if (uri) + free(uri); +} + +void __transaction_aborted_cb(http_transaction_h transaction, http_error_code_e reason, void *user_data) +{ + PRG("transaction_aborted_cb", transaction); + DBG("aborted reason: %d\n", reason); + + switch (reason) { + case HTTP_ERROR_OPERATION_FAILED: + DBG("HTTP_ERROR_OPERATION_FAILED\n"); + break; + case HTTP_ERROR_COULDNT_RESOLVE_HOST: + DBG("HTTP_ERROR_COULDNT_RESOLVE_HOST\n"); + break; + case HTTP_ERROR_COULDNT_CONNECT: + DBG("HTTP_ERROR_COULDNT_CONNECT\n"); + break; + case HTTP_ERROR_OPERATION_TIMEDOUT: + DBG("HTTP_ERROR_OPERATION_TIMEDOUT\n"); + break; + case HTTP_ERROR_SSL_CONNECT_ERROR: + DBG("HTTP_ERROR_SSL_CONNECT_ERROR\n"); + break; + case HTTP_ERROR_CANCELED: + DBG("HTTP_ERROR_CANCELED\n"); + break; + default: + DBG("Unknown error\n"); + break; + } + cancel = 0; + + return; +} + +void __transaction_progress_cb(http_transaction_h transaction, double download_total, double download_now, double upload_total, double upload_now, void *user_data) +{ + if (cancel) { + DBG("Cancel"); + http_transaction_cancel(transaction); + } +} + +void _register_callbacks(http_transaction_h transaction) +{ + http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL); + http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL); + http_transaction_set_uploaded_cb(transaction, __transaction_write_cb, NULL); + http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL); + http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL); + http_transaction_set_progress_cb(transaction, __transaction_progress_cb, NULL); +} + +int _write_message_body(http_transaction_h transaction, const char *file_path) +{ + FILE *fp; + size_t chunk_size = 50; + size_t result = 0; + char *buf = malloc(chunk_size + 1); + + fp = fopen(file_path, "rb"); + if (!fp) { + free(buf); + return -1; + } + + while ((result = fread(buf, 1, chunk_size, fp)) > 0) + http_transaction_request_write_body(transaction, strndup(buf, result)); + + free(buf); + fclose(fp); + + return 0; +} + +int test_http_init(void) +{ + int ret = http_init(); + if (ret == HTTP_ERROR_NONE) + return 1; + else return 0; +} + +int test_http_deinit(void) +{ + int ret = http_deinit(); + if (ret == HTTP_ERROR_NONE) + return 1; + else return 0; +} + +int test_http_session_create(void) +{ + int ret; + int session_mode; + + printf("Select session mode(0: NORMAL 1: PIPELINING): "); + ret = scanf("%d", &session_mode); + + ret = http_session_create(session_mode, &session); + if (ret != HTTP_ERROR_NONE) { + ERR("Fail to create session", ret); + return 0; + } + return 1; +} + +int test_http_session_destroy(void) +{ + int ret = http_session_destroy(session); + if (ret != HTTP_ERROR_NONE) { + ERR("Fail to destroy session", ret); + return 0; + } + + session = NULL; + + return 1; +} + +int test_simple_get(void) +{ + char uri[1024]; + int ret; + http_transaction_h transaction = NULL; + http_method_e method; + int tfo = false; + + printf("Input uri: "); + ret = scanf("%1023s", uri); + + ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction); + if (ret != 0) { + ERR("Fail to open transaction", ret); + return 0; + } + + http_transaction_request_get_method(transaction, &method); + ret = http_transaction_request_set_uri(transaction, uri); + if (ret != 0) { + ERR("Fail to set URI", ret); + return 0; + } + + printf("TCP Fast Oepn(0: disable, 1: enable): "); + ret = scanf("%1d", &tfo); + + ret = http_transaction_set_tcp_fastopen(transaction, tfo); + if (ret != 0) { + ERR("Fail to set tfo", ret); + return 0; + } + + _register_callbacks(transaction); + ret = http_transaction_submit(transaction); + if (ret != 0) { + ERR("Fail to submit transaction", ret); + return 0; + } + + return 1; +} + +int test_multiple_get(void) +{ + int ret; + http_transaction_h transaction[10]; + int count; + int i; + + printf("Input count of transactions(1~10): "); + ret = scanf("%d", &count); + + if (count < 0 || count > 10) { + printf("Invalid input!\n"); + return 0; + } + + for (i = 0; i < count; i++) { + char uri[1024]; + printf("Input uri for transaction[%d]: ", i + 1); + ret = scanf("%1023s", uri); + ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction[i]); + if (ret != 0) { + ERR("Fail to open transaction", ret); + return 0; + } + ret = http_transaction_request_set_uri(transaction[i], uri); + if (ret != 0) { + ERR("Fail to set URI", ret); + return 0; + } + _register_callbacks(transaction[i]); + http_transaction_submit(transaction[i]); + } + + return 1; +} + +int test_simple_post(void) +{ + int ret; + http_transaction_h transaction; + const char* post_msg = "name=tizen&project=capi-network-http"; + + ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction); + if (ret != 0) { + ERR("Fail to open transaction", ret); + return 0; + } + ret = http_transaction_request_set_uri(transaction, "http://httpbin.org/post"); + if (ret != 0) { + ERR("Fail to set URI", ret); + return 0; + } + http_transaction_header_add_field(transaction, "Expect", ""); + http_transaction_request_write_body(transaction, post_msg); + + _register_callbacks(transaction); + http_transaction_submit(transaction); + + return 1; +} + +int test_multipart_post(void) +{ + int ret; + http_transaction_h transaction; + char *file_path = "/home/test.txt"; + char *file_name = "test.txt"; + + ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction); + if (ret != 0) { + ERR("Fail to open transaction", ret); + return 0; + } + ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php"); + if (ret != 0) { + ERR("Fail to set URI", ret); + return 0; + } + + ret = http_transaction_request_add_multipart(transaction, HTTP_MULTIPART_FILE, + "sendfile", file_path, NULL); + + ret = http_transaction_request_add_multipart(transaction, HTTP_MULTIPART_CONTENTS, + "filename", file_name, NULL); + + http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL); + http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL); + http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL); + http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL); + http_transaction_set_progress_cb(transaction, __transaction_progress_cb, NULL); + + http_transaction_submit(transaction); + + return 1; +} + +int test_put(void) +{ + http_transaction_h transaction; + const char *file_path = "/home/test.txt"; + int ret = 0; + + ret = http_session_open_transaction(session, HTTP_METHOD_PUT, &transaction); + if (ret != 0) { + ERR("Fail to open transaction", ret); + return 0; + } + + ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php"); + if (ret != 0) { + ERR("Fail to set URI", ret); + return 0; + } + + ret = http_transaction_request_set_upload_file(transaction, file_path); + if (ret != 0) { + ERR("Fail to set file path", ret); + return 0; + } + + _register_callbacks(transaction); + ret = http_transaction_submit(transaction); + if (ret != 0) { + ERR("Fail to submit transaction", ret); + return 0; + } + + return 1; +} + +int test_simple_authentication_get(void) +{ + int ret; + http_transaction_h transaction = NULL; + http_method_e method; + char uri[1024]; + + ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction); + if (ret != 0) { + ERR("Fail to open transaction", ret); + return 0; + } + + printf("Input uri for transaction: "); + ret = scanf("%1023s", uri); + + http_transaction_request_get_method(transaction, &method); + ret = http_transaction_request_set_uri(transaction, uri); + if (ret != 0) { + ERR("Fail to set URI", ret); + return 0; + } + + _register_callbacks(transaction); + ret = http_transaction_submit(transaction); + if (ret != 0) { + ERR("Fail to submit transaction", ret); + return 0; + } + + return 1; +} + +int test_post_chunk(void) +{ + int ret; + http_transaction_h transaction; + char path[1024] = {0, }; + + printf("Input path: "); + ret = scanf("%1023s", path); + + if (strlen(path) < 1) + return 0; + + ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction); + if (ret != 0) { + ERR("Fail to open transaction", ret); + return 0; + } + + ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php"); + if (ret != 0) { + ERR("Fail to set URI", ret); + return 0; + } + + _register_callbacks(transaction); + ret = _write_message_body(transaction, path); + if (ret != 0) { + ERR("Failed write message body", ret); + return 0; + + } + + http_transaction_set_ready_to_write(transaction, TRUE); + http_transaction_header_add_field(transaction, "Transfer-Encoding", "chunked"); + http_transaction_header_add_field(transaction, "Expect", ""); + http_transaction_submit(transaction); + + return 1; +} + +int test_cancel(void) +{ + char uri[1024]; + int ret; + http_transaction_h transaction = NULL; + http_method_e method; + + cancel = 1; + + printf("Input uri: "); + ret = scanf("%1023s", uri); + + ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction); + if (ret != 0) { + ERR("Fail to open transaction", ret); + return 0; + } + + http_transaction_request_get_method(transaction, &method); + ret = http_transaction_request_set_uri(transaction, uri); + if (ret != 0) { + ERR("Fail to set URI", ret); + return 0; + } + + _register_callbacks(transaction); + ret = http_transaction_submit(transaction); + + if (ret != 0) { + ERR("Fail to submit transaction", ret); + return 0; + } + + return 1; +} + +gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) +{ + int rv; + char a[10]; + + printf("Event received from stdin\n"); + + rv = read(0, a, 10); + + if (rv <= 0 || a[0] == '0') + exit(1); + + if (a[0] == '\n' || a[0] == '\r') { + printf("\n\n Network Connection API Test App\n\n"); + printf("Options..\n"); + printf("1 - Initialize\n"); + printf("2 - Deinitialize\n"); + printf("3 - Create session\n"); + printf("4 - Destroy session\n"); + printf("5 - Simple GET\n"); + printf("6 - Multiple GET\n"); + printf("7 - Simple POST\n"); + printf("8 - Multipart POST\n"); + printf("9 - Simple Authentication GET\n"); + printf("a - Upload file (PUT)\n"); + printf("b - Upload file (POST)\n"); + printf("c - Cancel\n"); + printf("0 - Exit \n"); + printf("ENTER - Show options menu.......\n"); + } + + switch (a[0]) { + case '1': + rv = test_http_init(); + break; + case '2': + rv = test_http_deinit(); + break; + case '3': + rv = test_http_session_create(); + break; + case '4': + rv = test_http_session_destroy(); + break; + case '5': + rv = test_simple_get(); + break; + case '6': + rv = test_multiple_get(); + break; + case '7': + rv = test_simple_post(); + break; + case '8': + rv = test_multipart_post(); + break; + case '9': + rv = test_simple_authentication_get(); + break; + case 'a': + rv = test_put(); + break; + case 'b': + rv = test_post_chunk(); + break; + case 'c': + rv = test_cancel(); + break; + } + + if (rv == 1) + printf("Operation succeeded!\n"); + else + printf("Operation failed!\n"); + + return true; +} + +int main(int argc, char **argv) +{ + GMainLoop *mainloop; + +#if !GLIB_CHECK_VERSION(2, 36, 0) + g_type_init(); +#endif + mainloop = g_main_loop_new(NULL, false); + + GIOChannel *channel = g_io_channel_unix_new(0); + g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL); + printf("Test Thread created...\n"); + g_main_loop_run(mainloop); + + return 0; +} + + +