SM: Test of security_manager_shm_open wrapper. 73/87573/5
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Wed, 7 Sep 2016 11:36:42 +0000 (13:36 +0200)
committerGerrit Code Review <gerrit@review.vlan103.tizen.org>
Tue, 29 Nov 2016 17:19:32 +0000 (09:19 -0800)
Change-Id: Idf0320067d8815438cb019df4d5caacd04fe2843

src/security-manager-tests/CMakeLists.txt
src/security-manager-tests/test_cases_shm.cpp [new file with mode: 0644]

index 030f206..5bd230b 100644 (file)
@@ -48,6 +48,7 @@ SET(SEC_MGR_SOURCES
     ${PROJECT_SOURCE_DIR}/src/security-manager-tests/test_cases_private_sharing.cpp
     ${PROJECT_SOURCE_DIR}/src/security-manager-tests/test_cases_public_sharing.cpp
     ${PROJECT_SOURCE_DIR}/src/security-manager-tests/test_cases_register_paths.cpp
+    ${PROJECT_SOURCE_DIR}/src/security-manager-tests/test_cases_shm.cpp
     ${PROJECT_SOURCE_DIR}/src/security-manager-tests/test_cases_trusted_sharing.cpp
     ${PROJECT_SOURCE_DIR}/src/security-manager-tests/security_manager_tests.cpp
     ${PROJECT_SOURCE_DIR}/src/security-manager-tests/common/app_install_helper.cpp
@@ -89,6 +90,7 @@ TARGET_LINK_LIBRARIES(${TARGET_SEC_MGR_TESTS}
     ${SEC_MGR_TESTS_DEP_LIBRARIES}
     dpl-test-framework
     tests-common
+    rt
     ${CMAKE_THREAD_LIBS_INIT}
     )
 
diff --git a/src/security-manager-tests/test_cases_shm.cpp b/src/security-manager-tests/test_cases_shm.cpp
new file mode 100644 (file)
index 0000000..d8f8293
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * 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.
+ */
+#include <fcntl.h>
+#include <linux/xattr.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/smack.h>
+#include <sys/stat.h>
+
+#include <memory>
+
+#include <dpl/test/test_runner.h>
+#include <dpl/test/test_runner_child.h>
+
+#include <app_install_helper.h>
+#include <memory.h>
+#include <scoped_installer.h>
+#include <sm_api.h>
+
+#include <security-manager.h>
+
+using namespace SecurityManagerTest;
+
+RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER_SHM)
+
+RUNNER_TEST(shm01_create_file) {
+    AppInstallHelper app("shm01_app");
+    const char *shmName = "shm01_testName";
+    auto exLabel = app.generateAppLabel();
+    ScopedInstaller req(app);
+    char *label = NULL;
+
+    // clean up environment
+    shm_unlink(shmName);
+
+    int fd = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
+    RUNNER_ASSERT_ERRNO_MSG(fd >= 0, "security_manager_shm_open failed");
+    FdUniquePtr file(&fd);
+
+    ssize_t len = smack_new_label_from_file(fd, XATTR_NAME_SMACK, &label);
+    RUNNER_ASSERT_MSG(len > 0, "");
+
+    CStringPtr ptr(label);
+
+    RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
+        << " Found: " << label);
+
+    int ret = shm_unlink(shmName);
+    RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
+}
+
+RUNNER_TEST(shm02_double_open) {
+    AppInstallHelper app("shm02_app");
+    const char *shmName = "shm02_testName";
+    auto exLabel = app.generateAppLabel();
+    ScopedInstaller req(app);
+    char *label = NULL;
+
+    // clean up environment
+    shm_unlink(shmName);
+
+    int fd1 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
+    RUNNER_ASSERT_ERRNO_MSG(fd1 >= 0, "security_manager_shm_open failed");
+    FdUniquePtr file1(&fd1);
+
+    ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
+    RUNNER_ASSERT_MSG(len > 0, "");
+
+    CStringPtr ptr(label);
+
+    RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
+        << " Found: " << label);
+
+    int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
+    RUNNER_ASSERT_ERRNO_MSG(fd2 >= 0, "security_manager_shm_open failed");
+    FdUniquePtr file2(&fd2);
+
+    int ret = shm_unlink(shmName);
+    RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
+}
+
+RUNNER_TEST(shm03_double_share) {
+    const char *shmName = "shm03_testName";
+
+    AppInstallHelper appa("shm03_testAppA");
+    auto exLabel = appa.generateAppLabel();
+    ScopedInstaller reqa(appa);
+
+    AppInstallHelper appb("shm03_testAppB");
+    ScopedInstaller reqb(appb);
+
+    // clean up environment
+    shm_unlink(shmName);
+
+    char *label = NULL;
+
+    int fd1 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appa.getAppId().c_str());
+    RUNNER_ASSERT_ERRNO_MSG(fd1 >= 0, "security_manager_shm_open failed");
+    FdUniquePtr file1(&fd1);
+
+    ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
+    RUNNER_ASSERT_MSG(len > 0, "");
+
+    CStringPtr ptr(label);
+
+    RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
+        << " Found: " << label);
+
+    int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appb.getAppId().c_str());
+    int err = errno;
+    RUNNER_ASSERT_ERRNO_MSG(fd2 < 0 && err == EACCES, "security_manager_shm_open should failed with EPERM but is");
+    FdUniquePtr file2(&fd2);
+
+    int ret = shm_unlink(shmName);
+    RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
+}
+