Merge branch 'tizen' into nether
[platform/core/test/security-tests.git] / src / security-manager-tests / test_cases_shm.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include <fcntl.h>
17 #include <linux/xattr.h>
18 #include <string.h>
19 #include <sys/mman.h>
20 #include <sys/smack.h>
21 #include <sys/stat.h>
22
23 #include <memory>
24
25 #include <dpl/test/test_runner.h>
26 #include <dpl/test/test_runner_child.h>
27
28 #include <app_install_helper.h>
29 #include <memory.h>
30 #include <scoped_installer.h>
31 #include <sm_api.h>
32
33 #include <security-manager.h>
34
35 using namespace SecurityManagerTest;
36
37 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER_SHM)
38
39 RUNNER_TEST(shm01_create_file)
40 {
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);
46     char *label = NULL;
47
48     // clean up environment
49     shm_unlink(shmName);
50
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);
54
55     ssize_t len = smack_new_label_from_file(fd, XATTR_NAME_SMACK, &label);
56     RUNNER_ASSERT_MSG(len > 0, "");
57
58     CStringPtr ptr(label);
59
60     RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
61         << " Found: " << label);
62
63     int ret = shm_unlink(shmName);
64     RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
65 }
66
67 RUNNER_TEST(shm02_double_open)
68 {
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);
74     char *label = NULL;
75
76     // clean up environment
77     shm_unlink(shmName);
78
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);
82
83     ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
84     RUNNER_ASSERT_MSG(len > 0, "");
85
86     CStringPtr ptr(label);
87
88     RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
89         << " Found: " << label);
90
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);
94
95     int ret = shm_unlink(shmName);
96     RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
97 }
98
99 RUNNER_TEST(shm03_double_share)
100 {
101     RUNNER_IGNORED_MSG("This test is turn off because SMACK is still disabled for POSIX shared memory");
102     const char *shmName = "shm03_testName";
103
104     AppInstallHelper appa("shm03_testAppA");
105     auto exLabel = appa.generateAppLabel();
106     ScopedInstaller reqa(appa);
107
108     AppInstallHelper appb("shm03_testAppB");
109     ScopedInstaller reqb(appb);
110
111     // clean up environment
112     shm_unlink(shmName);
113
114     char *label = NULL;
115
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);
119
120     ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
121     RUNNER_ASSERT_MSG(len > 0, "");
122
123     CStringPtr ptr(label);
124
125     RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
126         << " Found: " << label);
127
128     int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appb.getAppId().c_str());
129     int err = errno;
130     RUNNER_ASSERT_ERRNO_MSG(fd2 < 0 && err == EACCES, "security_manager_shm_open should failed with EPERM but is");
131     FdUniquePtr file2(&fd2);
132
133     int ret = shm_unlink(shmName);
134     RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
135 }
136
137 RUNNER_TEST(shm04_performance_test_shm_open)
138 {
139     RUNNER_IGNORED_MSG("This test is turn off because SMACK is still disabled for POSIX shared memory");
140     const char *shmName = "shm04_testName";
141
142     // clean up environment
143     shm_unlink(shmName);
144
145     RUNNER_PERF_TEST_BEGIN(10);
146
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");
150         close(fd);
151         shm_unlink(shmName);
152     }
153
154     RUNNER_PERF_TEST_END();
155 }
156
157 RUNNER_TEST(shm05_performance_test_shm_open_wrapper)
158 {
159     RUNNER_IGNORED_MSG("This test is turn off because SMACK is still disabled for POSIX shared memory");
160     const char *shmName = "shm05_testName";
161
162     AppInstallHelper appa("shm05_app");
163     ScopedInstaller reqa(appa);
164
165     // clean up environment
166     shm_unlink(shmName);
167
168     std::string appId = appa.getAppId();
169
170     RUNNER_PERF_TEST_BEGIN(10);
171
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);
175         close(fd);
176         shm_unlink(shmName);
177     }
178
179     RUNNER_PERF_TEST_END();
180 }
181