[SmokeTest] Add SIGINT and SIGSEGV handling
[platform/core/appfw/wgt-backend.git] / src / unit_tests / smoke_utils.h
1 // Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #ifndef UNIT_TESTS_SMOKE_UTILS_H_
6 #define UNIT_TESTS_SMOKE_UTILS_H_
7
8
9 #include <boost/filesystem/path.hpp>
10
11 #include <common/paths.h>
12 #include <common/pkgmgr_interface.h>
13 #include <common/pkgmgr_query.h>
14 #include <common/tzip_interface.h>
15 #include <gum/gum-user.h>
16 #include <gum/gum-user-service.h>
17 #include <gum/common/gum-user-types.h>
18
19 #include <gtest/gtest.h>
20
21 #include <memory>
22 #include <vector>
23 #include <string>
24
25 #include "hybrid/hybrid_installer.h"
26 #include "wgt/wgt_app_query_interface.h"
27
28 #define SIZEOFARRAY(ARR)                                                       \
29   sizeof(ARR) / sizeof(ARR[0])                                                 \
30
31 #define EXTENDED_ASSERT_TRUE(expression) do {                                  \
32     bool tmp = expression;                                                     \
33     EXPECT_TRUE(tmp) << #expression << " is not true";                         \
34     if (!tmp)                                                                  \
35         return false;                                                          \
36 } while (0);
37
38 #define EXTENDED_ASSERT_FALSE(expression) do {                                 \
39     bool tmp = expression;                                                     \
40     EXPECT_FALSE(tmp) << #expression << " is not false";                       \
41     if (tmp)                                                                   \
42         return false;                                                          \
43 } while (0);
44
45 #define EXTENDED_ASSERT_EQ(expression, value) do {                             \
46     auto ret = expression;                                                     \
47     EXPECT_EQ(ret, value) << #expression << " is not equal to " << #value;     \
48     if (ret != value)                                                          \
49         return false;                                                          \
50 } while (0);
51
52 namespace bf = boost::filesystem;
53 namespace bs = boost::system;
54 namespace ci = common_installer;
55
56 extern const uid_t kGlobalUserUid;
57 extern const uid_t kGlobalUserGid;
58 extern const uid_t kDefaultUserUid;
59 extern uid_t kTestUserId;
60 extern gid_t kTestGroupId;
61 extern std::string kTestUserIdStr;
62 extern const char kNormalUserName[];
63 extern const char kSystemShareGroupName[];
64 extern const std::string& kDefaultUserIdStr;
65 extern const char kLegacyExtImageDir[];
66 extern const char kMigrateTestDBName[];
67
68 extern const bf::path kSmokePackagesDirectory;
69
70 enum RWDirectory {
71     DATA,
72     CACHE,
73     SHARED_CACHE,
74     SHARED_DATA,
75     SHARED_TRUSTED
76 };
77
78 extern const char* rwDirectories[];
79
80 // common entries
81 extern const std::vector<std::string> kDBEntries;
82 // globaluser entries
83 extern const char kGlobalManifestDir[];
84 extern const char kSkelDir[];
85 extern const char kPreloadApps[];
86 extern const char kPreloadManifestDir[];
87 extern const char kPreloadIcons[];
88
89 extern testing::Environment *env;
90 void signalHandler(int signum);
91
92 enum class RequestResult {
93   NORMAL,
94   FAIL
95 };
96
97 class ScopedTzipInterface {
98  public:
99   explicit ScopedTzipInterface(const std::string& pkgid)
100       : pkg_path_(bf::path(ci::GetRootAppPath(false,
101             kTestUserId)) / pkgid),
102         interface_(ci::GetMountLocation(pkg_path_)),
103         mounted_(true) {
104     interface_.MountZip(ci::GetZipPackageLocation(pkg_path_, pkgid));
105   }
106
107   void Release() {
108     if (mounted_) {
109       interface_.UnmountZip();
110       mounted_ = false;
111     }
112   }
113
114   ~ScopedTzipInterface() {
115     Release();
116   }
117
118  private:
119   bf::path pkg_path_;
120   ci::TzipInterface interface_;
121   bool mounted_;
122 };
123
124 class TestPkgmgrInstaller : public ci::PkgmgrInstallerInterface {
125  public:
126   bool CreatePkgMgrInstaller(pkgmgr_installer** installer,
127                              ci::InstallationMode* mode) {
128     *installer = pkgmgr_installer_offline_new();
129     if (!*installer)
130       return false;
131     *mode = ci::InstallationMode::ONLINE;
132     return true;
133   }
134
135   bool ShouldCreateSignal() const {
136     return false;
137   }
138 };
139
140 enum class PackageType {
141   WGT,
142   HYBRID
143 };
144
145 ci::RequestMode ParseRequestMode(int argc,  char** argv);
146
147 bool TouchFile(const bf::path& path);
148
149 bool AddUser(const char *user_name);
150
151 bool DeleteUser(const char *user_name, bool rem_home_dir);
152
153 bool AddTestUser(const char *user_name);
154
155 bool DeleteTestUser(const char *user_name);
156
157 void RemoveAllRecoveryFiles();
158
159 bf::path FindRecoveryFile();
160
161 bf::path GetPackageRoot(const std::string& pkgid, uid_t uid);
162
163 bool ValidateFileContentInPackage(const std::string& pkgid,
164                                   const std::string& relative,
165                                   const std::string& expected,
166                                   bool is_readonly = false);
167
168 void AddDataFiles(const std::string& pkgid, uid_t uid);
169
170 bool ValidateDataFiles(const std::string& pkgid, uid_t uid);
171
172 bool ValidatePackageRWFS(const std::string& pkgid, uid_t uid);
173
174 bool ValidatePackageFS(const std::string& pkgid,
175                        const std::vector<std::string>& appids,
176                        uid_t uid, gid_t gid, bool is_readonly);
177
178 void PackageCheckCleanup(const std::string& pkgid,
179     const std::vector<std::string>&, bool is_readonly = false);
180
181 bool ValidatePackage(const std::string& pkgid,
182     const std::vector<std::string>& appids, bool is_readonly = false);
183
184 void ValidateExternalPackageFS(const std::string& pkgid,
185                                const std::vector<std::string>& appids,
186                                uid_t uid, gid_t gid);
187
188 void ValidateExternalPackage(const std::string& pkgid,
189                              const std::vector<std::string>& appids);
190
191 bool CheckPackageNonExistance(const std::string& pkgid,
192                               const std::vector<std::string>& appids);
193
194 void CheckPackageReadonlyNonExistance(const std::string& pkgid,
195                                       const std::vector<std::string>& appids);
196
197 std::unique_ptr<ci::AppQueryInterface> CreateQueryInterface();
198
199 std::unique_ptr<ci::AppInstaller> CreateInstaller(ci::PkgMgrPtr pkgmgr,
200                                                   PackageType type);
201
202 void TestRollbackAfterEachStep(int argc, const char*argv[],
203                                std::function<bool()> validator,
204                                PackageType type = PackageType::WGT);
205
206 void CrashAfterEachStep(std::vector<std::string> args,
207                         std::function<bool(int iter)> validator,
208                         PackageType type = PackageType::WGT);
209
210 ci::AppInstaller::Result RunInstallerWithPkgrmgr(ci::PkgMgrPtr pkgmgr,
211                                                  PackageType type,
212                                                  RequestResult mode);
213 ci::AppInstaller::Result CallBackend(int argc,
214                                      const char* argv[],
215                                      PackageType type,
216                                      RequestResult mode
217                                      = RequestResult::NORMAL);
218
219 ci::AppInstaller::Result Install(const bf::path& path,
220                                  PackageType type,
221                                  RequestResult mode = RequestResult::NORMAL);
222
223 ci::AppInstaller::Result InstallPreload(const bf::path& path, PackageType type,
224     RequestResult mode = RequestResult::NORMAL);
225
226 bool CheckAvailableExternalPath();
227
228 ci::AppInstaller::Result InstallExternal(const bf::path& path,
229                                  PackageType type,
230                                  RequestResult mode = RequestResult::NORMAL);
231
232 ci::AppInstaller::Result MigrateLegacyExternalImage(const std::string& pkgid,
233                                  const bf::path& path,
234                                  const bf::path& legacy_path,
235                                  PackageType type,
236                                  RequestResult mode = RequestResult::NORMAL);
237
238 ci::AppInstaller::Result MountInstall(const bf::path& path,
239     PackageType type, RequestResult mode = RequestResult::NORMAL);
240
241 ci::AppInstaller::Result Uninstall(const std::string& pkgid,
242                                    PackageType type,
243                                    bool is_preload,
244                                    RequestResult mode = RequestResult::NORMAL);
245
246 ci::AppInstaller::Result RDSUpdate(const bf::path& path,
247                                    const std::string& pkgid,
248                                    PackageType type,
249                                    RequestResult mode = RequestResult::NORMAL);
250
251 ci::AppInstaller::Result DeltaInstall(const bf::path& path,
252     const bf::path& delta_package, PackageType type);
253
254 ci::AppInstaller::Result EnablePackage(const std::string& pkgid,
255                                   PackageType type,
256                                   RequestResult mode = RequestResult::NORMAL);
257
258 ci::AppInstaller::Result DisablePackage(const std::string& pkgid,
259                                   PackageType type,
260                                   RequestResult mode = RequestResult::NORMAL);
261
262 ci::AppInstaller::Result Recover(const bf::path& recovery_file,
263                                  PackageType type,
264                                  RequestResult mode = RequestResult::NORMAL);
265
266 void BackupPath(const bf::path& path);
267
268 void RestorePath(const bf::path& path);
269
270 std::vector<bf::path> SetupBackupDirectories();
271
272 void UninstallAllAppsInDirectory(bf::path dir, bool is_preload);
273
274 void UninstallAllSmokeApps(ci::RequestMode request_mode);
275
276
277 #endif  // UNIT_TESTS_SMOKE_UTILS_H_