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) {
40 AppInstallHelper app("shm01_app");
41 const char *shmName = "shm01_testName";
42 auto exLabel = app.generateAppLabel();
43 ScopedInstaller req(app);
46 // clean up environment
49 int fd = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
50 RUNNER_ASSERT_ERRNO_MSG(fd >= 0, "security_manager_shm_open failed");
51 FdUniquePtr file(&fd);
53 ssize_t len = smack_new_label_from_file(fd, XATTR_NAME_SMACK, &label);
54 RUNNER_ASSERT_MSG(len > 0, "");
56 CStringPtr ptr(label);
58 RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
59 << " Found: " << label);
61 int ret = shm_unlink(shmName);
62 RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
65 RUNNER_TEST(shm02_double_open) {
66 AppInstallHelper app("shm02_app");
67 const char *shmName = "shm02_testName";
68 auto exLabel = app.generateAppLabel();
69 ScopedInstaller req(app);
72 // clean up environment
75 int fd1 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
76 RUNNER_ASSERT_ERRNO_MSG(fd1 >= 0, "security_manager_shm_open failed");
77 FdUniquePtr file1(&fd1);
79 ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
80 RUNNER_ASSERT_MSG(len > 0, "");
82 CStringPtr ptr(label);
84 RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
85 << " Found: " << label);
87 int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
88 RUNNER_ASSERT_ERRNO_MSG(fd2 >= 0, "security_manager_shm_open failed");
89 FdUniquePtr file2(&fd2);
91 int ret = shm_unlink(shmName);
92 RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
95 RUNNER_TEST(shm03_double_share) {
96 const char *shmName = "shm03_testName";
98 AppInstallHelper appa("shm03_testAppA");
99 auto exLabel = appa.generateAppLabel();
100 ScopedInstaller reqa(appa);
102 AppInstallHelper appb("shm03_testAppB");
103 ScopedInstaller reqb(appb);
105 // clean up environment
110 int fd1 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appa.getAppId().c_str());
111 RUNNER_ASSERT_ERRNO_MSG(fd1 >= 0, "security_manager_shm_open failed");
112 FdUniquePtr file1(&fd1);
114 ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
115 RUNNER_ASSERT_MSG(len > 0, "");
117 CStringPtr ptr(label);
119 RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
120 << " Found: " << label);
122 int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appb.getAppId().c_str());
124 RUNNER_ASSERT_ERRNO_MSG(fd2 < 0 && err == EACCES, "security_manager_shm_open should failed with EPERM but is");
125 FdUniquePtr file2(&fd2);
127 int ret = shm_unlink(shmName);
128 RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");