Fix WgtArchiveInfo
[platform/core/appfw/wgt-backend.git] / src / unit_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 <unit_tests/common/smoke_utils.h>
9
10 #include <gtest/gtest.h>
11 #include <gtest/gtest-death-test.h>
12
13 #include "unit_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   }
31   void TearDown() override {
32     ASSERT_TRUE(request_mode_ == ci::RequestMode::GLOBAL ||
33                 (request_mode_ == ci::RequestMode::USER &&
34                 kGlobalUserUid != test_user.uid));
35     WgtBackendInterface backend(std::to_string(test_user.uid));
36     UninstallAllSmokeApps(request_mode_, test_user.uid, &backend);
37     for (auto& path : backups_)
38       ASSERT_TRUE(RestorePath(path));
39     if (request_mode_ == ci::RequestMode::USER)
40       ASSERT_TRUE(DeleteTestUser());
41   }
42   User test_user;
43
44  private:
45   ci::RequestMode request_mode_;
46   std::vector<bf::path> backups_;
47 };
48
49 }  // namespace smoke_test
50
51 namespace {
52
53 smoke_test::SmokeEnvironment *env = nullptr;
54 void signalHandler(int signum) {
55   env->TearDown();
56   exit(signum);
57 }
58
59 }  // namespace
60
61 namespace smoke_test {
62
63 class SmokeTest : public testing::Test {
64  public:
65   SmokeTest() : backend(std::to_string(env->test_user.uid)),
66       params{PackageType::WGT, false} {
67     params.test_user.uid = env->test_user.uid;
68     params.test_user.gid = env->test_user.gid;
69   }
70  protected:
71   WgtBackendInterface backend;
72   TestParameters params;
73 };
74
75 class HybridSmokeTest : public testing::Test {
76  public:
77   HybridSmokeTest() : backend(std::to_string(env->test_user.uid)),
78       params{PackageType::HYBRID, false} {
79     params.test_user.uid = env->test_user.uid;
80     params.test_user.gid = env->test_user.gid;
81   }
82  protected:
83   HybridBackendInterface backend;
84   TestParameters params;
85 };
86
87 TEST_F(SmokeTest, RecoveryMode_ForInstallation) {
88   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForInstallation.wgt";
89   std::string pkgid = "smokewgt09";
90   std::string appid = "smokewgt09.RecoveryModeForInstallation";
91
92   std::string test_user_str = std::to_string(params.test_user.uid);
93   std::vector<std::string> args =
94       {"", "-i", path.string(), "-u", test_user_str.c_str()};
95   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
96     if (step >= 2) {
97       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
98                                                 params.test_user.uid);
99       EXTENDED_ASSERT_FALSE(recovery_file.empty());
100       EXTENDED_ASSERT_EQ(backend.Recover(recovery_file),
101           ci::AppInstaller::Result::OK);
102       EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
103     }
104     return true;
105   }, params.pkg_type);
106 }
107
108 TEST_F(SmokeTest, RecoveryMode_ForUpdate) {
109   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForUpdate.wgt";
110   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForUpdate_2.wgt";
111   RemoveAllRecoveryFiles("/wgt-recovery", params.test_user.uid);
112   ASSERT_EQ(backend.Install(path_old), ci::AppInstaller::Result::OK);
113   std::string pkgid = "smokewgt10";
114   std::string appid = "smokewgt10.RecoveryModeForUpdate";
115   AddDataFiles(pkgid, params.test_user.uid);
116
117   std::string test_user_str = std::to_string(params.test_user.uid);
118   std::vector<std::string> args =
119       {"", "-i", path_new.string(), "-u", test_user_str.c_str()};
120   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
121     if (step >= 2) {
122       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
123                                                 params.test_user.uid);
124       EXTENDED_ASSERT_FALSE(recovery_file.empty());
125       EXTENDED_ASSERT_EQ(backend.Recover(recovery_file),
126           ci::AppInstaller::Result::OK);
127       EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
128
129       EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
130           "res/wgt/VERSION", "1\n", params));
131       EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
132     }
133     return true;
134   }, params.pkg_type);
135 }
136
137 TEST_F(SmokeTest, RecoveryMode_ForDelta) {
138   bf::path path_old = kSmokePackagesDirectory / "RecoveryMode_ForDelta.wgt";
139   bf::path path_new = kSmokePackagesDirectory / "RecoveryMode_ForDelta.delta";
140   std::string pkgid = "smokewgt30";
141   std::string appid = "smokewgt30.RecoveryModeForDelta";
142   RemoveAllRecoveryFiles("/wgt-recovery", params.test_user.uid);
143   ASSERT_EQ(backend.Install(path_old), ci::AppInstaller::Result::OK);
144   AddDataFiles(pkgid, params.test_user.uid);
145   std::string test_user_str = std::to_string(params.test_user.uid);
146   std::vector<std::string> args =
147       {"", "-i", path_new.string(), "-u", test_user_str.c_str()};
148   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
149     if (step >= 2) {
150       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
151                                                 params.test_user.uid);
152       EXTENDED_ASSERT_FALSE(recovery_file.empty());
153       EXTENDED_ASSERT_EQ(backend.Recover(recovery_file),
154           ci::AppInstaller::Result::OK);
155       EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
156
157       EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
158           "res/wgt/VERSION", "1\n", params));
159       EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
160     }
161     return true;
162   }, params.pkg_type);
163 }
164
165 TEST_F(SmokeTest, RecoveryMode_ForMountInstall) {
166   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForMountInstall.wgt";
167   std::string pkgid = "smokewgt31";
168   std::string appid = "smokewgt31.RecoveryModeForMountInstall";
169   RemoveAllRecoveryFiles("/wgt-recovery", params.test_user.uid);
170   std::string test_user_str = std::to_string(params.test_user.uid);
171   std::vector<std::string> args =
172       {"", "-w", path.string(), "-u", test_user_str.c_str()};
173   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
174     if (step >= 2) {
175       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
176                                                 params.test_user.uid);
177       EXTENDED_ASSERT_FALSE(recovery_file.empty());
178       EXTENDED_ASSERT_EQ(backend.Recover(recovery_file),
179           ci::AppInstaller::Result::OK);
180       EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
181     }
182     return true;
183   }, params.pkg_type);
184 }
185
186 TEST_F(SmokeTest, RecoveryMode_ForMountUpdate) {
187   bf::path path_old =
188       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate.wgt";
189   bf::path path_new =
190       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate_2.wgt";
191   std::string pkgid = "smokewgt32";
192   std::string appid = "smokewgt32.RecoveryModeForMountUpdate";
193   RemoveAllRecoveryFiles("/wgt-recovery", params.test_user.uid);
194   ASSERT_EQ(backend.MountInstall(path_old), ci::AppInstaller::Result::OK);
195   AddDataFiles(pkgid, params.test_user.uid);
196   std::string test_user_str = std::to_string(params.test_user.uid);
197   std::vector<std::string> args =
198       {"", "-w", path_new.string(), "-u", test_user_str.c_str()};
199   backend.CrashAfterEachStep(&args, [&](int step) -> bool {
200     if (step >= 2) {
201       // Filesystem may be mounted after crash
202       ScopedTzipInterface poweroff_unmount_interface(pkgid,
203                                                      params.test_user.uid);
204       poweroff_unmount_interface.Release();
205
206       bf::path recovery_file = FindRecoveryFile("/wgt-recovery",
207                                                 params.test_user.uid);
208       EXTENDED_ASSERT_FALSE(recovery_file.empty());
209       EXTENDED_ASSERT_EQ(backend.Recover(recovery_file),
210           ci::AppInstaller::Result::OK);
211
212       ScopedTzipInterface interface(pkgid, params.test_user.uid);
213       EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
214       EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(
215           pkgid, "res/wgt/VERSION", "1\n", params));
216       EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
217     }
218     return true;
219   }, params.pkg_type);
220 }
221
222
223 TEST_F(SmokeTest, InstallationMode_Rollback) {
224   bf::path path = kSmokePackagesDirectory / "InstallationMode_Rollback.wgt";
225   std::string pkgid = "smokewgt06";
226   std::string appid = "smokewgt06.InstallationModeRollback";
227
228   std::string test_user_str = std::to_string(params.test_user.uid);
229   const char* argv[] = {"", "-i", path.c_str(), "-u", test_user_str.c_str()};
230   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
231     EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
232     return true;
233   });
234 }
235
236 TEST_F(SmokeTest, UpdateMode_Rollback) {
237   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Rollback.wgt";
238   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Rollback_2.wgt";
239   std::string pkgid = "smokewgt07";
240   std::string appid = "smokewgt07.UpdateModeRollback";
241   ASSERT_EQ(backend.Install(path_old), ci::AppInstaller::Result::OK);
242   AddDataFiles(pkgid, params.test_user.uid);
243   std::string test_user_str = std::to_string(params.test_user.uid);
244   const char* argv[] =
245       {"", "-i", path_new.c_str(), "-u", test_user_str.c_str()};
246   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
247     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
248
249     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
250         "res/wgt/VERSION", "1\n", params));
251     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
252     return true;
253   });
254 }
255
256 TEST_F(SmokeTest, DeltaMode_Rollback) {
257   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback.wgt";
258   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode_Rollback.delta";
259   std::string pkgid = "smokewgt01";
260   std::string appid = "smokewgt01.DeltaMode";
261   ASSERT_EQ(backend.Install(path), ci::AppInstaller::Result::OK);
262   AddDataFiles(pkgid, params.test_user.uid);
263   std::string test_user_str = std::to_string(params.test_user.uid);
264   const char* argv[] =
265       {"", "-i", delta_package.c_str(), "-u", test_user_str.c_str()};
266   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
267     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
268     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
269                                              "version 1\n", params));
270     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
271     EXTENDED_ASSERT_TRUE(bf::exists(GetPackageRoot(
272         pkgid, params.test_user.uid) / "res/wgt/DELETED"));
273     EXTENDED_ASSERT_FALSE(bf::exists(GetPackageRoot(
274         pkgid, params.test_user.uid) / "res/wgt/ADDED"));
275     return true;
276   });
277 }
278
279 TEST_F(HybridSmokeTest, InstallationMode_Rollback) {
280   bf::path path = kSmokePackagesDirectory /
281       "InstallationMode_Rollback_Hybrid.wgt";
282   std::string pkgid = "smokehyb07";
283   std::string appid1 = "smokehyb07.web";
284   std::string test_user_str = std::to_string(params.test_user.uid);
285   const char* argv[] = {"", "-i", path.c_str(), "-u", test_user_str.c_str()};
286   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
287      EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
288      return true;
289   });
290 }
291
292 TEST_F(HybridSmokeTest, UpdateMode_Rollback) {
293   bf::path path_old = kSmokePackagesDirectory /
294       "UpdateMode_Rollback_Hybrid.wgt";
295   bf::path path_new = kSmokePackagesDirectory /
296       "UpdateMode_Rollback_Hybrid_2.wgt";
297   std::string pkgid = "smokehyb08";
298   std::string appid1 = "smokehyb08.web";
299   ASSERT_EQ(backend.Install(path_old), ci::AppInstaller::Result::OK);
300   AddDataFiles(pkgid, params.test_user.uid);
301   std::string test_user_str = std::to_string(params.test_user.uid);
302   const char* argv[] =
303       {"", "-i", path_new.c_str(), "-u", test_user_str.c_str()};
304   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
305     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid1}, params));
306
307     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
308         "res/wgt/VERSION", "1\n", params));
309     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
310         "lib/VERSION", "1\n", params));
311     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
312     return true;
313   });
314 }
315
316 TEST_F(HybridSmokeTest, DeltaMode_Rollback_Hybrid) {
317   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback_Hybrid.wgt";
318   bf::path delta_package = kSmokePackagesDirectory /
319       "DeltaMode_Rollback_Hybrid.delta";
320   std::string pkgid = "smokehyb11";
321   std::string appid1 = "smokehyb11.web";
322   ASSERT_EQ(backend.Install(path), ci::AppInstaller::Result::OK);
323   AddDataFiles(pkgid, params.test_user.uid);
324   std::string test_user_str = std::to_string(params.test_user.uid);
325   const char* argv[] = {"", "-i", path.c_str(), "-u", test_user_str.c_str()};
326   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
327     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid1}, params));
328     // Check delta modifications
329     bf::path root_path = GetPackageRoot(pkgid, params.test_user.uid);
330     EXTENDED_ASSERT_TRUE(bf::exists(root_path / "res" / "wgt" / "DELETED"));
331     EXTENDED_ASSERT_FALSE(bf::exists(root_path / "res" / "wgt" / "ADDED"));
332     EXTENDED_ASSERT_TRUE(bf::exists(root_path / "lib" / "DELETED"));
333     EXTENDED_ASSERT_FALSE(bf::exists(root_path / "lib" / "ADDED"));
334     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
335                                              "version 1\n", params));
336     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/MODIFIED",
337                                              "version 1\n", params));
338     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
339     return true;
340   });
341 }
342
343 TEST_F(HybridSmokeTest, MountInstallationMode_Rollback) {
344   bf::path path = kSmokePackagesDirectory /
345       "MountInstallationMode_Rollback_Hybrid.wgt";
346   std::string pkgid = "smokehyb09";
347   std::string appid1 = "smokehyb09.web";
348   std::string test_user_str = std::to_string(params.test_user.uid);
349   const char* argv[] = {"", "-w", path.c_str(), "-u", test_user_str.c_str()};
350   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
351     ScopedTzipInterface interface(pkgid, params.test_user.uid);
352     EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
353     return true;
354   });
355 }
356
357 TEST_F(HybridSmokeTest, MountUpdateMode_Rollback) {
358   bf::path path_old = kSmokePackagesDirectory /
359       "MountUpdateMode_Rollback_Hybrid.wgt";
360   bf::path path_new = kSmokePackagesDirectory /
361       "MountUpdateMode_Rollback_Hybrid_2.wgt";
362   std::string pkgid = "smokehyb10";
363   std::string appid1 = "smokehyb10.web";
364   ASSERT_EQ(backend.MountInstall(path_old), ci::AppInstaller::Result::OK);
365   AddDataFiles(pkgid, params.test_user.uid);
366   std::string test_user_str = std::to_string(params.test_user.uid);
367   const char* argv[] =
368       {"", "-w", path_new.c_str(), "-u", test_user_str.c_str()};
369   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
370     ScopedTzipInterface interface(pkgid, params.test_user.uid);
371     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid1}, params));
372
373     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
374         "res/wgt/VERSION", "1\n", params));
375     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
376         "lib/VERSION", "1\n", params));
377     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
378     return true;
379   });
380 }
381
382 TEST_F(SmokeTest, MountInstallationMode_Rollback) {
383   bf::path path =
384       kSmokePackagesDirectory / "MountInstallationMode_Rollback.wgt";
385   std::string pkgid = "smokewgt33";
386   std::string appid = "smokewgt33.web";
387   std::string test_user_str = std::to_string(params.test_user.uid);
388   const char* argv[] = {"", "-w", path.c_str(), "-u", test_user_str.c_str()};
389   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
390     ScopedTzipInterface interface(pkgid, params.test_user.uid);
391     EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, params));
392     return true;
393   });
394 }
395
396 TEST_F(SmokeTest, MountUpdateMode_Rollback) {
397   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode_Rollback.wgt";
398   bf::path path_new =
399       kSmokePackagesDirectory / "MountUpdateMode_Rollback_2.wgt";
400   std::string pkgid = "smokewgt34";
401   std::string appid = "smokewgt34.web";
402   ASSERT_EQ(backend.MountInstall(path_old),
403             ci::AppInstaller::Result::OK);
404   AddDataFiles(pkgid, params.test_user.uid);
405   std::string test_user_str = std::to_string(params.test_user.uid);
406   const char* argv[] =
407       {"", "-w", path_new.c_str(), "-u", test_user_str.c_str()};
408   backend.TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [&]() -> bool {
409     ScopedTzipInterface interface(pkgid, params.test_user.uid);
410     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}, params));
411
412     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
413         "res/wgt/VERSION", "1\n", params));
414     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, params.test_user.uid));
415     return true;
416   });
417 }
418
419 }  // namespace smoke_test
420
421 int main(int argc,  char** argv) {
422   try {
423     ci::RequestMode request_mode = smoke_test::ParseRequestMode(argc, argv);
424     if (getuid() != 0 || request_mode != ci::RequestMode::GLOBAL) {
425       std::cout << "Skip tests for preload request" << std::endl;
426       ::testing::GTEST_FLAG(filter) = "SmokeTest.*";
427     }
428     testing::InitGoogleTest(&argc, argv);
429     ::env = static_cast<smoke_test::SmokeEnvironment*>(
430         testing::AddGlobalTestEnvironment(
431             new smoke_test::SmokeEnvironment(request_mode)));
432     signal(SIGINT, ::signalHandler);
433     signal(SIGSEGV, ::signalHandler);
434     return RUN_ALL_TESTS();
435   } catch (...) {
436     std::cout << "Exception occurred during testing";
437     return 1;
438   }
439 }