Add new smoke tests
[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   RemoveAllRecoveryFiles();
111   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
112   AddDataFiles(pkgid, kTestUserId);
113   std::vector<std::string> args =
114       {"", "-i", path_new.string(), "-u", kTestUserIdStr.c_str()};
115   CrashAfterEachStep(args, [=](int step) -> bool {
116     if (step >= 1) {
117       bf::path recovery_file = FindRecoveryFile();
118       EXTENDED_ASSERT_FALSE(recovery_file.empty());
119       EXTENDED_ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
120           ci::AppInstaller::Result::OK);
121       EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}));
122
123       EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
124           "res/wgt/VERSION", "1\n"));
125       EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, kTestUserId));
126     }
127     return true;
128   });
129 }
130
131 TEST_F(SmokeTest, RecoveryMode_ForMountInstall) {
132   bf::path path = kSmokePackagesDirectory / "RecoveryMode_ForMountInstall.wgt";
133   std::string pkgid = "smokewgt31";
134   std::string appid = "smokewgt31.RecoveryModeForMountInstall";
135   RemoveAllRecoveryFiles();
136   std::vector<std::string> args =
137       {"", "-w", path.string(), "-u", kTestUserIdStr.c_str()};
138   CrashAfterEachStep(args, [=](int step) -> bool {
139     if (step >= 1) {
140       bf::path recovery_file = FindRecoveryFile();
141       EXTENDED_ASSERT_FALSE(recovery_file.empty());
142       EXTENDED_ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
143           ci::AppInstaller::Result::OK);
144       EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, {appid}));
145     }
146     return true;
147   });
148 }
149
150 TEST_F(SmokeTest, RecoveryMode_ForMountUpdate) {
151   bf::path path_old =
152       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate.wgt";
153   bf::path path_new =
154       kSmokePackagesDirectory / "RecoveryMode_ForMountUpdate_2.wgt";
155   std::string pkgid = "smokewgt32";
156   std::string appid = "smokewgt32.RecoveryModeForMountUpdate";
157   RemoveAllRecoveryFiles();
158   ASSERT_EQ(MountInstall(path_old, PackageType::WGT),
159             ci::AppInstaller::Result::OK);
160   AddDataFiles(pkgid, kTestUserId);
161   std::vector<std::string> args =
162       {"", "-w", path_new.string(), "-u", kTestUserIdStr.c_str()};
163   CrashAfterEachStep(args, [=](int step) -> bool {
164     if (step >= 1) {
165       // Filesystem may be mounted after crash
166       ScopedTzipInterface poweroff_unmount_interface(pkgid);
167       poweroff_unmount_interface.Release();
168
169       bf::path recovery_file = FindRecoveryFile();
170       EXTENDED_ASSERT_FALSE(recovery_file.empty());
171       EXTENDED_ASSERT_EQ(Recover(recovery_file, PackageType::WGT),
172           ci::AppInstaller::Result::OK);
173
174       ScopedTzipInterface interface(pkgid);
175       EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}));
176       EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(
177           pkgid, "res/wgt/VERSION", "1\n"));
178       EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, kTestUserId));
179     }
180     return true;
181   });
182 }
183
184
185 TEST_F(SmokeTest, InstallationMode_Rollback) {
186   bf::path path = kSmokePackagesDirectory / "InstallationMode_Rollback.wgt";
187   std::string pkgid = "smokewgt06";
188   std::string appid = "smokewgt06.InstallationModeRollback";
189
190   const char* argv[] = {"", "-i", path.c_str(), "-u", kTestUserIdStr.c_str()};
191   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
192     EXTENDED_ASSERT_EQ(Install(path, PackageType::WGT, RequestResult::FAIL),
193               ci::AppInstaller::Result::ERROR);
194     EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, {appid}));
195     return true;
196   });
197 }
198
199 TEST_F(SmokeTest, UpdateMode_Rollback) {
200   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Rollback.wgt";
201   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Rollback_2.wgt";
202   std::string pkgid = "smokewgt07";
203   std::string appid = "smokewgt07.UpdateModeRollback";
204   ASSERT_EQ(Install(path_old, PackageType::WGT), ci::AppInstaller::Result::OK);
205   AddDataFiles(pkgid, kTestUserId);
206   const char* argv[] =
207       {"", "-i", path_new.c_str(), "-u", kTestUserIdStr.c_str()};
208   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
209     EXTENDED_ASSERT_EQ(Install(path_new, PackageType::WGT, RequestResult::FAIL),
210                       ci::AppInstaller::Result::ERROR);
211     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}));
212
213     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
214         "res/wgt/VERSION", "1\n"));
215     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, kTestUserId));
216     return true;
217   });
218 }
219
220 TEST_F(SmokeTest, DeltaMode_Rollback) {
221   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback.wgt";
222   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode_Rollback.delta";
223   std::string pkgid = "smokewgt01";
224   std::string appid = "smokewgt01.DeltaMode";
225   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
226   AddDataFiles(pkgid, kTestUserId);
227   const char* argv[] =
228       {"", "-i", delta_package.c_str(), "-u", kTestUserIdStr.c_str()};
229   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
230     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}));
231     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
232                                              "version 1\n"));
233     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, kTestUserId));
234     EXTENDED_ASSERT_TRUE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
235                            "res/wgt/DELETED"));
236     EXTENDED_ASSERT_FALSE(bf::exists(GetPackageRoot(pkgid, kTestUserId) /
237                             "res/wgt/ADDED"));
238     return true;
239   });
240 }
241
242 TEST_F(SmokeTest, InstallationMode_Rollback_Hybrid) {
243   bf::path path = kSmokePackagesDirectory /
244       "InstallationMode_Rollback_Hybrid.wgt";
245   std::string pkgid = "smokehyb07";
246   std::string appid1 = "smokehyb07.web";
247   const char* argv[] = {"", "-i", path.c_str(), "-u", kTestUserIdStr.c_str()};
248   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
249      EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, {appid1}));
250      return true;
251   }, PackageType::HYBRID);
252 }
253
254 TEST_F(SmokeTest, UpdateMode_Rollback_Hybrid) {
255   bf::path path_old = kSmokePackagesDirectory /
256       "UpdateMode_Rollback_Hybrid.wgt";
257   bf::path path_new = kSmokePackagesDirectory /
258       "UpdateMode_Rollback_Hybrid_2.wgt";
259   std::string pkgid = "smokehyb08";
260   std::string appid1 = "smokehyb08.web";
261   ASSERT_EQ(Install(path_old, PackageType::HYBRID),
262             ci::AppInstaller::Result::OK);
263   AddDataFiles(pkgid, kTestUserId);
264   const char* argv[] =
265       {"", "-i", path_new.c_str(), "-u", kTestUserIdStr.c_str()};
266   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
267     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid1}));
268
269     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
270         "res/wgt/VERSION", "1\n"));
271     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
272         "lib/VERSION", "1\n"));
273     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, kTestUserId));
274     return true;
275   }, PackageType::HYBRID);
276 }
277
278 TEST_F(SmokeTest, DeltaMode_Rollback_Hybrid) {
279   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback_Hybrid.wgt";
280   bf::path delta_package = kSmokePackagesDirectory /
281       "DeltaMode_Rollback_Hybrid.delta";
282   std::string pkgid = "smokehyb11";
283   std::string appid1 = "smokehyb11.web";
284   ASSERT_EQ(Install(path, PackageType::HYBRID), ci::AppInstaller::Result::OK);
285   AddDataFiles(pkgid, kTestUserId);
286   const char* argv[] = {"", "-i", path.c_str(), "-u", kTestUserIdStr.c_str()};
287   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
288     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid1}));
289     // Check delta modifications
290     bf::path root_path = GetPackageRoot(pkgid, kTestUserId);
291     EXTENDED_ASSERT_TRUE(bf::exists(root_path / "res" / "wgt" / "DELETED"));
292     EXTENDED_ASSERT_FALSE(bf::exists(root_path / "res" / "wgt" / "ADDED"));
293     EXTENDED_ASSERT_TRUE(bf::exists(root_path / "lib" / "DELETED"));
294     EXTENDED_ASSERT_FALSE(bf::exists(root_path / "lib" / "ADDED"));
295     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "res/wgt/MODIFIED",
296                                              "version 1\n"));
297     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid, "lib/MODIFIED",
298                                              "version 1\n"));
299     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, kTestUserId));
300     return true;
301   });
302 }
303
304 TEST_F(SmokeTest, MountInstallationMode_Rollback_Hybrid) {
305   bf::path path = kSmokePackagesDirectory /
306       "MountInstallationMode_Rollback_Hybrid.wgt";
307   std::string pkgid = "smokehyb09";
308   std::string appid1 = "smokehyb09.web";
309   const char* argv[] = {"", "-w", path.c_str(), "-u", kTestUserIdStr.c_str()};
310   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
311     ScopedTzipInterface interface(pkgid);
312     EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, {appid1}));
313     return true;
314   }, PackageType::HYBRID);
315 }
316
317 TEST_F(SmokeTest, MountUpdateMode_Rollback_Hybrid) {
318   bf::path path_old = kSmokePackagesDirectory /
319       "MountUpdateMode_Rollback_Hybrid.wgt";
320   bf::path path_new = kSmokePackagesDirectory /
321       "MountUpdateMode_Rollback_Hybrid_2.wgt";
322   std::string pkgid = "smokehyb10";
323   std::string appid1 = "smokehyb10.web";
324   ASSERT_EQ(MountInstall(path_old, PackageType::HYBRID),
325             ci::AppInstaller::Result::OK);
326   AddDataFiles(pkgid, kTestUserId);
327   const char* argv[] =
328       {"", "-w", path_new.c_str(), "-u", kTestUserIdStr.c_str()};
329   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
330     ScopedTzipInterface interface(pkgid);
331     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid1}));
332
333     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
334         "res/wgt/VERSION", "1\n"));
335     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
336         "lib/VERSION", "1\n"));
337     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, kTestUserId));
338     return true;
339   });
340 }
341
342 TEST_F(SmokeTest, MountInstallationMode_Rollback) {
343   bf::path path =
344       kSmokePackagesDirectory / "MountInstallationMode_Rollback.wgt";
345   std::string pkgid = "smokewgt33";
346   std::string appid = "smokewgt33.web";
347   const char* argv[] = {"", "-w", path.c_str(), "-u", kTestUserIdStr.c_str()};
348   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
349     ScopedTzipInterface interface(pkgid);
350     EXTENDED_ASSERT_TRUE(CheckPackageNonExistance(pkgid, {appid}));
351     return true;
352   });
353 }
354
355 TEST_F(SmokeTest, MountUpdateMode_Rollback) {
356   bf::path path_old = kSmokePackagesDirectory / "MountUpdateMode_Rollback.wgt";
357   bf::path path_new =
358       kSmokePackagesDirectory / "MountUpdateMode_Rollback_2.wgt";
359   std::string pkgid = "smokewgt34";
360   std::string appid = "smokewgt34.web";
361   ASSERT_EQ(MountInstall(path_old, PackageType::WGT),
362             ci::AppInstaller::Result::OK);
363   AddDataFiles(pkgid, kTestUserId);
364   const char* argv[] =
365       {"", "-w", path_new.c_str(), "-u", kTestUserIdStr.c_str()};
366   TestRollbackAfterEachStep(SIZEOFARRAY(argv), argv, [=]() -> bool {
367     EXTENDED_ASSERT_EQ(MountInstall(path_new, PackageType::WGT,
368         RequestResult::FAIL), ci::AppInstaller::Result::ERROR);
369     ScopedTzipInterface interface(pkgid);
370     EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, {appid}));
371
372     EXTENDED_ASSERT_TRUE(ValidateFileContentInPackage(pkgid,
373         "res/wgt/VERSION", "1\n"));
374     EXTENDED_ASSERT_TRUE(ValidateDataFiles(pkgid, kTestUserId));
375     return true;
376   });
377 }
378
379 }  // namespace common_installer
380
381 int main(int argc,  char** argv) {
382   ci::RequestMode request_mode = ParseRequestMode(argc, argv);
383   if (getuid() != 0 || request_mode != ci::RequestMode::GLOBAL) {
384     std::cout << "Skip tests for preload request" << std::endl;
385     ::testing::GTEST_FLAG(filter) = "SmokeTest.*";
386   }
387   testing::InitGoogleTest(&argc, argv);
388   env = testing::AddGlobalTestEnvironment(
389       new common_installer::SmokeEnvironment(request_mode));
390   signal(SIGINT, signalHandler);
391   signal(SIGSEGV, signalHandler);
392   return RUN_ALL_TESTS();
393 }