blob: f216c53be49c0b6f8bc8ffde5ef1e69afe705b05 [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>
28
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
Calin Juravle7d765462017-09-04 15:57:10 -070036#include "dexopt.h"
37#include "InstalldNativeService.h"
38#include "globals.h"
39#include "tests/test_utils.h"
40#include "utils.h"
41
Calin Juravle29591732017-11-20 17:46:19 -080042using android::base::ReadFully;
43using android::base::unique_fd;
44
Calin Juravle7d765462017-09-04 15:57:10 -070045namespace android {
46namespace installd {
47
48// TODO(calin): try to dedup this code.
49#if defined(__arm__)
50static const std::string kRuntimeIsa = "arm";
51#elif defined(__aarch64__)
52static const std::string kRuntimeIsa = "arm64";
53#elif defined(__mips__) && !defined(__LP64__)
54static const std::string kRuntimeIsa = "mips";
55#elif defined(__mips__) && defined(__LP64__)
56static const std::string kRuntimeIsa = "mips64";
57#elif defined(__i386__)
58static const std::string kRuntimeIsa = "x86";
59#elif defined(__x86_64__)
60static const std::string kRuntimeIsa = "x86_64";
61#else
62static const std::string kRuntimeIsa = "none";
63#endif
64
65int get_property(const char *key, char *value, const char *default_value) {
66 return property_get(key, value, default_value);
67}
68
69bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
70 const char *instruction_set) {
71 return calculate_oat_file_path_default(path, oat_dir, apk_path, instruction_set);
72}
73
74bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
75 const char *instruction_set) {
76 return calculate_odex_file_path_default(path, apk_path, instruction_set);
77}
78
79bool create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) {
80 return create_cache_path_default(path, src, instruction_set);
81}
82
83static void run_cmd(const std::string& cmd) {
84 system(cmd.c_str());
85}
86
Andreas Gampe2ef09a72018-08-29 14:50:41 -070087static int mkdir(const std::string& path, uid_t owner, gid_t group, mode_t mode) {
88 int ret = ::mkdir(path.c_str(), mode);
89 if (ret != 0) {
90 return ret;
91 }
92 ret = ::chown(path.c_str(), owner, group);
93 if (ret != 0) {
94 return ret;
95 }
96 return ::chmod(path.c_str(), mode);
Calin Juravle7d765462017-09-04 15:57:10 -070097}
98
Calin Juravle29591732017-11-20 17:46:19 -080099static int log_callback(int type, const char *fmt, ...) { // NOLINT
100 va_list ap;
101 int priority;
102
103 switch (type) {
104 case SELINUX_WARNING:
105 priority = ANDROID_LOG_WARN;
106 break;
107 case SELINUX_INFO:
108 priority = ANDROID_LOG_INFO;
109 break;
110 default:
111 priority = ANDROID_LOG_ERROR;
112 break;
113 }
114 va_start(ap, fmt);
115 LOG_PRI_VA(priority, "SELinux", fmt, ap);
116 va_end(ap);
117 return 0;
118}
119
120static bool init_selinux() {
121 int selinux_enabled = (is_selinux_enabled() > 0);
122
123 union selinux_callback cb;
124 cb.func_log = log_callback;
125 selinux_set_callback(SELINUX_CB_LOG, cb);
126
127 if (selinux_enabled && selinux_status_open(true) < 0) {
128 LOG(ERROR) << "Could not open selinux status; exiting";
129 return false;
130 }
131
132 return true;
133}
134
Calin Juravle7d765462017-09-04 15:57:10 -0700135// Base64 encoding of a simple dex files with 2 methods.
136static const char kDexFile[] =
137 "UEsDBBQAAAAIAOiOYUs9y6BLCgEAABQCAAALABwAY2xhc3Nlcy5kZXhVVAkAA/Ns+lkOHv1ZdXgL"
138 "AAEEI+UCAASIEwAAS0mt4DIwNmX4qpn7j/2wA7v7N+ZvoQpCJRlVx5SWa4YaiDAxMBQwMDBUhJkI"
139 "MUBBDyMDAzsDRJwFxAdioBDDHAYEYAbiFUAM1M5wAIhFGCGKDIDYAogdgNgDiH2BOAiI0xghekDm"
140 "sQIxGxQzM6ACRijNhCbOhCZfyohdPYyuh8szgtVkMkLsLhAAqeCDi+ejibPZZOZlltgxsDnqZSWW"
141 "JTKwOUFoZh9HayDhZM0g5AMS0M9JzEvX90/KSk0usWZgDAMaws5nAyXBzmpoYGlgAjsAyJoBMp0b"
142 "zQ8gGhbOTEhhzYwU3qxIYc2GFN6MClC/AhUyKUDMAYU9M1Qc5F8GKBscVgIQM0FxCwBQSwECHgMU"
143 "AAAACADojmFLPcugSwoBAAAUAgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAA/Ns"
144 "+ll1eAsAAQQj5QIABIgTAABQSwUGAAAAAAEAAQBRAAAATwEAAAAA";
145
146
147class DexoptTest : public testing::Test {
148protected:
149 static constexpr bool kDebug = false;
150 static constexpr uid_t kSystemUid = 1000;
151 static constexpr uid_t kSystemGid = 1000;
152 static constexpr int32_t kOSdkVersion = 25;
153 static constexpr int32_t kAppDataFlags = FLAG_STORAGE_CE | FLAG_STORAGE_DE;
Calin Juravle7d765462017-09-04 15:57:10 -0700154 static constexpr int32_t kTestUserId = 0;
Calin Juravled2affb82017-11-28 17:41:43 -0800155 static constexpr uid_t kTestAppId = 19999;
156
157 const gid_t kTestAppUid = multiuser_get_uid(kTestUserId, kTestAppId);
158 const uid_t kTestAppGid = multiuser_get_shared_gid(kTestUserId, kTestAppId);
Calin Juravle7d765462017-09-04 15:57:10 -0700159
160 InstalldNativeService* service_;
161 std::unique_ptr<std::string> volume_uuid_;
162 std::string package_name_;
Calin Juravle0d0a4922018-01-23 19:54:11 -0800163 std::string apk_path_;
Calin Juravle7d765462017-09-04 15:57:10 -0700164 std::string app_apk_dir_;
165 std::string app_private_dir_ce_;
166 std::string app_private_dir_de_;
167 std::string se_info_;
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000168 std::string app_oat_dir_;
Calin Juravle7d765462017-09-04 15:57:10 -0700169
170 int64_t ce_data_inode_;
171
172 std::string secondary_dex_ce_;
173 std::string secondary_dex_ce_link_;
174 std::string secondary_dex_de_;
175
176 virtual void SetUp() {
177 setenv("ANDROID_LOG_TAGS", "*:v", 1);
178 android::base::InitLogging(nullptr);
Calin Juravle29591732017-11-20 17:46:19 -0800179 // Initialize the globals holding the file system main paths (/data/, /system/ etc..).
180 // This is needed in order to compute the application and profile paths.
181 ASSERT_TRUE(init_globals_from_data_and_root());
182 // Initialize selinux log callbacks.
183 // This ensures that selinux is up and running and re-directs the selinux messages
184 // to logcat (in order to make it easier to investigate test results).
185 ASSERT_TRUE(init_selinux());
Calin Juravle7d765462017-09-04 15:57:10 -0700186 service_ = new InstalldNativeService();
187
188 volume_uuid_ = nullptr;
189 package_name_ = "com.installd.test.dexopt";
190 se_info_ = "default";
Calin Juravle7d765462017-09-04 15:57:10 -0700191 app_apk_dir_ = android_app_dir + package_name_;
192
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700193 ASSERT_TRUE(create_mock_app());
Calin Juravle7d765462017-09-04 15:57:10 -0700194 }
195
196 virtual void TearDown() {
197 if (!kDebug) {
198 service_->destroyAppData(
199 volume_uuid_, package_name_, kTestUserId, kAppDataFlags, ce_data_inode_);
200 run_cmd("rm -rf " + app_apk_dir_);
201 run_cmd("rm -rf " + app_private_dir_ce_);
202 run_cmd("rm -rf " + app_private_dir_de_);
203 }
204 delete service_;
205 }
206
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700207 ::testing::AssertionResult create_mock_app() {
Calin Juravle7d765462017-09-04 15:57:10 -0700208 // Create the oat dir.
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000209 app_oat_dir_ = app_apk_dir_ + "/oat";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700210 if (mkdir(app_apk_dir_, kSystemUid, kSystemGid, 0755) != 0) {
211 return ::testing::AssertionFailure() << "Could not create app dir " << app_apk_dir_
212 << " : " << strerror(errno);
213 }
214 binder::Status status = service_->createOatDir(app_oat_dir_, kRuntimeIsa);
215 if (!status.isOk()) {
216 return ::testing::AssertionFailure() << "Could not create oat dir: "
217 << status.toString8().c_str();
218 }
Calin Juravle7d765462017-09-04 15:57:10 -0700219
220 // Copy the primary apk.
Calin Juravle0d0a4922018-01-23 19:54:11 -0800221 apk_path_ = app_apk_dir_ + "/base.jar";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700222 std::string error_msg;
223 if (!WriteBase64ToFile(kDexFile, apk_path_, kSystemUid, kSystemGid, 0644, &error_msg)) {
224 return ::testing::AssertionFailure() << "Could not write base64 file to " << apk_path_
225 << " : " << error_msg;
226 }
Calin Juravle7d765462017-09-04 15:57:10 -0700227
228 // Create the app user data.
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700229 status = service_->createAppData(
230 volume_uuid_,
231 package_name_,
232 kTestUserId,
233 kAppDataFlags,
234 kTestAppUid,
235 se_info_,
236 kOSdkVersion,
237 &ce_data_inode_);
238 if (!status.isOk()) {
239 return ::testing::AssertionFailure() << "Could not create app data: "
240 << status.toString8().c_str();
241 }
Calin Juravle7d765462017-09-04 15:57:10 -0700242
243 // Create a secondary dex file on CE storage
244 const char* volume_uuid_cstr = volume_uuid_ == nullptr ? nullptr : volume_uuid_->c_str();
245 app_private_dir_ce_ = create_data_user_ce_package_path(
246 volume_uuid_cstr, kTestUserId, package_name_.c_str());
247 secondary_dex_ce_ = app_private_dir_ce_ + "/secondary_ce.jar";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700248 if (!WriteBase64ToFile(kDexFile,
249 secondary_dex_ce_,
250 kTestAppUid,
251 kTestAppGid,
252 0600,
253 &error_msg)) {
254 return ::testing::AssertionFailure() << "Could not write base64 file to "
255 << secondary_dex_ce_ << " : " << error_msg;
256 }
Calin Juravle7d765462017-09-04 15:57:10 -0700257 std::string app_private_dir_ce_link = create_data_user_ce_package_path_as_user_link(
258 volume_uuid_cstr, kTestUserId, package_name_.c_str());
259 secondary_dex_ce_link_ = app_private_dir_ce_link + "/secondary_ce.jar";
260
261 // Create a secondary dex file on DE storage.
262 app_private_dir_de_ = create_data_user_de_package_path(
263 volume_uuid_cstr, kTestUserId, package_name_.c_str());
264 secondary_dex_de_ = app_private_dir_de_ + "/secondary_de.jar";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700265 if (!WriteBase64ToFile(kDexFile,
266 secondary_dex_de_,
267 kTestAppUid,
268 kTestAppGid,
269 0600,
270 &error_msg)) {
271 return ::testing::AssertionFailure() << "Could not write base64 file to "
272 << secondary_dex_de_ << " : " << error_msg;
273 }
Calin Juravle7d765462017-09-04 15:57:10 -0700274
275 // Fix app data uid.
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700276 status = service_->fixupAppData(volume_uuid_, kTestUserId);
277 if (!status.isOk()) {
278 return ::testing::AssertionFailure() << "Could not fixup app data: "
279 << status.toString8().c_str();
280 }
281
282 return ::testing::AssertionSuccess();
Calin Juravle7d765462017-09-04 15:57:10 -0700283 }
284
285
Calin Juravle29591732017-11-20 17:46:19 -0800286 std::string GetSecondaryDexArtifact(const std::string& path, const std::string& type) {
Calin Juravle7d765462017-09-04 15:57:10 -0700287 std::string::size_type end = path.rfind('.');
288 std::string::size_type start = path.rfind('/', end);
289 return path.substr(0, start) + "/oat/" + kRuntimeIsa + "/" +
290 path.substr(start + 1, end - start) + type;
291 }
292
Calin Juravle29591732017-11-20 17:46:19 -0800293 void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag,
Calin Juravle7d765462017-09-04 15:57:10 -0700294 bool should_binder_call_succeed, bool should_dex_be_compiled = true,
Andreas Gampe023b2242018-02-28 16:03:25 -0800295 /*out */ binder::Status* binder_result = nullptr, int32_t uid = -1) {
Calin Juravled2affb82017-11-28 17:41:43 -0800296 if (uid == -1) {
297 uid = kTestAppUid;
298 }
Calin Juravle7d765462017-09-04 15:57:10 -0700299 std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_));
300 int32_t dexopt_needed = 0; // does not matter;
301 std::unique_ptr<std::string> out_path = nullptr; // does not matter
302 int32_t dex_flags = DEXOPT_SECONDARY_DEX | dex_storage_flag;
303 std::string compiler_filter = "speed-profile";
304 std::unique_ptr<std::string> class_loader_context_ptr(new std::string("&"));
305 std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_));
306 bool downgrade = false;
David Brazdil570d3982018-01-16 20:15:43 +0000307 int32_t target_sdk_version = 0; // default
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000308 std::unique_ptr<std::string> profile_name_ptr = nullptr;
Calin Juravle62c5a372018-02-01 17:03:23 +0000309 std::unique_ptr<std::string> dm_path_ptr = nullptr;
Calin Juravle2efc4022018-02-13 18:31:32 -0800310 std::unique_ptr<std::string> compilation_reason_ptr = nullptr;
Calin Juravle7d765462017-09-04 15:57:10 -0700311
312 binder::Status result = service_->dexopt(path,
313 uid,
314 package_name_ptr,
315 kRuntimeIsa,
316 dexopt_needed,
317 out_path,
318 dex_flags,
319 compiler_filter,
320 volume_uuid_,
321 class_loader_context_ptr,
322 se_info_ptr,
David Brazdil570d3982018-01-16 20:15:43 +0000323 downgrade,
Calin Juravle408cd4a2018-01-20 23:34:18 -0800324 target_sdk_version,
Calin Juravle62c5a372018-02-01 17:03:23 +0000325 profile_name_ptr,
Calin Juravle2efc4022018-02-13 18:31:32 -0800326 dm_path_ptr,
327 compilation_reason_ptr);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800328 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravle7d765462017-09-04 15:57:10 -0700329 int expected_access = should_dex_be_compiled ? 0 : -1;
Calin Juravle29591732017-11-20 17:46:19 -0800330 std::string odex = GetSecondaryDexArtifact(path, "odex");
331 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
332 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700333 ASSERT_EQ(expected_access, access(odex.c_str(), R_OK));
334 ASSERT_EQ(expected_access, access(vdex.c_str(), R_OK));
335 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
Andreas Gampe023b2242018-02-28 16:03:25 -0800336 if (binder_result != nullptr) {
337 *binder_result = result;
338 }
Calin Juravle7d765462017-09-04 15:57:10 -0700339 }
340
341 void reconcile_secondary_dex(const std::string& path, int32_t storage_flag,
342 bool should_binder_call_succeed, bool should_dex_exist, bool should_dex_be_deleted,
Calin Juravled2affb82017-11-28 17:41:43 -0800343 int32_t uid = -1, std::string* package_override = nullptr) {
344 if (uid == -1) {
345 uid = kTestAppUid;
346 }
Calin Juravle7d765462017-09-04 15:57:10 -0700347 std::vector<std::string> isas;
348 isas.push_back(kRuntimeIsa);
349 bool out_secondary_dex_exists = false;
350 binder::Status result = service_->reconcileSecondaryDexFile(
351 path,
352 package_override == nullptr ? package_name_ : *package_override,
353 uid,
354 isas,
355 volume_uuid_,
356 storage_flag,
357 &out_secondary_dex_exists);
358
Andreas Gampef6a93b12018-02-27 09:51:15 -0800359 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravle7d765462017-09-04 15:57:10 -0700360 ASSERT_EQ(should_dex_exist, out_secondary_dex_exists);
361
362 int expected_access = should_dex_be_deleted ? -1 : 0;
Calin Juravle29591732017-11-20 17:46:19 -0800363 std::string odex = GetSecondaryDexArtifact(path, "odex");
364 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
365 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700366 ASSERT_EQ(expected_access, access(odex.c_str(), F_OK));
367 ASSERT_EQ(expected_access, access(vdex.c_str(), F_OK));
368 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
369 }
Calin Juravle29591732017-11-20 17:46:19 -0800370
371 void CheckFileAccess(const std::string& file, uid_t uid, gid_t gid, mode_t mode) {
372 struct stat st;
373 ASSERT_EQ(0, stat(file.c_str(), &st));
374 ASSERT_EQ(uid, st.st_uid);
375 ASSERT_EQ(gid, st.st_gid);
376 ASSERT_EQ(mode, st.st_mode);
377 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000378
379 void CompilePrimaryDexOk(std::string compiler_filter,
380 int32_t dex_flags,
381 const char* oat_dir,
382 int32_t uid,
383 int32_t dexopt_needed,
Andreas Gampe023b2242018-02-28 16:03:25 -0800384 binder::Status* binder_result = nullptr,
Calin Juravle62c5a372018-02-01 17:03:23 +0000385 const char* dm_path = nullptr,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000386 bool downgrade = false) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800387 CompilePrimaryDex(compiler_filter,
388 dex_flags,
389 oat_dir,
390 uid,
391 dexopt_needed,
392 dm_path,
393 downgrade,
394 true,
395 binder_result);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000396 }
397
398 void CompilePrimaryDexFail(std::string compiler_filter,
399 int32_t dex_flags,
400 const char* oat_dir,
401 int32_t uid,
402 int32_t dexopt_needed,
Andreas Gampe023b2242018-02-28 16:03:25 -0800403 binder::Status* binder_result = nullptr,
Calin Juravle62c5a372018-02-01 17:03:23 +0000404 const char* dm_path = nullptr,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000405 bool downgrade = false) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800406 CompilePrimaryDex(compiler_filter,
407 dex_flags,
408 oat_dir,
409 uid,
410 dexopt_needed,
411 dm_path,
412 downgrade,
413 false,
414 binder_result);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000415 }
416
417 void CompilePrimaryDex(std::string compiler_filter,
418 int32_t dex_flags,
419 const char* oat_dir,
420 int32_t uid,
421 int32_t dexopt_needed,
Calin Juravle62c5a372018-02-01 17:03:23 +0000422 const char* dm_path,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000423 bool downgrade,
Andreas Gampe023b2242018-02-28 16:03:25 -0800424 bool should_binder_call_succeed,
425 /*out */ binder::Status* binder_result) {
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000426 std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_));
427 std::unique_ptr<std::string> out_path(
428 oat_dir == nullptr ? nullptr : new std::string(oat_dir));
429 std::unique_ptr<std::string> class_loader_context_ptr(new std::string("&"));
430 std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_));
431 int32_t target_sdk_version = 0; // default
432 std::unique_ptr<std::string> profile_name_ptr(new std::string("primary.prof"));
Calin Juravle62c5a372018-02-01 17:03:23 +0000433 std::unique_ptr<std::string> dm_path_ptr = nullptr;
434 if (dm_path != nullptr) {
435 dm_path_ptr.reset(new std::string(dm_path));
436 }
Calin Juravle2efc4022018-02-13 18:31:32 -0800437 std::unique_ptr<std::string> compilation_reason_ptr(new std::string("test-reason"));
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000438
439 bool prof_result;
440 binder::Status prof_binder_result = service_->prepareAppProfile(
Jeff Sharkey1ebe77f2018-02-27 11:40:45 -0700441 package_name_, kTestUserId, kTestAppId, *profile_name_ptr, apk_path_,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000442 /*dex_metadata*/ nullptr, &prof_result);
443
Andreas Gampef6a93b12018-02-27 09:51:15 -0800444 ASSERT_TRUE(prof_binder_result.isOk()) << prof_binder_result.toString8().c_str();
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000445 ASSERT_TRUE(prof_result);
446
447 binder::Status result = service_->dexopt(apk_path_,
448 uid,
449 package_name_ptr,
450 kRuntimeIsa,
451 dexopt_needed,
452 out_path,
453 dex_flags,
454 compiler_filter,
455 volume_uuid_,
456 class_loader_context_ptr,
457 se_info_ptr,
458 downgrade,
459 target_sdk_version,
Calin Juravle62c5a372018-02-01 17:03:23 +0000460 profile_name_ptr,
Calin Juravle2efc4022018-02-13 18:31:32 -0800461 dm_path_ptr,
462 compilation_reason_ptr);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800463 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000464
465 if (!should_binder_call_succeed) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800466 if (binder_result != nullptr) {
467 *binder_result = result;
468 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000469 return;
470 }
471 // Check the access to the compiler output.
472 // - speed-profile artifacts are not world-wide readable.
473 // - files are owned by the system uid.
474 std::string odex = GetPrimaryDexArtifact(oat_dir, apk_path_, "odex");
475 std::string vdex = GetPrimaryDexArtifact(oat_dir, apk_path_, "vdex");
476 std::string art = GetPrimaryDexArtifact(oat_dir, apk_path_, "art");
477
Calin Juravle5bd1c722018-02-01 17:23:54 +0000478 bool is_public = (dex_flags & DEXOPT_PUBLIC) != 0;
479 mode_t mode = S_IFREG | (is_public ? 0644 : 0640);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000480 CheckFileAccess(odex, kSystemUid, uid, mode);
481 CheckFileAccess(vdex, kSystemUid, uid, mode);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000482
Calin Juravle5bd1c722018-02-01 17:23:54 +0000483 if (compiler_filter == "speed-profile") {
484 CheckFileAccess(art, kSystemUid, uid, mode);
485 }
Andreas Gampe023b2242018-02-28 16:03:25 -0800486 if (binder_result != nullptr) {
487 *binder_result = result;
488 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000489 }
490
491 std::string GetPrimaryDexArtifact(const char* oat_dir,
492 const std::string& dex_path,
493 const std::string& type) {
494 if (oat_dir == nullptr) {
495 std::string path = dex_path;
496 for (auto it = path.begin() + 1; it < path.end(); ++it) {
497 if (*it == '/') {
498 *it = '@';
499 }
500 }
501 return android_data_dir + DALVIK_CACHE + '/' + kRuntimeIsa + "/" + path
502 + "@classes.dex";
503 } else {
504 std::string::size_type name_end = dex_path.rfind('.');
505 std::string::size_type name_start = dex_path.rfind('/');
506 return std::string(oat_dir) + "/" + kRuntimeIsa + "/" +
507 dex_path.substr(name_start + 1, name_end - name_start) + type;
508 }
509 }
Calin Juravle7d765462017-09-04 15:57:10 -0700510};
511
512
513TEST_F(DexoptTest, DexoptSecondaryCe) {
514 LOG(INFO) << "DexoptSecondaryCe";
Calin Juravle29591732017-11-20 17:46:19 -0800515 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700516 /*binder_ok*/ true, /*compile_ok*/ true);
517}
518
519TEST_F(DexoptTest, DexoptSecondaryCeLink) {
520 LOG(INFO) << "DexoptSecondaryCeLink";
Calin Juravle29591732017-11-20 17:46:19 -0800521 CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700522 /*binder_ok*/ true, /*compile_ok*/ true);
523}
524
525TEST_F(DexoptTest, DexoptSecondaryDe) {
526 LOG(INFO) << "DexoptSecondaryDe";
Calin Juravle29591732017-11-20 17:46:19 -0800527 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700528 /*binder_ok*/ true, /*compile_ok*/ true);
529}
530
531TEST_F(DexoptTest, DexoptSecondaryDoesNotExist) {
532 LOG(INFO) << "DexoptSecondaryDoesNotExist";
533 // If the file validates but does not exist we do not treat it as an error.
Andreas Gampe023b2242018-02-28 16:03:25 -0800534 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800535 CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800536 /*binder_ok*/ true, /*compile_ok*/ false, &status);
537 EXPECT_STREQ(status.toString8().c_str(), "No error");
Calin Juravle7d765462017-09-04 15:57:10 -0700538}
539
540TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) {
541 LOG(INFO) << "DexoptSecondaryStorageValidationError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800542 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800543 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800544 /*binder_ok*/ false, /*compile_ok*/ false, &status);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800545 EXPECT_STREQ(status.toString8().c_str(),
546 "Status(-8): '-1: Dexoptanalyzer path validation failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700547}
548
549TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) {
550 LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800551 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800552 CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800553 /*binder_ok*/ false, /*compile_ok*/ false, &status);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800554 EXPECT_STREQ(status.toString8().c_str(),
555 "Status(-8): '-1: Dexoptanalyzer path validation failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700556}
557
558TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) {
559 LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800560 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800561 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800562 /*binder_ok*/ false, /*compile_ok*/ false, &status, kSystemUid);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800563 EXPECT_STREQ(status.toString8().c_str(), "Status(-8): '-1: Dexoptanalyzer open zip failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700564}
565
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000566TEST_F(DexoptTest, DexoptPrimaryPublic) {
567 LOG(INFO) << "DexoptPrimaryPublic";
568 CompilePrimaryDexOk("verify",
569 DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC,
570 app_oat_dir_.c_str(),
571 kTestAppGid,
572 DEX2OAT_FROM_SCRATCH);
573}
574
Calin Juravle5bd1c722018-02-01 17:23:54 +0000575TEST_F(DexoptTest, DexoptPrimaryFailedInvalidFilter) {
576 LOG(INFO) << "DexoptPrimaryFailedInvalidFilter";
Andreas Gampe023b2242018-02-28 16:03:25 -0800577 binder::Status status;
Calin Juravle5bd1c722018-02-01 17:23:54 +0000578 CompilePrimaryDexFail("awesome-filter",
579 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC,
580 app_oat_dir_.c_str(),
581 kTestAppGid,
Andreas Gampe023b2242018-02-28 16:03:25 -0800582 DEX2OAT_FROM_SCRATCH,
583 &status);
584 EXPECT_STREQ(status.toString8().c_str(),
585 "Status(-8): \'256: Dex2oat invocation for "
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800586 "/data/app/com.installd.test.dexopt/base.jar failed: unspecified dex2oat error'");
Calin Juravle5bd1c722018-02-01 17:23:54 +0000587}
588
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000589TEST_F(DexoptTest, DexoptPrimaryProfileNonPublic) {
590 LOG(INFO) << "DexoptPrimaryProfileNonPublic";
591 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700592 DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000593 app_oat_dir_.c_str(),
594 kTestAppGid,
595 DEX2OAT_FROM_SCRATCH);
596}
597
598TEST_F(DexoptTest, DexoptPrimaryProfilePublic) {
599 LOG(INFO) << "DexoptPrimaryProfilePublic";
Calin Juravle5bd1c722018-02-01 17:23:54 +0000600 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700601 DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_PUBLIC |
602 DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000603 app_oat_dir_.c_str(),
604 kTestAppGid,
605 DEX2OAT_FROM_SCRATCH);
606}
607
608TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) {
609 LOG(INFO) << "DexoptPrimaryBackgroundOk";
610 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700611 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED |
612 DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000613 app_oat_dir_.c_str(),
614 kTestAppGid,
615 DEX2OAT_FROM_SCRATCH);
616}
617
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000618class PrimaryDexReCompilationTest : public DexoptTest {
619 public:
620 virtual void SetUp() {
621 DexoptTest::SetUp();
622 CompilePrimaryDexOk("verify",
623 DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC,
624 app_oat_dir_.c_str(),
625 kTestAppGid,
626 DEX2OAT_FROM_SCRATCH);
627 std::string odex = GetSecondaryDexArtifact(apk_path_, "odex");
628 std::string vdex = GetSecondaryDexArtifact(apk_path_, "vdex");
629
630 first_compilation_odex_fd_.reset(open(odex.c_str(), O_RDONLY));
631 first_compilation_vdex_fd_.reset(open(vdex.c_str(), O_RDONLY));
632 }
633
634 virtual void TearDown() {
635 first_compilation_odex_fd_.reset(-1);
636 first_compilation_vdex_fd_.reset(-1);
637 DexoptTest::TearDown();
638 }
639
640 protected:
641 unique_fd first_compilation_odex_fd_;
642 unique_fd first_compilation_vdex_fd_;
643};
644
645TEST_F(PrimaryDexReCompilationTest, DexoptPrimaryUpdateInPlaceVdex) {
646 LOG(INFO) << "DexoptPrimaryUpdateInPlaceVdex";
647
648 CompilePrimaryDexOk("verify",
649 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC,
650 app_oat_dir_.c_str(),
651 kTestAppGid,
652 DEX2OAT_FOR_BOOT_IMAGE);
653}
Calin Juravle7d765462017-09-04 15:57:10 -0700654
655class ReconcileTest : public DexoptTest {
656 virtual void SetUp() {
657 DexoptTest::SetUp();
Calin Juravle29591732017-11-20 17:46:19 -0800658 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700659 /*binder_ok*/ true, /*compile_ok*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800660 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700661 /*binder_ok*/ true, /*compile_ok*/ true);
662 }
663};
664
665TEST_F(ReconcileTest, ReconcileSecondaryCeExists) {
666 LOG(INFO) << "ReconcileSecondaryCeExists";
667 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
668 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
669}
670
671TEST_F(ReconcileTest, ReconcileSecondaryCeLinkExists) {
672 LOG(INFO) << "ReconcileSecondaryCeLinkExists";
673 reconcile_secondary_dex(secondary_dex_ce_link_, FLAG_STORAGE_CE,
674 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
675}
676
677TEST_F(ReconcileTest, ReconcileSecondaryDeExists) {
678 LOG(INFO) << "ReconcileSecondaryDeExists";
679 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
680 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
681}
682
683TEST_F(ReconcileTest, ReconcileSecondaryDeDoesNotExist) {
684 LOG(INFO) << "ReconcileSecondaryDeDoesNotExist";
685 run_cmd("rm -rf " + secondary_dex_de_);
686 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
687 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ true);
688}
689
690TEST_F(ReconcileTest, ReconcileSecondaryStorageValidationError) {
691 // Validation errors will not clean the odex/vdex/art files but will mark
692 // the file as non existent so that the PM knows it should purge it from its
693 // records.
694 LOG(INFO) << "ReconcileSecondaryStorageValidationError";
695 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_DE,
696 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false);
697}
698
699TEST_F(ReconcileTest, ReconcileSecondaryAppOwnershipValidationError) {
700 LOG(INFO) << "ReconcileSecondaryAppOwnershipValidationError";
701 // Attempt to reconcile the dex files of the test app from a different app.
702 std::string another_app = "another.app";
703 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
704 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid, &another_app);
705}
706
707TEST_F(ReconcileTest, ReconcileSecondaryAcessViaDifferentUidError) {
708 LOG(INFO) << "ReconcileSecondaryAcessViaDifferentUidError";
709 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
710 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid);
711}
712
Calin Juravle29591732017-11-20 17:46:19 -0800713class ProfileTest : public DexoptTest {
714 protected:
715 std::string cur_profile_;
716 std::string ref_profile_;
717 std::string snap_profile_;
718
Calin Juravle824a64d2018-01-18 20:23:17 -0800719 static constexpr const char* kPrimaryProfile = "primary.prof";
720
Calin Juravle29591732017-11-20 17:46:19 -0800721 virtual void SetUp() {
722 DexoptTest::SetUp();
723 cur_profile_ = create_current_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -0800724 kTestUserId, package_name_, kPrimaryProfile, /*is_secondary_dex*/ false);
725 ref_profile_ = create_reference_profile_path(package_name_, kPrimaryProfile,
726 /*is_secondary_dex*/ false);
727 snap_profile_ = create_snapshot_profile_path(package_name_, kPrimaryProfile);
Calin Juravle29591732017-11-20 17:46:19 -0800728 }
729
Calin Juravle824a64d2018-01-18 20:23:17 -0800730 void SetupProfile(const std::string& path, uid_t uid, gid_t gid, mode_t mode,
731 int32_t num_dex) {
732 run_cmd("profman --generate-test-profile-seed=" + std::to_string(num_dex) +
733 " --generate-test-profile-num-dex=" + std::to_string(num_dex) +
734 " --generate-test-profile=" + path);
Calin Juravle29591732017-11-20 17:46:19 -0800735 ::chmod(path.c_str(), mode);
736 ::chown(path.c_str(), uid, gid);
737 }
738
739 void SetupProfiles(bool setup_ref) {
740 SetupProfile(cur_profile_, kTestAppUid, kTestAppGid, 0600, 1);
741 if (setup_ref) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800742 SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0600, 2);
Calin Juravle29591732017-11-20 17:46:19 -0800743 }
744 }
745
Calin Juravlec3596c32017-12-05 12:29:15 -0800746 void createProfileSnapshot(int32_t appid, const std::string& package_name,
747 bool expected_result) {
Calin Juravle29591732017-11-20 17:46:19 -0800748 bool result;
Calin Juravlec3596c32017-12-05 12:29:15 -0800749 binder::Status binder_result = service_->createProfileSnapshot(
Calin Juravle0d0a4922018-01-23 19:54:11 -0800750 appid, package_name, kPrimaryProfile, apk_path_, &result);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800751 ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str();
Calin Juravle29591732017-11-20 17:46:19 -0800752 ASSERT_EQ(expected_result, result);
753
754 if (!expected_result) {
755 // Do not check the files if we expect to fail.
756 return;
757 }
758
759 // Check that the snapshot was created witht he expected acess flags.
760 CheckFileAccess(snap_profile_, kSystemUid, kSystemGid, 0600 | S_IFREG);
761
762 // The snapshot should be equivalent to the merge of profiles.
763 std::string expected_profile_content = snap_profile_ + ".expected";
764 run_cmd("rm -f " + expected_profile_content);
765 run_cmd("touch " + expected_profile_content);
766 run_cmd("profman --profile-file=" + cur_profile_ +
767 " --profile-file=" + ref_profile_ +
Calin Juravle40b5f462018-02-07 04:11:52 -0800768 " --reference-profile-file=" + expected_profile_content +
769 " --apk=" + apk_path_);
Calin Juravle29591732017-11-20 17:46:19 -0800770
771 ASSERT_TRUE(AreFilesEqual(expected_profile_content, snap_profile_));
772
773 pid_t pid = fork();
774 if (pid == 0) {
775 /* child */
776 TransitionToSystemServer();
777
778 // System server should be able to open the the spanshot.
779 unique_fd fd(open(snap_profile_.c_str(), O_RDONLY));
780 ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno);
781 _exit(0);
782 }
783 /* parent */
784 ASSERT_TRUE(WIFEXITED(wait_child(pid)));
785 }
786
Calin Juravle408cd4a2018-01-20 23:34:18 -0800787 void mergePackageProfiles(const std::string& package_name,
788 const std::string& code_path,
789 bool expected_result) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800790 bool result;
791 binder::Status binder_result = service_->mergeProfiles(
Calin Juravle408cd4a2018-01-20 23:34:18 -0800792 kTestAppUid, package_name, code_path, &result);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800793 ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str();
Calin Juravle824a64d2018-01-18 20:23:17 -0800794 ASSERT_EQ(expected_result, result);
795
796 if (!expected_result) {
797 // Do not check the files if we expect to fail.
798 return;
799 }
800
801 // Check that the snapshot was created witht he expected acess flags.
802 CheckFileAccess(ref_profile_, kTestAppUid, kTestAppUid, 0600 | S_IFREG);
803
804 // The snapshot should be equivalent to the merge of profiles.
805 std::string ref_profile_content = ref_profile_ + ".expected";
806 run_cmd("rm -f " + ref_profile_content);
807 run_cmd("touch " + ref_profile_content);
808 run_cmd("profman --profile-file=" + cur_profile_ +
809 " --profile-file=" + ref_profile_ +
810 " --reference-profile-file=" + ref_profile_content);
811
812 ASSERT_TRUE(AreFilesEqual(ref_profile_content, ref_profile_));
813 }
814
Calin Juravlec3b049e2018-01-18 22:32:58 -0800815 // TODO(calin): add dex metadata tests once the ART change is merged.
816 void preparePackageProfile(const std::string& package_name, const std::string& profile_name,
817 bool expected_result) {
818 bool result;
819 binder::Status binder_result = service_->prepareAppProfile(
Jeff Sharkey1ebe77f2018-02-27 11:40:45 -0700820 package_name, kTestUserId, kTestAppId, profile_name, apk_path_,
Calin Juravlec3b049e2018-01-18 22:32:58 -0800821 /*dex_metadata*/ nullptr, &result);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800822 ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str();
Calin Juravlec3b049e2018-01-18 22:32:58 -0800823 ASSERT_EQ(expected_result, result);
824
825 if (!expected_result) {
826 // Do not check the files if we expect to fail.
827 return;
828 }
829
830 std::string code_path_cur_prof = create_current_profile_path(
831 kTestUserId, package_name, profile_name, /*is_secondary_dex*/ false);
832 std::string code_path_ref_profile = create_reference_profile_path(package_name,
833 profile_name, /*is_secondary_dex*/ false);
834
835 // Check that we created the current profile.
836 CheckFileAccess(code_path_cur_prof, kTestAppUid, kTestAppUid, 0600 | S_IFREG);
837
838 // Without dex metadata we don't generate a reference profile.
839 ASSERT_EQ(-1, access(code_path_ref_profile.c_str(), R_OK));
840 }
841
Calin Juravle0d0a4922018-01-23 19:54:11 -0800842 protected:
Calin Juravle29591732017-11-20 17:46:19 -0800843 void TransitionToSystemServer() {
844 ASSERT_TRUE(DropCapabilities(kSystemUid, kSystemGid));
845 int32_t res = selinux_android_setcontext(
846 kSystemUid, true, se_info_.c_str(), "system_server");
847 ASSERT_EQ(0, res) << "Failed to setcon " << strerror(errno);
848 }
849
850 bool AreFilesEqual(const std::string& file1, const std::string& file2) {
851 std::vector<uint8_t> content1;
852 std::vector<uint8_t> content2;
853
854 if (!ReadAll(file1, &content1)) return false;
855 if (!ReadAll(file2, &content2)) return false;
856 return content1 == content2;
857 }
858
859 bool ReadAll(const std::string& file, std::vector<uint8_t>* content) {
860 unique_fd fd(open(file.c_str(), O_RDONLY));
861 if (fd < 0) {
862 PLOG(ERROR) << "Failed to open " << file;
863 return false;
864 }
865 struct stat st;
866 if (fstat(fd, &st) != 0) {
867 PLOG(ERROR) << "Failed to stat " << file;
868 return false;
869 }
870 content->resize(st.st_size);
871 bool result = ReadFully(fd, content->data(), content->size());
872 if (!result) {
873 PLOG(ERROR) << "Failed to read " << file;
874 }
875 return result;
876 }
877};
878
879TEST_F(ProfileTest, ProfileSnapshotOk) {
880 LOG(INFO) << "ProfileSnapshotOk";
881
882 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800883 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800884}
885
886// The reference profile is created on the fly. We need to be able to
887// snapshot without one.
888TEST_F(ProfileTest, ProfileSnapshotOkNoReference) {
889 LOG(INFO) << "ProfileSnapshotOkNoReference";
890
891 SetupProfiles(/*setup_ref*/ false);
Calin Juravlec3596c32017-12-05 12:29:15 -0800892 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800893}
894
895TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) {
896 LOG(INFO) << "ProfileSnapshotFailWrongPackage";
897
898 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800899 createProfileSnapshot(kTestAppId, "not.there", /*expected_result*/ false);
Calin Juravle29591732017-11-20 17:46:19 -0800900}
901
902TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) {
903 LOG(INFO) << "ProfileSnapshotDestroySnapshot";
904
905 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800906 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800907
Calin Juravle824a64d2018-01-18 20:23:17 -0800908 binder::Status binder_result = service_->destroyProfileSnapshot(package_name_, kPrimaryProfile);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800909 ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str();
Calin Juravle29591732017-11-20 17:46:19 -0800910 struct stat st;
911 ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st));
912 ASSERT_EQ(ENOENT, errno);
913}
914
Calin Juravle824a64d2018-01-18 20:23:17 -0800915TEST_F(ProfileTest, ProfileMergeOk) {
916 LOG(INFO) << "ProfileMergeOk";
917
918 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -0800919 mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true);
Calin Juravle824a64d2018-01-18 20:23:17 -0800920}
921
922// The reference profile is created on the fly. We need to be able to
923// merge without one.
924TEST_F(ProfileTest, ProfileMergeOkNoReference) {
925 LOG(INFO) << "ProfileMergeOkNoReference";
926
927 SetupProfiles(/*setup_ref*/ false);
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
931TEST_F(ProfileTest, ProfileMergeFailWrongPackage) {
932 LOG(INFO) << "ProfileMergeFailWrongPackage";
933
934 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -0800935 mergePackageProfiles("not.there", "primary.prof", /*expected_result*/ false);
Calin Juravle824a64d2018-01-18 20:23:17 -0800936}
937
Calin Juravled2affb82017-11-28 17:41:43 -0800938TEST_F(ProfileTest, ProfileDirOk) {
939 LOG(INFO) << "ProfileDirOk";
940
941 std::string cur_profile_dir = create_primary_current_profile_package_dir_path(
942 kTestUserId, package_name_);
943 std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_,
Calin Juravle824a64d2018-01-18 20:23:17 -0800944 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravled2affb82017-11-28 17:41:43 -0800945 std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_);
946
947 CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
Calin Juravle6f06eb62017-11-28 18:44:53 -0800948 CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR);
949}
950
951// Verify that the profile directories are fixed up during an upgrade.
952// (The reference profile directory is prepared lazily).
953TEST_F(ProfileTest, ProfileDirOkAfterFixup) {
954 LOG(INFO) << "ProfileDirOkAfterFixup";
955
956 std::string cur_profile_dir = create_primary_current_profile_package_dir_path(
957 kTestUserId, package_name_);
958 std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_,
Calin Juravle824a64d2018-01-18 20:23:17 -0800959 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravle6f06eb62017-11-28 18:44:53 -0800960 std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_);
961
962 // Simulate a pre-P setup by changing the owner to kTestAppGid and permissions to 0700.
963 ASSERT_EQ(0, chown(ref_profile_dir.c_str(), kTestAppGid, kTestAppGid));
964 ASSERT_EQ(0, chmod(ref_profile_dir.c_str(), 0700));
965
966 // Run createAppData again which will offer to fix-up the profile directories.
967 ASSERT_TRUE(service_->createAppData(
968 volume_uuid_,
969 package_name_,
970 kTestUserId,
971 kAppDataFlags,
972 kTestAppUid,
973 se_info_,
974 kOSdkVersion,
975 &ce_data_inode_).isOk());
976
977 // Check the file access.
978 CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
Calin Juravle6f06eb62017-11-28 18:44:53 -0800979 CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR);
Calin Juravled2affb82017-11-28 17:41:43 -0800980}
981
Calin Juravlec3b049e2018-01-18 22:32:58 -0800982TEST_F(ProfileTest, ProfilePrepareOk) {
983 LOG(INFO) << "ProfilePrepareOk";
984 preparePackageProfile(package_name_, "split.prof", /*expected_result*/ true);
985}
986
987TEST_F(ProfileTest, ProfilePrepareFailInvalidPackage) {
988 LOG(INFO) << "ProfilePrepareFailInvalidPackage";
989 preparePackageProfile("not.there.package", "split.prof", /*expected_result*/ false);
990}
991
992TEST_F(ProfileTest, ProfilePrepareFailProfileChangedUid) {
993 LOG(INFO) << "ProfilePrepareFailProfileChangedUid";
994 SetupProfiles(/*setup_ref*/ false);
995 // Change the uid on the profile to trigger a failure.
996 ::chown(cur_profile_.c_str(), kTestAppUid + 1, kTestAppGid + 1);
997 preparePackageProfile(package_name_, "primary.prof", /*expected_result*/ false);
998}
999
Calin Juravle0d0a4922018-01-23 19:54:11 -08001000
1001class BootProfileTest : public ProfileTest {
1002 public:
1003 virtual void setup() {
1004 ProfileTest::SetUp();
1005 intial_android_profiles_dir = android_profiles_dir;
1006 }
1007
1008 virtual void TearDown() {
1009 android_profiles_dir = intial_android_profiles_dir;
1010 ProfileTest::TearDown();
1011 }
1012
1013 void UpdateAndroidProfilesDir(const std::string& profile_dir) {
1014 android_profiles_dir = profile_dir;
1015 // We need to create the reference profile directory in the new profile dir.
1016 run_cmd("mkdir -p " + profile_dir + "/ref");
1017 }
1018
1019 void createBootImageProfileSnapshot(const std::string& classpath, bool expected_result) {
1020 bool result;
1021 binder::Status binder_result = service_->createProfileSnapshot(
1022 -1, "android", "android.prof", classpath, &result);
1023 ASSERT_TRUE(binder_result.isOk());
1024 ASSERT_EQ(expected_result, result);
1025
1026 if (!expected_result) {
1027 // Do not check the files if we expect to fail.
1028 return;
1029 }
1030
1031 // Check that the snapshot was created with he expected access flags.
1032 const std::string boot_profile = create_snapshot_profile_path("android", "android.prof");
1033 CheckFileAccess(boot_profile, kSystemUid, kSystemGid, 0600 | S_IFREG);
1034
1035 pid_t pid = fork();
1036 if (pid == 0) {
1037 /* child */
1038 TransitionToSystemServer();
1039
1040 // System server should be able to open the snapshot.
1041 unique_fd fd(open(boot_profile.c_str(), O_RDONLY));
1042 ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno);
1043 _exit(0);
1044 }
1045 /* parent */
1046 ASSERT_TRUE(WIFEXITED(wait_child(pid)));
1047 }
1048 protected:
1049 std::string intial_android_profiles_dir;
1050};
1051
1052TEST_F(BootProfileTest, BootProfileSnapshotOk) {
1053 LOG(INFO) << "BootProfileSnapshotOk";
1054 char* boot_classpath = getenv("BOOTCLASSPATH");
1055 ASSERT_TRUE(boot_classpath != nullptr);
1056 createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true);
1057}
1058
1059TEST_F(BootProfileTest, BootProfileSnapshotFailEmptyClasspath) {
1060 LOG(INFO) << "BootProfileSnapshotFailEmptyClasspath";
1061
1062 createBootImageProfileSnapshot(/*boot_classpath*/ "", /*expected_result*/ false);
1063}
1064
1065TEST_F(BootProfileTest, BootProfileSnapshotOkNoProfiles) {
1066 LOG(INFO) << "BootProfileSnapshotOkNoProfiles";
1067 char* boot_classpath = getenv("BOOTCLASSPATH");
1068 ASSERT_TRUE(boot_classpath != nullptr);
1069
1070 // The app_apk_dir has no profiles. So we shouldn't be able to merge anything.
1071 // Still, this is not a failure case.
1072 UpdateAndroidProfilesDir(app_apk_dir_);
1073 createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true);
1074}
1075
1076// Verify that profile collection.
1077TEST_F(BootProfileTest, CollectProfiles) {
1078 LOG(INFO) << "CollectProfiles";
1079
1080 // Create some profile directories mimicking the real profile structure.
1081 run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/ref");
1082 run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/0/");
1083 run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/1/");
1084 // Create an empty profile.
1085 run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/1/primary.prof");
1086 // Create a random file.
1087 run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/0/non.profile.file");
1088
1089 // Create some non-empty profiles.
1090 std::string current_prof = app_private_dir_de_ + "/profiles/cur/0/primary.prof";
1091 run_cmd("echo 1 > " + current_prof);
1092 std::string ref_prof = app_private_dir_de_ + "/profiles/ref/primary.prof";
1093 run_cmd("echo 1 > " + ref_prof);
1094
1095 UpdateAndroidProfilesDir(app_private_dir_de_ + "/profiles");
1096
1097 std::vector<std::string> profiles;
1098 collect_profiles(&profiles);
1099
1100 // Only two profiles should be in the output.
1101 ASSERT_EQ(2u, profiles.size());
1102 ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), current_prof) != profiles.end());
1103 ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), ref_prof) != profiles.end());
1104}
1105
Calin Juravle7d765462017-09-04 15:57:10 -07001106} // namespace installd
1107} // namespace android