blob: ebeae9624c30e8a3c103c5e4b5033af641abeb7e [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;
261
262 binder::Status result = service_->dexopt(path,
263 uid,
264 package_name_ptr,
265 kRuntimeIsa,
266 dexopt_needed,
267 out_path,
268 dex_flags,
269 compiler_filter,
270 volume_uuid_,
271 class_loader_context_ptr,
272 se_info_ptr,
273 downgrade);
274 ASSERT_EQ(should_binder_call_succeed, result.isOk());
275 int expected_access = should_dex_be_compiled ? 0 : -1;
Calin Juravle29591732017-11-20 17:46:19 -0800276 std::string odex = GetSecondaryDexArtifact(path, "odex");
277 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
278 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700279 ASSERT_EQ(expected_access, access(odex.c_str(), R_OK));
280 ASSERT_EQ(expected_access, access(vdex.c_str(), R_OK));
281 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
282 }
283
284 void reconcile_secondary_dex(const std::string& path, int32_t storage_flag,
285 bool should_binder_call_succeed, bool should_dex_exist, bool should_dex_be_deleted,
Calin Juravled2affb82017-11-28 17:41:43 -0800286 int32_t uid = -1, std::string* package_override = nullptr) {
287 if (uid == -1) {
288 uid = kTestAppUid;
289 }
Calin Juravle7d765462017-09-04 15:57:10 -0700290 std::vector<std::string> isas;
291 isas.push_back(kRuntimeIsa);
292 bool out_secondary_dex_exists = false;
293 binder::Status result = service_->reconcileSecondaryDexFile(
294 path,
295 package_override == nullptr ? package_name_ : *package_override,
296 uid,
297 isas,
298 volume_uuid_,
299 storage_flag,
300 &out_secondary_dex_exists);
301
302 ASSERT_EQ(should_binder_call_succeed, result.isOk());
303 ASSERT_EQ(should_dex_exist, out_secondary_dex_exists);
304
305 int expected_access = should_dex_be_deleted ? -1 : 0;
Calin Juravle29591732017-11-20 17:46:19 -0800306 std::string odex = GetSecondaryDexArtifact(path, "odex");
307 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
308 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700309 ASSERT_EQ(expected_access, access(odex.c_str(), F_OK));
310 ASSERT_EQ(expected_access, access(vdex.c_str(), F_OK));
311 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
312 }
Calin Juravle29591732017-11-20 17:46:19 -0800313
314 void CheckFileAccess(const std::string& file, uid_t uid, gid_t gid, mode_t mode) {
315 struct stat st;
316 ASSERT_EQ(0, stat(file.c_str(), &st));
317 ASSERT_EQ(uid, st.st_uid);
318 ASSERT_EQ(gid, st.st_gid);
319 ASSERT_EQ(mode, st.st_mode);
320 }
Calin Juravle7d765462017-09-04 15:57:10 -0700321};
322
323
324TEST_F(DexoptTest, DexoptSecondaryCe) {
325 LOG(INFO) << "DexoptSecondaryCe";
Calin Juravle29591732017-11-20 17:46:19 -0800326 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700327 /*binder_ok*/ true, /*compile_ok*/ true);
328}
329
330TEST_F(DexoptTest, DexoptSecondaryCeLink) {
331 LOG(INFO) << "DexoptSecondaryCeLink";
Calin Juravle29591732017-11-20 17:46:19 -0800332 CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700333 /*binder_ok*/ true, /*compile_ok*/ true);
334}
335
336TEST_F(DexoptTest, DexoptSecondaryDe) {
337 LOG(INFO) << "DexoptSecondaryDe";
Calin Juravle29591732017-11-20 17:46:19 -0800338 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700339 /*binder_ok*/ true, /*compile_ok*/ true);
340}
341
342TEST_F(DexoptTest, DexoptSecondaryDoesNotExist) {
343 LOG(INFO) << "DexoptSecondaryDoesNotExist";
344 // If the file validates but does not exist we do not treat it as an error.
Calin Juravle29591732017-11-20 17:46:19 -0800345 CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700346 /*binder_ok*/ true, /*compile_ok*/ false);
347}
348
349TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) {
350 LOG(INFO) << "DexoptSecondaryStorageValidationError";
Calin Juravle29591732017-11-20 17:46:19 -0800351 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700352 /*binder_ok*/ false, /*compile_ok*/ false);
353}
354
355TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) {
356 LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError";
Calin Juravle29591732017-11-20 17:46:19 -0800357 CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700358 /*binder_ok*/ false, /*compile_ok*/ false);
359}
360
361TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) {
362 LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError";
Calin Juravle29591732017-11-20 17:46:19 -0800363 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700364 /*binder_ok*/ false, /*compile_ok*/ false, kSystemUid);
365}
366
367
368class ReconcileTest : public DexoptTest {
369 virtual void SetUp() {
370 DexoptTest::SetUp();
Calin Juravle29591732017-11-20 17:46:19 -0800371 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700372 /*binder_ok*/ true, /*compile_ok*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800373 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700374 /*binder_ok*/ true, /*compile_ok*/ true);
375 }
376};
377
378TEST_F(ReconcileTest, ReconcileSecondaryCeExists) {
379 LOG(INFO) << "ReconcileSecondaryCeExists";
380 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
381 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
382}
383
384TEST_F(ReconcileTest, ReconcileSecondaryCeLinkExists) {
385 LOG(INFO) << "ReconcileSecondaryCeLinkExists";
386 reconcile_secondary_dex(secondary_dex_ce_link_, FLAG_STORAGE_CE,
387 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
388}
389
390TEST_F(ReconcileTest, ReconcileSecondaryDeExists) {
391 LOG(INFO) << "ReconcileSecondaryDeExists";
392 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
393 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
394}
395
396TEST_F(ReconcileTest, ReconcileSecondaryDeDoesNotExist) {
397 LOG(INFO) << "ReconcileSecondaryDeDoesNotExist";
398 run_cmd("rm -rf " + secondary_dex_de_);
399 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
400 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ true);
401}
402
403TEST_F(ReconcileTest, ReconcileSecondaryStorageValidationError) {
404 // Validation errors will not clean the odex/vdex/art files but will mark
405 // the file as non existent so that the PM knows it should purge it from its
406 // records.
407 LOG(INFO) << "ReconcileSecondaryStorageValidationError";
408 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_DE,
409 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false);
410}
411
412TEST_F(ReconcileTest, ReconcileSecondaryAppOwnershipValidationError) {
413 LOG(INFO) << "ReconcileSecondaryAppOwnershipValidationError";
414 // Attempt to reconcile the dex files of the test app from a different app.
415 std::string another_app = "another.app";
416 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
417 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid, &another_app);
418}
419
420TEST_F(ReconcileTest, ReconcileSecondaryAcessViaDifferentUidError) {
421 LOG(INFO) << "ReconcileSecondaryAcessViaDifferentUidError";
422 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
423 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid);
424}
425
Calin Juravle29591732017-11-20 17:46:19 -0800426class ProfileTest : public DexoptTest {
427 protected:
428 std::string cur_profile_;
429 std::string ref_profile_;
430 std::string snap_profile_;
431
432 virtual void SetUp() {
433 DexoptTest::SetUp();
434 cur_profile_ = create_current_profile_path(
435 kTestUserId, package_name_, /*is_secondary_dex*/ false);
436 ref_profile_ = create_reference_profile_path(package_name_, /*is_secondary_dex*/ false);
437 snap_profile_ = create_snapshot_profile_path(package_name_, "base.jar");
438 }
439
440 void SetupProfile(const std::string& path, uid_t uid, gid_t gid, mode_t mode, int32_t seed) {
441 run_cmd("profman --generate-test-profile-seed=" + std::to_string(seed) +
442 " --generate-test-profile-num-dex=2 --generate-test-profile=" + path);
443 ::chmod(path.c_str(), mode);
444 ::chown(path.c_str(), uid, gid);
445 }
446
447 void SetupProfiles(bool setup_ref) {
448 SetupProfile(cur_profile_, kTestAppUid, kTestAppGid, 0600, 1);
449 if (setup_ref) {
450 SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0060, 2);
451 }
452 }
453
454 void SnapshotProfile(int32_t appid, const std::string& package_name, bool expected_result) {
455 bool result;
456 binder::Status binder_result = service_->snapshotProfile(
457 appid, package_name, "base.jar", &result);
458 ASSERT_TRUE(binder_result.isOk());
459 ASSERT_EQ(expected_result, result);
460
461 if (!expected_result) {
462 // Do not check the files if we expect to fail.
463 return;
464 }
465
466 // Check that the snapshot was created witht he expected acess flags.
467 CheckFileAccess(snap_profile_, kSystemUid, kSystemGid, 0600 | S_IFREG);
468
469 // The snapshot should be equivalent to the merge of profiles.
470 std::string expected_profile_content = snap_profile_ + ".expected";
471 run_cmd("rm -f " + expected_profile_content);
472 run_cmd("touch " + expected_profile_content);
473 run_cmd("profman --profile-file=" + cur_profile_ +
474 " --profile-file=" + ref_profile_ +
475 " --reference-profile-file=" + expected_profile_content);
476
477 ASSERT_TRUE(AreFilesEqual(expected_profile_content, snap_profile_));
478
479 pid_t pid = fork();
480 if (pid == 0) {
481 /* child */
482 TransitionToSystemServer();
483
484 // System server should be able to open the the spanshot.
485 unique_fd fd(open(snap_profile_.c_str(), O_RDONLY));
486 ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno);
487 _exit(0);
488 }
489 /* parent */
490 ASSERT_TRUE(WIFEXITED(wait_child(pid)));
491 }
492
493 private:
494 void TransitionToSystemServer() {
495 ASSERT_TRUE(DropCapabilities(kSystemUid, kSystemGid));
496 int32_t res = selinux_android_setcontext(
497 kSystemUid, true, se_info_.c_str(), "system_server");
498 ASSERT_EQ(0, res) << "Failed to setcon " << strerror(errno);
499 }
500
501 bool AreFilesEqual(const std::string& file1, const std::string& file2) {
502 std::vector<uint8_t> content1;
503 std::vector<uint8_t> content2;
504
505 if (!ReadAll(file1, &content1)) return false;
506 if (!ReadAll(file2, &content2)) return false;
507 return content1 == content2;
508 }
509
510 bool ReadAll(const std::string& file, std::vector<uint8_t>* content) {
511 unique_fd fd(open(file.c_str(), O_RDONLY));
512 if (fd < 0) {
513 PLOG(ERROR) << "Failed to open " << file;
514 return false;
515 }
516 struct stat st;
517 if (fstat(fd, &st) != 0) {
518 PLOG(ERROR) << "Failed to stat " << file;
519 return false;
520 }
521 content->resize(st.st_size);
522 bool result = ReadFully(fd, content->data(), content->size());
523 if (!result) {
524 PLOG(ERROR) << "Failed to read " << file;
525 }
526 return result;
527 }
528};
529
530TEST_F(ProfileTest, ProfileSnapshotOk) {
531 LOG(INFO) << "ProfileSnapshotOk";
532
533 SetupProfiles(/*setup_ref*/ true);
534 SnapshotProfile(kTestAppId, package_name_, /*expected_result*/ true);
535}
536
537// The reference profile is created on the fly. We need to be able to
538// snapshot without one.
539TEST_F(ProfileTest, ProfileSnapshotOkNoReference) {
540 LOG(INFO) << "ProfileSnapshotOkNoReference";
541
542 SetupProfiles(/*setup_ref*/ false);
543 SnapshotProfile(kTestAppId, package_name_, /*expected_result*/ true);
544}
545
546TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) {
547 LOG(INFO) << "ProfileSnapshotFailWrongPackage";
548
549 SetupProfiles(/*setup_ref*/ true);
550 SnapshotProfile(kTestAppId, "not.there", /*expected_result*/ false);
551}
552
553TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) {
554 LOG(INFO) << "ProfileSnapshotDestroySnapshot";
555
556 SetupProfiles(/*setup_ref*/ true);
557 SnapshotProfile(kTestAppId, package_name_, /*expected_result*/ true);
558
559 binder::Status binder_result = service_->destroyProfileSnapshot(package_name_, "base.jar");
560 ASSERT_TRUE(binder_result.isOk());
561 struct stat st;
562 ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st));
563 ASSERT_EQ(ENOENT, errno);
564}
565
Calin Juravled2affb82017-11-28 17:41:43 -0800566TEST_F(ProfileTest, ProfileDirOk) {
567 LOG(INFO) << "ProfileDirOk";
568
569 std::string cur_profile_dir = create_primary_current_profile_package_dir_path(
570 kTestUserId, package_name_);
571 std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_,
572 /*is_secondary_dex*/false);
573 std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_);
574
575 CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
576 CheckFileAccess(cur_profile_file, kTestAppUid, kTestAppUid, 0600 | S_IFREG);
577 CheckFileAccess(ref_profile_dir, kTestAppGid, kTestAppGid, 0701 | S_IFDIR);
578}
579
Calin Juravle7d765462017-09-04 15:57:10 -0700580} // namespace installd
581} // namespace android