From c33837648238fbfdac99c1985e391f676bbe580c Mon Sep 17 00:00:00 2001 From: "sangwan.kwon" Date: Mon, 10 Apr 2017 19:02:12 +0900 Subject: [PATCH] Add SSL communication testcase Change-Id: I9936cab98398e0abb550b997ebe8026171a44739 Signed-off-by: sangwan.kwon --- packaging/trust-anchor.spec | 3 ++- tests/CMakeLists.txt | 9 +++++++-- tests/main.cpp | 6 ++++++ tests/script/CMakeLists.txt | 7 ++++--- tests/script/pre-test.sh.in | 5 +++++ tests/test-curl.cpp | 38 ++++++++++++++++++++++++++++++++++++++ tests/test-launcher.cpp | 22 ++++++++++++++++++++-- tests/test-util.cpp | 27 +++++++++++++++++++++++++++ tests/test-util.hxx | 2 ++ 9 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 tests/test-curl.cpp diff --git a/packaging/trust-anchor.spec b/packaging/trust-anchor.spec index 3afeae3..a809008 100644 --- a/packaging/trust-anchor.spec +++ b/packaging/trust-anchor.spec @@ -102,6 +102,7 @@ The package provides Trust Anchor API development files. %package -n trust-anchor-test Summary: Trust Anchor API test Group: Development/Libraries +BuildRequires: pkgconfig(libcurl) %description -n trust-anchor-test Testcases for trust anchor library @@ -111,6 +112,6 @@ Testcases for trust anchor library %{_bindir}/%{lib_name}-test-launcher %{_bindir}/%{lib_name}-test-clauncher %{_bindir}/%{lib_name}-test-internal +%{_bindir}/%{lib_name}-test-init.sh %{TZ_SYS_DATA}/%{lib_name}/test %{TZ_SYS_DATA}/%{lib_name}/test/certs -%{TZ_SYS_DATA}/%{lib_name}/test/script diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0a8da00..3ed8e83 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,15 +16,18 @@ # @author Sangwan Kwon (sangwan.kwon@samsung.com) # @breif Make trust anchor test program # +PKG_CHECK_MODULES(${TARGET_TANCHOR_TEST}_DEP REQUIRED libcurl) INCLUDE_DIRECTORIES(SYSTEM . ${TANCHOR_INCLUDE} ${TANCHOR_SRC} - ${${TARGET_TANCHOR_LIB}_DEP_INCLUDE_DIRS}) + ${${TARGET_TANCHOR_LIB}_DEP_INCLUDE_DIRS} + ${${TARGET_TANCHOR_TEST}_DEP_INCLUDE_DIRS}) FUNCTION(BUILD_TEST TEST_NAME TEST_SRCS) ADD_EXECUTABLE(${TEST_NAME} ${TEST_SRCS}) - TARGET_LINK_LIBRARIES(${TEST_NAME} ${TARGET_TANCHOR_LIB}) + TARGET_LINK_LIBRARIES(${TEST_NAME} ${TARGET_TANCHOR_LIB} + ${${TARGET_TANCHOR_TEST}_DEP_LIBRARIES}) SET_SOURCE_FILES_PROPERTIES(${TEST_SRCS} PROPERTIES COMPILE_FLAGS "-D_GNU_SOURCE -fPIE") @@ -52,6 +55,8 @@ SET(CAPI_LAUNCHER_SRCS main.cpp test-util.cpp test-capi-launcher.cpp) SET(INTERNAL_SRCS main.cpp + test-curl.cpp + test-util.cpp test-certificate.cpp) SET(TEST_INSTALLER ${TARGET_TANCHOR_TEST}-installer) diff --git a/tests/main.cpp b/tests/main.cpp index 2704320..3602af3 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -24,11 +24,17 @@ #include +#include + int main(int /*argc*/, char** /*argv*/) { audit::Logger::setLogLevel(audit::LogLevel::Trace); + curl_global_init(CURL_GLOBAL_DEFAULT); + testbench::Testbench::runAllTestSuites(); + curl_global_cleanup(); + return 0; } diff --git a/tests/script/CMakeLists.txt b/tests/script/CMakeLists.txt index 2b763c6..eea3de8 100644 --- a/tests/script/CMakeLists.txt +++ b/tests/script/CMakeLists.txt @@ -16,9 +16,10 @@ # @author Sangwan Kwon (sangwan.kwon@samsung.com) # -CONFIGURE_FILE(pre-test.sh.in pre-test.sh @ONLY) -INSTALL(FILES pre-test.sh - DESTINATION ${TANCHOR_TEST}/script +SET(TEST_INIT ${TARGET_TANCHOR_TEST}-init.sh) +CONFIGURE_FILE(pre-test.sh.in ${TEST_INIT} @ONLY) +INSTALL(FILES ${TEST_INIT} + DESTINATION ${BIN_INSTALL_DIR} PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE diff --git a/tests/script/pre-test.sh.in b/tests/script/pre-test.sh.in index c03974b..37a600e 100755 --- a/tests/script/pre-test.sh.in +++ b/tests/script/pre-test.sh.in @@ -25,6 +25,8 @@ LAUNCHER=@BIN_INSTALL_DIR@/@TEST_LAUNCHER@ CLAUNCHER=@BIN_INSTALL_DIR@/@TEST_CLAUNCHER@ INTERNAL=@BIN_INSTALL_DIR@/@TEST_INTERNAL@ +CURL=@BIN_INSTALL_DIR@/curl + declare -a TESTS=("$INSTALLER $LAUNCHER $CLAUNCER $INTERNAL") for t in "${TESTS[@]}" @@ -36,6 +38,9 @@ for t in "${TESTS[@]}" chsmack -e "@SMACK_LABEL@" $t done +# curl should be System label for https communication test +chsmack -e "@SMACK_LABEL@" $CURL + # launch() needs CAP_SYS_ADMIN setcap cap_sys_admin+ep $LAUNCHER setcap cap_sys_admin+ep $CLAUNCHER diff --git a/tests/test-curl.cpp b/tests/test-curl.cpp new file mode 100644 index 0000000..580695f --- /dev/null +++ b/tests/test-curl.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2017 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-curl.cpp + * @author Sangwan Kwon (sangwan.kwon@samsung.com) + * @version 0.1 + * @brief Unit test program of Curl + */ + +#include + +#include +#include + +#include "test-util.hxx" +#include "test-resource.hxx" + +TESTCASE(CONNECT_SSL) +{ + int ret = test::util::connectSSL("https://google.com"); + TEST_EXPECT(true, ret == 0); + + if (ret != 0) + std::cout << "Check wifi connection.." << std::endl; +} diff --git a/tests/test-launcher.cpp b/tests/test-launcher.cpp index 0e2681c..aa45a27 100644 --- a/tests/test-launcher.cpp +++ b/tests/test-launcher.cpp @@ -26,6 +26,8 @@ #include +#include + #include "test-util.hxx" #include "test-resource.hxx" @@ -36,25 +38,41 @@ TESTCASE(TRUST_ANCHOR_LAUNCH) auto beforeCat = test::util::cat(TZ_SYS_RO_CA_BUNDLE); tanchor::TrustAnchor ta(DUMMY_PKG_ID, APP_CERTS_DIR); - int ret = ta.install(true); + int ret = ta.install(false); + + std::cout << "## Before trust-anchor launch#############" << std::endl; + ret = test::util::connectSSL("https://google.com"); + std::cout << "##########################################" << std::endl; + TEST_EXPECT(true, ret == 0); // pre-condition int pid = fork(); if (pid == 0) { - ret = ta.launch(true); + ret = ta.launch(false); TEST_EXPECT(true, ret == 0); + // check file-system auto afterLsChild = test::util::ls(TZ_SYS_RO_CA_CERTS); TEST_EXPECT(true, beforeLs != afterLsChild); auto afterCatChild = test::util::cat(TZ_SYS_RO_CA_BUNDLE); TEST_EXPECT(true, beforeCat != afterCatChild); + + // check SSL communication + std::cout << "## After trust-anchor launch(APP)#########" << std::endl; + ret = test::util::connectSSL("https://google.com"); + std::cout << "##########################################" << std::endl; + TEST_EXPECT(false, ret == 0); + } else { auto afterLsParent = test::util::ls(TZ_SYS_RO_CA_CERTS); TEST_EXPECT(true, beforeLs == afterLsParent); auto afterCatParent = test::util::cat(TZ_SYS_RO_CA_BUNDLE); TEST_EXPECT(true, beforeCat == afterCatParent); + + ret = test::util::connectSSL("https://google.com"); + TEST_EXPECT(true, ret == 0); } } diff --git a/tests/test-util.cpp b/tests/test-util.cpp index 07ebbe3..5aed04c 100644 --- a/tests/test-util.cpp +++ b/tests/test-util.cpp @@ -23,10 +23,13 @@ #include +#include #include #include #include +#include + namespace test { namespace util { @@ -66,5 +69,29 @@ std::string cat(const char *path) return ret; } +int connectSSL(const std::string &addr) +{ + if (addr.empty()) + return -1; + + using CURLPtr = std::unique_ptr; + CURLPtr curl(::curl_easy_init(), ::curl_easy_cleanup); + if (curl == nullptr) { + std::cout << "Failed to get curl object." << std::endl; + return -1; + } + + curl_easy_setopt(curl.get(), CURLOPT_URL, addr.c_str()); + + CURLcode res = curl_easy_perform(curl.get()); + if(res != CURLE_OK) { + std::cout << "Failed to connect failed: " + << curl_easy_strerror(res) << std::endl; + return -1; + } + + return 0; +} + } // namespace util } // namespace test diff --git a/tests/test-util.hxx b/tests/test-util.hxx index f1099ee..aecb816 100644 --- a/tests/test-util.hxx +++ b/tests/test-util.hxx @@ -30,5 +30,7 @@ namespace util { std::string ls(const char *path); std::string cat(const char *path); +int connectSSL(const std::string &addr); + } // namespace util } // namespace test -- 2.7.4