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> |
| 26 | #include <android-base/stringprintf.h> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 27 | #include <android-base/unique_fd.h> |
| 28 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 29 | #include <cutils/properties.h> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 30 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 31 | #include <gtest/gtest.h> |
| 32 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 33 | #include <selinux/android.h> |
| 34 | #include <selinux/avc.h> |
| 35 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 36 | #include "dexopt.h" |
| 37 | #include "InstalldNativeService.h" |
| 38 | #include "globals.h" |
| 39 | #include "tests/test_utils.h" |
| 40 | #include "utils.h" |
| 41 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 42 | using android::base::ReadFully; |
| 43 | using android::base::unique_fd; |
| 44 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace installd { |
| 47 | |
| 48 | // TODO(calin): try to dedup this code. |
| 49 | #if defined(__arm__) |
| 50 | static const std::string kRuntimeIsa = "arm"; |
| 51 | #elif defined(__aarch64__) |
| 52 | static const std::string kRuntimeIsa = "arm64"; |
| 53 | #elif defined(__mips__) && !defined(__LP64__) |
| 54 | static const std::string kRuntimeIsa = "mips"; |
| 55 | #elif defined(__mips__) && defined(__LP64__) |
| 56 | static const std::string kRuntimeIsa = "mips64"; |
| 57 | #elif defined(__i386__) |
| 58 | static const std::string kRuntimeIsa = "x86"; |
| 59 | #elif defined(__x86_64__) |
| 60 | static const std::string kRuntimeIsa = "x86_64"; |
| 61 | #else |
| 62 | static const std::string kRuntimeIsa = "none"; |
| 63 | #endif |
| 64 | |
| 65 | int get_property(const char *key, char *value, const char *default_value) { |
| 66 | return property_get(key, value, default_value); |
| 67 | } |
| 68 | |
| 69 | bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, |
| 70 | const char *instruction_set) { |
| 71 | return calculate_oat_file_path_default(path, oat_dir, apk_path, instruction_set); |
| 72 | } |
| 73 | |
| 74 | bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path, |
| 75 | const char *instruction_set) { |
| 76 | return calculate_odex_file_path_default(path, apk_path, instruction_set); |
| 77 | } |
| 78 | |
| 79 | bool create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) { |
| 80 | return create_cache_path_default(path, src, instruction_set); |
| 81 | } |
| 82 | |
| 83 | static void run_cmd(const std::string& cmd) { |
| 84 | system(cmd.c_str()); |
| 85 | } |
| 86 | |
| 87 | static void mkdir(const std::string& path, uid_t owner, gid_t group, mode_t mode) { |
| 88 | ::mkdir(path.c_str(), mode); |
| 89 | ::chown(path.c_str(), owner, group); |
| 90 | ::chmod(path.c_str(), mode); |
| 91 | } |
| 92 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 93 | static int log_callback(int type, const char *fmt, ...) { // NOLINT |
| 94 | va_list ap; |
| 95 | int priority; |
| 96 | |
| 97 | switch (type) { |
| 98 | case SELINUX_WARNING: |
| 99 | priority = ANDROID_LOG_WARN; |
| 100 | break; |
| 101 | case SELINUX_INFO: |
| 102 | priority = ANDROID_LOG_INFO; |
| 103 | break; |
| 104 | default: |
| 105 | priority = ANDROID_LOG_ERROR; |
| 106 | break; |
| 107 | } |
| 108 | va_start(ap, fmt); |
| 109 | LOG_PRI_VA(priority, "SELinux", fmt, ap); |
| 110 | va_end(ap); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static bool init_selinux() { |
| 115 | int selinux_enabled = (is_selinux_enabled() > 0); |
| 116 | |
| 117 | union selinux_callback cb; |
| 118 | cb.func_log = log_callback; |
| 119 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 120 | |
| 121 | if (selinux_enabled && selinux_status_open(true) < 0) { |
| 122 | LOG(ERROR) << "Could not open selinux status; exiting"; |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 129 | // Base64 encoding of a simple dex files with 2 methods. |
| 130 | static const char kDexFile[] = |
| 131 | "UEsDBBQAAAAIAOiOYUs9y6BLCgEAABQCAAALABwAY2xhc3Nlcy5kZXhVVAkAA/Ns+lkOHv1ZdXgL" |
| 132 | "AAEEI+UCAASIEwAAS0mt4DIwNmX4qpn7j/2wA7v7N+ZvoQpCJRlVx5SWa4YaiDAxMBQwMDBUhJkI" |
| 133 | "MUBBDyMDAzsDRJwFxAdioBDDHAYEYAbiFUAM1M5wAIhFGCGKDIDYAogdgNgDiH2BOAiI0xghekDm" |
| 134 | "sQIxGxQzM6ACRijNhCbOhCZfyohdPYyuh8szgtVkMkLsLhAAqeCDi+ejibPZZOZlltgxsDnqZSWW" |
| 135 | "JTKwOUFoZh9HayDhZM0g5AMS0M9JzEvX90/KSk0usWZgDAMaws5nAyXBzmpoYGlgAjsAyJoBMp0b" |
| 136 | "zQ8gGhbOTEhhzYwU3qxIYc2GFN6MClC/AhUyKUDMAYU9M1Qc5F8GKBscVgIQM0FxCwBQSwECHgMU" |
| 137 | "AAAACADojmFLPcugSwoBAAAUAgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAA/Ns" |
| 138 | "+ll1eAsAAQQj5QIABIgTAABQSwUGAAAAAAEAAQBRAAAATwEAAAAA"; |
| 139 | |
| 140 | |
| 141 | class DexoptTest : public testing::Test { |
| 142 | protected: |
| 143 | static constexpr bool kDebug = false; |
| 144 | static constexpr uid_t kSystemUid = 1000; |
| 145 | static constexpr uid_t kSystemGid = 1000; |
| 146 | static constexpr int32_t kOSdkVersion = 25; |
| 147 | static constexpr int32_t kAppDataFlags = FLAG_STORAGE_CE | FLAG_STORAGE_DE; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 148 | static constexpr int32_t kTestUserId = 0; |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 149 | static constexpr uid_t kTestAppId = 19999; |
| 150 | |
| 151 | const gid_t kTestAppUid = multiuser_get_uid(kTestUserId, kTestAppId); |
| 152 | const uid_t kTestAppGid = multiuser_get_shared_gid(kTestUserId, kTestAppId); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 153 | |
| 154 | InstalldNativeService* service_; |
| 155 | std::unique_ptr<std::string> volume_uuid_; |
| 156 | std::string package_name_; |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 157 | std::string apk_path_; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 158 | std::string app_apk_dir_; |
| 159 | std::string app_private_dir_ce_; |
| 160 | std::string app_private_dir_de_; |
| 161 | std::string se_info_; |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 162 | std::string app_oat_dir_; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 163 | |
| 164 | int64_t ce_data_inode_; |
| 165 | |
| 166 | std::string secondary_dex_ce_; |
| 167 | std::string secondary_dex_ce_link_; |
| 168 | std::string secondary_dex_de_; |
| 169 | |
| 170 | virtual void SetUp() { |
| 171 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
| 172 | android::base::InitLogging(nullptr); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 173 | // Initialize the globals holding the file system main paths (/data/, /system/ etc..). |
| 174 | // This is needed in order to compute the application and profile paths. |
| 175 | ASSERT_TRUE(init_globals_from_data_and_root()); |
| 176 | // Initialize selinux log callbacks. |
| 177 | // This ensures that selinux is up and running and re-directs the selinux messages |
| 178 | // to logcat (in order to make it easier to investigate test results). |
| 179 | ASSERT_TRUE(init_selinux()); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 180 | service_ = new InstalldNativeService(); |
| 181 | |
| 182 | volume_uuid_ = nullptr; |
| 183 | package_name_ = "com.installd.test.dexopt"; |
| 184 | se_info_ = "default"; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 185 | app_apk_dir_ = android_app_dir + package_name_; |
| 186 | |
| 187 | create_mock_app(); |
| 188 | } |
| 189 | |
| 190 | virtual void TearDown() { |
| 191 | if (!kDebug) { |
| 192 | service_->destroyAppData( |
| 193 | volume_uuid_, package_name_, kTestUserId, kAppDataFlags, ce_data_inode_); |
| 194 | run_cmd("rm -rf " + app_apk_dir_); |
| 195 | run_cmd("rm -rf " + app_private_dir_ce_); |
| 196 | run_cmd("rm -rf " + app_private_dir_de_); |
| 197 | } |
| 198 | delete service_; |
| 199 | } |
| 200 | |
| 201 | void create_mock_app() { |
| 202 | // Create the oat dir. |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 203 | app_oat_dir_ = app_apk_dir_ + "/oat"; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 204 | mkdir(app_apk_dir_, kSystemUid, kSystemGid, 0755); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 205 | service_->createOatDir(app_oat_dir_, kRuntimeIsa); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 206 | |
| 207 | // Copy the primary apk. |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 208 | apk_path_ = app_apk_dir_ + "/base.jar"; |
| 209 | ASSERT_TRUE(WriteBase64ToFile(kDexFile, apk_path_, kSystemUid, kSystemGid, 0644)); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 210 | |
| 211 | // Create the app user data. |
| 212 | ASSERT_TRUE(service_->createAppData( |
| 213 | volume_uuid_, |
| 214 | package_name_, |
| 215 | kTestUserId, |
| 216 | kAppDataFlags, |
| 217 | kTestAppUid, |
| 218 | se_info_, |
| 219 | kOSdkVersion, |
| 220 | &ce_data_inode_).isOk()); |
| 221 | |
| 222 | // Create a secondary dex file on CE storage |
| 223 | const char* volume_uuid_cstr = volume_uuid_ == nullptr ? nullptr : volume_uuid_->c_str(); |
| 224 | app_private_dir_ce_ = create_data_user_ce_package_path( |
| 225 | volume_uuid_cstr, kTestUserId, package_name_.c_str()); |
| 226 | secondary_dex_ce_ = app_private_dir_ce_ + "/secondary_ce.jar"; |
| 227 | ASSERT_TRUE(WriteBase64ToFile(kDexFile, secondary_dex_ce_, kTestAppUid, kTestAppGid, 0600)); |
| 228 | std::string app_private_dir_ce_link = create_data_user_ce_package_path_as_user_link( |
| 229 | volume_uuid_cstr, kTestUserId, package_name_.c_str()); |
| 230 | secondary_dex_ce_link_ = app_private_dir_ce_link + "/secondary_ce.jar"; |
| 231 | |
| 232 | // Create a secondary dex file on DE storage. |
| 233 | app_private_dir_de_ = create_data_user_de_package_path( |
| 234 | volume_uuid_cstr, kTestUserId, package_name_.c_str()); |
| 235 | secondary_dex_de_ = app_private_dir_de_ + "/secondary_de.jar"; |
| 236 | ASSERT_TRUE(WriteBase64ToFile(kDexFile, secondary_dex_de_, kTestAppUid, kTestAppGid, 0600)); |
| 237 | |
| 238 | // Fix app data uid. |
| 239 | ASSERT_TRUE(service_->fixupAppData(volume_uuid_, kTestUserId).isOk()); |
| 240 | } |
| 241 | |
| 242 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 243 | std::string GetSecondaryDexArtifact(const std::string& path, const std::string& type) { |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 244 | std::string::size_type end = path.rfind('.'); |
| 245 | std::string::size_type start = path.rfind('/', end); |
| 246 | return path.substr(0, start) + "/oat/" + kRuntimeIsa + "/" + |
| 247 | path.substr(start + 1, end - start) + type; |
| 248 | } |
| 249 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 250 | void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 251 | bool should_binder_call_succeed, bool should_dex_be_compiled = true, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 252 | /*out */ binder::Status* binder_result = nullptr, int32_t uid = -1) { |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 253 | if (uid == -1) { |
| 254 | uid = kTestAppUid; |
| 255 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 256 | std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_)); |
| 257 | int32_t dexopt_needed = 0; // does not matter; |
| 258 | std::unique_ptr<std::string> out_path = nullptr; // does not matter |
| 259 | int32_t dex_flags = DEXOPT_SECONDARY_DEX | dex_storage_flag; |
| 260 | std::string compiler_filter = "speed-profile"; |
| 261 | std::unique_ptr<std::string> class_loader_context_ptr(new std::string("&")); |
| 262 | std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_)); |
| 263 | bool downgrade = false; |
David Brazdil | 570d398 | 2018-01-16 20:15:43 +0000 | [diff] [blame] | 264 | int32_t target_sdk_version = 0; // default |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 265 | std::unique_ptr<std::string> profile_name_ptr = nullptr; |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 266 | std::unique_ptr<std::string> dm_path_ptr = nullptr; |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 267 | std::unique_ptr<std::string> compilation_reason_ptr = nullptr; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 268 | |
| 269 | binder::Status result = service_->dexopt(path, |
| 270 | uid, |
| 271 | package_name_ptr, |
| 272 | kRuntimeIsa, |
| 273 | dexopt_needed, |
| 274 | out_path, |
| 275 | dex_flags, |
| 276 | compiler_filter, |
| 277 | volume_uuid_, |
| 278 | class_loader_context_ptr, |
| 279 | se_info_ptr, |
David Brazdil | 570d398 | 2018-01-16 20:15:43 +0000 | [diff] [blame] | 280 | downgrade, |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 281 | target_sdk_version, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 282 | profile_name_ptr, |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 283 | dm_path_ptr, |
| 284 | compilation_reason_ptr); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 285 | ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str(); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 286 | int expected_access = should_dex_be_compiled ? 0 : -1; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 287 | std::string odex = GetSecondaryDexArtifact(path, "odex"); |
| 288 | std::string vdex = GetSecondaryDexArtifact(path, "vdex"); |
| 289 | std::string art = GetSecondaryDexArtifact(path, "art"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 290 | ASSERT_EQ(expected_access, access(odex.c_str(), R_OK)); |
| 291 | ASSERT_EQ(expected_access, access(vdex.c_str(), R_OK)); |
| 292 | 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^] | 293 | if (binder_result != nullptr) { |
| 294 | *binder_result = result; |
| 295 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void reconcile_secondary_dex(const std::string& path, int32_t storage_flag, |
| 299 | 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] | 300 | int32_t uid = -1, std::string* package_override = nullptr) { |
| 301 | if (uid == -1) { |
| 302 | uid = kTestAppUid; |
| 303 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 304 | std::vector<std::string> isas; |
| 305 | isas.push_back(kRuntimeIsa); |
| 306 | bool out_secondary_dex_exists = false; |
| 307 | binder::Status result = service_->reconcileSecondaryDexFile( |
| 308 | path, |
| 309 | package_override == nullptr ? package_name_ : *package_override, |
| 310 | uid, |
| 311 | isas, |
| 312 | volume_uuid_, |
| 313 | storage_flag, |
| 314 | &out_secondary_dex_exists); |
| 315 | |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 316 | ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str(); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 317 | ASSERT_EQ(should_dex_exist, out_secondary_dex_exists); |
| 318 | |
| 319 | int expected_access = should_dex_be_deleted ? -1 : 0; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 320 | std::string odex = GetSecondaryDexArtifact(path, "odex"); |
| 321 | std::string vdex = GetSecondaryDexArtifact(path, "vdex"); |
| 322 | std::string art = GetSecondaryDexArtifact(path, "art"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 323 | ASSERT_EQ(expected_access, access(odex.c_str(), F_OK)); |
| 324 | ASSERT_EQ(expected_access, access(vdex.c_str(), F_OK)); |
| 325 | ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image. |
| 326 | } |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 327 | |
| 328 | void CheckFileAccess(const std::string& file, uid_t uid, gid_t gid, mode_t mode) { |
| 329 | struct stat st; |
| 330 | ASSERT_EQ(0, stat(file.c_str(), &st)); |
| 331 | ASSERT_EQ(uid, st.st_uid); |
| 332 | ASSERT_EQ(gid, st.st_gid); |
| 333 | ASSERT_EQ(mode, st.st_mode); |
| 334 | } |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 335 | |
| 336 | void CompilePrimaryDexOk(std::string compiler_filter, |
| 337 | int32_t dex_flags, |
| 338 | const char* oat_dir, |
| 339 | int32_t uid, |
| 340 | int32_t dexopt_needed, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 341 | binder::Status* binder_result = nullptr, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 342 | const char* dm_path = nullptr, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 343 | bool downgrade = false) { |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 344 | CompilePrimaryDex(compiler_filter, |
| 345 | dex_flags, |
| 346 | oat_dir, |
| 347 | uid, |
| 348 | dexopt_needed, |
| 349 | dm_path, |
| 350 | downgrade, |
| 351 | true, |
| 352 | binder_result); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void CompilePrimaryDexFail(std::string compiler_filter, |
| 356 | int32_t dex_flags, |
| 357 | const char* oat_dir, |
| 358 | int32_t uid, |
| 359 | int32_t dexopt_needed, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 360 | binder::Status* binder_result = nullptr, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 361 | const char* dm_path = nullptr, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 362 | bool downgrade = false) { |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 363 | CompilePrimaryDex(compiler_filter, |
| 364 | dex_flags, |
| 365 | oat_dir, |
| 366 | uid, |
| 367 | dexopt_needed, |
| 368 | dm_path, |
| 369 | downgrade, |
| 370 | false, |
| 371 | binder_result); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void CompilePrimaryDex(std::string compiler_filter, |
| 375 | int32_t dex_flags, |
| 376 | const char* oat_dir, |
| 377 | int32_t uid, |
| 378 | int32_t dexopt_needed, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 379 | const char* dm_path, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 380 | bool downgrade, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 381 | bool should_binder_call_succeed, |
| 382 | /*out */ binder::Status* binder_result) { |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 383 | std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_)); |
| 384 | std::unique_ptr<std::string> out_path( |
| 385 | oat_dir == nullptr ? nullptr : new std::string(oat_dir)); |
| 386 | std::unique_ptr<std::string> class_loader_context_ptr(new std::string("&")); |
| 387 | std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_)); |
| 388 | int32_t target_sdk_version = 0; // default |
| 389 | std::unique_ptr<std::string> profile_name_ptr(new std::string("primary.prof")); |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 390 | std::unique_ptr<std::string> dm_path_ptr = nullptr; |
| 391 | if (dm_path != nullptr) { |
| 392 | dm_path_ptr.reset(new std::string(dm_path)); |
| 393 | } |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 394 | std::unique_ptr<std::string> compilation_reason_ptr(new std::string("test-reason")); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 395 | |
| 396 | bool prof_result; |
| 397 | binder::Status prof_binder_result = service_->prepareAppProfile( |
Jeff Sharkey | 1ebe77f | 2018-02-27 11:40:45 -0700 | [diff] [blame] | 398 | package_name_, kTestUserId, kTestAppId, *profile_name_ptr, apk_path_, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 399 | /*dex_metadata*/ nullptr, &prof_result); |
| 400 | |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 401 | ASSERT_TRUE(prof_binder_result.isOk()) << prof_binder_result.toString8().c_str(); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 402 | ASSERT_TRUE(prof_result); |
| 403 | |
| 404 | binder::Status result = service_->dexopt(apk_path_, |
| 405 | uid, |
| 406 | package_name_ptr, |
| 407 | kRuntimeIsa, |
| 408 | dexopt_needed, |
| 409 | out_path, |
| 410 | dex_flags, |
| 411 | compiler_filter, |
| 412 | volume_uuid_, |
| 413 | class_loader_context_ptr, |
| 414 | se_info_ptr, |
| 415 | downgrade, |
| 416 | target_sdk_version, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 417 | profile_name_ptr, |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 418 | dm_path_ptr, |
| 419 | compilation_reason_ptr); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 420 | ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str(); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 421 | |
| 422 | if (!should_binder_call_succeed) { |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 423 | if (binder_result != nullptr) { |
| 424 | *binder_result = result; |
| 425 | } |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | // Check the access to the compiler output. |
| 429 | // - speed-profile artifacts are not world-wide readable. |
| 430 | // - files are owned by the system uid. |
| 431 | std::string odex = GetPrimaryDexArtifact(oat_dir, apk_path_, "odex"); |
| 432 | std::string vdex = GetPrimaryDexArtifact(oat_dir, apk_path_, "vdex"); |
| 433 | std::string art = GetPrimaryDexArtifact(oat_dir, apk_path_, "art"); |
| 434 | |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 435 | bool is_public = (dex_flags & DEXOPT_PUBLIC) != 0; |
| 436 | mode_t mode = S_IFREG | (is_public ? 0644 : 0640); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 437 | CheckFileAccess(odex, kSystemUid, uid, mode); |
| 438 | CheckFileAccess(vdex, kSystemUid, uid, mode); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 439 | |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 440 | if (compiler_filter == "speed-profile") { |
| 441 | CheckFileAccess(art, kSystemUid, uid, mode); |
| 442 | } |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 443 | if (binder_result != nullptr) { |
| 444 | *binder_result = result; |
| 445 | } |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | std::string GetPrimaryDexArtifact(const char* oat_dir, |
| 449 | const std::string& dex_path, |
| 450 | const std::string& type) { |
| 451 | if (oat_dir == nullptr) { |
| 452 | std::string path = dex_path; |
| 453 | for (auto it = path.begin() + 1; it < path.end(); ++it) { |
| 454 | if (*it == '/') { |
| 455 | *it = '@'; |
| 456 | } |
| 457 | } |
| 458 | return android_data_dir + DALVIK_CACHE + '/' + kRuntimeIsa + "/" + path |
| 459 | + "@classes.dex"; |
| 460 | } else { |
| 461 | std::string::size_type name_end = dex_path.rfind('.'); |
| 462 | std::string::size_type name_start = dex_path.rfind('/'); |
| 463 | return std::string(oat_dir) + "/" + kRuntimeIsa + "/" + |
| 464 | dex_path.substr(name_start + 1, name_end - name_start) + type; |
| 465 | } |
| 466 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 467 | }; |
| 468 | |
| 469 | |
| 470 | TEST_F(DexoptTest, DexoptSecondaryCe) { |
| 471 | LOG(INFO) << "DexoptSecondaryCe"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 472 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 473 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 474 | } |
| 475 | |
| 476 | TEST_F(DexoptTest, DexoptSecondaryCeLink) { |
| 477 | LOG(INFO) << "DexoptSecondaryCeLink"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 478 | CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 479 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 480 | } |
| 481 | |
| 482 | TEST_F(DexoptTest, DexoptSecondaryDe) { |
| 483 | LOG(INFO) << "DexoptSecondaryDe"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 484 | CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 485 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 486 | } |
| 487 | |
| 488 | TEST_F(DexoptTest, DexoptSecondaryDoesNotExist) { |
| 489 | LOG(INFO) << "DexoptSecondaryDoesNotExist"; |
| 490 | // 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^] | 491 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 492 | CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 493 | /*binder_ok*/ true, /*compile_ok*/ false, &status); |
| 494 | EXPECT_STREQ(status.toString8().c_str(), "No error"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) { |
| 498 | LOG(INFO) << "DexoptSecondaryStorageValidationError"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 499 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 500 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 501 | /*binder_ok*/ false, /*compile_ok*/ false, &status); |
| 502 | EXPECT_STREQ(status.toString8().c_str(), "Status(-8): '-1: Failed processing secondary.'"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) { |
| 506 | LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 507 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 508 | CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 509 | /*binder_ok*/ false, /*compile_ok*/ false, &status); |
| 510 | EXPECT_STREQ(status.toString8().c_str(), "Status(-8): '-1: Failed processing secondary.'"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) { |
| 514 | LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 515 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 516 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 517 | /*binder_ok*/ false, /*compile_ok*/ false, &status, kSystemUid); |
| 518 | EXPECT_STREQ(status.toString8().c_str(), "Status(-8): '-1: Failed processing secondary.'"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 521 | TEST_F(DexoptTest, DexoptPrimaryPublic) { |
| 522 | LOG(INFO) << "DexoptPrimaryPublic"; |
| 523 | CompilePrimaryDexOk("verify", |
| 524 | DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC, |
| 525 | app_oat_dir_.c_str(), |
| 526 | kTestAppGid, |
| 527 | DEX2OAT_FROM_SCRATCH); |
| 528 | } |
| 529 | |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 530 | TEST_F(DexoptTest, DexoptPrimaryFailedInvalidFilter) { |
| 531 | LOG(INFO) << "DexoptPrimaryFailedInvalidFilter"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 532 | binder::Status status; |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 533 | CompilePrimaryDexFail("awesome-filter", |
| 534 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC, |
| 535 | app_oat_dir_.c_str(), |
| 536 | kTestAppGid, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame^] | 537 | DEX2OAT_FROM_SCRATCH, |
| 538 | &status); |
| 539 | EXPECT_STREQ(status.toString8().c_str(), |
| 540 | "Status(-8): \'256: Dex2oat invocation for " |
| 541 | "/data/app/com.installd.test.dexopt/base.jar failed with 0x0100\'"); |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 544 | TEST_F(DexoptTest, DexoptPrimaryProfileNonPublic) { |
| 545 | LOG(INFO) << "DexoptPrimaryProfileNonPublic"; |
| 546 | CompilePrimaryDexOk("speed-profile", |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 547 | DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 548 | app_oat_dir_.c_str(), |
| 549 | kTestAppGid, |
| 550 | DEX2OAT_FROM_SCRATCH); |
| 551 | } |
| 552 | |
| 553 | TEST_F(DexoptTest, DexoptPrimaryProfilePublic) { |
| 554 | LOG(INFO) << "DexoptPrimaryProfilePublic"; |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 555 | CompilePrimaryDexOk("speed-profile", |
| 556 | DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_PUBLIC, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 557 | app_oat_dir_.c_str(), |
| 558 | kTestAppGid, |
| 559 | DEX2OAT_FROM_SCRATCH); |
| 560 | } |
| 561 | |
| 562 | TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) { |
| 563 | LOG(INFO) << "DexoptPrimaryBackgroundOk"; |
| 564 | CompilePrimaryDexOk("speed-profile", |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 565 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 566 | app_oat_dir_.c_str(), |
| 567 | kTestAppGid, |
| 568 | DEX2OAT_FROM_SCRATCH); |
| 569 | } |
| 570 | |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 571 | class PrimaryDexReCompilationTest : public DexoptTest { |
| 572 | public: |
| 573 | virtual void SetUp() { |
| 574 | DexoptTest::SetUp(); |
| 575 | CompilePrimaryDexOk("verify", |
| 576 | DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC, |
| 577 | app_oat_dir_.c_str(), |
| 578 | kTestAppGid, |
| 579 | DEX2OAT_FROM_SCRATCH); |
| 580 | std::string odex = GetSecondaryDexArtifact(apk_path_, "odex"); |
| 581 | std::string vdex = GetSecondaryDexArtifact(apk_path_, "vdex"); |
| 582 | |
| 583 | first_compilation_odex_fd_.reset(open(odex.c_str(), O_RDONLY)); |
| 584 | first_compilation_vdex_fd_.reset(open(vdex.c_str(), O_RDONLY)); |
| 585 | } |
| 586 | |
| 587 | virtual void TearDown() { |
| 588 | first_compilation_odex_fd_.reset(-1); |
| 589 | first_compilation_vdex_fd_.reset(-1); |
| 590 | DexoptTest::TearDown(); |
| 591 | } |
| 592 | |
| 593 | protected: |
| 594 | unique_fd first_compilation_odex_fd_; |
| 595 | unique_fd first_compilation_vdex_fd_; |
| 596 | }; |
| 597 | |
| 598 | TEST_F(PrimaryDexReCompilationTest, DexoptPrimaryUpdateInPlaceVdex) { |
| 599 | LOG(INFO) << "DexoptPrimaryUpdateInPlaceVdex"; |
| 600 | |
| 601 | CompilePrimaryDexOk("verify", |
| 602 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC, |
| 603 | app_oat_dir_.c_str(), |
| 604 | kTestAppGid, |
| 605 | DEX2OAT_FOR_BOOT_IMAGE); |
| 606 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 607 | |
| 608 | class ReconcileTest : public DexoptTest { |
| 609 | virtual void SetUp() { |
| 610 | DexoptTest::SetUp(); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 611 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 612 | /*binder_ok*/ true, /*compile_ok*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 613 | CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 614 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 615 | } |
| 616 | }; |
| 617 | |
| 618 | TEST_F(ReconcileTest, ReconcileSecondaryCeExists) { |
| 619 | LOG(INFO) << "ReconcileSecondaryCeExists"; |
| 620 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE, |
| 621 | /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false); |
| 622 | } |
| 623 | |
| 624 | TEST_F(ReconcileTest, ReconcileSecondaryCeLinkExists) { |
| 625 | LOG(INFO) << "ReconcileSecondaryCeLinkExists"; |
| 626 | reconcile_secondary_dex(secondary_dex_ce_link_, FLAG_STORAGE_CE, |
| 627 | /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false); |
| 628 | } |
| 629 | |
| 630 | TEST_F(ReconcileTest, ReconcileSecondaryDeExists) { |
| 631 | LOG(INFO) << "ReconcileSecondaryDeExists"; |
| 632 | reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE, |
| 633 | /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false); |
| 634 | } |
| 635 | |
| 636 | TEST_F(ReconcileTest, ReconcileSecondaryDeDoesNotExist) { |
| 637 | LOG(INFO) << "ReconcileSecondaryDeDoesNotExist"; |
| 638 | run_cmd("rm -rf " + secondary_dex_de_); |
| 639 | reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE, |
| 640 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ true); |
| 641 | } |
| 642 | |
| 643 | TEST_F(ReconcileTest, ReconcileSecondaryStorageValidationError) { |
| 644 | // Validation errors will not clean the odex/vdex/art files but will mark |
| 645 | // the file as non existent so that the PM knows it should purge it from its |
| 646 | // records. |
| 647 | LOG(INFO) << "ReconcileSecondaryStorageValidationError"; |
| 648 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_DE, |
| 649 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false); |
| 650 | } |
| 651 | |
| 652 | TEST_F(ReconcileTest, ReconcileSecondaryAppOwnershipValidationError) { |
| 653 | LOG(INFO) << "ReconcileSecondaryAppOwnershipValidationError"; |
| 654 | // Attempt to reconcile the dex files of the test app from a different app. |
| 655 | std::string another_app = "another.app"; |
| 656 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE, |
| 657 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid, &another_app); |
| 658 | } |
| 659 | |
| 660 | TEST_F(ReconcileTest, ReconcileSecondaryAcessViaDifferentUidError) { |
| 661 | LOG(INFO) << "ReconcileSecondaryAcessViaDifferentUidError"; |
| 662 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE, |
| 663 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid); |
| 664 | } |
| 665 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 666 | class ProfileTest : public DexoptTest { |
| 667 | protected: |
| 668 | std::string cur_profile_; |
| 669 | std::string ref_profile_; |
| 670 | std::string snap_profile_; |
| 671 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 672 | static constexpr const char* kPrimaryProfile = "primary.prof"; |
| 673 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 674 | virtual void SetUp() { |
| 675 | DexoptTest::SetUp(); |
| 676 | cur_profile_ = create_current_profile_path( |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 677 | kTestUserId, package_name_, kPrimaryProfile, /*is_secondary_dex*/ false); |
| 678 | ref_profile_ = create_reference_profile_path(package_name_, kPrimaryProfile, |
| 679 | /*is_secondary_dex*/ false); |
| 680 | snap_profile_ = create_snapshot_profile_path(package_name_, kPrimaryProfile); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 683 | void SetupProfile(const std::string& path, uid_t uid, gid_t gid, mode_t mode, |
| 684 | int32_t num_dex) { |
| 685 | run_cmd("profman --generate-test-profile-seed=" + std::to_string(num_dex) + |
| 686 | " --generate-test-profile-num-dex=" + std::to_string(num_dex) + |
| 687 | " --generate-test-profile=" + path); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 688 | ::chmod(path.c_str(), mode); |
| 689 | ::chown(path.c_str(), uid, gid); |
| 690 | } |
| 691 | |
| 692 | void SetupProfiles(bool setup_ref) { |
| 693 | SetupProfile(cur_profile_, kTestAppUid, kTestAppGid, 0600, 1); |
| 694 | if (setup_ref) { |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 695 | SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0600, 2); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 699 | void createProfileSnapshot(int32_t appid, const std::string& package_name, |
| 700 | bool expected_result) { |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 701 | bool result; |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 702 | binder::Status binder_result = service_->createProfileSnapshot( |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 703 | appid, package_name, kPrimaryProfile, apk_path_, &result); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 704 | ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str(); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 705 | ASSERT_EQ(expected_result, result); |
| 706 | |
| 707 | if (!expected_result) { |
| 708 | // Do not check the files if we expect to fail. |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | // Check that the snapshot was created witht he expected acess flags. |
| 713 | CheckFileAccess(snap_profile_, kSystemUid, kSystemGid, 0600 | S_IFREG); |
| 714 | |
| 715 | // The snapshot should be equivalent to the merge of profiles. |
| 716 | std::string expected_profile_content = snap_profile_ + ".expected"; |
| 717 | run_cmd("rm -f " + expected_profile_content); |
| 718 | run_cmd("touch " + expected_profile_content); |
| 719 | run_cmd("profman --profile-file=" + cur_profile_ + |
| 720 | " --profile-file=" + ref_profile_ + |
Calin Juravle | 40b5f46 | 2018-02-07 04:11:52 -0800 | [diff] [blame] | 721 | " --reference-profile-file=" + expected_profile_content + |
| 722 | " --apk=" + apk_path_); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 723 | |
| 724 | ASSERT_TRUE(AreFilesEqual(expected_profile_content, snap_profile_)); |
| 725 | |
| 726 | pid_t pid = fork(); |
| 727 | if (pid == 0) { |
| 728 | /* child */ |
| 729 | TransitionToSystemServer(); |
| 730 | |
| 731 | // System server should be able to open the the spanshot. |
| 732 | unique_fd fd(open(snap_profile_.c_str(), O_RDONLY)); |
| 733 | ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno); |
| 734 | _exit(0); |
| 735 | } |
| 736 | /* parent */ |
| 737 | ASSERT_TRUE(WIFEXITED(wait_child(pid))); |
| 738 | } |
| 739 | |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 740 | void mergePackageProfiles(const std::string& package_name, |
| 741 | const std::string& code_path, |
| 742 | bool expected_result) { |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 743 | bool result; |
| 744 | binder::Status binder_result = service_->mergeProfiles( |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 745 | kTestAppUid, package_name, code_path, &result); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 746 | ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str(); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 747 | ASSERT_EQ(expected_result, result); |
| 748 | |
| 749 | if (!expected_result) { |
| 750 | // Do not check the files if we expect to fail. |
| 751 | return; |
| 752 | } |
| 753 | |
| 754 | // Check that the snapshot was created witht he expected acess flags. |
| 755 | CheckFileAccess(ref_profile_, kTestAppUid, kTestAppUid, 0600 | S_IFREG); |
| 756 | |
| 757 | // The snapshot should be equivalent to the merge of profiles. |
| 758 | std::string ref_profile_content = ref_profile_ + ".expected"; |
| 759 | run_cmd("rm -f " + ref_profile_content); |
| 760 | run_cmd("touch " + ref_profile_content); |
| 761 | run_cmd("profman --profile-file=" + cur_profile_ + |
| 762 | " --profile-file=" + ref_profile_ + |
| 763 | " --reference-profile-file=" + ref_profile_content); |
| 764 | |
| 765 | ASSERT_TRUE(AreFilesEqual(ref_profile_content, ref_profile_)); |
| 766 | } |
| 767 | |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 768 | // TODO(calin): add dex metadata tests once the ART change is merged. |
| 769 | void preparePackageProfile(const std::string& package_name, const std::string& profile_name, |
| 770 | bool expected_result) { |
| 771 | bool result; |
| 772 | binder::Status binder_result = service_->prepareAppProfile( |
Jeff Sharkey | 1ebe77f | 2018-02-27 11:40:45 -0700 | [diff] [blame] | 773 | package_name, kTestUserId, kTestAppId, profile_name, apk_path_, |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 774 | /*dex_metadata*/ nullptr, &result); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 775 | ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str(); |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 776 | ASSERT_EQ(expected_result, result); |
| 777 | |
| 778 | if (!expected_result) { |
| 779 | // Do not check the files if we expect to fail. |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | std::string code_path_cur_prof = create_current_profile_path( |
| 784 | kTestUserId, package_name, profile_name, /*is_secondary_dex*/ false); |
| 785 | std::string code_path_ref_profile = create_reference_profile_path(package_name, |
| 786 | profile_name, /*is_secondary_dex*/ false); |
| 787 | |
| 788 | // Check that we created the current profile. |
| 789 | CheckFileAccess(code_path_cur_prof, kTestAppUid, kTestAppUid, 0600 | S_IFREG); |
| 790 | |
| 791 | // Without dex metadata we don't generate a reference profile. |
| 792 | ASSERT_EQ(-1, access(code_path_ref_profile.c_str(), R_OK)); |
| 793 | } |
| 794 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 795 | protected: |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 796 | void TransitionToSystemServer() { |
| 797 | ASSERT_TRUE(DropCapabilities(kSystemUid, kSystemGid)); |
| 798 | int32_t res = selinux_android_setcontext( |
| 799 | kSystemUid, true, se_info_.c_str(), "system_server"); |
| 800 | ASSERT_EQ(0, res) << "Failed to setcon " << strerror(errno); |
| 801 | } |
| 802 | |
| 803 | bool AreFilesEqual(const std::string& file1, const std::string& file2) { |
| 804 | std::vector<uint8_t> content1; |
| 805 | std::vector<uint8_t> content2; |
| 806 | |
| 807 | if (!ReadAll(file1, &content1)) return false; |
| 808 | if (!ReadAll(file2, &content2)) return false; |
| 809 | return content1 == content2; |
| 810 | } |
| 811 | |
| 812 | bool ReadAll(const std::string& file, std::vector<uint8_t>* content) { |
| 813 | unique_fd fd(open(file.c_str(), O_RDONLY)); |
| 814 | if (fd < 0) { |
| 815 | PLOG(ERROR) << "Failed to open " << file; |
| 816 | return false; |
| 817 | } |
| 818 | struct stat st; |
| 819 | if (fstat(fd, &st) != 0) { |
| 820 | PLOG(ERROR) << "Failed to stat " << file; |
| 821 | return false; |
| 822 | } |
| 823 | content->resize(st.st_size); |
| 824 | bool result = ReadFully(fd, content->data(), content->size()); |
| 825 | if (!result) { |
| 826 | PLOG(ERROR) << "Failed to read " << file; |
| 827 | } |
| 828 | return result; |
| 829 | } |
| 830 | }; |
| 831 | |
| 832 | TEST_F(ProfileTest, ProfileSnapshotOk) { |
| 833 | LOG(INFO) << "ProfileSnapshotOk"; |
| 834 | |
| 835 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 836 | createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | // The reference profile is created on the fly. We need to be able to |
| 840 | // snapshot without one. |
| 841 | TEST_F(ProfileTest, ProfileSnapshotOkNoReference) { |
| 842 | LOG(INFO) << "ProfileSnapshotOkNoReference"; |
| 843 | |
| 844 | SetupProfiles(/*setup_ref*/ false); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 845 | createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) { |
| 849 | LOG(INFO) << "ProfileSnapshotFailWrongPackage"; |
| 850 | |
| 851 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 852 | createProfileSnapshot(kTestAppId, "not.there", /*expected_result*/ false); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) { |
| 856 | LOG(INFO) << "ProfileSnapshotDestroySnapshot"; |
| 857 | |
| 858 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 859 | createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 860 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 861 | binder::Status binder_result = service_->destroyProfileSnapshot(package_name_, kPrimaryProfile); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 862 | ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str(); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 863 | struct stat st; |
| 864 | ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st)); |
| 865 | ASSERT_EQ(ENOENT, errno); |
| 866 | } |
| 867 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 868 | TEST_F(ProfileTest, ProfileMergeOk) { |
| 869 | LOG(INFO) << "ProfileMergeOk"; |
| 870 | |
| 871 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 872 | mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | // The reference profile is created on the fly. We need to be able to |
| 876 | // merge without one. |
| 877 | TEST_F(ProfileTest, ProfileMergeOkNoReference) { |
| 878 | LOG(INFO) << "ProfileMergeOkNoReference"; |
| 879 | |
| 880 | SetupProfiles(/*setup_ref*/ false); |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 881 | mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | TEST_F(ProfileTest, ProfileMergeFailWrongPackage) { |
| 885 | LOG(INFO) << "ProfileMergeFailWrongPackage"; |
| 886 | |
| 887 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 888 | mergePackageProfiles("not.there", "primary.prof", /*expected_result*/ false); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 889 | } |
| 890 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 891 | TEST_F(ProfileTest, ProfileDirOk) { |
| 892 | LOG(INFO) << "ProfileDirOk"; |
| 893 | |
| 894 | std::string cur_profile_dir = create_primary_current_profile_package_dir_path( |
| 895 | kTestUserId, package_name_); |
| 896 | std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_, |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 897 | kPrimaryProfile, /*is_secondary_dex*/false); |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 898 | std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_); |
| 899 | |
| 900 | CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 901 | CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR); |
| 902 | } |
| 903 | |
| 904 | // Verify that the profile directories are fixed up during an upgrade. |
| 905 | // (The reference profile directory is prepared lazily). |
| 906 | TEST_F(ProfileTest, ProfileDirOkAfterFixup) { |
| 907 | LOG(INFO) << "ProfileDirOkAfterFixup"; |
| 908 | |
| 909 | std::string cur_profile_dir = create_primary_current_profile_package_dir_path( |
| 910 | kTestUserId, package_name_); |
| 911 | std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_, |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 912 | kPrimaryProfile, /*is_secondary_dex*/false); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 913 | std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_); |
| 914 | |
| 915 | // Simulate a pre-P setup by changing the owner to kTestAppGid and permissions to 0700. |
| 916 | ASSERT_EQ(0, chown(ref_profile_dir.c_str(), kTestAppGid, kTestAppGid)); |
| 917 | ASSERT_EQ(0, chmod(ref_profile_dir.c_str(), 0700)); |
| 918 | |
| 919 | // Run createAppData again which will offer to fix-up the profile directories. |
| 920 | ASSERT_TRUE(service_->createAppData( |
| 921 | volume_uuid_, |
| 922 | package_name_, |
| 923 | kTestUserId, |
| 924 | kAppDataFlags, |
| 925 | kTestAppUid, |
| 926 | se_info_, |
| 927 | kOSdkVersion, |
| 928 | &ce_data_inode_).isOk()); |
| 929 | |
| 930 | // Check the file access. |
| 931 | CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 932 | CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR); |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 935 | TEST_F(ProfileTest, ProfilePrepareOk) { |
| 936 | LOG(INFO) << "ProfilePrepareOk"; |
| 937 | preparePackageProfile(package_name_, "split.prof", /*expected_result*/ true); |
| 938 | } |
| 939 | |
| 940 | TEST_F(ProfileTest, ProfilePrepareFailInvalidPackage) { |
| 941 | LOG(INFO) << "ProfilePrepareFailInvalidPackage"; |
| 942 | preparePackageProfile("not.there.package", "split.prof", /*expected_result*/ false); |
| 943 | } |
| 944 | |
| 945 | TEST_F(ProfileTest, ProfilePrepareFailProfileChangedUid) { |
| 946 | LOG(INFO) << "ProfilePrepareFailProfileChangedUid"; |
| 947 | SetupProfiles(/*setup_ref*/ false); |
| 948 | // Change the uid on the profile to trigger a failure. |
| 949 | ::chown(cur_profile_.c_str(), kTestAppUid + 1, kTestAppGid + 1); |
| 950 | preparePackageProfile(package_name_, "primary.prof", /*expected_result*/ false); |
| 951 | } |
| 952 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 953 | |
| 954 | class BootProfileTest : public ProfileTest { |
| 955 | public: |
| 956 | virtual void setup() { |
| 957 | ProfileTest::SetUp(); |
| 958 | intial_android_profiles_dir = android_profiles_dir; |
| 959 | } |
| 960 | |
| 961 | virtual void TearDown() { |
| 962 | android_profiles_dir = intial_android_profiles_dir; |
| 963 | ProfileTest::TearDown(); |
| 964 | } |
| 965 | |
| 966 | void UpdateAndroidProfilesDir(const std::string& profile_dir) { |
| 967 | android_profiles_dir = profile_dir; |
| 968 | // We need to create the reference profile directory in the new profile dir. |
| 969 | run_cmd("mkdir -p " + profile_dir + "/ref"); |
| 970 | } |
| 971 | |
| 972 | void createBootImageProfileSnapshot(const std::string& classpath, bool expected_result) { |
| 973 | bool result; |
| 974 | binder::Status binder_result = service_->createProfileSnapshot( |
| 975 | -1, "android", "android.prof", classpath, &result); |
| 976 | ASSERT_TRUE(binder_result.isOk()); |
| 977 | ASSERT_EQ(expected_result, result); |
| 978 | |
| 979 | if (!expected_result) { |
| 980 | // Do not check the files if we expect to fail. |
| 981 | return; |
| 982 | } |
| 983 | |
| 984 | // Check that the snapshot was created with he expected access flags. |
| 985 | const std::string boot_profile = create_snapshot_profile_path("android", "android.prof"); |
| 986 | CheckFileAccess(boot_profile, kSystemUid, kSystemGid, 0600 | S_IFREG); |
| 987 | |
| 988 | pid_t pid = fork(); |
| 989 | if (pid == 0) { |
| 990 | /* child */ |
| 991 | TransitionToSystemServer(); |
| 992 | |
| 993 | // System server should be able to open the snapshot. |
| 994 | unique_fd fd(open(boot_profile.c_str(), O_RDONLY)); |
| 995 | ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno); |
| 996 | _exit(0); |
| 997 | } |
| 998 | /* parent */ |
| 999 | ASSERT_TRUE(WIFEXITED(wait_child(pid))); |
| 1000 | } |
| 1001 | protected: |
| 1002 | std::string intial_android_profiles_dir; |
| 1003 | }; |
| 1004 | |
| 1005 | TEST_F(BootProfileTest, BootProfileSnapshotOk) { |
| 1006 | LOG(INFO) << "BootProfileSnapshotOk"; |
| 1007 | char* boot_classpath = getenv("BOOTCLASSPATH"); |
| 1008 | ASSERT_TRUE(boot_classpath != nullptr); |
| 1009 | createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true); |
| 1010 | } |
| 1011 | |
| 1012 | TEST_F(BootProfileTest, BootProfileSnapshotFailEmptyClasspath) { |
| 1013 | LOG(INFO) << "BootProfileSnapshotFailEmptyClasspath"; |
| 1014 | |
| 1015 | createBootImageProfileSnapshot(/*boot_classpath*/ "", /*expected_result*/ false); |
| 1016 | } |
| 1017 | |
| 1018 | TEST_F(BootProfileTest, BootProfileSnapshotOkNoProfiles) { |
| 1019 | LOG(INFO) << "BootProfileSnapshotOkNoProfiles"; |
| 1020 | char* boot_classpath = getenv("BOOTCLASSPATH"); |
| 1021 | ASSERT_TRUE(boot_classpath != nullptr); |
| 1022 | |
| 1023 | // The app_apk_dir has no profiles. So we shouldn't be able to merge anything. |
| 1024 | // Still, this is not a failure case. |
| 1025 | UpdateAndroidProfilesDir(app_apk_dir_); |
| 1026 | createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true); |
| 1027 | } |
| 1028 | |
| 1029 | // Verify that profile collection. |
| 1030 | TEST_F(BootProfileTest, CollectProfiles) { |
| 1031 | LOG(INFO) << "CollectProfiles"; |
| 1032 | |
| 1033 | // Create some profile directories mimicking the real profile structure. |
| 1034 | run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/ref"); |
| 1035 | run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/0/"); |
| 1036 | run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/1/"); |
| 1037 | // Create an empty profile. |
| 1038 | run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/1/primary.prof"); |
| 1039 | // Create a random file. |
| 1040 | run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/0/non.profile.file"); |
| 1041 | |
| 1042 | // Create some non-empty profiles. |
| 1043 | std::string current_prof = app_private_dir_de_ + "/profiles/cur/0/primary.prof"; |
| 1044 | run_cmd("echo 1 > " + current_prof); |
| 1045 | std::string ref_prof = app_private_dir_de_ + "/profiles/ref/primary.prof"; |
| 1046 | run_cmd("echo 1 > " + ref_prof); |
| 1047 | |
| 1048 | UpdateAndroidProfilesDir(app_private_dir_de_ + "/profiles"); |
| 1049 | |
| 1050 | std::vector<std::string> profiles; |
| 1051 | collect_profiles(&profiles); |
| 1052 | |
| 1053 | // Only two profiles should be in the output. |
| 1054 | ASSERT_EQ(2u, profiles.size()); |
| 1055 | ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), current_prof) != profiles.end()); |
| 1056 | ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), ref_prof) != profiles.end()); |
| 1057 | } |
| 1058 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1059 | } // namespace installd |
| 1060 | } // namespace android |