blob: 19b42b57ff0c97ee869c594aa2268cd87d71acf7 [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;
147 static constexpr uid_t kTestAppUid = 19999;
148 static constexpr gid_t kTestAppGid = 19999;
Calin Juravle29591732017-11-20 17:46:19 -0800149 static constexpr uid_t kTestAppId = kTestAppUid;
Calin Juravle7d765462017-09-04 15:57:10 -0700150 static constexpr int32_t kTestUserId = 0;
151
152 InstalldNativeService* service_;
153 std::unique_ptr<std::string> volume_uuid_;
154 std::string package_name_;
155 std::string app_apk_dir_;
156 std::string app_private_dir_ce_;
157 std::string app_private_dir_de_;
158 std::string se_info_;
159
160 int64_t ce_data_inode_;
161
162 std::string secondary_dex_ce_;
163 std::string secondary_dex_ce_link_;
164 std::string secondary_dex_de_;
165
166 virtual void SetUp() {
167 setenv("ANDROID_LOG_TAGS", "*:v", 1);
168 android::base::InitLogging(nullptr);
Calin Juravle29591732017-11-20 17:46:19 -0800169 // Initialize the globals holding the file system main paths (/data/, /system/ etc..).
170 // This is needed in order to compute the application and profile paths.
171 ASSERT_TRUE(init_globals_from_data_and_root());
172 // Initialize selinux log callbacks.
173 // This ensures that selinux is up and running and re-directs the selinux messages
174 // to logcat (in order to make it easier to investigate test results).
175 ASSERT_TRUE(init_selinux());
Calin Juravle7d765462017-09-04 15:57:10 -0700176 service_ = new InstalldNativeService();
177
178 volume_uuid_ = nullptr;
179 package_name_ = "com.installd.test.dexopt";
180 se_info_ = "default";
Calin Juravle7d765462017-09-04 15:57:10 -0700181 app_apk_dir_ = android_app_dir + package_name_;
182
183 create_mock_app();
184 }
185
186 virtual void TearDown() {
187 if (!kDebug) {
188 service_->destroyAppData(
189 volume_uuid_, package_name_, kTestUserId, kAppDataFlags, ce_data_inode_);
190 run_cmd("rm -rf " + app_apk_dir_);
191 run_cmd("rm -rf " + app_private_dir_ce_);
192 run_cmd("rm -rf " + app_private_dir_de_);
193 }
194 delete service_;
195 }
196
197 void create_mock_app() {
198 // Create the oat dir.
199 std::string app_oat_dir = app_apk_dir_ + "/oat";
200 mkdir(app_apk_dir_, kSystemUid, kSystemGid, 0755);
201 service_->createOatDir(app_oat_dir, kRuntimeIsa);
202
203 // Copy the primary apk.
204 std::string apk_path = app_apk_dir_ + "/base.jar";
205 ASSERT_TRUE(WriteBase64ToFile(kDexFile, apk_path, kSystemUid, kSystemGid, 0644));
206
207 // Create the app user data.
208 ASSERT_TRUE(service_->createAppData(
209 volume_uuid_,
210 package_name_,
211 kTestUserId,
212 kAppDataFlags,
213 kTestAppUid,
214 se_info_,
215 kOSdkVersion,
216 &ce_data_inode_).isOk());
217
218 // Create a secondary dex file on CE storage
219 const char* volume_uuid_cstr = volume_uuid_ == nullptr ? nullptr : volume_uuid_->c_str();
220 app_private_dir_ce_ = create_data_user_ce_package_path(
221 volume_uuid_cstr, kTestUserId, package_name_.c_str());
222 secondary_dex_ce_ = app_private_dir_ce_ + "/secondary_ce.jar";
223 ASSERT_TRUE(WriteBase64ToFile(kDexFile, secondary_dex_ce_, kTestAppUid, kTestAppGid, 0600));
224 std::string app_private_dir_ce_link = create_data_user_ce_package_path_as_user_link(
225 volume_uuid_cstr, kTestUserId, package_name_.c_str());
226 secondary_dex_ce_link_ = app_private_dir_ce_link + "/secondary_ce.jar";
227
228 // Create a secondary dex file on DE storage.
229 app_private_dir_de_ = create_data_user_de_package_path(
230 volume_uuid_cstr, kTestUserId, package_name_.c_str());
231 secondary_dex_de_ = app_private_dir_de_ + "/secondary_de.jar";
232 ASSERT_TRUE(WriteBase64ToFile(kDexFile, secondary_dex_de_, kTestAppUid, kTestAppGid, 0600));
233
234 // Fix app data uid.
235 ASSERT_TRUE(service_->fixupAppData(volume_uuid_, kTestUserId).isOk());
236 }
237
238
Calin Juravle29591732017-11-20 17:46:19 -0800239 std::string GetSecondaryDexArtifact(const std::string& path, const std::string& type) {
Calin Juravle7d765462017-09-04 15:57:10 -0700240 std::string::size_type end = path.rfind('.');
241 std::string::size_type start = path.rfind('/', end);
242 return path.substr(0, start) + "/oat/" + kRuntimeIsa + "/" +
243 path.substr(start + 1, end - start) + type;
244 }
245
Calin Juravle29591732017-11-20 17:46:19 -0800246 void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag,
Calin Juravle7d765462017-09-04 15:57:10 -0700247 bool should_binder_call_succeed, bool should_dex_be_compiled = true,
248 int uid = kTestAppUid) {
249 std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_));
250 int32_t dexopt_needed = 0; // does not matter;
251 std::unique_ptr<std::string> out_path = nullptr; // does not matter
252 int32_t dex_flags = DEXOPT_SECONDARY_DEX | dex_storage_flag;
253 std::string compiler_filter = "speed-profile";
254 std::unique_ptr<std::string> class_loader_context_ptr(new std::string("&"));
255 std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_));
256 bool downgrade = false;
257
258 binder::Status result = service_->dexopt(path,
259 uid,
260 package_name_ptr,
261 kRuntimeIsa,
262 dexopt_needed,
263 out_path,
264 dex_flags,
265 compiler_filter,
266 volume_uuid_,
267 class_loader_context_ptr,
268 se_info_ptr,
269 downgrade);
270 ASSERT_EQ(should_binder_call_succeed, result.isOk());
271 int expected_access = should_dex_be_compiled ? 0 : -1;
Calin Juravle29591732017-11-20 17:46:19 -0800272 std::string odex = GetSecondaryDexArtifact(path, "odex");
273 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
274 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700275 ASSERT_EQ(expected_access, access(odex.c_str(), R_OK));
276 ASSERT_EQ(expected_access, access(vdex.c_str(), R_OK));
277 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
278 }
279
280 void reconcile_secondary_dex(const std::string& path, int32_t storage_flag,
281 bool should_binder_call_succeed, bool should_dex_exist, bool should_dex_be_deleted,
282 int uid = kTestAppUid, std::string* package_override = nullptr) {
283 std::vector<std::string> isas;
284 isas.push_back(kRuntimeIsa);
285 bool out_secondary_dex_exists = false;
286 binder::Status result = service_->reconcileSecondaryDexFile(
287 path,
288 package_override == nullptr ? package_name_ : *package_override,
289 uid,
290 isas,
291 volume_uuid_,
292 storage_flag,
293 &out_secondary_dex_exists);
294
295 ASSERT_EQ(should_binder_call_succeed, result.isOk());
296 ASSERT_EQ(should_dex_exist, out_secondary_dex_exists);
297
298 int expected_access = should_dex_be_deleted ? -1 : 0;
Calin Juravle29591732017-11-20 17:46:19 -0800299 std::string odex = GetSecondaryDexArtifact(path, "odex");
300 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
301 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700302 ASSERT_EQ(expected_access, access(odex.c_str(), F_OK));
303 ASSERT_EQ(expected_access, access(vdex.c_str(), F_OK));
304 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
305 }
Calin Juravle29591732017-11-20 17:46:19 -0800306
307 void CheckFileAccess(const std::string& file, uid_t uid, gid_t gid, mode_t mode) {
308 struct stat st;
309 ASSERT_EQ(0, stat(file.c_str(), &st));
310 ASSERT_EQ(uid, st.st_uid);
311 ASSERT_EQ(gid, st.st_gid);
312 ASSERT_EQ(mode, st.st_mode);
313 }
Calin Juravle7d765462017-09-04 15:57:10 -0700314};
315
316
317TEST_F(DexoptTest, DexoptSecondaryCe) {
318 LOG(INFO) << "DexoptSecondaryCe";
Calin Juravle29591732017-11-20 17:46:19 -0800319 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700320 /*binder_ok*/ true, /*compile_ok*/ true);
321}
322
323TEST_F(DexoptTest, DexoptSecondaryCeLink) {
324 LOG(INFO) << "DexoptSecondaryCeLink";
Calin Juravle29591732017-11-20 17:46:19 -0800325 CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700326 /*binder_ok*/ true, /*compile_ok*/ true);
327}
328
329TEST_F(DexoptTest, DexoptSecondaryDe) {
330 LOG(INFO) << "DexoptSecondaryDe";
Calin Juravle29591732017-11-20 17:46:19 -0800331 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700332 /*binder_ok*/ true, /*compile_ok*/ true);
333}
334
335TEST_F(DexoptTest, DexoptSecondaryDoesNotExist) {
336 LOG(INFO) << "DexoptSecondaryDoesNotExist";
337 // If the file validates but does not exist we do not treat it as an error.
Calin Juravle29591732017-11-20 17:46:19 -0800338 CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700339 /*binder_ok*/ true, /*compile_ok*/ false);
340}
341
342TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) {
343 LOG(INFO) << "DexoptSecondaryStorageValidationError";
Calin Juravle29591732017-11-20 17:46:19 -0800344 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700345 /*binder_ok*/ false, /*compile_ok*/ false);
346}
347
348TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) {
349 LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError";
Calin Juravle29591732017-11-20 17:46:19 -0800350 CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700351 /*binder_ok*/ false, /*compile_ok*/ false);
352}
353
354TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) {
355 LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError";
Calin Juravle29591732017-11-20 17:46:19 -0800356 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700357 /*binder_ok*/ false, /*compile_ok*/ false, kSystemUid);
358}
359
360
361class ReconcileTest : public DexoptTest {
362 virtual void SetUp() {
363 DexoptTest::SetUp();
Calin Juravle29591732017-11-20 17:46:19 -0800364 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700365 /*binder_ok*/ true, /*compile_ok*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800366 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700367 /*binder_ok*/ true, /*compile_ok*/ true);
368 }
369};
370
371TEST_F(ReconcileTest, ReconcileSecondaryCeExists) {
372 LOG(INFO) << "ReconcileSecondaryCeExists";
373 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
374 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
375}
376
377TEST_F(ReconcileTest, ReconcileSecondaryCeLinkExists) {
378 LOG(INFO) << "ReconcileSecondaryCeLinkExists";
379 reconcile_secondary_dex(secondary_dex_ce_link_, FLAG_STORAGE_CE,
380 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
381}
382
383TEST_F(ReconcileTest, ReconcileSecondaryDeExists) {
384 LOG(INFO) << "ReconcileSecondaryDeExists";
385 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
386 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
387}
388
389TEST_F(ReconcileTest, ReconcileSecondaryDeDoesNotExist) {
390 LOG(INFO) << "ReconcileSecondaryDeDoesNotExist";
391 run_cmd("rm -rf " + secondary_dex_de_);
392 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
393 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ true);
394}
395
396TEST_F(ReconcileTest, ReconcileSecondaryStorageValidationError) {
397 // Validation errors will not clean the odex/vdex/art files but will mark
398 // the file as non existent so that the PM knows it should purge it from its
399 // records.
400 LOG(INFO) << "ReconcileSecondaryStorageValidationError";
401 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_DE,
402 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false);
403}
404
405TEST_F(ReconcileTest, ReconcileSecondaryAppOwnershipValidationError) {
406 LOG(INFO) << "ReconcileSecondaryAppOwnershipValidationError";
407 // Attempt to reconcile the dex files of the test app from a different app.
408 std::string another_app = "another.app";
409 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
410 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid, &another_app);
411}
412
413TEST_F(ReconcileTest, ReconcileSecondaryAcessViaDifferentUidError) {
414 LOG(INFO) << "ReconcileSecondaryAcessViaDifferentUidError";
415 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
416 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid);
417}
418
Calin Juravle29591732017-11-20 17:46:19 -0800419class ProfileTest : public DexoptTest {
420 protected:
421 std::string cur_profile_;
422 std::string ref_profile_;
423 std::string snap_profile_;
424
425 virtual void SetUp() {
426 DexoptTest::SetUp();
427 cur_profile_ = create_current_profile_path(
428 kTestUserId, package_name_, /*is_secondary_dex*/ false);
429 ref_profile_ = create_reference_profile_path(package_name_, /*is_secondary_dex*/ false);
430 snap_profile_ = create_snapshot_profile_path(package_name_, "base.jar");
431 }
432
433 void SetupProfile(const std::string& path, uid_t uid, gid_t gid, mode_t mode, int32_t seed) {
434 run_cmd("profman --generate-test-profile-seed=" + std::to_string(seed) +
435 " --generate-test-profile-num-dex=2 --generate-test-profile=" + path);
436 ::chmod(path.c_str(), mode);
437 ::chown(path.c_str(), uid, gid);
438 }
439
440 void SetupProfiles(bool setup_ref) {
441 SetupProfile(cur_profile_, kTestAppUid, kTestAppGid, 0600, 1);
442 if (setup_ref) {
443 SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0060, 2);
444 }
445 }
446
447 void SnapshotProfile(int32_t appid, const std::string& package_name, bool expected_result) {
448 bool result;
449 binder::Status binder_result = service_->snapshotProfile(
450 appid, package_name, "base.jar", &result);
451 ASSERT_TRUE(binder_result.isOk());
452 ASSERT_EQ(expected_result, result);
453
454 if (!expected_result) {
455 // Do not check the files if we expect to fail.
456 return;
457 }
458
459 // Check that the snapshot was created witht he expected acess flags.
460 CheckFileAccess(snap_profile_, kSystemUid, kSystemGid, 0600 | S_IFREG);
461
462 // The snapshot should be equivalent to the merge of profiles.
463 std::string expected_profile_content = snap_profile_ + ".expected";
464 run_cmd("rm -f " + expected_profile_content);
465 run_cmd("touch " + expected_profile_content);
466 run_cmd("profman --profile-file=" + cur_profile_ +
467 " --profile-file=" + ref_profile_ +
468 " --reference-profile-file=" + expected_profile_content);
469
470 ASSERT_TRUE(AreFilesEqual(expected_profile_content, snap_profile_));
471
472 pid_t pid = fork();
473 if (pid == 0) {
474 /* child */
475 TransitionToSystemServer();
476
477 // System server should be able to open the the spanshot.
478 unique_fd fd(open(snap_profile_.c_str(), O_RDONLY));
479 ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno);
480 _exit(0);
481 }
482 /* parent */
483 ASSERT_TRUE(WIFEXITED(wait_child(pid)));
484 }
485
486 private:
487 void TransitionToSystemServer() {
488 ASSERT_TRUE(DropCapabilities(kSystemUid, kSystemGid));
489 int32_t res = selinux_android_setcontext(
490 kSystemUid, true, se_info_.c_str(), "system_server");
491 ASSERT_EQ(0, res) << "Failed to setcon " << strerror(errno);
492 }
493
494 bool AreFilesEqual(const std::string& file1, const std::string& file2) {
495 std::vector<uint8_t> content1;
496 std::vector<uint8_t> content2;
497
498 if (!ReadAll(file1, &content1)) return false;
499 if (!ReadAll(file2, &content2)) return false;
500 return content1 == content2;
501 }
502
503 bool ReadAll(const std::string& file, std::vector<uint8_t>* content) {
504 unique_fd fd(open(file.c_str(), O_RDONLY));
505 if (fd < 0) {
506 PLOG(ERROR) << "Failed to open " << file;
507 return false;
508 }
509 struct stat st;
510 if (fstat(fd, &st) != 0) {
511 PLOG(ERROR) << "Failed to stat " << file;
512 return false;
513 }
514 content->resize(st.st_size);
515 bool result = ReadFully(fd, content->data(), content->size());
516 if (!result) {
517 PLOG(ERROR) << "Failed to read " << file;
518 }
519 return result;
520 }
521};
522
523TEST_F(ProfileTest, ProfileSnapshotOk) {
524 LOG(INFO) << "ProfileSnapshotOk";
525
526 SetupProfiles(/*setup_ref*/ true);
527 SnapshotProfile(kTestAppId, package_name_, /*expected_result*/ true);
528}
529
530// The reference profile is created on the fly. We need to be able to
531// snapshot without one.
532TEST_F(ProfileTest, ProfileSnapshotOkNoReference) {
533 LOG(INFO) << "ProfileSnapshotOkNoReference";
534
535 SetupProfiles(/*setup_ref*/ false);
536 SnapshotProfile(kTestAppId, package_name_, /*expected_result*/ true);
537}
538
539TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) {
540 LOG(INFO) << "ProfileSnapshotFailWrongPackage";
541
542 SetupProfiles(/*setup_ref*/ true);
543 SnapshotProfile(kTestAppId, "not.there", /*expected_result*/ false);
544}
545
546TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) {
547 LOG(INFO) << "ProfileSnapshotDestroySnapshot";
548
549 SetupProfiles(/*setup_ref*/ true);
550 SnapshotProfile(kTestAppId, package_name_, /*expected_result*/ true);
551
552 binder::Status binder_result = service_->destroyProfileSnapshot(package_name_, "base.jar");
553 ASSERT_TRUE(binder_result.isOk());
554 struct stat st;
555 ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st));
556 ASSERT_EQ(ENOENT, errno);
557}
558
Calin Juravle7d765462017-09-04 15:57:10 -0700559} // namespace installd
560} // namespace android