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.
5 #include "smoke_tests/common/smoke_utils.h"
7 #include <gum/gum-user.h>
8 #include <gum/gum-user-service.h>
9 #include <gum/common/gum-user-types.h>
10 #include <manifest_parser/utils/version_number.h>
11 #include <sys/smack.h>
13 #include <vconf-internal-keys.h>
15 #include <boost/filesystem/path.hpp>
16 #include <gtest/gtest.h>
18 #include <common/installer/app_installer.h>
19 #include <common/utils/paths.h>
20 #include <common/pkgmgr_interface.h>
21 #include <common/utils/pkgmgr_query.h>
22 #include <common/tzip_interface.h>
24 #include "pkgmgr_parser_db.h"
31 namespace bf = boost::filesystem;
32 namespace bs = boost::system;
33 namespace ci = common_installer;
34 namespace bo = boost::program_options;
38 const uid_t kDefaultUserUid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
39 const gid_t kDefaultUserGid = tzplatform_getgid(TZ_SYS_DEFAULT_USER);
40 const char kNormalUserName[] = "smokeuser";
41 const char kSystemShareGroupName[] = "system_share";
42 const char kMigrateTestDBName[] = "app2sd_migrate.db";
44 const std::vector<std::string> kDBEntries = {
45 {".pkgmgr_parser.db"},
46 {".pkgmgr_parser.db-journal"},
48 {".pkgmgr_cert.db-journal"},
50 {".app2sd.db-journal"},
53 const char kGlobalManifestDir[] = "/opt/share/packages";
54 const char kSkelDir[] = "/etc/skel/apps_rw";
55 const char kPreloadApps[] = "/usr/apps";
56 const char kPreloadManifestDir[] = "/usr/share/packages";
57 const char kPreloadIcons[] = "/usr/share/icons";
58 const char kData[] = "data";
59 const char kShared[] = ".shared";
60 const char kSharedTmp[] = ".shared_tmp";
70 const char* rwDirectories[] = {
80 namespace smoke_test {
82 const char kLegacyExtImageDir[] = "legacy_extimage_dir";
83 const std::string& kDefaultUserIdStr = std::to_string(kDefaultUserUid);
84 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
85 const uid_t kGlobalUserGid = tzplatform_getgid(TZ_SYS_GLOBALAPP_USER);
86 extern const bf::path kSdkDirectory = "/home/owner/share/tmp/sdk_tools";
88 ci::RequestMode ParseRequestMode(int argc, char** argv) {
89 bo::options_description desc("Available options");
91 ("request-mode", bo::value<std::string>(), "set request mode")
92 ("global-request,g", "set request mode to global")
93 ("user-request,u", "set request mode to user");
96 bo::store(bo::command_line_parser(argc, argv).
97 options(desc).allow_unregistered().run(), vm);
100 if (vm.count("global-request")) {
101 std::cout << "Request mode was set to global." << std::endl;
102 return ci::RequestMode::GLOBAL;
104 if (vm.count("user-request")) {
105 std::cout << "Request mode was set to user." << std::endl;
106 return ci::RequestMode::USER;
108 if (vm.count("request-mode")) {
109 if (vm["request-mode"].as<std::string>() == "global") {
110 std::cout << "Request mode was set to global." << std::endl;
111 return ci::RequestMode::GLOBAL;
113 if (vm["request-mode"].as<std::string>() == "user") {
114 std::cout << "Request mode was set to user." << std::endl;
115 return ci::RequestMode::USER;
117 std::cout << "Cannot set request mode to "
118 << vm["request-mode"].as<std::string>() << std::endl;
120 std::cout << "Request mode was set to global." << std::endl;
121 return ci::RequestMode::GLOBAL;
124 static bool AddUser(const char* user_name) {
125 GumUser* user = nullptr;
126 user = gum_user_create_sync(FALSE);
128 LOG(WARNING) << "Failed to create gum user! (user name: "
130 g_object_set(G_OBJECT(user), "username", user_name, "usertype",
131 GUM_USERTYPE_NORMAL, NULL);
132 gboolean rval = FALSE;
133 rval = gum_user_add_sync(user);
134 g_object_unref(user);
138 static bool DeleteUser(const char* user_name, bool rem_home_dir) {
140 GumUser* guser = gum_user_get_by_name_sync(user_name, FALSE);
142 rval = gum_user_delete_sync(guser, rem_home_dir);
146 bool AddTestUser(User* test_user) {
147 std::cout << "Adding test user: " << kNormalUserName << std::endl;
148 AddUser(kNormalUserName);
149 if (boost::optional<uid_t> uid = ci::GetUidByUserName(kNormalUserName)) {
150 test_user->uid = *uid;
151 std::cout << "User created properly: uid=" << *uid;
152 if (boost::optional<gid_t> gid = ci::GetGidByUid(*uid)) {
153 test_user->gid = *gid;
154 std::cout << " gid=" << *gid;
156 std::cout << std::endl;
159 LOG(ERROR) << "Adding test user failed";
163 bool DeleteTestUser() {
164 std::cout << "Deleting test user: " << kNormalUserName << std::endl;
166 if (boost::optional<uid_t> uid = ci::GetUidByUserName(kNormalUserName))
170 DeleteUser(kNormalUserName, true);
171 if (!ci::GetUidByUserName(kNormalUserName)) {
172 std::cout << "User deleted properly: user_name=" << kNormalUserName
173 << " uid=" << test_uid << std::endl;
176 LOG(ERROR) << "Deleting test user failed";
180 bool TouchFile(const bf::path& path) {
181 FILE* f = fopen(path.c_str(), "w+");
188 void AddDataFiles(const std::string& pkgid, uid_t uid) {
189 if (uid == kGlobalUserUid) {
190 ci::UserList list = ci::GetUserList();
191 for (auto l : list) {
192 auto pkg_path = GetPackageRoot(pkgid, std::get<0>(l));
193 ASSERT_TRUE(TouchFile(pkg_path / "data" / "file1.txt"));
194 ASSERT_TRUE(TouchFile(pkg_path / "data" / "file2.txt"));
197 auto pkg_path = GetPackageRoot(pkgid, uid);
198 ASSERT_TRUE(TouchFile(pkg_path / "data" / "file1.txt"));
199 ASSERT_TRUE(TouchFile(pkg_path / "data" / "file2.txt"));
203 void RemoveAllRecoveryFiles(const std::string& prefix, uid_t uid) {
204 bf::path root_path = ci::GetRootAppPath(false, uid);
205 if (!bf::exists(root_path))
207 for (auto& dir_entry : boost::make_iterator_range(
208 bf::directory_iterator(root_path), bf::directory_iterator())) {
209 if (bf::is_regular_file(dir_entry)) {
210 if (dir_entry.path().string().find(prefix) != std::string::npos) {
211 bs::error_code error;
212 bf::remove(dir_entry.path(), error);
214 LOG(ERROR) << "Failed to remove " << dir_entry.path()
215 << ": " << error.message();
221 bf::path FindRecoveryFile(const std::string& prefix, uid_t uid) {
222 bf::path root_path = ci::GetRootAppPath(false, uid);
223 if (!bf::exists(root_path))
226 for (auto& dir_entry : boost::make_iterator_range(
227 bf::directory_iterator(root_path), bf::directory_iterator())) {
228 if (bf::is_regular_file(dir_entry)) {
229 if (dir_entry.path().string().find(prefix) != std::string::npos) {
230 return dir_entry.path();
237 std::unique_ptr<ci::recovery::RecoveryFile> GetRecoverFileInfo(
238 const bf::path& recovery_file_path) {
239 return ci::recovery::RecoveryFile::OpenRecoveryFile(recovery_file_path);
242 bf::path GetPackageRoot(const std::string& pkgid, uid_t uid) {
243 bf::path root_path = ci::GetRootAppPath(false, uid);
244 return root_path / pkgid;
247 bool ValidateFileContentInPackage(const std::string& pkgid,
248 const std::string& relative,
249 const std::string& expected,
250 const TestParameters& params) {
251 bf::path file_path = ci::GetRootAppPath(params.is_readonly,
252 params.test_user.uid);
253 file_path = file_path / pkgid / relative;
254 if (!bf::exists(file_path)) {
255 LOG(ERROR) << file_path << " doesn't exist";
258 FILE* handle = fopen(file_path.c_str(), "r");
260 LOG(ERROR) << file_path << " cannot be open";
264 std::array<char, 200> buffer;
265 while (fgets(buffer.data(), buffer.size(), handle)) {
266 content += buffer.data();
269 return content == expected;
272 static bool ValidatePackageRWFS(const std::string& pkgid, uid_t uid) {
273 bf::path root_path = ci::GetRootAppPath(false, uid);
274 bf::path package_path = root_path / pkgid;
275 bf::path data_path = package_path / rwDirectories[DATA];
276 bf::path cache_path = package_path / rwDirectories[CACHE];
277 bf::path shared_data_path = package_path / rwDirectories[SHARED_DATA];
279 EXTENDED_ASSERT_TRUE(bf::exists(data_path));
280 EXTENDED_ASSERT_TRUE(bf::exists(cache_path));
283 stat(data_path.c_str(), &stats);
284 // gid of RW dirs should be system_share
285 boost::optional<gid_t> system_share =
286 ci::GetGidByGroupName(kSystemShareGroupName);
287 EXTENDED_ASSERT_EQ(uid, stats.st_uid);
288 EXTENDED_ASSERT_EQ(*system_share, stats.st_gid);
289 if (bf::exists(shared_data_path)) {
290 stat(shared_data_path.c_str(), &stats);
291 EXTENDED_ASSERT_EQ(uid, stats.st_uid);
292 EXTENDED_ASSERT_EQ(*system_share, stats.st_gid);
295 stat(cache_path.c_str(), &stats);
296 EXTENDED_ASSERT_EQ(uid, stats.st_uid);
297 EXTENDED_ASSERT_EQ(*system_share, stats.st_gid);
301 static bool ValidatePackageFS(const std::string& pkgid, const Apps& apps,
302 const TestParameters& params) {
303 bf::path root_path = ci::GetRootAppPath(params.is_readonly,
304 params.test_user.uid);
305 bf::path package_path = root_path / pkgid;
306 bf::path shared_path = package_path / "shared";
307 EXTENDED_ASSERT_TRUE(bf::exists(root_path));
308 EXTENDED_ASSERT_TRUE(bf::exists(package_path));
309 EXTENDED_ASSERT_TRUE(bf::exists(shared_path));
311 bf::path manifest_path =
312 bf::path(getUserManifestPath(params.test_user.uid,
313 params.is_readonly)) / (pkgid + ".xml");
314 EXTENDED_ASSERT_TRUE(bf::exists(manifest_path));
316 for (auto& app : apps) {
317 const std::string &exec = app.second;
318 bf::path binary_path = package_path / "bin" / exec;
319 EXTENDED_ASSERT_TRUE(bf::exists(binary_path));
322 if (params.pkg_type == PackageType::WGT ||
323 params.pkg_type == PackageType::HYBRID) {
324 bf::path widget_root_path = package_path / "res" / "wgt";
325 bf::path config_path = widget_root_path / "config.xml";
326 EXTENDED_ASSERT_TRUE(bf::exists(widget_root_path));
327 EXTENDED_ASSERT_TRUE(bf::exists(config_path));
329 bf::path private_tmp_path = package_path / "tmp";
330 EXTENDED_ASSERT_TRUE(bf::exists(private_tmp_path));
333 // backups should not exist
334 bf::path package_backup = ci::GetBackupPathForPackagePath(package_path);
335 bf::path manifest_backup = ci::GetBackupPathForManifestFile(manifest_path);
336 EXTENDED_ASSERT_FALSE(bf::exists(package_backup));
337 EXTENDED_ASSERT_FALSE(bf::exists(manifest_backup));
339 for (bf::recursive_directory_iterator iter(package_path);
340 iter != bf::recursive_directory_iterator(); ++iter) {
341 if (bf::is_symlink(symlink_status(iter->path())))
343 bool is_rw_dir = false;
344 for (const auto rw_dir : rwDirectories) {
345 bf::path rw_dir_path = rw_dir;
346 is_rw_dir |= ci::MakeRelativePath(iter->path(), package_path)
349 if (is_rw_dir || iter->path().filename() == ".mmc") {
354 stat(iter->path().c_str(), &stats);
355 EXTENDED_ASSERT_EQ(params.test_user.uid, stats.st_uid);
356 EXTENDED_ASSERT_EQ(params.test_user.gid, stats.st_gid);
361 bool ValidatePackage(const std::string& pkgid, const Apps& apps,
362 const TestParameters& params) {
363 ci::PkgQueryInterface pkg_query(pkgid, params.test_user.uid, true);
364 EXTENDED_ASSERT_TRUE(pkg_query.IsPackageInstalled(
365 ci::GetRequestMode(params.test_user.uid)));
366 EXTENDED_ASSERT_TRUE(ValidatePackageFS(pkgid, apps, params));
367 if (params.test_user.uid == kGlobalUserUid) {
368 ci::UserList list = ci::GetUserList();
370 EXTENDED_ASSERT_TRUE(ValidatePackageRWFS(pkgid, std::get<0>(l)));
372 EXTENDED_ASSERT_TRUE(ValidatePackageRWFS(pkgid, params.test_user.uid));
377 bool ValidateDataFiles(const std::string& pkgid, uid_t uid) {
378 if (uid == kGlobalUserUid) {
379 ci::UserList list = ci::GetUserList();
380 for (auto l : list) {
381 auto pkg_path = GetPackageRoot(pkgid, std::get<0>(l));
382 EXTENDED_ASSERT_TRUE(bf::exists(pkg_path / "data" / "file1.txt"));
383 EXTENDED_ASSERT_TRUE(bf::exists(pkg_path / "data" / "file2.txt"));
386 auto pkg_path = GetPackageRoot(pkgid, uid);
387 EXTENDED_ASSERT_TRUE(bf::exists(pkg_path / "data" / "file1.txt"));
388 EXTENDED_ASSERT_TRUE(bf::exists(pkg_path / "data" / "file2.txt"));
393 static bool ValidateExternalPackageFS(const std::string& pkgid,
394 const Apps& apps, const TestParameters& params) {
395 EXTENDED_ASSERT_EQ(app2ext_usr_enable_external_pkg(pkgid.c_str(),
396 params.test_user.uid), 0);
397 bf::path root_path = ci::GetRootAppPath(false, params.test_user.uid);
398 if (params.pkg_type == PackageType::TPK) {
399 EXTENDED_ASSERT_TRUE(bf::exists(root_path / pkgid / ".mmc" / "bin"));
400 EXTENDED_ASSERT_TRUE(bf::exists(root_path / pkgid / ".mmc" / "lib"));
402 EXTENDED_ASSERT_TRUE(bf::exists(root_path / pkgid / ".mmc" / "res"));
403 EXTENDED_ASSERT_TRUE(ValidatePackageFS(pkgid, apps, params));
404 EXTENDED_ASSERT_EQ(app2ext_usr_disable_external_pkg(pkgid.c_str(),
405 params.test_user.uid), 0);
409 bool ValidateExternalPackage(const std::string& pkgid, const Apps& apps,
410 const TestParameters& params) {
411 ci::PkgQueryInterface pkg_query(pkgid, params.test_user.uid, true);
412 std::string storage = pkg_query.StorageForPkgId();
413 bf::path ext_mount_path = ci::GetExternalCardPath();
414 if (bf::is_empty(ext_mount_path)) {
415 LOG(INFO) << "Sdcard not exists!";
416 EXTENDED_ASSERT_EQ(storage, "installed_internal");
418 EXTENDED_ASSERT_EQ(storage, "installed_external");
420 EXTENDED_ASSERT_TRUE(ValidateExternalPackageFS(pkgid, apps, params));
424 bool ValidateExtendedPackage(const std::string& pkgid, const Apps& apps,
425 const TestParameters& params) {
426 ci::PkgQueryInterface pkg_query(pkgid, params.test_user.uid, true);
427 std::string storage = pkg_query.StorageForPkgId();
428 bf::path extended_path =
429 bf::path(ci::GetExtendedRootAppPath(params.test_user.uid)) / pkgid;
430 if (!bf::exists(extended_path)) {
431 LOG(INFO) << "Extended storage not exists!";
432 EXTENDED_ASSERT_EQ(storage, "installed_internal");
434 EXTENDED_ASSERT_EQ(storage, "installed_extended");
436 EXTENDED_ASSERT_TRUE(ValidatePackage(pkgid, apps, params));
440 static bool PackageCheckCleanup(const std::string& pkgid,
441 const TestParameters& params) {
442 bf::path root_path = ci::GetRootAppPath(params.is_readonly,
443 params.test_user.uid);
444 bf::path package_path = root_path / pkgid;
445 EXTENDED_ASSERT_FALSE(bf::exists(package_path));
447 bf::path manifest_path = bf::path(getUserManifestPath(params.test_user.uid,
448 params.is_readonly)) / (pkgid + ".xml");
449 EXTENDED_ASSERT_FALSE(bf::exists(manifest_path));
451 // backups should not exist
452 bf::path package_backup = ci::GetBackupPathForPackagePath(package_path);
453 bf::path manifest_backup = ci::GetBackupPathForManifestFile(manifest_path);
454 EXTENDED_ASSERT_FALSE(bf::exists(package_backup));
455 EXTENDED_ASSERT_FALSE(bf::exists(manifest_backup));
459 bool CheckPackageNonExistance(const std::string& pkgid,
460 const TestParameters& params) {
461 ci::PkgQueryInterface pkg_query(pkgid, params.test_user.uid, true);
462 EXTENDED_ASSERT_FALSE(pkg_query.IsPackageInstalled(
463 ci::GetRequestMode(params.test_user.uid)));
464 EXTENDED_ASSERT_TRUE(PackageCheckCleanup(pkgid, params));
465 if (params.test_user.uid == kGlobalUserUid) {
466 bf::path skel_path(kSkelDir);
467 EXTENDED_ASSERT_FALSE(bf::exists(skel_path / pkgid));
468 EXTENDED_ASSERT_FALSE(bf::exists(skel_path / kShared / pkgid));
469 EXTENDED_ASSERT_FALSE(bf::exists(skel_path / kSharedTmp / pkgid));
470 ci::UserList list = ci::GetUserList();
471 for (auto& l : list) {
472 bf::path root_path = ci::GetRootAppPath(false, std::get<0>(l));
473 EXTENDED_ASSERT_FALSE(bf::exists(root_path / kShared / pkgid));
474 EXTENDED_ASSERT_FALSE(bf::exists(root_path / kSharedTmp / pkgid));
475 bf::path package_path = root_path / pkgid;
476 EXTENDED_ASSERT_FALSE(bf::exists(package_path));
482 bool CheckAvailableExternalPath() {
483 bf::path ext_mount_path = ci::GetExternalCardPath();
484 LOG(DEBUG) << "ext_mount_path :" << ext_mount_path;
485 if (ext_mount_path.empty()) {
486 LOG(ERROR) << "Sdcard not exists!";
492 bool CheckAvailableExtendedPath() {
493 bf::path extended_path = bf::path(tzplatform_getenv(TZ_SYS_EXTENDEDSD));
494 LOG(DEBUG) << "extended_path :" << extended_path;
495 // TODO(jeremy.jang): It should be checked by libstorage API.
496 if (!bf::exists(extended_path)) {
497 LOG(ERROR) << "Extended storage not exists!";
503 bool CheckPackageReadonlyNonExistance(const std::string& pkgid,
504 const TestParameters& params) {
505 ci::PkgQueryInterface pkg_query(pkgid, params.test_user.uid, true);
506 EXTENDED_ASSERT_FALSE(pkg_query.IsPackageInstalled(
507 ci::GetRequestMode(params.test_user.uid)));
508 EXTENDED_ASSERT_TRUE(PackageCheckCleanup(pkgid, params));
512 static bool CheckSharedDataExistanceForPath(const bf::path& apps_rw,
513 const std::string& pkgid) {
514 bf::path shared_data_path = apps_rw / pkgid / rwDirectories[SHARED_DATA];
515 bf::path shared = apps_rw / kShared / pkgid / kData;
516 bf::path shared_tmp = apps_rw / kSharedTmp / pkgid;
517 EXTENDED_ASSERT_TRUE(bf::exists(shared_data_path));
518 EXTENDED_ASSERT_TRUE(bf::exists(shared));
519 EXTENDED_ASSERT_TRUE(bf::exists(shared_tmp));
523 static bool CheckSharedDataPermissions(const bf::path& apps_rw,
524 const std::string& pkgid, uid_t uid) {
525 bf::path shared_data_path = apps_rw / pkgid / rwDirectories[SHARED_DATA];
526 bf::path shared = apps_rw / kShared / pkgid / kData;
527 bf::path shared_tmp = apps_rw / kSharedTmp / pkgid;
528 // gid of RW dirs should be system_share
529 boost::optional<gid_t> system_share =
530 ci::GetGidByGroupName(kSystemShareGroupName);
532 stat(shared_data_path.c_str(), &stats);
533 EXTENDED_ASSERT_EQ(uid, stats.st_uid);
534 EXTENDED_ASSERT_EQ(*system_share, stats.st_gid);
535 stat(shared.c_str(), &stats);
536 EXTENDED_ASSERT_EQ(uid, stats.st_uid);
537 EXTENDED_ASSERT_EQ(*system_share, stats.st_gid);
538 stat(shared_tmp.c_str(), &stats);
539 EXTENDED_ASSERT_EQ(uid, stats.st_uid);
540 EXTENDED_ASSERT_EQ(kDefaultUserGid, stats.st_gid);
544 bool CheckSharedDataExistance(const std::string& pkgid,
545 const TestParameters& params) {
546 if (params.test_user.uid == kGlobalUserUid) {
547 bf::path skel_path(kSkelDir);
548 EXTENDED_ASSERT_TRUE(CheckSharedDataExistanceForPath(kSkelDir, pkgid));
549 ci::UserList list = ci::GetUserList();
550 for (auto& l : list) {
551 uid_t uid = std::get<0>(l);
552 bf::path apps_rw = ci::GetRootAppPath(false, uid);
553 EXTENDED_ASSERT_TRUE(CheckSharedDataExistanceForPath(apps_rw, pkgid));
554 EXTENDED_ASSERT_TRUE(CheckSharedDataPermissions(apps_rw, pkgid, uid));
557 bf::path apps_rw = ci::GetRootAppPath(false, params.test_user.uid);
558 EXTENDED_ASSERT_TRUE(CheckSharedDataExistanceForPath(apps_rw, pkgid));
559 EXTENDED_ASSERT_TRUE(
560 CheckSharedDataPermissions(apps_rw, pkgid, params.test_user.uid));
565 static bool CheckSharedDataNonExistanceForPath(const bf::path& apps_rw,
566 const std::string pkgid) {
567 bf::path shared_data_path = apps_rw / pkgid / rwDirectories[SHARED_DATA];
568 bf::path shared = apps_rw / kShared / pkgid / kData;
569 bf::path shared_tmp = apps_rw / kSharedTmp / pkgid;
570 EXTENDED_ASSERT_FALSE(bf::exists(shared_data_path));
571 EXTENDED_ASSERT_FALSE(bf::exists(shared));
572 EXTENDED_ASSERT_FALSE(bf::exists(shared_tmp));
576 bool CheckSharedDataNonExistance(const std::string& pkgid,
577 const TestParameters& params) {
578 if (params.test_user.uid == kGlobalUserUid) {
579 bf::path skel_path(kSkelDir);
580 EXTENDED_ASSERT_TRUE(
581 CheckSharedDataNonExistanceForPath(skel_path, pkgid));
583 ci::UserList list = ci::GetUserList();
584 for (auto& l : list) {
585 uid_t uid = std::get<0>(l);
586 bf::path apps_rw = ci::GetRootAppPath(false, uid);
587 EXTENDED_ASSERT_TRUE(CheckSharedDataNonExistanceForPath(apps_rw, pkgid));
590 bf::path apps_rw = ci::GetRootAppPath(false, params.test_user.uid);
591 EXTENDED_ASSERT_TRUE(CheckSharedDataNonExistanceForPath(apps_rw, pkgid));
596 void BackendInterface::TestRollbackAfterEachStep(int argc, const char* argv[],
597 std::function<bool()> validator) const {
598 ci::Subprocess backend_helper = CreateSubprocess();
599 bool result = backend_helper.RunFunc({[&]() -> int {
600 TestPkgmgrInstaller pkgmgr_installer;
601 std::shared_ptr<ci::AppQueryInterface> query_interface =
602 CreateQueryInterface();
604 ci::PkgMgrInterface::Create(argc, const_cast<char**>(argv),
608 LOG(ERROR) << "Failed to initialize pkgmgr interface";
611 AppInstallerPtr backend;
612 unsigned int insert_idx = 0;
614 backend = CreateFailExpectedInstaller(pkgmgr, insert_idx);
615 LOG(DEBUG) << "StepFail is inserted at: " << insert_idx;
616 ci::AppInstaller::Result ret = backend->Run();
617 if (ret != ci::AppInstaller::Result::ERROR) {
618 LOG(ERROR) << "StepFail not executed";
624 } while (insert_idx < backend->StepCount());
625 if (insert_idx != backend->StepCount()) {
626 LOG(ERROR) << "Fail to validate";
632 ASSERT_EQ(result, true);
633 int status = backend_helper.Wait();
634 ASSERT_NE(WIFEXITED(status), 0);
635 ASSERT_EQ(WEXITSTATUS(status), 0);
638 void BackendInterface::CrashAfterEachStep(std::vector<std::string>* args,
639 std::function<bool(int iter)> validator, PackageType type) const {
640 std::unique_ptr<const char*[]> argv(new const char*[args->size()]);
641 for (size_t i = 0; i < args->size(); ++i) {
642 argv[i] = args->at(i).c_str();
644 TestPkgmgrInstaller pkgmgr_installer;
645 auto query_interface = CreateQueryInterface();
647 ci::PkgMgrInterface::Create(args->size(), const_cast<char**>(argv.get()),
651 LOG(ERROR) << "Failed to initialize pkgmgr interface";
654 auto backend = CreateFailExpectedInstaller(pkgmgr, 0);
655 ASSERT_EQ(backend->Run(), ci::AppInstaller::Result::ERROR);
656 int stepCount = backend->StepCount();
658 args->push_back("-idx");
659 args->push_back(std::to_string(stepCount));
661 std::string prefix = (type == PackageType::TPK) ? "tpk" : "wgt";
662 for (i = 0; i < stepCount; i++) {
663 ci::Subprocess backend_crash(
664 "/usr/bin/" + prefix + "-installer-ut/smoke-test-helper");
665 args->back() = std::to_string(i);
666 backend_crash.Run(*args);
667 ASSERT_NE(backend_crash.Wait(), 0);
671 ASSERT_EQ(stepCount, i);
673 args->push_back("-type_clean");
674 for (i = stepCount - 1; i >= 2; i--) {
675 ci::Subprocess backend_crash(
676 "/usr/bin/" + prefix + "-installer-ut/smoke-test-helper");
677 auto it = args->end();
679 *it = std::to_string(i);
680 backend_crash.Run(*args);
681 ASSERT_NE(backend_crash.Wait(), 0);
688 BackendInterface::CommandResult BackendInterface::RunInstallersWithPkgmgr(
689 ci::PkgMgrPtr pkgmgr) const {
690 std::list<AppInstallerPtr> installers;
691 for (int i = 0; i < pkgmgr->GetRequestInfoCount(); i++) {
692 AppInstallerPtr installer;
693 if (mode_ == RequestResult::FAIL && i == pkgmgr->GetRequestInfoCount() - 1)
694 installer = factory_->CreateFailExpectedInstaller(i, pkgmgr);
696 installer = factory_->CreateInstaller(i, pkgmgr);
698 LOG(ERROR) << "Failed to create installer";
700 installers.emplace_back(std::move(installer));
703 // FIXME: I think we should not implement this logic here...
704 CommandResult result = CommandResult::OK;
705 std::list<AppInstallerPtr>::iterator it(installers.begin());
706 for (; it != installers.end(); ++it) {
707 result = (*it)->Process();
708 if (result != CommandResult::OK)
711 if (it != installers.end() && result == CommandResult::ERROR) {
713 CommandResult ret = (*it)->Undo();
714 if (ret != CommandResult::OK && ret != CommandResult::ERROR)
715 result = CommandResult::UNDO_ERROR;
716 } while (it-- != installers.begin());
720 if ((*it)->Clean() != CommandResult::OK)
721 result = CommandResult::CLEANUP_ERROR;
722 } while (it-- != installers.begin());
727 BackendInterface::CommandResult BackendInterface::CallBackendWithRunner(
728 int argc, const char* argv[]) const {
729 TestPkgmgrInstaller pkgmgr_installer;
730 auto pkgmgr = ci::PkgMgrInterface::Create(argc, const_cast<char**>(argv),
733 LOG(ERROR) << "Failed to initialize pkgmgr interface";
734 return BackendInterface::CommandResult::UNKNOWN;
736 return RunInstallersWithPkgmgr(pkgmgr);
739 BackendInterface::CommandResult BackendInterface::Install(
740 const std::vector<bf::path>& paths) const {
741 std::vector<const char*> argv;
742 argv.emplace_back("");
743 argv.emplace_back("-i");
744 for (const auto& p : paths)
745 argv.emplace_back(p.string().c_str());
746 return CallBackendWithRunner(argv.size(), argv.data());
749 BackendInterface::CommandResult BackendInterface::Uninstall(
750 const std::vector<std::string>& pkgids) const {
751 std::vector<const char*> argv;
752 argv.emplace_back("");
753 argv.emplace_back("-d");
754 for (const auto& p : pkgids)
755 argv.emplace_back(p.c_str());
756 return CallBackendWithRunner(argv.size(), argv.data());
759 BackendInterface::CommandResult BackendInterface::InstallSuccess(
760 const std::vector<bf::path>& paths) const {
761 RequestResult tmp_mode = mode_;
762 RequestResult &original_mode = const_cast<RequestResult&>(mode_);
763 original_mode = RequestResult::NORMAL;
764 if (Install(paths) != BackendInterface::CommandResult::OK) {
765 LOG(ERROR) << "Failed to install application. Cannot update";
766 return BackendInterface::CommandResult::UNKNOWN;
768 original_mode = tmp_mode;
769 return BackendInterface::CommandResult::OK;
772 BackendInterface::CommandResult BackendInterface::RunInstallerWithPkgrmgr(
773 ci::PkgMgrPtr pkgmgr) const {
774 std::unique_ptr<ci::AppInstaller> installer;
776 case RequestResult::FAIL:
777 installer = CreateFailExpectedInstaller(pkgmgr);
780 installer = CreateInstaller(pkgmgr);
783 return installer->Run();
786 BackendInterface::CommandResult BackendInterface::CallBackend(int argc,
787 const char* argv[]) const {
788 TestPkgmgrInstaller pkgmgr_installer;
789 auto query_interface = CreateQueryInterface();
790 auto pkgmgr = ci::PkgMgrInterface::Create(argc, const_cast<char**>(argv),
791 &pkgmgr_installer, query_interface);
793 LOG(ERROR) << "Failed to initialize pkgmgr interface";
794 return BackendInterface::CommandResult::UNKNOWN;
796 return RunInstallerWithPkgrmgr(pkgmgr);
799 BackendInterface::CommandResult BackendInterface::Install(
800 const bf::path& path) const {
801 const char* argv[] = {"", "-i", path.c_str(), "-u", uid_str_.c_str()};
802 return CallBackend(SIZEOFARRAY(argv), argv);
805 BackendInterface::CommandResult BackendInterface::InstallPreload(
806 const bf::path& path) const {
807 const char* argv[] = {"", "-i", path.c_str(), "--preload"};
808 return CallBackend(SIZEOFARRAY(argv), argv);
811 BackendInterface::CommandResult BackendInterface::InstallWithStorage(
812 const bf::path& path, StorageType type) const {
813 int default_storage = 0;
816 case StorageType::EXTERNAL:
819 case StorageType::EXTENDED:
823 LOG(ERROR) << "Unknown storage type";
826 if (vconf_get_int(VCONFKEY_SETAPPL_DEFAULT_MEM_INSTALL_APPLICATIONS_INT,
827 &default_storage) != 0) {
828 LOG(ERROR) << "Failed to get value of "
829 "VCONFKEY_SETAPPL_DEFAULT_MEM_INSTALL_APPLICATIONS_INT";
830 return BackendInterface::CommandResult::ERROR;
832 if (vconf_set_int(VCONFKEY_SETAPPL_DEFAULT_MEM_INSTALL_APPLICATIONS_INT,
834 LOG(ERROR) << "Failed to set value of "
835 "VCONFKEY_SETAPPL_DEFAULT_MEM_INSTALL_APPLICATIONS_INT";
836 return BackendInterface::CommandResult::ERROR;
839 const char* argv[] = {"", "-i", path.c_str(), "-u", uid_str_.c_str()};
840 BackendInterface::CommandResult result = CallBackend(SIZEOFARRAY(argv), argv);
842 if (vconf_set_int(VCONFKEY_SETAPPL_DEFAULT_MEM_INSTALL_APPLICATIONS_INT,
843 default_storage) != 0) {
844 LOG(ERROR) << "Failed to set value of "
845 "VCONFKEY_SETAPPL_DEFAULT_MEM_INSTALL_APPLICATIONS_INT";
846 return BackendInterface::CommandResult::ERROR;
852 BackendInterface::CommandResult BackendInterface::MigrateLegacyExternalImage(
853 const std::string& pkgid,
854 const bf::path& path,
855 const bf::path& legacy_path) const {
856 if (InstallWithStorage(path, StorageType::EXTERNAL) !=
857 BackendInterface::CommandResult::OK) {
858 LOG(ERROR) << "Failed to install application. Cannot perform Migrate";
859 return BackendInterface::CommandResult::ERROR;
862 bf::path ext_mount_path = ci::GetExternalCardPath();
863 if (bf::is_empty(ext_mount_path)) {
864 LOG(ERROR) << "Sdcard not exists!";
865 return BackendInterface::CommandResult::ERROR;
867 bf::path app2sd_path = ext_mount_path / "app2sd";
869 char* image_name = app2ext_usr_getname_image(pkgid.c_str(),
872 LOG(ERROR) << "Failed to get external image name";
873 return BackendInterface::CommandResult::ERROR;
875 bf::path org_image = app2sd_path / image_name;
878 bs::error_code error;
879 bf::remove(org_image, error);
881 LOG(ERROR) << "Failed to remove org image";
882 return BackendInterface::CommandResult::ERROR;
885 bf::path db_path = tzplatform_getenv(TZ_SYS_DB);
886 bf::path app2sd_db = db_path / ".app2sd.db";
887 bf::path app2sd_db_journal = db_path / ".app2sd.db-journal";
888 bf::remove(app2sd_db, error);
890 LOG(ERROR) << "Failed to remove app2sd db";
891 return BackendInterface::CommandResult::ERROR;
893 bf::remove(app2sd_db_journal, error);
895 LOG(ERROR) << "Failed to remove app2sd journal db";
896 return BackendInterface::CommandResult::ERROR;
899 bf::path app2sd_migrate_db = legacy_path / kMigrateTestDBName;
900 if (!ci::CopyFile(app2sd_migrate_db, app2sd_db)) {
901 LOG(ERROR) << "Failed to copy test db";
902 return BackendInterface::CommandResult::ERROR;
905 bf::path legacy_src = legacy_path / pkgid;
906 bf::path legacy_dst = app2sd_path / pkgid;
907 if (!ci::CopyFile(legacy_src, legacy_dst)) {
908 LOG(ERROR) << "Failed to copy test image";
909 return BackendInterface::CommandResult::ERROR;
911 const char* argv[] = {"", "--migrate-extimg", pkgid.c_str(),
912 "-u", uid_str_.c_str()};
913 return CallBackend(SIZEOFARRAY(argv), argv);
916 BackendInterface::CommandResult BackendInterface::RDSUpdate(
917 const bf::path& path,
918 const std::string& pkgid) const {
919 RequestResult tmp_mode = mode_;
920 RequestResult &original_mode = const_cast<RequestResult&>(mode_);
921 original_mode = RequestResult::NORMAL;
922 if (Install(path) != BackendInterface::CommandResult::OK) {
923 LOG(ERROR) << "Failed to install application. Cannot perform RDS";
924 return BackendInterface::CommandResult::UNKNOWN;
926 const char* argv[] = {"", "-r", pkgid.c_str(), "-u",
928 original_mode = tmp_mode;
929 return CallBackend(SIZEOFARRAY(argv), argv);
932 BackendInterface::CommandResult BackendInterface::EnablePackage(
933 const std::string& pkgid) const {
934 const char* argv[] = {"", "-A", pkgid.c_str(), "-u", uid_str_.c_str()};
935 return CallBackend(SIZEOFARRAY(argv), argv);
938 BackendInterface::CommandResult BackendInterface::DisablePackage(
939 const std::string& pkgid) const {
940 const char* argv[] = {"", "-D", pkgid.c_str(), "-u", uid_str_.c_str()};
941 return CallBackend(SIZEOFARRAY(argv), argv);
944 BackendInterface::CommandResult BackendInterface::Recover(
945 const bf::path& recovery_file) const {
946 const char* argv[] = {"", "-b", recovery_file.c_str(), "-u",
948 return CallBackend(SIZEOFARRAY(argv), argv);
951 BackendInterface::CommandResult BackendInterface::ManifestDirectInstall(
952 const std::string& pkgid) const {
953 const char* argv[] = {"", "-y", pkgid.c_str(), "-u", uid_str_.c_str()};
954 return CallBackend(SIZEOFARRAY(argv), argv);
957 BackendInterface::CommandResult BackendInterface::Uninstall(
958 const std::string& pkgid) const {
959 const char* argv[] = {"", "-d", pkgid.c_str(), "-u", uid_str_.c_str()};
960 return CallBackend(SIZEOFARRAY(argv), argv);
963 BackendInterface::CommandResult BackendInterface::UninstallPreload(
964 const std::string& pkgid) const {
965 const char* argv[] = {"", "-d", pkgid.c_str(), "--preload",
967 return CallBackend(SIZEOFARRAY(argv), argv);
970 BackendInterface::CommandResult BackendInterface::InstallSuccess(
971 const bf::path& path) const {
972 RequestResult tmp_mode = mode_;
973 RequestResult &original_mode = const_cast<RequestResult&>(mode_);
974 original_mode = RequestResult::NORMAL;
975 if (Install(path) != BackendInterface::CommandResult::OK) {
976 LOG(ERROR) << "Failed to install application. Cannot update";
977 return BackendInterface::CommandResult::UNKNOWN;
979 original_mode = tmp_mode;
980 return BackendInterface::CommandResult::OK;
983 BackendInterface::CommandResult BackendInterface::InstallPreloadSuccess(
984 const bf::path& path) const {
985 RequestResult tmp_mode = mode_;
986 RequestResult &original_mode = const_cast<RequestResult&>(mode_);
987 original_mode = RequestResult::NORMAL;
988 if (InstallPreload(path) != BackendInterface::CommandResult::OK) {
989 LOG(ERROR) << "Failed to install application. Cannot update";
990 return BackendInterface::CommandResult::UNKNOWN;
992 original_mode = tmp_mode;
993 return BackendInterface::CommandResult::OK;
996 BackendInterface::CommandResult BackendInterface::MountInstall(
997 const bf::path& path) const {
998 const char* argv[] = {"", "-w", path.c_str(), "-u", uid_str_.c_str()};
999 return CallBackend(SIZEOFARRAY(argv), argv);
1002 BackendInterface::CommandResult BackendInterface::MountInstallSuccess(
1003 const bf::path& path) const {
1004 RequestResult tmp_mode = mode_;
1005 RequestResult &original_mode = const_cast<RequestResult&>(mode_);
1006 original_mode = RequestResult::NORMAL;
1007 if (MountInstall(path) != BackendInterface::CommandResult::OK) {
1008 LOG(ERROR) << "Failed to mount-install application. Cannot mount-update";
1009 return BackendInterface::CommandResult::UNKNOWN;
1011 original_mode = tmp_mode;
1012 return BackendInterface::CommandResult::OK;
1015 static boost::filesystem::path GetTrashPath(
1016 const boost::filesystem::path& path) {
1017 return path.string() + ".trash";
1020 BackendInterface::SubProcessResult BackendInterface::RunSubprocess(
1021 std::vector<std::string> args) const {
1022 args.push_back("-remove_plugin_steps");
1023 ci::Subprocess backend = CreateSubprocess();
1024 backend.RunWithArgs(args);
1025 int status = backend.Wait();
1026 if (WIFEXITED(status)) {
1027 if (WEXITSTATUS(status) == 0)
1028 return BackendInterface::SubProcessResult::SUCCESS;
1030 return BackendInterface::SubProcessResult::FAIL;
1032 return BackendInterface::SubProcessResult::UnKnown;
1035 BackendInterface::SubProcessResult BackendInterface::RunSubprocessAndKill(
1036 std::vector<std::string> args, useconds_t delay) const {
1037 args.push_back("-remove_plugin_steps");
1038 ci::Subprocess backend = CreateSubprocess();
1039 backend.RunWithArgs(args);
1042 int status = backend.Wait();
1043 if (WIFEXITED(status)) {
1044 if (WEXITSTATUS(status) == 0)
1045 return BackendInterface::SubProcessResult::SUCCESS;
1047 return BackendInterface::SubProcessResult::FAIL;
1049 if (WTERMSIG(status) == SIGKILL)
1050 return BackendInterface::SubProcessResult::KILLED;
1052 return BackendInterface::SubProcessResult::UnKnown;
1056 BackendInterface::SubProcessResult BackendInterface::InstallWithSubprocess(
1057 const bf::path& path) const {
1058 std::vector<std::string> args =
1059 { "-i", path.string(), "-u", uid_str_ };
1060 return RunSubprocess(args);
1063 BackendInterface::SubProcessResult BackendInterface::MountInstallWithSubprocess(
1064 const bf::path& path) const {
1065 std::vector<std::string> args =
1066 { "-w", path.string(), "-u", uid_str_ };
1067 return RunSubprocess(args);
1070 BackendInterface::SubProcessResult BackendInterface::RecoverWithSubprocess(
1071 const bf::path& path) const {
1072 std::vector<std::string> args =
1073 { "-b", path.string(), "-u", uid_str_ };
1074 return RunSubprocess(args);
1077 BackendInterface::SubProcessResult BackendInterface::UninstallWithSubprocess(
1078 const std::string& pkgid) const {
1079 std::vector<std::string> args = { "-d", pkgid, "-u", uid_str_ };
1080 return RunSubprocess(args);
1083 BackendInterface::SubProcessResult
1084 BackendInterface::InstallPreloadWithSubprocess(
1085 const boost::filesystem::path& path) const {
1086 std::vector<std::string> args = { "", "-i", path.string(), "--preload" };
1087 return RunSubprocess(args);
1090 BackendInterface::SubProcessResult
1091 BackendInterface::InstallWithSubprocessAndKill(
1092 const bf::path& path, useconds_t delay) const {
1093 std::vector<std::string> args =
1094 { "-i", path.string(), "-u", uid_str_ };
1095 return RunSubprocessAndKill(args, delay);
1098 BackendInterface::SubProcessResult
1099 BackendInterface::MountInstallWithSubprocessAndKill(
1100 const bf::path& path, useconds_t delay) const {
1101 std::vector<std::string> args =
1102 { "-w", path.string(), "-u", uid_str_ };
1103 return RunSubprocessAndKill(args, delay);
1106 BackendInterface::SubProcessResult
1107 BackendInterface::UninstallWithSubprocessAndKill(
1108 const std::string& pkgid, useconds_t delay) const {
1109 std::vector<std::string> args =
1110 { "-d", pkgid, "-u", uid_str_ };
1111 return RunSubprocessAndKill(args, delay);
1114 BackendInterface::SubProcessResult BackendInterface::InstallPkgsWithSubprocess(
1115 const std::vector<bf::path>& paths) const {
1116 std::vector<std::string> args;
1117 args.emplace_back("-i");
1118 for (const bf::path& p : paths)
1119 args.emplace_back(p.string());
1120 args.emplace_back("-u");
1121 args.emplace_back(uid_str_);
1122 return RunSubprocess(args);
1125 BackendInterface::SubProcessResult
1126 BackendInterface::MountInstallPkgsWithSubprocess(
1127 const std::vector<bf::path>& paths) const {
1128 std::vector<std::string> args;
1129 args.emplace_back("-w");
1130 for (const bf::path& p : paths)
1131 args.emplace_back(p.string());
1132 args.emplace_back("-u");
1133 args.emplace_back(uid_str_);
1134 return RunSubprocess(args);
1137 BackendInterface::SubProcessResult BackendInterface::RecoverPkgsWithSubprocess(
1138 const std::vector<bf::path>& paths) const {
1139 std::vector<std::string> args;
1140 args.emplace_back("-b");
1141 for (const bf::path& p : paths)
1142 args.emplace_back(p.string());
1143 args.emplace_back("-u");
1144 args.emplace_back(uid_str_);
1145 return RunSubprocess(args);
1148 BackendInterface::SubProcessResult
1149 BackendInterface::UninstallPkgsWithSubprocess(
1150 const std::vector<std::string>& pkgids) const {
1151 std::vector<std::string> args;
1152 args.emplace_back("-d");
1153 args.insert(args.end(), pkgids.begin(), pkgids.end());
1154 args.emplace_back("-u");
1155 args.emplace_back(uid_str_);
1156 return RunSubprocess(args);
1159 BackendInterface::SubProcessResult
1160 BackendInterface::InstallPkgsWithSubprocessAndKill(
1161 const std::vector<bf::path>& paths, useconds_t delay) const {
1162 std::vector<std::string> args;
1163 args.emplace_back("-i");
1164 for (const bf::path& p : paths)
1165 args.emplace_back(p.string());
1166 args.emplace_back("-u");
1167 args.emplace_back(uid_str_);
1168 return RunSubprocessAndKill(args, delay);
1171 BackendInterface::SubProcessResult
1172 BackendInterface::MountInstallPkgsWithSubprocessAndKill(
1173 const std::vector<bf::path>& paths, useconds_t delay) const {
1174 std::vector<std::string> args;
1175 args.emplace_back("-w");
1176 for (const bf::path& p : paths)
1177 args.emplace_back(p.string());
1178 args.emplace_back("-u");
1179 args.emplace_back(uid_str_);
1180 return RunSubprocessAndKill(args, delay);
1183 bool CopySmackAccess(const boost::filesystem::path& src,
1184 const boost::filesystem::path& dst) {
1185 if (!bf::exists(src) && !bf::is_symlink(src)) {
1186 LOG(ERROR) << "Failed to copy smack access label";
1190 char* access_label = nullptr;
1191 ret = smack_lgetlabel(src.c_str(), &access_label, SMACK_LABEL_ACCESS);
1193 LOG(ERROR) << "get access label from [" << src << "] fail";
1198 ret = smack_lsetlabel(dst.c_str(), access_label, SMACK_LABEL_ACCESS);
1201 LOG(ERROR) << "set access label to [" << dst << "] fail";
1207 bool CopySmackExec(const boost::filesystem::path& src,
1208 const boost::filesystem::path& dst) {
1209 if (!bf::exists(src) && !bf::is_symlink(src)) {
1210 LOG(ERROR) << "Failed to copy smack exec label";
1214 char *exec_label = nullptr;
1215 ret = smack_lgetlabel(src.c_str(), &exec_label, SMACK_LABEL_EXEC);
1217 LOG(ERROR) << "get exec label from [" << src << "] fail";
1222 ret = smack_lsetlabel(dst.c_str(), exec_label, SMACK_LABEL_EXEC);
1225 LOG(ERROR) << "set exec label to [" << dst << "] fail";
1231 bool CopySmackMmap(const boost::filesystem::path& src,
1232 const boost::filesystem::path& dst) {
1233 if (!bf::exists(src) && !bf::is_symlink(src)) {
1234 LOG(ERROR) << "Failed to copy smack mmap label";
1238 char *mmap_label = nullptr;
1239 ret = smack_lgetlabel(src.c_str(), &mmap_label, SMACK_LABEL_MMAP);
1241 LOG(ERROR) << "get mmap label from [" << src << "] fail";
1246 ret = smack_lsetlabel(dst.c_str(), mmap_label, SMACK_LABEL_MMAP);
1249 LOG(ERROR) << "set mmap label to [" << dst << "] fail";
1255 bool CopySmackTransmute(const boost::filesystem::path& src,
1256 const boost::filesystem::path& dst) {
1257 if (!bf::exists(src)) {
1258 LOG(ERROR) << "Failed to copy smack tranmute label";
1262 char *transmute_label = nullptr;
1263 ret = smack_lgetlabel(src.c_str(), &transmute_label, SMACK_LABEL_TRANSMUTE);
1265 LOG(ERROR) << "get access label from [" << src << "] fail";
1268 if (!transmute_label) {
1269 ret = smack_lsetlabel(dst.c_str(), "0", SMACK_LABEL_TRANSMUTE);
1271 if (strcmp(transmute_label, "TRUE") == 0)
1272 ret = smack_lsetlabel(dst.c_str(), "1", SMACK_LABEL_TRANSMUTE);
1274 ret = smack_lsetlabel(dst.c_str(), "0", SMACK_LABEL_TRANSMUTE);
1275 free(transmute_label);
1277 LOG(ERROR) << "set access label to [" << dst << "] fail";
1285 bool CopySmackLabels(const boost::filesystem::path& src,
1286 const boost::filesystem::path& dst) {
1287 if (!CopySmackAccess(src, dst))
1289 if (!CopySmackExec(src, dst))
1291 if (!CopySmackMmap(src, dst))
1293 if (!bf::is_symlink(src) && bf::is_directory(src)) {
1294 if (!CopySmackTransmute(src, dst))
1300 bool CopyAndRemoveWithSmack(const bf::path& src, const bf::path& dst) {
1301 bs::error_code error;
1302 if (bf::exists(dst)) {
1304 bf::remove_all(dst, error);
1306 std::cout << "Exception occurred during remove [" << dst.string()
1307 << "], and skip this file"<< std::endl;
1310 if (!bf::is_directory(dst)) {
1311 LOG(ERROR) << "remove_all fail";
1317 if (bf::is_symlink(src)) {
1318 bf::copy_symlink(src, dst, error);
1320 LOG(ERROR) << "Failed to copy symlink: " << src << ", "
1324 if (!CopySmackLabels(src, dst)) {
1325 LOG(ERROR) << "copy smack label from [" << src.string()
1326 << "] to [" << dst.string() << "] fail";
1329 } else if (bf::is_directory(src)) {
1330 if (!bf::exists(dst)) {
1331 bf::create_directories(dst, error);
1333 LOG(ERROR) << "create directories fail";
1336 ci::CopyOwnershipAndPermissions(src, dst);
1337 if (!CopySmackLabels(src, dst)) {
1338 LOG(ERROR) << "copy smack label from [" << src.string()
1339 << "] to [" << dst.string() << "] fail";
1343 bool success = true;
1344 for (bf::directory_iterator file(src);
1345 file != bf::directory_iterator();
1347 bf::path current(file->path());
1348 bf::path target = dst / current.filename();
1349 success &= CopyAndRemoveWithSmack(current, target);
1351 bf::remove_all(src);
1355 bf::copy_file(src, dst);
1356 ci::CopyOwnershipAndPermissions(src, dst);
1357 if (!CopySmackLabels(src, dst)) {
1358 LOG(ERROR) << "copy smack label from [" << src.string()
1359 << "] to [" << dst.string() << "] fail";
1362 bf::remove_all(src);
1365 std::cout << "Exception occurred during copy [" << src.string()
1366 << "], and skip this file"<< std::endl;
1373 bool BackupPathCopyAndRemove(const bf::path& path) {
1374 if (!bf::exists(path))
1377 bf::path backup_path = path.string() + ".bck";
1378 std::cout << "Backup path: " << path << " to " << backup_path << std::endl;
1379 if (!CopyAndRemoveWithSmack(path, backup_path)) {
1380 LOG(ERROR) << "Failed to setup test environment. Does some previous"
1381 << " test crashed? Path: "
1382 << backup_path << " should not exist.";
1388 bool BackupPath(const bf::path& path) {
1389 bf::path trash_path = GetTrashPath(path);
1390 if (bf::exists(trash_path)) {
1391 LOG(ERROR) << trash_path << " exists. Please remove "
1392 << trash_path << " manually!";
1395 bf::path backup_path = path.string() + ".bck";
1396 std::cout << "Backup path: " << path << " to " << backup_path << std::endl;
1397 bs::error_code error;
1398 bf::remove_all(backup_path, error);
1400 LOG(ERROR) << "Remove failed: " << backup_path
1401 << " (" << error.message() << ")";
1402 if (bf::exists(path)) {
1403 bf::rename(path, backup_path, error);
1405 LOG(ERROR) << "Failed to setup test environment. Does some previous"
1406 << " test crashed? Path: "
1407 << backup_path << " should not exist.";
1411 if (bf::is_directory(backup_path))
1412 bf::create_directory(path);
1417 void CreateDatabase() {
1418 pkgmgr_parser_create_and_initialize_db(kGlobalUserUid);
1419 pkgmgr_parser_create_and_initialize_db(getuid());
1422 bool RestorePathCopyAndRemove(const bf::path& path) {
1423 bf::path backup_path = path.string() + ".bck";
1424 if (!bf::exists(backup_path))
1427 std::cout << "Restore path: " << path << " from " << backup_path << std::endl;
1428 if (!CopyAndRemoveWithSmack(backup_path, path)) {
1429 LOG(ERROR) << "Failed to restore backup path: " << backup_path;
1435 bool RestorePath(const bf::path& path) {
1436 bf::path backup_path = path.string() + ".bck";
1437 std::cout << "Restore path: " << path << " from " << backup_path << std::endl;
1438 bs::error_code error;
1439 bf::remove_all(path, error);
1441 bf::path trash_path = GetTrashPath(path);
1442 LOG(ERROR) << "Remove failed: " << path << " (" << error.message() << ")";
1443 std::cout << "Moving " << path << " to " << trash_path << std::endl;
1444 bf::rename(path, trash_path, error);
1446 LOG(ERROR) << "Failed to move " << path << " to " << trash_path
1447 << " (" << error.message() << ")";
1449 LOG(ERROR) << trash_path << " should be removed manually!";
1451 if (bf::exists(backup_path)) {
1452 bf::rename(backup_path, path, error);
1454 LOG(ERROR) << "Failed to restore backup path: " << backup_path
1455 << " (" << error.message() << ")";
1462 std::vector<bf::path> SetupBackupDirectories(uid_t test_uid) {
1463 std::vector<bf::path> entries;
1464 bf::path db_dir = bf::path(tzplatform_getenv(TZ_SYS_DB));
1465 if (test_uid != kGlobalUserUid)
1466 db_dir = db_dir / "user" / std::to_string(test_uid);
1467 for (auto e : kDBEntries) {
1468 bf::path path = db_dir / e;
1469 entries.emplace_back(path);
1472 if (getuid() == 0) {
1473 entries.emplace_back(kPreloadApps);
1474 entries.emplace_back(kPreloadManifestDir);
1475 entries.emplace_back(kPreloadIcons);
1478 if (test_uid == kGlobalUserUid) {
1479 entries.emplace_back(kSkelDir);
1480 entries.emplace_back(kGlobalManifestDir);
1481 ci::UserList list = ci::GetUserList();
1482 for (auto l : list) {
1483 bf::path apps = std::get<2>(l) / "apps_rw";
1484 entries.emplace_back(apps);
1487 tzplatform_set_user(test_uid);
1488 bf::path approot = tzplatform_getenv(TZ_USER_APPROOT);
1489 tzplatform_reset_user();
1490 entries.emplace_back(approot);
1493 bf::path apps_rw = ci::GetRootAppPath(false, test_uid);
1494 entries.emplace_back(apps_rw);
1495 entries.emplace_back(kSdkDirectory);
1500 void UninstallAllAppsInDirectory(bf::path dir, bool is_preload,
1501 BackendInterface* backend) {
1502 if (bf::exists(dir)) {
1503 for (auto& dir_entry : boost::make_iterator_range(
1504 bf::directory_iterator(dir), bf::directory_iterator())) {
1505 if (dir_entry.path().string().find("smoke") != std::string::npos &&
1506 bf::is_directory(dir_entry)) {
1507 std::string package = dir_entry.path().filename().string();
1508 std::regex pkg_regex("smoke[a-zA-Z0-9]{5,}");
1509 if (std::regex_match(package, pkg_regex)) {
1510 BackendInterface::CommandResult result =
1511 BackendInterface::CommandResult::OK;
1513 result = backend->UninstallPreload(
1514 dir_entry.path().filename().string());
1516 result = backend->Uninstall(
1517 dir_entry.path().filename().string());
1518 if (result != BackendInterface::CommandResult::OK) {
1519 LOG(ERROR) << "Cannot uninstall smoke test app: "
1520 << dir_entry.path().filename().string();
1528 void UninstallAllSmokeApps(ci::RequestMode request_mode, uid_t test_uid,
1529 BackendInterface *backend) {
1530 std::cout << "Uninstalling all smoke apps" << std::endl;
1531 bf::path apps_rw = ci::GetRootAppPath(false, test_uid);
1532 UninstallAllAppsInDirectory(apps_rw, false, backend);
1533 if (getuid() == 0 && request_mode == ci::RequestMode::GLOBAL) {
1534 bf::path root_path = kPreloadApps;
1535 UninstallAllAppsInDirectory(root_path, true, backend);
1539 int GetAppInstalledTime(const char* appid, uid_t uid) {
1541 int installed_time = 0;
1542 pkgmgrinfo_appinfo_h handle = NULL;
1543 ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &handle);
1544 if (ret != PMINFO_R_OK)
1546 ret = pkgmgrinfo_appinfo_get_installed_time(handle, &installed_time);
1547 if (ret != PMINFO_R_OK) {
1548 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1551 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1552 return installed_time;
1555 } // namespace smoke_test