2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <linux/xattr.h>
20 #include <sys/smack.h>
25 #include <dpl/test/test_runner.h>
26 #include <dpl/test/test_runner_child.h>
28 #include <app_install_helper.h>
30 #include <scoped_installer.h>
33 #include <security-manager.h>
35 using namespace SecurityManagerTest;
37 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER_SHM)
39 RUNNER_TEST(shm01_create_file)
41 RUNNER_IGNORED_MSG("This test is turn off because SMACK is still disabled for POSIX shared memory");
42 AppInstallHelper app("shm01_app");
43 const char *shmName = "shm01_testName";
44 auto exLabel = app.generateAppLabel();
45 ScopedInstaller req(app);
48 // clean up environment
51 int fd = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
52 RUNNER_ASSERT_ERRNO_MSG(fd >= 0, "security_manager_shm_open failed");
53 FdUniquePtr file(&fd);
55 ssize_t len = smack_new_label_from_file(fd, XATTR_NAME_SMACK, &label);
56 RUNNER_ASSERT_MSG(len > 0, "");
58 CStringPtr ptr(label);
60 RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
61 << " Found: " << label);
63 int ret = shm_unlink(shmName);
64 RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
67 RUNNER_TEST(shm02_double_open)
69 RUNNER_IGNORED_MSG("This test is turn off because SMACK is still disabled for POSIX shared memory");
70 AppInstallHelper app("shm02_app");
71 const char *shmName = "shm02_testName";
72 auto exLabel = app.generateAppLabel();
73 ScopedInstaller req(app);
76 // clean up environment
79 int fd1 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
80 RUNNER_ASSERT_ERRNO_MSG(fd1 >= 0, "security_manager_shm_open failed");
81 FdUniquePtr file1(&fd1);
83 ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
84 RUNNER_ASSERT_MSG(len > 0, "");
86 CStringPtr ptr(label);
88 RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
89 << " Found: " << label);
91 int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
92 RUNNER_ASSERT_ERRNO_MSG(fd2 >= 0, "security_manager_shm_open failed");
93 FdUniquePtr file2(&fd2);
95 int ret = shm_unlink(shmName);
96 RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
99 RUNNER_TEST(shm03_double_share)
101 RUNNER_IGNORED_MSG("This test is turn off because SMACK is still disabled for POSIX shared memory");
102 const char *shmName = "shm03_testName";
104 AppInstallHelper appa("shm03_testAppA");
105 auto exLabel = appa.generateAppLabel();
106 ScopedInstaller reqa(appa);
108 AppInstallHelper appb("shm03_testAppB");
109 ScopedInstaller reqb(appb);
111 // clean up environment
116 int fd1 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appa.getAppId().c_str());
117 RUNNER_ASSERT_ERRNO_MSG(fd1 >= 0, "security_manager_shm_open failed");
118 FdUniquePtr file1(&fd1);
120 ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
121 RUNNER_ASSERT_MSG(len > 0, "");
123 CStringPtr ptr(label);
125 RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
126 << " Found: " << label);
128 int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appb.getAppId().c_str());
130 RUNNER_ASSERT_ERRNO_MSG(fd2 < 0 && err == EACCES, "security_manager_shm_open should failed with EPERM but is");
131 FdUniquePtr file2(&fd2);
133 int ret = shm_unlink(shmName);
134 RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
137 RUNNER_TEST(shm04_performance_test_shm_open)
139 RUNNER_IGNORED_MSG("This test is turn off because SMACK is still disabled for POSIX shared memory");
140 const char *shmName = "shm04_testName";
142 // clean up environment
145 RUNNER_PERF_TEST_BEGIN(10);
147 for (int i=0; i<1000; ++i) {
148 int fd = shm_open(shmName, O_CREAT | O_RDWR, 0666);
149 RUNNER_ASSERT_ERRNO_MSG(fd >= 0, "shm_open failed");
154 RUNNER_PERF_TEST_END();
157 RUNNER_TEST(shm05_performance_test_shm_open_wrapper)
159 RUNNER_IGNORED_MSG("This test is turn off because SMACK is still disabled for POSIX shared memory");
160 const char *shmName = "shm05_testName";
162 AppInstallHelper appa("shm05_app");
163 ScopedInstaller reqa(appa);
165 // clean up environment
168 std::string appId = appa.getAppId();
170 RUNNER_PERF_TEST_BEGIN(10);
172 for (int i=0; i<1000; ++i) {
173 int fd = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appId.c_str());
174 RUNNER_ASSERT_ERRNO_MSG(fd >= 0, "security_manager_shm_open failed: " << fd << " iteration " << i);
179 RUNNER_PERF_TEST_END();