Release version 0.15.33
[platform/core/appfw/wgt-backend.git] / test / smoke_tests / extensive_smoke_test.cc
1 // Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include <common/utils/subprocess.h>
6
7 #include <common/utils/file_util.h>
8 #include <smoke_tests/common/smoke_utils.h>
9
10 #include <gtest/gtest.h>
11 #include <gtest/gtest-death-test.h>
12
13 #include "smoke_tests/wgt_smoke_utils.h"
14
15 namespace ci = common_installer;
16
17 namespace smoke_test {
18
19 class SmokeEnvironment : public testing::Environment {
20  public:
21   explicit SmokeEnvironment(ci::RequestMode mode) {\
22     request_mode_ = mode;
23   }
24   void SetUp() override {
25     if (request_mode_ == ci::RequestMode::USER)
26       ASSERT_TRUE(AddTestUser(&test_user));
27     backups_ = SetupBackupDirectories(test_user.uid);
28     for (auto& path : backups_)
29       ASSERT_TRUE(BackupPath(path));
30     CreateDatabase();
31   }
32   void TearDown() override {
33     ASSERT_TRUE(request_mode_ == ci::RequestMode::GLOBAL ||
34                 (request_mode_ == ci::RequestMode::USER &&
35                 kGlobalUserUid != test_user.uid));
36     WgtBackendInterface backend(std::to_string(test_user.uid));
37     UninstallAllSmokeApps(request_mode_, test_user.uid, &backend);
38     for (auto& path : backups_)
39       ASSERT_TRUE(RestorePath(path));
40     if (request_mode_ == ci::RequestMode::USER)
41       ASSERT_TRUE(DeleteTestUser());
42   }
43   User test_user;
44
45  private:
46   ci::RequestMode request_mode_;
47   std::vector<bf::path> backups_;
48 };
49
50 }  // namespace smoke_test
51
52 namespace {
53
54 smoke_test::SmokeEnvironment *env = nullptr;
55 void signalHandler(int signum) {
56   env->TearDown();
57   exit(signum);
58 }
59
60 }  // namespace
61
62 namespace smoke_test {
63
64 class SmokeTest : public testing::Test {
65  public:
66   SmokeTest() : backend(std::to_string(env->test_user.uid)),
67       params{PackageType::WGT, false} {
68     params.test_user.uid = env->test_user.uid;
69     params.test_user.gid = env->test_user.gid;
70   }
71  protected:
72   WgtBackendInterface backend;
73   TestParameters params;
74 };
75
76 class PreloadSmokeTest : public testing::Test {
77  public:
78   PreloadSmokeTest() : backend(std::to_string(env->test_user.uid)),
79       params{PackageType::WGT, true}  {
80     params.test_user.uid = env->test_user.uid;
81     params.test_user.gid = env->test_user.gid;
82   }
83  protected:
84   WgtBackendInterface backend;
85   TestParameters params;
86 };
87
88 class HybridSmokeTest : public testing::Test {
89  public:
90   HybridSmokeTest() : backend(std::to_string(env->test_user.uid)),
91       params{PackageType::HYBRID, false} {
92     params.test_user.uid = env->test_user.uid;
93     params.test_user.gid = env->test_user.gid;
94   }
95  protected:
96   HybridBackendInterface backend;
97   TestParameters params;
98 };
99
100 TEST_F(SmokeTest, RecoveryMode_ForInstallation) {
101   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForInstallation.wgt";
102   std::string pkgid = "smokewgt09";
103   std::string appid = "smokewgt09.RecoveryModeForInstallation";
104
105   std::string test_user_str = std::to_string(params.test_user.uid);
106   std::vector<std::string> args =
107       {"", "-i", path.string(), "-u", test_user_str.c_str()};
108   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
109     if (step >= 2) {
110       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
111                                                 params.test_user.uid);
112       EXTENDED_ASSERT_FALSE(recovery_file.empty());
113       std::unique_ptr<ci::recovery::RecoveryFile> recovery_info =
114           GetRecoverFileInfo(recovery_file);
115       EXTENDED_ASSERT_EQ(backend.RecoverWithSubprocess(recovery_file),
116           BackendInterface::SubProcessResult::SUCCESS);
117       if (recovery_info->cleanup()) {
118         EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
119         EXTENDED_ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
120             BackendInterface::SubProcessResult::SUCCESS);
121       } else {
122         EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
123       }
124     }
125     return true;
126   }, params.pkg_type);
127 }
128
129 TEST_F(SmokeTest, RecoveryMode_ForUpdate) {
130   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForUpdate.wgt";
131   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForUpdate_2.wgt";
132   std::string pkgid = "smokewgt10";
133   std::string appid = "smokewgt10.RecoveryModeForUpdate";
134   RemoveAllRecoveryFiles("/wgt-recovery", params.test_user.uid);
135   ASSERT_EQ(backend.InstallWithSubprocess(path_old),
136       BackendInterface::SubProcessResult::SUCCESS);
137   std::vector<bf::path> added_files;
138   AddDataFiles(pkgid, params.test_user.uid, &added_files);
139   FileInfoCollector expected_file_status_old(pkgid, params);
140   ASSERT_TRUE(expected_file_status_old.Init());
141   ASSERT_EQ(backend.InstallWithSubprocess(path_new),
142       BackendInterface::SubProcessResult::SUCCESS);
143   FileInfoCollector expected_file_status_new(pkgid, params);
144   ASSERT_TRUE(expected_file_status_new.Init());
145   ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
146       BackendInterface::SubProcessResult::SUCCESS);
147   ASSERT_EQ(backend.InstallWithSubprocess(path_old),
148       BackendInterface::SubProcessResult::SUCCESS);
149   AddDataFiles(pkgid, params.test_user.uid);
150
151   std::string test_user_str = std::to_string(params.test_user.uid);
152   std::vector<std::string> args =
153       {"", "-i", path_new.string(), "-u", test_user_str.c_str()};
154   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
155     if (step >= 2) {
156       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
157                                                 params.test_user.uid);
158       EXTENDED_ASSERT_FALSE(recovery_file.empty());
159       std::unique_ptr<ci::recovery::RecoveryFile> recovery_info =
160           GetRecoverFileInfo(recovery_file);
161       EXTENDED_ASSERT_EQ(backend.RecoverWithSubprocess(recovery_file),
162           BackendInterface::SubProcessResult::SUCCESS);
163       EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
164
165       std::string version = recovery_info->cleanup() ? "2\n" :"1\n";
166       EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
167           "res/wgt/VERSION", version, params));
168       EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
169       FileInfoCollector new_file_status(pkgid, params);
170       EXTENDED_ASSERT_TRUE(new_file_status.Init());
171
172       if (recovery_info->cleanup()) {
173         EXTENDED_ASSERT_TRUE(
174             expected_file_status_new.IsEqual(new_file_status, &added_files));
175         EXTENDED_ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
176             BackendInterface::SubProcessResult::SUCCESS);
177         EXTENDED_ASSERT_EQ(backend.InstallWithSubprocess(path_old),
178             BackendInterface::SubProcessResult::SUCCESS);
179         AddDataFiles(pkgid, params.test_user.uid);
180       } else {
181         EXTENDED_ASSERT_TRUE(
182             expected_file_status_old.IsEqual(new_file_status, &added_files));
183       }
184     }
185     return true;
186   }, params.pkg_type);
187 }
188
189 TEST_F(SmokeTest, RecoveryMode_ForDelta) {
190   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForDelta.wgt";
191   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForDelta.delta";
192   std::string pkgid = "smokewgt30";
193   std::string appid = "smokewgt30.RecoveryModeForDelta";
194   RemoveAllRecoveryFiles("/wgt-recovery", params.test_user.uid);
195   ASSERT_EQ(BackendInterface::SubProcessResult::SUCCESS,
196       backend.InstallWithSubprocess(path_old));
197   std::vector<bf::path> added_files;
198   AddDataFiles(pkgid, params.test_user.uid, &added_files);
199   FileInfoCollector expected_file_status_old(pkgid, params);
200   ASSERT_TRUE(expected_file_status_old.Init());
201   ASSERT_EQ(backend.InstallWithSubprocess(path_new),
202       BackendInterface::SubProcessResult::SUCCESS);
203   FileInfoCollector expected_file_status_new(pkgid, params);
204   ASSERT_TRUE(expected_file_status_new.Init());
205   ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
206       BackendInterface::SubProcessResult::SUCCESS);
207   ASSERT_EQ(backend.InstallWithSubprocess(path_old),
208       BackendInterface::SubProcessResult::SUCCESS);
209   AddDataFiles(pkgid, params.test_user.uid);
210   std::string test_user_str = std::to_string(params.test_user.uid);
211   std::vector<std::string> args =
212       {"", "-i", path_new.string(), "-u", test_user_str.c_str()};
213   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
214     if (step >= 2) {
215       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
216                                                 params.test_user.uid);
217       EXTENDED_ASSERT_FALSE(recovery_file.empty());
218       std::unique_ptr<ci::recovery::RecoveryFile> recovery_info =
219           GetRecoverFileInfo(recovery_file);
220       EXTENDED_ASSERT_EQ(backend.RecoverWithSubprocess(recovery_file),
221           BackendInterface::SubProcessResult::SUCCESS);
222       EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
223       std::string contents = recovery_info->cleanup() ? "2\n" : "1\n";
224
225       EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
226           "res/wgt/VERSION", contents, params));
227       EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
228       FileInfoCollector new_file_status(pkgid, params);
229       EXTENDED_ASSERT_TRUE(new_file_status.Init());
230
231       if (recovery_info->cleanup()) {
232         EXTENDED_ASSERT_TRUE(
233             expected_file_status_new.IsEqual(new_file_status, &added_files));
234         EXTENDED_ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
235             BackendInterface::SubProcessResult::SUCCESS);
236         EXTENDED_ASSERT_EQ(backend.InstallWithSubprocess(path_old),
237             BackendInterface::SubProcessResult::SUCCESS);
238         AddDataFiles(pkgid, params.test_user.uid);
239       } else {
240         EXTENDED_ASSERT_TRUE(
241             expected_file_status_old.IsEqual(new_file_status, &added_files));
242       }
243     }
244     return true;
245   }, params.pkg_type);
246 }
247
248 TEST_F(SmokeTest, RecoveryMode_ForMountInstall) {
249   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForMountInstall.wgt";
250   std::string pkgid = "smokewgt31";
251   std::string appid = "smokewgt31.RecoveryModeForMountInstall";
252   RemoveAllRecoveryFiles("/wgt-recovery", params.test_user.uid);
253   std::string test_user_str = std::to_string(params.test_user.uid);
254   std::vector<std::string> args =
255       {"", "-w", path.string(), "-u", test_user_str.c_str()};
256   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
257     if (step >= 2) {
258       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
259                                                 params.test_user.uid);
260       EXTENDED_ASSERT_FALSE(recovery_file.empty());
261       std::unique_ptr<ci::recovery::RecoveryFile> recovery_info =
262           GetRecoverFileInfo(recovery_file);
263       EXTENDED_ASSERT_EQ(backend.RecoverWithSubprocess(recovery_file),
264           BackendInterface::SubProcessResult::SUCCESS);
265       ScopedTzipInterface interface(pkgid, params.test_user.uid);
266       if (recovery_info->cleanup()) {
267         EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
268         EXTENDED_ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
269             BackendInterface::SubProcessResult::SUCCESS);
270       } else {
271         EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
272       }
273     }
274     return true;
275   }, params.pkg_type);
276 }
277
278 TEST_F(SmokeTest, RecoveryMode_ForMountUpdate) {
279   bf::path path_old =
280       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate.wgt";
281   bf::path path_new =
282       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate_2.wgt";
283   std::string pkgid = "smokewgt32";
284   std::string appid = "smokewgt32.RecoveryModeForMountUpdate";
285   RemoveAllRecoveryFiles("/wgt-recovery", params.test_user.uid);
286   ASSERT_EQ(backend.MountInstallWithSubprocess(path_old),
287       BackendInterface::SubProcessResult::SUCCESS);
288   std::vector<bf::path> added_files;
289   AddDataFiles(pkgid, params.test_user.uid, &added_files);
290   FileInfoCollector expected_file_status_old(pkgid, params);
291   ASSERT_TRUE(expected_file_status_old.Init());
292   ASSERT_EQ(backend.MountInstallWithSubprocess(path_new),
293       BackendInterface::SubProcessResult::SUCCESS);
294   FileInfoCollector expected_file_status_new(pkgid, params);
295   ASSERT_TRUE(expected_file_status_new.Init());
296   ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
297       BackendInterface::SubProcessResult::SUCCESS);
298   ASSERT_EQ(backend.MountInstallWithSubprocess(path_old),
299       BackendInterface::SubProcessResult::SUCCESS);
300   AddDataFiles(pkgid, params.test_user.uid);
301   std::string test_user_str = std::to_string(params.test_user.uid);
302   std::vector<std::string> args =
303       {"", "-w", path_new.string(), "-u", test_user_str.c_str()};
304   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
305     if (step >= 2) {
306       // Filesystem may be mounted after crash
307       ScopedTzipInterface poweroff_unmount_interface(pkgid,
308                                                      params.test_user.uid);
309       poweroff_unmount_interface.Release();
310
311       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
312                                                 params.test_user.uid);
313       EXTENDED_ASSERT_FALSE(recovery_file.empty());
314       std::unique_ptr<ci::recovery::RecoveryFile> recovery_info =
315           GetRecoverFileInfo(recovery_file);
316       EXTENDED_ASSERT_EQ(backend.RecoverWithSubprocess(recovery_file),
317           BackendInterface::SubProcessResult::SUCCESS);
318
319       ScopedTzipInterface interface(pkgid, params.test_user.uid);
320       EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
321       std::string version = recovery_info->cleanup() ? "2\n" : "1\n";
322       EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(
323           pkgid, "res/wgt/VERSION", version, params));
324       EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
325       interface.Release();
326       FileInfoCollector new_file_status(pkgid, params);
327       EXTENDED_ASSERT_TRUE(new_file_status.Init());
328       if (recovery_info->cleanup()) {
329         EXTENDED_ASSERT_TRUE(
330             expected_file_status_new.IsEqual(new_file_status, &added_files));
331         EXTENDED_ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
332             BackendInterface::SubProcessResult::SUCCESS);
333         EXTENDED_ASSERT_EQ(backend.MountInstallWithSubprocess(path_old),
334             BackendInterface::SubProcessResult::SUCCESS);
335         AddDataFiles(pkgid, params.test_user.uid);
336       } else {
337         EXTENDED_ASSERT_TRUE(
338             expected_file_status_old.IsEqual(new_file_status, &added_files));
339       }
340     }
341     return true;
342   }, params.pkg_type);
343 }
344
345
346 TEST_F(SmokeTest, InstallationMode_Rollback) {
347   bf::path path = kSmokePackagesDirectory / "InstallationMode_Rollback.wgt";
348   std::string pkgid = "smokewgt06";
349   std::string appid = "smokewgt06.InstallationModeRollback";
350
351   std::string test_user_str = std::to_string(params.test_user.uid);
352   const char* argv[] = {"", "-i", path.c_str(), "-u", test_user_str.c_str()};
353   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
354     EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
355     return true;
356   });
357 }
358
359 TEST_F(SmokeTest, UpdateMode_Rollback) {
360   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Rollback.wgt";
361   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Rollback_2.wgt";
362   std::string pkgid = "smokewgt07";
363   std::string appid = "smokewgt07.UpdateModeRollback";
364   ASSERT_EQ(backend.InstallWithSubprocess(path_old),
365       BackendInterface::SubProcessResult::SUCCESS);
366   std::vector<bf::path> added_files;
367   AddDataFiles(pkgid, params.test_user.uid, &added_files);
368   FileInfoCollector expected_file_status(pkgid, params);
369   ASSERT_TRUE(expected_file_status.Init());
370   std::string test_user_str = std::to_string(params.test_user.uid);
371   const char* argv[] =
372       {"", "-i", path_new.c_str(), "-u", test_user_str.c_str()};
373   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
374     FileInfoCollector new_file_status(pkgid, params);
375     EXTENDED_ASSERT_TRUE(new_file_status.Init());
376     EXTENDED_ASSERT_TRUE(
377         expected_file_status.IsEqual(new_file_status, &added_files));
378     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
379
380     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
381         "res/wgt/VERSION", "1\n", params));
382     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
383     return true;
384   });
385 }
386
387 TEST_F(SmokeTest, DeltaMode_Rollback) {
388   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback.wgt";
389   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode_Rollback.delta";
390   std::string pkgid = "smokewgt01";
391   std::string appid = "smokewgt01.DeltaMode";
392   ASSERT_EQ(backend.InstallWithSubprocess(path),
393       BackendInterface::SubProcessResult::SUCCESS);
394   std::vector<bf::path> added_files;
395   AddDataFiles(pkgid, params.test_user.uid, &added_files);
396   FileInfoCollector expected_file_status(pkgid, params);
397   ASSERT_TRUE(expected_file_status.Init());
398   std::string test_user_str = std::to_string(params.test_user.uid);
399   const char* argv[] =
400       {"", "-i", delta_package.c_str(), "-u", test_user_str.c_str()};
401   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
402     FileInfoCollector new_file_status(pkgid, params);
403     EXTENDED_ASSERT_TRUE(new_file_status.Init());
404     EXTENDED_ASSERT_TRUE(
405         expected_file_status.IsEqual(new_file_status, &added_files));
406     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
407     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
408                                              "version 1\n", params));
409     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
410     EXTENDED_ASSERT_TRUE(bf::exists(GetPackageRoot(
411         pkgid, params.test_user.uid) / "res/wgt/DELETED"));
412     EXTENDED_ASSERT_FALSE(bf::exists(GetPackageRoot(
413         pkgid, params.test_user.uid) / "res/wgt/ADDED"));
414     return true;
415   });
416 }
417
418 TEST_F(HybridSmokeTest, InstallationMode_Rollback) {
419   bf::path path = kSmokePackagesDirectory /
420       "InstallationMode_Rollback_Hybrid.wgt";
421   std::string pkgid = "smokehyb07";
422   std::string appid1 = "smokehyb07.web";
423   std::string test_user_str = std::to_string(params.test_user.uid);
424   const char* argv[] = {"", "-i", path.c_str(), "-u", test_user_str.c_str()};
425   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
426      EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
427      return true;
428   });
429 }
430
431 TEST_F(HybridSmokeTest, UpdateMode_Rollback) {
432   bf::path path_old = kSmokePackagesDirectory /
433       "UpdateMode_Rollback_Hybrid.wgt";
434   bf::path path_new = kSmokePackagesDirectory /
435       "UpdateMode_Rollback_Hybrid_2.wgt";
436   std::string pkgid = "smokehyb08";
437   std::string appid1 = "smokehyb08.web";
438   ASSERT_EQ(backend.InstallWithSubprocess(path_old),
439       BackendInterface::SubProcessResult::SUCCESS);
440   std::vector<bf::path> added_files;
441   AddDataFiles(pkgid, params.test_user.uid, &added_files);
442   FileInfoCollector expected_file_status(pkgid, params);
443   ASSERT_TRUE(expected_file_status.Init());
444   std::string test_user_str = std::to_string(params.test_user.uid);
445   const char* argv[] =
446       {"", "-i", path_new.c_str(), "-u", test_user_str.c_str()};
447   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
448     FileInfoCollector new_file_status(pkgid, params);
449     EXTENDED_ASSERT_TRUE(new_file_status.Init());
450     EXTENDED_ASSERT_TRUE(
451         expected_file_status.IsEqual(new_file_status, &added_files));
452     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid1}, params));
453
454     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
455         "res/wgt/VERSION", "1\n", params));
456     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
457         "lib/VERSION", "1\n", params));
458     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
459     return true;
460   });
461 }
462
463 TEST_F(HybridSmokeTest, DeltaMode_Rollback_Hybrid) {
464   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback_Hybrid.wgt";
465   bf::path delta_package = kSmokePackagesDirectory /
466       "DeltaMode_Rollback_Hybrid.delta";
467   std::string pkgid = "smokehyb11";
468   std::string appid1 = "smokehyb11.web";
469   ASSERT_EQ(backend.InstallWithSubprocess(path),
470       BackendInterface::SubProcessResult::SUCCESS);
471   std::vector<bf::path> added_files;
472   AddDataFiles(pkgid, params.test_user.uid, &added_files);
473   FileInfoCollector expected_file_status(pkgid, params);
474   ASSERT_TRUE(expected_file_status.Init());
475   std::string test_user_str = std::to_string(params.test_user.uid);
476   const char* argv[] = {"", "-i", path.c_str(), "-u", test_user_str.c_str()};
477   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
478     FileInfoCollector new_file_status(pkgid, params);
479     EXTENDED_ASSERT_TRUE(new_file_status.Init());
480     EXTENDED_ASSERT_TRUE(
481         expected_file_status.IsEqual(new_file_status, &added_files));
482     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid1}, params));
483     // Check delta modifications
484     bf::path root_path = GetPackageRoot(pkgid, params.test_user.uid);
485     EXTENDED_ASSERT_TRUE(bf::exists(root_path / "res" / "wgt" / "DELETED"));
486     EXTENDED_ASSERT_FALSE(bf::exists(root_path / "res" / "wgt" / "ADDED"));
487     EXTENDED_ASSERT_TRUE(bf::exists(root_path / "lib" / "DELETED"));
488     EXTENDED_ASSERT_FALSE(bf::exists(root_path / "lib" / "ADDED"));
489     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
490                                              "version 1\n", params));
491     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/MODIFIED",
492                                              "version 1\n", params));
493     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
494     return true;
495   });
496 }
497
498 TEST_F(HybridSmokeTest, MountInstallationMode_Rollback) {
499   bf::path path = kSmokePackagesDirectory /
500       "MountInstallationMode_Rollback_Hybrid.wgt";
501   std::string pkgid = "smokehyb09";
502   std::string appid1 = "smokehyb09.web";
503   std::string test_user_str = std::to_string(params.test_user.uid);
504   const char* argv[] = {"", "-w", path.c_str(), "-u", test_user_str.c_str()};
505   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
506     ScopedTzipInterface interface(pkgid, params.test_user.uid);
507     EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
508     return true;
509   });
510 }
511
512 TEST_F(HybridSmokeTest, MountUpdateMode_Rollback) {
513   bf::path path_old = kSmokePackagesDirectory /
514       "MountUpdateMode_Rollback_Hybrid.wgt";
515   bf::path path_new = kSmokePackagesDirectory /
516       "MountUpdateMode_Rollback_Hybrid_2.wgt";
517   std::string pkgid = "smokehyb10";
518   std::string appid1 = "smokehyb10.web";
519   ASSERT_EQ(backend.MountInstallWithSubprocess(path_old),
520       BackendInterface::SubProcessResult::SUCCESS);
521   std::vector<bf::path> added_files;
522   AddDataFiles(pkgid, params.test_user.uid, &added_files);
523   FileInfoCollector expected_file_status(pkgid, params);
524   ASSERT_TRUE(expected_file_status.Init());
525   std::string test_user_str = std::to_string(params.test_user.uid);
526   const char* argv[] =
527       {"", "-w", path_new.c_str(), "-u", test_user_str.c_str()};
528   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
529     FileInfoCollector new_file_status(pkgid, params);
530     EXTENDED_ASSERT_TRUE(new_file_status.Init());
531     EXTENDED_ASSERT_TRUE(
532         expected_file_status.IsEqual(new_file_status, &added_files));
533     ScopedTzipInterface interface(pkgid, params.test_user.uid);
534     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid1}, params));
535
536     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
537         "res/wgt/VERSION", "1\n", params));
538     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
539         "lib/VERSION", "1\n", params));
540     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
541     return true;
542   });
543 }
544
545 TEST_F(SmokeTest, MountInstallationMode_Rollback) {
546   bf::path path =
547       kSmokePackagesDirectory / "MountInstallationMode_Rollback.wgt";
548   std::string pkgid = "smokewgt33";
549   std::string appid = "smokewgt33.web";
550   std::string test_user_str = std::to_string(params.test_user.uid);
551   const char* argv[] = {"", "-w", path.c_str(), "-u", test_user_str.c_str()};
552   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
553     ScopedTzipInterface interface(pkgid, params.test_user.uid);
554     EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
555     return true;
556   });
557 }
558
559 TEST_F(SmokeTest, MountUpdateMode_Rollback) {
560   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode_Rollback.wgt";
561   bf::path path_new =
562       kSmokePackagesDirectory / "MountUpdateMode_Rollback_2.wgt";
563   std::string pkgid = "smokewgt34";
564   std::string appid = "smokewgt34.web";
565   ASSERT_EQ(backend.MountInstallWithSubprocess(path_old),
566             BackendInterface::SubProcessResult::SUCCESS);
567   std::vector<bf::path> added_files;
568   AddDataFiles(pkgid, params.test_user.uid, &added_files);
569   FileInfoCollector expected_file_status(pkgid, params);
570   ASSERT_TRUE(expected_file_status.Init());
571   std::string test_user_str = std::to_string(params.test_user.uid);
572   const char* argv[] =
573       {"", "-w", path_new.c_str(), "-u", test_user_str.c_str()};
574   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
575     FileInfoCollector new_file_status(pkgid, params);
576     EXTENDED_ASSERT_TRUE(new_file_status.Init());
577     EXTENDED_ASSERT_TRUE(
578         expected_file_status.IsEqual(new_file_status, &added_files));
579     ScopedTzipInterface interface(pkgid, params.test_user.uid);
580     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
581
582     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
583         "res/wgt/VERSION", "1\n", params));
584     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
585     return true;
586   });
587 }
588
589 TEST_F(PreloadSmokeTest, RecoveryMode_ForReadonlyUpdateInstall) {
590   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
591   bf::path path_old = kSmokePackagesDirectory /
592       "RecoveryMode_ForReadonlyUpdateInstall.wgt";
593   bf::path path_new = kSmokePackagesDirectory /
594       "RecoveryMode_ForReadonlyUpdateInstall_2.wgt";
595   std::string pkgid = "smokewgt51";
596   std::string appid = "smokewgt51.RecoveryModeForReadonlyUpdateInstall";
597   RemoveAllRecoveryFiles("/wgt-recovery", params.test_user.uid);
598   ASSERT_EQ(backend.InstallPreloadWithSubprocess(path_old),
599       BackendInterface::SubProcessResult::SUCCESS);
600   std::vector<bf::path> added_files;
601   AddDataFiles(pkgid, params.test_user.uid, &added_files);
602   FileInfoCollector expected_file_status_old(pkgid, params);
603   ASSERT_TRUE(expected_file_status_old.Init());
604   ASSERT_EQ(backend.InstallWithSubprocess(path_new),
605       BackendInterface::SubProcessResult::SUCCESS);
606   params.is_readonly = false;
607   FileInfoCollector expected_file_status_new(pkgid, params);
608   ASSERT_TRUE(expected_file_status_new.Init());
609   params.is_readonly = true;
610   ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
611       BackendInterface::SubProcessResult::SUCCESS);
612   AddDataFiles(pkgid, params.test_user.uid);
613   std::string test_uid_str = std::to_string(params.test_user.uid);
614   std::vector<std::string> args =
615       {"", "-i", path_new.string(), "-u", test_uid_str.c_str()};
616   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
617     if (step >= 2) {
618       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
619                                                 params.test_user.uid);
620       EXTENDED_ASSERT_FALSE(recovery_file.empty());
621       std::unique_ptr<ci::recovery::RecoveryFile> recovery_info =
622           GetRecoverFileInfo(recovery_file);
623       EXTENDED_ASSERT_EQ(backend.RecoverWithSubprocess(recovery_file),
624           BackendInterface::SubProcessResult::SUCCESS);
625       if (recovery_info->cleanup())
626         params.is_readonly = false;
627       EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
628
629       std::string version = recovery_info->cleanup() ? "2\n" :"1\n";
630       EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
631           "res/wgt/VERSION", version, params));
632       EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
633       FileInfoCollector new_file_status(pkgid, params);
634       EXTENDED_ASSERT_TRUE(new_file_status.Init());
635
636       if (recovery_info->cleanup()) {
637         EXTENDED_ASSERT_TRUE(
638             expected_file_status_new.IsEqual(new_file_status, &added_files));
639         EXTENDED_ASSERT_EQ(backend.UninstallWithSubprocess(pkgid),
640             BackendInterface::SubProcessResult::SUCCESS);
641         AddDataFiles(pkgid, params.test_user.uid);
642       } else {
643         EXTENDED_ASSERT_TRUE(
644             expected_file_status_old.IsEqual(new_file_status, &added_files));
645       }
646     }
647     return true;
648   }, params.pkg_type);
649 }
650
651 }  // namespace smoke_test
652
653 int main(int argc,  char** argv) {
654   try {
655     ci::RequestMode request_mode = smoke_test::ParseRequestMode(argc, argv);
656     if (getuid() != 0 || request_mode != ci::RequestMode::GLOBAL) {
657       std::cout << "Skip tests for preload request" << std::endl;
658       ::testing::GTEST_FLAG(filter) = "SmokeTest.*";
659     }
660     testing::InitGoogleTest(&argc, argv);
661     ::env = static_cast<smoke_test::SmokeEnvironment*>(
662         testing::AddGlobalTestEnvironment(
663             new smoke_test::SmokeEnvironment(request_mode)));
664     signal(SIGINT, ::signalHandler);
665     signal(SIGSEGV, ::signalHandler);
666     return RUN_ALL_TESTS();
667   } catch (...) {
668     std::cout << "Exception occurred during testing";
669     return 1;
670   }
671 }