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