From: Dongsun Lee Date: Fri, 13 May 2016 02:28:16 +0000 (+0900) Subject: add TCs for contents scanning X-Git-Tag: accepted/tizen/common/20160614.143943^2~131 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F68%2F69368%2F11;p=platform%2Fupstream%2Fcsr-framework.git add TCs for contents scanning Change-Id: Ia1da131da0aab74efec0be15995e0a8496ff36f1 Signed-off-by: Dongsun Lee --- diff --git a/engine/content-screening/resources/csret_cs_virus_signatures b/engine/content-screening/resources/csret_cs_virus_signatures index 989d1fd..37af007 100644 --- a/engine/content-screening/resources/csret_cs_virus_signatures +++ b/engine/content-screening/resources/csret_cs_virus_signatures @@ -17,3 +17,9 @@ severity=MEDIUM threat_type=RISKY detailed_url=http://medium.malware.com signature=RISKY_MALWARE + +name=test_generic +severity=LOW +threat_type=GENERIC +detailed_url=http://low.malware.com +signature=GENERIC_MALWARE diff --git a/packaging/csr-framework.spec b/packaging/csr-framework.spec index 8f3f640..6878fd7 100644 --- a/packaging/csr-framework.spec +++ b/packaging/csr-framework.spec @@ -80,6 +80,7 @@ License: Apache-2.0 and BSL-1.0 Group: Security/Testing BuildRequires: boost-devel BuildRequires: pkgconfig(pkgmgr-info) +BuildRequires: pkgconfig(glib-2.0) Requires: %{name} = %{version} %description test @@ -89,6 +90,14 @@ test program of csr-framework %setup -q %build + +# define build architecture +%ifarch %{ix86} +%define test_target emulator +%else +%define test_target target +%endif + %cmake . \ -DCMAKE_BUILD_TYPE=%{?build_type:%build_type}%{!?build_type:RELEASE} \ -DCMAKE_VERBOSE_MAKEFILE=ON \ @@ -106,6 +115,7 @@ test program of csr-framework -DSAMPLE_ENGINE_RO_RES_DIR:PATH=%{sample_engine_ro_res_dir} \ -DSAMPLE_ENGINE_RW_WORKING_DIR:PATH=%{sample_engine_rw_working_dir} \ -DSAMPLE_ENGINE_DIR:PATH=%{sample_engine_dir} \ + -DTEST_TARGET=%{test_target} \ -DTEST_DIR:PATH=%{test_dir} make %{?jobs:-j%jobs} diff --git a/packaging/csr-test.manifest.in b/packaging/csr-test.manifest.in index f2c2337..09f5e52 100644 --- a/packaging/csr-test.manifest.in +++ b/packaging/csr-test.manifest.in @@ -22,6 +22,7 @@ + diff --git a/packaging/csr.manifest.in b/packaging/csr.manifest.in index 07e8978..23018e6 100644 --- a/packaging/csr.manifest.in +++ b/packaging/csr.manifest.in @@ -25,6 +25,7 @@ + diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 737bb26..a03d518 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,9 +18,13 @@ # FIND_PACKAGE(Threads REQUIRED) ADD_DEFINITIONS("-DBOOST_TEST_DYN_LINK") +ADD_DEFINITIONS("-DTEST_TARGET=\"${TEST_TARGET}\"") PKG_CHECK_MODULES(${TARGET_CSR_TEST}_DEP REQUIRED + glib-2.0 + pkgmgr + pkgmgr-info ) SET(${TARGET_CSR_TEST}_SRCS @@ -33,6 +37,7 @@ SET(${TARGET_CSR_TEST}_SRCS test-api-engine-web-protection.cpp test-main.cpp test-common.cpp + test-helper.cpp ) INCLUDE_DIRECTORIES( diff --git a/test/resources/emulator/MaliciousTpk.tpk b/test/resources/emulator/MaliciousTpk.tpk new file mode 100644 index 0000000..7bce491 Binary files /dev/null and b/test/resources/emulator/MaliciousTpk.tpk differ diff --git a/test/resources/emulator/MaliciousWgt.wgt b/test/resources/emulator/MaliciousWgt.wgt new file mode 100644 index 0000000..2dca5bb Binary files /dev/null and b/test/resources/emulator/MaliciousWgt.wgt differ diff --git a/test/resources/maliciousapp.tpk b/test/resources/maliciousapp.tpk deleted file mode 100644 index 82b4346..0000000 Binary files a/test/resources/maliciousapp.tpk and /dev/null differ diff --git a/test/resources/target/MaliciousTpk.tpk b/test/resources/target/MaliciousTpk.tpk new file mode 100644 index 0000000..8debd01 Binary files /dev/null and b/test/resources/target/MaliciousTpk.tpk differ diff --git a/test/resources/target/MaliciousWgt.wgt b/test/resources/target/MaliciousWgt.wgt new file mode 100644 index 0000000..2dca5bb Binary files /dev/null and b/test/resources/target/MaliciousWgt.wgt differ diff --git a/test/resources/test_generic_file b/test/resources/test_generic_file new file mode 100644 index 0000000..7c2f4b2 --- /dev/null +++ b/test/resources/test_generic_file @@ -0,0 +1 @@ +aabbccGENERIC_MALWARE112233 diff --git a/test/test-api-content-screening.cpp b/test/test-api-content-screening.cpp index 91340db..7c3ca95 100644 --- a/test/test-api-content-screening.cpp +++ b/test/test-api-content-screening.cpp @@ -25,14 +25,49 @@ #include #include #include +#include #include #include "test-common.h" +#include "test-helper.h" #define TEST_FILE_NORMAL TEST_DIR "/test_normal_file" -#define TEST_FILE_MALWARE TEST_DIR "/test_malware_file" -#define TEST_FILE_RISKY TEST_DIR "/test_risky_file" -#define TEST_APP_ROOT TEST_DIR "/test_app" +#define TEST_FILE_HIGH TEST_DIR "/test_malware_file" +#define TEST_FILE_MEDIUM TEST_DIR "/test_risky_file" +#define TEST_FILE_LOW TEST_DIR "/test_generic_file" + +#define TEST_FILE_MEDIA "/opt/usr/media/test_malware_file" +#define TEST_FILE_TMP "/tmp/test_malware_file" +#define TEST_FILE_APP_ROOT "/opt/usr/apps" +#define TEST_FILE_NO_EXIST "/tmp/not_existing_file" + +#define TEST_WGT_PKG_ID "hFhcNcbE6K" +#define TEST_WGT_APP_ROOT TEST_FILE_APP_ROOT "/" TEST_WGT_PKG_ID +#define TEST_WGT_MAL_FILE TEST_WGT_APP_ROOT "/res/wgt/data/malicious.txt" +#define TEST_WGT_PATH TEST_DIR "/" TEST_TARGET "/MaliciousWgt.wgt" +#define TEST_WGT_TYPE "WGT" + +#define TEST_TPK_PKG_ID "org.example.malicioustpk" +#define TEST_TPK_APP_ROOT TEST_FILE_APP_ROOT "/" TEST_TPK_PKG_ID +#define TEST_TPK_MAL_FILE TEST_TPK_APP_ROOT "/shared/res/malicious.txt" +#define TEST_TPK_PATH TEST_DIR "/" TEST_TARGET "/MaliciousTpk.tpk" +#define TEST_TPK_TYPE "TPK" + +#define MALWARE_HIGH_NAME "test_malware" +#define MALWARE_HIGH_SEVERITY CSR_CS_SEVERITY_HIGH +#define MALWARE_HIGH_DETAILED_URL "http://high.malware.com" +#define MALWARE_HIGH_SIGNATURE "X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*" + +#define MALWARE_MEDIUM_NAME "test_risk" +#define MALWARE_MEDIUM_SEVERITY CSR_CS_SEVERITY_MEDIUM +#define MALWARE_MEDIUM_DETAILED_URL "http://medium.malware.com" +#define MALWARE_MEDIUM_SIGNATURE "RISKY_MALWARE" + +#define MALWARE_LOW_NAME "test_generic" +#define MALWARE_LOW_SEVERITY CSR_CS_SEVERITY_LOW +#define MALWARE_LOW_DETAILED_URL "http://low.malware.com" +#define MALWARE_LOW_SIGNATURE "GENERIC_MALWARE" + BOOST_AUTO_TEST_SUITE(API_CONTENT_SCREENING) @@ -88,7 +123,7 @@ BOOST_AUTO_TEST_CASE(set_values_to_context_negative) EXCEPTION_GUARD_END } -BOOST_AUTO_TEST_CASE(scan_data) +BOOST_AUTO_TEST_CASE(scan_data_normal) { EXCEPTION_GUARD_START @@ -97,14 +132,95 @@ BOOST_AUTO_TEST_CASE(scan_data) csr_cs_detected_h detected; unsigned char data[100] = {0, }; + // no malware detected ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected), CSR_ERROR_NONE); - // no malware detected CHECK_IS_NULL(detected); EXCEPTION_GUARD_END } +BOOST_AUTO_TEST_CASE(scan_data_high) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + unsigned char data[100] = {0, }; + time_t start_time = time(nullptr); + + // severity high detected + memcpy(data, MALWARE_HIGH_SIGNATURE, strlen(MALWARE_HIGH_SIGNATURE)); + ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected), CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, nullptr, false, nullptr); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_data_medium) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + unsigned char data[100] = {0, }; + time_t start_time = time(nullptr); + + // severity medium detected + memcpy(data, MALWARE_MEDIUM_SIGNATURE, strlen(MALWARE_MEDIUM_SIGNATURE)); + ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected), CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_MEDIUM_NAME, MALWARE_MEDIUM_SEVERITY, MALWARE_MEDIUM_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, nullptr, false, nullptr); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_data_low) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + unsigned char data[100] = {0, }; + time_t start_time = time(nullptr); + + // severity low detected + memcpy(data, MALWARE_LOW_SIGNATURE, strlen(MALWARE_LOW_SIGNATURE)); + ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected), CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_LOW_NAME, MALWARE_LOW_SEVERITY, MALWARE_LOW_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, nullptr, false, nullptr); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_data_negative) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + unsigned char data[100] = {0, }; + + ASSERT_IF(csr_cs_scan_data(nullptr, data, sizeof(data), &detected), CSR_ERROR_INVALID_HANDLE); + + ASSERT_IF(csr_cs_scan_data(context, nullptr, sizeof(data), &detected), CSR_ERROR_INVALID_PARAMETER); + + ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), nullptr), CSR_ERROR_INVALID_PARAMETER); + + EXCEPTION_GUARD_END +} + BOOST_AUTO_TEST_CASE(scan_file_normal) { EXCEPTION_GUARD_START @@ -121,32 +237,181 @@ BOOST_AUTO_TEST_CASE(scan_file_normal) EXCEPTION_GUARD_END } -BOOST_AUTO_TEST_CASE(scan_file_malware) +BOOST_AUTO_TEST_CASE(scan_file_high) { EXCEPTION_GUARD_START auto c = Test::Context(); auto context = c.get(); csr_cs_detected_h detected; + time_t start_time = time(nullptr); + + touch_file(TEST_FILE_HIGH); - ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MALWARE, &detected), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_HIGH, &detected), CSR_ERROR_NONE); CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, TEST_FILE_HIGH, false, nullptr); EXCEPTION_GUARD_END } -BOOST_AUTO_TEST_CASE(scan_file_risky) +BOOST_AUTO_TEST_CASE(scan_file_medium) { EXCEPTION_GUARD_START auto c = Test::Context(); auto context = c.get(); csr_cs_detected_h detected; + time_t start_time = time(nullptr); - ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_RISKY, &detected), CSR_ERROR_NONE); + touch_file(TEST_FILE_MEDIUM); + + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIUM, &detected), CSR_ERROR_NONE); CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_MEDIUM_NAME, MALWARE_MEDIUM_SEVERITY, MALWARE_MEDIUM_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, TEST_FILE_MEDIUM, false, nullptr); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_file_low) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + time_t start_time = time(nullptr); + + touch_file(TEST_FILE_LOW); + + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_LOW, &detected), CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_LOW_NAME, MALWARE_LOW_SEVERITY, MALWARE_LOW_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, TEST_FILE_LOW, false, nullptr); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_file_media_dir) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + time_t start_time = time(nullptr); + + copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA); + + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, TEST_FILE_MEDIA, false, nullptr); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_file_tmp_dir) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + time_t start_time = time(nullptr); + + copy_file(TEST_FILE_HIGH, TEST_FILE_TMP); + + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_TMP, &detected), CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, TEST_FILE_TMP, false, nullptr); + + EXCEPTION_GUARD_END +} + +// TODO : Add TC for different directories +BOOST_AUTO_TEST_CASE(scan_file_wgt_dir) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + time_t start_time = time(nullptr); + + uninstall_app(TEST_WGT_PKG_ID); + BOOST_REQUIRE_MESSAGE(install_app(TEST_WGT_PATH, TEST_WGT_TYPE) == true, "FAIL TO INSTALL."); + + ASSERT_IF(csr_cs_scan_file(context, TEST_WGT_MAL_FILE, &detected), CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, TEST_WGT_APP_ROOT, true, TEST_WGT_PKG_ID); + + BOOST_REQUIRE_MESSAGE(uninstall_app(TEST_WGT_PKG_ID) == true, "FAIL TO UNINSTALL."); + + EXCEPTION_GUARD_END +} + +// TODO : Add TC for different directories +BOOST_AUTO_TEST_CASE(scan_file_tpk_dir) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + time_t start_time = time(nullptr); + + uninstall_app(TEST_TPK_PKG_ID); + BOOST_REQUIRE_MESSAGE(install_app(TEST_TPK_PATH, TEST_TPK_TYPE) == true, "FAIL TO INSTALL."); + + ASSERT_IF(csr_cs_scan_file(context, TEST_TPK_MAL_FILE, &detected), CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(detected); + ASSERT_DETECTED(detected, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL); + ASSERT_DETECTED_EXT(detected, start_time, TEST_TPK_APP_ROOT, true, TEST_TPK_PKG_ID); + + BOOST_REQUIRE_MESSAGE(uninstall_app(TEST_TPK_PKG_ID) == true, "FAIL TO UNINSTALL."); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_file_invalid_params) +{ + + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + + ASSERT_IF(csr_cs_scan_file(nullptr, TEST_FILE_NORMAL, &detected), CSR_ERROR_INVALID_HANDLE); + + ASSERT_IF(csr_cs_scan_file(context, nullptr, &detected), CSR_ERROR_INVALID_PARAMETER); + + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_NORMAL, nullptr), CSR_ERROR_INVALID_PARAMETER); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(scan_file_not_existing_file) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_NO_EXIST, &detected), CSR_ERROR_FILE_DO_NOT_EXIST); EXCEPTION_GUARD_END } @@ -158,13 +423,45 @@ BOOST_AUTO_TEST_CASE(get_detected_malware) auto c = Test::Context(); auto context = c.get(); csr_cs_detected_h detected; + csr_cs_detected_h stored; - ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_RISKY, &detected), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIUM, &detected), CSR_ERROR_NONE); CHECK_IS_NOT_NULL(detected); - ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_RISKY, &detected), + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_MEDIUM, &stored), CSR_ERROR_NONE); - CHECK_IS_NOT_NULL(detected); + CHECK_IS_NOT_NULL(stored); + + ASSERT_DETECTED_HANDLE(detected, stored); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_detected_malware_invalid_param) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h stored; + + ASSERT_IF(csr_cs_get_detected_malware(nullptr, TEST_FILE_MEDIUM, &stored), CSR_ERROR_INVALID_HANDLE); + ASSERT_IF(csr_cs_get_detected_malware(context, nullptr, &stored), CSR_ERROR_INVALID_PARAMETER); + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_MEDIUM, nullptr), CSR_ERROR_INVALID_PARAMETER); + + EXCEPTION_GUARD_END; +} + +BOOST_AUTO_TEST_CASE(get_detected_malware_not_existing_file) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h stored; + + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_NO_EXIST, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); EXCEPTION_GUARD_END } @@ -176,25 +473,90 @@ BOOST_AUTO_TEST_CASE(get_detected_malwares) auto c = Test::Context(); auto context = c.get(); csr_cs_detected_list_h detected_list; - csr_cs_detected_h detected; + csr_cs_detected_h detected_high, detected_medium; + csr_cs_detected_h stored; + const char *file_path_actual; size_t cnt = 0; + size_t idx = 0; + size_t compared_cnt = 0; const char *dirs[1] = { TEST_DIR }; - ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MALWARE, &detected), CSR_ERROR_NONE); - CHECK_IS_NOT_NULL(detected); - ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_RISKY, &detected), CSR_ERROR_NONE); - CHECK_IS_NOT_NULL(detected); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_HIGH, &detected_high), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(detected_high); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIUM, &detected_medium), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(detected_medium); ASSERT_IF(csr_cs_get_detected_malwares(context, dirs, sizeof(dirs) / sizeof(const char *), &detected_list, &cnt), CSR_ERROR_NONE); CHECK_IS_NOT_NULL(detected_list); - BOOST_REQUIRE_MESSAGE(cnt >= 2, "Detected malware count is invalid. " - "expected: >= 2, cnt: " << cnt); + for (idx = 0; idx < cnt; idx++) { + ASSERT_IF(csr_cs_dlist_get_detected(detected_list, idx, &stored), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(stored); + ASSERT_IF(csr_cs_detected_get_file_name(stored, &file_path_actual), CSR_ERROR_NONE); + if (strcmp(TEST_FILE_HIGH, file_path_actual) == 0) { + ASSERT_DETECTED_HANDLE(detected_high, stored); + compared_cnt++; + } else if (strcmp(TEST_FILE_MEDIUM, file_path_actual) == 0) { + ASSERT_DETECTED_HANDLE(detected_medium, stored); + compared_cnt++; + } else { + continue; + } + } + + BOOST_REQUIRE_MESSAGE(compared_cnt == 2, "Detected malware count is invalid. " + "expected: == 2, cnt: " << compared_cnt); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_detected_malwares_invalid_params) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_list_h detected_list; + size_t cnt = 0; + + const char *dirs[1] = { + TEST_DIR + }; + + ASSERT_IF(csr_cs_get_detected_malwares(nullptr, dirs, sizeof(dirs) / sizeof(const char *), + &detected_list, &cnt), CSR_ERROR_INVALID_HANDLE); + ASSERT_IF(csr_cs_get_detected_malwares(context, nullptr, sizeof(dirs) / sizeof(const char *), + &detected_list, &cnt), CSR_ERROR_INVALID_PARAMETER); + ASSERT_IF(csr_cs_get_detected_malwares(context, dirs, sizeof(dirs) / sizeof(const char *), + nullptr, &cnt), CSR_ERROR_INVALID_PARAMETER); + ASSERT_IF(csr_cs_get_detected_malwares(context, dirs, sizeof(dirs) / sizeof(const char *), + &detected_list, nullptr), CSR_ERROR_INVALID_PARAMETER); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_detected_malwares_not_existing_dir) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_list_h detected_list; + size_t cnt = 0; + + const char *dirs[1] = { + TEST_FILE_NO_EXIST + }; + + ASSERT_IF(csr_cs_get_detected_malwares(context, dirs, sizeof(dirs) / sizeof(const char *), + &detected_list, &cnt), CSR_ERROR_NONE); + + ASSERT_IF(cnt, static_cast(0)); EXCEPTION_GUARD_END } @@ -205,18 +567,49 @@ BOOST_AUTO_TEST_CASE(get_ignored_malware) auto c = Test::Context(); auto context = c.get(); - csr_cs_detected_h detected; + csr_cs_detected_h detected, stored; - ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MALWARE, &detected), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_HIGH, &detected), CSR_ERROR_NONE); CHECK_IS_NOT_NULL(detected); ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_IGNORE), CSR_ERROR_NONE); - ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MALWARE, &detected), + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_HIGH, &stored), CSR_ERROR_NONE); - CHECK_IS_NOT_NULL(detected); + CHECK_IS_NOT_NULL(stored); + + ASSERT_DETECTED_HANDLE(detected, stored); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_ignored_malware_invalid_param) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h stored; + + ASSERT_IF(csr_cs_get_ignored_malware(nullptr, TEST_FILE_MEDIUM, &stored), CSR_ERROR_INVALID_HANDLE); + ASSERT_IF(csr_cs_get_ignored_malware(context, nullptr, &stored), CSR_ERROR_INVALID_PARAMETER); + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MEDIUM, nullptr), CSR_ERROR_INVALID_PARAMETER); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_ignored_malware_not_existing_file) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h stored; + + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_NO_EXIST, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); EXCEPTION_GUARD_END } @@ -228,29 +621,93 @@ BOOST_AUTO_TEST_CASE(get_ignored_malwares) auto c = Test::Context(); auto context = c.get(); csr_cs_detected_list_h ignored_list; - csr_cs_detected_h detected; + csr_cs_detected_h detected_high, detected_medium, stored; size_t cnt = 0; + size_t idx = 0; + size_t compared_cnt = 0; + const char *file_path_actual; const char *dirs[1] = { TEST_DIR }; - ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MALWARE, &detected), + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_HIGH, &detected_high), CSR_ERROR_NONE); - CHECK_IS_NOT_NULL(detected); - ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_IGNORE), + CHECK_IS_NOT_NULL(detected_high); + ASSERT_IF(csr_cs_judge_detected_malware(context, detected_high, CSR_CS_ACTION_IGNORE), CSR_ERROR_NONE); - ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_RISKY, &detected), + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIUM, &detected_medium), CSR_ERROR_NONE); - CHECK_IS_NOT_NULL(detected); - ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_IGNORE), + CHECK_IS_NOT_NULL(detected_medium); + ASSERT_IF(csr_cs_judge_detected_malware(context, detected_medium, CSR_CS_ACTION_IGNORE), CSR_ERROR_NONE); - ASSERT_IF(csr_cs_get_ignored_malwares(context, dirs, - sizeof(dirs) / sizeof(const char *), + ASSERT_IF(csr_cs_get_ignored_malwares(context, dirs, sizeof(dirs) / sizeof(const char *), &ignored_list, &cnt), CSR_ERROR_NONE); CHECK_IS_NOT_NULL(ignored_list); - ASSERT_IF(cnt, static_cast(2)); + + for (idx = 0; idx < cnt; idx++) { + ASSERT_IF(csr_cs_dlist_get_detected(ignored_list, idx, &stored), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(stored); + ASSERT_IF(csr_cs_detected_get_file_name(stored, &file_path_actual), CSR_ERROR_NONE); + if (strcmp(TEST_FILE_HIGH, file_path_actual) == 0) { + ASSERT_DETECTED_HANDLE(detected_high, stored); + compared_cnt++; + } else if (strcmp(TEST_FILE_MEDIUM, file_path_actual) == 0) { + ASSERT_DETECTED_HANDLE(detected_medium, stored); + compared_cnt++; + } else { + continue; + } + } + + ASSERT_IF(compared_cnt, static_cast(2)); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_ignored_malwares_invalid_params) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_list_h ignored_list; + size_t cnt = 0; + + const char *dirs[1] = { + TEST_DIR + }; + + ASSERT_IF(csr_cs_get_ignored_malwares(nullptr, dirs, sizeof(dirs) / sizeof(const char *), + &ignored_list, &cnt), CSR_ERROR_INVALID_HANDLE); + ASSERT_IF(csr_cs_get_ignored_malwares(context, nullptr, sizeof(dirs) / sizeof(const char *), + &ignored_list, &cnt), CSR_ERROR_INVALID_PARAMETER); + ASSERT_IF(csr_cs_get_ignored_malwares(context, dirs, sizeof(dirs) / sizeof(const char *), + nullptr, &cnt), CSR_ERROR_INVALID_PARAMETER); + ASSERT_IF(csr_cs_get_ignored_malwares(context, dirs, sizeof(dirs) / sizeof(const char *), + &ignored_list, nullptr), CSR_ERROR_INVALID_PARAMETER); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_ignored_malwares_not_existing_dir) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_list_h ignored_list; + size_t cnt = 0; + + const char *dirs[1] = { + TEST_FILE_NO_EXIST + }; + + ASSERT_IF(csr_cs_get_ignored_malwares(context, dirs, sizeof(dirs) / sizeof(const char *), + &ignored_list, &cnt), CSR_ERROR_NONE); + + ASSERT_IF(cnt, static_cast(0)); EXCEPTION_GUARD_END } @@ -261,12 +718,209 @@ BOOST_AUTO_TEST_CASE(judge_detected_malware) auto c = Test::Context(); auto context = c.get(); + csr_cs_detected_h detected, stored; + + // remove earlier test results + copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE); + copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA); + + // store detected malware + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(detected); + + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(stored); + + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + // INGORE + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_IGNORE), CSR_ERROR_NONE); + + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(stored); + + // UNIGNORE + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_UNIGNORE), CSR_ERROR_NONE); + + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(stored); + + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + // REMOVE + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE), CSR_ERROR_NONE); + + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(judge_detected_malware_ignored_rescan) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected, stored; + + // remove earlier test results + copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE); + copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA); + + // store detected malware + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(detected); + + // ignore + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_IGNORE), CSR_ERROR_NONE); + + // rescan + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(detected); + + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(stored); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(judge_detected_malware_remove_file) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected, stored; + + // remove earlier test results + copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE); + copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA); + + // store detected malware + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(detected); + + // remove detected malware file + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE), CSR_ERROR_NONE); + + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + ASSERT_IF(is_file_exist(TEST_FILE_MEDIA), false); + + // make ignored + copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_IGNORE), CSR_ERROR_NONE); + + // remove ignored malware file + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE), CSR_ERROR_NONE); + + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE), CSR_ERROR_NONE); + + ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MEDIA, &stored), CSR_ERROR_NONE); + CHECK_IS_NULL(stored); + + ASSERT_IF(is_file_exist(TEST_FILE_MEDIA), false); + + // remove not existing file + copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA); + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MEDIA, &detected), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE), CSR_ERROR_NONE); + + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE), + CSR_ERROR_FILE_DO_NOT_EXIST); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(judge_detected_malware_remove_wgt) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); csr_cs_detected_h detected; - ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MALWARE, &detected), CSR_ERROR_NONE); + // remove old test data + uninstall_app(TEST_WGT_PKG_ID); + BOOST_REQUIRE_MESSAGE(install_app(TEST_WGT_PATH, TEST_WGT_TYPE) == true, "FAIL TO INSTALL."); + + // remove detected malware file + ASSERT_IF(csr_cs_scan_file(context, TEST_WGT_MAL_FILE, &detected), CSR_ERROR_NONE); CHECK_IS_NOT_NULL(detected); - ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_UNIGNORE), - CSR_ERROR_NONE); + + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE), CSR_ERROR_NONE); + + ASSERT_IF(is_app_exist(TEST_WGT_PKG_ID), false); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(judge_detected_malware_remove_tpk) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + + // remove old test data + uninstall_app(TEST_TPK_PKG_ID); + BOOST_REQUIRE_MESSAGE(install_app(TEST_TPK_PATH, TEST_TPK_TYPE) == true, "FAIL TO INSTALL."); + + // remove detected malware file + ASSERT_IF(csr_cs_scan_file(context, TEST_TPK_MAL_FILE, &detected), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(detected); + + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE), CSR_ERROR_NONE); + + ASSERT_IF(is_app_exist(TEST_TPK_PKG_ID), false); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(judge_detected_malware_invalid_params) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + + ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_HIGH, &detected), CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(detected); + + ASSERT_IF(csr_cs_judge_detected_malware(nullptr, detected, CSR_CS_ACTION_REMOVE), + CSR_ERROR_INVALID_HANDLE); + ASSERT_IF(csr_cs_judge_detected_malware(context, nullptr, CSR_CS_ACTION_REMOVE), + CSR_ERROR_INVALID_PARAMETER); + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, static_cast(-1)), + CSR_ERROR_INVALID_PARAMETER); EXCEPTION_GUARD_END } diff --git a/test/test-helper.cpp b/test/test-helper.cpp new file mode 100644 index 0000000..1fcb177 --- /dev/null +++ b/test/test-helper.cpp @@ -0,0 +1,260 @@ +/* + * Copyright (c) 2016 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 + */ +/* + * @file test-helper.cpp + * @author Dongsun Lee (ds73.lee@samsung.com) + * @version 1.0 + * @brief CSR API test helper + */ + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include "test-common.h" + +void ASSERT_STRING(const char *expected, const char *actual, const char *msg) +{ + if (expected == actual) // true including nullptr + return; + + // null string and empty string are same + if ((expected == nullptr) && (actual != nullptr) && (strlen(actual) == 0)) + return; + if ((actual == nullptr) && (expected != nullptr) && (strlen(expected) == 0)) + return; + + BOOST_REQUIRE_MESSAGE((expected != nullptr) && (actual != nullptr) && (strcmp(expected, actual) == 0), + std::string((msg == nullptr) ? "NULL" : msg) + << ", EXPECTED=" << std::string((expected == nullptr) ? "NULL" : expected) + << ", ACTUAL=" << std::string((actual == nullptr) ? "NULL" : actual)); +} + +void ASSERT_DETECTED(csr_cs_detected_h detected, const char *name, int severity, const char *detailed_url) +{ + csr_cs_severity_level_e d_severity; + const char *d_malware_name; + const char *d_detailed_url; + + ASSERT_IF(csr_cs_detected_get_severity(detected, &d_severity), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_malware_name(detected, &d_malware_name), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_detailed_url(detected, &d_detailed_url), CSR_ERROR_NONE); + + BOOST_REQUIRE_MESSAGE(severity == d_severity, + "EXPECTED=" << severity << ", ACTUAL=" << d_severity); + ASSERT_STRING(name, d_malware_name, "MALWARE NAME CMP FAIL"); + ASSERT_STRING(detailed_url, d_detailed_url, "DETAILED ULR CMP FAIL"); +} + +void ASSERT_DETECTED_EXT(csr_cs_detected_h detected, time_t time, const char *file_name, bool is_app, const char *pkg_id) +{ + time_t d_timestamp; + const char *d_file_name; + bool d_is_app; + const char *d_pkg_id; + + ASSERT_IF(csr_cs_detected_get_timestamp(detected, &d_timestamp), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_file_name(detected, &d_file_name), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_is_app(detected, &d_is_app), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_pkg_id(detected, &d_pkg_id), CSR_ERROR_NONE); + + BOOST_REQUIRE_MESSAGE(time <= d_timestamp, + "TIMESTAMP CMP FAIL. EXPECTED should be smaller than ACTUAL, EXPECTED=" + << time << ", ACTUAL=" << d_timestamp); + ASSERT_STRING(file_name, d_file_name, "NAME CMP FAIL"); + BOOST_REQUIRE_MESSAGE(is_app == d_is_app, + "IS_APP CMP FAIL. EXPECTED=" << is_app << ", ACTUAL=" << d_is_app); + ASSERT_STRING(pkg_id, d_pkg_id, "PKGID CMP FAIL"); +} + +void ASSERT_DETECTED_HANDLE(csr_cs_detected_h expected, csr_cs_detected_h actual) +{ + csr_cs_severity_level_e e_severity, a_severity; + const char *e_malware_name, *a_malware_name; + const char *e_detailed_url, *a_detailed_url; + time_t e_timestamp, a_timestamp; + const char *e_file_name, *a_file_name; + bool e_is_app, a_is_app; + const char *e_pkg_id, *a_pkg_id; + + ASSERT_IF(csr_cs_detected_get_severity(expected, &e_severity), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_malware_name(expected, &e_malware_name), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_detailed_url(expected, &e_detailed_url), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_timestamp(expected, &e_timestamp), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_file_name(expected, &e_file_name), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_is_app(expected, &e_is_app), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_pkg_id(expected, &e_pkg_id), CSR_ERROR_NONE); + + ASSERT_IF(csr_cs_detected_get_severity(actual, &a_severity), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_malware_name(actual, &a_malware_name), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_detailed_url(actual, &a_detailed_url), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_timestamp(actual, &a_timestamp), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_file_name(actual, &a_file_name), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_is_app(actual, &a_is_app), CSR_ERROR_NONE); + ASSERT_IF(csr_cs_detected_get_pkg_id(actual, &a_pkg_id), CSR_ERROR_NONE); + + BOOST_REQUIRE_MESSAGE(e_severity == a_severity, + "EXPECTED=" << e_severity << ", ACTUAL=" << a_severity); + ASSERT_STRING(e_malware_name, a_malware_name, "MALWARE NAME CMP FAIL"); + ASSERT_STRING(e_detailed_url, a_detailed_url, "DETAILED ULR CMP FAIL"); + BOOST_REQUIRE_MESSAGE(e_timestamp == a_timestamp, + "EXPECTED=" << e_timestamp << ", ACTUAL=" << a_timestamp); + ASSERT_STRING(e_file_name, a_file_name, "FILE NAME CMP FAIL"); + BOOST_REQUIRE_MESSAGE(e_is_app == a_is_app, + "EXPECTED=" << e_is_app << ", ACTUAL=" << a_is_app); + ASSERT_STRING(e_pkg_id, a_pkg_id, "PKG ID CMP FAIL"); +} + + +void copy_file(const char *src_file, const char *dest_file) +{ + std::ifstream srce(src_file, std::ios::binary) ; + std::ofstream dest(dest_file, std::ios::binary) ; + dest << srce.rdbuf() ; +} + +void touch_file(const char *file) +{ + struct utimbuf new_times; + time_t now = time(nullptr); + + new_times.actime = now; + new_times.modtime = now; + + utime(file, &new_times); +} + +bool is_file_exist(const char *file) +{ + return (access(file, F_OK) != -1); +} + +bool installed; +GMainLoop *installMainLoop; +int __app_install_cb(int req_id, const char *pkg_type, const char *pkgid, + const char *key, const char *val, const void *pmsg, void *data) +{ + (void) req_id; + (void) pkg_type; + (void) pkgid; + (void) pmsg; + (void) data; + + installed = false; + + if (key && strncmp(key, "end", strlen("end")) == 0) { + if (strncmp(val, "ok", strlen("ok")) == 0) { + installed = true; + g_main_loop_quit(installMainLoop); + g_main_loop_unref(installMainLoop); + } + } + + return 0; +} + +gboolean __app_install_timeout(gpointer) +{ + installed = false; + return TRUE; +} + +bool install_app(const char *app_path, const char *pkg_type) +{ + auto pkgmgr = pkgmgr_client_new(PC_REQUEST); + CHECK_IS_NOT_NULL(pkgmgr); + + installed = false; + int ret = pkgmgr_client_install(pkgmgr, pkg_type, nullptr, app_path, nullptr, PM_QUIET, + __app_install_cb, nullptr); + if (ret <= PKGMGR_R_OK) + return false; + + g_timeout_add_seconds(30, __app_install_timeout, nullptr); + installMainLoop = g_main_loop_new(nullptr, false); + g_main_loop_run(installMainLoop); + pkgmgr_client_free(pkgmgr); + + return installed; +} + + +bool uninstalled; +GMainLoop *uninstallMainLoop; +int __app_uninstall_cb(int req_id, const char *pkg_type, const char *pkgid, const char *key, + const char *val, const void *pmsg, void *data) +{ + (void) req_id; + (void) pkg_type; + (void) pkgid; + (void) pmsg; + (void) data; + + uninstalled = false; + + if (key && strncmp(key, "end", strlen("end")) == 0) { + if (strncmp(val, "ok", strlen("ok")) == 0) { + uninstalled = true; + g_main_loop_quit(uninstallMainLoop); + g_main_loop_unref(uninstallMainLoop); + } + } + + return 0; +} + +gboolean __app_uninstall_timeout(gpointer) +{ + uninstalled = false; + return TRUE; +} + +bool uninstall_app(const char *pkg_id) +{ + auto pkgmgr = pkgmgr_client_new(PC_REQUEST); + CHECK_IS_NOT_NULL(pkgmgr); + + uninstalled = false; + int ret = pkgmgr_client_uninstall(pkgmgr, nullptr, pkg_id, PM_QUIET, __app_uninstall_cb, nullptr); + if (ret <= PKGMGR_R_OK) + return false; + + g_timeout_add_seconds(30, __app_uninstall_timeout, nullptr); + uninstallMainLoop = g_main_loop_new(nullptr, false); + g_main_loop_run(uninstallMainLoop); + pkgmgr_client_free(pkgmgr); + + return uninstalled; +} + +bool is_app_exist(const char *pkg_id) +{ + pkgmgrinfo_pkginfo_h handle; + + if (pkgmgrinfo_pkginfo_get_pkginfo(pkg_id, &handle) != PMINFO_R_OK) + return false; + + pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + return true; +} diff --git a/test/test-helper.h b/test/test-helper.h new file mode 100644 index 0000000..eab62ec --- /dev/null +++ b/test/test-helper.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2016 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 + */ +/* + * @file test-helper.h + * @author Dongsun Lee (ds73.lee@samsung.com) + * @version 1.0 + * @brief CSR API test helper + */ +#include + +void ASSERT_STRING(const char *expected, const char *actual, const char *msg); + +void ASSERT_DETECTED(csr_cs_detected_h detected, const char *name, int severity, const char *detailed_url); + +void ASSERT_DETECTED_EXT(csr_cs_detected_h detected, time_t time, const char *file_name, bool is_app, const char *pkg_id); + +void ASSERT_DETECTED_HANDLE(csr_cs_detected_h expected, csr_cs_detected_h actual); + +void copy_file(const char *src_file, const char *dest_file); + +void touch_file(const char *file); + +bool is_file_exist(const char *file); + +bool install_app(const char *app_path, const char *app_type); + +bool uninstall_app(const char *pkg_id); + +bool is_app_exist(const char *pkg_id);