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