Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 17 | #include <cstdlib> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 18 | #include <fcntl.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 23 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 24 | #include <android-base/file.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 26 | #include <android-base/properties.h> |
| 27 | #include <android-base/scopeguard.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 29 | #include <android-base/unique_fd.h> |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 30 | #include <binder/Status.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 32 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 33 | #include <gtest/gtest.h> |
| 34 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 35 | #include <selinux/android.h> |
| 36 | #include <selinux/avc.h> |
| 37 | |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 38 | #include "binder_test_utils.h" |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 39 | #include "dexopt.h" |
| 40 | #include "InstalldNativeService.h" |
| 41 | #include "globals.h" |
| 42 | #include "tests/test_utils.h" |
| 43 | #include "utils.h" |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 44 | #include "ziparchive/zip_writer.h" |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 45 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 46 | using android::base::ReadFully; |
| 47 | using android::base::unique_fd; |
| 48 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 49 | namespace android { |
| 50 | namespace installd { |
| 51 | |
| 52 | // TODO(calin): try to dedup this code. |
| 53 | #if defined(__arm__) |
| 54 | static const std::string kRuntimeIsa = "arm"; |
| 55 | #elif defined(__aarch64__) |
| 56 | static const std::string kRuntimeIsa = "arm64"; |
| 57 | #elif defined(__mips__) && !defined(__LP64__) |
| 58 | static const std::string kRuntimeIsa = "mips"; |
| 59 | #elif defined(__mips__) && defined(__LP64__) |
| 60 | static const std::string kRuntimeIsa = "mips64"; |
| 61 | #elif defined(__i386__) |
| 62 | static const std::string kRuntimeIsa = "x86"; |
| 63 | #elif defined(__x86_64__) |
| 64 | static const std::string kRuntimeIsa = "x86_64"; |
| 65 | #else |
| 66 | static const std::string kRuntimeIsa = "none"; |
| 67 | #endif |
| 68 | |
| 69 | int get_property(const char *key, char *value, const char *default_value) { |
| 70 | return property_get(key, value, default_value); |
| 71 | } |
| 72 | |
| 73 | bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, |
| 74 | const char *instruction_set) { |
| 75 | return calculate_oat_file_path_default(path, oat_dir, apk_path, instruction_set); |
| 76 | } |
| 77 | |
| 78 | bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path, |
| 79 | const char *instruction_set) { |
| 80 | return calculate_odex_file_path_default(path, apk_path, instruction_set); |
| 81 | } |
| 82 | |
| 83 | bool create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) { |
| 84 | return create_cache_path_default(path, src, instruction_set); |
| 85 | } |
| 86 | |
| 87 | static void run_cmd(const std::string& cmd) { |
| 88 | system(cmd.c_str()); |
| 89 | } |
| 90 | |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 91 | template <typename Visitor> |
| 92 | static void run_cmd_and_process_output(const std::string& cmd, const Visitor& visitor) { |
| 93 | FILE* file = popen(cmd.c_str(), "r"); |
| 94 | CHECK(file != nullptr) << "Failed to ptrace " << cmd; |
| 95 | char* line = nullptr; |
| 96 | while (true) { |
| 97 | size_t n = 0u; |
| 98 | ssize_t value = getline(&line, &n, file); |
| 99 | if (value == -1) { |
| 100 | break; |
| 101 | } |
| 102 | visitor(line); |
| 103 | } |
| 104 | free(line); |
| 105 | fclose(file); |
| 106 | } |
| 107 | |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 108 | static int mkdir(const std::string& path, uid_t owner, gid_t group, mode_t mode) { |
| 109 | int ret = ::mkdir(path.c_str(), mode); |
| 110 | if (ret != 0) { |
| 111 | return ret; |
| 112 | } |
| 113 | ret = ::chown(path.c_str(), owner, group); |
| 114 | if (ret != 0) { |
| 115 | return ret; |
| 116 | } |
| 117 | return ::chmod(path.c_str(), mode); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 120 | static int log_callback(int type, const char *fmt, ...) { // NOLINT |
| 121 | va_list ap; |
| 122 | int priority; |
| 123 | |
| 124 | switch (type) { |
| 125 | case SELINUX_WARNING: |
| 126 | priority = ANDROID_LOG_WARN; |
| 127 | break; |
| 128 | case SELINUX_INFO: |
| 129 | priority = ANDROID_LOG_INFO; |
| 130 | break; |
| 131 | default: |
| 132 | priority = ANDROID_LOG_ERROR; |
| 133 | break; |
| 134 | } |
| 135 | va_start(ap, fmt); |
| 136 | LOG_PRI_VA(priority, "SELinux", fmt, ap); |
| 137 | va_end(ap); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static bool init_selinux() { |
| 142 | int selinux_enabled = (is_selinux_enabled() > 0); |
| 143 | |
| 144 | union selinux_callback cb; |
| 145 | cb.func_log = log_callback; |
| 146 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 147 | |
| 148 | if (selinux_enabled && selinux_status_open(true) < 0) { |
| 149 | LOG(ERROR) << "Could not open selinux status; exiting"; |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 156 | // Base64 encoding of a simple dex files with 2 methods. |
| 157 | static const char kDexFile[] = |
| 158 | "UEsDBBQAAAAIAOiOYUs9y6BLCgEAABQCAAALABwAY2xhc3Nlcy5kZXhVVAkAA/Ns+lkOHv1ZdXgL" |
| 159 | "AAEEI+UCAASIEwAAS0mt4DIwNmX4qpn7j/2wA7v7N+ZvoQpCJRlVx5SWa4YaiDAxMBQwMDBUhJkI" |
| 160 | "MUBBDyMDAzsDRJwFxAdioBDDHAYEYAbiFUAM1M5wAIhFGCGKDIDYAogdgNgDiH2BOAiI0xghekDm" |
| 161 | "sQIxGxQzM6ACRijNhCbOhCZfyohdPYyuh8szgtVkMkLsLhAAqeCDi+ejibPZZOZlltgxsDnqZSWW" |
| 162 | "JTKwOUFoZh9HayDhZM0g5AMS0M9JzEvX90/KSk0usWZgDAMaws5nAyXBzmpoYGlgAjsAyJoBMp0b" |
| 163 | "zQ8gGhbOTEhhzYwU3qxIYc2GFN6MClC/AhUyKUDMAYU9M1Qc5F8GKBscVgIQM0FxCwBQSwECHgMU" |
| 164 | "AAAACADojmFLPcugSwoBAAAUAgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAA/Ns" |
| 165 | "+ll1eAsAAQQj5QIABIgTAABQSwUGAAAAAAEAAQBRAAAATwEAAAAA"; |
| 166 | |
Andreas Gampe | 1b8700f | 2018-11-01 14:47:00 -0700 | [diff] [blame] | 167 | class DexoptTestEnvTest : public testing::Test { |
| 168 | }; |
| 169 | |
| 170 | TEST_F(DexoptTestEnvTest, CheckSelinux) { |
| 171 | ASSERT_EQ(1, is_selinux_enabled()); |
| 172 | |
| 173 | // Crude cutout for virtual devices. |
| 174 | #if !defined(__i386__) && !defined(__x86_64__) |
| 175 | constexpr bool kIsX86 = false; |
| 176 | #else |
| 177 | constexpr bool kIsX86 = true; |
| 178 | #endif |
Andreas Gampe | 1d1e7c9 | 2018-11-05 09:46:28 -0800 | [diff] [blame] | 179 | ASSERT_TRUE(1 == security_getenforce() || kIsX86 || true /* b/119032200 */); |
Andreas Gampe | 1b8700f | 2018-11-01 14:47:00 -0700 | [diff] [blame] | 180 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 181 | |
| 182 | class DexoptTest : public testing::Test { |
| 183 | protected: |
| 184 | static constexpr bool kDebug = false; |
| 185 | static constexpr uid_t kSystemUid = 1000; |
| 186 | static constexpr uid_t kSystemGid = 1000; |
| 187 | static constexpr int32_t kOSdkVersion = 25; |
| 188 | static constexpr int32_t kAppDataFlags = FLAG_STORAGE_CE | FLAG_STORAGE_DE; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 189 | static constexpr int32_t kTestUserId = 0; |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 190 | static constexpr uid_t kTestAppId = 19999; |
| 191 | |
| 192 | const gid_t kTestAppUid = multiuser_get_uid(kTestUserId, kTestAppId); |
| 193 | const uid_t kTestAppGid = multiuser_get_shared_gid(kTestUserId, kTestAppId); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 194 | |
| 195 | InstalldNativeService* service_; |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 196 | std::optional<std::string> volume_uuid_; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 197 | std::string package_name_; |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 198 | std::string apk_path_; |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 199 | std::string empty_dm_file_; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 200 | std::string app_apk_dir_; |
| 201 | std::string app_private_dir_ce_; |
| 202 | std::string app_private_dir_de_; |
| 203 | std::string se_info_; |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 204 | std::string app_oat_dir_; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 205 | |
| 206 | int64_t ce_data_inode_; |
| 207 | |
| 208 | std::string secondary_dex_ce_; |
| 209 | std::string secondary_dex_ce_link_; |
| 210 | std::string secondary_dex_de_; |
| 211 | |
| 212 | virtual void SetUp() { |
| 213 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
| 214 | android::base::InitLogging(nullptr); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 215 | // Initialize the globals holding the file system main paths (/data/, /system/ etc..). |
| 216 | // This is needed in order to compute the application and profile paths. |
| 217 | ASSERT_TRUE(init_globals_from_data_and_root()); |
| 218 | // Initialize selinux log callbacks. |
| 219 | // This ensures that selinux is up and running and re-directs the selinux messages |
| 220 | // to logcat (in order to make it easier to investigate test results). |
| 221 | ASSERT_TRUE(init_selinux()); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 222 | service_ = new InstalldNativeService(); |
| 223 | |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 224 | volume_uuid_ = std::nullopt; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 225 | package_name_ = "com.installd.test.dexopt"; |
| 226 | se_info_ = "default"; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 227 | app_apk_dir_ = android_app_dir + package_name_; |
| 228 | |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 229 | ASSERT_TRUE(create_mock_app()); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | virtual void TearDown() { |
| 233 | if (!kDebug) { |
| 234 | service_->destroyAppData( |
| 235 | volume_uuid_, package_name_, kTestUserId, kAppDataFlags, ce_data_inode_); |
| 236 | run_cmd("rm -rf " + app_apk_dir_); |
| 237 | run_cmd("rm -rf " + app_private_dir_ce_); |
| 238 | run_cmd("rm -rf " + app_private_dir_de_); |
| 239 | } |
| 240 | delete service_; |
| 241 | } |
| 242 | |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 243 | ::testing::AssertionResult create_mock_app() { |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 244 | // For debug mode, the directory might already exist. Avoid erroring out. |
| 245 | if (mkdir(app_apk_dir_, kSystemUid, kSystemGid, 0755) != 0 && !kDebug) { |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 246 | return ::testing::AssertionFailure() << "Could not create app dir " << app_apk_dir_ |
| 247 | << " : " << strerror(errno); |
| 248 | } |
Alex Buynytskyy | 17d8f82 | 2019-06-20 16:29:57 -0700 | [diff] [blame] | 249 | |
| 250 | // Initialize the oat dir path. |
| 251 | app_oat_dir_ = app_apk_dir_ + "/oat"; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 252 | |
| 253 | // Copy the primary apk. |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 254 | apk_path_ = app_apk_dir_ + "/base.jar"; |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 255 | std::string error_msg; |
| 256 | if (!WriteBase64ToFile(kDexFile, apk_path_, kSystemUid, kSystemGid, 0644, &error_msg)) { |
| 257 | return ::testing::AssertionFailure() << "Could not write base64 file to " << apk_path_ |
| 258 | << " : " << error_msg; |
| 259 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 260 | |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 261 | // Create an empty dm file. |
| 262 | empty_dm_file_ = apk_path_ + ".dm"; |
| 263 | { |
| 264 | int fd = open(empty_dm_file_.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| 265 | if (fd < 0) { |
| 266 | return ::testing::AssertionFailure() << "Could not open " << empty_dm_file_; |
| 267 | } |
| 268 | FILE* file = fdopen(fd, "wb"); |
| 269 | if (file == nullptr) { |
| 270 | return ::testing::AssertionFailure() << "Null file for " << empty_dm_file_ |
| 271 | << " fd=" << fd; |
| 272 | } |
| 273 | ZipWriter writer(file); |
| 274 | // Add vdex to zip. |
| 275 | writer.StartEntry("primary.prof", ZipWriter::kCompress); |
| 276 | writer.FinishEntry(); |
| 277 | writer.Finish(); |
Josh Gao | 1197f72 | 2019-08-09 14:33:34 -0700 | [diff] [blame] | 278 | fclose(file); |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 279 | } |
| 280 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 281 | // Create the app user data. |
Alex Buynytskyy | 17d8f82 | 2019-06-20 16:29:57 -0700 | [diff] [blame] | 282 | binder::Status status = service_->createAppData( |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 283 | volume_uuid_, |
| 284 | package_name_, |
| 285 | kTestUserId, |
| 286 | kAppDataFlags, |
| 287 | kTestAppUid, |
| 288 | se_info_, |
| 289 | kOSdkVersion, |
| 290 | &ce_data_inode_); |
| 291 | if (!status.isOk()) { |
| 292 | return ::testing::AssertionFailure() << "Could not create app data: " |
| 293 | << status.toString8().c_str(); |
| 294 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 295 | |
| 296 | // Create a secondary dex file on CE storage |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 297 | const char* volume_uuid_cstr = volume_uuid_ ? volume_uuid_->c_str() : nullptr; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 298 | app_private_dir_ce_ = create_data_user_ce_package_path( |
| 299 | volume_uuid_cstr, kTestUserId, package_name_.c_str()); |
| 300 | secondary_dex_ce_ = app_private_dir_ce_ + "/secondary_ce.jar"; |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 301 | if (!WriteBase64ToFile(kDexFile, |
| 302 | secondary_dex_ce_, |
| 303 | kTestAppUid, |
| 304 | kTestAppGid, |
| 305 | 0600, |
| 306 | &error_msg)) { |
| 307 | return ::testing::AssertionFailure() << "Could not write base64 file to " |
| 308 | << secondary_dex_ce_ << " : " << error_msg; |
| 309 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 310 | std::string app_private_dir_ce_link = create_data_user_ce_package_path_as_user_link( |
| 311 | volume_uuid_cstr, kTestUserId, package_name_.c_str()); |
| 312 | secondary_dex_ce_link_ = app_private_dir_ce_link + "/secondary_ce.jar"; |
| 313 | |
| 314 | // Create a secondary dex file on DE storage. |
| 315 | app_private_dir_de_ = create_data_user_de_package_path( |
| 316 | volume_uuid_cstr, kTestUserId, package_name_.c_str()); |
| 317 | secondary_dex_de_ = app_private_dir_de_ + "/secondary_de.jar"; |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 318 | if (!WriteBase64ToFile(kDexFile, |
| 319 | secondary_dex_de_, |
| 320 | kTestAppUid, |
| 321 | kTestAppGid, |
| 322 | 0600, |
| 323 | &error_msg)) { |
| 324 | return ::testing::AssertionFailure() << "Could not write base64 file to " |
| 325 | << secondary_dex_de_ << " : " << error_msg; |
| 326 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 327 | |
| 328 | // Fix app data uid. |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 329 | status = service_->fixupAppData(volume_uuid_, kTestUserId); |
| 330 | if (!status.isOk()) { |
| 331 | return ::testing::AssertionFailure() << "Could not fixup app data: " |
| 332 | << status.toString8().c_str(); |
| 333 | } |
| 334 | |
| 335 | return ::testing::AssertionSuccess(); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 339 | std::string GetSecondaryDexArtifact(const std::string& path, const std::string& type) { |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 340 | std::string::size_type end = path.rfind('.'); |
| 341 | std::string::size_type start = path.rfind('/', end); |
| 342 | return path.substr(0, start) + "/oat/" + kRuntimeIsa + "/" + |
| 343 | path.substr(start + 1, end - start) + type; |
| 344 | } |
| 345 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 346 | void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 347 | bool should_binder_call_succeed, bool should_dex_be_compiled = true, |
David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 348 | /*out */ binder::Status* binder_result = nullptr, int32_t uid = -1, |
| 349 | const char* class_loader_context = nullptr) { |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 350 | if (uid == -1) { |
| 351 | uid = kTestAppUid; |
| 352 | } |
David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 353 | if (class_loader_context == nullptr) { |
| 354 | class_loader_context = "&"; |
| 355 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 356 | int32_t dexopt_needed = 0; // does not matter; |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 357 | std::optional<std::string> out_path; // does not matter |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 358 | int32_t dex_flags = DEXOPT_SECONDARY_DEX | dex_storage_flag; |
| 359 | std::string compiler_filter = "speed-profile"; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 360 | bool downgrade = false; |
David Brazdil | 570d398 | 2018-01-16 20:15:43 +0000 | [diff] [blame] | 361 | int32_t target_sdk_version = 0; // default |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 362 | std::optional<std::string> profile_name; |
| 363 | std::optional<std::string> dm_path; |
| 364 | std::optional<std::string> compilation_reason; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 365 | |
| 366 | binder::Status result = service_->dexopt(path, |
| 367 | uid, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 368 | package_name_, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 369 | kRuntimeIsa, |
| 370 | dexopt_needed, |
| 371 | out_path, |
| 372 | dex_flags, |
| 373 | compiler_filter, |
| 374 | volume_uuid_, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 375 | class_loader_context, |
| 376 | se_info_, |
David Brazdil | 570d398 | 2018-01-16 20:15:43 +0000 | [diff] [blame] | 377 | downgrade, |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 378 | target_sdk_version, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 379 | profile_name, |
| 380 | dm_path, |
| 381 | compilation_reason); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 382 | ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str(); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 383 | int expected_access = should_dex_be_compiled ? 0 : -1; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 384 | std::string odex = GetSecondaryDexArtifact(path, "odex"); |
| 385 | std::string vdex = GetSecondaryDexArtifact(path, "vdex"); |
| 386 | std::string art = GetSecondaryDexArtifact(path, "art"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 387 | ASSERT_EQ(expected_access, access(odex.c_str(), R_OK)); |
| 388 | ASSERT_EQ(expected_access, access(vdex.c_str(), R_OK)); |
| 389 | ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image. |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 390 | if (binder_result != nullptr) { |
| 391 | *binder_result = result; |
| 392 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void reconcile_secondary_dex(const std::string& path, int32_t storage_flag, |
| 396 | bool should_binder_call_succeed, bool should_dex_exist, bool should_dex_be_deleted, |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 397 | int32_t uid = -1, std::string* package_override = nullptr) { |
| 398 | if (uid == -1) { |
| 399 | uid = kTestAppUid; |
| 400 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 401 | std::vector<std::string> isas; |
| 402 | isas.push_back(kRuntimeIsa); |
| 403 | bool out_secondary_dex_exists = false; |
| 404 | binder::Status result = service_->reconcileSecondaryDexFile( |
| 405 | path, |
| 406 | package_override == nullptr ? package_name_ : *package_override, |
| 407 | uid, |
| 408 | isas, |
| 409 | volume_uuid_, |
| 410 | storage_flag, |
| 411 | &out_secondary_dex_exists); |
| 412 | |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 413 | ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str(); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 414 | ASSERT_EQ(should_dex_exist, out_secondary_dex_exists); |
| 415 | |
| 416 | int expected_access = should_dex_be_deleted ? -1 : 0; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 417 | std::string odex = GetSecondaryDexArtifact(path, "odex"); |
| 418 | std::string vdex = GetSecondaryDexArtifact(path, "vdex"); |
| 419 | std::string art = GetSecondaryDexArtifact(path, "art"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 420 | ASSERT_EQ(expected_access, access(odex.c_str(), F_OK)); |
| 421 | ASSERT_EQ(expected_access, access(vdex.c_str(), F_OK)); |
| 422 | ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image. |
| 423 | } |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 424 | |
| 425 | void CheckFileAccess(const std::string& file, uid_t uid, gid_t gid, mode_t mode) { |
| 426 | struct stat st; |
| 427 | ASSERT_EQ(0, stat(file.c_str(), &st)); |
| 428 | ASSERT_EQ(uid, st.st_uid); |
| 429 | ASSERT_EQ(gid, st.st_gid); |
| 430 | ASSERT_EQ(mode, st.st_mode); |
| 431 | } |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 432 | |
| 433 | void CompilePrimaryDexOk(std::string compiler_filter, |
| 434 | int32_t dex_flags, |
| 435 | const char* oat_dir, |
| 436 | int32_t uid, |
| 437 | int32_t dexopt_needed, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 438 | binder::Status* binder_result = nullptr, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 439 | const char* dm_path = nullptr, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 440 | bool downgrade = false) { |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 441 | CompilePrimaryDex(compiler_filter, |
| 442 | dex_flags, |
| 443 | oat_dir, |
| 444 | uid, |
| 445 | dexopt_needed, |
| 446 | dm_path, |
| 447 | downgrade, |
| 448 | true, |
| 449 | binder_result); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void CompilePrimaryDexFail(std::string compiler_filter, |
| 453 | int32_t dex_flags, |
| 454 | const char* oat_dir, |
| 455 | int32_t uid, |
| 456 | int32_t dexopt_needed, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 457 | binder::Status* binder_result = nullptr, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 458 | const char* dm_path = nullptr, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 459 | bool downgrade = false) { |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 460 | CompilePrimaryDex(compiler_filter, |
| 461 | dex_flags, |
| 462 | oat_dir, |
| 463 | uid, |
| 464 | dexopt_needed, |
| 465 | dm_path, |
| 466 | downgrade, |
| 467 | false, |
| 468 | binder_result); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void CompilePrimaryDex(std::string compiler_filter, |
| 472 | int32_t dex_flags, |
| 473 | const char* oat_dir, |
| 474 | int32_t uid, |
| 475 | int32_t dexopt_needed, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 476 | const char* dm_path, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 477 | bool downgrade, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 478 | bool should_binder_call_succeed, |
| 479 | /*out */ binder::Status* binder_result) { |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 480 | std::optional<std::string> out_path = oat_dir ? std::make_optional<std::string>(oat_dir) : std::nullopt; |
| 481 | std::string class_loader_context = "&"; |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 482 | int32_t target_sdk_version = 0; // default |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 483 | std::string profile_name = "primary.prof"; |
| 484 | std::optional<std::string> dm_path_opt = dm_path ? std::make_optional<std::string>(dm_path) : std::nullopt; |
| 485 | std::string compilation_reason = "test-reason"; |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 486 | |
| 487 | bool prof_result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 488 | ASSERT_BINDER_SUCCESS(service_->prepareAppProfile( |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 489 | package_name_, kTestUserId, kTestAppId, profile_name, apk_path_, |
| 490 | dm_path_opt, &prof_result)); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 491 | ASSERT_TRUE(prof_result); |
| 492 | |
| 493 | binder::Status result = service_->dexopt(apk_path_, |
| 494 | uid, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 495 | package_name_, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 496 | kRuntimeIsa, |
| 497 | dexopt_needed, |
| 498 | out_path, |
| 499 | dex_flags, |
| 500 | compiler_filter, |
| 501 | volume_uuid_, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 502 | class_loader_context, |
| 503 | se_info_, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 504 | downgrade, |
| 505 | target_sdk_version, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 506 | profile_name, |
| 507 | dm_path_opt, |
| 508 | compilation_reason); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 509 | ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str(); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 510 | |
| 511 | if (!should_binder_call_succeed) { |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 512 | if (binder_result != nullptr) { |
| 513 | *binder_result = result; |
| 514 | } |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 515 | return; |
| 516 | } |
| 517 | // Check the access to the compiler output. |
| 518 | // - speed-profile artifacts are not world-wide readable. |
| 519 | // - files are owned by the system uid. |
| 520 | std::string odex = GetPrimaryDexArtifact(oat_dir, apk_path_, "odex"); |
| 521 | std::string vdex = GetPrimaryDexArtifact(oat_dir, apk_path_, "vdex"); |
| 522 | std::string art = GetPrimaryDexArtifact(oat_dir, apk_path_, "art"); |
| 523 | |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 524 | bool is_public = (dex_flags & DEXOPT_PUBLIC) != 0; |
| 525 | mode_t mode = S_IFREG | (is_public ? 0644 : 0640); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 526 | CheckFileAccess(odex, kSystemUid, uid, mode); |
| 527 | CheckFileAccess(vdex, kSystemUid, uid, mode); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 528 | |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 529 | if (compiler_filter == "speed-profile") { |
| 530 | CheckFileAccess(art, kSystemUid, uid, mode); |
| 531 | } |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 532 | if (binder_result != nullptr) { |
| 533 | *binder_result = result; |
| 534 | } |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | std::string GetPrimaryDexArtifact(const char* oat_dir, |
| 538 | const std::string& dex_path, |
| 539 | const std::string& type) { |
| 540 | if (oat_dir == nullptr) { |
| 541 | std::string path = dex_path; |
| 542 | for (auto it = path.begin() + 1; it < path.end(); ++it) { |
| 543 | if (*it == '/') { |
| 544 | *it = '@'; |
| 545 | } |
| 546 | } |
| 547 | return android_data_dir + DALVIK_CACHE + '/' + kRuntimeIsa + "/" + path |
| 548 | + "@classes.dex"; |
| 549 | } else { |
| 550 | std::string::size_type name_end = dex_path.rfind('.'); |
| 551 | std::string::size_type name_start = dex_path.rfind('/'); |
| 552 | return std::string(oat_dir) + "/" + kRuntimeIsa + "/" + |
| 553 | dex_path.substr(name_start + 1, name_end - name_start) + type; |
| 554 | } |
| 555 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 556 | }; |
| 557 | |
| 558 | |
| 559 | TEST_F(DexoptTest, DexoptSecondaryCe) { |
| 560 | LOG(INFO) << "DexoptSecondaryCe"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 561 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 562 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 563 | } |
| 564 | |
| 565 | TEST_F(DexoptTest, DexoptSecondaryCeLink) { |
| 566 | LOG(INFO) << "DexoptSecondaryCeLink"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 567 | CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 568 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 569 | } |
| 570 | |
David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 571 | TEST_F(DexoptTest, DexoptSecondaryCeWithContext) { |
| 572 | LOG(INFO) << "DexoptSecondaryCeWithContext"; |
| 573 | std::string class_loader_context = "PCL[" + secondary_dex_ce_ + "]"; |
| 574 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
| 575 | /*binder_ok*/ true, /*compile_ok*/ true, nullptr, -1, class_loader_context.c_str()); |
| 576 | } |
| 577 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 578 | TEST_F(DexoptTest, DexoptSecondaryDe) { |
| 579 | LOG(INFO) << "DexoptSecondaryDe"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 580 | CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 581 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 582 | } |
| 583 | |
David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 584 | TEST_F(DexoptTest, DexoptSecondaryDeWithContext) { |
| 585 | LOG(INFO) << "DexoptSecondaryDeWithContext"; |
| 586 | std::string class_loader_context = "PCL[" + secondary_dex_de_ + "]"; |
| 587 | CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE, |
| 588 | /*binder_ok*/ true, /*compile_ok*/ true, nullptr, -1, class_loader_context.c_str()); |
| 589 | } |
| 590 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 591 | TEST_F(DexoptTest, DexoptSecondaryDoesNotExist) { |
| 592 | LOG(INFO) << "DexoptSecondaryDoesNotExist"; |
| 593 | // If the file validates but does not exist we do not treat it as an error. |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 594 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 595 | CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 596 | /*binder_ok*/ true, /*compile_ok*/ false, &status); |
| 597 | EXPECT_STREQ(status.toString8().c_str(), "No error"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) { |
| 601 | LOG(INFO) << "DexoptSecondaryStorageValidationError"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 602 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 603 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 604 | /*binder_ok*/ false, /*compile_ok*/ false, &status); |
Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 605 | EXPECT_STREQ(status.toString8().c_str(), |
Martijn Coenen | 290de46 | 2019-03-21 13:14:59 +0100 | [diff] [blame] | 606 | "Status(-8, EX_SERVICE_SPECIFIC): '-1: Dexoptanalyzer path validation failed'"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) { |
| 610 | LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 611 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 612 | CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 613 | /*binder_ok*/ false, /*compile_ok*/ false, &status); |
Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 614 | EXPECT_STREQ(status.toString8().c_str(), |
Martijn Coenen | 290de46 | 2019-03-21 13:14:59 +0100 | [diff] [blame] | 615 | "Status(-8, EX_SERVICE_SPECIFIC): '-1: Dexoptanalyzer path validation failed'"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) { |
| 619 | LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 620 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 621 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 622 | /*binder_ok*/ false, /*compile_ok*/ false, &status, kSystemUid); |
Martijn Coenen | 290de46 | 2019-03-21 13:14:59 +0100 | [diff] [blame] | 623 | EXPECT_STREQ(status.toString8().c_str(), |
| 624 | "Status(-8, EX_SERVICE_SPECIFIC): '-1: Dexoptanalyzer open zip failed'"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 627 | TEST_F(DexoptTest, DexoptPrimaryPublic) { |
| 628 | LOG(INFO) << "DexoptPrimaryPublic"; |
| 629 | CompilePrimaryDexOk("verify", |
| 630 | DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC, |
| 631 | app_oat_dir_.c_str(), |
| 632 | kTestAppGid, |
| 633 | DEX2OAT_FROM_SCRATCH); |
| 634 | } |
| 635 | |
Alex Buynytskyy | 17d8f82 | 2019-06-20 16:29:57 -0700 | [diff] [blame] | 636 | TEST_F(DexoptTest, DexoptPrimaryPublicCreateOatDir) { |
| 637 | LOG(INFO) << "DexoptPrimaryPublic"; |
| 638 | ASSERT_BINDER_SUCCESS(service_->createOatDir(app_oat_dir_, kRuntimeIsa)); |
| 639 | CompilePrimaryDexOk("verify", |
| 640 | DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC, |
| 641 | app_oat_dir_.c_str(), |
| 642 | kTestAppGid, |
| 643 | DEX2OAT_FROM_SCRATCH); |
| 644 | } |
| 645 | |
Patrick Baumann | 2271b3e | 2020-04-14 17:03:00 -0700 | [diff] [blame] | 646 | TEST_F(DexoptTest, DexoptPrimaryPublicRestore) { |
| 647 | LOG(INFO) << "DexoptPrimaryPublicRestore"; |
| 648 | CompilePrimaryDexOk("verify", |
| 649 | DEXOPT_FOR_RESTORE | DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC, |
| 650 | app_oat_dir_.c_str(), |
| 651 | kTestAppGid, |
| 652 | DEX2OAT_FROM_SCRATCH); |
| 653 | } |
| 654 | |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 655 | TEST_F(DexoptTest, DexoptPrimaryFailedInvalidFilter) { |
| 656 | LOG(INFO) << "DexoptPrimaryFailedInvalidFilter"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 657 | binder::Status status; |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 658 | CompilePrimaryDexFail("awesome-filter", |
| 659 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC, |
| 660 | app_oat_dir_.c_str(), |
| 661 | kTestAppGid, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 662 | DEX2OAT_FROM_SCRATCH, |
| 663 | &status); |
| 664 | EXPECT_STREQ(status.toString8().c_str(), |
Martijn Coenen | 290de46 | 2019-03-21 13:14:59 +0100 | [diff] [blame] | 665 | "Status(-8, EX_SERVICE_SPECIFIC): \'256: Dex2oat invocation for " |
Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 666 | "/data/app/com.installd.test.dexopt/base.jar failed: unspecified dex2oat error'"); |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 669 | TEST_F(DexoptTest, DexoptPrimaryProfileNonPublic) { |
| 670 | LOG(INFO) << "DexoptPrimaryProfileNonPublic"; |
| 671 | CompilePrimaryDexOk("speed-profile", |
Mathieu Chartier | 2d5a1bc | 2018-04-03 14:55:43 -0700 | [diff] [blame] | 672 | DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_GENERATE_APP_IMAGE, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 673 | app_oat_dir_.c_str(), |
| 674 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 675 | DEX2OAT_FROM_SCRATCH, |
| 676 | /*binder_result=*/nullptr, |
| 677 | empty_dm_file_.c_str()); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | TEST_F(DexoptTest, DexoptPrimaryProfilePublic) { |
| 681 | LOG(INFO) << "DexoptPrimaryProfilePublic"; |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 682 | CompilePrimaryDexOk("speed-profile", |
Mathieu Chartier | 2d5a1bc | 2018-04-03 14:55:43 -0700 | [diff] [blame] | 683 | DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_PUBLIC | |
| 684 | DEXOPT_GENERATE_APP_IMAGE, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 685 | app_oat_dir_.c_str(), |
| 686 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 687 | DEX2OAT_FROM_SCRATCH, |
| 688 | /*binder_result=*/nullptr, |
| 689 | empty_dm_file_.c_str()); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) { |
| 693 | LOG(INFO) << "DexoptPrimaryBackgroundOk"; |
| 694 | CompilePrimaryDexOk("speed-profile", |
Mathieu Chartier | 2d5a1bc | 2018-04-03 14:55:43 -0700 | [diff] [blame] | 695 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED | |
| 696 | DEXOPT_GENERATE_APP_IMAGE, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 697 | app_oat_dir_.c_str(), |
| 698 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 699 | DEX2OAT_FROM_SCRATCH, |
| 700 | /*binder_result=*/nullptr, |
| 701 | empty_dm_file_.c_str()); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 704 | TEST_F(DexoptTest, ResolveStartupConstStrings) { |
| 705 | LOG(INFO) << "DexoptDex2oatResolveStartupStrings"; |
| 706 | const std::string property = "persist.device_config.runtime.dex2oat_resolve_startup_strings"; |
| 707 | const std::string previous_value = android::base::GetProperty(property, ""); |
| 708 | auto restore_property = android::base::make_scope_guard([=]() { |
| 709 | android::base::SetProperty(property, previous_value); |
| 710 | }); |
| 711 | std::string odex = GetPrimaryDexArtifact(app_oat_dir_.c_str(), apk_path_, "odex"); |
| 712 | // Disable the property to start. |
| 713 | bool found_disable = false; |
| 714 | ASSERT_TRUE(android::base::SetProperty(property, "false")) << property; |
| 715 | CompilePrimaryDexOk("speed-profile", |
| 716 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED | |
| 717 | DEXOPT_GENERATE_APP_IMAGE, |
| 718 | app_oat_dir_.c_str(), |
| 719 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 720 | DEX2OAT_FROM_SCRATCH, |
| 721 | /*binder_result=*/nullptr, |
| 722 | empty_dm_file_.c_str()); |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 723 | run_cmd_and_process_output( |
| 724 | "oatdump --header-only --oat-file=" + odex, |
| 725 | [&](const std::string& line) { |
| 726 | if (line.find("--resolve-startup-const-strings=false") != std::string::npos) { |
| 727 | found_disable = true; |
| 728 | } |
| 729 | }); |
| 730 | EXPECT_TRUE(found_disable); |
| 731 | // Enable the property and inspect that .art artifact is larger. |
| 732 | bool found_enable = false; |
| 733 | ASSERT_TRUE(android::base::SetProperty(property, "true")) << property; |
| 734 | CompilePrimaryDexOk("speed-profile", |
| 735 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED | |
| 736 | DEXOPT_GENERATE_APP_IMAGE, |
| 737 | app_oat_dir_.c_str(), |
| 738 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 739 | DEX2OAT_FROM_SCRATCH, |
| 740 | /*binder_result=*/nullptr, |
| 741 | empty_dm_file_.c_str()); |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 742 | run_cmd_and_process_output( |
| 743 | "oatdump --header-only --oat-file=" + odex, |
| 744 | [&](const std::string& line) { |
| 745 | if (line.find("--resolve-startup-const-strings=true") != std::string::npos) { |
| 746 | found_enable = true; |
| 747 | } |
| 748 | }); |
| 749 | EXPECT_TRUE(found_enable); |
| 750 | } |
| 751 | |
David Sehr | 6df89a6 | 2020-04-15 20:43:48 -0700 | [diff] [blame] | 752 | TEST_F(DexoptTest, DexoptDex2oat64Enabled) { |
| 753 | LOG(INFO) << "DexoptDex2oat64Enabled"; |
| 754 | const std::string property = "dalvik.vm.dex2oat64.enabled"; |
| 755 | const std::string previous_value = android::base::GetProperty(property, ""); |
| 756 | auto restore_property = android::base::make_scope_guard([=]() { |
| 757 | android::base::SetProperty(property, previous_value); |
| 758 | }); |
| 759 | std::string odex = GetPrimaryDexArtifact(app_oat_dir_.c_str(), apk_path_, "odex"); |
| 760 | // Disable the property and use dex2oat32. |
| 761 | ASSERT_TRUE(android::base::SetProperty(property, "false")) << property; |
| 762 | CompilePrimaryDexOk("speed-profile", |
| 763 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED | |
| 764 | DEXOPT_GENERATE_APP_IMAGE, |
| 765 | app_oat_dir_.c_str(), |
| 766 | kTestAppGid, |
| 767 | DEX2OAT_FROM_SCRATCH, |
| 768 | /*binder_result=*/nullptr, |
| 769 | empty_dm_file_.c_str()); |
| 770 | // Enable the property and use dex2oat64. |
| 771 | ASSERT_TRUE(android::base::SetProperty(property, "true")) << property; |
| 772 | CompilePrimaryDexOk("speed-profile", |
| 773 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED | |
| 774 | DEXOPT_GENERATE_APP_IMAGE, |
| 775 | app_oat_dir_.c_str(), |
| 776 | kTestAppGid, |
| 777 | DEX2OAT_FROM_SCRATCH, |
| 778 | /*binder_result=*/nullptr, |
| 779 | empty_dm_file_.c_str()); |
| 780 | } |
| 781 | |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 782 | class PrimaryDexReCompilationTest : public DexoptTest { |
| 783 | public: |
| 784 | virtual void SetUp() { |
| 785 | DexoptTest::SetUp(); |
| 786 | CompilePrimaryDexOk("verify", |
| 787 | DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC, |
| 788 | app_oat_dir_.c_str(), |
| 789 | kTestAppGid, |
| 790 | DEX2OAT_FROM_SCRATCH); |
| 791 | std::string odex = GetSecondaryDexArtifact(apk_path_, "odex"); |
| 792 | std::string vdex = GetSecondaryDexArtifact(apk_path_, "vdex"); |
| 793 | |
| 794 | first_compilation_odex_fd_.reset(open(odex.c_str(), O_RDONLY)); |
| 795 | first_compilation_vdex_fd_.reset(open(vdex.c_str(), O_RDONLY)); |
| 796 | } |
| 797 | |
| 798 | virtual void TearDown() { |
| 799 | first_compilation_odex_fd_.reset(-1); |
| 800 | first_compilation_vdex_fd_.reset(-1); |
| 801 | DexoptTest::TearDown(); |
| 802 | } |
| 803 | |
| 804 | protected: |
| 805 | unique_fd first_compilation_odex_fd_; |
| 806 | unique_fd first_compilation_vdex_fd_; |
| 807 | }; |
| 808 | |
| 809 | TEST_F(PrimaryDexReCompilationTest, DexoptPrimaryUpdateInPlaceVdex) { |
| 810 | LOG(INFO) << "DexoptPrimaryUpdateInPlaceVdex"; |
| 811 | |
| 812 | CompilePrimaryDexOk("verify", |
| 813 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC, |
| 814 | app_oat_dir_.c_str(), |
| 815 | kTestAppGid, |
| 816 | DEX2OAT_FOR_BOOT_IMAGE); |
| 817 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 818 | |
| 819 | class ReconcileTest : public DexoptTest { |
| 820 | virtual void SetUp() { |
| 821 | DexoptTest::SetUp(); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 822 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 823 | /*binder_ok*/ true, /*compile_ok*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 824 | CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 825 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 826 | } |
| 827 | }; |
| 828 | |
| 829 | TEST_F(ReconcileTest, ReconcileSecondaryCeExists) { |
| 830 | LOG(INFO) << "ReconcileSecondaryCeExists"; |
| 831 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE, |
| 832 | /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false); |
| 833 | } |
| 834 | |
| 835 | TEST_F(ReconcileTest, ReconcileSecondaryCeLinkExists) { |
| 836 | LOG(INFO) << "ReconcileSecondaryCeLinkExists"; |
| 837 | reconcile_secondary_dex(secondary_dex_ce_link_, FLAG_STORAGE_CE, |
| 838 | /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false); |
| 839 | } |
| 840 | |
| 841 | TEST_F(ReconcileTest, ReconcileSecondaryDeExists) { |
| 842 | LOG(INFO) << "ReconcileSecondaryDeExists"; |
| 843 | reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE, |
| 844 | /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false); |
| 845 | } |
| 846 | |
| 847 | TEST_F(ReconcileTest, ReconcileSecondaryDeDoesNotExist) { |
| 848 | LOG(INFO) << "ReconcileSecondaryDeDoesNotExist"; |
| 849 | run_cmd("rm -rf " + secondary_dex_de_); |
| 850 | reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE, |
| 851 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ true); |
| 852 | } |
| 853 | |
| 854 | TEST_F(ReconcileTest, ReconcileSecondaryStorageValidationError) { |
| 855 | // Validation errors will not clean the odex/vdex/art files but will mark |
| 856 | // the file as non existent so that the PM knows it should purge it from its |
| 857 | // records. |
| 858 | LOG(INFO) << "ReconcileSecondaryStorageValidationError"; |
| 859 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_DE, |
| 860 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false); |
| 861 | } |
| 862 | |
| 863 | TEST_F(ReconcileTest, ReconcileSecondaryAppOwnershipValidationError) { |
| 864 | LOG(INFO) << "ReconcileSecondaryAppOwnershipValidationError"; |
| 865 | // Attempt to reconcile the dex files of the test app from a different app. |
| 866 | std::string another_app = "another.app"; |
| 867 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE, |
| 868 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid, &another_app); |
| 869 | } |
| 870 | |
| 871 | TEST_F(ReconcileTest, ReconcileSecondaryAcessViaDifferentUidError) { |
| 872 | LOG(INFO) << "ReconcileSecondaryAcessViaDifferentUidError"; |
| 873 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE, |
| 874 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid); |
| 875 | } |
| 876 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 877 | class ProfileTest : public DexoptTest { |
| 878 | protected: |
| 879 | std::string cur_profile_; |
| 880 | std::string ref_profile_; |
| 881 | std::string snap_profile_; |
| 882 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 883 | static constexpr const char* kPrimaryProfile = "primary.prof"; |
| 884 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 885 | virtual void SetUp() { |
| 886 | DexoptTest::SetUp(); |
| 887 | cur_profile_ = create_current_profile_path( |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 888 | kTestUserId, package_name_, kPrimaryProfile, /*is_secondary_dex*/ false); |
| 889 | ref_profile_ = create_reference_profile_path(package_name_, kPrimaryProfile, |
| 890 | /*is_secondary_dex*/ false); |
| 891 | snap_profile_ = create_snapshot_profile_path(package_name_, kPrimaryProfile); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 894 | void SetupProfile(const std::string& path, uid_t uid, gid_t gid, mode_t mode, |
| 895 | int32_t num_dex) { |
| 896 | run_cmd("profman --generate-test-profile-seed=" + std::to_string(num_dex) + |
| 897 | " --generate-test-profile-num-dex=" + std::to_string(num_dex) + |
| 898 | " --generate-test-profile=" + path); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 899 | ::chmod(path.c_str(), mode); |
| 900 | ::chown(path.c_str(), uid, gid); |
| 901 | } |
| 902 | |
| 903 | void SetupProfiles(bool setup_ref) { |
| 904 | SetupProfile(cur_profile_, kTestAppUid, kTestAppGid, 0600, 1); |
| 905 | if (setup_ref) { |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 906 | SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0600, 2); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 910 | void createProfileSnapshot(int32_t appid, const std::string& package_name, |
| 911 | bool expected_result) { |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 912 | bool result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 913 | ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot( |
| 914 | appid, package_name, kPrimaryProfile, apk_path_, &result)); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 915 | ASSERT_EQ(expected_result, result); |
| 916 | |
| 917 | if (!expected_result) { |
| 918 | // Do not check the files if we expect to fail. |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | // Check that the snapshot was created witht he expected acess flags. |
| 923 | CheckFileAccess(snap_profile_, kSystemUid, kSystemGid, 0600 | S_IFREG); |
| 924 | |
| 925 | // The snapshot should be equivalent to the merge of profiles. |
| 926 | std::string expected_profile_content = snap_profile_ + ".expected"; |
| 927 | run_cmd("rm -f " + expected_profile_content); |
| 928 | run_cmd("touch " + expected_profile_content); |
Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 929 | // We force merging when creating the expected profile to make sure |
| 930 | // that the random profiles do not affect the output. |
| 931 | run_cmd("profman --force-merge --profile-file=" + cur_profile_ + |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 932 | " --profile-file=" + ref_profile_ + |
Calin Juravle | 40b5f46 | 2018-02-07 04:11:52 -0800 | [diff] [blame] | 933 | " --reference-profile-file=" + expected_profile_content + |
| 934 | " --apk=" + apk_path_); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 935 | |
| 936 | ASSERT_TRUE(AreFilesEqual(expected_profile_content, snap_profile_)); |
| 937 | |
| 938 | pid_t pid = fork(); |
| 939 | if (pid == 0) { |
| 940 | /* child */ |
| 941 | TransitionToSystemServer(); |
| 942 | |
| 943 | // System server should be able to open the the spanshot. |
| 944 | unique_fd fd(open(snap_profile_.c_str(), O_RDONLY)); |
| 945 | ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno); |
| 946 | _exit(0); |
| 947 | } |
| 948 | /* parent */ |
| 949 | ASSERT_TRUE(WIFEXITED(wait_child(pid))); |
| 950 | } |
| 951 | |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 952 | void mergePackageProfiles(const std::string& package_name, |
| 953 | const std::string& code_path, |
| 954 | bool expected_result) { |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 955 | bool result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 956 | ASSERT_BINDER_SUCCESS(service_->mergeProfiles( |
| 957 | kTestAppUid, package_name, code_path, &result)); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 958 | ASSERT_EQ(expected_result, result); |
| 959 | |
| 960 | if (!expected_result) { |
| 961 | // Do not check the files if we expect to fail. |
| 962 | return; |
| 963 | } |
| 964 | |
| 965 | // Check that the snapshot was created witht he expected acess flags. |
| 966 | CheckFileAccess(ref_profile_, kTestAppUid, kTestAppUid, 0600 | S_IFREG); |
| 967 | |
| 968 | // The snapshot should be equivalent to the merge of profiles. |
| 969 | std::string ref_profile_content = ref_profile_ + ".expected"; |
| 970 | run_cmd("rm -f " + ref_profile_content); |
| 971 | run_cmd("touch " + ref_profile_content); |
| 972 | run_cmd("profman --profile-file=" + cur_profile_ + |
| 973 | " --profile-file=" + ref_profile_ + |
| 974 | " --reference-profile-file=" + ref_profile_content); |
| 975 | |
| 976 | ASSERT_TRUE(AreFilesEqual(ref_profile_content, ref_profile_)); |
| 977 | } |
| 978 | |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 979 | // TODO(calin): add dex metadata tests once the ART change is merged. |
| 980 | void preparePackageProfile(const std::string& package_name, const std::string& profile_name, |
| 981 | bool expected_result) { |
| 982 | bool result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 983 | ASSERT_BINDER_SUCCESS(service_->prepareAppProfile( |
Jeff Sharkey | 1ebe77f | 2018-02-27 11:40:45 -0700 | [diff] [blame] | 984 | package_name, kTestUserId, kTestAppId, profile_name, apk_path_, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 985 | /*dex_metadata*/ {}, &result)); |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 986 | ASSERT_EQ(expected_result, result); |
| 987 | |
| 988 | if (!expected_result) { |
| 989 | // Do not check the files if we expect to fail. |
| 990 | return; |
| 991 | } |
| 992 | |
| 993 | std::string code_path_cur_prof = create_current_profile_path( |
| 994 | kTestUserId, package_name, profile_name, /*is_secondary_dex*/ false); |
| 995 | std::string code_path_ref_profile = create_reference_profile_path(package_name, |
| 996 | profile_name, /*is_secondary_dex*/ false); |
| 997 | |
| 998 | // Check that we created the current profile. |
| 999 | CheckFileAccess(code_path_cur_prof, kTestAppUid, kTestAppUid, 0600 | S_IFREG); |
| 1000 | |
| 1001 | // Without dex metadata we don't generate a reference profile. |
| 1002 | ASSERT_EQ(-1, access(code_path_ref_profile.c_str(), R_OK)); |
| 1003 | } |
| 1004 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1005 | protected: |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1006 | void TransitionToSystemServer() { |
| 1007 | ASSERT_TRUE(DropCapabilities(kSystemUid, kSystemGid)); |
Andreas Gampe | f04b0b4 | 2019-03-25 11:20:57 -0700 | [diff] [blame] | 1008 | int32_t res = selinux_android_setcon("u:r:system_server:s0"); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1009 | ASSERT_EQ(0, res) << "Failed to setcon " << strerror(errno); |
| 1010 | } |
| 1011 | |
| 1012 | bool AreFilesEqual(const std::string& file1, const std::string& file2) { |
| 1013 | std::vector<uint8_t> content1; |
| 1014 | std::vector<uint8_t> content2; |
| 1015 | |
| 1016 | if (!ReadAll(file1, &content1)) return false; |
| 1017 | if (!ReadAll(file2, &content2)) return false; |
| 1018 | return content1 == content2; |
| 1019 | } |
| 1020 | |
| 1021 | bool ReadAll(const std::string& file, std::vector<uint8_t>* content) { |
| 1022 | unique_fd fd(open(file.c_str(), O_RDONLY)); |
| 1023 | if (fd < 0) { |
| 1024 | PLOG(ERROR) << "Failed to open " << file; |
| 1025 | return false; |
| 1026 | } |
| 1027 | struct stat st; |
| 1028 | if (fstat(fd, &st) != 0) { |
| 1029 | PLOG(ERROR) << "Failed to stat " << file; |
| 1030 | return false; |
| 1031 | } |
| 1032 | content->resize(st.st_size); |
| 1033 | bool result = ReadFully(fd, content->data(), content->size()); |
| 1034 | if (!result) { |
| 1035 | PLOG(ERROR) << "Failed to read " << file; |
| 1036 | } |
| 1037 | return result; |
| 1038 | } |
| 1039 | }; |
| 1040 | |
| 1041 | TEST_F(ProfileTest, ProfileSnapshotOk) { |
| 1042 | LOG(INFO) << "ProfileSnapshotOk"; |
| 1043 | |
| 1044 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 1045 | createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // The reference profile is created on the fly. We need to be able to |
| 1049 | // snapshot without one. |
| 1050 | TEST_F(ProfileTest, ProfileSnapshotOkNoReference) { |
| 1051 | LOG(INFO) << "ProfileSnapshotOkNoReference"; |
| 1052 | |
| 1053 | SetupProfiles(/*setup_ref*/ false); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 1054 | createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) { |
| 1058 | LOG(INFO) << "ProfileSnapshotFailWrongPackage"; |
| 1059 | |
| 1060 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 1061 | createProfileSnapshot(kTestAppId, "not.there", /*expected_result*/ false); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) { |
| 1065 | LOG(INFO) << "ProfileSnapshotDestroySnapshot"; |
| 1066 | |
| 1067 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 1068 | createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1069 | |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 1070 | ASSERT_BINDER_SUCCESS(service_->destroyProfileSnapshot(package_name_, kPrimaryProfile)); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1071 | struct stat st; |
| 1072 | ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st)); |
| 1073 | ASSERT_EQ(ENOENT, errno); |
| 1074 | } |
| 1075 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1076 | TEST_F(ProfileTest, ProfileMergeOk) { |
| 1077 | LOG(INFO) << "ProfileMergeOk"; |
| 1078 | |
| 1079 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 1080 | mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | // The reference profile is created on the fly. We need to be able to |
| 1084 | // merge without one. |
| 1085 | TEST_F(ProfileTest, ProfileMergeOkNoReference) { |
| 1086 | LOG(INFO) << "ProfileMergeOkNoReference"; |
| 1087 | |
| 1088 | SetupProfiles(/*setup_ref*/ false); |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 1089 | mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | TEST_F(ProfileTest, ProfileMergeFailWrongPackage) { |
| 1093 | LOG(INFO) << "ProfileMergeFailWrongPackage"; |
| 1094 | |
| 1095 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 1096 | mergePackageProfiles("not.there", "primary.prof", /*expected_result*/ false); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 1099 | TEST_F(ProfileTest, ProfileDirOk) { |
| 1100 | LOG(INFO) << "ProfileDirOk"; |
| 1101 | |
| 1102 | std::string cur_profile_dir = create_primary_current_profile_package_dir_path( |
| 1103 | kTestUserId, package_name_); |
| 1104 | std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_, |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1105 | kPrimaryProfile, /*is_secondary_dex*/false); |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 1106 | std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_); |
| 1107 | |
| 1108 | CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1109 | CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR); |
| 1110 | } |
| 1111 | |
| 1112 | // Verify that the profile directories are fixed up during an upgrade. |
| 1113 | // (The reference profile directory is prepared lazily). |
| 1114 | TEST_F(ProfileTest, ProfileDirOkAfterFixup) { |
| 1115 | LOG(INFO) << "ProfileDirOkAfterFixup"; |
| 1116 | |
| 1117 | std::string cur_profile_dir = create_primary_current_profile_package_dir_path( |
| 1118 | kTestUserId, package_name_); |
| 1119 | std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_, |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1120 | kPrimaryProfile, /*is_secondary_dex*/false); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1121 | std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_); |
| 1122 | |
| 1123 | // Simulate a pre-P setup by changing the owner to kTestAppGid and permissions to 0700. |
| 1124 | ASSERT_EQ(0, chown(ref_profile_dir.c_str(), kTestAppGid, kTestAppGid)); |
| 1125 | ASSERT_EQ(0, chmod(ref_profile_dir.c_str(), 0700)); |
| 1126 | |
| 1127 | // Run createAppData again which will offer to fix-up the profile directories. |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 1128 | ASSERT_BINDER_SUCCESS(service_->createAppData( |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1129 | volume_uuid_, |
| 1130 | package_name_, |
| 1131 | kTestUserId, |
| 1132 | kAppDataFlags, |
| 1133 | kTestAppUid, |
| 1134 | se_info_, |
| 1135 | kOSdkVersion, |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 1136 | &ce_data_inode_)); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1137 | |
| 1138 | // Check the file access. |
| 1139 | CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1140 | CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR); |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 1143 | TEST_F(ProfileTest, ProfilePrepareOk) { |
| 1144 | LOG(INFO) << "ProfilePrepareOk"; |
| 1145 | preparePackageProfile(package_name_, "split.prof", /*expected_result*/ true); |
| 1146 | } |
| 1147 | |
| 1148 | TEST_F(ProfileTest, ProfilePrepareFailInvalidPackage) { |
| 1149 | LOG(INFO) << "ProfilePrepareFailInvalidPackage"; |
| 1150 | preparePackageProfile("not.there.package", "split.prof", /*expected_result*/ false); |
| 1151 | } |
| 1152 | |
| 1153 | TEST_F(ProfileTest, ProfilePrepareFailProfileChangedUid) { |
| 1154 | LOG(INFO) << "ProfilePrepareFailProfileChangedUid"; |
| 1155 | SetupProfiles(/*setup_ref*/ false); |
| 1156 | // Change the uid on the profile to trigger a failure. |
| 1157 | ::chown(cur_profile_.c_str(), kTestAppUid + 1, kTestAppGid + 1); |
| 1158 | preparePackageProfile(package_name_, "primary.prof", /*expected_result*/ false); |
| 1159 | } |
| 1160 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1161 | |
| 1162 | class BootProfileTest : public ProfileTest { |
| 1163 | public: |
Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 1164 | std::vector<const std::string> extra_apps_; |
| 1165 | std::vector<int64_t> extra_ce_data_inodes_; |
| 1166 | |
| 1167 | virtual void SetUp() { |
| 1168 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1169 | ProfileTest::SetUp(); |
| 1170 | intial_android_profiles_dir = android_profiles_dir; |
Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 1171 | // Generate profiles for some extra apps. |
| 1172 | // When merging boot profile we split profiles into small groups to avoid |
| 1173 | // opening a lot of file descriptors at the same time. |
| 1174 | // (Currently the group size for aggregation is 10) |
| 1175 | // |
| 1176 | // To stress test that works fine, create profile for more apps. |
| 1177 | createAppProfilesForBootMerge(21); |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | virtual void TearDown() { |
| 1181 | android_profiles_dir = intial_android_profiles_dir; |
Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 1182 | deleteAppProfilesForBootMerge(); |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1183 | ProfileTest::TearDown(); |
| 1184 | } |
| 1185 | |
Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 1186 | void createAppProfilesForBootMerge(size_t number_of_profiles) { |
| 1187 | for (size_t i = 0; i < number_of_profiles; i++) { |
| 1188 | int64_t ce_data_inode; |
| 1189 | std::string package_name = "dummy_test_pkg" + std::to_string(i); |
| 1190 | LOG(INFO) << package_name; |
| 1191 | ASSERT_BINDER_SUCCESS(service_->createAppData( |
| 1192 | volume_uuid_, |
| 1193 | package_name, |
| 1194 | kTestUserId, |
| 1195 | kAppDataFlags, |
| 1196 | kTestAppUid, |
| 1197 | se_info_, |
| 1198 | kOSdkVersion, |
| 1199 | &ce_data_inode)); |
| 1200 | extra_apps_.push_back(package_name); |
| 1201 | extra_ce_data_inodes_.push_back(ce_data_inode); |
| 1202 | std::string profile = create_current_profile_path( |
| 1203 | kTestUserId, package_name, kPrimaryProfile, /*is_secondary_dex*/ false); |
| 1204 | SetupProfile(profile, kTestAppUid, kTestAppGid, 0600, 1); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | void deleteAppProfilesForBootMerge() { |
| 1209 | if (kDebug) { |
| 1210 | return; |
| 1211 | } |
| 1212 | for (size_t i = 0; i < extra_apps_.size(); i++) { |
| 1213 | service_->destroyAppData( |
| 1214 | volume_uuid_, extra_apps_[i], kTestUserId, kAppDataFlags, extra_ce_data_inodes_[i]); |
| 1215 | } |
| 1216 | } |
| 1217 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1218 | void UpdateAndroidProfilesDir(const std::string& profile_dir) { |
| 1219 | android_profiles_dir = profile_dir; |
| 1220 | // We need to create the reference profile directory in the new profile dir. |
| 1221 | run_cmd("mkdir -p " + profile_dir + "/ref"); |
| 1222 | } |
| 1223 | |
| 1224 | void createBootImageProfileSnapshot(const std::string& classpath, bool expected_result) { |
| 1225 | bool result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 1226 | ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot( |
| 1227 | -1, "android", "android.prof", classpath, &result)); |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1228 | ASSERT_EQ(expected_result, result); |
| 1229 | |
| 1230 | if (!expected_result) { |
| 1231 | // Do not check the files if we expect to fail. |
| 1232 | return; |
| 1233 | } |
| 1234 | |
| 1235 | // Check that the snapshot was created with he expected access flags. |
| 1236 | const std::string boot_profile = create_snapshot_profile_path("android", "android.prof"); |
| 1237 | CheckFileAccess(boot_profile, kSystemUid, kSystemGid, 0600 | S_IFREG); |
| 1238 | |
| 1239 | pid_t pid = fork(); |
| 1240 | if (pid == 0) { |
| 1241 | /* child */ |
| 1242 | TransitionToSystemServer(); |
| 1243 | |
| 1244 | // System server should be able to open the snapshot. |
| 1245 | unique_fd fd(open(boot_profile.c_str(), O_RDONLY)); |
| 1246 | ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno); |
| 1247 | _exit(0); |
| 1248 | } |
| 1249 | /* parent */ |
| 1250 | ASSERT_TRUE(WIFEXITED(wait_child(pid))); |
| 1251 | } |
| 1252 | protected: |
| 1253 | std::string intial_android_profiles_dir; |
| 1254 | }; |
| 1255 | |
| 1256 | TEST_F(BootProfileTest, BootProfileSnapshotOk) { |
| 1257 | LOG(INFO) << "BootProfileSnapshotOk"; |
| 1258 | char* boot_classpath = getenv("BOOTCLASSPATH"); |
| 1259 | ASSERT_TRUE(boot_classpath != nullptr); |
| 1260 | createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true); |
| 1261 | } |
| 1262 | |
| 1263 | TEST_F(BootProfileTest, BootProfileSnapshotFailEmptyClasspath) { |
| 1264 | LOG(INFO) << "BootProfileSnapshotFailEmptyClasspath"; |
| 1265 | |
| 1266 | createBootImageProfileSnapshot(/*boot_classpath*/ "", /*expected_result*/ false); |
| 1267 | } |
| 1268 | |
| 1269 | TEST_F(BootProfileTest, BootProfileSnapshotOkNoProfiles) { |
| 1270 | LOG(INFO) << "BootProfileSnapshotOkNoProfiles"; |
| 1271 | char* boot_classpath = getenv("BOOTCLASSPATH"); |
| 1272 | ASSERT_TRUE(boot_classpath != nullptr); |
| 1273 | |
| 1274 | // The app_apk_dir has no profiles. So we shouldn't be able to merge anything. |
| 1275 | // Still, this is not a failure case. |
| 1276 | UpdateAndroidProfilesDir(app_apk_dir_); |
| 1277 | createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true); |
| 1278 | } |
| 1279 | |
| 1280 | // Verify that profile collection. |
| 1281 | TEST_F(BootProfileTest, CollectProfiles) { |
| 1282 | LOG(INFO) << "CollectProfiles"; |
| 1283 | |
| 1284 | // Create some profile directories mimicking the real profile structure. |
| 1285 | run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/ref"); |
| 1286 | run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/0/"); |
| 1287 | run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/1/"); |
| 1288 | // Create an empty profile. |
| 1289 | run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/1/primary.prof"); |
| 1290 | // Create a random file. |
| 1291 | run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/0/non.profile.file"); |
| 1292 | |
| 1293 | // Create some non-empty profiles. |
| 1294 | std::string current_prof = app_private_dir_de_ + "/profiles/cur/0/primary.prof"; |
| 1295 | run_cmd("echo 1 > " + current_prof); |
| 1296 | std::string ref_prof = app_private_dir_de_ + "/profiles/ref/primary.prof"; |
| 1297 | run_cmd("echo 1 > " + ref_prof); |
| 1298 | |
| 1299 | UpdateAndroidProfilesDir(app_private_dir_de_ + "/profiles"); |
| 1300 | |
| 1301 | std::vector<std::string> profiles; |
| 1302 | collect_profiles(&profiles); |
| 1303 | |
| 1304 | // Only two profiles should be in the output. |
| 1305 | ASSERT_EQ(2u, profiles.size()); |
| 1306 | ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), current_prof) != profiles.end()); |
| 1307 | ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), ref_prof) != profiles.end()); |
| 1308 | } |
| 1309 | |
Calin Juravle | f74a737 | 2019-02-28 20:29:41 -0800 | [diff] [blame] | 1310 | TEST_F(DexoptTest, select_execution_binary) { |
| 1311 | LOG(INFO) << "DexoptTestselect_execution_binary"; |
| 1312 | |
| 1313 | std::string release_str = app_private_dir_ce_ + "/release"; |
| 1314 | std::string debug_str = app_private_dir_ce_ + "/debug"; |
| 1315 | |
| 1316 | // Setup the binaries. Note that we only need executable files to actually |
| 1317 | // test the execution binary selection |
| 1318 | run_cmd("touch " + release_str); |
| 1319 | run_cmd("touch " + debug_str); |
| 1320 | run_cmd("chmod 777 " + release_str); |
| 1321 | run_cmd("chmod 777 " + debug_str); |
| 1322 | |
| 1323 | const char* release = release_str.c_str(); |
| 1324 | const char* debug = debug_str.c_str(); |
| 1325 | |
| 1326 | ASSERT_STREQ(release, select_execution_binary( |
| 1327 | release, |
| 1328 | debug, |
| 1329 | /*background_job_compile=*/ false, |
| 1330 | /*is_debug_runtime=*/ false, |
| 1331 | /*is_release=*/ false, |
| 1332 | /*is_debuggable_build=*/ false)); |
| 1333 | |
| 1334 | ASSERT_STREQ(release, select_execution_binary( |
| 1335 | release, |
| 1336 | debug, |
| 1337 | /*background_job_compile=*/ true, |
| 1338 | /*is_debug_runtime=*/ false, |
| 1339 | /*is_release=*/ true, |
| 1340 | /*is_debuggable_build=*/ true)); |
| 1341 | |
| 1342 | ASSERT_STREQ(debug, select_execution_binary( |
| 1343 | release, |
| 1344 | debug, |
| 1345 | /*background_job_compile=*/ false, |
| 1346 | /*is_debug_runtime=*/ true, |
| 1347 | /*is_release=*/ false, |
| 1348 | /*is_debuggable_build=*/ false)); |
| 1349 | |
| 1350 | ASSERT_STREQ(debug, select_execution_binary( |
| 1351 | release, |
| 1352 | debug, |
| 1353 | /*background_job_compile=*/ true, |
| 1354 | /*is_debug_runtime=*/ false, |
| 1355 | /*is_release=*/ false, |
| 1356 | /*is_debuggable_build=*/ true)); |
| 1357 | |
| 1358 | |
| 1359 | // Select the release when the debug file is not there. |
| 1360 | ASSERT_STREQ(release, select_execution_binary( |
| 1361 | release, |
| 1362 | "does_not_exist", |
| 1363 | /*background_job_compile=*/ false, |
| 1364 | /*is_debug_runtime=*/ true, |
| 1365 | /*is_release=*/ false, |
| 1366 | /*is_debuggable_build=*/ false)); |
| 1367 | } |
| 1368 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1369 | } // namespace installd |
| 1370 | } // namespace android |