Merge branch 'ckm' into tizen
[platform/core/test/security-tests.git] / src / common / access_provider.cpp
1 /*
2  * Copyright (c) 2013 - 2019 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 /*
17  * @file        access_provider.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Common functions and macros used in security-tests package.
21  */
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <sys/smack.h>
25
26 #include <map>
27
28 #include <tests_common.h>
29
30 #include <access_provider.h>
31 #include <scoped_process_label.h>
32
33 namespace SecurityServer {
34
35 AccessProvider::AccessProvider(const std::string &myLabel)
36   : m_myLabel(myLabel)
37 {}
38
39 void AccessProvider::allowSS() {
40     m_smackAccess.add(m_myLabel, "System::Run", "x");
41 }
42
43 void AccessProvider::addSubjectRule(const std::string &subject, const std::string &rule) {
44     m_smackAccess.add(subject, m_myLabel, rule);
45 }
46
47 void AccessProvider::addObjectRule(const std::string &object, const std::string &rule) {
48     m_smackAccess.add(m_myLabel, object, rule);
49 }
50
51 void AccessProvider::apply() {
52     m_smackAccess.apply();
53 }
54
55 void AccessProvider::applyAndSwithToUser(int uid, int gid) {
56     RUNNER_ASSERT_MSG(0 == smack_revoke_subject(m_myLabel.c_str()),
57         "Error in smack_revoke_subject(" << m_myLabel << ")");
58     apply();
59     ScopedProcessLabel spl(m_myLabel, false);
60     RUNNER_ASSERT_MSG(0 == setgid(gid),
61         "Error in setgid.");
62     RUNNER_ASSERT_MSG(0 == setuid(uid),
63         "Error in setuid.");
64 }
65
66 } // namespace SecurityServer
67