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