blob: 81135422b62f6e3731c4853f30274b00654a51cb [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 Juravle29591732017-11-20 17:46:19 -080017#include <fcntl.h>
Calin Juravle7d765462017-09-04 15:57:10 -070018#include <stdlib.h>
19#include <string.h>
Calin Juravle29591732017-11-20 17:46:19 -080020#include <sys/types.h>
21#include <sys/stat.h>
Calin Juravle7d765462017-09-04 15:57:10 -070022
Calin Juravle29591732017-11-20 17:46:19 -080023#include <android-base/file.h>
Calin Juravle7d765462017-09-04 15:57:10 -070024#include <android-base/logging.h>
25#include <android-base/stringprintf.h>
Calin Juravle29591732017-11-20 17:46:19 -080026#include <android-base/unique_fd.h>
27
Calin Juravle7d765462017-09-04 15:57:10 -070028#include <cutils/properties.h>
Calin Juravle29591732017-11-20 17:46:19 -080029
Calin Juravle7d765462017-09-04 15:57:10 -070030#include <gtest/gtest.h>
31
Calin Juravle29591732017-11-20 17:46:19 -080032#include <selinux/android.h>
33#include <selinux/avc.h>
34
Calin Juravle7d765462017-09-04 15:57:10 -070035#include "dexopt.h"
36#include "InstalldNativeService.h"
37#include "globals.h"
38#include "tests/test_utils.h"
39#include "utils.h"
40
Calin Juravle29591732017-11-20 17:46:19 -080041using android::base::ReadFully;
42using android::base::unique_fd;
43
Calin Juravle7d765462017-09-04 15:57:10 -070044namespace android {
45namespace installd {
46
47// TODO(calin): try to dedup this code.
48#if defined(__arm__)
49static const std::string kRuntimeIsa = "arm";
50#elif defined(__aarch64__)
51static const std::string kRuntimeIsa = "arm64";
52#elif defined(__mips__) && !defined(__LP64__)
53static const std::string kRuntimeIsa = "mips";
54#elif defined(__mips__) && defined(__LP64__)
55static const std::string kRuntimeIsa = "mips64";
56#elif defined(__i386__)
57static const std::string kRuntimeIsa = "x86";
58#elif defined(__x86_64__)
59static const std::string kRuntimeIsa = "x86_64";
60#else
61static const std::string kRuntimeIsa = "none";
62#endif
63
64int get_property(const char *key, char *value, const char *default_value) {
65 return property_get(key, value, default_value);
66}
67
68bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
69 const char *instruction_set) {
70 return calculate_oat_file_path_default(path, oat_dir, apk_path, instruction_set);
71}
72
73bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
74 const char *instruction_set) {
75 return calculate_odex_file_path_default(path, apk_path, instruction_set);
76}
77
78bool create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) {
79 return create_cache_path_default(path, src, instruction_set);
80}
81
82static void run_cmd(const std::string& cmd) {
83 system(cmd.c_str());
84}
85
86static void mkdir(const std::string& path, uid_t owner, gid_t group, mode_t mode) {
87 ::mkdir(path.c_str(), mode);
88 ::chown(path.c_str(), owner, group);
89 ::chmod(path.c_str(), mode);
90}
91
Calin Juravle29591732017-11-20 17:46:19 -080092static int log_callback(int type, const char *fmt, ...) { // NOLINT
93 va_list ap;
94 int priority;
95
96 switch (type) {
97 case SELINUX_WARNING:
98 priority = ANDROID_LOG_WARN;
99 break;
100 case SELINUX_INFO:
101 priority = ANDROID_LOG_INFO;
102 break;
103 default:
104 priority = ANDROID_LOG_ERROR;
105 break;
106 }
107 va_start(ap, fmt);
108 LOG_PRI_VA(priority, "SELinux", fmt, ap);
109 va_end(ap);
110 return 0;
111}
112
113static bool init_selinux() {
114 int selinux_enabled = (is_selinux_enabled() > 0);
115
116 union selinux_callback cb;
117 cb.func_log = log_callback;
118 selinux_set_callback(SELINUX_CB_LOG, cb);
119
120 if (selinux_enabled && selinux_status_open(true) < 0) {
121 LOG(ERROR) << "Could not open selinux status; exiting";
122 return false;
123 }
124
125 return true;
126}
127
Calin Juravle7d765462017-09-04 15:57:10 -0700128// Base64 encoding of a simple dex files with 2 methods.
129static const char kDexFile[] =
130 "UEsDBBQAAAAIAOiOYUs9y6BLCgEAABQCAAALABwAY2xhc3Nlcy5kZXhVVAkAA/Ns+lkOHv1ZdXgL"
131 "AAEEI+UCAASIEwAAS0mt4DIwNmX4qpn7j/2wA7v7N+ZvoQpCJRlVx5SWa4YaiDAxMBQwMDBUhJkI"
132 "MUBBDyMDAzsDRJwFxAdioBDDHAYEYAbiFUAM1M5wAIhFGCGKDIDYAogdgNgDiH2BOAiI0xghekDm"
133 "sQIxGxQzM6ACRijNhCbOhCZfyohdPYyuh8szgtVkMkLsLhAAqeCDi+ejibPZZOZlltgxsDnqZSWW"
134 "JTKwOUFoZh9HayDhZM0g5AMS0M9JzEvX90/KSk0usWZgDAMaws5nAyXBzmpoYGlgAjsAyJoBMp0b"
135 "zQ8gGhbOTEhhzYwU3qxIYc2GFN6MClC/AhUyKUDMAYU9M1Qc5F8GKBscVgIQM0FxCwBQSwECHgMU"
136 "AAAACADojmFLPcugSwoBAAAUAgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAA/Ns"
137 "+ll1eAsAAQQj5QIABIgTAABQSwUGAAAAAAEAAQBRAAAATwEAAAAA";
138
139
140class DexoptTest : public testing::Test {
141protected:
142 static constexpr bool kDebug = false;
143 static constexpr uid_t kSystemUid = 1000;
144 static constexpr uid_t kSystemGid = 1000;
145 static constexpr int32_t kOSdkVersion = 25;
146 static constexpr int32_t kAppDataFlags = FLAG_STORAGE_CE | FLAG_STORAGE_DE;
Calin Juravle7d765462017-09-04 15:57:10 -0700147 static constexpr int32_t kTestUserId = 0;
Calin Juravled2affb82017-11-28 17:41:43 -0800148 static constexpr uid_t kTestAppId = 19999;
149
150 const gid_t kTestAppUid = multiuser_get_uid(kTestUserId, kTestAppId);
151 const uid_t kTestAppGid = multiuser_get_shared_gid(kTestUserId, kTestAppId);
Calin Juravle7d765462017-09-04 15:57:10 -0700152
153 InstalldNativeService* service_;
154 std::unique_ptr<std::string> volume_uuid_;
155 std::string package_name_;
156 std::string app_apk_dir_;
157 std::string app_private_dir_ce_;
158 std::string app_private_dir_de_;
159 std::string se_info_;
160
161 int64_t ce_data_inode_;
162
163 std::string secondary_dex_ce_;
164 std::string secondary_dex_ce_link_;
165 std::string secondary_dex_de_;
166
167 virtual void SetUp() {
168 setenv("ANDROID_LOG_TAGS", "*:v", 1);
169 android::base::InitLogging(nullptr);
Calin Juravle29591732017-11-20 17:46:19 -0800170 // Initialize the globals holding the file system main paths (/data/, /system/ etc..).
171 // This is needed in order to compute the application and profile paths.
172 ASSERT_TRUE(init_globals_from_data_and_root());
173 // Initialize selinux log callbacks.
174 // This ensures that selinux is up and running and re-directs the selinux messages
175 // to logcat (in order to make it easier to investigate test results).
176 ASSERT_TRUE(init_selinux());
Calin Juravle7d765462017-09-04 15:57:10 -0700177 service_ = new InstalldNativeService();
178
179 volume_uuid_ = nullptr;
180 package_name_ = "com.installd.test.dexopt";
181 se_info_ = "default";
Calin Juravle7d765462017-09-04 15:57:10 -0700182 app_apk_dir_ = android_app_dir + package_name_;
183
184 create_mock_app();
185 }
186
187 virtual void TearDown() {
188 if (!kDebug) {
189 service_->destroyAppData(
190 volume_uuid_, package_name_, kTestUserId, kAppDataFlags, ce_data_inode_);
191 run_cmd("rm -rf " + app_apk_dir_);
192 run_cmd("rm -rf " + app_private_dir_ce_);
193 run_cmd("rm -rf " + app_private_dir_de_);
194 }
195 delete service_;
196 }
197
198 void create_mock_app() {
199 // Create the oat dir.
200 std::string app_oat_dir = app_apk_dir_ + "/oat";
201 mkdir(app_apk_dir_, kSystemUid, kSystemGid, 0755);
202 service_->createOatDir(app_oat_dir, kRuntimeIsa);
203
204 // Copy the primary apk.
205 std::string apk_path = app_apk_dir_ + "/base.jar";
206 ASSERT_TRUE(WriteBase64ToFile(kDexFile, apk_path, kSystemUid, kSystemGid, 0644));
207
208 // Create the app user data.
209 ASSERT_TRUE(service_->createAppData(
210 volume_uuid_,
211 package_name_,
212 kTestUserId,
213 kAppDataFlags,
214 kTestAppUid,
215 se_info_,
216 kOSdkVersion,
217 &ce_data_inode_).isOk());
218
219 // Create a secondary dex file on CE storage
220 const char* volume_uuid_cstr = volume_uuid_ == nullptr ? nullptr : volume_uuid_->c_str();
221 app_private_dir_ce_ = create_data_user_ce_package_path(
222 volume_uuid_cstr, kTestUserId, package_name_.c_str());
223 secondary_dex_ce_ = app_private_dir_ce_ + "/secondary_ce.jar";
224 ASSERT_TRUE(WriteBase64ToFile(kDexFile, secondary_dex_ce_, kTestAppUid, kTestAppGid, 0600));
225 std::string app_private_dir_ce_link = create_data_user_ce_package_path_as_user_link(
226 volume_uuid_cstr, kTestUserId, package_name_.c_str());
227 secondary_dex_ce_link_ = app_private_dir_ce_link + "/secondary_ce.jar";
228
229 // Create a secondary dex file on DE storage.
230 app_private_dir_de_ = create_data_user_de_package_path(
231 volume_uuid_cstr, kTestUserId, package_name_.c_str());
232 secondary_dex_de_ = app_private_dir_de_ + "/secondary_de.jar";
233 ASSERT_TRUE(WriteBase64ToFile(kDexFile, secondary_dex_de_, kTestAppUid, kTestAppGid, 0600));
234
235 // Fix app data uid.
236 ASSERT_TRUE(service_->fixupAppData(volume_uuid_, kTestUserId).isOk());
237 }
238
239
Calin Juravle29591732017-11-20 17:46:19 -0800240 std::string GetSecondaryDexArtifact(const std::string& path, const std::string& type) {
Calin Juravle7d765462017-09-04 15:57:10 -0700241 std::string::size_type end = path.rfind('.');
242 std::string::size_type start = path.rfind('/', end);
243 return path.substr(0, start) + "/oat/" + kRuntimeIsa + "/" +
244 path.substr(start + 1, end - start) + type;
245 }
246
Calin Juravle29591732017-11-20 17:46:19 -0800247 void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag,
Calin Juravle7d765462017-09-04 15:57:10 -0700248 bool should_binder_call_succeed, bool should_dex_be_compiled = true,
Calin Juravled2affb82017-11-28 17:41:43 -0800249 int32_t uid = -1) {
250 if (uid == -1) {
251 uid = kTestAppUid;
252 }
Calin Juravle7d765462017-09-04 15:57:10 -0700253 std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_));
254 int32_t dexopt_needed = 0; // does not matter;
255 std::unique_ptr<std::string> out_path = nullptr; // does not matter
256 int32_t dex_flags = DEXOPT_SECONDARY_DEX | dex_storage_flag;
257 std::string compiler_filter = "speed-profile";
258 std::unique_ptr<std::string> class_loader_context_ptr(new std::string("&"));
259 std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_));
260 bool downgrade = false;
David Brazdil570d3982018-01-16 20:15:43 +0000261 int32_t target_sdk_version = 0; // default
Calin Juravle408cd4a2018-01-20 23:34:18 -0800262 std::unique_ptr<std::string> profile_name_ptr(new std::string("primary.prof"));
Calin Juravle7d765462017-09-04 15:57:10 -0700263
Calin Juravle408cd4a2018-01-20 23:34:18 -0800264 bool prof_result;
265 binder::Status prof_binder_result = service_->prepareAppProfile(
266 package_name_, kTestUserId, kTestAppId, *profile_name_ptr, /*code path*/ "base.apk",
267 /*dex_metadata*/ nullptr, &prof_result);
268 ASSERT_TRUE(prof_binder_result.isOk());
269 ASSERT_TRUE(prof_result);
Calin Juravle7d765462017-09-04 15:57:10 -0700270 binder::Status result = service_->dexopt(path,
271 uid,
272 package_name_ptr,
273 kRuntimeIsa,
274 dexopt_needed,
275 out_path,
276 dex_flags,
277 compiler_filter,
278 volume_uuid_,
279 class_loader_context_ptr,
280 se_info_ptr,
David Brazdil570d3982018-01-16 20:15:43 +0000281 downgrade,
Calin Juravle408cd4a2018-01-20 23:34:18 -0800282 target_sdk_version,
283 profile_name_ptr);
Calin Juravle7d765462017-09-04 15:57:10 -0700284 ASSERT_EQ(should_binder_call_succeed, result.isOk());
285 int expected_access = should_dex_be_compiled ? 0 : -1;
Calin Juravle29591732017-11-20 17:46:19 -0800286 std::string odex = GetSecondaryDexArtifact(path, "odex");
287 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
288 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700289 ASSERT_EQ(expected_access, access(odex.c_str(), R_OK));
290 ASSERT_EQ(expected_access, access(vdex.c_str(), R_OK));
291 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
292 }
293
294 void reconcile_secondary_dex(const std::string& path, int32_t storage_flag,
295 bool should_binder_call_succeed, bool should_dex_exist, bool should_dex_be_deleted,
Calin Juravled2affb82017-11-28 17:41:43 -0800296 int32_t uid = -1, std::string* package_override = nullptr) {
297 if (uid == -1) {
298 uid = kTestAppUid;
299 }
Calin Juravle7d765462017-09-04 15:57:10 -0700300 std::vector<std::string> isas;
301 isas.push_back(kRuntimeIsa);
302 bool out_secondary_dex_exists = false;
303 binder::Status result = service_->reconcileSecondaryDexFile(
304 path,
305 package_override == nullptr ? package_name_ : *package_override,
306 uid,
307 isas,
308 volume_uuid_,
309 storage_flag,
310 &out_secondary_dex_exists);
311
312 ASSERT_EQ(should_binder_call_succeed, result.isOk());
313 ASSERT_EQ(should_dex_exist, out_secondary_dex_exists);
314
315 int expected_access = should_dex_be_deleted ? -1 : 0;
Calin Juravle29591732017-11-20 17:46:19 -0800316 std::string odex = GetSecondaryDexArtifact(path, "odex");
317 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
318 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700319 ASSERT_EQ(expected_access, access(odex.c_str(), F_OK));
320 ASSERT_EQ(expected_access, access(vdex.c_str(), F_OK));
321 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
322 }
Calin Juravle29591732017-11-20 17:46:19 -0800323
324 void CheckFileAccess(const std::string& file, uid_t uid, gid_t gid, mode_t mode) {
325 struct stat st;
326 ASSERT_EQ(0, stat(file.c_str(), &st));
327 ASSERT_EQ(uid, st.st_uid);
328 ASSERT_EQ(gid, st.st_gid);
329 ASSERT_EQ(mode, st.st_mode);
330 }
Calin Juravle7d765462017-09-04 15:57:10 -0700331};
332
333
334TEST_F(DexoptTest, DexoptSecondaryCe) {
335 LOG(INFO) << "DexoptSecondaryCe";
Calin Juravle29591732017-11-20 17:46:19 -0800336 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700337 /*binder_ok*/ true, /*compile_ok*/ true);
338}
339
340TEST_F(DexoptTest, DexoptSecondaryCeLink) {
341 LOG(INFO) << "DexoptSecondaryCeLink";
Calin Juravle29591732017-11-20 17:46:19 -0800342 CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700343 /*binder_ok*/ true, /*compile_ok*/ true);
344}
345
346TEST_F(DexoptTest, DexoptSecondaryDe) {
347 LOG(INFO) << "DexoptSecondaryDe";
Calin Juravle29591732017-11-20 17:46:19 -0800348 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700349 /*binder_ok*/ true, /*compile_ok*/ true);
350}
351
352TEST_F(DexoptTest, DexoptSecondaryDoesNotExist) {
353 LOG(INFO) << "DexoptSecondaryDoesNotExist";
354 // If the file validates but does not exist we do not treat it as an error.
Calin Juravle29591732017-11-20 17:46:19 -0800355 CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700356 /*binder_ok*/ true, /*compile_ok*/ false);
357}
358
359TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) {
360 LOG(INFO) << "DexoptSecondaryStorageValidationError";
Calin Juravle29591732017-11-20 17:46:19 -0800361 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700362 /*binder_ok*/ false, /*compile_ok*/ false);
363}
364
365TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) {
366 LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError";
Calin Juravle29591732017-11-20 17:46:19 -0800367 CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700368 /*binder_ok*/ false, /*compile_ok*/ false);
369}
370
371TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) {
372 LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError";
Calin Juravle29591732017-11-20 17:46:19 -0800373 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700374 /*binder_ok*/ false, /*compile_ok*/ false, kSystemUid);
375}
376
377
378class ReconcileTest : public DexoptTest {
379 virtual void SetUp() {
380 DexoptTest::SetUp();
Calin Juravle29591732017-11-20 17:46:19 -0800381 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700382 /*binder_ok*/ true, /*compile_ok*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800383 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700384 /*binder_ok*/ true, /*compile_ok*/ true);
385 }
386};
387
388TEST_F(ReconcileTest, ReconcileSecondaryCeExists) {
389 LOG(INFO) << "ReconcileSecondaryCeExists";
390 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
391 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
392}
393
394TEST_F(ReconcileTest, ReconcileSecondaryCeLinkExists) {
395 LOG(INFO) << "ReconcileSecondaryCeLinkExists";
396 reconcile_secondary_dex(secondary_dex_ce_link_, FLAG_STORAGE_CE,
397 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
398}
399
400TEST_F(ReconcileTest, ReconcileSecondaryDeExists) {
401 LOG(INFO) << "ReconcileSecondaryDeExists";
402 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
403 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
404}
405
406TEST_F(ReconcileTest, ReconcileSecondaryDeDoesNotExist) {
407 LOG(INFO) << "ReconcileSecondaryDeDoesNotExist";
408 run_cmd("rm -rf " + secondary_dex_de_);
409 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
410 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ true);
411}
412
413TEST_F(ReconcileTest, ReconcileSecondaryStorageValidationError) {
414 // Validation errors will not clean the odex/vdex/art files but will mark
415 // the file as non existent so that the PM knows it should purge it from its
416 // records.
417 LOG(INFO) << "ReconcileSecondaryStorageValidationError";
418 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_DE,
419 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false);
420}
421
422TEST_F(ReconcileTest, ReconcileSecondaryAppOwnershipValidationError) {
423 LOG(INFO) << "ReconcileSecondaryAppOwnershipValidationError";
424 // Attempt to reconcile the dex files of the test app from a different app.
425 std::string another_app = "another.app";
426 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
427 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid, &another_app);
428}
429
430TEST_F(ReconcileTest, ReconcileSecondaryAcessViaDifferentUidError) {
431 LOG(INFO) << "ReconcileSecondaryAcessViaDifferentUidError";
432 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
433 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid);
434}
435
Calin Juravle29591732017-11-20 17:46:19 -0800436class ProfileTest : public DexoptTest {
437 protected:
438 std::string cur_profile_;
439 std::string ref_profile_;
440 std::string snap_profile_;
441
Calin Juravle824a64d2018-01-18 20:23:17 -0800442 static constexpr const char* kPrimaryProfile = "primary.prof";
443
Calin Juravle29591732017-11-20 17:46:19 -0800444 virtual void SetUp() {
445 DexoptTest::SetUp();
446 cur_profile_ = create_current_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -0800447 kTestUserId, package_name_, kPrimaryProfile, /*is_secondary_dex*/ false);
448 ref_profile_ = create_reference_profile_path(package_name_, kPrimaryProfile,
449 /*is_secondary_dex*/ false);
450 snap_profile_ = create_snapshot_profile_path(package_name_, kPrimaryProfile);
Calin Juravle29591732017-11-20 17:46:19 -0800451 }
452
Calin Juravle824a64d2018-01-18 20:23:17 -0800453 void SetupProfile(const std::string& path, uid_t uid, gid_t gid, mode_t mode,
454 int32_t num_dex) {
455 run_cmd("profman --generate-test-profile-seed=" + std::to_string(num_dex) +
456 " --generate-test-profile-num-dex=" + std::to_string(num_dex) +
457 " --generate-test-profile=" + path);
Calin Juravle29591732017-11-20 17:46:19 -0800458 ::chmod(path.c_str(), mode);
459 ::chown(path.c_str(), uid, gid);
460 }
461
462 void SetupProfiles(bool setup_ref) {
463 SetupProfile(cur_profile_, kTestAppUid, kTestAppGid, 0600, 1);
464 if (setup_ref) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800465 SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0600, 2);
Calin Juravle29591732017-11-20 17:46:19 -0800466 }
467 }
468
Calin Juravlec3596c32017-12-05 12:29:15 -0800469 void createProfileSnapshot(int32_t appid, const std::string& package_name,
470 bool expected_result) {
Calin Juravle29591732017-11-20 17:46:19 -0800471 bool result;
Calin Juravlec3596c32017-12-05 12:29:15 -0800472 binder::Status binder_result = service_->createProfileSnapshot(
Calin Juravle824a64d2018-01-18 20:23:17 -0800473 appid, package_name, kPrimaryProfile, &result);
Calin Juravle29591732017-11-20 17:46:19 -0800474 ASSERT_TRUE(binder_result.isOk());
475 ASSERT_EQ(expected_result, result);
476
477 if (!expected_result) {
478 // Do not check the files if we expect to fail.
479 return;
480 }
481
482 // Check that the snapshot was created witht he expected acess flags.
483 CheckFileAccess(snap_profile_, kSystemUid, kSystemGid, 0600 | S_IFREG);
484
485 // The snapshot should be equivalent to the merge of profiles.
486 std::string expected_profile_content = snap_profile_ + ".expected";
487 run_cmd("rm -f " + expected_profile_content);
488 run_cmd("touch " + expected_profile_content);
489 run_cmd("profman --profile-file=" + cur_profile_ +
490 " --profile-file=" + ref_profile_ +
491 " --reference-profile-file=" + expected_profile_content);
492
493 ASSERT_TRUE(AreFilesEqual(expected_profile_content, snap_profile_));
494
495 pid_t pid = fork();
496 if (pid == 0) {
497 /* child */
498 TransitionToSystemServer();
499
500 // System server should be able to open the the spanshot.
501 unique_fd fd(open(snap_profile_.c_str(), O_RDONLY));
502 ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno);
503 _exit(0);
504 }
505 /* parent */
506 ASSERT_TRUE(WIFEXITED(wait_child(pid)));
507 }
508
Calin Juravle408cd4a2018-01-20 23:34:18 -0800509 void mergePackageProfiles(const std::string& package_name,
510 const std::string& code_path,
511 bool expected_result) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800512 bool result;
513 binder::Status binder_result = service_->mergeProfiles(
Calin Juravle408cd4a2018-01-20 23:34:18 -0800514 kTestAppUid, package_name, code_path, &result);
Calin Juravle824a64d2018-01-18 20:23:17 -0800515 ASSERT_TRUE(binder_result.isOk());
516 ASSERT_EQ(expected_result, result);
517
518 if (!expected_result) {
519 // Do not check the files if we expect to fail.
520 return;
521 }
522
523 // Check that the snapshot was created witht he expected acess flags.
524 CheckFileAccess(ref_profile_, kTestAppUid, kTestAppUid, 0600 | S_IFREG);
525
526 // The snapshot should be equivalent to the merge of profiles.
527 std::string ref_profile_content = ref_profile_ + ".expected";
528 run_cmd("rm -f " + ref_profile_content);
529 run_cmd("touch " + ref_profile_content);
530 run_cmd("profman --profile-file=" + cur_profile_ +
531 " --profile-file=" + ref_profile_ +
532 " --reference-profile-file=" + ref_profile_content);
533
534 ASSERT_TRUE(AreFilesEqual(ref_profile_content, ref_profile_));
535 }
536
Calin Juravlec3b049e2018-01-18 22:32:58 -0800537 // TODO(calin): add dex metadata tests once the ART change is merged.
538 void preparePackageProfile(const std::string& package_name, const std::string& profile_name,
539 bool expected_result) {
540 bool result;
541 binder::Status binder_result = service_->prepareAppProfile(
542 package_name, kTestUserId, kTestAppId, profile_name, /*code path*/ "base.apk",
543 /*dex_metadata*/ nullptr, &result);
544 ASSERT_TRUE(binder_result.isOk());
545 ASSERT_EQ(expected_result, result);
546
547 if (!expected_result) {
548 // Do not check the files if we expect to fail.
549 return;
550 }
551
552 std::string code_path_cur_prof = create_current_profile_path(
553 kTestUserId, package_name, profile_name, /*is_secondary_dex*/ false);
554 std::string code_path_ref_profile = create_reference_profile_path(package_name,
555 profile_name, /*is_secondary_dex*/ false);
556
557 // Check that we created the current profile.
558 CheckFileAccess(code_path_cur_prof, kTestAppUid, kTestAppUid, 0600 | S_IFREG);
559
560 // Without dex metadata we don't generate a reference profile.
561 ASSERT_EQ(-1, access(code_path_ref_profile.c_str(), R_OK));
562 }
563
Calin Juravle29591732017-11-20 17:46:19 -0800564 private:
565 void TransitionToSystemServer() {
566 ASSERT_TRUE(DropCapabilities(kSystemUid, kSystemGid));
567 int32_t res = selinux_android_setcontext(
568 kSystemUid, true, se_info_.c_str(), "system_server");
569 ASSERT_EQ(0, res) << "Failed to setcon " << strerror(errno);
570 }
571
572 bool AreFilesEqual(const std::string& file1, const std::string& file2) {
573 std::vector<uint8_t> content1;
574 std::vector<uint8_t> content2;
575
576 if (!ReadAll(file1, &content1)) return false;
577 if (!ReadAll(file2, &content2)) return false;
578 return content1 == content2;
579 }
580
581 bool ReadAll(const std::string& file, std::vector<uint8_t>* content) {
582 unique_fd fd(open(file.c_str(), O_RDONLY));
583 if (fd < 0) {
584 PLOG(ERROR) << "Failed to open " << file;
585 return false;
586 }
587 struct stat st;
588 if (fstat(fd, &st) != 0) {
589 PLOG(ERROR) << "Failed to stat " << file;
590 return false;
591 }
592 content->resize(st.st_size);
593 bool result = ReadFully(fd, content->data(), content->size());
594 if (!result) {
595 PLOG(ERROR) << "Failed to read " << file;
596 }
597 return result;
598 }
599};
600
601TEST_F(ProfileTest, ProfileSnapshotOk) {
602 LOG(INFO) << "ProfileSnapshotOk";
603
604 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800605 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800606}
607
608// The reference profile is created on the fly. We need to be able to
609// snapshot without one.
610TEST_F(ProfileTest, ProfileSnapshotOkNoReference) {
611 LOG(INFO) << "ProfileSnapshotOkNoReference";
612
613 SetupProfiles(/*setup_ref*/ false);
Calin Juravlec3596c32017-12-05 12:29:15 -0800614 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800615}
616
617TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) {
618 LOG(INFO) << "ProfileSnapshotFailWrongPackage";
619
620 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800621 createProfileSnapshot(kTestAppId, "not.there", /*expected_result*/ false);
Calin Juravle29591732017-11-20 17:46:19 -0800622}
623
624TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) {
625 LOG(INFO) << "ProfileSnapshotDestroySnapshot";
626
627 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800628 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800629
Calin Juravle824a64d2018-01-18 20:23:17 -0800630 binder::Status binder_result = service_->destroyProfileSnapshot(package_name_, kPrimaryProfile);
Calin Juravle29591732017-11-20 17:46:19 -0800631 ASSERT_TRUE(binder_result.isOk());
632 struct stat st;
633 ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st));
634 ASSERT_EQ(ENOENT, errno);
635}
636
Calin Juravle824a64d2018-01-18 20:23:17 -0800637TEST_F(ProfileTest, ProfileMergeOk) {
638 LOG(INFO) << "ProfileMergeOk";
639
640 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -0800641 mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true);
Calin Juravle824a64d2018-01-18 20:23:17 -0800642}
643
644// The reference profile is created on the fly. We need to be able to
645// merge without one.
646TEST_F(ProfileTest, ProfileMergeOkNoReference) {
647 LOG(INFO) << "ProfileMergeOkNoReference";
648
649 SetupProfiles(/*setup_ref*/ false);
Calin Juravle408cd4a2018-01-20 23:34:18 -0800650 mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true);
Calin Juravle824a64d2018-01-18 20:23:17 -0800651}
652
653TEST_F(ProfileTest, ProfileMergeFailWrongPackage) {
654 LOG(INFO) << "ProfileMergeFailWrongPackage";
655
656 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -0800657 mergePackageProfiles("not.there", "primary.prof", /*expected_result*/ false);
Calin Juravle824a64d2018-01-18 20:23:17 -0800658}
659
Calin Juravled2affb82017-11-28 17:41:43 -0800660TEST_F(ProfileTest, ProfileDirOk) {
661 LOG(INFO) << "ProfileDirOk";
662
663 std::string cur_profile_dir = create_primary_current_profile_package_dir_path(
664 kTestUserId, package_name_);
665 std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_,
Calin Juravle824a64d2018-01-18 20:23:17 -0800666 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravled2affb82017-11-28 17:41:43 -0800667 std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_);
668
669 CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
Calin Juravle6f06eb62017-11-28 18:44:53 -0800670 CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR);
671}
672
673// Verify that the profile directories are fixed up during an upgrade.
674// (The reference profile directory is prepared lazily).
675TEST_F(ProfileTest, ProfileDirOkAfterFixup) {
676 LOG(INFO) << "ProfileDirOkAfterFixup";
677
678 std::string cur_profile_dir = create_primary_current_profile_package_dir_path(
679 kTestUserId, package_name_);
680 std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_,
Calin Juravle824a64d2018-01-18 20:23:17 -0800681 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravle6f06eb62017-11-28 18:44:53 -0800682 std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_);
683
684 // Simulate a pre-P setup by changing the owner to kTestAppGid and permissions to 0700.
685 ASSERT_EQ(0, chown(ref_profile_dir.c_str(), kTestAppGid, kTestAppGid));
686 ASSERT_EQ(0, chmod(ref_profile_dir.c_str(), 0700));
687
688 // Run createAppData again which will offer to fix-up the profile directories.
689 ASSERT_TRUE(service_->createAppData(
690 volume_uuid_,
691 package_name_,
692 kTestUserId,
693 kAppDataFlags,
694 kTestAppUid,
695 se_info_,
696 kOSdkVersion,
697 &ce_data_inode_).isOk());
698
699 // Check the file access.
700 CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
Calin Juravle6f06eb62017-11-28 18:44:53 -0800701 CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR);
Calin Juravled2affb82017-11-28 17:41:43 -0800702}
703
Calin Juravlec3b049e2018-01-18 22:32:58 -0800704TEST_F(ProfileTest, ProfilePrepareOk) {
705 LOG(INFO) << "ProfilePrepareOk";
706 preparePackageProfile(package_name_, "split.prof", /*expected_result*/ true);
707}
708
709TEST_F(ProfileTest, ProfilePrepareFailInvalidPackage) {
710 LOG(INFO) << "ProfilePrepareFailInvalidPackage";
711 preparePackageProfile("not.there.package", "split.prof", /*expected_result*/ false);
712}
713
714TEST_F(ProfileTest, ProfilePrepareFailProfileChangedUid) {
715 LOG(INFO) << "ProfilePrepareFailProfileChangedUid";
716 SetupProfiles(/*setup_ref*/ false);
717 // Change the uid on the profile to trigger a failure.
718 ::chown(cur_profile_.c_str(), kTestAppUid + 1, kTestAppGid + 1);
719 preparePackageProfile(package_name_, "primary.prof", /*expected_result*/ false);
720}
721
Calin Juravle7d765462017-09-04 15:57:10 -0700722} // namespace installd
723} // namespace android