blob: e2afe1d4a3e4a79846b2be9b730e811cb9bae148 [file] [log] [blame]
Calin Juravle7d765462017-09-04 15:57:10 -07001/*
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 Juravle0d0a4922018-01-23 19:54:11 -080017#include <cstdlib>
Calin Juravle29591732017-11-20 17:46:19 -080018#include <fcntl.h>
Calin Juravle7d765462017-09-04 15:57:10 -070019#include <stdlib.h>
20#include <string.h>
Calin Juravle29591732017-11-20 17:46:19 -080021#include <sys/types.h>
22#include <sys/stat.h>
Calin Juravle7d765462017-09-04 15:57:10 -070023
Calin Juravle29591732017-11-20 17:46:19 -080024#include <android-base/file.h>
Calin Juravle7d765462017-09-04 15:57:10 -070025#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
Calin Juravle29591732017-11-20 17:46:19 -080027#include <android-base/unique_fd.h>
Andreas Gampef448ced2019-01-31 09:22:27 -080028#include <binder/Status.h>
Calin Juravle7d765462017-09-04 15:57:10 -070029#include <cutils/properties.h>
Calin Juravle29591732017-11-20 17:46:19 -080030
Calin Juravle7d765462017-09-04 15:57:10 -070031#include <gtest/gtest.h>
32
Calin Juravle29591732017-11-20 17:46:19 -080033#include <selinux/android.h>
34#include <selinux/avc.h>
35
Andreas Gampef448ced2019-01-31 09:22:27 -080036#include "binder_test_utils.h"
Calin Juravle7d765462017-09-04 15:57:10 -070037#include "dexopt.h"
38#include "InstalldNativeService.h"
39#include "globals.h"
40#include "tests/test_utils.h"
41#include "utils.h"
42
Calin Juravle29591732017-11-20 17:46:19 -080043using android::base::ReadFully;
44using android::base::unique_fd;
45
Calin Juravle7d765462017-09-04 15:57:10 -070046namespace android {
47namespace installd {
48
49// TODO(calin): try to dedup this code.
50#if defined(__arm__)
51static const std::string kRuntimeIsa = "arm";
52#elif defined(__aarch64__)
53static const std::string kRuntimeIsa = "arm64";
54#elif defined(__mips__) && !defined(__LP64__)
55static const std::string kRuntimeIsa = "mips";
56#elif defined(__mips__) && defined(__LP64__)
57static const std::string kRuntimeIsa = "mips64";
58#elif defined(__i386__)
59static const std::string kRuntimeIsa = "x86";
60#elif defined(__x86_64__)
61static const std::string kRuntimeIsa = "x86_64";
62#else
63static const std::string kRuntimeIsa = "none";
64#endif
65
66int get_property(const char *key, char *value, const char *default_value) {
67 return property_get(key, value, default_value);
68}
69
70bool 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
75bool 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
80bool 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
84static void run_cmd(const std::string& cmd) {
85 system(cmd.c_str());
86}
87
Andreas Gampe2ef09a72018-08-29 14:50:41 -070088static 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 Juravle7d765462017-09-04 15:57:10 -070098}
99
Calin Juravle29591732017-11-20 17:46:19 -0800100static 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
121static 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 Juravle7d765462017-09-04 15:57:10 -0700136// Base64 encoding of a simple dex files with 2 methods.
137static 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 Gampe1b8700f2018-11-01 14:47:00 -0700147class DexoptTestEnvTest : public testing::Test {
148};
149
150TEST_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 Gampe1d1e7c92018-11-05 09:46:28 -0800159 ASSERT_TRUE(1 == security_getenforce() || kIsX86 || true /* b/119032200 */);
Andreas Gampe1b8700f2018-11-01 14:47:00 -0700160}
Calin Juravle7d765462017-09-04 15:57:10 -0700161
162class DexoptTest : public testing::Test {
163protected:
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 Juravle7d765462017-09-04 15:57:10 -0700169 static constexpr int32_t kTestUserId = 0;
Calin Juravled2affb82017-11-28 17:41:43 -0800170 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 Juravle7d765462017-09-04 15:57:10 -0700174
175 InstalldNativeService* service_;
176 std::unique_ptr<std::string> volume_uuid_;
177 std::string package_name_;
Calin Juravle0d0a4922018-01-23 19:54:11 -0800178 std::string apk_path_;
Calin Juravle7d765462017-09-04 15:57:10 -0700179 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 Juravlec4f6a0b2018-02-01 01:27:24 +0000183 std::string app_oat_dir_;
Calin Juravle7d765462017-09-04 15:57:10 -0700184
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 Juravle29591732017-11-20 17:46:19 -0800194 // 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 Juravle7d765462017-09-04 15:57:10 -0700201 service_ = new InstalldNativeService();
202
203 volume_uuid_ = nullptr;
204 package_name_ = "com.installd.test.dexopt";
205 se_info_ = "default";
Calin Juravle7d765462017-09-04 15:57:10 -0700206 app_apk_dir_ = android_app_dir + package_name_;
207
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700208 ASSERT_TRUE(create_mock_app());
Calin Juravle7d765462017-09-04 15:57:10 -0700209 }
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 Gampe2ef09a72018-08-29 14:50:41 -0700222 ::testing::AssertionResult create_mock_app() {
Calin Juravle7d765462017-09-04 15:57:10 -0700223 // Create the oat dir.
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000224 app_oat_dir_ = app_apk_dir_ + "/oat";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700225 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 Juravle7d765462017-09-04 15:57:10 -0700234
235 // Copy the primary apk.
Calin Juravle0d0a4922018-01-23 19:54:11 -0800236 apk_path_ = app_apk_dir_ + "/base.jar";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700237 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 Juravle7d765462017-09-04 15:57:10 -0700242
243 // Create the app user data.
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700244 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 Juravle7d765462017-09-04 15:57:10 -0700257
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 Gampe2ef09a72018-08-29 14:50:41 -0700263 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 Juravle7d765462017-09-04 15:57:10 -0700272 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 Gampe2ef09a72018-08-29 14:50:41 -0700280 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 Juravle7d765462017-09-04 15:57:10 -0700289
290 // Fix app data uid.
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700291 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 Juravle7d765462017-09-04 15:57:10 -0700298 }
299
300
Calin Juravle29591732017-11-20 17:46:19 -0800301 std::string GetSecondaryDexArtifact(const std::string& path, const std::string& type) {
Calin Juravle7d765462017-09-04 15:57:10 -0700302 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 Juravle29591732017-11-20 17:46:19 -0800308 void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag,
Calin Juravle7d765462017-09-04 15:57:10 -0700309 bool should_binder_call_succeed, bool should_dex_be_compiled = true,
Andreas Gampe023b2242018-02-28 16:03:25 -0800310 /*out */ binder::Status* binder_result = nullptr, int32_t uid = -1) {
Calin Juravled2affb82017-11-28 17:41:43 -0800311 if (uid == -1) {
312 uid = kTestAppUid;
313 }
Calin Juravle7d765462017-09-04 15:57:10 -0700314 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 Brazdil570d3982018-01-16 20:15:43 +0000322 int32_t target_sdk_version = 0; // default
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000323 std::unique_ptr<std::string> profile_name_ptr = nullptr;
Calin Juravle62c5a372018-02-01 17:03:23 +0000324 std::unique_ptr<std::string> dm_path_ptr = nullptr;
Calin Juravle2efc4022018-02-13 18:31:32 -0800325 std::unique_ptr<std::string> compilation_reason_ptr = nullptr;
Calin Juravle7d765462017-09-04 15:57:10 -0700326
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 Brazdil570d3982018-01-16 20:15:43 +0000338 downgrade,
Calin Juravle408cd4a2018-01-20 23:34:18 -0800339 target_sdk_version,
Calin Juravle62c5a372018-02-01 17:03:23 +0000340 profile_name_ptr,
Calin Juravle2efc4022018-02-13 18:31:32 -0800341 dm_path_ptr,
342 compilation_reason_ptr);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800343 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravle7d765462017-09-04 15:57:10 -0700344 int expected_access = should_dex_be_compiled ? 0 : -1;
Calin Juravle29591732017-11-20 17:46:19 -0800345 std::string odex = GetSecondaryDexArtifact(path, "odex");
346 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
347 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700348 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 Gampe023b2242018-02-28 16:03:25 -0800351 if (binder_result != nullptr) {
352 *binder_result = result;
353 }
Calin Juravle7d765462017-09-04 15:57:10 -0700354 }
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 Juravled2affb82017-11-28 17:41:43 -0800358 int32_t uid = -1, std::string* package_override = nullptr) {
359 if (uid == -1) {
360 uid = kTestAppUid;
361 }
Calin Juravle7d765462017-09-04 15:57:10 -0700362 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 Gampef6a93b12018-02-27 09:51:15 -0800374 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravle7d765462017-09-04 15:57:10 -0700375 ASSERT_EQ(should_dex_exist, out_secondary_dex_exists);
376
377 int expected_access = should_dex_be_deleted ? -1 : 0;
Calin Juravle29591732017-11-20 17:46:19 -0800378 std::string odex = GetSecondaryDexArtifact(path, "odex");
379 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
380 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700381 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 Juravle29591732017-11-20 17:46:19 -0800385
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 Juravlec4f6a0b2018-02-01 01:27:24 +0000393
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 Gampe023b2242018-02-28 16:03:25 -0800399 binder::Status* binder_result = nullptr,
Calin Juravle62c5a372018-02-01 17:03:23 +0000400 const char* dm_path = nullptr,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000401 bool downgrade = false) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800402 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 Juravlec4f6a0b2018-02-01 01:27:24 +0000411 }
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 Gampe023b2242018-02-28 16:03:25 -0800418 binder::Status* binder_result = nullptr,
Calin Juravle62c5a372018-02-01 17:03:23 +0000419 const char* dm_path = nullptr,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000420 bool downgrade = false) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800421 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 Juravlec4f6a0b2018-02-01 01:27:24 +0000430 }
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 Juravle62c5a372018-02-01 17:03:23 +0000437 const char* dm_path,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000438 bool downgrade,
Andreas Gampe023b2242018-02-28 16:03:25 -0800439 bool should_binder_call_succeed,
440 /*out */ binder::Status* binder_result) {
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000441 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 Juravle62c5a372018-02-01 17:03:23 +0000448 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 Juravle2efc4022018-02-13 18:31:32 -0800452 std::unique_ptr<std::string> compilation_reason_ptr(new std::string("test-reason"));
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000453
454 bool prof_result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800455 ASSERT_BINDER_SUCCESS(service_->prepareAppProfile(
Jeff Sharkey1ebe77f2018-02-27 11:40:45 -0700456 package_name_, kTestUserId, kTestAppId, *profile_name_ptr, apk_path_,
Andreas Gampef448ced2019-01-31 09:22:27 -0800457 /*dex_metadata*/ nullptr, &prof_result));
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000458 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 Juravle62c5a372018-02-01 17:03:23 +0000473 profile_name_ptr,
Calin Juravle2efc4022018-02-13 18:31:32 -0800474 dm_path_ptr,
475 compilation_reason_ptr);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800476 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000477
478 if (!should_binder_call_succeed) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800479 if (binder_result != nullptr) {
480 *binder_result = result;
481 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000482 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 Juravle5bd1c722018-02-01 17:23:54 +0000491 bool is_public = (dex_flags & DEXOPT_PUBLIC) != 0;
492 mode_t mode = S_IFREG | (is_public ? 0644 : 0640);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000493 CheckFileAccess(odex, kSystemUid, uid, mode);
494 CheckFileAccess(vdex, kSystemUid, uid, mode);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000495
Calin Juravle5bd1c722018-02-01 17:23:54 +0000496 if (compiler_filter == "speed-profile") {
497 CheckFileAccess(art, kSystemUid, uid, mode);
498 }
Andreas Gampe023b2242018-02-28 16:03:25 -0800499 if (binder_result != nullptr) {
500 *binder_result = result;
501 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000502 }
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 Juravle7d765462017-09-04 15:57:10 -0700523};
524
525
526TEST_F(DexoptTest, DexoptSecondaryCe) {
527 LOG(INFO) << "DexoptSecondaryCe";
Calin Juravle29591732017-11-20 17:46:19 -0800528 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700529 /*binder_ok*/ true, /*compile_ok*/ true);
530}
531
532TEST_F(DexoptTest, DexoptSecondaryCeLink) {
533 LOG(INFO) << "DexoptSecondaryCeLink";
Calin Juravle29591732017-11-20 17:46:19 -0800534 CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700535 /*binder_ok*/ true, /*compile_ok*/ true);
536}
537
538TEST_F(DexoptTest, DexoptSecondaryDe) {
539 LOG(INFO) << "DexoptSecondaryDe";
Calin Juravle29591732017-11-20 17:46:19 -0800540 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700541 /*binder_ok*/ true, /*compile_ok*/ true);
542}
543
544TEST_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 Gampe023b2242018-02-28 16:03:25 -0800547 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800548 CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800549 /*binder_ok*/ true, /*compile_ok*/ false, &status);
550 EXPECT_STREQ(status.toString8().c_str(), "No error");
Calin Juravle7d765462017-09-04 15:57:10 -0700551}
552
553TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) {
554 LOG(INFO) << "DexoptSecondaryStorageValidationError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800555 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800556 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800557 /*binder_ok*/ false, /*compile_ok*/ false, &status);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800558 EXPECT_STREQ(status.toString8().c_str(),
559 "Status(-8): '-1: Dexoptanalyzer path validation failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700560}
561
562TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) {
563 LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800564 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800565 CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800566 /*binder_ok*/ false, /*compile_ok*/ false, &status);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800567 EXPECT_STREQ(status.toString8().c_str(),
568 "Status(-8): '-1: Dexoptanalyzer path validation failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700569}
570
571TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) {
572 LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800573 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800574 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800575 /*binder_ok*/ false, /*compile_ok*/ false, &status, kSystemUid);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800576 EXPECT_STREQ(status.toString8().c_str(), "Status(-8): '-1: Dexoptanalyzer open zip failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700577}
578
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000579TEST_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 Juravle5bd1c722018-02-01 17:23:54 +0000588TEST_F(DexoptTest, DexoptPrimaryFailedInvalidFilter) {
589 LOG(INFO) << "DexoptPrimaryFailedInvalidFilter";
Andreas Gampe023b2242018-02-28 16:03:25 -0800590 binder::Status status;
Calin Juravle5bd1c722018-02-01 17:23:54 +0000591 CompilePrimaryDexFail("awesome-filter",
592 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC,
593 app_oat_dir_.c_str(),
594 kTestAppGid,
Andreas Gampe023b2242018-02-28 16:03:25 -0800595 DEX2OAT_FROM_SCRATCH,
596 &status);
597 EXPECT_STREQ(status.toString8().c_str(),
598 "Status(-8): \'256: Dex2oat invocation for "
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800599 "/data/app/com.installd.test.dexopt/base.jar failed: unspecified dex2oat error'");
Calin Juravle5bd1c722018-02-01 17:23:54 +0000600}
601
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000602TEST_F(DexoptTest, DexoptPrimaryProfileNonPublic) {
603 LOG(INFO) << "DexoptPrimaryProfileNonPublic";
604 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700605 DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000606 app_oat_dir_.c_str(),
607 kTestAppGid,
608 DEX2OAT_FROM_SCRATCH);
609}
610
611TEST_F(DexoptTest, DexoptPrimaryProfilePublic) {
612 LOG(INFO) << "DexoptPrimaryProfilePublic";
Calin Juravle5bd1c722018-02-01 17:23:54 +0000613 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700614 DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_PUBLIC |
615 DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000616 app_oat_dir_.c_str(),
617 kTestAppGid,
618 DEX2OAT_FROM_SCRATCH);
619}
620
621TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) {
622 LOG(INFO) << "DexoptPrimaryBackgroundOk";
623 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700624 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED |
625 DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000626 app_oat_dir_.c_str(),
627 kTestAppGid,
628 DEX2OAT_FROM_SCRATCH);
629}
630
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000631class 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
658TEST_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 Juravle7d765462017-09-04 15:57:10 -0700667
668class ReconcileTest : public DexoptTest {
669 virtual void SetUp() {
670 DexoptTest::SetUp();
Calin Juravle29591732017-11-20 17:46:19 -0800671 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700672 /*binder_ok*/ true, /*compile_ok*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800673 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700674 /*binder_ok*/ true, /*compile_ok*/ true);
675 }
676};
677
678TEST_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
684TEST_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
690TEST_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
696TEST_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
703TEST_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
712TEST_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
720TEST_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 Juravle29591732017-11-20 17:46:19 -0800726class ProfileTest : public DexoptTest {
727 protected:
728 std::string cur_profile_;
729 std::string ref_profile_;
730 std::string snap_profile_;
731
Calin Juravle824a64d2018-01-18 20:23:17 -0800732 static constexpr const char* kPrimaryProfile = "primary.prof";
733
Calin Juravle29591732017-11-20 17:46:19 -0800734 virtual void SetUp() {
735 DexoptTest::SetUp();
736 cur_profile_ = create_current_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -0800737 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 Juravle29591732017-11-20 17:46:19 -0800741 }
742
Calin Juravle824a64d2018-01-18 20:23:17 -0800743 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 Juravle29591732017-11-20 17:46:19 -0800748 ::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 Juravle824a64d2018-01-18 20:23:17 -0800755 SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0600, 2);
Calin Juravle29591732017-11-20 17:46:19 -0800756 }
757 }
758
Calin Juravlec3596c32017-12-05 12:29:15 -0800759 void createProfileSnapshot(int32_t appid, const std::string& package_name,
760 bool expected_result) {
Calin Juravle29591732017-11-20 17:46:19 -0800761 bool result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800762 ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
763 appid, package_name, kPrimaryProfile, apk_path_, &result));
Calin Juravle29591732017-11-20 17:46:19 -0800764 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 Juravle40b5f462018-02-07 04:11:52 -0800780 " --reference-profile-file=" + expected_profile_content +
781 " --apk=" + apk_path_);
Calin Juravle29591732017-11-20 17:46:19 -0800782
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 Juravle408cd4a2018-01-20 23:34:18 -0800799 void mergePackageProfiles(const std::string& package_name,
800 const std::string& code_path,
801 bool expected_result) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800802 bool result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800803 ASSERT_BINDER_SUCCESS(service_->mergeProfiles(
804 kTestAppUid, package_name, code_path, &result));
Calin Juravle824a64d2018-01-18 20:23:17 -0800805 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 Juravlec3b049e2018-01-18 22:32:58 -0800826 // 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 Gampef448ced2019-01-31 09:22:27 -0800830 ASSERT_BINDER_SUCCESS(service_->prepareAppProfile(
Jeff Sharkey1ebe77f2018-02-27 11:40:45 -0700831 package_name, kTestUserId, kTestAppId, profile_name, apk_path_,
Andreas Gampef448ced2019-01-31 09:22:27 -0800832 /*dex_metadata*/ nullptr, &result));
Calin Juravlec3b049e2018-01-18 22:32:58 -0800833 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 Juravle0d0a4922018-01-23 19:54:11 -0800852 protected:
Calin Juravle29591732017-11-20 17:46:19 -0800853 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
889TEST_F(ProfileTest, ProfileSnapshotOk) {
890 LOG(INFO) << "ProfileSnapshotOk";
891
892 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800893 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800894}
895
896// The reference profile is created on the fly. We need to be able to
897// snapshot without one.
898TEST_F(ProfileTest, ProfileSnapshotOkNoReference) {
899 LOG(INFO) << "ProfileSnapshotOkNoReference";
900
901 SetupProfiles(/*setup_ref*/ false);
Calin Juravlec3596c32017-12-05 12:29:15 -0800902 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800903}
904
905TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) {
906 LOG(INFO) << "ProfileSnapshotFailWrongPackage";
907
908 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800909 createProfileSnapshot(kTestAppId, "not.there", /*expected_result*/ false);
Calin Juravle29591732017-11-20 17:46:19 -0800910}
911
912TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) {
913 LOG(INFO) << "ProfileSnapshotDestroySnapshot";
914
915 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800916 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800917
Andreas Gampef448ced2019-01-31 09:22:27 -0800918 ASSERT_BINDER_SUCCESS(service_->destroyProfileSnapshot(package_name_, kPrimaryProfile));
Calin Juravle29591732017-11-20 17:46:19 -0800919 struct stat st;
920 ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st));
921 ASSERT_EQ(ENOENT, errno);
922}
923
Calin Juravle824a64d2018-01-18 20:23:17 -0800924TEST_F(ProfileTest, ProfileMergeOk) {
925 LOG(INFO) << "ProfileMergeOk";
926
927 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -0800928 mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true);
Calin Juravle824a64d2018-01-18 20:23:17 -0800929}
930
931// The reference profile is created on the fly. We need to be able to
932// merge without one.
933TEST_F(ProfileTest, ProfileMergeOkNoReference) {
934 LOG(INFO) << "ProfileMergeOkNoReference";
935
936 SetupProfiles(/*setup_ref*/ false);
Calin Juravle408cd4a2018-01-20 23:34:18 -0800937 mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true);
Calin Juravle824a64d2018-01-18 20:23:17 -0800938}
939
940TEST_F(ProfileTest, ProfileMergeFailWrongPackage) {
941 LOG(INFO) << "ProfileMergeFailWrongPackage";
942
943 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -0800944 mergePackageProfiles("not.there", "primary.prof", /*expected_result*/ false);
Calin Juravle824a64d2018-01-18 20:23:17 -0800945}
946
Calin Juravled2affb82017-11-28 17:41:43 -0800947TEST_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 Juravle824a64d2018-01-18 20:23:17 -0800953 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravled2affb82017-11-28 17:41:43 -0800954 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 Juravle6f06eb62017-11-28 18:44:53 -0800957 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).
962TEST_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 Juravle824a64d2018-01-18 20:23:17 -0800968 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravle6f06eb62017-11-28 18:44:53 -0800969 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 Gampef448ced2019-01-31 09:22:27 -0800976 ASSERT_BINDER_SUCCESS(service_->createAppData(
Calin Juravle6f06eb62017-11-28 18:44:53 -0800977 volume_uuid_,
978 package_name_,
979 kTestUserId,
980 kAppDataFlags,
981 kTestAppUid,
982 se_info_,
983 kOSdkVersion,
Andreas Gampef448ced2019-01-31 09:22:27 -0800984 &ce_data_inode_));
Calin Juravle6f06eb62017-11-28 18:44:53 -0800985
986 // Check the file access.
987 CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
Calin Juravle6f06eb62017-11-28 18:44:53 -0800988 CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR);
Calin Juravled2affb82017-11-28 17:41:43 -0800989}
990
Calin Juravlec3b049e2018-01-18 22:32:58 -0800991TEST_F(ProfileTest, ProfilePrepareOk) {
992 LOG(INFO) << "ProfilePrepareOk";
993 preparePackageProfile(package_name_, "split.prof", /*expected_result*/ true);
994}
995
996TEST_F(ProfileTest, ProfilePrepareFailInvalidPackage) {
997 LOG(INFO) << "ProfilePrepareFailInvalidPackage";
998 preparePackageProfile("not.there.package", "split.prof", /*expected_result*/ false);
999}
1000
1001TEST_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 Juravle0d0a4922018-01-23 19:54:11 -08001009
1010class 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 Gampef448ced2019-01-31 09:22:27 -08001030 ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
1031 -1, "android", "android.prof", classpath, &result));
Calin Juravle0d0a4922018-01-23 19:54:11 -08001032 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
1060TEST_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
1067TEST_F(BootProfileTest, BootProfileSnapshotFailEmptyClasspath) {
1068 LOG(INFO) << "BootProfileSnapshotFailEmptyClasspath";
1069
1070 createBootImageProfileSnapshot(/*boot_classpath*/ "", /*expected_result*/ false);
1071}
1072
1073TEST_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.
1085TEST_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 Juravle7d765462017-09-04 15:57:10 -07001114} // namespace installd
1115} // namespace android