Revert "[SmokeTest] Add request mode choice" 80/119580/2
authorjongmyeong ko <jongmyeong.ko@samsung.com>
Fri, 17 Mar 2017 10:30:30 +0000 (03:30 -0700)
committerjongmyeong ko <jongmyeong.ko@samsung.com>
Fri, 17 Mar 2017 10:59:35 +0000 (03:59 -0700)
This reverts commit e2687ca7eb39fc8aa1ba278199a2b641e376f4ab.

Change-Id: I830fd406565a3d43adc0fba3f752ba6348671bcd

src/unit_tests/smoke_test.cc

index 0fb52ff..d52b00b 100644 (file)
@@ -6,7 +6,6 @@
 #include <boost/filesystem/path.hpp>
 #include <boost/range/iterator_range.hpp>
 #include <boost/system/error_code.hpp>
-#include <boost/format.hpp>
 
 #include <common/paths.h>
 #include <common/pkgmgr_interface.h>
@@ -26,7 +25,6 @@
 #include <tzplatform_config.h>
 #include <vconf.h>
 #include <vconf-internal-keys.h>
-#include <getopt.h>
 
 #include <array>
 #include <cstdio>
@@ -46,51 +44,15 @@ namespace bs = boost::system;
 namespace ci = common_installer;
 
 namespace {
-ci::RequestMode kRequestMode = ci::RequestMode::GLOBAL;
-const char *short_opts = "g:u";
-const struct option long_opts[] = {
-    { "request_mode", 1, NULL, 'r' },
-    { 0, 0, 0, 0 }  /* sentinel */
-};
-const char *request_modes[] {
-     "global",
-     "user"
-};
-void ParseRequestMode(int argc,  char** argv) {
-  int c;
-  int opt_idx = 0;
-  do {
-    c = getopt_long(argc, argv, short_opts, long_opts, &opt_idx);
-    switch (c) {
-    case 'r':
-      if (strncmp(optarg, request_modes[0], strlen(request_modes[0])) == 0)
-        kRequestMode = ci::RequestMode::GLOBAL;
-      else if (strncmp(optarg, request_modes[1], strlen(request_modes[1])) == 0)
-        kRequestMode = ci::RequestMode::USER;
-      else
-        LOG(ERROR) << "Wrong request mode " << optarg
-                   << ". Available: \"global\" or \"user\"";
-      break;
-    case 'g':
-      kRequestMode = ci::RequestMode::GLOBAL;
-      break;
-    case 'u':
-      kRequestMode = ci::RequestMode::USER;
-      break;
-    default:
-      break;
-    }
-  } while (c != -1);
-}
 
 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
 const uid_t kGlobalUserGid = tzplatform_getgid(TZ_SYS_GLOBALAPP_USER);
 const uid_t kDefaultUserUid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
-uid_t kTestUserId = kGlobalUserUid;
-gid_t kTestGroupId = kGlobalUserGid;
-std::string kTestUserIdStr = std::to_string(kTestUserId);
-const char kNormalUserName[] = "smokeuser";
+const uid_t kTestUserId = kGlobalUserUid;
+const gid_t kTestGroupId = kGlobalUserGid;
 const char kSystemShareGroupName[] = "system_share";
+const std::string& kTestUserIdStr =
+    std::to_string(kTestUserId);
 const std::string& kDefaultUserIdStr = std::to_string(kDefaultUserUid);
 const char kLegacyExtImageDir[] = "legacy_extimage_dir";
 const char kMigrateTestDBName[] = "app2sd_migrate.db";
@@ -191,40 +153,6 @@ bool TouchFile(const bf::path& path) {
   return true;
 }
 
-bool AddTestUser(const char *user_name) {
-  std::cout << "Adding test user: " << user_name << std::endl;
-  gboolean ret = ci::AddUser(user_name);
-  if (boost::optional<uid_t> uid = ci::GetUidByUserName(user_name)) {
-    kTestUserId = *uid;
-    kTestUserIdStr = std::to_string(kTestUserId);
-    std::cout << "User created properly: uid=" << *uid;
-    if (boost::optional<gid_t> gid = ci::GetGidByUid(*uid)) {
-      kTestGroupId = *gid;
-      std::cout << " gid=" << *gid;
-    }
-    std::cout << std::endl;
-    return true;
-  }
-  LOG(ERROR) << "Adding test user failed";
-  return false;
-}
-
-bool DeleteTestUser(const char *user_name) {
-  std::cout << "Deleting test user: " << user_name << std::endl;
-  uid_t test_uid;
-  if (boost::optional<uid_t> uid = ci::GetUidByUserName(user_name))
-    test_uid = *uid;
-  gboolean ret = ci::DeleteUser(user_name, true);
-  if (boost::optional<uid_t> uid = ci::GetUidByUserName(user_name));
-  else {
-    std::cout << "User deleted properly: user_name=" << user_name
-              << " uid=" << test_uid << std::endl;
-    return true;
-  }
-  LOG(ERROR) << "Deleting test user failed";
-  return false;
-}
-
 void RemoveAllRecoveryFiles() {
   bf::path root_path = ci::GetRootAppPath(false,
       kTestUserId);
@@ -741,11 +669,11 @@ void RestorePath(const bf::path& path) {
   }
 }
 
-std::vector<bf::path> SetupBackupDirectories() {
+std::vector<bf::path> SetupBackupDirectories(uid_t uid) {
   std::vector<bf::path> entries;
   bf::path db_dir = bf::path(tzplatform_getenv(TZ_SYS_DB));
-  if (kTestUserId != kGlobalUserUid)
-    db_dir = db_dir / "user" / std::to_string(kTestUserId);
+  if (uid != kGlobalUserUid)
+    db_dir = db_dir / "user" / std::to_string(uid);
   for (auto e : kDBEntries) {
     bf::path path = db_dir / e;
     entries.emplace_back(path);
@@ -757,7 +685,7 @@ std::vector<bf::path> SetupBackupDirectories() {
     entries.emplace_back(kPreloadIcons);
   }
 
-  if (kTestUserId == kGlobalUserUid) {
+  if (uid == kGlobalUserUid) {
     entries.emplace_back(kSkelDir);
     entries.emplace_back(kGlobalManifestDir);
     ci::UserList list = ci::GetUserList();
@@ -766,13 +694,13 @@ std::vector<bf::path> SetupBackupDirectories() {
       entries.emplace_back(apps);
     }
   } else {
-    tzplatform_set_user(kTestUserId);
+    tzplatform_set_user(uid);
     bf::path approot = tzplatform_getenv(TZ_USER_APPROOT);
     tzplatform_reset_user();
     entries.emplace_back(approot);
   }
 
-  bf::path apps_rw = ci::GetRootAppPath(false, kTestUserId);
+  bf::path apps_rw = ci::GetRootAppPath(false, uid);
   entries.emplace_back(apps_rw);
 
   return entries;
@@ -799,12 +727,12 @@ void UninstallAllAppsInDirectory(bf::path dir, bool is_preload) {
   }
 }
 
-void UninstallAllSmokeApps() {
-  if (getuid() == 0 && kRequestMode == ci::RequestMode::GLOBAL) {
+void UninstallAllSmokeApps(uid_t uid) {
+  if (getuid() == 0) {
     bf::path root_path = kPreloadApps;
     UninstallAllAppsInDirectory(root_path, true);
   }
-  bf::path apps_rw = ci::GetRootAppPath(false, kTestUserId);
+  bf::path apps_rw = ci::GetRootAppPath(false, uid);
   UninstallAllAppsInDirectory(apps_rw, false);
 }
 
@@ -814,44 +742,27 @@ namespace common_installer {
 
 class SmokeEnvironment : public testing::Environment {
  public:
-  explicit SmokeEnvironment() {
+  explicit SmokeEnvironment(uid_t uid) : uid_(uid) {
   }
   void SetUp() override {
-    if (kRequestMode == ci::RequestMode::USER)
-      ASSERT_TRUE(AddTestUser(kNormalUserName));
-    else {
-      kTestUserId = kGlobalUserUid;
-      kTestGroupId = kGlobalUserGid;
-      kTestUserIdStr = std::to_string(kTestUserId);
-    }
-    backups_ = SetupBackupDirectories();
+    backups_ = SetupBackupDirectories(uid_);
     for (auto& path : backups_)
       BackupPath(path);
   }
   void TearDown() override {
-    ASSERT_TRUE(kRequestMode == ci::RequestMode::GLOBAL ||
-                (kRequestMode == ci::RequestMode::USER &&
-                kGlobalUserUid != kTestUserId));
-    UninstallAllSmokeApps();
+    UninstallAllSmokeApps(uid_);
     for (auto& path : backups_)
       RestorePath(path);
-    if (kRequestMode == ci::RequestMode::USER)
-      ASSERT_TRUE(DeleteTestUser(kNormalUserName));
   }
 
  private:
+  uid_t uid_;
   std::vector<bf::path> backups_;
 };
 
 class SmokeTest : public testing::Test {
 };
 
-class PreloadSmokeTest : public testing::Test {
-  void SetUp() override {
-    ASSERT_TRUE(kRequestMode == ci::RequestMode::GLOBAL);
-  }
-};
-
 TEST_F(SmokeTest, InstallationMode) {
   bf::path path = kSmokePackagesDirectory / "InstallationMode.wgt";
   std::string pkgid = "smokewgt03";
@@ -1386,7 +1297,7 @@ TEST_F(SmokeTest, MigrateLegacyExternalImageMode) {
   ValidateExternalPackage(pkgid, {appid});
 }
 
-TEST_F(PreloadSmokeTest, InstallationMode_Preload) {
+TEST_F(SmokeTest, InstallationMode_Preload) {
   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
   bf::path path = kSmokePackagesDirectory / "InstallationMode_Preload.wgt";
   std::string pkgid = "smokewgt37";
@@ -1396,7 +1307,7 @@ TEST_F(PreloadSmokeTest, InstallationMode_Preload) {
   ValidatePackage(pkgid, {appid}, true);
 }
 
-TEST_F(PreloadSmokeTest, UpdateMode_Preload) {
+TEST_F(SmokeTest, UpdateMode_Preload) {
   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
   bf::path path_old = kSmokePackagesDirectory / "UpdateMode_Preload.wgt";
   bf::path path_new = kSmokePackagesDirectory / "UpdateMode_Preload2.wgt";
@@ -1414,7 +1325,7 @@ TEST_F(PreloadSmokeTest, UpdateMode_Preload) {
   ValidateDataFiles(pkgid, kTestUserId);
 }
 
-TEST_F(PreloadSmokeTest, DeinstallationMode_Preload) {
+TEST_F(SmokeTest, DeinstallationMode_Preload) {
   ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user";
   bf::path path = kSmokePackagesDirectory / "DeinstallationMode_Preload.wgt";
   std::string pkgid = "smokewgt39";
@@ -1501,8 +1412,7 @@ TEST_F(SmokeTest, SharedRes30HybridDelta) {
 
 int main(int argc,  char** argv) {
   testing::InitGoogleTest(&argc, argv);
-  testing::Environment *env = testing::AddGlobalTestEnvironment(
-      new common_installer::SmokeEnvironment());
-  ParseRequestMode(argc, argv);
+  testing::AddGlobalTestEnvironment(
+      new common_installer::SmokeEnvironment(kGlobalUserUid));
   return RUN_ALL_TESTS();
 }