Merge branch 'tizen' into yaca
[platform/core/test/security-tests.git] / src / common / access_provider.cpp
1 /*
2  * Copyright (c) 2013 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
32 namespace SecurityServer {
33
34 AccessProvider::AccessProvider(const std::string &myLabel)
35   : m_myLabel(myLabel)
36 {}
37
38 void AccessProvider::allowSS() {
39     m_smackAccess.add(m_myLabel, "System::Run", "x");
40 }
41
42 void AccessProvider::addSubjectRule(const std::string &subject, const std::string &rule) {
43     m_smackAccess.add(subject, m_myLabel, rule);
44 }
45
46 void AccessProvider::addObjectRule(const std::string &object, const std::string &rule) {
47     m_smackAccess.add(m_myLabel, object, rule);
48 }
49
50 void AccessProvider::apply() {
51     m_smackAccess.apply();
52 }
53
54 void AccessProvider::applyAndSwithToUser(int uid, int gid) {
55     RUNNER_ASSERT_MSG(0 == smack_revoke_subject(m_myLabel.c_str()),
56         "Error in smack_revoke_subject(" << m_myLabel << ")");
57     apply();
58     RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(m_myLabel.c_str()),
59         "Error in smack_set_label_for_self.");
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