Merge branch 'ckm' into tizen
[platform/core/test/security-tests.git] / src / common / privilege_manager.cpp
1 /*
2  * Copyright (c) 2018 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 #include <new>
18 #include <glib.h>
19 #include <gmodule.h>
20
21 #include "dpl/test/test_runner.h"
22 #include "privilege_manager.h"
23
24 namespace PrivilegeManager {
25
26 void setPrivacyPrivileges(const uid_t uid, const std::string &pkgId,
27     const std::string &tizenVersion, const std::vector<std::string> &privileges,
28     const privilege_manager_package_type_e &type)
29 {
30     GList *list = nullptr;
31
32     for (const std::string &privilege : privileges) {
33         gchar *str = g_strdup(privilege.c_str());
34         if (str == nullptr) {
35             g_list_free_full(list, g_free);
36             throw std::bad_alloc();
37         }
38
39         GList *listNew = g_list_append(list, str);
40         if (listNew == nullptr) {
41             g_list_free_full(list, g_free);
42             throw std::bad_alloc();
43         }
44
45         list = listNew;
46     }
47
48     int ret = privilege_package_info_set_privacy_privilege(uid, pkgId.c_str(),
49         type, tizenVersion.empty() ? "4.0" : tizenVersion.c_str(), list);
50     g_list_free_full(list, g_free);
51
52     RUNNER_ASSERT_MSG(ret == PRVMGR_ERR_NONE,
53         "privilege_package_info_set_privacy_privilege failed: " << ret);
54 }
55
56 void unsetPrivacyPrivileges(const uid_t uid, const std::string &pkgId)
57 {
58     int ret = privilege_package_info_unset_package_privilege_info(
59         uid, pkgId.c_str());
60
61     RUNNER_ASSERT_MSG(ret == PRVMGR_ERR_NONE,
62         "privilege_package_info_unset_privacy_privilege failed: " << ret);
63 }
64
65 } // namespace PrivilegeManager