684028146bb5be1b2a92b93950faef7584ae14d6
[platform/core/test/security-tests.git] / src / security-manager-tests / common / sm_commons.h
1 /*
2  * Copyright (c) 2016-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 #pragma once
18
19 #include <ftw.h>
20 #include <string>
21 #include <sys/capability.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <vector>
25 #include <functional>
26
27 #include <security-manager-types.h>
28
29 #include <app_install_helper.h>
30 #include <memory.h>
31 #include <temp_test_user.h>
32 #include <tzplatform.h>
33
34 DEFINE_SMARTPTR(cap_free, _cap_struct, CapsSetsUniquePtr);
35
36 const int FTW_MAX_FDS = 16;
37
38 typedef std::vector<std::string> privileges_t;
39
40 int nftw_remove_labels(const char *fpath, const struct stat* /*sb*/,
41                        int /*typeflag*/, struct FTW* /*ftwbuf*/);
42 void check_app_permissions(const std::string &app_id, const std::string &pkg_id,
43                            const std::string &user, const privileges_t &allowed_privs,
44                            const privileges_t &denied_privs, bool isHybrid = false);
45 void sm_app_has_privileges(const AppInstallHelper &app,
46                            const std::vector<std::string> &privileges,
47                            int result);
48 void check_app_after_install(const std::string &app_id, const std::string &pkg_id,
49                              bool isHybrid = false);
50 void check_app_after_install(const std::string &app_id, const std::string &pkg_id,
51                              const privileges_t &allowed_privs,
52                              const privileges_t &denied_privs,
53                              bool isHybrid = false);
54 void check_path(const std::string &path, const std::string &label,
55                 bool transmute = true, bool execute = false);
56 void check_app_after_uninstall(const std::string &app_id, const std::string &pkg_id,
57                                bool isHybrid = false, bool removePkg = false);
58 void check_app_after_uninstall(const std::string &app_id, const std::string &pkg_id,
59                                const privileges_t &privileges, bool isHybrid = false,
60                                bool removePkg = false);
61 std::string access_opposite(std::string &access);
62 void check_exact_smack_accesses(const std::string &subject,
63                                 const std::string &object,
64                                 const std::string &access);
65
66 CapsSetsUniquePtr setCaps(const char *cap_string);
67
68 void accessTest(const std::string &id, const std::string &testPath, int accessType);
69 void runAccessTest(const std::string &label, uid_t uid, gid_t gid,
70                    const std::string &testPath, int accessType);
71 void runAccessTest(const AppInstallHelper &app, const std::string &testPath, int accessType);
72 void runSystemAccessTest(uid_t uid, gid_t gid, const std::string &testPath, int accessType);
73
74 bool isAskuserDisabled();
75 bool isPrivilegePrivacy(const std::string &priv);
76 int countPrivacyPrivileges(const PrivilegeVector &privs);
77 int countPrivacyPrivileges(const std::vector<std::string> &privs);
78
79 int setLauncherSecurityAttributes(uid_t uid, gid_t gid);
80 int setLauncherSecurityAttributes(TemporaryTestUser &user);