90099e2a9ddb0f4fb14249e86d5920ae5ab4a30a
[platform/core/test/security-tests.git] / src / security-manager-tests / test_cases_nss.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 <algorithm>
17 #include <string>
18 #include <vector>
19
20 #include <grp.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include <dpl/test/test_runner.h>
25 #include <dpl/test/test_runner_child.h>
26 #include <policy_configuration.h>
27 #include <sm_api.h>
28 #include <sm_policy_request.h>
29 #include <sm_user_request.h>
30 #include <temp_test_user.h>
31
32 #include <security-manager.h>
33
34 using namespace SecurityManagerTest;
35
36 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER_NSS_PLUGIN)
37
38 RUNNER_CHILD_TEST(nss_01_unknown_user) {
39     const std::string newUserName = "nss_01_user";
40     PolicyConfiguration pc;
41     TemporaryTestUser testUser(newUserName, GUM_USERTYPE_NORMAL, false);
42     testUser.create();
43
44     auto gidVector = pc.getGid();
45
46     RUNNER_ASSERT_MSG(0 == initgroups(newUserName.c_str(), 0), "Init groups failed");
47
48     gid_t list[64];
49     int grsize = getgroups(64, list);
50     size_t counter = 0;
51
52     for (size_t i=0; i<gidVector.size(); ++i) {
53         for (int j=0; j<grsize; ++j)
54             if(list[j] == gidVector[i]) {
55                 counter++;
56                 break;
57             }
58     }
59
60     RUNNER_ASSERT_MSG(gidVector.size() == counter,
61         "Process should have all groups related with privileges but it have only " <<
62         counter << " of " << gidVector.size() << " required groups");
63 }
64
65 RUNNER_CHILD_TEST(nss_02_normal_user_all_priv) {
66     const std::string newUserName = "nss_02_user";
67     PolicyConfiguration pc;
68     TemporaryTestUser testUser(newUserName, GUM_USERTYPE_NORMAL, false);
69     testUser.create();
70
71     auto gidVector = pc.getUserGid(PolicyConfiguration::NORMAL);
72
73     UserRequest addUserRequest;
74     addUserRequest.setUid(testUser.getUid());
75     addUserRequest.setUserType(SM_USER_TYPE_NORMAL);
76     Api::addUser(addUserRequest);
77
78     RUNNER_ASSERT_MSG(0 == initgroups(newUserName.c_str(), 0), "Init groups failed");
79
80     gid_t list[64];
81     int grsize = getgroups(64, list);
82     size_t counter = 0;
83
84     for (size_t i=0; i<gidVector.size(); ++i) {
85         for (int j=0; j<grsize; ++j)
86             if(list[j] == gidVector[i]) {
87                 counter++;
88                 break;
89             }
90     }
91
92     RUNNER_ASSERT_MSG(gidVector.size() == counter,
93         "Process should have all groups related with privileges but it have only " <<
94         counter << " of " << gidVector.size() << " required groups");
95 }
96
97 RUNNER_CHILD_TEST(nss_03_normal_user_without_camera) {
98     const std::string newUserName = "nss_03_user";
99     TemporaryTestUser testUser(newUserName, GUM_USERTYPE_NORMAL, false);
100     testUser.create();
101     gid_t cameraPrivId = nameToGid("priv_camera");
102
103     UserRequest addUserRequest;
104     addUserRequest.setUid(testUser.getUid());
105     addUserRequest.setUserType(SM_USER_TYPE_NORMAL);
106     Api::addUser(addUserRequest);
107
108     PolicyRequest policyRequest;
109     PolicyEntry entry(
110         SECURITY_MANAGER_ANY,
111         std::to_string(static_cast<int>(testUser.getUid())),
112         "http://tizen.org/privilege/camera");
113     entry.setMaxLevel("Deny");
114     policyRequest.addEntry(entry);
115     Api::sendPolicy(policyRequest);
116
117     RUNNER_ASSERT_MSG(0 == initgroups(newUserName.c_str(), 0), "Init groups failed");
118
119     gid_t list[64];
120     int grsize = getgroups(64, list);
121     size_t counter = 0;
122
123     for (int i=0; i<grsize; ++i) {
124         if (list[i] == cameraPrivId) {
125             counter++;
126             break;
127         }
128     }
129
130     RUNNER_ASSERT_MSG(0 == counter, "Process should not have priv_camera group");
131
132     PolicyConfiguration pc;
133     auto gidVector = pc.getUserGid(PolicyConfiguration::NORMAL);
134     gidVector.erase(
135         std::remove_if(gidVector.begin(), gidVector.end(), [=](gid_t g) { return g == cameraPrivId; }),
136         gidVector.end());
137
138     for (size_t i=0; i<gidVector.size(); ++i) {
139         for (int j=0; j<grsize; ++j)
140             if(list[j] == gidVector[i]) {
141                 counter++;
142                 break;
143             }
144     }
145
146     RUNNER_ASSERT_MSG(gidVector.size() == counter,
147         "Process should have all groups related with privileges but it have only " <<
148         counter << " of " << gidVector.size() << " required groups");
149 }
150
151 RUNNER_CHILD_TEST(nss_04_guest_user) {
152     const std::string newUserName = "nss_04_user";
153     TemporaryTestUser testUser(newUserName, GUM_USERTYPE_GUEST, false);
154     testUser.create();
155
156     UserRequest addUserRequest;
157     addUserRequest.setUid(testUser.getUid());
158     addUserRequest.setUserType(SM_USER_TYPE_GUEST);
159     Api::addUser(addUserRequest);
160
161     RUNNER_ASSERT_MSG(0 == initgroups(newUserName.c_str(), 0), "Init groups failed");
162
163     gid_t list[64];
164     int grsize = getgroups(64, list);
165     size_t counter = 0;
166
167     PolicyConfiguration pc;
168     auto gidVector = pc.getUserGid(PolicyConfiguration::GUEST);
169
170     for (size_t i=0; i<gidVector.size(); ++i) {
171         for (int j=0; j<grsize; ++j)
172             if(list[j] == gidVector[i]) {
173                 counter++;
174                 break;
175             }
176     }
177
178     RUNNER_ASSERT_MSG(gidVector.size() == counter,
179         "Process should have all groups related with privileges but it have only " <<
180         counter << " of " << gidVector.size() << " required groups");
181 }
182
183