Merge "Smoke tests for preload installation, update and deinstallation" into tizen
[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   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
599 }
600
601 void BackupPath(const bf::path& path) {
602   bf::path backup_path = path.string() + ".bck";
603   std::cout << "Backup path: " << path << " to " << backup_path << std::endl;
604   bs::error_code error;
605   bf::remove_all(backup_path, error);
606   if (error)
607     LOG(ERROR) << "Remove failed: " << backup_path
608                << " (" << error.message() << ")";
609   if (bf::exists(path)) {
610     bf::rename(path, backup_path, error);
611     if (error)
612       LOG(ERROR) << "Failed to setup test environment. Does some previous"
613                  << " test crashed? Path: "
614                  << backup_path << " should not exist.";
615     assert(!error);
616   }
617 }
618
619 void RestorePath(const bf::path& path) {
620   bf::path backup_path = path.string() + ".bck";
621   std::cout << "Restore path: " << path << " from " << backup_path << std::endl;
622   bs::error_code error;
623   bf::remove_all(path, error);
624   if (error)
625     LOG(ERROR) << "Remove failed: " << path
626                << " (" << error.message() << ")";
627   if (bf::exists(backup_path)) {
628     bf::rename(backup_path, path, error);
629     if (error)
630       LOG(ERROR) << "Failed to restore backup path: " << backup_path
631                  << " (" << error.message() << ")";
632   }
633 }
634
635 std::vector<bf::path> SetupBackupDirectories(uid_t uid) {
636   std::vector<bf::path> entries;
637   bf::path db_dir = bf::path(tzplatform_getenv(TZ_SYS_DB));
638   if (uid != kGlobalUserUid)
639     db_dir = db_dir / "user" / std::to_string(uid);
640   for (auto e : kDBEntries) {
641     bf::path path = db_dir / e;
642     entries.emplace_back(path);
643   }
644
645   if (getuid() == 0) {
646     entries.emplace_back(kPreloadApps);
647     entries.emplace_back(kPreloadManifestDir);
648     entries.emplace_back(kPreloadIcons);
649   }
650
651   if (uid == kGlobalUserUid) {
652     entries.emplace_back(kSkelDir);
653     entries.emplace_back(kGlobalManifestDir);
654     ci::UserList list = ci::GetUserList();
655     for (auto l : list) {
656       bf::path apps = std::get<2>(l) / "apps_rw";
657       entries.emplace_back(apps);
658     }
659   } else {
660     tzplatform_set_user(uid);
661     bf::path approot = tzplatform_getenv(TZ_USER_APPROOT);
662     tzplatform_reset_user();
663     entries.emplace_back(approot);
664   }
665
666   bf::path apps_rw = ci::GetRootAppPath(false, uid);
667   entries.emplace_back(apps_rw);
668
669   return entries;
670 }
671
672 }  // namespace
673
674 namespace common_installer {
675
676 class SmokeEnvironment : public testing::Environment {
677  public:
678   explicit SmokeEnvironment(uid_t uid) : uid_(uid) {
679   }
680   void SetUp() override {
681     backups_ = SetupBackupDirectories(uid_);
682     for (auto& path : backups_)
683       BackupPath(path);
684   }
685   void TearDown() override {
686     // TODO(s89.jang): Uninstall smoke packages to clear security context
687     for (auto& path : backups_)
688       RestorePath(path);
689   }
690
691  private:
692   uid_t uid_;
693   std::vector<bf::path> backups_;
694 };
695
696 class SmokeTest : public testing::Test {
697 };
698
699 TEST_F(SmokeTest, InstallationMode) {
700   bf::path path = kSmokePackagesDirectory / "InstallationMode.wgt";
701   std::string pkgid = "smokeapp03";
702   std::string appid = "smokeapp03.InstallationMode";
703   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
704   ValidatePackage(pkgid, {appid});
705 }
706
707 TEST_F(SmokeTest, UpdateMode) {
708   bf::path path_old = kSmokePackagesDirectory / "UpdateMode.wgt";
709   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_2.wgt";
710   std::string pkgid = "smokeapp04";
711   std::string appid = "smokeapp04.UpdateMode";
712   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
713   AddDataFiles(pkgid, kTestUserId);
714   ASSERT_EQ(Install(path_new, PackageType::WGT), ci::AppInstaller::Result::OK);
715   ValidatePackage(pkgid, {appid});
716
717   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
718   ValidateDataFiles(pkgid, kTestUserId);
719 }
720
721 TEST_F(SmokeTest, DeinstallationMode) {
722   bf::path path = kSmokePackagesDirectory / "DeinstallationMode.wgt";
723   std::string pkgid = "smokeapp05";
724   std::string appid = "smokeapp05.DeinstallationMode";
725   ASSERT_EQ(Install(path, PackageType::WGT),
726             ci::AppInstaller::Result::OK);
727   ASSERT_EQ(Uninstall(pkgid, PackageType::WGT), ci::AppInstaller::Result::OK);
728   CheckPackageNonExistance(pkgid, {appid});
729 }
730
731 TEST_F(SmokeTest, RDSMode) {
732   bf::path path = kSmokePackagesDirectory / "RDSMode.wgt";
733   std::string pkgid = "smokeapp11";
734   std::string appid = "smokeapp11.RDSMode";
735   bf::path delta_directory = kSmokePackagesDirectory / "delta_dir/";
736   bf::path sdk_expected_directory =
737       bf::path(ci::GetRootAppPath(false, kTestUserId)) / "tmp" / pkgid;
738   bs::error_code error;
739   bf::create_directories(sdk_expected_directory.parent_path(), error);
740   ASSERT_FALSE(error);
741   ASSERT_TRUE(CopyDir(delta_directory, sdk_expected_directory));
742   ASSERT_EQ(RDSUpdate(path, pkgid, PackageType::WGT),
743             ci::AppInstaller::Result::OK);
744   ValidatePackage(pkgid, {appid});
745
746   // Check delta modifications
747   ASSERT_FALSE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
748       "res" / "wgt" / "DELETED"));
749   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
750       "res" / "wgt" / "ADDED"));
751   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED", "2\n"));
752 }
753
754 TEST_F(SmokeTest, EnablePkg) {
755   bf::path path = kSmokePackagesDirectory / "EnablePkg.wgt";
756   std::string pkgid = "smokeapp22";
757   ASSERT_EQ(Install(path, PackageType::WGT),
758             ci::AppInstaller::Result::OK);
759   ASSERT_EQ(DisablePackage(pkgid, PackageType::WGT),
760             ci::AppInstaller::Result::OK);
761   ASSERT_EQ(EnablePackage(pkgid, PackageType::WGT),
762             ci::AppInstaller::Result::OK);
763
764   ASSERT_TRUE(ci::QueryIsPackageInstalled(pkgid,
765       ci::GetRequestMode(kTestUserId),
766       kTestUserId));
767 }
768
769 TEST_F(SmokeTest, DisablePkg) {
770   bf::path path = kSmokePackagesDirectory / "DisablePkg.wgt";
771   std::string pkgid = "smokeapp21";
772   std::string appid = "smokeapp21.DisablePkg";
773   ASSERT_EQ(Install(path, PackageType::WGT),
774             ci::AppInstaller::Result::OK);
775   ASSERT_EQ(DisablePackage(pkgid, PackageType::WGT),
776             ci::AppInstaller::Result::OK);
777   ASSERT_TRUE(ci::QueryIsDisabledPackage(pkgid, kTestUserId));
778   ValidatePackage(pkgid, {appid});
779 }
780
781 TEST_F(SmokeTest, DeltaMode) {
782   bf::path path = kSmokePackagesDirectory / "DeltaMode.wgt";
783   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode.delta";
784   std::string pkgid = "smokeapp17";
785   std::string appid = "smokeapp17.DeltaMode";
786   ASSERT_EQ(DeltaInstall(path, delta_package, PackageType::WGT),
787             ci::AppInstaller::Result::OK);
788   ValidatePackage(pkgid, {appid});
789
790   // Check delta modifications
791   ASSERT_FALSE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
792       "res" / "wgt" / "DELETED"));
793   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
794       "res" / "wgt" / "ADDED"));
795   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
796       "res" / "wgt" / "css" / "style.css"));
797   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
798       "res" / "wgt" / "images" / "tizen_32.png"));
799   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
800       "res" / "wgt" / "js" / "main.js"));
801   ASSERT_TRUE(ValidateFileContentInPackage(
802       pkgid, "res/wgt/MODIFIED", "version 2\n"));
803 }
804
805 TEST_F(SmokeTest, RecoveryMode_ForInstallation) {
806   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForInstallation.wgt";
807   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
808   backend_crash.Run("-i", path.string(), "-u", kTestUserIdStr.c_str());
809   ASSERT_NE(backend_crash.Wait(), 0);
810
811   std::string pkgid = "smokeapp09";
812   std::string appid = "smokeapp09.RecoveryModeForInstallation";
813   bf::path recovery_file = FindRecoveryFile();
814   ASSERT_FALSE(recovery_file.empty());
815   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
816       ci::AppInstaller::Result::OK);
817   CheckPackageNonExistance(pkgid, {appid});
818 }
819
820 TEST_F(SmokeTest, RecoveryMode_ForUpdate) {
821   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForUpdate.wgt";
822   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForUpdate_2.wgt";
823   RemoveAllRecoveryFiles();
824   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
825   std::string pkgid = "smokeapp10";
826   std::string appid = "smokeapp10.RecoveryModeForUpdate";
827   AddDataFiles(pkgid, kTestUserId);
828   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
829   backend_crash.Run("-i", path_new.string(), "-u", kTestUserIdStr.c_str());
830   ASSERT_NE(backend_crash.Wait(), 0);
831
832   bf::path recovery_file = FindRecoveryFile();
833   ASSERT_FALSE(recovery_file.empty());
834   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
835             ci::AppInstaller::Result::OK);
836   ValidatePackage(pkgid, {appid});
837
838   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
839   ValidateDataFiles(pkgid, kTestUserId);
840 }
841
842 TEST_F(SmokeTest, RecoveryMode_ForDelta) {
843   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForDelta.wgt";
844   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForDelta.delta";
845   RemoveAllRecoveryFiles();
846   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
847   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
848   backend_crash.Run("-i", path_new.string(), "-u", kTestUserIdStr.c_str());
849   ASSERT_NE(backend_crash.Wait(), 0);
850
851   std::string pkgid = "smokeapp30";
852   std::string appid = "smokeapp30.RecoveryModeForDelta";
853   bf::path recovery_file = FindRecoveryFile();
854   ASSERT_FALSE(recovery_file.empty());
855   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
856             ci::AppInstaller::Result::OK);
857   ValidatePackage(pkgid, {appid});
858
859   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
860 }
861
862 TEST_F(SmokeTest, RecoveryMode_ForMountInstall) {
863   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForMountInstall.wgt";
864   RemoveAllRecoveryFiles();
865   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
866   backend_crash.Run("-w", path.string(), "-u", kTestUserIdStr.c_str());
867   ASSERT_NE(backend_crash.Wait(), 0);
868
869   std::string pkgid = "smokeapp31";
870   std::string appid = "smokeapp31.RecoveryModeForMountInstall";
871   bf::path recovery_file = FindRecoveryFile();
872   ASSERT_FALSE(recovery_file.empty());
873   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
874             ci::AppInstaller::Result::OK);
875   CheckPackageNonExistance(pkgid, {appid});
876 }
877
878 TEST_F(SmokeTest, RecoveryMode_ForMountUpdate) {
879   bf::path path_old =
880       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate.wgt";
881   bf::path path_new =
882       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate_2.wgt";
883   std::string pkgid = "smokeapp32";
884   std::string appid = "smokeapp32.RecoveryModeForMountUpdate";
885   RemoveAllRecoveryFiles();
886   ASSERT_EQ(MountInstall(path_old, PackageType::WGT),
887             ci::AppInstaller::Result::OK);
888   AddDataFiles(pkgid, kTestUserId);
889   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
890   backend_crash.Run("-w", path_new.string(), "-u", kTestUserIdStr.c_str());
891   ASSERT_NE(backend_crash.Wait(), 0);
892
893   // Filesystem may be mounted after crash
894   ScopedTzipInterface poweroff_unmount_interface(pkgid);
895   poweroff_unmount_interface.Release();
896
897   bf::path recovery_file = FindRecoveryFile();
898   ASSERT_FALSE(recovery_file.empty());
899   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
900             ci::AppInstaller::Result::OK);
901
902   ScopedTzipInterface interface(pkgid);
903   ValidatePackage(pkgid, {appid});
904   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
905   ValidateDataFiles(pkgid, kTestUserId);
906 }
907
908 TEST_F(SmokeTest, InstallationMode_GoodSignature) {
909   bf::path path = kSmokePackagesDirectory / "InstallationMode_GoodSignature.wgt";  // NOLINT
910   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
911 }
912
913 TEST_F(SmokeTest, InstallationMode_WrongSignature) {
914   bf::path path = kSmokePackagesDirectory / "InstallationMode_WrongSignature.wgt";  // NOLINT
915   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::ERROR);
916 }
917
918 TEST_F(SmokeTest, InstallationMode_Rollback) {
919   bf::path path = kSmokePackagesDirectory / "InstallationMode_Rollback.wgt";
920   std::string pkgid = "smokeapp06";
921   std::string appid = "smokeapp06.InstallationModeRollback";
922   ASSERT_EQ(Install(path, PackageType::WGT, RequestResult::FAIL),
923             ci::AppInstaller::Result::ERROR);
924   CheckPackageNonExistance(pkgid, {appid});
925 }
926
927 TEST_F(SmokeTest, UpdateMode_Rollback) {
928   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Rollback.wgt";
929   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Rollback_2.wgt";
930   std::string pkgid = "smokeapp07";
931   std::string appid = "smokeapp07.UpdateModeRollback";
932   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
933   AddDataFiles(pkgid, kTestUserId);
934   ASSERT_EQ(Install(path_new, PackageType::WGT, RequestResult::FAIL),
935                     ci::AppInstaller::Result::ERROR);
936   ValidatePackage(pkgid, {appid});
937
938   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
939   ValidateDataFiles(pkgid, kTestUserId);
940 }
941
942 TEST_F(SmokeTest, DeltaMode_Rollback) {
943   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback.wgt";
944   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode_Rollback.delta";
945   std::string pkgid = "smokeapp37";
946   std::string appid = "smokeapp37.DeltaMode";
947   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
948   AddDataFiles(pkgid, kTestUserId);
949   ASSERT_EQ(Install(delta_package, PackageType::WGT, RequestResult::FAIL),
950             ci::AppInstaller::Result::ERROR);
951
952   ValidatePackage(pkgid, {appid});
953   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
954                                            "version 1\n"));
955   ValidateDataFiles(pkgid, kTestUserId);
956   ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
957                          "res/wgt/DELETED"));
958   ASSERT_FALSE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
959                           "res/wgt/ADDED"));
960 }
961
962 TEST_F(SmokeTest, InstallationMode_Hybrid) {
963   bf::path path = kSmokePackagesDirectory / "InstallationMode_Hybrid.wgt";
964   std::string pkgid = "smokehyb01";
965   // Excutable for native app doesn't create symlink
966   std::string appid1 = "smokehyb01.Web";
967   ASSERT_EQ(Install(path, PackageType::HYBRID), ci::AppInstaller::Result::OK);
968   ValidatePackage(pkgid, {appid1});
969 }
970
971 TEST_F(SmokeTest, UpdateMode_Hybrid) {
972   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Hybrid.wgt";
973   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Hybrid_2.wgt";
974   std::string pkgid = "smokehyb02";
975   std::string appid1 = "smokehyb02.Web";
976   ASSERT_EQ(Install(path_old, PackageType::HYBRID),
977             ci::AppInstaller::Result::OK);
978 //  AddDataFiles(pkgid, kTestUserId);
979   ASSERT_EQ(Install(path_new, PackageType::HYBRID),
980             ci::AppInstaller::Result::OK);
981   ValidatePackage(pkgid, {appid1});
982
983   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
984   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "VERSION", "2\n"));
985 //  ValidateDataFiles(pkgid, kTestUserId);
986 }
987
988 TEST_F(SmokeTest, DeinstallationMode_Hybrid) {
989   bf::path path = kSmokePackagesDirectory / "DeinstallationMode_Hybrid.wgt";
990   std::string pkgid = "smokehyb03";
991   std::string appid1 = "smokehyb03.Web";
992   ASSERT_EQ(Install(path, PackageType::HYBRID),
993             ci::AppInstaller::Result::OK);
994   ASSERT_EQ(Uninstall(pkgid, PackageType::HYBRID),
995             ci::AppInstaller::Result::OK);
996   CheckPackageNonExistance(pkgid, {appid1});
997 }
998
999 TEST_F(SmokeTest, DeltaMode_Hybrid) {
1000   bf::path path = kSmokePackagesDirectory / "DeltaMode_Hybrid.wgt";
1001   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode_Hybrid.delta";
1002   std::string pkgid = "smokehyb04";
1003   std::string appid1 = "smokehyb04.Web";
1004   ASSERT_EQ(DeltaInstall(path, delta_package, PackageType::HYBRID),
1005             ci::AppInstaller::Result::OK);
1006   ValidatePackage(pkgid, {appid1});
1007
1008   // Check delta modifications
1009   bf::path root_path = ci::GetRootAppPath(false,
1010       kTestUserId);
1011   ASSERT_FALSE(bf::exists(root_path / pkgid / "res" / "wgt" / "DELETED"));
1012   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "ADDED"));
1013   ASSERT_FALSE(bf::exists(root_path / pkgid / "lib" / "DELETED"));
1014   ASSERT_TRUE(bf::exists(root_path / pkgid / "lib" / "ADDED"));
1015   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "css" / "style.css"));  // NOLINT
1016   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "images" / "tizen_32.png"));  // NOLINT
1017   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "js" / "main.js"));
1018   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED", "version 2\n"));  // NOLINT
1019   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/MODIFIED", "version 2\n"));  // NOLINT
1020 }
1021
1022 TEST_F(SmokeTest, MountInstallationMode_Hybrid) {
1023   bf::path path = kSmokePackagesDirectory / "MountInstallationMode_Hybrid.wgt";
1024   std::string pkgid = "smokehyb05";
1025   std::string appid1 = "smokehyb05.web";
1026   ASSERT_EQ(MountInstall(path, PackageType::HYBRID),
1027             ci::AppInstaller::Result::OK);
1028   ScopedTzipInterface interface(pkgid);
1029   ValidatePackage(pkgid, {appid1});
1030 }
1031
1032 TEST_F(SmokeTest, MountUpdateMode_Hybrid) {
1033   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode_Hybrid.wgt";
1034   bf::path path_new = kSmokePackagesDirectory / "MountUpdateMode_Hybrid_2.wgt";
1035   std::string pkgid = "smokehyb06";
1036   std::string appid1 = "smokehyb06.web";
1037   ASSERT_EQ(MountInstall(path_old, PackageType::HYBRID),
1038             ci::AppInstaller::Result::OK);
1039   AddDataFiles(pkgid, kTestUserId);
1040   ASSERT_EQ(MountInstall(path_new, PackageType::HYBRID),
1041             ci::AppInstaller::Result::OK);
1042   ScopedTzipInterface interface(pkgid);
1043   ValidatePackage(pkgid, {appid1});
1044
1045   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
1046   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/VERSION", "2\n"));
1047   ValidateDataFiles(pkgid, kTestUserId);
1048 }
1049
1050 TEST_F(SmokeTest, InstallationMode_Rollback_Hybrid) {
1051   bf::path path = kSmokePackagesDirectory /
1052       "InstallationMode_Rollback_Hybrid.wgt";
1053   std::string pkgid = "smokehyb07";
1054   std::string appid1 = "smokehyb07.web";
1055   ASSERT_EQ(Install(path, PackageType::HYBRID, RequestResult::FAIL),
1056             ci::AppInstaller::Result::ERROR);
1057   CheckPackageNonExistance(pkgid, {appid1});
1058 }
1059
1060 TEST_F(SmokeTest, UpdateMode_Rollback_Hybrid) {
1061   bf::path path_old = kSmokePackagesDirectory /
1062       "UpdateMode_Rollback_Hybrid.wgt";
1063   bf::path path_new = kSmokePackagesDirectory /
1064       "UpdateMode_Rollback_Hybrid_2.wgt";
1065   std::string pkgid = "smokehyb08";
1066   std::string appid1 = "smokehyb08.web";
1067   ASSERT_EQ(Install(path_old, PackageType::HYBRID),
1068             ci::AppInstaller::Result::OK);
1069   AddDataFiles(pkgid, kTestUserId);
1070   ASSERT_EQ(Install(path_new, PackageType::HYBRID,
1071       RequestResult::FAIL), ci::AppInstaller::Result::ERROR);
1072   ValidatePackage(pkgid, {appid1});
1073
1074   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
1075   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/VERSION", "1\n"));
1076   ValidateDataFiles(pkgid, kTestUserId);
1077 }
1078
1079 TEST_F(SmokeTest, DeltaMode_Rollback_Hybrid) {
1080   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback_Hybrid.wgt";
1081   bf::path delta_package = kSmokePackagesDirectory /
1082       "DeltaMode_Rollback_Hybrid.delta";
1083   std::string pkgid = "smokehyb11";
1084   std::string appid1 = "smokehyb11.web";
1085   ASSERT_EQ(Install(path, PackageType::HYBRID), ci::AppInstaller::Result::OK);
1086   AddDataFiles(pkgid, kTestUserId);
1087   ASSERT_EQ(Install(delta_package, PackageType::HYBRID, RequestResult::FAIL),
1088             ci::AppInstaller::Result::ERROR);
1089
1090   ValidatePackage(pkgid, {appid1});
1091   // Check delta modifications
1092   bf::path root_path = GetPackageRoot(pkgid, kTestUserId);
1093   ASSERT_TRUE(bf::exists(root_path / "res" / "wgt" / "DELETED"));
1094   ASSERT_FALSE(bf::exists(root_path / "res" / "wgt" / "ADDED"));
1095   ASSERT_TRUE(bf::exists(root_path / "lib" / "DELETED"));
1096   ASSERT_FALSE(bf::exists(root_path / "lib" / "ADDED"));
1097   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
1098                                            "version 1\n"));
1099   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/MODIFIED",
1100                                            "version 1\n"));
1101   ValidateDataFiles(pkgid, kTestUserId);
1102 }
1103
1104 TEST_F(SmokeTest, MountInstallationMode_Rollback_Hybrid) {
1105   bf::path path = kSmokePackagesDirectory /
1106       "MountInstallationMode_Rollback_Hybrid.wgt";
1107   std::string pkgid = "smokehyb09";
1108   std::string appid1 = "smokehyb09.web";
1109   ASSERT_EQ(MountInstall(path, PackageType::HYBRID, RequestResult::FAIL),
1110       ci::AppInstaller::Result::ERROR);
1111   ScopedTzipInterface interface(pkgid);
1112   CheckPackageNonExistance(pkgid, {appid1});
1113 }
1114
1115 TEST_F(SmokeTest, MountUpdateMode_Rollback_Hybrid) {
1116   bf::path path_old = kSmokePackagesDirectory /
1117       "MountUpdateMode_Rollback_Hybrid.wgt";
1118   bf::path path_new = kSmokePackagesDirectory /
1119       "MountUpdateMode_Rollback_Hybrid_2.wgt";
1120   std::string pkgid = "smokehyb10";
1121   std::string appid1 = "smokehyb10.web";
1122   ASSERT_EQ(MountInstall(path_old, PackageType::HYBRID),
1123             ci::AppInstaller::Result::OK);
1124   AddDataFiles(pkgid, kTestUserId);
1125   ASSERT_EQ(MountInstall(path_new, PackageType::HYBRID,
1126       RequestResult::FAIL), ci::AppInstaller::Result::ERROR);
1127   ScopedTzipInterface interface(pkgid);
1128   ValidatePackage(pkgid, {appid1});
1129
1130   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
1131   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/VERSION", "1\n"));
1132   ValidateDataFiles(pkgid, kTestUserId);
1133 }
1134
1135 TEST_F(SmokeTest, MountInstallationMode) {
1136   bf::path path = kSmokePackagesDirectory / "MountInstallationMode.wgt";
1137   std::string pkgid = "smokeapp28";
1138   std::string appid = "smokeapp28.InstallationMode";
1139   ASSERT_EQ(MountInstall(path, PackageType::WGT), ci::AppInstaller::Result::OK);
1140   ScopedTzipInterface interface(pkgid);
1141   ValidatePackage(pkgid, {appid});
1142 }
1143
1144 TEST_F(SmokeTest, MountUpdateMode) {
1145   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode.wgt";
1146   bf::path path_new = kSmokePackagesDirectory / "MountUpdateMode_2.wgt";
1147   std::string pkgid = "smokeapp29";
1148   std::string appid = "smokeapp29.UpdateMode";
1149   ASSERT_EQ(MountInstall(path_old, PackageType::WGT),
1150             ci::AppInstaller::Result::OK);
1151   AddDataFiles(pkgid, kTestUserId);
1152   ASSERT_EQ(MountInstall(path_new, PackageType::WGT),
1153             ci::AppInstaller::Result::OK);
1154   ScopedTzipInterface interface(pkgid);
1155   ValidatePackage(pkgid, {appid});
1156
1157   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
1158   ValidateDataFiles(pkgid, kTestUserId);
1159 }
1160
1161 TEST_F(SmokeTest, MountInstallationMode_Rollback) {
1162   bf::path path =
1163       kSmokePackagesDirectory / "MountInstallationMode_Rollback.wgt";
1164   std::string pkgid = "smokeapp33";
1165   std::string appid = "smokeapp33.web";
1166   ASSERT_EQ(MountInstall(path, PackageType::WGT, RequestResult::FAIL),
1167             ci::AppInstaller::Result::ERROR);
1168   ScopedTzipInterface interface(pkgid);
1169   CheckPackageNonExistance(pkgid, {appid});
1170 }
1171
1172 TEST_F(SmokeTest, MountUpdateMode_Rollback) {
1173   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode_Rollback.wgt";
1174   bf::path path_new =
1175       kSmokePackagesDirectory / "MountUpdateMode_Rollback_2.wgt";
1176   std::string pkgid = "smokeapp34";
1177   std::string appid = "smokeapp34.web";
1178   ASSERT_EQ(MountInstall(path_old, PackageType::WGT),
1179             ci::AppInstaller::Result::OK);
1180   AddDataFiles(pkgid, kTestUserId);
1181   ASSERT_EQ(MountInstall(path_new, PackageType::WGT,
1182       RequestResult::FAIL), ci::AppInstaller::Result::ERROR);
1183   ScopedTzipInterface interface(pkgid);
1184   ValidatePackage(pkgid, {appid});
1185
1186   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
1187   ValidateDataFiles(pkgid, kTestUserId);
1188 }
1189
1190 TEST_F(SmokeTest, UserDefinedPlugins) {
1191   bf::path path = kSmokePackagesDirectory / "SimpleEchoPrivilege.wgt";
1192   std::string pkgid = "0CSPVhKmRk";
1193   std::string appid = "0CSPVhKmRk.SimpleEcho";
1194   std::string call_privilege = "http://tizen.org/privilege/call";
1195   std::string location_privilege = "http://tizen.org/privilege/location";
1196   std::string power_privilege = "http://tizen.org/privilege/power";
1197
1198   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
1199   ValidatePackage(pkgid, {appid});
1200   std::vector<std::string> res;
1201   ASSERT_TRUE(ci::QueryPrivilegesForPkgId(pkgid, kTestUserId, &res));
1202   ASSERT_TRUE(std::find(res.begin(), res.end(), call_privilege) != res.end());
1203   ASSERT_TRUE(std::find(res.begin(), res.end(), location_privilege)
1204           != res.end());
1205   ASSERT_TRUE(std::find(res.begin(), res.end(), power_privilege) != res.end());
1206 }
1207
1208 TEST_F(SmokeTest, InstallExternalMode) {
1209   ASSERT_TRUE(CheckAvailableExternalPath());
1210   bf::path path = kSmokePackagesDirectory / "InstallExternalMode.wgt";
1211   std::string pkgid = "smokeapp35";
1212   std::string appid = "smokeapp35.web";
1213   ASSERT_EQ(InstallExternal(path, PackageType::WGT),
1214       ci::AppInstaller::Result::OK);
1215   ValidateExternalPackage(pkgid, {appid});
1216 }
1217
1218 TEST_F(SmokeTest, MigrateLegacyExternalImageMode) {
1219   ASSERT_TRUE(CheckAvailableExternalPath());
1220   bf::path path =
1221       kSmokePackagesDirectory / "MigrateLegacyExternalImageMode.wgt";
1222   std::string pkgid = "smokeapp36";
1223   std::string appid = "smokeapp36.web";
1224   bf::path legacy_path = kSmokePackagesDirectory / kLegacyExtImageDir;
1225   ASSERT_EQ(MigrateLegacyExternalImage(pkgid, path, legacy_path,
1226       PackageType::WGT), ci::AppInstaller::Result::OK);
1227   ValidateExternalPackage(pkgid, {appid});
1228 }
1229
1230 TEST_F(SmokeTest, InstallationMode_Preload) {
1231   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
1232   bf::path path = kSmokePackagesDirectory / "InstallationMode_Preload.wgt";
1233   std::string pkgid = "smokeapp37";
1234   std::string appid = "smokeapp37.InstallationModePreload";
1235   ASSERT_EQ(InstallPreload(path, PackageType::WGT),
1236             ci::AppInstaller::Result::OK);
1237   ValidatePackage(pkgid, {appid}, true);
1238 }
1239
1240 TEST_F(SmokeTest, UpdateMode_Preload) {
1241   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
1242   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Preload.wgt";
1243   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Preload2.wgt";
1244   std::string pkgid = "smokeapp38";
1245   std::string appid = "smokeapp38.UpdateModePreload";
1246   ASSERT_EQ(InstallPreload(path_old, PackageType::WGT),
1247             ci::AppInstaller::Result::OK);
1248   AddDataFiles(pkgid, kTestUserId);
1249   ASSERT_EQ(InstallPreload(path_new, PackageType::WGT),
1250             ci::AppInstaller::Result::OK);
1251   ValidatePackage(pkgid, {appid}, true);
1252
1253   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2",
1254                                            true));
1255   ValidateDataFiles(pkgid, kTestUserId);
1256 }
1257
1258 TEST_F(SmokeTest, DeinstallationMode_Preload) {
1259   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
1260   bf::path path = kSmokePackagesDirectory / "DeinstallationMode_Preload.wgt";
1261   std::string pkgid = "smokeapp39";
1262   std::string appid = "smokeapp39.DeinstallationModePreload";
1263   ASSERT_EQ(InstallPreload(path, PackageType::WGT),
1264             ci::AppInstaller::Result::OK);
1265   ASSERT_EQ(UninstallPreload(pkgid, PackageType::WGT),
1266             ci::AppInstaller::Result::OK);
1267   CheckPackageReadonlyNonExistance(pkgid, {appid});
1268 }
1269
1270 }  // namespace common_installer
1271
1272 int main(int argc,  char** argv) {
1273   testing::InitGoogleTest(&argc, argv);
1274   testing::AddGlobalTestEnvironment(
1275       new common_installer::SmokeEnvironment(kGlobalUserUid));
1276   return RUN_ALL_TESTS();
1277 }