Prepare environment for libteec tests 84/148084/38
authorakoszewski <a.koszewski@samsung.com>
Mon, 4 Sep 2017 14:23:06 +0000 (16:23 +0200)
committerTomasz Swierczek <t.swierczek@samsung.com>
Tue, 21 Nov 2017 12:26:40 +0000 (12:26 +0000)
Create first test on trustzone branch

Change-Id: I3a30146c645f6168eb9ad2466a3c5b53d05523b3

CMakeLists.txt
packaging/security-tests.manifest
packaging/security-tests.spec
src/CMakeLists.txt
src/libteec-tests/CMakeLists.txt [new file with mode: 0644]
src/libteec-tests/libteec_tests.cpp [new file with mode: 0644]
src/libteec-tests/ta-files/optee/00000000-0000-0000-0000-746374657374.ta [new file with mode: 0644]
src/libteec-tests/ta-files/simulator/00000000000000000000746374657374 [new file with mode: 0644]
src/libteec-tests/test_cases.cpp [new file with mode: 0644]
src/security-tests-all.sh
src/security-tests.sh

index a89c09ce2666583c85d3d9e61a9ca89b36be1486..a34fab20c8b4368a605d4b6002f6c89434e1e859 100644 (file)
@@ -64,6 +64,7 @@ IF(BUILD_ALL_TESTS)
     SET(BUILD_CYNARA ON)
     SET(BUILD_WEB ON)
     SET(BUILD_YACA ON)
+    SET(BUILD_LIBTEEC ON)
 ENDIF(BUILD_ALL_TESTS)
 
 # If supported for the target machine, emit position-independent code,suitable
index a65ddaa2d17c4a5708e8f1cc4c24f6417dfe24d7..a6edb18fb5335dbf0e9dfd12f6d954eaf9a639c9 100644 (file)
@@ -11,6 +11,7 @@
         <filesystem path="/usr/bin/cynara-tests" exec_label="_" />
         <filesystem path="/usr/bin/ckm-tests" exec_label="User" />
         <filesystem path="/usr/bin/ckm-tests" exec_label="System" />
+        <filesystem path="/usr/bin/libteec-tests" exec_label="System::Privileged" />
     </assign>
     <request>
         <domain name="_" />
index 709a3e64c0063b2144788d0ea955114d4723cc60..b840990f42d1fecc7294464f0b7a2190668bb49c 100644 (file)
@@ -11,6 +11,7 @@ BuildRequires: cmake
 BuildRequires: libattr-devel
 BuildRequires: pkgconfig(libcap)
 BuildRequires: pkgconfig(libsmack)
+BuildRequires: pkgconfig(tef-libteec)
 BuildRequires: pkgconfig(security-manager)
 BuildRequires: pkgconfig(key-manager)
 BuildRequires: pkgconfig(yaca)
@@ -35,6 +36,7 @@ Requires: diffutils
 
 %global ckm_test_dir %{?TZ_SYS_SHARE:%TZ_SYS_SHARE/ckm-test/}%{!?TZ_SYS_SHARE:/usr/share/ckm-test/}
 %global ckm_rw_data_dir %{?TZ_SYS_DATA:%TZ_SYS_DATA/ckm/}%{!?TZ_SYS_DATA:/opt/data/ckm/}
+%global ta_files_dir %{?TZ_SYS_SHARE:%TZ_SYS_SHARE/libteec-tests/ta-files/}%{!?TZ_SYS_SHARE:/usr/share/libteec-tests/ta-files/}
 
 %description
 Security tests repository - for tests that can't be kept together with code.
@@ -54,10 +56,12 @@ cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \
 %if "%{sec_product_feature_security_mdfpp_enable}" == "1"
         -DSECURITY_MDFPP_STATE_ENABLE=1 \
 %endif
+        -DTARGET_ARCH=%{target} \
         -DCMAKE_VERBOSE_MAKEFILE=ON       \
         -DCYNARA_DB_DIR=%{_localstatedir}/cynara/db \
         -DAPP_USER="security_test_user" \
         -DCKM_TEST_DIR=%{ckm_test_dir} \
+        -DTA_FILES_DIR=%{ta_files_dir} \
         -DCKM_RW_DATA_DIR=%{ckm_rw_data_dir} \
         -DGLOBAL_APP_DIR=%{TZ_SYS_RW_APP} \
         -DLOCAL_APP_DIR="%{TZ_SYS_HOME}/security_test_user/apps_rw"
@@ -100,6 +104,7 @@ echo "security-tests postinst done ..."
 /usr/bin/ckm-tests
 /usr/bin/ckm-integration-tests
 /usr/bin/yaca-test
+%{_bindir}/libteec-tests
 %{ckm_test_dir}/*
 /etc/security-tests
 /usr/lib/security-tests/cynara-tests/plugins/single-policy/*
@@ -112,6 +117,7 @@ echo "security-tests postinst done ..."
 %{_prefix}/share/yaca-test
 %dir %{_prefix}/share/security-tests-cleanup-test
 %{_prefix}/share/security-tests-cleanup-test/*
+%{ta_files_dir}/*
 
 %postun
 id -u security_test_user 1>/dev/null 2>&1 && gum-utils -o -d --uid=`id -u security_test_user`
index 471c28ec55e80928de4246e83a08092acae3136e..6bef3e98ce4a9dc97e3a7e43dfb9c9707e510d56 100644 (file)
@@ -110,3 +110,7 @@ ENDIF(BUILD_WEB)
 IF(BUILD_YACA)
     ADD_SUBDIRECTORY(yaca)
 ENDIF(BUILD_YACA)
+
+IF(BUILD_LIBTEEC)
+    ADD_SUBDIRECTORY(libteec-tests)
+ENDIF(BUILD_LIBTEEC)
diff --git a/src/libteec-tests/CMakeLists.txt b/src/libteec-tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..27482d4
--- /dev/null
@@ -0,0 +1,77 @@
+# 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.
+
+INCLUDE(FindPkgConfig)
+SET(LIBTEEC_TARGET_TEST "libteec-tests")
+
+#files to compile
+SET(LIBTEEC_TARGET_TEST_SOURCES
+    ${PROJECT_SOURCE_DIR}/src/libteec-tests/libteec_tests.cpp
+    ${PROJECT_SOURCE_DIR}/src/libteec-tests/test_cases.cpp
+    )
+
+#dependencies
+PKG_CHECK_MODULES(LIBTEEC_TARGET_DEP
+    REQUIRED
+    tef-libteec
+    libgum
+    security-manager
+    )
+
+#header directories
+INCLUDE_DIRECTORIES(SYSTEM
+    ${LIBTEEC_TARGET_DEP_INCLUDE_DIRS}
+    )
+
+INCLUDE_DIRECTORIES(
+    ${PROJECT_SOURCE_DIR}/src/common/
+    )
+
+ADD_DEFINITIONS("-DTA_FILES_DIR=\"${TA_FILES_DIR}\"")
+
+ADD_EXECUTABLE(${LIBTEEC_TARGET_TEST} ${LIBTEEC_TARGET_TEST_SOURCES})
+
+#linker directories
+TARGET_LINK_LIBRARIES(${LIBTEEC_TARGET_TEST}
+    ${LIBTEEC_TARGET_DEP_LIBRARIES}
+    dpl-test-framework
+    tests-common
+    )
+
+#place for output file
+INSTALL(TARGETS ${LIBTEEC_TARGET_TEST}
+    DESTINATION /usr/bin
+    PERMISSIONS OWNER_READ
+                OWNER_WRITE
+                OWNER_EXECUTE
+                GROUP_READ
+                GROUP_EXECUTE
+                WORLD_READ
+                WORLD_EXECUTE
+    )
+
+if(${TARGET_ARCH} STREQUAL "armv7l")
+    SET(PROJECT_TA_FILES_DIR
+        ${PROJECT_SOURCE_DIR}/src/libteec-tests/ta-files/optee/
+        )
+else(${TARGET_ARCH} STREQUAL "armv7l")
+    SET(PROJECT_TA_FILES_DIR
+        ${PROJECT_SOURCE_DIR}/src/libteec-tests/ta-files/simulator/
+        )
+endif(${TARGET_ARCH} STREQUAL "armv7l")
+
+INSTALL(DIRECTORY
+    ${PROJECT_TA_FILES_DIR}
+    DESTINATION ${TA_FILES_DIR}
+)
\ No newline at end of file
diff --git a/src/libteec-tests/libteec_tests.cpp b/src/libteec-tests/libteec_tests.cpp
new file mode 100644 (file)
index 0000000..8a5f603
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+#include <dpl/test/test_runner.h>
+
+int main (int argc, char *argv[])
+{
+    return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);
+}
diff --git a/src/libteec-tests/ta-files/optee/00000000-0000-0000-0000-746374657374.ta b/src/libteec-tests/ta-files/optee/00000000-0000-0000-0000-746374657374.ta
new file mode 100644 (file)
index 0000000..67eb35f
Binary files /dev/null and b/src/libteec-tests/ta-files/optee/00000000-0000-0000-0000-746374657374.ta differ
diff --git a/src/libteec-tests/ta-files/simulator/00000000000000000000746374657374 b/src/libteec-tests/ta-files/simulator/00000000000000000000746374657374
new file mode 100644 (file)
index 0000000..d0bf7fd
Binary files /dev/null and b/src/libteec-tests/ta-files/simulator/00000000000000000000746374657374 differ
diff --git a/src/libteec-tests/test_cases.cpp b/src/libteec-tests/test_cases.cpp
new file mode 100644 (file)
index 0000000..8b7ff7b
--- /dev/null
@@ -0,0 +1,214 @@
+/*
+ * 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.
+ */
+
+#include <sm_api.h>
+#include <dpl/test/test_runner.h>
+#include <dpl/test/test_runner_child.h>
+#include <app_install_helper.h>
+#include <scoped_installer.h>
+#include <tests_common.h>
+
+#include <tee_client_api.h>
+
+#include <string>
+#include <sstream>
+#include <memory>
+#include <fstream>
+#include <unistd.h>
+#include <iostream>
+#include <iomanip>
+#include <exception>
+
+#define DEFINETOSTR(name) case name: return #name
+
+std::string errToString(TEEC_Result err)
+{
+    switch(err) {
+        DEFINETOSTR(TEEC_SUCCESS);
+        DEFINETOSTR(TEEC_ERROR_GENERIC);
+        DEFINETOSTR(TEEC_ERROR_ACCESS_DENIED);
+        DEFINETOSTR(TEEC_ERROR_CANCEL);
+        DEFINETOSTR(TEEC_ERROR_ACCESS_CONFLICT);
+        DEFINETOSTR(TEEC_ERROR_EXCESS_DATA);
+        DEFINETOSTR(TEEC_ERROR_BAD_FORMAT);
+        DEFINETOSTR(TEEC_ERROR_BAD_PARAMETERS);
+        DEFINETOSTR(TEEC_ERROR_BAD_STATE);
+        DEFINETOSTR(TEEC_ERROR_ITEM_NOT_FOUND);
+        DEFINETOSTR(TEEC_ERROR_NOT_IMPLEMENTED);
+        DEFINETOSTR(TEEC_ERROR_NOT_SUPPORTED);
+        DEFINETOSTR(TEEC_ERROR_NO_DATA);
+        DEFINETOSTR(TEEC_ERROR_OUT_OF_MEMORY);
+        DEFINETOSTR(TEEC_ERROR_BUSY);
+        DEFINETOSTR(TEEC_ERROR_COMMUNICATION);
+        DEFINETOSTR(TEEC_ERROR_SECURITY);
+        DEFINETOSTR(TEEC_ERROR_SHORT_BUFFER);
+        DEFINETOSTR(TEEC_ERROR_EXTERNAL_CANCEL);
+        DEFINETOSTR(TEEC_ERROR_TARGET_DEAD);
+        default:
+            return "Error code not recognised";
+    }
+}
+
+std::string originToString(uint32_t origin)
+{
+    switch(origin) {
+        DEFINETOSTR(TEEC_ORIGIN_API);
+        DEFINETOSTR(TEEC_ORIGIN_COMMS);
+        DEFINETOSTR(TEEC_ORIGIN_TEE);
+        DEFINETOSTR(TEEC_ORIGIN_TRUSTED_APP);
+        default:
+            return "Return origin not recognised";
+    }
+}
+
+#undef DEFINETOSTR
+
+template <typename T>
+void toString(std::ostream& stream, T t)
+{
+    stream << std::hex << std::setw(sizeof(T)*2) << std::setfill('0') << t;
+}
+
+template <>
+void toString<unsigned char>(std::ostream& stream, unsigned char t)
+{
+    stream << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned short>(t);
+}
+
+template <typename T, size_t N>
+void toString(std::ostream& stream, const T (&t)[N])
+{
+    for (size_t i = 0; i < N; i++)
+        toString(stream, t[i]);
+}
+
+std::string uuidToString(const TEEC_UUID& uuid)
+{
+    std::stringstream ss;
+    toString(ss, uuid.timeLow);
+    toString(ss, uuid.timeMid);
+    toString(ss, uuid.timeHiAndVersion);
+    toString(ss, uuid.clockSeqAndNode);
+    return ss.str();
+}
+
+class Ta
+{
+public:
+    explicit Ta(const TEEC_UUID& uuid)
+      : m_uuidStr(uuidToString(uuid))
+    {
+        m_path = "/usr/lib/tastore/";
+        RUNNER_ASSERT_MSG(std::ifstream(m_path).good(), "tastore folder not found");
+
+        std::string src_path = TA_FILES_DIR + m_uuidStr;
+        std::string dest_path = m_path + m_uuidStr;
+        RUNNER_ASSERT_MSG(std::ifstream(src_path).good(), "TA file not found in test assets");
+
+        std::ifstream src(src_path, std::ios::binary);
+        std::ofstream dest(dest_path, std::ios::binary);
+        dest << src.rdbuf();
+
+        RUNNER_ASSERT_MSG(isInstalled(), "Installing test TA failed");
+        int res = smack_set_label_for_path(dest_path.c_str(), XATTR_NAME_SMACK, 1, "_");
+        RUNNER_ASSERT_MSG(res >= 0, "Failed to set smack label on TA file");
+    }
+
+    ~Ta()
+    {
+        try {
+            std::string rmPath = m_path + m_uuidStr;
+            std::remove(rmPath.c_str());
+
+            std::string extPath = m_path + m_uuidStr + "-ext/";
+            std::string extPathUuid = extPath + m_uuidStr;
+
+            rmPath = extPathUuid + ".image";
+            std::remove(rmPath.c_str());
+            rmPath = extPathUuid + ".manifest";
+            std::remove(rmPath.c_str());
+            rmPath = extPath;
+            std::remove(rmPath.c_str());
+        } catch (std::exception& e) {
+            std::cerr << "Exception thrown in SystemTa destructor: " << e.what() << std::endl;
+        } catch (...) {
+            std::cerr << "Unknown exception thrown in SystemTa destructor" << std::endl;
+        }
+    }
+
+    bool isInstalled() const
+    {
+        return std::ifstream(m_path + m_uuidStr).good();
+    }
+
+protected:
+    std::string m_uuidStr;
+    std::string m_path;
+};
+
+RUNNER_TEST_GROUP_INIT(LIBTEEC)
+
+RUNNER_CHILD_TEST(libteec_01_load_TA_as_app)
+{
+    const TEEC_UUID taUuid =
+        { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x74, 0x63, 0x74, 0x65, 0x73, 0x74} };
+
+    Ta ta(taUuid);
+
+    TemporaryTestUser tmpUser("libteec_01_test_user", GUM_USERTYPE_NORMAL);
+    tmpUser.create();
+
+    const std::string privilege = "http://tizen.org/privilege/tee.client";
+    AppInstallHelper app("libteec_01_test_app", tmpUser.getUid());
+    app.addPrivilege(privilege);
+    ScopedInstaller appInstall(app);
+
+    auto fun = [&]()
+    {
+        auto contextPtr = std::unique_ptr<TEEC_Context, decltype(&TEEC_FinalizeContext)>
+            (NULL, &TEEC_FinalizeContext);
+        auto sessionPtr = std::unique_ptr<TEEC_Session, decltype(&TEEC_CloseSession)>
+            (NULL, &TEEC_CloseSession);
+
+        TEEC_Context context;
+        TEEC_Session session;
+
+        SecurityManagerTest::Api::setProcessLabel(app.getAppId());
+        RUNNER_ASSERT_ERRNO_MSG(
+            drop_root_privileges(tmpUser.getUid(), tmpUser.getGid()) == 0,
+            "drop_root_privileges failed");
+
+        TEEC_Result res;
+        res = TEEC_InitializeContext(NULL, &context);
+        contextPtr.reset(&context);
+
+        RUNNER_ASSERT_MSG(res == TEEC_SUCCESS,
+            "Failed to initialize context. Error code: " << errToString(res));
+
+        uint32_t returnOrigin;
+        res = TEEC_OpenSession(&context,
+            &session, &taUuid, TEEC_LOGIN_PUBLIC, NULL, NULL, &returnOrigin);
+        sessionPtr.reset(&session);
+
+        RUNNER_ASSERT_MSG(res == TEEC_SUCCESS,
+            "Opening libteec session returned wrong value: " << errToString(res));
+
+        RUNNER_ASSERT_MSG(returnOrigin == TEEC_ORIGIN_TEE,
+            "Wrong return origin from TEEC_OpenSession: " << originToString(returnOrigin));
+    };
+
+    runInChildParentWait(fun);
+}
index 9ab5b5c8c64c92dadcce613e92e724b270eea471..7c05f364cc77570e4a8f44d59b5d81aa7daa50f3 100644 (file)
@@ -57,6 +57,7 @@ runTest security-manager
 runTest cynara
 runTest ckm
 runTest yaca
+runTest libteec
 
 printSummary
 
index 395625841ac53424a0dda5323f286677463c2880..226ab92d423bb457440f84e720d2376e927ab217 100644 (file)
@@ -52,6 +52,12 @@ case $1 in
     echo
     yaca-test "${@:2}"
     ;;
+"libteec")
+    echo "========================================================================="
+    echo "LIBTEEC TESTS"
+    echo
+    libteec-tests "${@:2}"
+    ;;
 *)
     echo "Correct using:"
     echo "    security_test.sh <module> <args_for_module>"