From e2687ca7eb39fc8aa1ba278199a2b641e376f4ab Mon Sep 17 00:00:00 2001 From: Damian Pietruchowski Date: Wed, 22 Feb 2017 17:12:39 +0100 Subject: [PATCH] [SmokeTest] Add request mode choice Smoke tests can be run with normal or global user. Normal user is created in environment SetUp() and is deleted in TearDown() method. Normal user name is "smokeuser". You can run tests by normal user with flags: -u or --request_mode=user You can run tests by global user with flags: -g or --request_mode=global Without above flags tests are run with global user by default Requires: - https://review.tizen.org/gerrit/#/c/116229/ Signed-off-by: Damian Pietruchowski Change-Id: If59225a8fac3f4ed25277f31466549c0cc636c50 --- src/unit_tests/smoke_test.cc | 134 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 112 insertions(+), 22 deletions(-) diff --git a/src/unit_tests/smoke_test.cc b/src/unit_tests/smoke_test.cc index 844fa1c..38b9945 100644 --- a/src/unit_tests/smoke_test.cc +++ b/src/unit_tests/smoke_test.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -25,6 +26,7 @@ #include #include #include +#include #include #include @@ -43,15 +45,51 @@ 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); -const uid_t kTestUserId = kGlobalUserUid; -const gid_t kTestGroupId = kGlobalUserGid; +uid_t kTestUserId = kGlobalUserUid; +gid_t kTestGroupId = kGlobalUserGid; +std::string kTestUserIdStr = std::to_string(kTestUserId); +const char kNormalUserName[] = "smokeuser"; 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"; @@ -152,6 +190,40 @@ 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 = ci::GetUidByUserName(user_name)) { + kTestUserId = *uid; + kTestUserIdStr = std::to_string(kTestUserId); + std::cout << "User created properly: uid=" << *uid; + if (boost::optional 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 = ci::GetUidByUserName(user_name)) + test_uid = *uid; + gboolean ret = ci::DeleteUser(user_name, true); + if (boost::optional 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); @@ -664,11 +736,11 @@ void RestorePath(const bf::path& path) { } } -std::vector SetupBackupDirectories(uid_t uid) { +std::vector SetupBackupDirectories() { std::vector entries; bf::path db_dir = bf::path(tzplatform_getenv(TZ_SYS_DB)); - if (uid != kGlobalUserUid) - db_dir = db_dir / "user" / std::to_string(uid); + if (kTestUserId != kGlobalUserUid) + db_dir = db_dir / "user" / std::to_string(kTestUserId); for (auto e : kDBEntries) { bf::path path = db_dir / e; entries.emplace_back(path); @@ -680,7 +752,7 @@ std::vector SetupBackupDirectories(uid_t uid) { entries.emplace_back(kPreloadIcons); } - if (uid == kGlobalUserUid) { + if (kTestUserId == kGlobalUserUid) { entries.emplace_back(kSkelDir); entries.emplace_back(kGlobalManifestDir); ci::UserList list = ci::GetUserList(); @@ -689,13 +761,13 @@ std::vector SetupBackupDirectories(uid_t uid) { entries.emplace_back(apps); } } else { - tzplatform_set_user(uid); + tzplatform_set_user(kTestUserId); bf::path approot = tzplatform_getenv(TZ_USER_APPROOT); tzplatform_reset_user(); entries.emplace_back(approot); } - bf::path apps_rw = ci::GetRootAppPath(false, uid); + bf::path apps_rw = ci::GetRootAppPath(false, kTestUserId); entries.emplace_back(apps_rw); return entries; @@ -718,12 +790,12 @@ void UninstallAllAppsInDirectory(bf::path dir, bool is_preload) { } } -void UninstallAllSmokeApps(uid_t uid) { - if (getuid() == 0) { +void UninstallAllSmokeApps() { + if (getuid() == 0 && kRequestMode == ci::RequestMode::GLOBAL) { bf::path root_path = kPreloadApps; UninstallAllAppsInDirectory(root_path, true); } - bf::path apps_rw = ci::GetRootAppPath(false, uid); + bf::path apps_rw = ci::GetRootAppPath(false, kTestUserId); UninstallAllAppsInDirectory(apps_rw, false); } @@ -733,27 +805,44 @@ namespace common_installer { class SmokeEnvironment : public testing::Environment { public: - explicit SmokeEnvironment(uid_t uid) : uid_(uid) { + explicit SmokeEnvironment() { } void SetUp() override { - backups_ = SetupBackupDirectories(uid_); + if (kRequestMode == ci::RequestMode::USER) + ASSERT_TRUE(AddTestUser(kNormalUserName)); + else { + kTestUserId = kGlobalUserUid; + kTestGroupId = kGlobalUserGid; + kTestUserIdStr = std::to_string(kTestUserId); + } + backups_ = SetupBackupDirectories(); for (auto& path : backups_) BackupPath(path); } void TearDown() override { - UninstallAllSmokeApps(uid_); + ASSERT_TRUE(kRequestMode == ci::RequestMode::GLOBAL || + (kRequestMode == ci::RequestMode::USER && + kGlobalUserUid != kTestUserId)); + UninstallAllSmokeApps(); for (auto& path : backups_) RestorePath(path); + if (kRequestMode == ci::RequestMode::USER) + ASSERT_TRUE(DeleteTestUser(kNormalUserName)); } private: - uid_t uid_; std::vector 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"; @@ -1286,7 +1375,7 @@ TEST_F(SmokeTest, MigrateLegacyExternalImageMode) { ValidateExternalPackage(pkgid, {appid}); } -TEST_F(SmokeTest, InstallationMode_Preload) { +TEST_F(PreloadSmokeTest, InstallationMode_Preload) { ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user"; bf::path path = kSmokePackagesDirectory / "InstallationMode_Preload.wgt"; std::string pkgid = "smokewgt37"; @@ -1296,7 +1385,7 @@ TEST_F(SmokeTest, InstallationMode_Preload) { ValidatePackage(pkgid, {appid}, true); } -TEST_F(SmokeTest, UpdateMode_Preload) { +TEST_F(PreloadSmokeTest, 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"; @@ -1314,7 +1403,7 @@ TEST_F(SmokeTest, UpdateMode_Preload) { ValidateDataFiles(pkgid, kTestUserId); } -TEST_F(SmokeTest, DeinstallationMode_Preload) { +TEST_F(PreloadSmokeTest, DeinstallationMode_Preload) { ASSERT_EQ(getuid(), 0) << "Test cannot be run by normal user"; bf::path path = kSmokePackagesDirectory / "DeinstallationMode_Preload.wgt"; std::string pkgid = "smokewgt39"; @@ -1330,7 +1419,8 @@ TEST_F(SmokeTest, DeinstallationMode_Preload) { int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); - testing::AddGlobalTestEnvironment( - new common_installer::SmokeEnvironment(kGlobalUserUid)); + testing::Environment *env = testing::AddGlobalTestEnvironment( + new common_installer::SmokeEnvironment()); + ParseRequestMode(argc, argv); return RUN_ALL_TESTS(); } -- 2.7.4