e9a689226de92e20874eb082c2a6f80ace7eb499
[platform/core/test/security-tests.git] / src / security-manager-tests / common / sm_commons.h
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
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
26 #include <security-manager-types.h>
27
28 #include <app_install_helper.h>
29 #include <memory.h>
30 #include <sm_db.h>
31 #include <temp_test_user.h>
32
33 DEFINE_SMARTPTR(cap_free, _cap_struct, CapsSetsUniquePtr);
34
35 struct app_attributes {
36     std::string package;
37     std::string Tizen_ver;
38 };
39
40 const int FTW_MAX_FDS = 16;
41
42 extern const privileges_t SM_ALLOWED_PRIVILEGES;
43 extern const privileges_t SM_DENIED_PRIVILEGES;
44 extern const privileges_t SM_NO_PRIVILEGES;
45 extern const std::vector<std::string> SM_ALLOWED_GROUPS;
46 static const char *const SM_RW_PATH = "/opt/usr/globalapps/sm_test_02_pkg_id_full/app_dir";
47
48 const std::string uidToStr(const uid_t uid);
49
50 std::string generateAppLabel(const std::string &appId);
51 std::string generatePkgLabel(const std::string &pkgId);
52 std::string genRWPath(int app_num);
53 std::string genROPath(int app_num);
54 std::string genPublicROPath(int app_num);
55 std::string genOwnerRWOthersROPath(int app_num);
56
57 int nftw_remove_labels(const char *fpath, const struct stat* /*sb*/,
58                        int /*typeflag*/, struct FTW* /*ftwbuf*/);
59 void check_app_permissions(const char *const app_id, const char *const pkg_id,
60                            const char *const user, const privileges_t &allowed_privs,
61                            const privileges_t &denied_privs);
62 void check_app_after_install(const char *const app_id, const char *const pkg_id);
63 void check_app_after_install(const char *const app_id, const char *const pkg_id,
64                              const privileges_t &allowed_privs,
65                              const privileges_t &denied_privs,
66                              const std::vector<std::string> &allowed_groups);
67 void check_path(const std::string &path, const std::string &label);
68 void check_app_path_after_install(int app_num, const char *pkgId, bool others_enabled=false);
69 void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
70                                const bool is_pkg_removed);
71 void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
72                                const privileges_t &privileges, const bool is_pkg_removed);
73
74 std::string access_opposite(std::string &access);
75 void check_exact_smack_accesses(const std::string &subject,
76                                 const std::string &object,
77                                 const std::string &access);
78
79 CapsSetsUniquePtr setCaps(const char *cap_string);
80 void prepare_app_env(int app_num, bool others_enabled = false);
81 void install_app(const char *app_id, const char *pkg_id, uid_t uid = 0,
82                  app_install_type type = SM_APP_INSTALL_NONE, bool check_after = true);
83 void uninstall_app(const char *app_id, const char *pkg_id,
84                    bool expect_pkg_removed = false, app_install_type type = SM_APP_INSTALL_NONE,
85                    bool check_after = true);
86
87 void createTestDirs(const TemporaryTestUser &user,
88                     const std::string &appId, const std::string &pkgId);
89 void removeTestDirs(const TemporaryTestUser &user,
90                     const std::string &appId, const std::string &pkgId);