Merge "Step Recover Per User Directories for Recovery Mode added" into tizen
[platform/core/appfw/wgt-backend.git] / src / unit_tests / smoke_test.cc
index d0c1fbd..0057a32 100644 (file)
@@ -551,15 +551,17 @@ ci::AppInstaller::Result MountInstall(const bf::path& path,
 
 ci::AppInstaller::Result Uninstall(const std::string& pkgid,
                                    PackageType type,
+                                   bool is_preload,
                                    RequestResult mode = RequestResult::NORMAL) {
-  const char* argv[] = {"", "-d", pkgid.c_str(), "-u", kTestUserIdStr.c_str()};
-  return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
-}
-
-ci::AppInstaller::Result UninstallPreload(const std::string& pkgid,
-    PackageType type, RequestResult mode = RequestResult::NORMAL) {
-  const char* argv[] = {"", "-d", pkgid.c_str(), "--preload", "--force-remove"};
-  return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
+  if (is_preload) {
+    const char* argv[] = {"", "-d", pkgid.c_str(), "--preload",
+        "--force-remove"};
+    return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
+  } else {
+    const char* argv[] = {"", "-d", pkgid.c_str(), "-u",
+        kTestUserIdStr.c_str()};
+    return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
+  }
 }
 
 ci::AppInstaller::Result RDSUpdate(const bf::path& path,
@@ -677,6 +679,32 @@ std::vector<bf::path> SetupBackupDirectories(uid_t uid) {
   return entries;
 }
 
+void UninstallAllAppsInDirectory(bf::path dir, bool is_preload) {
+  if(bf::exists(dir)) {
+    for (auto& dir_entry : boost::make_iterator_range(
+        bf::directory_iterator(dir), bf::directory_iterator())) {
+      if (dir_entry.path().string().find("smoke") != std::string::npos &&
+          bf::is_directory(dir_entry)) {
+        if(Uninstall(dir_entry.path().filename().string(), PackageType::WGT,
+            is_preload, RequestResult::NORMAL) !=
+            ci::AppInstaller::Result::OK) {
+          LOG(ERROR) << "Cannot uninstall smoke test app: "
+              << dir_entry.path().filename().string();
+        }
+      }
+    }
+  }
+}
+
+void UninstallAllSmokeApps(uid_t uid) {
+  if (getuid() == 0) {
+    bf::path root_path = kPreloadApps;
+    UninstallAllAppsInDirectory(root_path, true);
+  }
+  bf::path apps_rw = ci::GetRootAppPath(false, uid);
+  UninstallAllAppsInDirectory(apps_rw, false);
+}
+
 }  // namespace
 
 namespace common_installer {
@@ -691,7 +719,7 @@ class SmokeEnvironment : public testing::Environment {
       BackupPath(path);
   }
   void TearDown() override {
-    // TODO(s89.jang): Uninstall smoke packages to clear security context
+    UninstallAllSmokeApps(uid_);
     for (auto& path : backups_)
       RestorePath(path);
   }
@@ -704,6 +732,11 @@ class SmokeEnvironment : public testing::Environment {
 class SmokeTest : public testing::Test {
 };
 
+// TODO(b.kunikowski): New smoke app pkgid name convention.
+//    Change all smokeapp pkgids to smokewgt, because now there
+//    may be some smoke app pkgid identical as in tpk smoke tests, and this may
+//    cause bad results of some smoke tests.
+
 TEST_F(SmokeTest, InstallationMode) {
   bf::path path = kSmokePackagesDirectory / "InstallationMode.wgt";
   std::string pkgid = "smokeapp03";
@@ -732,7 +765,8 @@ TEST_F(SmokeTest, DeinstallationMode) {
   std::string appid = "smokeapp05.DeinstallationMode";
   ASSERT_EQ(Install(path, PackageType::WGT),
             ci::AppInstaller::Result::OK);
-  ASSERT_EQ(Uninstall(pkgid, PackageType::WGT), ci::AppInstaller::Result::OK);
+  ASSERT_EQ(Uninstall(pkgid, PackageType::WGT, false),
+            ci::AppInstaller::Result::OK);
   CheckPackageNonExistance(pkgid, {appid});
 }
 
@@ -950,8 +984,8 @@ TEST_F(SmokeTest, UpdateMode_Rollback) {
 TEST_F(SmokeTest, DeltaMode_Rollback) {
   bf::path path = kSmokePackagesDirectory / "DeltaMode_Rollback.wgt";
   bf::path delta_package = kSmokePackagesDirectory / "DeltaMode_Rollback.delta";
-  std::string pkgid = "smokeapp37";
-  std::string appid = "smokeapp37.DeltaMode";
+  std::string pkgid = "smokewgt01";
+  std::string appid = "smokewgt01.DeltaMode";
   ASSERT_EQ(Install(path, PackageType::WGT), ci::AppInstaller::Result::OK);
   AddDataFiles(pkgid, kTestUserId);
   ASSERT_EQ(Install(delta_package, PackageType::WGT, RequestResult::FAIL),
@@ -999,7 +1033,7 @@ TEST_F(SmokeTest, DeinstallationMode_Hybrid) {
   std::string appid1 = "smokehyb03.Web";
   ASSERT_EQ(Install(path, PackageType::HYBRID),
             ci::AppInstaller::Result::OK);
-  ASSERT_EQ(Uninstall(pkgid, PackageType::HYBRID),
+  ASSERT_EQ(Uninstall(pkgid, PackageType::HYBRID, false),
             ci::AppInstaller::Result::OK);
   CheckPackageNonExistance(pkgid, {appid1});
 }
@@ -1197,8 +1231,8 @@ TEST_F(SmokeTest, MountUpdateMode_Rollback) {
 
 TEST_F(SmokeTest, UserDefinedPlugins) {
   bf::path path = kSmokePackagesDirectory / "SimpleEchoPrivilege.wgt";
-  std::string pkgid = "0CSPVhKmRk";
-  std::string appid = "0CSPVhKmRk.SimpleEcho";
+  std::string pkgid = "smokewgt02";
+  std::string appid = "smokewgt02.SimpleEcho";
   std::string call_privilege = "http://tizen.org/privilege/call";
   std::string location_privilege = "http://tizen.org/privilege/location";
   std::string power_privilege = "http://tizen.org/privilege/power";
@@ -1270,7 +1304,7 @@ TEST_F(SmokeTest, DeinstallationMode_Preload) {
   std::string appid = "smokeapp39.DeinstallationModePreload";
   ASSERT_EQ(InstallPreload(path, PackageType::WGT),
             ci::AppInstaller::Result::OK);
-  ASSERT_EQ(UninstallPreload(pkgid, PackageType::WGT),
+  ASSERT_EQ(Uninstall(pkgid, PackageType::WGT, true),
             ci::AppInstaller::Result::OK);
   CheckPackageReadonlyNonExistance(pkgid, {appid});
 }