From a3b7fb6f828acc51cdc89c296b32b4081fd0d2e4 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 Change-Id: Ibf6e195dd21c78f1c7d19c45c4fa0753ec1baf7f --- CMakeLists.txt | 1 + packaging/wgt-backend.spec | 1 + src/unit_tests/CMakeLists.txt | 1 + src/unit_tests/smoke_test.cc | 161 ++++++++++++++++++++++++++++++++++++------ 4 files changed, 142 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db797e2..465821f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ PKG_CHECK_MODULES(PKGMGR_TYPES_DEPS REQUIRED pkgmgr-types) PKG_CHECK_MODULES(ENCRYPTION_DEPS REQUIRED libwebappenc) PKG_CHECK_MODULES(TPK_INSTALLER_DEPS REQUIRED tpk-installer) PKG_CHECK_MODULES(VCONF_DEPS REQUIRED vconf) +PKG_CHECK_MODULES(GUM_DEPS REQUIRED libgum) FIND_PACKAGE(Boost REQUIRED COMPONENTS system filesystem regex program_options) FIND_PACKAGE(GTest REQUIRED) diff --git a/packaging/wgt-backend.spec b/packaging/wgt-backend.spec index f892f08..b80b728 100644 --- a/packaging/wgt-backend.spec +++ b/packaging/wgt-backend.spec @@ -20,6 +20,7 @@ BuildRequires: pkgconfig(pkgmgr-types) BuildRequires: pkgconfig(libwebappenc) BuildRequires: pkgconfig(tpk-installer) BuildRequires: pkgconfig(vconf) +BuildRequires: pkgconfig(libgum) %description This is a package that installs the WGT backend of pkgmgr. diff --git a/src/unit_tests/CMakeLists.txt b/src/unit_tests/CMakeLists.txt index 3947045..7b0f80d 100644 --- a/src/unit_tests/CMakeLists.txt +++ b/src/unit_tests/CMakeLists.txt @@ -20,6 +20,7 @@ INSTALL(DIRECTORY test_samples/ DESTINATION ${SHAREDIR}/${DESTINATION_DIR}/test_ APPLY_PKG_CONFIG(${TARGET_SMOKE_TEST} PUBLIC Boost GTEST + GUM_DEPS ) APPLY_PKG_CONFIG(${TARGET_MANIFEST_TEST} PUBLIC Boost diff --git a/src/unit_tests/smoke_test.cc b/src/unit_tests/smoke_test.cc index d52b00b..0cbbfb8 100644 --- a/src/unit_tests/smoke_test.cc +++ b/src/unit_tests/smoke_test.cc @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include @@ -16,6 +18,9 @@ #include #include #include +#include +#include +#include #include #include @@ -25,6 +30,7 @@ #include #include #include +#include #include #include @@ -41,18 +47,54 @@ namespace bf = boost::filesystem; namespace bs = boost::system; +namespace bo = boost::program_options; namespace ci = common_installer; namespace { +ci::RequestMode ParseRequestMode(int argc, char** argv) { + bo::options_description desc("Available options"); + desc.add_options() + ("request-mode", bo::value(), "set request mode") + ("global-request,g", "set request mode to global") + ("user-request,u", "set request mode to user"); + + bo::variables_map vm; + bo::store(bo::parse_command_line(argc, argv, desc), vm); + bo::notify(vm); + + if (vm.count("global-request")) { + std::cout << "Request mode was set to global." << std::endl; + return ci::RequestMode::GLOBAL; + } + if (vm.count("user-request")) { + std::cout << "Request mode was set to user." << std::endl; + return ci::RequestMode::USER; + } + if (vm.count("request-mode")) { + if (vm["request-mode"].as() == "global") { + std::cout << "Request mode was set to global." << std::endl; + return ci::RequestMode::GLOBAL; + } + if (vm["request-mode"].as() == "user") { + std::cout << "Request mode was set to user." << std::endl; + return ci::RequestMode::USER; + } + std::cout << "Cannot set request mode to " + << vm["request-mode"].as() << std::endl; + std::cout << "Request mode was set to global." << std::endl; + return ci::RequestMode::GLOBAL; + + } +} 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"; @@ -153,6 +195,62 @@ bool TouchFile(const bf::path& path) { return true; } +bool AddUser(const char *user_name) { + GumUser* user = nullptr; + user = gum_user_create_sync(FALSE); + if (user == nullptr) + LOG(WARNING) << "Failed to create gum user! (user name: " + << user_name << ")"; + g_object_set(G_OBJECT(user), "username", user_name, "usertype", + GUM_USERTYPE_NORMAL, NULL); + gboolean rval = FALSE; + rval = gum_user_add_sync(user); + g_object_unref(user); + return rval; +} + +bool DeleteUser(const char *user_name, bool rem_home_dir) { + bool rval = FALSE; + GumUser* guser = gum_user_get_by_name_sync(user_name, FALSE); + if(guser) + rval = gum_user_delete_sync(guser, rem_home_dir); + return rval; +} + +bool AddTestUser(const char *user_name) { + std::cout << "Adding test user: " << user_name << std::endl; + bool ret = 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; + bool ret = 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); @@ -669,11 +767,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); @@ -685,7 +783,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(); @@ -694,13 +792,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; @@ -727,12 +825,12 @@ void UninstallAllAppsInDirectory(bf::path dir, bool is_preload) { } } -void UninstallAllSmokeApps(uid_t uid) { - if (getuid() == 0) { +void UninstallAllSmokeApps(ci::RequestMode request_mode) { + if (getuid() == 0 && request_mode == 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); } @@ -742,27 +840,46 @@ namespace common_installer { class SmokeEnvironment : public testing::Environment { public: - explicit SmokeEnvironment(uid_t uid) : uid_(uid) { + explicit SmokeEnvironment(ci::RequestMode mode) {\ + request_mode_ = mode; } void SetUp() override { - backups_ = SetupBackupDirectories(uid_); + if (request_mode_ == 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(request_mode_ == ci::RequestMode::GLOBAL || + (request_mode_ == ci::RequestMode::USER && + kGlobalUserUid != kTestUserId)); + UninstallAllSmokeApps(request_mode_); for (auto& path : backups_) RestorePath(path); + if (request_mode_ == ci::RequestMode::USER) + ASSERT_TRUE(DeleteTestUser(kNormalUserName)); } private: - uid_t uid_; + ci::RequestMode request_mode_; std::vector backups_; }; class SmokeTest : public testing::Test { }; +class PreloadSmokeTest : public testing::Test { + void SetUp() override { + ASSERT_EQ(kGlobalUserUid, kTestUserId); + } +}; + TEST_F(SmokeTest, InstallationMode) { bf::path path = kSmokePackagesDirectory / "InstallationMode.wgt"; std::string pkgid = "smokewgt03"; @@ -1297,7 +1414,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"; @@ -1307,7 +1424,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"; @@ -1325,7 +1442,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"; @@ -1412,7 +1529,7 @@ TEST_F(SmokeTest, SharedRes30HybridDelta) { 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