diff options
author | 2021-02-10 13:52:40 +0000 | |
---|---|---|
committer | 2021-02-17 11:41:42 +0000 | |
commit | d6e00a754bb5a7f041a4506b0602293fd9cce0e0 (patch) | |
tree | 605c24f4b0d1d72f3c3dd633d3ad1c8bad6e2f4e | |
parent | 3d2b93e6acb4e7ac6fc808f17813975f250dbf89 (diff) |
libartbase: add utilities for on-device signing files
Bug: 160683548
Test: art_file_utils_test.cc
Change-Id: I17967eebda0a565c033b095fb1deb6ceeaa3760d
-rw-r--r-- | build/apex/linker.config.json | 2 | ||||
-rw-r--r-- | libartbase/base/file_utils.cc | 196 | ||||
-rw-r--r-- | libartbase/base/file_utils.h | 67 | ||||
-rw-r--r-- | libartbase/base/file_utils_test.cc | 140 |
4 files changed, 347 insertions, 58 deletions
diff --git a/build/apex/linker.config.json b/build/apex/linker.config.json index 74f5ace9bf..7cdbe8020b 100644 --- a/build/apex/linker.config.json +++ b/build/apex/linker.config.json @@ -14,6 +14,6 @@ // If on-device signing has run, there may be oat / odex files for // the boot class path extensions and system_server in the ART APEX data // directory. - "/data/misc/apexdata/com.android.art/system/framework" + "/data/misc/apexdata/com.android.art/dalvik-cache" ] } diff --git a/libartbase/base/file_utils.cc b/libartbase/base/file_utils.cc index 152900a3c8..56382fb7f5 100644 --- a/libartbase/base/file_utils.cc +++ b/libartbase/base/file_utils.cc @@ -38,9 +38,10 @@ #endif #endif - #include <memory> +#include <sstream> +#include "android-base/file.h" #include "android-base/stringprintf.h" #include "android-base/strings.h" @@ -65,7 +66,6 @@ namespace art { using android::base::StringPrintf; static constexpr const char* kClassesDex = "classes.dex"; -static constexpr const char* kApexDefaultPath = "/apex/"; static constexpr const char* kAndroidRootEnvVar = "ANDROID_ROOT"; static constexpr const char* kAndroidRootDefaultPath = "/system"; static constexpr const char* kAndroidSystemExtRootEnvVar = "ANDROID_SYSTEM_EXT"; @@ -75,6 +75,8 @@ static constexpr const char* kAndroidDataDefaultPath = "/data"; static constexpr const char* kAndroidArtRootEnvVar = "ANDROID_ART_ROOT"; static constexpr const char* kAndroidConscryptRootEnvVar = "ANDROID_CONSCRYPT_ROOT"; static constexpr const char* kAndroidI18nRootEnvVar = "ANDROID_I18N_ROOT"; +static constexpr const char* kApexDefaultPath = "/apex/"; +static constexpr const char* kArtApexDataEnvVar = "ART_APEX_DATA"; // Get the "root" directory containing the "lib" directory where this instance // of the libartbase library (which contains `GetRootContainingLibartbase`) is @@ -174,9 +176,11 @@ static const char* GetAndroidDirSafe(const char* env_var, return android_dir; } -static const char* GetAndroidDir(const char* env_var, const char* default_dir) { +static const char* GetAndroidDir(const char* env_var, + const char* default_dir, + bool must_exist = true) { std::string error_msg; - const char* dir = GetAndroidDirSafe(env_var, default_dir, /* must_exist= */ true, &error_msg); + const char* dir = GetAndroidDirSafe(env_var, default_dir, must_exist, &error_msg); if (dir != nullptr) { return dir; } else { @@ -275,14 +279,74 @@ std::string GetAndroidData() { return GetAndroidDir(kAndroidDataEnvVar, kAndroidDataDefaultPath); } +std::string GetArtApexData() { + return GetAndroidDir(kArtApexDataEnvVar, kArtApexDataDefaultPath, /*must_exist=*/false); +} + +static std::string GetFirstBootClasspathExtensionJar(const std::string& android_root) { + DCHECK(kIsTargetBuild); + + // This method finds the first non-APEX DEX file in the boot class path as defined by the + // DEX2OATBOOTCLASSPATH environment variable. This corresponds to the first boot classpath + // extension (see IMAGE SECTION documentation in image.h). When on-device signing is used the + // boot class extensions are compiled together as a single image with a name derived from the + // first extension. This first boot classpath extension is usually + // '/system/framework/framework.jar'. + // + // DEX2OATBOOTCLASSPATH is generated at build time by in the init.environ.rc.in: + // ${ANDROID_BUILD_TOP}/system/core/rootdir/Android.mk + // and initialized on Android by init in init.environ.rc: + // ${ANDROID_BUILD_TOP}/system/core/rootdir/init.environ.rc.in. + // It is used by installd too. + const char* bcp = getenv("DEX2OATBOOTCLASSPATH"); + const std::string kDefaultBcpExtensionJar = android_root + "/framework/framework.jar"; + if (bcp != nullptr) { + for (std::string_view component : SplitString(bcp, ':')) { + if (component.empty()) { + continue; + } + if (!LocationIsOnApex(component)) { + return std::string{component}; + } + } + } + return kDefaultBcpExtensionJar; +} + std::string GetDefaultBootImageLocation(const std::string& android_root) { + constexpr static const char* kJavalibBootArt = "javalib/boot.art"; + constexpr static const char* kEtcBootImageProf = "etc/boot-image.prof"; + // Boot image consists of two parts: - // - the primary boot image in the ART apex (contains the Core Libraries) - // - the boot image extension on the system partition (contains framework libraries) - return StringPrintf("%s/javalib/boot.art:%s/framework/boot-framework.art!%s/etc/boot-image.prof", + // - the primary boot image in the ART APEX (contains the Core Libraries) + // - the boot image extensions (contains framework libraries) on the system partition, or + // in the ART APEX data directory, if an update for the ART module has been been installed. + if (kIsTargetBuild) { + // If the ART APEX has been updated, the compiled boot image extension will be in the ART APEX + // data directory (assuming there is space). Otherwise, for a factory installed ART APEX it is + // under $ANDROID_ROOT/framework/. + const std::string first_extension_jar{GetFirstBootClasspathExtensionJar(android_root)}; + const std::string boot_extension_image = GetApexDataBootImage(first_extension_jar); + const std::string boot_extension_filename = + GetSystemImageFilename(boot_extension_image.c_str(), kRuntimeISA); + if (OS::FileExists(boot_extension_filename.c_str(), /*check_file_type=*/true)) { + return StringPrintf("%s/%s:%s!%s/%s", + kAndroidArtApexDefaultPath, + kJavalibBootArt, + boot_extension_image.c_str(), + android_root.c_str(), + kEtcBootImageProf); + } else if (errno == EACCES) { + // Additional warning for potential SELinux misconfiguration. + PLOG(ERROR) << "Default boot image check failed, could not stat: " << boot_extension_image; + } + } + return StringPrintf("%s/%s:%s/framework/boot-framework.art!%s/%s", kAndroidArtApexDefaultPath, + kJavalibBootArt, + android_root.c_str(), android_root.c_str(), - android_root.c_str()); + kEtcBootImageProf); } std::string GetDefaultBootImageLocation(std::string* error_msg) { @@ -293,6 +357,17 @@ std::string GetDefaultBootImageLocation(std::string* error_msg) { return GetDefaultBootImageLocation(android_root); } +static std::string GetDalvikCacheDirectory(std::string_view root_directory, + std::string_view sub_directory = {}) { + static constexpr std::string_view kDalvikCache = "dalvik-cache"; + std::stringstream oss; + oss << root_directory << '/' << kDalvikCache; + if (!sub_directory.empty()) { + oss << '/' << sub_directory; + } + return oss.str(); +} + void GetDalvikCache(const char* subdir, const bool create_if_absent, std::string* dalvik_cache, bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache) { #ifdef _WIN32 @@ -315,7 +390,7 @@ void GetDalvikCache(const char* subdir, const bool create_if_absent, std::string } else { *have_android_data = true; } - const std::string dalvik_cache_root = android_data + "/dalvik-cache"; + const std::string dalvik_cache_root = GetDalvikCacheDirectory(android_data); *dalvik_cache = dalvik_cache_root + '/' + subdir; *dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str()); *is_global_cache = (android_data == kAndroidDataDefaultPath); @@ -327,18 +402,6 @@ void GetDalvikCache(const char* subdir, const bool create_if_absent, std::string #endif } -std::string GetDalvikCache(const char* subdir) { - CHECK(subdir != nullptr); - std::string android_data = GetAndroidData(); - const std::string dalvik_cache_root = android_data + "/dalvik-cache"; - const std::string dalvik_cache = dalvik_cache_root + '/' + subdir; - if (!OS::DirectoryExists(dalvik_cache.c_str())) { - // TODO: Check callers. Traditional behavior is to not abort. - return ""; - } - return dalvik_cache; -} - bool GetDalvikCacheFilename(const char* location, const char* cache_location, std::string* filename, std::string* error_msg) { if (location[0] != '/') { @@ -357,6 +420,68 @@ bool GetDalvikCacheFilename(const char* location, const char* cache_location, return true; } +static std::string GetApexDataDalvikCacheDirectory(InstructionSet isa) { + if (isa != InstructionSet::kNone) { + return GetDalvikCacheDirectory(GetArtApexData(), GetInstructionSetString(isa)); + } + return GetDalvikCacheDirectory(GetArtApexData()); +} + +static std::string GetApexDataDalvikCacheFilename(std::string_view dex_location, + InstructionSet isa, + bool encode_location, + std::string_view file_extension) { + if (LocationIsOnApex(dex_location)) { + return {}; + } + std::string apex_data_dalvik_cache = GetApexDataDalvikCacheDirectory(isa); + if (encode_location) { + // Arguments: "/system/framework/xyz.jar", "arm", true, "odex" + // Result: + // "/data/misc/apexdata/com.android.art/dalvik-cache/arm/system@framework@xyz.jar@classes.odex" + std::string result, unused_error_msg; + GetDalvikCacheFilename(std::string{dex_location}.c_str(), + apex_data_dalvik_cache.c_str(), + &result, + &unused_error_msg); + return ReplaceFileExtension(result, file_extension); + } else { + // Arguments: "/system/framework/xyz.jar", "x86_64", false, "art" + // Results: "/data/misc/apexdata/com.android.art/dalvik-cache/x86_64/boot-xyz.jar@classes.art" + std::string basename = android::base::Basename(std::string{dex_location}); + return apex_data_dalvik_cache + "/boot-" + ReplaceFileExtension(basename, file_extension); + } +} + +std::string GetApexDataOatFilename(std::string_view location, InstructionSet isa) { + return GetApexDataDalvikCacheFilename(location, isa, /*encode_location=*/false, "oat"); +} + +std::string GetApexDataOdexFilename(std::string_view location, InstructionSet isa) { + return GetApexDataDalvikCacheFilename(location, isa, /*encode_location=*/true, "odex"); +} + +std::string GetApexDataBootImage(std::string_view dex_location) { + return GetApexDataDalvikCacheFilename(dex_location, + InstructionSet::kNone, + /*encode_location=*/false, + kArtImageExtension); +} + +std::string GetApexDataImage(std::string_view dex_location) { + return GetApexDataDalvikCacheFilename(dex_location, + InstructionSet::kNone, + /*encode_location=*/true, + kArtImageExtension); +} + +std::string GetApexDataDalvikCacheFilename(std::string_view dex_location, + InstructionSet isa, + std::string_view file_extension) { + return GetApexDataDalvikCacheFilename( + dex_location, isa, /*encode_location=*/true, file_extension); +} + std::string GetVdexFilename(const std::string& oat_location) { return ReplaceFileExtension(oat_location, "vdex"); } @@ -378,16 +503,25 @@ std::string GetSystemImageFilename(const char* location, const InstructionSet is return filename; } -std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension) { +std::string ReplaceFileExtension(std::string_view filename, std::string_view new_extension) { const size_t last_ext = filename.find_last_of("./"); + std::string result; if (last_ext == std::string::npos || filename[last_ext] != '.') { - return filename + "." + new_extension; + result.reserve(filename.size() + 1 + new_extension.size()); + result.append(filename).append(".").append(new_extension); } else { - return filename.substr(0, last_ext + 1) + new_extension; + result.reserve(last_ext + 1 + new_extension.size()); + result.append(filename.substr(0, last_ext + 1)).append(new_extension); } + return result; +} + +bool LocationIsOnArtApexData(std::string_view location) { + const std::string art_apex_data = GetArtApexData(); + return android::base::StartsWith(location, art_apex_data); } -bool LocationIsOnArtModule(const char* full_path) { +bool LocationIsOnArtModule(std::string_view full_path) { std::string unused_error_msg; std::string module_path = GetArtRootSafe(/* must_exist= */ kIsTargetBuild, &unused_error_msg); if (module_path.empty()) { @@ -413,7 +547,7 @@ static bool EndsWithSlash(const char* str) { // All of `default_path`, `subdir` and the value of environment variable `env_var` // are expected to begin with a slash and not end with one. If this ever changes, // the path-building logic should be updated. -static bool IsLocationOnModule(const char* full_path, +static bool IsLocationOnModule(std::string_view full_path, const char* env_var, const char* default_path, const char* subdir = nullptr) { @@ -444,31 +578,31 @@ static bool IsLocationOnModule(const char* full_path, return android::base::StartsWith(full_path, path_prefix); } -bool LocationIsOnSystemFramework(const char* full_path) { +bool LocationIsOnSystemFramework(std::string_view full_path) { return IsLocationOnModule(full_path, kAndroidRootEnvVar, kAndroidRootDefaultPath, /* subdir= */ "framework/"); } -bool LocationIsOnSystemExtFramework(const char* full_path) { +bool LocationIsOnSystemExtFramework(std::string_view full_path) { return IsLocationOnModule(full_path, kAndroidSystemExtRootEnvVar, kAndroidSystemExtRootDefaultPath, /* subdir= */ "framework/"); } -bool LocationIsOnConscryptModule(const char* full_path) { +bool LocationIsOnConscryptModule(std::string_view full_path) { return IsLocationOnModule( full_path, kAndroidConscryptRootEnvVar, kAndroidConscryptApexDefaultPath); } -bool LocationIsOnI18nModule(const char* full_path) { +bool LocationIsOnI18nModule(std::string_view full_path) { return IsLocationOnModule( full_path, kAndroidI18nRootEnvVar, kAndroidI18nApexDefaultPath); } -bool LocationIsOnApex(const char* full_path) { +bool LocationIsOnApex(std::string_view full_path) { return android::base::StartsWith(full_path, kApexDefaultPath); } diff --git a/libartbase/base/file_utils.h b/libartbase/base/file_utils.h index bcab28826a..67caaa6cee 100644 --- a/libartbase/base/file_utils.h +++ b/libartbase/base/file_utils.h @@ -28,9 +28,12 @@ namespace art { static constexpr const char* kAndroidArtApexDefaultPath = "/apex/com.android.art"; +static constexpr const char* kArtApexDataDefaultPath = "/data/misc/apexdata/com.android.art"; static constexpr const char* kAndroidConscryptApexDefaultPath = "/apex/com.android.conscrypt"; static constexpr const char* kAndroidI18nApexDefaultPath = "/apex/com.android.i18n"; +static constexpr const char* kArtImageExtension = "art"; + // These methods return the Android Root, which is the historical location of // the Android "system" directory, containing the built Android artifacts. On // target, this is normally "/system". On host this is usually a directory under @@ -62,30 +65,53 @@ std::string GetAndroidData(); // Find $ANDROID_DATA, /data, or return an empty string. std::string GetAndroidDataSafe(/*out*/ std::string* error_msg); +// Find $ART_APEX_DATA, /data/misc/apexdata/com.android.art, or abort. +std::string GetArtApexData(); + // Returns the default boot image location (ANDROID_ROOT/framework/boot.art). // Returns an empty string if ANDROID_ROOT is not set. std::string GetDefaultBootImageLocation(std::string* error_msg); -// Returns the default boot image location, based on the passed android root. +// Returns the default boot image location, based on the passed `android_root`. std::string GetDefaultBootImageLocation(const std::string& android_root); -// Returns the dalvik-cache location, with subdir appended. Returns the empty string if the cache -// could not be found. -std::string GetDalvikCache(const char* subdir); - // Return true if we found the dalvik cache and stored it in the dalvik_cache argument. -// have_android_data will be set to true if we have an ANDROID_DATA that exists, -// dalvik_cache_exists will be true if there is a dalvik-cache directory that is present. -// The flag is_global_cache tells whether this cache is /data/dalvik-cache. +// `have_android_data` will be set to true if we have an ANDROID_DATA that exists, +// `dalvik_cache_exists` will be true if there is a dalvik-cache directory that is present. +// The flag `is_global_cache` tells whether this cache is /data/dalvik-cache. void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache, bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache); // Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be -// rooted at cache_location. -bool GetDalvikCacheFilename(const char* file_location, const char* cache_location, +// rooted at `cache_location`. +bool GetDalvikCacheFilename(const char* location, const char* cache_location, std::string* filename, std::string* error_msg); -// Returns the system location for an image +// Gets the oat location in the ART APEX data directory for a DEX file installed anywhere other +// than in an APEX. Returns the oat filename if `location` is valid, empty string otherwise. +std::string GetApexDataOatFilename(std::string_view location, InstructionSet isa); + +// Gets the odex location in the ART APEX data directory for a DEX file installed anywhere other +// than in an APEX. Returns the odex filename if `location` is valid, empty string otherwise. +std::string GetApexDataOdexFilename(std::string_view location, InstructionSet isa); + +// Gets the boot image in the ART APEX data directory for a DEX file installed anywhere other +// than in an APEX. Returns the image location if `dex_location` is valid, empty string otherwise. +std::string GetApexDataBootImage(std::string_view dex_location); + +// Gets the image in the ART APEX data directory for a DEX file installed installed anywhere other +// than in an APEX. Returns the image location if `dex_location` is valid, empty string otherwise. +std::string GetApexDataImage(std::string_view dex_location); + +// Gets the name of a file in the ART APEX directory dalvik-cache. This method assumes the +// `dex_location` is for an application and that the `dex_location` is not within an APEX. +// Returns the location of the file in the dalvik-cache +std::string GetApexDataDalvikCacheFilename(std::string_view dex_location, + InstructionSet isa, + std::string_view file_extension); + +// Returns the system location for an image. This method inserts the `isa` between the +// dirname and basename of `location`. std::string GetSystemImageFilename(const char* location, InstructionSet isa); // Returns the vdex filename for the given oat filename. @@ -96,28 +122,31 @@ std::string GetVdexFilename(const std::string& oat_filename); // a period, and `new_extension`. // Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc" // ReplaceFileExtension("foo", "abc") == "foo.abc" -std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension); +std::string ReplaceFileExtension(std::string_view filename, std::string_view new_extension); // Return whether the location is on /apex/com.android.art -bool LocationIsOnArtModule(const char* location); +bool LocationIsOnArtModule(std::string_view location); + +// Return whether the location is on /data/misc/apexdata/com.android.art/. +bool LocationIsOnArtApexData(std::string_view location); // Return whether the location is on /apex/com.android.conscrypt -bool LocationIsOnConscryptModule(const char* location); +bool LocationIsOnConscryptModule(std::string_view location); // Return whether the location is on /apex/com.android.i18n -bool LocationIsOnI18nModule(const char* location); +bool LocationIsOnI18nModule(std::string_view location); // Return whether the location is on system (i.e. android root). bool LocationIsOnSystem(const char* location); -// Return whether the location is on system/framework (i.e. android_root/framework). -bool LocationIsOnSystemFramework(const char* location); +// Return whether the location is on system/framework (i.e. $ANDROID_ROOT/framework). +bool LocationIsOnSystemFramework(std::string_view location); // Return whether the location is on system_ext/framework -bool LocationIsOnSystemExtFramework(const char* location); +bool LocationIsOnSystemExtFramework(std::string_view location); // Return whether the location is on /apex/. -bool LocationIsOnApex(const char* location); +bool LocationIsOnApex(std::string_view location); // Compare the ART module root against android root. Returns true if they are // both known and distinct. This is meant to be a proxy for 'running with apex'. diff --git a/libartbase/base/file_utils_test.cc b/libartbase/base/file_utils_test.cc index 313754e5a6..b7cc625969 100644 --- a/libartbase/base/file_utils_test.cc +++ b/libartbase/base/file_utils_test.cc @@ -19,6 +19,8 @@ #include <libgen.h> #include <stdlib.h> +#include <optional> + #include "base/stl_util.h" #include "common_art_test.h" @@ -26,6 +28,34 @@ namespace art { class FileUtilsTest : public CommonArtTest {}; +// Helper class that removes an environment variable whilst in scope. +class ScopedUnsetEnvironmentVariable { + public: + explicit ScopedUnsetEnvironmentVariable(const char* variable) + : variable_{variable}, old_value_{GetOldValue(variable)} { + unsetenv(variable); + } + + ~ScopedUnsetEnvironmentVariable() { + if (old_value_.has_value()) { + static constexpr int kReplace = 1; // tidy-issue: replace argument has libc dependent name. + setenv(variable_, old_value_.value().c_str(), kReplace); + } else { + unsetenv(variable_); + } + } + + private: + static std::optional<std::string> GetOldValue(const char* variable) { + const char* value = getenv(variable); + return value != nullptr ? std::optional<std::string>{value} : std::nullopt; + } + + const char* variable_; + std::optional<std::string> old_value_; + DISALLOW_COPY_AND_ASSIGN(ScopedUnsetEnvironmentVariable); +}; + TEST_F(FileUtilsTest, GetDalvikCacheFilename) { std::string name; std::string error; @@ -46,13 +76,6 @@ TEST_F(FileUtilsTest, GetDalvikCacheFilename) { EXPECT_EQ("/foo/system@framework@boot.oat", name); } -TEST_F(FileUtilsTest, GetDalvikCache) { - EXPECT_STREQ("", GetDalvikCache("should-not-exist123").c_str()); - - EXPECT_STREQ((android_data_ + "/dalvik-cache/.").c_str(), GetDalvikCache(".").c_str()); -} - - TEST_F(FileUtilsTest, GetSystemImageFilename) { EXPECT_STREQ("/system/framework/arm/boot.art", GetSystemImageFilename("/system/framework/boot.art", InstructionSet::kArm).c_str()); @@ -165,4 +188,107 @@ TEST_F(FileUtilsTest, ReplaceFileExtension) { EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file", "vdex")); } +TEST_F(FileUtilsTest, GetApexDataOatFilename) { + ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT"); + ScopedUnsetEnvironmentVariable i18n_root("ANDROID_I18N_ROOT"); + ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA"); + + EXPECT_EQ(GetArtApexData() + "/dalvik-cache/arm/boot-beep.oat", + GetApexDataOatFilename("/product/javalib/beep.jar", InstructionSet::kArm)); + + const std::string art_apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar"; + EXPECT_EQ(std::string{}, GetApexDataOatFilename(art_apex_jar.c_str(), InstructionSet::kArm)); + + const std::string i18n_jar = + std::string {kAndroidI18nApexDefaultPath} + "/javalib/core-icu4j.jar"; + EXPECT_EQ(std::string{}, GetApexDataOatFilename(i18n_jar, InstructionSet::kArm)); + + const std::string system_jar_apexdata_oat = GetArtApexData() + "/dalvik-cache/x86/boot-lace.oat"; + EXPECT_EQ(system_jar_apexdata_oat, + GetApexDataOatFilename("/system/framework/lace.jar", InstructionSet::kX86)); +} + +TEST_F(FileUtilsTest, GetApexDataOdexFilename) { + ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT"); + ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA"); + + EXPECT_EQ(GetArtApexData() + "/dalvik-cache/arm/data@some@code.odex", + GetApexDataOdexFilename("/data/some/code.dex", InstructionSet::kArm)); + + const std::string art_apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar"; + EXPECT_EQ(std::string{}, GetApexDataOdexFilename(art_apex_jar.c_str(), InstructionSet::kArm)); + + const std::string i18n_jar = + std::string {kAndroidI18nApexDefaultPath} + "/javalib/core-icu4j.jar"; + EXPECT_EQ(std::string{}, GetApexDataOdexFilename(i18n_jar.c_str(), InstructionSet::kArm)); + + const std::string system_jar_apexdata_odex = + GetArtApexData() + "/dalvik-cache/x86/system@framework@cookie.jar@classes.odex"; + EXPECT_EQ(system_jar_apexdata_odex, + GetApexDataOdexFilename("/system/framework/cookie.jar", InstructionSet::kX86)); +} + +TEST_F(FileUtilsTest, GetApexDataBootImage) { + ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT"); + ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA"); + + EXPECT_EQ(std::string{}, + GetApexDataBootImage(std::string {kAndroidI18nApexDefaultPath} + "/javalib/bar.jar")); + + // Check image location has the prefix "boot-" in front of the basename of dex location and + // that image suffix is .art. + const std::string system_jar = "/system/framework/disk.jar"; + const std::string boot_image = GetApexDataBootImage(system_jar.c_str()); + EXPECT_EQ(GetArtApexData() + "/dalvik-cache/boot-disk.art", boot_image); + + // Check the image filename corresponds to the oat file for the same system jar. + const InstructionSet isa = InstructionSet::kArm64; + const std::string boot_image_filename = GetSystemImageFilename(boot_image.c_str(), isa); + const std::string accompanying_oat_file = ReplaceFileExtension(boot_image_filename, "oat"); + EXPECT_EQ(accompanying_oat_file, GetApexDataOatFilename(system_jar.c_str(), isa)); +} + +TEST_F(FileUtilsTest, GetApexDataImage) { + ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT"); + ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA"); + + EXPECT_EQ(std::string{}, + GetApexDataImage(std::string {kAndroidI18nApexDefaultPath} + "/lib/javalib/bar.jar")); + + // Check image has basename of dex location with the .art suffix. + const char* jar = "/system/framework/mcguffin/test.jar"; + const std::string image = GetApexDataImage(jar); + EXPECT_EQ(GetArtApexData() + "/dalvik-cache/system@framework@mcguffin@test.jar@classes.art", + image); + + // Check the image filename corresponds to the .odex file for the same system jar. + const InstructionSet isa = InstructionSet::kX86_64; + const std::string image_filename = GetSystemImageFilename(image.c_str(), isa); + const std::string accompanying_odex_file = ReplaceFileExtension(image_filename, "odex"); + EXPECT_EQ(accompanying_odex_file, GetApexDataOdexFilename(jar, isa)); +} + +TEST_F(FileUtilsTest, GetApexDataDalvikCacheFilename) { + // Check /apex inputs return empty string + const std::string apex_jar = std::string {kAndroidI18nApexDefaultPath} + "/lib/javalib/bar.jar"; + EXPECT_EQ(std::string{}, + GetApexDataDalvikCacheFilename(apex_jar, InstructionSet::kX86_64, "art")); + + // Check dalvik-cache filename follows convention. + const std::string non_apex_jar = "/vendor/javalib/test.jar"; + const std::string art_filename = + GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "art"); + CHECK_EQ(GetArtApexData() + "/dalvik-cache/arm/vendor@javalib@test.jar@classes.art", + art_filename); + + // Check ".art", ".odex" and ".vdex" filenames are the same with the appropriate extensions + // substituted. + const std::string odex_filename = + GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "odex"); + CHECK_EQ(odex_filename, ReplaceFileExtension(art_filename, "odex")); + const std::string vdex_filename = + GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "vdex"); + CHECK_EQ(vdex_filename, ReplaceFileExtension(art_filename, "vdex")); +} + } // namespace art |