SM: Shm prefrormance test
[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     AppInstallHelper app("shm01_app");
41     const char *shmName = "shm01_testName";
42     auto exLabel = app.generateAppLabel();
43     ScopedInstaller req(app);
44     char *label = NULL;
45
46     // clean up environment
47     shm_unlink(shmName);
48
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);
52
53     ssize_t len = smack_new_label_from_file(fd, XATTR_NAME_SMACK, &label);
54     RUNNER_ASSERT_MSG(len > 0, "");
55
56     CStringPtr ptr(label);
57
58     RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
59         << " Found: " << label);
60
61     int ret = shm_unlink(shmName);
62     RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
63 }
64
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);
70     char *label = NULL;
71
72     // clean up environment
73     shm_unlink(shmName);
74
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);
78
79     ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
80     RUNNER_ASSERT_MSG(len > 0, "");
81
82     CStringPtr ptr(label);
83
84     RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
85         << " Found: " << label);
86
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);
90
91     int ret = shm_unlink(shmName);
92     RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
93 }
94
95 RUNNER_TEST(shm03_double_share) {
96     const char *shmName = "shm03_testName";
97
98     AppInstallHelper appa("shm03_testAppA");
99     auto exLabel = appa.generateAppLabel();
100     ScopedInstaller reqa(appa);
101
102     AppInstallHelper appb("shm03_testAppB");
103     ScopedInstaller reqb(appb);
104
105     // clean up environment
106     shm_unlink(shmName);
107
108     char *label = NULL;
109
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);
113
114     ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
115     RUNNER_ASSERT_MSG(len > 0, "");
116
117     CStringPtr ptr(label);
118
119     RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
120         << " Found: " << label);
121
122     int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appb.getAppId().c_str());
123     int err = errno;
124     RUNNER_ASSERT_ERRNO_MSG(fd2 < 0 && err == EACCES, "security_manager_shm_open should failed with EPERM but is");
125     FdUniquePtr file2(&fd2);
126
127     int ret = shm_unlink(shmName);
128     RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
129 }
130
131 RUNNER_TEST(shm04_performance_test_shm_open) {
132     const char *shmName = "shm04_testName";
133
134     // clean up environment
135     shm_unlink(shmName);
136
137     RUNNER_PERF_TEST_BEGIN(10);
138
139     for (int i=0; i<1000; ++i) {
140         int fd = shm_open(shmName, O_CREAT | O_RDWR, 0666);
141         RUNNER_ASSERT_ERRNO_MSG(fd >= 0, "shm_open failed");
142         close(fd);
143         shm_unlink(shmName);
144     }
145
146     RUNNER_PERF_TEST_END();
147 }
148
149 RUNNER_TEST(shm05_performance_test_shm_open_wrapper) {
150     const char *shmName = "shm05_testName";
151
152     AppInstallHelper appa("shm05_app");
153     ScopedInstaller reqa(appa);
154
155     // clean up environment
156     shm_unlink(shmName);
157
158     std::string appId = appa.getAppId();
159
160     RUNNER_PERF_TEST_BEGIN(10);
161
162     for (int i=0; i<1000; ++i) {
163         int fd = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appId.c_str());
164         RUNNER_ASSERT_ERRNO_MSG(fd >= 0, "security_manager_shm_open failed: " << fd << " iteration " << i);
165         close(fd);
166         shm_unlink(shmName);
167     }
168
169     RUNNER_PERF_TEST_END();
170 }
171