Smoke tests for preload installation, update and deinstallation
[platform/core/appfw/wgt-backend.git] / src / unit_tests / smoke_test.cc
1 // Copyright (c) 2015 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 #include <boost/filesystem/operations.hpp>
6 #include <boost/filesystem/path.hpp>
7 #include <boost/range/iterator_range.hpp>
8 #include <boost/system/error_code.hpp>
9
10 #include <common/paths.h>
11 #include <common/pkgmgr_interface.h>
12 #include <common/pkgmgr_query.h>
13 #include <common/request.h>
14 #include <common/step/configuration/step_fail.h>
15 #include <common/tzip_interface.h>
16 #include <common/utils/file_util.h>
17 #include <common/utils/subprocess.h>
18 #include <common/utils/user_util.h>
19
20 #include <gtest/gtest.h>
21 #include <gtest/gtest-death-test.h>
22 #include <pkgmgr-info.h>
23 #include <signal.h>
24 #include <unistd.h>
25 #include <tzplatform_config.h>
26 #include <vconf.h>
27 #include <vconf-internal-keys.h>
28
29 #include <array>
30 #include <cstdio>
31 #include <cstdlib>
32 #include <vector>
33
34 #include "hybrid/hybrid_installer.h"
35 #include "wgt/wgt_app_query_interface.h"
36 #include "wgt/wgt_installer.h"
37
38 #define SIZEOFARRAY(ARR)                                                       \
39   sizeof(ARR) / sizeof(ARR[0])                                                 \
40
41 namespace bf = boost::filesystem;
42 namespace bs = boost::system;
43 namespace ci = common_installer;
44
45 namespace {
46
47 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
48 const uid_t kGlobalUserGid = tzplatform_getgid(TZ_SYS_GLOBALAPP_USER);
49 const uid_t kDefaultUserUid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
50 const uid_t kTestUserId = kGlobalUserUid;
51 const gid_t kTestGroupId = kGlobalUserGid;
52 const char kSystemShareGroupName[] = "system_share";
53 const std::string& kTestUserIdStr =
54     std::to_string(kTestUserId);
55 const std::string& kDefaultUserIdStr = std::to_string(kDefaultUserUid);
56 const char kLegacyExtImageDir[] = "legacy_extimage_dir";
57 const char kMigrateTestDBName[] = "app2sd_migrate.db";
58
59 const bf::path kSmokePackagesDirectory =
60     "/usr/share/wgt-backend-ut/test_samples/smoke/";
61
62 // common entries
63 const std::vector<std::string> kDBEntries = {
64   {".pkgmgr_parser.db"},
65   {".pkgmgr_parser.db-journal"},
66   {".pkgmgr_cert.db"},
67   {".pkgmgr_cert.db-journal"},
68   {".app2sd.db"},
69   {".app2sd.db-journal"},
70 };
71 // globaluser entries
72 const char kGlobalManifestDir[] = "/opt/share/packages";
73 const char kSkelDir[] = "/etc/skel/apps_rw";
74 const char kPreloadApps[] = "/usr/apps";
75 const char kPreloadManifestDir[] = "/usr/share/packages";
76 const char kPreloadIcons[] = "/usr/share/icons";
77
78 enum class RequestResult {
79   NORMAL,
80   FAIL
81 };
82
83 class ScopedTzipInterface {
84  public:
85   explicit ScopedTzipInterface(const std::string& pkgid)
86       : pkg_path_(bf::path(ci::GetRootAppPath(false,
87             kTestUserId)) / pkgid),
88         interface_(ci::GetMountLocation(pkg_path_)),
89         mounted_(true) {
90     interface_.MountZip(ci::GetZipPackageLocation(pkg_path_, pkgid));
91   }
92
93   void Release() {
94     if (mounted_) {
95       interface_.UnmountZip();
96       mounted_ = false;
97     }
98   }
99
100   ~ScopedTzipInterface() {
101     Release();
102   }
103
104  private:
105   bf::path pkg_path_;
106   ci::TzipInterface interface_;
107   bool mounted_;
108 };
109
110 class TestPkgmgrInstaller : public ci::PkgmgrInstallerInterface {
111  public:
112   bool CreatePkgMgrInstaller(pkgmgr_installer** installer,
113                              ci::InstallationMode* mode) {
114     *installer = pkgmgr_installer_offline_new();
115     if (!*installer)
116       return false;
117     *mode = ci::InstallationMode::ONLINE;
118     return true;
119   }
120
121   bool ShouldCreateSignal() const {
122     return false;
123   }
124 };
125
126 enum class PackageType {
127   WGT,
128   HYBRID
129 };
130
131 bool TouchFile(const bf::path& path) {
132   FILE* f = fopen(path.c_str(), "w+");
133   if (!f)
134     return false;
135   fclose(f);
136   return true;
137 }
138
139 void RemoveAllRecoveryFiles() {
140   bf::path root_path = ci::GetRootAppPath(false,
141       kTestUserId);
142   if (!bf::exists(root_path))
143     return;
144   for (auto& dir_entry : boost::make_iterator_range(
145          bf::directory_iterator(root_path), bf::directory_iterator())) {
146     if (bf::is_regular_file(dir_entry)) {
147       if (dir_entry.path().string().find("/recovery") != std::string::npos) {
148         bs::error_code error;
149         bf::remove(dir_entry.path(), error);
150       }
151     }
152   }
153 }
154
155 bf::path FindRecoveryFile() {
156   bf::path root_path = ci::GetRootAppPath(false,
157       kTestUserId);
158   for (auto& dir_entry : boost::make_iterator_range(
159          bf::directory_iterator(root_path), bf::directory_iterator())) {
160     if (bf::is_regular_file(dir_entry)) {
161       if (dir_entry.path().string().find("/recovery") != std::string::npos) {
162         return dir_entry.path();
163       }
164     }
165   }
166   return {};
167 }
168
169 bf::path GetPackageRoot(const std::string& pkgid, uid_t uid) {
170   bf::path root_path = ci::GetRootAppPath(false, uid);
171   return root_path / pkgid;
172 }
173
174 bool ValidateFileContentInPackage(const std::string& pkgid,
175                                   const std::string& relative,
176                                   const std::string& expected,
177                                   bool is_readonly = false) {
178   bf::path file_path = ci::GetRootAppPath(is_readonly, kTestUserId);
179   file_path = file_path / pkgid / relative;
180   if (!bf::exists(file_path)) {
181     LOG(ERROR) << file_path << " doesn't exist";
182     return false;
183   }
184   FILE* handle = fopen(file_path.c_str(), "r");
185   if (!handle) {
186     LOG(ERROR) << file_path << " cannot  be open";
187     return false;
188   }
189   std::string content;
190   std::array<char, 200> buffer;
191   while (fgets(buffer.data(), buffer.size(), handle)) {
192     content += buffer.data();
193   }
194   fclose(handle);
195   return content == expected;
196 }
197
198 void AddDataFiles(const std::string& pkgid, uid_t uid) {
199   if (uid == kGlobalUserUid) {
200     ci::UserList list = ci::GetUserList();
201     for (auto l : list) {
202       auto pkg_path = GetPackageRoot(pkgid, std::get<0>(l));
203       ASSERT_TRUE(TouchFile(pkg_path / "data" / "file1.txt"));
204       ASSERT_TRUE(TouchFile(pkg_path / "data" / "file2.txt"));
205     }
206   } else {
207     auto pkg_path = GetPackageRoot(pkgid, uid);
208     ASSERT_TRUE(TouchFile(pkg_path / "data" / "file1.txt"));
209     ASSERT_TRUE(TouchFile(pkg_path / "data" / "file2.txt"));
210   }
211 }
212
213 void ValidateDataFiles(const std::string& pkgid, uid_t uid) {
214   if (uid == kGlobalUserUid) {
215     ci::UserList list = ci::GetUserList();
216     for (auto l : list) {
217       auto pkg_path = GetPackageRoot(pkgid, std::get<0>(l));
218       ASSERT_TRUE(bf::exists(pkg_path / "data" / "file1.txt"));
219       ASSERT_TRUE(bf::exists(pkg_path / "data" / "file2.txt"));
220     }
221   } else {
222     auto pkg_path = GetPackageRoot(pkgid, uid);
223     ASSERT_TRUE(bf::exists(pkg_path / "data" / "file1.txt"));
224     ASSERT_TRUE(bf::exists(pkg_path / "data" / "file2.txt"));
225   }
226 }
227
228 void ValidatePackageRWFS(const std::string& pkgid, uid_t uid) {
229   bf::path root_path = ci::GetRootAppPath(false, uid);
230   bf::path package_path = root_path / pkgid;
231   bf::path data_path = package_path / "data";
232   bf::path cache_path = package_path / "cache";
233   bf::path shared_data_path = package_path / "shared" / "data";
234
235   ASSERT_TRUE(bf::exists(data_path));
236   ASSERT_TRUE(bf::exists(cache_path));
237
238   struct stat stats;
239   stat(data_path.c_str(), &stats);
240   // gid of RW dirs should be system_share
241   boost::optional<gid_t> system_share =
242     ci::GetGidByGroupName(kSystemShareGroupName);
243   ASSERT_EQ(uid, stats.st_uid) << "Invalid gid: " << data_path;
244   ASSERT_EQ(*system_share, stats.st_gid) << "Invalid gid: " << data_path;
245   if (bf::exists(shared_data_path)) {
246     stat(shared_data_path.c_str(), &stats);
247     ASSERT_EQ(uid, stats.st_uid) << "Invalid gid: " << shared_data_path;
248     ASSERT_EQ(*system_share, stats.st_gid) << "Invalid gid: "
249       << shared_data_path;
250   }
251
252   stat(cache_path.c_str(), &stats);
253   ASSERT_EQ(uid, stats.st_uid) << "Invalid gid: " << cache_path;
254   ASSERT_EQ(*system_share, stats.st_gid) << "Invalid gid: " << cache_path;
255 }
256
257 void ValidatePackageFS(const std::string& pkgid,
258                        const std::vector<std::string>& appids,
259                        uid_t uid, gid_t gid, bool is_readonly) {
260   bf::path root_path = ci::GetRootAppPath(is_readonly, uid);
261   bf::path package_path = root_path / pkgid;
262   bf::path shared_path = package_path / "shared";
263   ASSERT_TRUE(bf::exists(root_path));
264   ASSERT_TRUE(bf::exists(package_path));
265   ASSERT_TRUE(bf::exists(shared_path));
266
267   bf::path manifest_path =
268       bf::path(getUserManifestPath(uid, is_readonly)) / (pkgid + ".xml");
269   ASSERT_TRUE(bf::exists(manifest_path));
270
271   for (auto& appid : appids) {
272     bf::path binary_path = package_path / "bin" / appid;
273     ASSERT_TRUE(bf::exists(binary_path));
274   }
275
276   bf::path widget_root_path = package_path / "res" / "wgt";
277   bf::path config_path = widget_root_path / "config.xml";
278   ASSERT_TRUE(bf::exists(widget_root_path));
279   ASSERT_TRUE(bf::exists(config_path));
280
281   bf::path private_tmp_path = package_path / "tmp";
282   ASSERT_TRUE(bf::exists(private_tmp_path));
283
284   // backups should not exist
285   bf::path package_backup = ci::GetBackupPathForPackagePath(package_path);
286   bf::path manifest_backup = ci::GetBackupPathForManifestFile(manifest_path);
287   ASSERT_FALSE(bf::exists(package_backup));
288   ASSERT_FALSE(bf::exists(manifest_backup));
289
290   for (bf::recursive_directory_iterator iter(package_path);
291       iter != bf::recursive_directory_iterator(); ++iter) {
292     if (bf::is_symlink(symlink_status(iter->path())))
293       continue;
294     if (iter->path().filename() == "data" ||
295         iter->path().filename() == ".mmc")
296       continue;
297     struct stat stats;
298     stat(iter->path().c_str(), &stats);
299     ASSERT_EQ(uid, stats.st_uid) << "Invalid uid: " << iter->path();
300     ASSERT_EQ(gid, stats.st_gid) << "Invalid gid: " << iter->path();
301   }
302 }
303
304 void PackageCheckCleanup(const std::string& pkgid,
305     const std::vector<std::string>&, bool is_readonly = false) {
306   bf::path root_path = ci::GetRootAppPath(is_readonly, kTestUserId);
307   bf::path package_path = root_path / pkgid;
308   ASSERT_FALSE(bf::exists(package_path));
309
310   bf::path manifest_path = bf::path(getUserManifestPath(kTestUserId,
311       is_readonly)) / (pkgid + ".xml");
312   ASSERT_FALSE(bf::exists(manifest_path));
313
314   // backups should not exist
315   bf::path package_backup = ci::GetBackupPathForPackagePath(package_path);
316   bf::path manifest_backup = ci::GetBackupPathForManifestFile(manifest_path);
317   ASSERT_FALSE(bf::exists(package_backup));
318   ASSERT_FALSE(bf::exists(manifest_backup));
319 }
320
321 void ValidatePackage(const std::string& pkgid,
322     const std::vector<std::string>& appids, bool is_readonly = false) {
323   ASSERT_TRUE(ci::QueryIsPackageInstalled(
324       pkgid, ci::GetRequestMode(kTestUserId), kTestUserId));
325   ValidatePackageFS(pkgid, appids, kTestUserId, kTestGroupId, is_readonly);
326   if (kTestUserId == kGlobalUserUid) {
327     ci::UserList list = ci::GetUserList();
328     for (auto& l : list)
329       ValidatePackageRWFS(pkgid, std::get<0>(l));
330   } else {
331     ValidatePackageRWFS(pkgid, kTestUserId);
332   }
333 }
334
335 void ValidateExternalPackageFS(const std::string& pkgid,
336                                const std::vector<std::string>& appids,
337                                uid_t uid, gid_t gid) {
338   ASSERT_EQ(app2ext_usr_enable_external_pkg(pkgid.c_str(), uid), 0);
339   bf::path root_path = ci::GetRootAppPath(false, uid);
340   ASSERT_TRUE(bf::exists(root_path / pkgid / ".mmc" / "res"));
341   ValidatePackageFS(pkgid, appids, uid, gid, false);
342   ASSERT_EQ(app2ext_usr_disable_external_pkg(pkgid.c_str(), uid), 0);
343 }
344
345 void ValidateExternalPackage(const std::string& pkgid,
346                              const std::vector<std::string>& appids) {
347   ASSERT_TRUE(ci::QueryIsPackageInstalled(
348       pkgid, ci::GetRequestMode(kTestUserId),
349       kTestUserId));
350   std::string storage = ci::QueryStorageForPkgId(pkgid, kTestUserId);
351   bf::path ext_mount_path = ci::GetExternalCardPath();
352   if (bf::is_empty(ext_mount_path)) {
353     LOG(INFO) << "Sdcard not exists!";
354     ASSERT_EQ(storage, "installed_internal");
355   } else {
356     ASSERT_EQ(storage, "installed_external");
357   }
358   ValidateExternalPackageFS(pkgid, appids, kTestUserId, kTestGroupId);
359   if (kTestUserId == kGlobalUserUid) {
360     ci::UserList list = ci::GetUserList();
361     for (auto& l : list)
362       ValidatePackageRWFS(pkgid, std::get<0>(l));
363   } else {
364     ValidatePackageRWFS(pkgid, kTestUserId);
365   }
366 }
367
368 void CheckPackageNonExistance(const std::string& pkgid,
369                               const std::vector<std::string>& appids) {
370   ASSERT_FALSE(ci::QueryIsPackageInstalled(
371       pkgid, ci::GetRequestMode(kTestUserId),
372       kTestUserId));
373   PackageCheckCleanup(pkgid, appids);
374 }
375
376 void CheckPackageReadonlyNonExistance(const std::string& pkgid,
377                                       const std::vector<std::string>& appids) {
378   ASSERT_FALSE(ci::QueryIsPackageInstalled(
379       pkgid, ci::GetRequestMode(kTestUserId), kTestUserId));
380   PackageCheckCleanup(pkgid, appids, true);
381 }
382
383 std::unique_ptr<ci::AppQueryInterface> CreateQueryInterface() {
384   std::unique_ptr<ci::AppQueryInterface> query_interface(
385       new wgt::WgtAppQueryInterface());
386   return query_interface;
387 }
388
389 std::unique_ptr<ci::AppInstaller> CreateInstaller(ci::PkgMgrPtr pkgmgr,
390                                                   PackageType type) {
391   switch (type) {
392     case PackageType::WGT:
393       return std::unique_ptr<ci::AppInstaller>(new wgt::WgtInstaller(pkgmgr));
394     case PackageType::HYBRID:
395       return std::unique_ptr<ci::AppInstaller>(
396           new hybrid::HybridInstaller(pkgmgr));
397     default:
398       LOG(ERROR) << "Unknown installer type";
399       return nullptr;
400   }
401 }
402
403 ci::AppInstaller::Result RunInstallerWithPkgrmgr(ci::PkgMgrPtr pkgmgr,
404                                                  PackageType type,
405                                                  RequestResult mode) {
406   std::unique_ptr<ci::AppInstaller> installer = CreateInstaller(pkgmgr, type);
407   switch (mode) {
408   case RequestResult::FAIL:
409     installer->AddStep<ci::configuration::StepFail>();
410     break;
411   default:
412     break;
413   }
414   return installer->Run();
415 }
416 ci::AppInstaller::Result CallBackend(int argc,
417                                      const char* argv[],
418                                      PackageType type,
419                                      RequestResult mode = RequestResult::NORMAL
420                                      ) {
421   TestPkgmgrInstaller pkgmgr_installer;
422   std::unique_ptr<ci::AppQueryInterface> query_interface =
423       CreateQueryInterface();
424   auto pkgmgr =
425       ci::PkgMgrInterface::Create(argc, const_cast<char**>(argv),
426                                   &pkgmgr_installer, query_interface.get());
427   if (!pkgmgr) {
428     LOG(ERROR) << "Failed to initialize pkgmgr interface";
429     return ci::AppInstaller::Result::UNKNOWN;
430   }
431   return RunInstallerWithPkgrmgr(pkgmgr, type, mode);
432 }
433
434 ci::AppInstaller::Result Install(const bf::path& path,
435                                  PackageType type,
436                                  RequestResult mode = RequestResult::NORMAL) {
437   const char* argv[] = {"", "-i", path.c_str(), "-u", kTestUserIdStr.c_str()};
438   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
439 }
440
441 ci::AppInstaller::Result InstallPreload(const bf::path& path, PackageType type,
442     RequestResult mode = RequestResult::NORMAL) {
443   const char* argv[] = {"", "-i", path.c_str(), "--preload"};
444   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
445 }
446
447 bool CheckAvailableExternalPath() {
448   bf::path ext_mount_path = ci::GetExternalCardPath();
449   LOG(DEBUG) << "ext_mount_path :" << ext_mount_path;
450   if (ext_mount_path.empty()) {
451     LOG(ERROR) << "Sdcard not exists!";
452     return false;
453   }
454   return true;
455 }
456
457 ci::AppInstaller::Result InstallExternal(const bf::path& path,
458                                  PackageType type,
459                                  RequestResult mode = RequestResult::NORMAL) {
460   int default_storage = 0;
461   vconf_get_int(VCONFKEY_SETAPPL_DEFAULT_MEM_INSTALL_APPLICATIONS_INT,
462                 &default_storage);
463   vconf_set_int(VCONFKEY_SETAPPL_DEFAULT_MEM_INSTALL_APPLICATIONS_INT, 1);
464
465   const char* argv[] = {"", "-i", path.c_str(), "-u", kTestUserIdStr.c_str()};
466   ci::AppInstaller::Result result =
467                           CallBackend(SIZEOFARRAY(argv), argv, type, mode);
468
469   vconf_set_int(VCONFKEY_SETAPPL_DEFAULT_MEM_INSTALL_APPLICATIONS_INT,
470                 default_storage);
471   return result;
472 }
473
474 ci::AppInstaller::Result MigrateLegacyExternalImage(const std::string& pkgid,
475                                  const bf::path& path,
476                                  const bf::path& legacy_path,
477                                  PackageType type,
478                                  RequestResult mode = RequestResult::NORMAL) {
479   if (InstallExternal(path, type) != ci::AppInstaller::Result::OK) {
480     LOG(ERROR) << "Failed to install application. Cannot perform Migrate";
481     return ci::AppInstaller::Result::ERROR;
482   }
483
484   bf::path ext_mount_path = ci::GetExternalCardPath();
485   if (bf::is_empty(ext_mount_path)) {
486     LOG(ERROR) << "Sdcard not exists!";
487     return ci::AppInstaller::Result::ERROR;
488   }
489   bf::path app2sd_path = ext_mount_path / "app2sd";
490
491   char* image_name = app2ext_usr_getname_image(pkgid.c_str(),
492                      kGlobalUserUid);
493   if (!image_name) {
494     LOG(ERROR) << "Failed to get external image name";
495     return ci::AppInstaller::Result::ERROR;
496   }
497   bf::path org_image = app2sd_path / image_name;
498   free(image_name);
499
500   bs::error_code error;
501   bf::remove(org_image, error);
502   if (error) {
503     LOG(ERROR) << "Failed to remove org image";
504     return ci::AppInstaller::Result::ERROR;
505   }
506
507   bf::path db_path = tzplatform_getenv(TZ_SYS_DB);
508   bf::path app2sd_db = db_path / ".app2sd.db";
509   bf::path app2sd_db_journal = db_path / ".app2sd.db-journal";
510   bf::remove(app2sd_db, error);
511   if (error) {
512     LOG(ERROR) << "Failed to remove app2sd db";
513     return ci::AppInstaller::Result::ERROR;
514   }
515   bf::remove(app2sd_db_journal, error);
516   if (error) {
517     LOG(ERROR) << "Failed to remove app2sd journal db";
518     return ci::AppInstaller::Result::ERROR;
519   }
520
521   bf::path app2sd_migrate_db = legacy_path / kMigrateTestDBName;
522   if (!ci::CopyFile(app2sd_migrate_db, app2sd_db)) {
523     LOG(ERROR) << "Failed to copy test db";
524     return ci::AppInstaller::Result::ERROR;
525   }
526
527   bf::path legacy_src = legacy_path / pkgid;
528   bf::path legacy_dst = app2sd_path / pkgid;
529   if (!ci::CopyFile(legacy_src, legacy_dst)) {
530     LOG(ERROR) << "Failed to copy test image";
531     return ci::AppInstaller::Result::ERROR;
532   }
533   const char* argv[] = {"", "--migrate-extimg", pkgid.c_str(),
534                        "-u", kDefaultUserIdStr.c_str()};
535   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
536 }
537
538 ci::AppInstaller::Result MountInstall(const bf::path& path,
539     PackageType type, RequestResult mode = RequestResult::NORMAL) {
540   const char* argv[] = {"", "-w", path.c_str(), "-u", kTestUserIdStr.c_str()};
541   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
542 }
543
544 ci::AppInstaller::Result Uninstall(const std::string& pkgid,
545                                    PackageType type,
546                                    RequestResult mode = RequestResult::NORMAL) {
547   const char* argv[] = {"", "-d", pkgid.c_str(), "-u", kTestUserIdStr.c_str()};
548   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
549 }
550
551 ci::AppInstaller::Result UninstallPreload(const std::string& pkgid,
552     PackageType type, RequestResult mode = RequestResult::NORMAL) {
553   const char* argv[] = {"", "-d", pkgid.c_str(), "--preload", "--force-remove"};
554   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
555 }
556
557 ci::AppInstaller::Result RDSUpdate(const bf::path& path,
558                                    const std::string& pkgid,
559                                    PackageType type,
560                                    RequestResult mode = RequestResult::NORMAL) {
561   if (Install(path, type) != ci::AppInstaller::Result::OK) {
562     LOG(ERROR) << "Failed to install application. Cannot perform RDS";
563     return ci::AppInstaller::Result::UNKNOWN;
564   }
565   const char* argv[] = {"", "-r", pkgid.c_str(), "-u",
566                         kTestUserIdStr.c_str()};
567   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
568 }
569
570 ci::AppInstaller::Result DeltaInstall(const bf::path& path,
571     const bf::path& delta_package, PackageType type) {
572   if (Install(path, type) != ci::AppInstaller::Result::OK) {
573     LOG(ERROR) << "Failed to install application. Cannot perform delta update";
574     return ci::AppInstaller::Result::UNKNOWN;
575   }
576   return Install(delta_package, type);
577 }
578
579 ci::AppInstaller::Result EnablePackage(const std::string& pkgid,
580                                   PackageType type,
581                                   RequestResult mode = RequestResult::NORMAL) {
582   const char* argv[] = {"", "-A", pkgid.c_str(), "-u", kTestUserIdStr.c_str()};
583   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
584 }
585
586 ci::AppInstaller::Result DisablePackage(const std::string& pkgid,
587                                   PackageType type,
588                                   RequestResult mode = RequestResult::NORMAL) {
589   const char* argv[] = {"", "-D", pkgid.c_str(), "-u", kTestUserIdStr.c_str()};
590   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
591 }
592
593 ci::AppInstaller::Result Recover(const bf::path& recovery_file,
594                                  PackageType type,
595                                  RequestResult mode = RequestResult::NORMAL) {
596   const char* argv[] = {"", "-b", recovery_file.c_str(), "-u",
597                         kTestUserIdStr.c_str()};
598   TestPkgmgrInstaller pkgmgr_installer;
599   std::unique_ptr<ci::AppQueryInterface> query_interface =
600       CreateQueryInterface();
601   auto pkgmgr =
602       ci::PkgMgrInterface::Create(SIZEOFARRAY(argv), const_cast<char**>(argv),
603                                   &pkgmgr_installer, query_interface.get());
604   if (!pkgmgr) {
605     LOG(ERROR) << "Failed to initialize pkgmgr interface";
606     return ci::AppInstaller::Result::UNKNOWN;
607   }
608   return RunInstallerWithPkgrmgr(pkgmgr, type, mode);
609 }
610
611 void BackupPath(const bf::path& path) {
612   bf::path backup_path = path.string() + ".bck";
613   std::cout << "Backup path: " << path << " to " << backup_path << std::endl;
614   bs::error_code error;
615   bf::remove_all(backup_path, error);
616   if (error)
617     LOG(ERROR) << "Remove failed: " << backup_path
618                << " (" << error.message() << ")";
619   if (bf::exists(path)) {
620     bf::rename(path, backup_path, error);
621     if (error)
622       LOG(ERROR) << "Failed to setup test environment. Does some previous"
623                  << " test crashed? Path: "
624                  << backup_path << " should not exist.";
625     assert(!error);
626   }
627 }
628
629 void RestorePath(const bf::path& path) {
630   bf::path backup_path = path.string() + ".bck";
631   std::cout << "Restore path: " << path << " from " << backup_path << std::endl;
632   bs::error_code error;
633   bf::remove_all(path, error);
634   if (error)
635     LOG(ERROR) << "Remove failed: " << path
636                << " (" << error.message() << ")";
637   if (bf::exists(backup_path)) {
638     bf::rename(backup_path, path, error);
639     if (error)
640       LOG(ERROR) << "Failed to restore backup path: " << backup_path
641                  << " (" << error.message() << ")";
642   }
643 }
644
645 std::vector<bf::path> SetupBackupDirectories(uid_t uid) {
646   std::vector<bf::path> entries;
647   bf::path db_dir = bf::path(tzplatform_getenv(TZ_SYS_DB));
648   if (uid != kGlobalUserUid)
649     db_dir = db_dir / "user" / std::to_string(uid);
650   for (auto e : kDBEntries) {
651     bf::path path = db_dir / e;
652     entries.emplace_back(path);
653   }
654
655   if (getuid() == 0) {
656     entries.emplace_back(kPreloadApps);
657     entries.emplace_back(kPreloadManifestDir);
658     entries.emplace_back(kPreloadIcons);
659   }
660
661   if (uid == kGlobalUserUid) {
662     entries.emplace_back(kSkelDir);
663     entries.emplace_back(kGlobalManifestDir);
664     ci::UserList list = ci::GetUserList();
665     for (auto l : list) {
666       bf::path apps = std::get<2>(l) / "apps_rw";
667       entries.emplace_back(apps);
668     }
669   } else {
670     tzplatform_set_user(uid);
671     bf::path approot = tzplatform_getenv(TZ_USER_APPROOT);
672     tzplatform_reset_user();
673     entries.emplace_back(approot);
674   }
675
676   bf::path apps_rw = ci::GetRootAppPath(false, uid);
677   entries.emplace_back(apps_rw);
678
679   return entries;
680 }
681
682 }  // namespace
683
684 namespace common_installer {
685
686 class SmokeEnvironment : public testing::Environment {
687  public:
688   explicit SmokeEnvironment(uid_t uid) : uid_(uid) {
689   }
690   void SetUp() override {
691     backups_ = SetupBackupDirectories(uid_);
692     for (auto& path : backups_)
693       BackupPath(path);
694   }
695   void TearDown() override {
696     // TODO(s89.jang): Uninstall smoke packages to clear security context
697     for (auto& path : backups_)
698       RestorePath(path);
699   }
700
701  private:
702   uid_t uid_;
703   std::vector<bf::path> backups_;
704 };
705
706 class SmokeTest : public testing::Test {
707 };
708
709 TEST_F(SmokeTest, InstallationMode) {
710   bf::path path = kSmokePackagesDirectory / "InstallationMode.wgt";
711   std::string pkgid = "smokeapp03";
712   std::string appid = "smokeapp03.InstallationMode";
713   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
714   ValidatePackage(pkgid, {appid});
715 }
716
717 TEST_F(SmokeTest, UpdateMode) {
718   bf::path path_old = kSmokePackagesDirectory / "UpdateMode.wgt";
719   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_2.wgt";
720   std::string pkgid = "smokeapp04";
721   std::string appid = "smokeapp04.UpdateMode";
722   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
723   AddDataFiles(pkgid, kTestUserId);
724   ASSERT_EQ(Install(path_new, PackageType::WGT), ci::AppInstaller::Result::OK);
725   ValidatePackage(pkgid, {appid});
726
727   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
728   ValidateDataFiles(pkgid, kTestUserId);
729 }
730
731 TEST_F(SmokeTest, DeinstallationMode) {
732   bf::path path = kSmokePackagesDirectory / "DeinstallationMode.wgt";
733   std::string pkgid = "smokeapp05";
734   std::string appid = "smokeapp05.DeinstallationMode";
735   ASSERT_EQ(Install(path, PackageType::WGT),
736             ci::AppInstaller::Result::OK);
737   ASSERT_EQ(Uninstall(pkgid, PackageType::WGT), ci::AppInstaller::Result::OK);
738   CheckPackageNonExistance(pkgid, {appid});
739 }
740
741 TEST_F(SmokeTest, RDSMode) {
742   bf::path path = kSmokePackagesDirectory / "RDSMode.wgt";
743   std::string pkgid = "smokeapp11";
744   std::string appid = "smokeapp11.RDSMode";
745   bf::path delta_directory = kSmokePackagesDirectory / "delta_dir/";
746   bf::path sdk_expected_directory =
747       bf::path(ci::GetRootAppPath(false, kTestUserId)) / "tmp" / pkgid;
748   bs::error_code error;
749   bf::create_directories(sdk_expected_directory.parent_path(), error);
750   ASSERT_FALSE(error);
751   ASSERT_TRUE(CopyDir(delta_directory, sdk_expected_directory));
752   ASSERT_EQ(RDSUpdate(path, pkgid, PackageType::WGT),
753             ci::AppInstaller::Result::OK);
754   ValidatePackage(pkgid, {appid});
755
756   // Check delta modifications
757   ASSERT_FALSE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
758       "res" / "wgt" / "DELETED"));
759   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
760       "res" / "wgt" / "ADDED"));
761   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED", "2\n"));
762 }
763
764 TEST_F(SmokeTest, EnablePkg) {
765   bf::path path = kSmokePackagesDirectory / "EnablePkg.wgt";
766   std::string pkgid = "smokeapp22";
767   ASSERT_EQ(Install(path, PackageType::WGT),
768             ci::AppInstaller::Result::OK);
769   ASSERT_EQ(DisablePackage(pkgid, PackageType::WGT),
770             ci::AppInstaller::Result::OK);
771   ASSERT_EQ(EnablePackage(pkgid, PackageType::WGT),
772             ci::AppInstaller::Result::OK);
773
774   ASSERT_TRUE(ci::QueryIsPackageInstalled(pkgid,
775       ci::GetRequestMode(kTestUserId),
776       kTestUserId));
777 }
778
779 TEST_F(SmokeTest, DisablePkg) {
780   bf::path path = kSmokePackagesDirectory / "DisablePkg.wgt";
781   std::string pkgid = "smokeapp21";
782   std::string appid = "smokeapp21.DisablePkg";
783   ASSERT_EQ(Install(path, PackageType::WGT),
784             ci::AppInstaller::Result::OK);
785   ASSERT_EQ(DisablePackage(pkgid, PackageType::WGT),
786             ci::AppInstaller::Result::OK);
787   ASSERT_TRUE(ci::QueryIsDisabledPackage(pkgid, kTestUserId));
788   ValidatePackage(pkgid, {appid});
789 }
790
791 TEST_F(SmokeTest, DeltaMode) {
792   bf::path path = kSmokePackagesDirectory / "DeltaMode.wgt";
793   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode.delta";
794   std::string pkgid = "smokeapp17";
795   std::string appid = "smokeapp17.DeltaMode";
796   ASSERT_EQ(DeltaInstall(path, delta_package, PackageType::WGT),
797             ci::AppInstaller::Result::OK);
798   ValidatePackage(pkgid, {appid});
799
800   // Check delta modifications
801   ASSERT_FALSE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
802       "res" / "wgt" / "DELETED"));
803   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
804       "res" / "wgt" / "ADDED"));
805   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
806       "res" / "wgt" / "css" / "style.css"));
807   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
808       "res" / "wgt" / "images" / "tizen_32.png"));
809   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
810       "res" / "wgt" / "js" / "main.js"));
811   ASSERT_TRUE(ValidateFileContentInPackage(
812       pkgid, "res/wgt/MODIFIED", "version 2\n"));
813 }
814
815 TEST_F(SmokeTest, RecoveryMode_ForInstallation) {
816   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForInstallation.wgt";
817   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
818   backend_crash.Run("-i", path.string(), "-u", kTestUserIdStr.c_str());
819   ASSERT_NE(backend_crash.Wait(), 0);
820
821   std::string pkgid = "smokeapp09";
822   std::string appid = "smokeapp09.RecoveryModeForInstallation";
823   bf::path recovery_file = FindRecoveryFile();
824   ASSERT_FALSE(recovery_file.empty());
825   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
826       ci::AppInstaller::Result::OK);
827   CheckPackageNonExistance(pkgid, {appid});
828 }
829
830 TEST_F(SmokeTest, RecoveryMode_ForUpdate) {
831   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForUpdate.wgt";
832   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForUpdate_2.wgt";
833   RemoveAllRecoveryFiles();
834   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
835   std::string pkgid = "smokeapp10";
836   std::string appid = "smokeapp10.RecoveryModeForUpdate";
837   AddDataFiles(pkgid, kTestUserId);
838   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
839   backend_crash.Run("-i", path_new.string(), "-u", kTestUserIdStr.c_str());
840   ASSERT_NE(backend_crash.Wait(), 0);
841
842   bf::path recovery_file = FindRecoveryFile();
843   ASSERT_FALSE(recovery_file.empty());
844   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
845             ci::AppInstaller::Result::OK);
846   ValidatePackage(pkgid, {appid});
847
848   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
849   ValidateDataFiles(pkgid, kTestUserId);
850 }
851
852 TEST_F(SmokeTest, RecoveryMode_ForDelta) {
853   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForDelta.wgt";
854   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForDelta.delta";
855   RemoveAllRecoveryFiles();
856   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
857   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
858   backend_crash.Run("-i", path_new.string(), "-u", kTestUserIdStr.c_str());
859   ASSERT_NE(backend_crash.Wait(), 0);
860
861   std::string pkgid = "smokeapp30";
862   std::string appid = "smokeapp30.RecoveryModeForDelta";
863   bf::path recovery_file = FindRecoveryFile();
864   ASSERT_FALSE(recovery_file.empty());
865   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
866             ci::AppInstaller::Result::OK);
867   ValidatePackage(pkgid, {appid});
868
869   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
870 }
871
872 TEST_F(SmokeTest, RecoveryMode_ForMountInstall) {
873   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForMountInstall.wgt";
874   RemoveAllRecoveryFiles();
875   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
876   backend_crash.Run("-w", path.string(), "-u", kTestUserIdStr.c_str());
877   ASSERT_NE(backend_crash.Wait(), 0);
878
879   std::string pkgid = "smokeapp31";
880   std::string appid = "smokeapp31.RecoveryModeForMountInstall";
881   bf::path recovery_file = FindRecoveryFile();
882   ASSERT_FALSE(recovery_file.empty());
883   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
884             ci::AppInstaller::Result::OK);
885   CheckPackageNonExistance(pkgid, {appid});
886 }
887
888 TEST_F(SmokeTest, RecoveryMode_ForMountUpdate) {
889   bf::path path_old =
890       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate.wgt";
891   bf::path path_new =
892       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate_2.wgt";
893   std::string pkgid = "smokeapp32";
894   std::string appid = "smokeapp32.RecoveryModeForMountUpdate";
895   RemoveAllRecoveryFiles();
896   ASSERT_EQ(MountInstall(path_old, PackageType::WGT),
897             ci::AppInstaller::Result::OK);
898   AddDataFiles(pkgid, kTestUserId);
899   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
900   backend_crash.Run("-w", path_new.string(), "-u", kTestUserIdStr.c_str());
901   ASSERT_NE(backend_crash.Wait(), 0);
902
903   // Filesystem may be mounted after crash
904   ScopedTzipInterface poweroff_unmount_interface(pkgid);
905   poweroff_unmount_interface.Release();
906
907   bf::path recovery_file = FindRecoveryFile();
908   ASSERT_FALSE(recovery_file.empty());
909   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
910             ci::AppInstaller::Result::OK);
911
912   ScopedTzipInterface interface(pkgid);
913   ValidatePackage(pkgid, {appid});
914   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
915   ValidateDataFiles(pkgid, kTestUserId);
916 }
917
918 TEST_F(SmokeTest, InstallationMode_GoodSignature) {
919   bf::path path = kSmokePackagesDirectory / "InstallationMode_GoodSignature.wgt";  // NOLINT
920   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
921 }
922
923 TEST_F(SmokeTest, InstallationMode_WrongSignature) {
924   bf::path path = kSmokePackagesDirectory / "InstallationMode_WrongSignature.wgt";  // NOLINT
925   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::ERROR);
926 }
927
928 TEST_F(SmokeTest, InstallationMode_Rollback) {
929   bf::path path = kSmokePackagesDirectory / "InstallationMode_Rollback.wgt";
930   std::string pkgid = "smokeapp06";
931   std::string appid = "smokeapp06.InstallationModeRollback";
932   ASSERT_EQ(Install(path, PackageType::WGT, RequestResult::FAIL),
933             ci::AppInstaller::Result::ERROR);
934   CheckPackageNonExistance(pkgid, {appid});
935 }
936
937 TEST_F(SmokeTest, UpdateMode_Rollback) {
938   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Rollback.wgt";
939   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Rollback_2.wgt";
940   std::string pkgid = "smokeapp07";
941   std::string appid = "smokeapp07.UpdateModeRollback";
942   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
943   AddDataFiles(pkgid, kTestUserId);
944   ASSERT_EQ(Install(path_new, PackageType::WGT, RequestResult::FAIL),
945                     ci::AppInstaller::Result::ERROR);
946   ValidatePackage(pkgid, {appid});
947
948   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
949   ValidateDataFiles(pkgid, kTestUserId);
950 }
951
952 TEST_F(SmokeTest, DeltaMode_Rollback) {
953   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback.wgt";
954   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode_Rollback.delta";
955   std::string pkgid = "smokeapp37";
956   std::string appid = "smokeapp37.DeltaMode";
957   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
958   AddDataFiles(pkgid, kTestUserId);
959   ASSERT_EQ(Install(delta_package, PackageType::WGT, RequestResult::FAIL),
960             ci::AppInstaller::Result::ERROR);
961
962   ValidatePackage(pkgid, {appid});
963   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
964                                            "version 1\n"));
965   ValidateDataFiles(pkgid, kTestUserId);
966   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
967                          "res/wgt/DELETED"));
968   ASSERT_FALSE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
969                           "res/wgt/ADDED"));
970 }
971
972 TEST_F(SmokeTest, InstallationMode_Hybrid) {
973   bf::path path = kSmokePackagesDirectory / "InstallationMode_Hybrid.wgt";
974   std::string pkgid = "smokehyb01";
975   // Excutable for native app doesn't create symlink
976   std::string appid1 = "smokehyb01.Web";
977   ASSERT_EQ(Install(path, PackageType::HYBRID), ci::AppInstaller::Result::OK);
978   ValidatePackage(pkgid, {appid1});
979 }
980
981 TEST_F(SmokeTest, UpdateMode_Hybrid) {
982   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Hybrid.wgt";
983   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Hybrid_2.wgt";
984   std::string pkgid = "smokehyb02";
985   std::string appid1 = "smokehyb02.Web";
986   ASSERT_EQ(Install(path_old, PackageType::HYBRID),
987             ci::AppInstaller::Result::OK);
988 //  AddDataFiles(pkgid, kTestUserId);
989   ASSERT_EQ(Install(path_new, PackageType::HYBRID),
990             ci::AppInstaller::Result::OK);
991   ValidatePackage(pkgid, {appid1});
992
993   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
994   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "VERSION", "2\n"));
995 //  ValidateDataFiles(pkgid, kTestUserId);
996 }
997
998 TEST_F(SmokeTest, DeinstallationMode_Hybrid) {
999   bf::path path = kSmokePackagesDirectory / "DeinstallationMode_Hybrid.wgt";
1000   std::string pkgid = "smokehyb03";
1001   std::string appid1 = "smokehyb03.Web";
1002   ASSERT_EQ(Install(path, PackageType::HYBRID),
1003             ci::AppInstaller::Result::OK);
1004   ASSERT_EQ(Uninstall(pkgid, PackageType::HYBRID),
1005             ci::AppInstaller::Result::OK);
1006   CheckPackageNonExistance(pkgid, {appid1});
1007 }
1008
1009 TEST_F(SmokeTest, DeltaMode_Hybrid) {
1010   bf::path path = kSmokePackagesDirectory / "DeltaMode_Hybrid.wgt";
1011   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode_Hybrid.delta";
1012   std::string pkgid = "smokehyb04";
1013   std::string appid1 = "smokehyb04.Web";
1014   ASSERT_EQ(DeltaInstall(path, delta_package, PackageType::HYBRID),
1015             ci::AppInstaller::Result::OK);
1016   ValidatePackage(pkgid, {appid1});
1017
1018   // Check delta modifications
1019   bf::path root_path = ci::GetRootAppPath(false,
1020       kTestUserId);
1021   ASSERT_FALSE(bf::exists(root_path / pkgid / "res" / "wgt" / "DELETED"));
1022   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "ADDED"));
1023   ASSERT_FALSE(bf::exists(root_path / pkgid / "lib" / "DELETED"));
1024   ASSERT_TRUE(bf::exists(root_path / pkgid / "lib" / "ADDED"));
1025   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "css" / "style.css"));  // NOLINT
1026   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "images" / "tizen_32.png"));  // NOLINT
1027   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "js" / "main.js"));
1028   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED", "version 2\n"));  // NOLINT
1029   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/MODIFIED", "version 2\n"));  // NOLINT
1030 }
1031
1032 TEST_F(SmokeTest, MountInstallationMode_Hybrid) {
1033   bf::path path = kSmokePackagesDirectory / "MountInstallationMode_Hybrid.wgt";
1034   std::string pkgid = "smokehyb05";
1035   std::string appid1 = "smokehyb05.web";
1036   ASSERT_EQ(MountInstall(path, PackageType::HYBRID),
1037             ci::AppInstaller::Result::OK);
1038   ScopedTzipInterface interface(pkgid);
1039   ValidatePackage(pkgid, {appid1});
1040 }
1041
1042 TEST_F(SmokeTest, MountUpdateMode_Hybrid) {
1043   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode_Hybrid.wgt";
1044   bf::path path_new = kSmokePackagesDirectory / "MountUpdateMode_Hybrid_2.wgt";
1045   std::string pkgid = "smokehyb06";
1046   std::string appid1 = "smokehyb06.web";
1047   ASSERT_EQ(MountInstall(path_old, PackageType::HYBRID),
1048             ci::AppInstaller::Result::OK);
1049   AddDataFiles(pkgid, kTestUserId);
1050   ASSERT_EQ(MountInstall(path_new, PackageType::HYBRID),
1051             ci::AppInstaller::Result::OK);
1052   ScopedTzipInterface interface(pkgid);
1053   ValidatePackage(pkgid, {appid1});
1054
1055   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
1056   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/VERSION", "2\n"));
1057   ValidateDataFiles(pkgid, kTestUserId);
1058 }
1059
1060 TEST_F(SmokeTest, InstallationMode_Rollback_Hybrid) {
1061   bf::path path = kSmokePackagesDirectory /
1062       "InstallationMode_Rollback_Hybrid.wgt";
1063   std::string pkgid = "smokehyb07";
1064   std::string appid1 = "smokehyb07.web";
1065   ASSERT_EQ(Install(path, PackageType::HYBRID, RequestResult::FAIL),
1066             ci::AppInstaller::Result::ERROR);
1067   CheckPackageNonExistance(pkgid, {appid1});
1068 }
1069
1070 TEST_F(SmokeTest, UpdateMode_Rollback_Hybrid) {
1071   bf::path path_old = kSmokePackagesDirectory /
1072       "UpdateMode_Rollback_Hybrid.wgt";
1073   bf::path path_new = kSmokePackagesDirectory /
1074       "UpdateMode_Rollback_Hybrid_2.wgt";
1075   std::string pkgid = "smokehyb08";
1076   std::string appid1 = "smokehyb08.web";
1077   ASSERT_EQ(Install(path_old, PackageType::HYBRID),
1078             ci::AppInstaller::Result::OK);
1079   AddDataFiles(pkgid, kTestUserId);
1080   ASSERT_EQ(Install(path_new, PackageType::HYBRID,
1081       RequestResult::FAIL), ci::AppInstaller::Result::ERROR);
1082   ValidatePackage(pkgid, {appid1});
1083
1084   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
1085   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/VERSION", "1\n"));
1086   ValidateDataFiles(pkgid, kTestUserId);
1087 }
1088
1089 TEST_F(SmokeTest, DeltaMode_Rollback_Hybrid) {
1090   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback_Hybrid.wgt";
1091   bf::path delta_package = kSmokePackagesDirectory /
1092       "DeltaMode_Rollback_Hybrid.delta";
1093   std::string pkgid = "smokehyb11";
1094   std::string appid1 = "smokehyb11.web";
1095   ASSERT_EQ(Install(path, PackageType::HYBRID), ci::AppInstaller::Result::OK);
1096   AddDataFiles(pkgid, kTestUserId);
1097   ASSERT_EQ(Install(delta_package, PackageType::HYBRID, RequestResult::FAIL),
1098             ci::AppInstaller::Result::ERROR);
1099
1100   ValidatePackage(pkgid, {appid1});
1101   // Check delta modifications
1102   bf::path root_path = GetPackageRoot(pkgid, kTestUserId);
1103   ASSERT_TRUE(bf::exists(root_path / "res" / "wgt" / "DELETED"));
1104   ASSERT_FALSE(bf::exists(root_path / "res" / "wgt" / "ADDED"));
1105   ASSERT_TRUE(bf::exists(root_path / "lib" / "DELETED"));
1106   ASSERT_FALSE(bf::exists(root_path / "lib" / "ADDED"));
1107   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
1108                                            "version 1\n"));
1109   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/MODIFIED",
1110                                            "version 1\n"));
1111   ValidateDataFiles(pkgid, kTestUserId);
1112 }
1113
1114 TEST_F(SmokeTest, MountInstallationMode_Rollback_Hybrid) {
1115   bf::path path = kSmokePackagesDirectory /
1116       "MountInstallationMode_Rollback_Hybrid.wgt";
1117   std::string pkgid = "smokehyb09";
1118   std::string appid1 = "smokehyb09.web";
1119   ASSERT_EQ(MountInstall(path, PackageType::HYBRID, RequestResult::FAIL),
1120       ci::AppInstaller::Result::ERROR);
1121   ScopedTzipInterface interface(pkgid);
1122   CheckPackageNonExistance(pkgid, {appid1});
1123 }
1124
1125 TEST_F(SmokeTest, MountUpdateMode_Rollback_Hybrid) {
1126   bf::path path_old = kSmokePackagesDirectory /
1127       "MountUpdateMode_Rollback_Hybrid.wgt";
1128   bf::path path_new = kSmokePackagesDirectory /
1129       "MountUpdateMode_Rollback_Hybrid_2.wgt";
1130   std::string pkgid = "smokehyb10";
1131   std::string appid1 = "smokehyb10.web";
1132   ASSERT_EQ(MountInstall(path_old, PackageType::HYBRID),
1133             ci::AppInstaller::Result::OK);
1134   AddDataFiles(pkgid, kTestUserId);
1135   ASSERT_EQ(MountInstall(path_new, PackageType::HYBRID,
1136       RequestResult::FAIL), ci::AppInstaller::Result::ERROR);
1137   ScopedTzipInterface interface(pkgid);
1138   ValidatePackage(pkgid, {appid1});
1139
1140   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
1141   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/VERSION", "1\n"));
1142   ValidateDataFiles(pkgid, kTestUserId);
1143 }
1144
1145 TEST_F(SmokeTest, MountInstallationMode) {
1146   bf::path path = kSmokePackagesDirectory / "MountInstallationMode.wgt";
1147   std::string pkgid = "smokeapp28";
1148   std::string appid = "smokeapp28.InstallationMode";
1149   ASSERT_EQ(MountInstall(path, PackageType::WGT), ci::AppInstaller::Result::OK);
1150   ScopedTzipInterface interface(pkgid);
1151   ValidatePackage(pkgid, {appid});
1152 }
1153
1154 TEST_F(SmokeTest, MountUpdateMode) {
1155   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode.wgt";
1156   bf::path path_new = kSmokePackagesDirectory / "MountUpdateMode_2.wgt";
1157   std::string pkgid = "smokeapp29";
1158   std::string appid = "smokeapp29.UpdateMode";
1159   ASSERT_EQ(MountInstall(path_old, PackageType::WGT),
1160             ci::AppInstaller::Result::OK);
1161   AddDataFiles(pkgid, kTestUserId);
1162   ASSERT_EQ(MountInstall(path_new, PackageType::WGT),
1163             ci::AppInstaller::Result::OK);
1164   ScopedTzipInterface interface(pkgid);
1165   ValidatePackage(pkgid, {appid});
1166
1167   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
1168   ValidateDataFiles(pkgid, kTestUserId);
1169 }
1170
1171 TEST_F(SmokeTest, MountInstallationMode_Rollback) {
1172   bf::path path =
1173       kSmokePackagesDirectory / "MountInstallationMode_Rollback.wgt";
1174   std::string pkgid = "smokeapp33";
1175   std::string appid = "smokeapp33.web";
1176   ASSERT_EQ(MountInstall(path, PackageType::WGT, RequestResult::FAIL),
1177             ci::AppInstaller::Result::ERROR);
1178   ScopedTzipInterface interface(pkgid);
1179   CheckPackageNonExistance(pkgid, {appid});
1180 }
1181
1182 TEST_F(SmokeTest, MountUpdateMode_Rollback) {
1183   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode_Rollback.wgt";
1184   bf::path path_new =
1185       kSmokePackagesDirectory / "MountUpdateMode_Rollback_2.wgt";
1186   std::string pkgid = "smokeapp34";
1187   std::string appid = "smokeapp34.web";
1188   ASSERT_EQ(MountInstall(path_old, PackageType::WGT),
1189             ci::AppInstaller::Result::OK);
1190   AddDataFiles(pkgid, kTestUserId);
1191   ASSERT_EQ(MountInstall(path_new, PackageType::WGT,
1192       RequestResult::FAIL), ci::AppInstaller::Result::ERROR);
1193   ScopedTzipInterface interface(pkgid);
1194   ValidatePackage(pkgid, {appid});
1195
1196   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
1197   ValidateDataFiles(pkgid, kTestUserId);
1198 }
1199
1200 TEST_F(SmokeTest, UserDefinedPlugins) {
1201   bf::path path = kSmokePackagesDirectory / "SimpleEchoPrivilege.wgt";
1202   std::string pkgid = "0CSPVhKmRk";
1203   std::string appid = "0CSPVhKmRk.SimpleEcho";
1204   std::string call_privilege = "http://tizen.org/privilege/call";
1205   std::string location_privilege = "http://tizen.org/privilege/location";
1206   std::string power_privilege = "http://tizen.org/privilege/power";
1207
1208   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
1209   ValidatePackage(pkgid, {appid});
1210   std::vector<std::string> res;
1211   ASSERT_TRUE(ci::QueryPrivilegesForPkgId(pkgid, kTestUserId, &res));
1212   ASSERT_TRUE(std::find(res.begin(), res.end(), call_privilege) != res.end());
1213   ASSERT_TRUE(std::find(res.begin(), res.end(), location_privilege)
1214           != res.end());
1215   ASSERT_TRUE(std::find(res.begin(), res.end(), power_privilege) != res.end());
1216 }
1217
1218 TEST_F(SmokeTest, InstallExternalMode) {
1219   ASSERT_TRUE(CheckAvailableExternalPath());
1220   bf::path path = kSmokePackagesDirectory / "InstallExternalMode.wgt";
1221   std::string pkgid = "smokeapp35";
1222   std::string appid = "smokeapp35.web";
1223   ASSERT_EQ(InstallExternal(path, PackageType::WGT),
1224       ci::AppInstaller::Result::OK);
1225   ValidateExternalPackage(pkgid, {appid});
1226 }
1227
1228 TEST_F(SmokeTest, MigrateLegacyExternalImageMode) {
1229   ASSERT_TRUE(CheckAvailableExternalPath());
1230   bf::path path =
1231       kSmokePackagesDirectory / "MigrateLegacyExternalImageMode.wgt";
1232   std::string pkgid = "smokeapp36";
1233   std::string appid = "smokeapp36.web";
1234   bf::path legacy_path = kSmokePackagesDirectory / kLegacyExtImageDir;
1235   ASSERT_EQ(MigrateLegacyExternalImage(pkgid, path, legacy_path,
1236       PackageType::WGT), ci::AppInstaller::Result::OK);
1237   ValidateExternalPackage(pkgid, {appid});
1238 }
1239
1240 TEST_F(SmokeTest, InstallationMode_Preload) {
1241   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
1242   bf::path path = kSmokePackagesDirectory / "InstallationMode_Preload.wgt";
1243   std::string pkgid = "smokeapp37";
1244   std::string appid = "smokeapp37.InstallationModePreload";
1245   ASSERT_EQ(InstallPreload(path, PackageType::WGT),
1246             ci::AppInstaller::Result::OK);
1247   ValidatePackage(pkgid, {appid}, true);
1248 }
1249
1250 TEST_F(SmokeTest, UpdateMode_Preload) {
1251   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
1252   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Preload.wgt";
1253   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Preload2.wgt";
1254   std::string pkgid = "smokeapp38";
1255   std::string appid = "smokeapp38.UpdateModePreload";
1256   ASSERT_EQ(InstallPreload(path_old, PackageType::WGT),
1257             ci::AppInstaller::Result::OK);
1258   AddDataFiles(pkgid, kTestUserId);
1259   ASSERT_EQ(InstallPreload(path_new, PackageType::WGT),
1260             ci::AppInstaller::Result::OK);
1261   ValidatePackage(pkgid, {appid}, true);
1262
1263   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2",
1264                                            true));
1265   ValidateDataFiles(pkgid, kTestUserId);
1266 }
1267
1268 TEST_F(SmokeTest, DeinstallationMode_Preload) {
1269   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
1270   bf::path path = kSmokePackagesDirectory / "DeinstallationMode_Preload.wgt";
1271   std::string pkgid = "smokeapp39";
1272   std::string appid = "smokeapp39.DeinstallationModePreload";
1273   ASSERT_EQ(InstallPreload(path, PackageType::WGT),
1274             ci::AppInstaller::Result::OK);
1275   ASSERT_EQ(UninstallPreload(pkgid, PackageType::WGT),
1276             ci::AppInstaller::Result::OK);
1277   CheckPackageReadonlyNonExistance(pkgid, {appid});
1278 }
1279
1280 }  // namespace common_installer
1281
1282 int main(int argc,  char** argv) {
1283   testing::InitGoogleTest(&argc, argv);
1284   testing::AddGlobalTestEnvironment(
1285       new common_installer::SmokeEnvironment(kGlobalUserUid));
1286   return RUN_ALL_TESTS();
1287 }