void ScratchFile::Unlink() { if (!OS::FileExists(filename_.c_str())) { return;
}
Close(); int unlink_result = unlink(filename_.c_str());
CHECK_EQ(0, unlink_result);
}
// Temporarily drops all root capabilities when the test is run as root. This is a noop otherwise.
android::base::ScopeGuard<std::function<void()>> ScopedUnroot() {
ScopedCap old_cap(cap_get_proc());
CHECK_NE(old_cap.Get(), nullptr);
ScopedCap new_cap(cap_dup(old_cap.Get()));
CHECK_NE(new_cap.Get(), nullptr);
CHECK_EQ(cap_clear_flag(new_cap.Get(), CAP_EFFECTIVE), 0);
CHECK_EQ(cap_set_proc(new_cap.Get()), 0); // `old_cap` is actually not shared with anyone else, but we have to wrap it with a `shared_ptr` // because `std::function` requires captures to be copyable. return android::base::make_scope_guard(
[old_cap = std::make_shared<ScopedCap>(std::move(old_cap))]() {
CHECK_EQ(cap_set_proc(old_cap->Get()), 0);
});
}
void CommonArtTestImpl::SetUpAndroidRootEnvVars() { if (IsHost()) {
std::string android_host_out = GetAndroidHostOut();
// Environment variable ANDROID_ROOT is set on the device, but not // necessarily on the host. constchar* android_root_from_env = getenv("ANDROID_ROOT"); if (android_root_from_env == nullptr) { // Use ANDROID_HOST_OUT for ANDROID_ROOT.
setenv("ANDROID_ROOT", android_host_out.c_str(), 1);
android_root_from_env = getenv("ANDROID_ROOT");
}
// Environment variable ANDROID_I18N_ROOT is set on the device, but not // necessarily on the host. It needs to be set so that various libraries // like libcore / icu4j / icu4c can find their data files. constchar* android_i18n_root_from_env = getenv("ANDROID_I18N_ROOT"); if (android_i18n_root_from_env == nullptr) { // Use ${ANDROID_I18N_OUT}/com.android.i18n for ANDROID_I18N_ROOT.
std::string android_i18n_root = android_host_out;
android_i18n_root += "/com.android.i18n";
setenv("ANDROID_I18N_ROOT", android_i18n_root.c_str(), 1);
}
// Environment variable ANDROID_ART_ROOT is set on the device, but not // necessarily on the host. It needs to be set so that various libraries // like libcore / icu4j / icu4c can find their data files. constchar* android_art_root_from_env = getenv("ANDROID_ART_ROOT"); if (android_art_root_from_env == nullptr) { // Use ${ANDROID_HOST_OUT}/com.android.art for ANDROID_ART_ROOT.
std::string android_art_root = android_host_out;
android_art_root += "/com.android.art";
setenv("ANDROID_ART_ROOT", android_art_root.c_str(), 1);
}
// Environment variable ANDROID_TZDATA_ROOT is set on the device, but not // necessarily on the host. It needs to be set so that various libraries // like libcore / icu4j / icu4c can find their data files. constchar* android_tzdata_root_from_env = getenv("ANDROID_TZDATA_ROOT"); if (android_tzdata_root_from_env == nullptr) { // Use ${ANDROID_HOST_OUT}/com.android.tzdata for ANDROID_TZDATA_ROOT.
std::string android_tzdata_root = android_host_out;
android_tzdata_root += "/com.android.tzdata";
setenv("ANDROID_TZDATA_ROOT", android_tzdata_root.c_str(), 1);
}
setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>.
}
}
void CommonArtTestImpl::SetUpAndroidDataDir(std::string& android_data) { if (IsHost()) { constchar* tmpdir = getenv("TMPDIR"); if (tmpdir != nullptr && tmpdir[0] != 0) {
android_data = tmpdir;
} else {
android_data = "/tmp";
}
} else { // On target, we cannot use `/mnt/sdcard` because it is mounted `noexec`, // nor `/data/dalvik-cache` as it is not accessible on `user` builds. // Instead, use `/data/local/tmp`, which does not require any special // permission.
android_data = "/data/local/tmp";
}
android_data += "/art-data-XXXXXX"; if (mkdtemp(&android_data[0]) == nullptr) {
PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
}
setenv("ANDROID_DATA", android_data.c_str(), 1);
}
void CommonArtTestImpl::SetUp() { // Some tests clear these and when running with --no_isolate this can cause // later tests to fail
Locks::Init();
MemMap::Init();
SetUpAndroidRootEnvVars();
SetUpAndroidDataDir(android_data_);
// Re-use the data temporary directory for /system_ext tests
android_system_ext_.append(android_data_);
android_system_ext_.append("/system_ext"); int mkdir_result = mkdir(android_system_ext_.c_str(), 0700);
ASSERT_EQ(mkdir_result, 0);
setenv("SYSTEM_EXT_ROOT", android_system_ext_.c_str(), 1);
// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. #ifdef ART_TARGET #ifndef ART_TARGET_NATIVETEST_DIR #error"ART_TARGET_NATIVETEST_DIR not set." #endif // Wrap it as a string literal. #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" #else #define ART_TARGET_NATIVETEST_DIR_STRING "" #endif
std::string CommonArtTestImpl::GetTestDexFileName(constchar* name) const {
CHECK(name != nullptr); // The needed jar files for gtest are located next to the gtest binary itself.
std::string executable_dir = android::base::GetExecutableDirectory(); for (auto ext : {".jar", ".dex"}) {
std::string path = executable_dir + "/art-gtest-jars-" + name + ext; if (OS::FileExists(path.c_str())) { return path;
}
}
LOG(FATAL) << "Test file " << name << " not found";
UNREACHABLE();
}
std::string CommonArtTestImpl::GetImageDirectory() { if (IsHost()) { return GetHostBootClasspathInstallRoot() + "/apex/art_boot_images/javalib";
} // On device, the boot image is generated by `generate-boot-image`. // In a standalone test, the boot image is located next to the gtest binary itself.
std::string path = android::base::GetExecutableDirectory() + "/art_boot_images"; if (OS::DirectoryExists(path.c_str())) { return path;
} // In a chroot environment prepared by scripts, the boot image is located in a predefined // location on /system.
path = "/system/framework/art_boot_images"; if (OS::DirectoryExists(path.c_str())) { return path;
} // In art-target-gtest-chroot, the boot image is located in a predefined location on /data because // /system is a mount point that replicates the real one on device.
path = "/data/local/tmp/art_boot_images"; if (OS::DirectoryExists(path.c_str())) { return path;
}
LOG(FATAL) << "Boot image not found";
UNREACHABLE();
}
// Special return code for failures between fork and exec. Pick something that // the command is unlikely to use.
constexpr int kPostForkFailure = 134;
if (pid == 0) { if (!post_fork()) {
LOG(ERROR) << "Failed post-fork function"; exit(kPostForkFailure);
UNREACHABLE();
}
// Redirect stdout and stderr.
dup2(link[1].get(), STDOUT_FILENO);
dup2(link[1].get(), STDERR_FILENO);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.