blob: ca812bdeebc640412e6a2d5b7511facdd22577e6 [file] [log] [blame]
Jeff Sharkeye12d5962017-04-03 16:41:02 -06001/*
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
17#include <stdlib.h>
18#include <string.h>
19#include <sys/statvfs.h>
20#include <sys/xattr.h>
21
22#include <android-base/logging.h>
23#include <android-base/stringprintf.h>
24#include <cutils/properties.h>
25#include <gtest/gtest.h>
26
27#include "InstalldNativeService.h"
Jeff Sharkey1b9d9a62017-09-21 14:51:09 -060028#include "dexopt.h"
Jeff Sharkeye12d5962017-04-03 16:41:02 -060029#include "globals.h"
30#include "utils.h"
31
32using android::base::StringPrintf;
33
34namespace android {
35namespace installd {
36
37constexpr const char* kTestUuid = "TEST";
38
39static constexpr int FLAG_FORCE = 1 << 16;
40
41int get_property(const char *key, char *value, const char *default_value) {
42 return property_get(key, value, default_value);
43}
44
Jeff Sharkey1b9d9a62017-09-21 14:51:09 -060045bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
Calin Juravle249f2d32017-05-03 17:22:27 -070046 const char *instruction_set) {
Jeff Sharkey1b9d9a62017-09-21 14:51:09 -060047 return calculate_oat_file_path_default(path, oat_dir, apk_path, instruction_set);
48}
49
50bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
51 const char *instruction_set) {
52 return calculate_odex_file_path_default(path, apk_path, instruction_set);
53}
54
55bool create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) {
56 return create_cache_path_default(path, src, instruction_set);
Jeff Sharkeye12d5962017-04-03 16:41:02 -060057}
58
59static void mkdir(const char* path, uid_t owner, gid_t group, mode_t mode) {
60 const char* fullPath = StringPrintf("/data/local/tmp/user/0/%s", path).c_str();
61 ::mkdir(fullPath, mode);
62 ::chown(fullPath, owner, group);
63 ::chmod(fullPath, mode);
64}
65
66static void touch(const char* path, uid_t owner, gid_t group, mode_t mode) {
67 int fd = ::open(StringPrintf("/data/local/tmp/user/0/%s", path).c_str(),
68 O_RDWR | O_CREAT, mode);
69 ::fchown(fd, owner, group);
70 ::fchmod(fd, mode);
71 ::close(fd);
72}
73
74static int stat_gid(const char* path) {
75 struct stat buf;
76 ::stat(StringPrintf("/data/local/tmp/user/0/%s", path).c_str(), &buf);
77 return buf.st_gid;
78}
79
80static int stat_mode(const char* path) {
81 struct stat buf;
82 ::stat(StringPrintf("/data/local/tmp/user/0/%s", path).c_str(), &buf);
83 return buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);
84}
85
86class ServiceTest : public testing::Test {
87protected:
88 InstalldNativeService* service;
89 std::unique_ptr<std::string> testUuid;
90
91 virtual void SetUp() {
92 setenv("ANDROID_LOG_TAGS", "*:v", 1);
93 android::base::InitLogging(nullptr);
94
95 service = new InstalldNativeService();
96 testUuid = std::make_unique<std::string>();
97 *testUuid = std::string(kTestUuid);
98 system("mkdir -p /data/local/tmp/user/0");
Jeff Sharkey1b9d9a62017-09-21 14:51:09 -060099
100 init_globals_from_data_and_root();
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600101 }
102
103 virtual void TearDown() {
104 delete service;
105 system("rm -rf /data/local/tmp/user");
106 }
107};
108
109TEST_F(ServiceTest, FixupAppData_Upgrade) {
110 LOG(INFO) << "FixupAppData_Upgrade";
111
112 mkdir("com.example", 10000, 10000, 0700);
113 mkdir("com.example/normal", 10000, 10000, 0700);
114 mkdir("com.example/cache", 10000, 10000, 0700);
115 touch("com.example/cache/file", 10000, 10000, 0700);
116
117 service->fixupAppData(testUuid, 0);
118
119 EXPECT_EQ(10000, stat_gid("com.example/normal"));
120 EXPECT_EQ(20000, stat_gid("com.example/cache"));
121 EXPECT_EQ(20000, stat_gid("com.example/cache/file"));
122
123 EXPECT_EQ(0700, stat_mode("com.example/normal"));
124 EXPECT_EQ(02771, stat_mode("com.example/cache"));
125 EXPECT_EQ(0700, stat_mode("com.example/cache/file"));
126}
127
128TEST_F(ServiceTest, FixupAppData_Moved) {
129 LOG(INFO) << "FixupAppData_Moved";
130
131 mkdir("com.example", 10000, 10000, 0700);
132 mkdir("com.example/foo", 10000, 10000, 0700);
133 touch("com.example/foo/file", 10000, 20000, 0700);
134 mkdir("com.example/bar", 10000, 20000, 0700);
135 touch("com.example/bar/file", 10000, 20000, 0700);
136
137 service->fixupAppData(testUuid, 0);
138
139 EXPECT_EQ(10000, stat_gid("com.example/foo"));
140 EXPECT_EQ(20000, stat_gid("com.example/foo/file"));
141 EXPECT_EQ(10000, stat_gid("com.example/bar"));
142 EXPECT_EQ(10000, stat_gid("com.example/bar/file"));
143
144 service->fixupAppData(testUuid, FLAG_FORCE);
145
146 EXPECT_EQ(10000, stat_gid("com.example/foo"));
147 EXPECT_EQ(10000, stat_gid("com.example/foo/file"));
148 EXPECT_EQ(10000, stat_gid("com.example/bar"));
149 EXPECT_EQ(10000, stat_gid("com.example/bar/file"));
150}
151
Jeff Sharkey1b9d9a62017-09-21 14:51:09 -0600152TEST_F(ServiceTest, CalculateOat) {
153 char buf[PKG_PATH_MAX];
Calin Juravle249f2d32017-05-03 17:22:27 -0700154
Jeff Sharkey1b9d9a62017-09-21 14:51:09 -0600155 EXPECT_TRUE(calculate_oat_file_path(buf, "/path/to/oat", "/path/to/file.apk", "isa"));
156 EXPECT_EQ("/path/to/oat/isa/file.odex", std::string(buf));
157
158 EXPECT_FALSE(calculate_oat_file_path(buf, "/path/to/oat", "/path/to/file", "isa"));
159 EXPECT_FALSE(calculate_oat_file_path(buf, "/path/to/oat", "file", "isa"));
160}
161
162TEST_F(ServiceTest, CalculateOdex) {
163 char buf[PKG_PATH_MAX];
164
165 EXPECT_TRUE(calculate_odex_file_path(buf, "/path/to/file.apk", "isa"));
166 EXPECT_EQ("/path/to/oat/isa/file.odex", std::string(buf));
167}
168
169TEST_F(ServiceTest, CalculateCache) {
170 char buf[PKG_PATH_MAX];
171
172 EXPECT_TRUE(create_cache_path(buf, "/path/to/file.apk", "isa"));
173 EXPECT_EQ("/data/dalvik-cache/isa/path@to@file.apk@classes.dex", std::string(buf));
Calin Juravle249f2d32017-05-03 17:22:27 -0700174}
175
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600176} // namespace installd
177} // namespace android