49905e2e4a083affc876d8f625a84778d3e54ff5
[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 #include <common/paths.h>
10 #include <common/pkgmgr_interface.h>
11 #include <common/pkgmgr_query.h>
12 #include <common/request.h>
13 #include <common/step/configuration/step_fail.h>
14 #include <common/tzip_interface.h>
15 #include <common/utils/subprocess.h>
16 #include <gtest/gtest.h>
17 #include <gtest/gtest-death-test.h>
18 #include <pkgmgr-info.h>
19 #include <signal.h>
20 #include <unistd.h>
21 #include <tzplatform_config.h>
22
23 #include <array>
24 #include <cstdio>
25 #include <cstdlib>
26 #include <vector>
27
28 #include "hybrid/hybrid_installer.h"
29 #include "wgt/wgt_app_query_interface.h"
30 #include "wgt/wgt_installer.h"
31
32 #define SIZEOFARRAY(ARR)                                                       \
33   sizeof(ARR) / sizeof(ARR[0])                                                 \
34
35 namespace bf = boost::filesystem;
36 namespace bs = boost::system;
37 namespace ci = common_installer;
38
39 namespace {
40
41 const uid_t kTestUserId = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
42 const std::string kTestUserIdStr =
43     std::to_string(kTestUserId);
44
45 const bf::path kSmokePackagesDirectory =
46     "/usr/share/wgt-backend-ut/test_samples/smoke/";
47
48 const char kApplicationDir[] = ".applications";
49 const char kApplicationDirBackup[] = ".applications.bck";
50 const char KUserAppsDir[] = "apps_rw";
51 const char KUserAppsDirBackup[] = "apps_rw.bck";
52 const char kUserDataBaseDir[] = "/opt/dbspace/user";
53
54 enum class RequestResult {
55   NORMAL,
56   FAIL
57 };
58
59 class ScopedTzipInterface {
60  public:
61   explicit ScopedTzipInterface(const std::string& pkgid)
62       : pkg_path_(bf::path(ci::GetRootAppPath(false,
63             kTestUserId)) / pkgid),
64         interface_(ci::GetMountLocation(pkg_path_)),
65         mounted_(true) {
66     interface_.MountZip(ci::GetZipPackageLocation(pkg_path_, pkgid));
67   }
68
69   void Release() {
70     if (mounted_) {
71       interface_.UnmountZip();
72       mounted_ = false;
73     }
74   }
75
76   ~ScopedTzipInterface() {
77     Release();
78   }
79
80  private:
81   bf::path pkg_path_;
82   ci::TzipInterface interface_;
83   bool mounted_;
84 };
85
86 class TestPkgmgrInstaller : public ci::PkgmgrInstallerInterface {
87  public:
88   bool CreatePkgMgrInstaller(pkgmgr_installer** installer,
89                              ci::InstallationMode* mode) {
90     *installer = pkgmgr_installer_offline_new();
91     if (!*installer)
92       return false;
93     *mode = ci::InstallationMode::ONLINE;
94     return true;
95   }
96
97   bool ShouldCreateSignal() const {
98     return false;
99   }
100 };
101
102 enum class PackageType {
103   WGT,
104   HYBRID
105 };
106
107 bool TouchFile(const bf::path& path) {
108   FILE* f = fopen(path.c_str(), "w+");
109   if (!f)
110     return false;
111   fclose(f);
112   return true;
113 }
114
115 void RemoveAllRecoveryFiles() {
116   bf::path root_path = ci::GetRootAppPath(false,
117       kTestUserId);
118   if (!bf::exists(root_path))
119     return;
120   for (auto& dir_entry : boost::make_iterator_range(
121          bf::directory_iterator(root_path), bf::directory_iterator())) {
122     if (bf::is_regular_file(dir_entry)) {
123       if (dir_entry.path().string().find("/recovery") != std::string::npos) {
124         bs::error_code error;
125         bf::remove(dir_entry.path(), error);
126       }
127     }
128   }
129 }
130
131 bf::path FindRecoveryFile() {
132   bf::path root_path = ci::GetRootAppPath(false,
133       kTestUserId);
134   for (auto& dir_entry : boost::make_iterator_range(
135          bf::directory_iterator(root_path), bf::directory_iterator())) {
136     if (bf::is_regular_file(dir_entry)) {
137       if (dir_entry.path().string().find("/recovery") != std::string::npos) {
138         return dir_entry.path();
139       }
140     }
141   }
142   return {};
143 }
144
145 bool ValidateFileContentInPackage(const std::string& pkgid,
146                                   const std::string& relative,
147                                   const std::string& expected) {
148   bf::path root_path = ci::GetRootAppPath(false,
149       kTestUserId);
150   bf::path file_path = root_path / pkgid / relative;
151   if (!bf::exists(file_path)) {
152     LOG(ERROR) << file_path << " doesn't exist";
153     return false;
154   }
155   FILE* handle = fopen(file_path.c_str(), "r");
156   if (!handle) {
157     LOG(ERROR) << file_path << " cannot  be open";
158     return false;
159   }
160   std::string content;
161   std::array<char, 200> buffer;
162   while (fgets(buffer.data(), buffer.size(), handle)) {
163     content += buffer.data();
164   }
165   fclose(handle);
166   return content == expected;
167 }
168
169 void ValidatePackageFS(const std::string& pkgid,
170                        const std::vector<std::string>& appids) {
171   bf::path root_path = ci::GetRootAppPath(false,
172       kTestUserId);
173   bf::path package_path = root_path / pkgid;
174   bf::path data_path = package_path / "data";
175   bf::path shared_path = package_path / "shared";
176   bf::path cache_path = package_path / "cache";
177   ASSERT_TRUE(bf::exists(root_path));
178   ASSERT_TRUE(bf::exists(package_path));
179   ASSERT_TRUE(bf::exists(data_path));
180   ASSERT_TRUE(bf::exists(shared_path));
181   ASSERT_TRUE(bf::exists(cache_path));
182
183   bf::path manifest_path =
184       bf::path(getUserManifestPath(
185           kTestUserId, false)) / (pkgid + ".xml");
186   ASSERT_TRUE(bf::exists(manifest_path));
187
188   for (auto& appid : appids) {
189     bf::path binary_path = package_path / "bin" / appid;
190     ASSERT_TRUE(bf::exists(binary_path));
191   }
192
193   bf::path widget_root_path = package_path / "res" / "wgt";
194   bf::path config_path = widget_root_path / "config.xml";
195   ASSERT_TRUE(bf::exists(widget_root_path));
196   ASSERT_TRUE(bf::exists(config_path));
197
198   bf::path private_tmp_path = package_path / "tmp";
199   ASSERT_TRUE(bf::exists(private_tmp_path));
200
201   // backups should not exist
202   bf::path package_backup = ci::GetBackupPathForPackagePath(package_path);
203   bf::path manifest_backup = ci::GetBackupPathForManifestFile(manifest_path);
204   ASSERT_FALSE(bf::exists(package_backup));
205   ASSERT_FALSE(bf::exists(manifest_backup));
206 }
207
208 void PackageCheckCleanup(const std::string& pkgid,
209                          const std::vector<std::string>&) {
210   bf::path root_path = ci::GetRootAppPath(
211       false, kTestUserId);
212   bf::path package_path = root_path / pkgid;
213   ASSERT_FALSE(bf::exists(package_path));
214
215   bf::path manifest_path =
216       bf::path(getUserManifestPath(
217           kTestUserId, false)) / (pkgid + ".xml");
218   ASSERT_FALSE(bf::exists(manifest_path));
219
220   // backups should not exist
221   bf::path package_backup = ci::GetBackupPathForPackagePath(package_path);
222   bf::path manifest_backup = ci::GetBackupPathForManifestFile(manifest_path);
223   ASSERT_FALSE(bf::exists(package_backup));
224   ASSERT_FALSE(bf::exists(manifest_backup));
225 }
226
227 void ValidatePackage(const std::string& pkgid,
228                      const std::vector<std::string>& appids) {
229   ASSERT_TRUE(ci::QueryIsPackageInstalled(
230       pkgid, ci::GetRequestMode(kTestUserId),
231       kTestUserId));
232   ValidatePackageFS(pkgid, appids);
233 }
234
235 void CheckPackageNonExistance(const std::string& pkgid,
236                               const std::vector<std::string>& appids) {
237   ASSERT_FALSE(ci::QueryIsPackageInstalled(
238       pkgid, ci::GetRequestMode(kTestUserId),
239       kTestUserId));
240   PackageCheckCleanup(pkgid, appids);
241 }
242
243 std::unique_ptr<ci::AppQueryInterface> CreateQueryInterface() {
244   std::unique_ptr<ci::AppQueryInterface> query_interface(
245       new wgt::WgtAppQueryInterface());
246   return query_interface;
247 }
248
249 std::unique_ptr<ci::AppInstaller> CreateInstaller(ci::PkgMgrPtr pkgmgr,
250                                                   PackageType type) {
251   switch (type) {
252     case PackageType::WGT:
253       return std::unique_ptr<ci::AppInstaller>(new wgt::WgtInstaller(pkgmgr));
254     case PackageType::HYBRID:
255       return std::unique_ptr<ci::AppInstaller>(
256           new hybrid::HybridInstaller(pkgmgr));
257     default:
258       LOG(ERROR) << "Unknown installer type";
259       return nullptr;
260   }
261 }
262
263 ci::AppInstaller::Result RunInstallerWithPkgrmgr(ci::PkgMgrPtr pkgmgr,
264                                                  PackageType type,
265                                                  RequestResult mode) {
266   std::unique_ptr<ci::AppInstaller> installer = CreateInstaller(pkgmgr, type);
267   switch (mode) {
268   case RequestResult::FAIL:
269     installer->AddStep<ci::configuration::StepFail>();
270     break;
271   default:
272     break;
273   }
274   return installer->Run();
275 }
276 ci::AppInstaller::Result CallBackend(int argc,
277                                      const char* argv[],
278                                      PackageType type,
279                                      RequestResult mode = RequestResult::NORMAL
280                                      ) {
281   TestPkgmgrInstaller pkgmgr_installer;
282   std::unique_ptr<ci::AppQueryInterface> query_interface =
283       CreateQueryInterface();
284   auto pkgmgr =
285       ci::PkgMgrInterface::Create(argc, const_cast<char**>(argv),
286                                   &pkgmgr_installer, query_interface.get());
287   if (!pkgmgr) {
288     LOG(ERROR) << "Failed to initialize pkgmgr interface";
289     return ci::AppInstaller::Result::UNKNOWN;
290   }
291   return RunInstallerWithPkgrmgr(pkgmgr, type, mode);
292 }
293
294 ci::AppInstaller::Result Install(const bf::path& path,
295                                  PackageType type,
296                                  RequestResult mode = RequestResult::NORMAL) {
297   const char* argv[] = {"", "-i", path.c_str(), "-u", kTestUserIdStr.c_str()};
298   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
299 }
300
301 ci::AppInstaller::Result Update(const bf::path& path_old,
302                                 const bf::path& path_new,
303                                 PackageType type,
304                                 RequestResult mode = RequestResult::NORMAL) {
305   if (Install(path_old, type) != ci::AppInstaller::Result::OK) {
306     LOG(ERROR) << "Failed to install application. Cannot update";
307     return ci::AppInstaller::Result::UNKNOWN;
308   }
309   return Install(path_new, type, mode);
310 }
311
312 ci::AppInstaller::Result MountInstall(const bf::path& path,
313     PackageType type, RequestResult mode = RequestResult::NORMAL) {
314   const char* argv[] = {"", "-w", path.c_str(), "-u", kTestUserIdStr.c_str()};
315   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
316 }
317
318 ci::AppInstaller::Result MountUpdate(const bf::path& path_old,
319     const bf::path& path_new, PackageType type,
320     RequestResult mode = RequestResult::NORMAL) {
321   if (MountInstall(path_old, type) != ci::AppInstaller::Result::OK) {
322     LOG(ERROR) << "Failed to mount-install application. Cannot mount-update";
323     return ci::AppInstaller::Result::UNKNOWN;
324   }
325   return MountInstall(path_new, type, mode);
326 }
327
328 ci::AppInstaller::Result Uninstall(const std::string& pkgid,
329                                    PackageType type,
330                                    RequestResult mode = RequestResult::NORMAL) {
331   const char* argv[] = {"", "-d", pkgid.c_str(), "-u", kTestUserIdStr.c_str()};
332   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
333 }
334
335 ci::AppInstaller::Result Reinstall(const bf::path& path,
336                                    const bf::path& delta_dir,
337                                    PackageType type,
338                                    RequestResult mode = RequestResult::NORMAL) {
339   if (Install(path, type) != ci::AppInstaller::Result::OK) {
340     LOG(ERROR) << "Failed to install application. Cannot perform RDS";
341     return ci::AppInstaller::Result::UNKNOWN;
342   }
343   const char* argv[] = {"", "-r", delta_dir.c_str(), "-u",
344                         kTestUserIdStr.c_str()};
345   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
346 }
347
348 ci::AppInstaller::Result DeltaInstall(const bf::path& path,
349     const bf::path& delta_package, PackageType type) {
350   if (Install(path, type) != ci::AppInstaller::Result::OK) {
351     LOG(ERROR) << "Failed to install application. Cannot perform delta update";
352     return ci::AppInstaller::Result::UNKNOWN;
353   }
354   return Install(delta_package, type);
355 }
356
357 ci::AppInstaller::Result Clear(const std::string& pkgid,
358                                    PackageType type,
359                                    RequestResult mode = RequestResult::NORMAL) {
360   const char* argv[] = {"", "-c", pkgid.c_str(), "-u", kTestUserIdStr.c_str()};
361   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
362 }
363
364 ci::AppInstaller::Result EnablePackage(const std::string& pkgid,
365                                   PackageType type,
366                                   RequestResult mode = RequestResult::NORMAL) {
367   const char* argv[] = {"", "-A", pkgid.c_str(), "-u", kTestUserIdStr.c_str()};
368   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
369 }
370
371 ci::AppInstaller::Result DisablePackage(const std::string& pkgid,
372                                   PackageType type,
373                                   RequestResult mode = RequestResult::NORMAL) {
374   const char* argv[] = {"", "-D", pkgid.c_str(), "-u", kTestUserIdStr.c_str()};
375   return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
376 }
377
378 ci::AppInstaller::Result Recover(const bf::path& recovery_file,
379                                  PackageType type,
380                                  RequestResult mode = RequestResult::NORMAL) {
381   const char* argv[] = {"", "-b", recovery_file.c_str(), "-u",
382                         kTestUserIdStr.c_str()};
383   TestPkgmgrInstaller pkgmgr_installer;
384   std::unique_ptr<ci::AppQueryInterface> query_interface =
385       CreateQueryInterface();
386   auto pkgmgr =
387       ci::PkgMgrInterface::Create(SIZEOFARRAY(argv), const_cast<char**>(argv),
388                                   &pkgmgr_installer, query_interface.get());
389   if (!pkgmgr) {
390     LOG(ERROR) << "Failed to initialize pkgmgr interface";
391     return ci::AppInstaller::Result::UNKNOWN;
392   }
393   return RunInstallerWithPkgrmgr(pkgmgr, type, mode);
394 }
395
396 }  // namespace
397
398 namespace common_installer {
399
400 class SmokeEnvironment : public testing::Environment {
401  public:
402   explicit SmokeEnvironment(const bf::path& home) : home_(home) {
403   }
404   void SetUp() override {
405     bf::path UserDBDir = bf::path(kUserDataBaseDir) / kTestUserIdStr;
406     bf::path UserDBDirBackup = UserDBDir.string() + std::string(".bck");
407
408     bs::error_code error;
409     bf::remove_all(home_ / kApplicationDirBackup, error);
410     bf::remove_all(home_ / KUserAppsDirBackup, error);
411     bf::remove_all(UserDBDirBackup, error);
412     if (bf::exists(home_ / KUserAppsDir)) {
413       bf::rename(home_ / KUserAppsDir, home_ / KUserAppsDirBackup, error);
414       if (error)
415         LOG(ERROR) << "Failed to setup test environment. Does some previous"
416                    << " test crashed? Directory: "
417                    << (home_ / KUserAppsDirBackup) << " should not exist.";
418       assert(!error);
419     }
420     if (bf::exists(home_ / kApplicationDir)) {
421       bf::rename(home_ / kApplicationDir, home_ / kApplicationDirBackup, error);
422       if (error)
423         LOG(ERROR) << "Failed to setup test environment. Does some previous"
424                    << " test crashed? Directory: "
425                    << (home_ / kApplicationDirBackup) << " should not exist.";
426       assert(!error);
427     }
428     if (bf::exists(UserDBDir)) {
429       bf::rename(UserDBDir, UserDBDirBackup, error);
430       if (error)
431         LOG(ERROR) << "Failed to setup test environment. Does some previous"
432                    << " test crashed? Directory: "
433                    << UserDBDirBackup << " should not exist.";
434       assert(!error);
435     }
436   }
437   void TearDown() override {
438     bf::path UserDBDir = bf::path(kUserDataBaseDir) / kTestUserIdStr;
439     bf::path UserDBDirBackup = UserDBDir.string() + std::string(".bck");
440
441     bs::error_code error;
442     bf::remove_all(home_ / kApplicationDir, error);
443     bf::remove_all(home_ / KUserAppsDir, error);
444     bf::remove_all(UserDBDir, error);
445     if (bf::exists(home_ / KUserAppsDirBackup))
446       bf::rename(home_ / KUserAppsDirBackup, home_ / KUserAppsDir, error);
447     if (bf::exists(home_ / kApplicationDirBackup))
448       bf::rename(home_ / kApplicationDirBackup, home_ / kApplicationDir, error);
449     if (bf::exists(UserDBDirBackup))
450       bf::rename(UserDBDirBackup, UserDBDir, error);
451   }
452
453  private:
454   bf::path home_;
455 };
456
457 class SmokeTest : public testing::Test {
458 };
459
460 TEST_F(SmokeTest, InstallationMode) {
461   bf::path path = kSmokePackagesDirectory / "InstallationMode.wgt";
462   std::string pkgid = "smokeapp03";
463   std::string appid = "smokeapp03.InstallationMode";
464   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
465   ValidatePackage(pkgid, {appid});
466 }
467
468 TEST_F(SmokeTest, UpdateMode) {
469   bf::path path_old = kSmokePackagesDirectory / "UpdateMode.wgt";
470   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_2.wgt";
471   std::string pkgid = "smokeapp04";
472   std::string appid = "smokeapp04.UpdateMode";
473   ASSERT_EQ(Update(path_old, path_new, PackageType::WGT),
474             ci::AppInstaller::Result::OK);
475   ValidatePackage(pkgid, {appid});
476
477   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
478 }
479
480 TEST_F(SmokeTest, DeinstallationMode) {
481   bf::path path = kSmokePackagesDirectory / "DeinstallationMode.wgt";
482   std::string pkgid = "smokeapp05";
483   std::string appid = "smokeapp05.DeinstallationMode";
484   ASSERT_EQ(Install(path, PackageType::WGT),
485             ci::AppInstaller::Result::OK);
486   ASSERT_EQ(Uninstall(pkgid, PackageType::WGT), ci::AppInstaller::Result::OK);
487   CheckPackageNonExistance(pkgid, {appid});
488 }
489
490 TEST_F(SmokeTest, RDSMode) {
491   bf::path path = kSmokePackagesDirectory / "RDSMode.wgt";
492   bf::path delta_directory = kSmokePackagesDirectory / "delta_dir/";
493   std::string pkgid = "smokeapp11";
494   std::string appid = "smokeapp11.RDSMode";
495   ASSERT_EQ(Reinstall(path, delta_directory, PackageType::WGT),
496             ci::AppInstaller::Result::OK);
497   ValidatePackage(pkgid, {appid});
498
499   // Check delta modifications
500   bf::path root_path = ci::GetRootAppPath(false,
501       kTestUserId);
502   ASSERT_FALSE(bf::exists(root_path / pkgid / "res" / "wgt" / "DELETED"));
503   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "ADDED"));
504   ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED", "2\n");
505 }
506
507 TEST_F(SmokeTest, EnablePkg) {
508   bf::path path = kSmokePackagesDirectory / "EnablePkg.wgt";
509   std::string pkgid = "smokeapp22";
510   ASSERT_EQ(Install(path, PackageType::WGT),
511             ci::AppInstaller::Result::OK);
512   ASSERT_EQ(DisablePackage(pkgid, PackageType::WGT),
513             ci::AppInstaller::Result::OK);
514   ASSERT_EQ(EnablePackage(pkgid, PackageType::WGT),
515             ci::AppInstaller::Result::OK);
516
517   ASSERT_TRUE(ci::QueryIsPackageInstalled(pkgid,
518       ci::GetRequestMode(kTestUserId),
519       kTestUserId));
520 }
521
522 TEST_F(SmokeTest, DisablePkg) {
523   bf::path path = kSmokePackagesDirectory / "DisablePkg.wgt";
524   std::string pkgid = "smokeapp21";
525   std::string appid = "smokeapp21.DisablePkg";
526   ASSERT_EQ(Install(path, PackageType::WGT),
527             ci::AppInstaller::Result::OK);
528   ASSERT_EQ(DisablePackage(pkgid, PackageType::WGT),
529             ci::AppInstaller::Result::OK);
530   ASSERT_FALSE(ci::QueryIsPackageInstalled(pkgid,
531       ci::GetRequestMode(kTestUserId),
532       kTestUserId));
533   ValidatePackageFS(pkgid, {appid});
534 }
535
536 TEST_F(SmokeTest, ClearMode) {
537   bf::path path = kSmokePackagesDirectory / "ClearMode.wgt";
538   std::string pkgid = "smokeapp20";
539   std::string appid = "smokeapp20.ClearMode";
540   ASSERT_EQ(Install(path, PackageType::WGT),
541             ci::AppInstaller::Result::OK);
542   bf::path root_path = ci::GetRootAppPath(false,
543       kTestUserId);
544   bs::error_code error;
545   bf::create_directory(root_path / pkgid / "data" / "dir", error);
546   ASSERT_FALSE(error);
547   ASSERT_TRUE(TouchFile(root_path / pkgid / "data" / "dir" / "file"));
548   ASSERT_TRUE(TouchFile(root_path / pkgid / "data" / "file"));
549   ASSERT_EQ(Clear(pkgid, PackageType::WGT), ci::AppInstaller::Result::OK);
550   ValidatePackage(pkgid, {appid});
551   ASSERT_FALSE(bf::exists(root_path / pkgid / "data" / "dir" / "file"));
552   ASSERT_FALSE(bf::exists(root_path / pkgid / "res" / "file"));
553 }
554
555 TEST_F(SmokeTest, DeltaMode) {
556   bf::path path = kSmokePackagesDirectory / "DeltaMode.wgt";
557   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode.delta";
558   std::string pkgid = "smokeapp17";
559   std::string appid = "smokeapp17.DeltaMode";
560   ASSERT_EQ(DeltaInstall(path, delta_package, PackageType::WGT),
561             ci::AppInstaller::Result::OK);
562   ValidatePackage(pkgid, {appid});
563
564   // Check delta modifications
565   bf::path root_path = ci::GetRootAppPath(false,
566       kTestUserId);
567   ASSERT_FALSE(bf::exists(root_path / pkgid / "res" / "wgt" / "DELETED"));
568   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "ADDED"));
569   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "css" / "style.css"));  // NOLINT
570   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "images" / "tizen_32.png"));  // NOLINT
571   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "js" / "main.js"));
572   ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED", "version 2\n");
573 }
574
575 TEST_F(SmokeTest, RecoveryMode_ForInstallation) {
576   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForInstallation.wgt";
577   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
578   backend_crash.Run("-i", path.string(), "-u", kTestUserIdStr.c_str());
579   ASSERT_NE(backend_crash.Wait(), 0);
580
581   std::string pkgid = "smokeapp09";
582   std::string appid = "smokeapp09.RecoveryModeForInstallation";
583   bf::path recovery_file = FindRecoveryFile();
584   ASSERT_FALSE(recovery_file.empty());
585   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
586       ci::AppInstaller::Result::OK);
587   CheckPackageNonExistance(pkgid, {appid});
588 }
589
590 TEST_F(SmokeTest, RecoveryMode_ForUpdate) {
591   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForUpdate.wgt";
592   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForUpdate_2.wgt";
593   RemoveAllRecoveryFiles();
594   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
595   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
596   backend_crash.Run("-i", path_new.string(), "-u", kTestUserIdStr.c_str());
597   ASSERT_NE(backend_crash.Wait(), 0);
598
599   std::string pkgid = "smokeapp10";
600   std::string appid = "smokeapp10.RecoveryModeForUpdate";
601   bf::path recovery_file = FindRecoveryFile();
602   ASSERT_FALSE(recovery_file.empty());
603   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
604             ci::AppInstaller::Result::OK);
605   ValidatePackage(pkgid, {appid});
606
607   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
608 }
609
610 TEST_F(SmokeTest, RecoveryMode_ForDelta) {
611   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForDelta.wgt";
612   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForDelta.delta";
613   RemoveAllRecoveryFiles();
614   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
615   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
616   backend_crash.Run("-i", path_new.string(), "-u", kTestUserIdStr.c_str());
617   ASSERT_NE(backend_crash.Wait(), 0);
618
619   std::string pkgid = "smokeapp30";
620   std::string appid = "smokeapp30.RecoveryModeForDelta";
621   bf::path recovery_file = FindRecoveryFile();
622   ASSERT_FALSE(recovery_file.empty());
623   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
624             ci::AppInstaller::Result::OK);
625   ValidatePackage(pkgid, {appid});
626
627   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
628 }
629
630 TEST_F(SmokeTest, RecoveryMode_ForMountInstall) {
631   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForMountInstall.wgt";
632   RemoveAllRecoveryFiles();
633   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
634   backend_crash.Run("-w", path.string(), "-u", kTestUserIdStr.c_str());
635   ASSERT_NE(backend_crash.Wait(), 0);
636
637   std::string pkgid = "smokeapp31";
638   std::string appid = "smokeapp31.RecoveryModeForMountInstall";
639   bf::path recovery_file = FindRecoveryFile();
640   ASSERT_FALSE(recovery_file.empty());
641   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
642             ci::AppInstaller::Result::OK);
643   CheckPackageNonExistance(pkgid, {appid});
644 }
645
646 TEST_F(SmokeTest, RecoveryMode_ForMountUpdate) {
647   bf::path path_old =
648       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate.wgt";
649   bf::path path_new =
650       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate_2.wgt";
651   std::string pkgid = "smokeapp32";
652   std::string appid = "smokeapp32.RecoveryModeForMountUpdate";
653   RemoveAllRecoveryFiles();
654   ASSERT_EQ(MountInstall(path_old, PackageType::WGT),
655             ci::AppInstaller::Result::OK);
656   Subprocess backend_crash("/usr/bin/wgt-backend-ut/smoke-test-helper");
657   backend_crash.Run("-w", path_new.string(), "-u", kTestUserIdStr.c_str());
658   ASSERT_NE(backend_crash.Wait(), 0);
659
660   // Filesystem may be mounted after crash
661   ScopedTzipInterface poweroff_unmount_interface(pkgid);
662   poweroff_unmount_interface.Release();
663
664   bf::path recovery_file = FindRecoveryFile();
665   ASSERT_FALSE(recovery_file.empty());
666   ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
667             ci::AppInstaller::Result::OK);
668
669   ScopedTzipInterface interface(pkgid);
670   ValidatePackage(pkgid, {appid});
671   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
672 }
673
674 TEST_F(SmokeTest, InstallationMode_GoodSignature) {
675   bf::path path = kSmokePackagesDirectory / "InstallationMode_GoodSignature.wgt";  // NOLINT
676   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
677 }
678
679 TEST_F(SmokeTest, InstallationMode_WrongSignature) {
680   bf::path path = kSmokePackagesDirectory / "InstallationMode_WrongSignature.wgt";  // NOLINT
681   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::ERROR);
682 }
683
684 TEST_F(SmokeTest, InstallationMode_Rollback) {
685   bf::path path = kSmokePackagesDirectory / "InstallationMode_Rollback.wgt";
686   std::string pkgid = "smokeapp06";
687   std::string appid = "smokeapp06.InstallationModeRollback";
688   ASSERT_EQ(Install(path, PackageType::WGT, RequestResult::FAIL),
689             ci::AppInstaller::Result::ERROR);
690   CheckPackageNonExistance(pkgid, {appid});
691 }
692
693 TEST_F(SmokeTest, UpdateMode_Rollback) {
694   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Rollback.wgt";
695   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Rollback_2.wgt";
696   std::string pkgid = "smokeapp07";
697   std::string appid = "smokeapp07.UpdateModeRollback";
698   ASSERT_EQ(Update(path_old, path_new, PackageType::WGT, RequestResult::FAIL),
699                    ci::AppInstaller::Result::ERROR);
700   ValidatePackage(pkgid, {appid});
701
702   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "1\n"));
703 }
704
705 TEST_F(SmokeTest, InstallationMode_Hybrid) {
706   bf::path path = kSmokePackagesDirectory / "InstallationMode_Hybrid.wgt";
707   std::string pkgid = "smokehyb01";
708   std::string appid1 = "smokehyb01.Web";
709   std::string appid2 = "smokehyb01.Native";
710   ASSERT_EQ(Install(path, PackageType::HYBRID), ci::AppInstaller::Result::OK);
711   ValidatePackage(pkgid, {appid1, appid2});
712 }
713
714 TEST_F(SmokeTest, UpdateMode_Hybrid) {
715   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Hybrid.wgt";
716   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Hybrid_2.wgt";
717   std::string pkgid = "smokehyb02";
718   std::string appid1 = "smokehyb02.Web";
719   std::string appid2 = "smokehyb02.Native";
720   ASSERT_EQ(Update(path_old, path_new, PackageType::HYBRID),
721             ci::AppInstaller::Result::OK);
722   ValidatePackage(pkgid, {appid1, appid2});
723
724   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
725   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "VERSION", "2\n"));
726 }
727
728 TEST_F(SmokeTest, DeinstallationMode_Hybrid) {
729   bf::path path = kSmokePackagesDirectory / "DeinstallationMode_Hybrid.wgt";
730   std::string pkgid = "smokehyb03";
731   std::string appid1 = "smokehyb03.Web";
732   std::string appid2 = "smokehyb03.Native";
733   ASSERT_EQ(Install(path, PackageType::HYBRID),
734             ci::AppInstaller::Result::OK);
735   ASSERT_EQ(Uninstall(pkgid, PackageType::WGT), ci::AppInstaller::Result::OK);
736   CheckPackageNonExistance(pkgid, {appid1, appid2});
737 }
738
739 TEST_F(SmokeTest, DeltaMode_Hybrid) {
740   bf::path path = kSmokePackagesDirectory / "DeltaMode_Hybrid.wgt";
741   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode_Hybrid.delta";
742   std::string pkgid = "smokehyb04";
743   std::string appid1 = "smokehyb04.Web";
744   std::string appid2 = "smokehyb04.Native";
745   ASSERT_EQ(DeltaInstall(path, delta_package, PackageType::HYBRID),
746             ci::AppInstaller::Result::OK);
747   ValidatePackage(pkgid, {appid1, appid2});
748
749   // Check delta modifications
750   bf::path root_path = ci::GetRootAppPath(false,
751       kTestUserId);
752   ASSERT_FALSE(bf::exists(root_path / pkgid / "res" / "wgt" / "DELETED"));
753   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "ADDED"));
754   ASSERT_FALSE(bf::exists(root_path / pkgid / "lib" / "DELETED"));
755   ASSERT_TRUE(bf::exists(root_path / pkgid / "lib" / "ADDED"));
756   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "css" / "style.css"));  // NOLINT
757   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "images" / "tizen_32.png"));  // NOLINT
758   ASSERT_TRUE(bf::exists(root_path / pkgid / "res" / "wgt" / "js" / "main.js"));
759   ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED", "version 2\n");
760   ValidateFileContentInPackage(pkgid, "lib/MODIFIED", "version 2\n");
761 }
762
763 TEST_F(SmokeTest, MountInstallationMode) {
764   bf::path path = kSmokePackagesDirectory / "MountInstallationMode.wgt";
765   std::string pkgid = "smokeapp28";
766   std::string appid = "smokeapp28.InstallationMode";
767   ASSERT_EQ(MountInstall(path, PackageType::WGT), ci::AppInstaller::Result::OK);
768   ScopedTzipInterface interface(pkgid);
769   ValidatePackage(pkgid, {appid});
770 }
771
772 TEST_F(SmokeTest, MountUpdateMode) {
773   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode.wgt";
774   bf::path path_new = kSmokePackagesDirectory / "MountUpdateMode_2.wgt";
775   std::string pkgid = "smokeapp29";
776   std::string appid = "smokeapp29.UpdateMode";
777   ASSERT_EQ(MountUpdate(path_old, path_new, PackageType::WGT),
778             ci::AppInstaller::Result::OK);
779   ScopedTzipInterface interface(pkgid);
780   ValidatePackage(pkgid, {appid});
781
782   ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/VERSION", "2\n"));
783 }
784
785 }  // namespace common_installer
786
787 int main(int argc,  char** argv) {
788   testing::InitGoogleTest(&argc, argv);
789   bf::path directory =
790         bf::path("/home") / tzplatform_getenv(TZ_SYS_DEFAULT_USER);
791   testing::AddGlobalTestEnvironment(
792       new common_installer::SmokeEnvironment(directory));
793   return RUN_ALL_TESTS();
794 }