1 /*
2 * Copyright (C) 2011 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 "base/file_utils.h"
18
19 #include <libgen.h>
20 #include <stdlib.h>
21
22 #include <optional>
23 #include <vector>
24
25 #include "base/stl_util.h"
26 #include "common_art_test.h"
27
28 namespace art {
29
30 static constexpr const char kAndroidWifiApexDefaultPath[] = "/apex/com.android.wifi";
31
32 namespace {
33 class ScopedOverrideDalvikCacheSubDirectory {
34 public:
ScopedOverrideDalvikCacheSubDirectory(const char * override)35 explicit ScopedOverrideDalvikCacheSubDirectory(const char* override) {
36 OverrideDalvikCacheSubDirectory(override);
37 }
38
~ScopedOverrideDalvikCacheSubDirectory()39 ~ScopedOverrideDalvikCacheSubDirectory() { OverrideDalvikCacheSubDirectory("dalvik-cache"); }
40
41 private:
42 DISALLOW_COPY_AND_ASSIGN(ScopedOverrideDalvikCacheSubDirectory);
43 };
44 } // namespace
45
46 class FileUtilsTest : public CommonArtTest {};
47
TEST_F(FileUtilsTest,GetDalvikCacheFilename)48 TEST_F(FileUtilsTest, GetDalvikCacheFilename) {
49 std::string name;
50 std::string error;
51
52 EXPECT_TRUE(GetDalvikCacheFilename("/system/app/Foo.apk", "/foo", &name, &error)) << error;
53 EXPECT_EQ("/foo/system@[email protected]@classes.dex", name);
54
55 EXPECT_TRUE(GetDalvikCacheFilename("/data/app/foo-1.apk", "/foo", &name, &error)) << error;
56 EXPECT_EQ("/foo/data@[email protected]@classes.dex", name);
57
58 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/core.jar", "/foo", &name, &error)) << error;
59 EXPECT_EQ("/foo/system@[email protected]@classes.dex", name);
60
61 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.art", "/foo", &name, &error)) << error;
62 EXPECT_EQ("/foo/system@[email protected]", name);
63
64 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.oat", "/foo", &name, &error)) << error;
65 EXPECT_EQ("/foo/system@[email protected]", name);
66 }
67
TEST_F(FileUtilsTest,GetSystemImageFilename)68 TEST_F(FileUtilsTest, GetSystemImageFilename) {
69 EXPECT_STREQ("/system/framework/arm/boot.art",
70 GetSystemImageFilename("/system/framework/boot.art", InstructionSet::kArm).c_str());
71 }
72
73 // TODO(dsrbecky): b/160885380: This test is failing in eng-prod because libartbase
74 // is loaded from different path (under testcases).
TEST_F(FileUtilsTest,DISABLED_GetAndroidRootSafe)75 TEST_F(FileUtilsTest, DISABLED_GetAndroidRootSafe) {
76 std::string error_msg;
77
78 // We don't expect null returns for most cases, so don't check and let std::string crash.
79
80 // CommonArtTest sets ANDROID_ROOT, so expect this to be the same.
81 std::string android_root = GetAndroidRootSafe(&error_msg);
82 std::string android_root_env = getenv("ANDROID_ROOT");
83 EXPECT_EQ(android_root, android_root_env) << error_msg;
84
85 // Set ANDROID_ROOT to something else (but the directory must exist). So use dirname.
86 UniqueCPtr<char> root_dup(strdup(android_root_env.c_str()));
87 char* dir = dirname(root_dup.get());
88 ASSERT_EQ(0, setenv("ANDROID_ROOT", dir, /* overwrite */ 1));
89 std::string android_root2 = GetAndroidRootSafe(&error_msg);
90 EXPECT_STREQ(dir, android_root2.c_str()) << error_msg;
91
92 // Set a bogus value for ANDROID_ROOT. This should be an error.
93 ASSERT_EQ(0, setenv("ANDROID_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1));
94 EXPECT_EQ(GetAndroidRootSafe(&error_msg), "");
95 error_msg = "";
96
97 // Inferring the Android Root from the location of libartbase only works on host.
98 if (!kIsTargetBuild) {
99 // Unset ANDROID_ROOT and see that it still returns something (as libartbase code is running).
100 ASSERT_EQ(0, unsetenv("ANDROID_ROOT"));
101 std::string android_root3 = GetAndroidRootSafe(&error_msg);
102 // This should be the same as the other root (modulo realpath), otherwise the test setup is
103 // broken. On non-bionic. On bionic we can be running with a different libartbase that lives
104 // outside of ANDROID_ROOT.
105 UniqueCPtr<char> real_root3(realpath(android_root3.c_str(), nullptr));
106 #if !defined(__BIONIC__) || defined(__ANDROID__)
107 UniqueCPtr<char> real_root(realpath(android_root.c_str(), nullptr));
108 EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg;
109 #else
110 EXPECT_STRNE(real_root3.get(), "") << error_msg;
111 #endif
112 }
113
114 // Reset ANDROID_ROOT, as other things may depend on it.
115 ASSERT_EQ(0, setenv("ANDROID_ROOT", android_root_env.c_str(), /* overwrite */ 1));
116 }
117
TEST_F(FileUtilsTest,GetArtRootSafe)118 TEST_F(FileUtilsTest, GetArtRootSafe) {
119 std::string error_msg;
120 std::string android_art_root;
121 std::string android_art_root_env;
122
123 // TODO(b/130295968): Re-enable this part when the directory exists on host
124 if (kIsTargetBuild) {
125 // We don't expect null returns for most cases, so don't check and let std::string crash.
126
127 // CommonArtTest sets ANDROID_ART_ROOT, so expect this to be the same.
128 android_art_root = GetArtRootSafe(&error_msg);
129 android_art_root_env = getenv("ANDROID_ART_ROOT");
130 EXPECT_EQ(android_art_root, android_art_root_env) << error_msg;
131
132 // Set ANDROID_ART_ROOT to something else (but the directory must exist). So use dirname.
133 UniqueCPtr<char> root_dup(strdup(android_art_root_env.c_str()));
134 char* dir = dirname(root_dup.get());
135 ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", dir, /* overwrite */ 1));
136 std::string android_art_root2 = GetArtRootSafe(&error_msg);
137 EXPECT_STREQ(dir, android_art_root2.c_str()) << error_msg;
138 }
139
140 // Set a bogus value for ANDROID_ART_ROOT. This should be an error.
141 ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1));
142 EXPECT_EQ(GetArtRootSafe(&error_msg), "");
143
144 // Inferring the ART root from the location of libartbase only works on target.
145 if (kIsTargetBuild) {
146 // Disabled for now, as we cannot reliably use `GetRootContainingLibartbase`
147 // to find the ART root on target yet (see comment in `GetArtRootSafe`).
148 //
149 // TODO(b/129534335): Re-enable this part of the test on target when the
150 // only instance of libartbase is the one from the ART APEX.
151 if ((false)) {
152 // Unset ANDROID_ART_ROOT and see that it still returns something (as
153 // libartbase code is running).
154 ASSERT_EQ(0, unsetenv("ANDROID_ART_ROOT"));
155 std::string android_art_root3 = GetArtRootSafe(&error_msg);
156 // This should be the same as the other root (modulo realpath), otherwise
157 // the test setup is broken. On non-bionic. On bionic we can be running
158 // with a different libartbase that lives outside of ANDROID_ART_ROOT.
159 UniqueCPtr<char> real_root3(realpath(android_art_root3.c_str(), nullptr));
160 #if !defined(__BIONIC__) || defined(__ANDROID__)
161 UniqueCPtr<char> real_root(realpath(android_art_root.c_str(), nullptr));
162 EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg;
163 #else
164 EXPECT_STRNE(real_root3.get(), "") << error_msg;
165 #endif
166 }
167 }
168
169 // Reset ANDROID_ART_ROOT, as other things may depend on it.
170 ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", android_art_root_env.c_str(), /* overwrite */ 1));
171 }
172
TEST_F(FileUtilsTest,ReplaceFileExtension)173 TEST_F(FileUtilsTest, ReplaceFileExtension) {
174 EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file.oat", "vdex"));
175 EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file.oat", "vdex"));
176 EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file", "vdex"));
177 EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file", "vdex"));
178 EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file.oat", ".vdex"));
179 EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file", ".vdex"));
180 }
181
TEST_F(FileUtilsTest,ArtApexDataPath)182 TEST_F(FileUtilsTest, ArtApexDataPath) {
183 ScopedUnsetEnvironmentVariable no_env("ART_APEX_DATA");
184 EXPECT_EQ(kArtApexDataDefaultPath, GetArtApexData());
185
186 setenv("ART_APEX_DATA", "/path/from/env", /* overwrite */ 1);
187 EXPECT_EQ("/path/from/env", GetArtApexData());
188 }
189
TEST_F(FileUtilsTest,GetApexDataOatFilename)190 TEST_F(FileUtilsTest, GetApexDataOatFilename) {
191 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
192 ScopedUnsetEnvironmentVariable i18n_root("ANDROID_I18N_ROOT");
193 ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
194
195 EXPECT_EQ(GetArtApexData() + "/dalvik-cache/arm/boot-beep.oat",
196 GetApexDataOatFilename("/product/javalib/beep.jar", InstructionSet::kArm));
197
198 const std::string art_apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar";
199 EXPECT_EQ(std::string{}, GetApexDataOatFilename(art_apex_jar, InstructionSet::kArm));
200
201 const std::string i18n_jar = std::string{kAndroidI18nApexDefaultPath} + "/javalib/core-icu4j.jar";
202 EXPECT_EQ(std::string{}, GetApexDataOatFilename(i18n_jar, InstructionSet::kArm));
203
204 const std::string system_jar_apexdata_oat = GetArtApexData() + "/dalvik-cache/x86/boot-lace.oat";
205 EXPECT_EQ(system_jar_apexdata_oat,
206 GetApexDataOatFilename("/system/framework/lace.jar", InstructionSet::kX86));
207 }
208
TEST_F(FileUtilsTest,GetApexDataOdexFilename)209 TEST_F(FileUtilsTest, GetApexDataOdexFilename) {
210 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
211 ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
212
213 EXPECT_EQ(GetArtApexData() + "/dalvik-cache/arm/data@[email protected]",
214 GetApexDataOdexFilename("/data/some/code.dex", InstructionSet::kArm));
215
216 const std::string art_apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar";
217 EXPECT_EQ(
218 GetArtApexData() + "/dalvik-cache/arm/apex@com.android.art@[email protected]@classes.odex",
219 GetApexDataOdexFilename(art_apex_jar, InstructionSet::kArm));
220
221 const std::string i18n_jar = std::string{kAndroidI18nApexDefaultPath} + "/javalib/core-icu4j.jar";
222 EXPECT_EQ(GetArtApexData() +
223 "/dalvik-cache/arm/apex@com.android.i18n@[email protected]@classes.odex",
224 GetApexDataOdexFilename(i18n_jar, InstructionSet::kArm));
225
226 const std::string system_jar_apexdata_odex =
227 GetArtApexData() + "/dalvik-cache/x86/system@[email protected]@classes.odex";
228 EXPECT_EQ(system_jar_apexdata_odex,
229 GetApexDataOdexFilename("/system/framework/cookie.jar", InstructionSet::kX86));
230 }
231
TEST_F(FileUtilsTest,GetApexDataBootImage)232 TEST_F(FileUtilsTest, GetApexDataBootImage) {
233 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
234 ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
235
236 EXPECT_EQ(std::string{},
237 GetApexDataBootImage(std::string{kAndroidI18nApexDefaultPath} + "/javalib/bar.jar"));
238
239 // Check image location has the prefix "boot-" in front of the basename of dex location and
240 // that image suffix is .art.
241 const std::string system_jar = "/system/framework/disk.jar";
242 const std::string boot_image = GetApexDataBootImage(system_jar);
243 EXPECT_EQ(GetArtApexData() + "/dalvik-cache/boot-disk.art", boot_image);
244
245 // Check the image filename corresponds to the oat file for the same system jar.
246 const InstructionSet isa = InstructionSet::kArm64;
247 const std::string boot_image_filename = GetSystemImageFilename(boot_image.c_str(), isa);
248 const std::string accompanying_oat_file = ReplaceFileExtension(boot_image_filename, "oat");
249 EXPECT_EQ(accompanying_oat_file, GetApexDataOatFilename(system_jar, isa));
250 }
251
TEST_F(FileUtilsTest,GetApexDataImage)252 TEST_F(FileUtilsTest, GetApexDataImage) {
253 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
254 ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
255
256 EXPECT_EQ(
257 GetArtApexData() + "/dalvik-cache/apex@com.android.wifi@lib@[email protected]@classes.art",
258 GetApexDataImage(std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar"));
259
260 // Check image has basename of dex location with the .art suffix.
261 const char* jar = "/system/framework/mcguffin/test.jar";
262 const std::string image = GetApexDataImage(jar);
263 EXPECT_EQ(GetArtApexData() + "/dalvik-cache/system@framework@[email protected]@classes.art",
264 image);
265
266 // Check the image filename corresponds to the .odex file for the same system jar.
267 const InstructionSet isa = InstructionSet::kX86_64;
268 const std::string image_filename = GetSystemImageFilename(image.c_str(), isa);
269 const std::string accompanying_odex_file = ReplaceFileExtension(image_filename, "odex");
270 EXPECT_EQ(accompanying_odex_file, GetApexDataOdexFilename(jar, isa));
271 }
272
TEST_F(FileUtilsTest,GetApexDataDalvikCacheFilename)273 TEST_F(FileUtilsTest, GetApexDataDalvikCacheFilename) {
274 const std::string apex_jar = std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar";
275 EXPECT_EQ(GetArtApexData() +
276 "/dalvik-cache/x86_64/apex@com.android.wifi@lib@[email protected]@classes.art",
277 GetApexDataDalvikCacheFilename(apex_jar, InstructionSet::kX86_64, "art"));
278
279 // Check dalvik-cache filename follows convention.
280 const std::string non_apex_jar = "/vendor/javalib/test.jar";
281 const std::string art_filename =
282 GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "art");
283 CHECK_EQ(GetArtApexData() + "/dalvik-cache/arm/vendor@[email protected]@classes.art",
284 art_filename);
285
286 // Check ".art", ".odex" and ".vdex" filenames are the same with the appropriate extensions
287 // substituted.
288 const std::string odex_filename =
289 GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "odex");
290 CHECK_EQ(odex_filename, ReplaceFileExtension(art_filename, "odex"));
291 const std::string vdex_filename =
292 GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "vdex");
293 CHECK_EQ(vdex_filename, ReplaceFileExtension(art_filename, "vdex"));
294 }
295
TEST_F(FileUtilsTest,OverrideDalvikCacheSubDirectory)296 TEST_F(FileUtilsTest, OverrideDalvikCacheSubDirectory) {
297 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
298 ScopedUnsetEnvironmentVariable i18n_root("ANDROID_I18N_ROOT");
299 ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
300
301 ScopedOverrideDalvikCacheSubDirectory dalvik_cache("overridden-cache");
302
303 EXPECT_EQ(GetArtApexData() + "/overridden-cache/arm/boot-beep.oat",
304 GetApexDataOatFilename("/product/javalib/beep.jar", InstructionSet::kArm));
305
306 EXPECT_EQ(GetArtApexData() + "/overridden-cache/arm/data@[email protected]",
307 GetApexDataOdexFilename("/data/some/code.dex", InstructionSet::kArm));
308
309 const std::string system_jar = "/system/framework/disk.jar";
310 const std::string boot_image = GetApexDataBootImage(system_jar);
311 EXPECT_EQ(GetArtApexData() + "/overridden-cache/boot-disk.art", boot_image);
312
313 EXPECT_EQ(
314 GetArtApexData() + "/overridden-cache/apex@com.android.wifi@lib@[email protected]@classes.art",
315 GetApexDataImage(std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar"));
316
317 const std::string apex_jar = std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar";
318 EXPECT_EQ(GetArtApexData() +
319 "/overridden-cache/x86_64/apex@com.android.wifi@lib@[email protected]@classes.art",
320 GetApexDataDalvikCacheFilename(apex_jar, InstructionSet::kX86_64, "art"));
321 }
322
TEST_F(FileUtilsTest,GetSystemOdexFilenameForApex)323 TEST_F(FileUtilsTest, GetSystemOdexFilenameForApex) {
324 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
325
326 const std::string apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar";
327 EXPECT_EQ(
328 GetSystemExtRoot() + "/framework/oat/arm/apex@com.android.art@[email protected]@classes.odex",
329 GetSystemOdexFilenameForApex(apex_jar, InstructionSet::kArm));
330 }
331
TEST_F(FileUtilsTest,ApexNameFromLocation)332 TEST_F(FileUtilsTest, ApexNameFromLocation) {
333 EXPECT_EQ("", ApexNameFromLocation(""));
334 EXPECT_EQ("", ApexNameFromLocation("/apex/com.android.foo"));
335 EXPECT_EQ("", ApexNameFromLocation("/apex//something"));
336 EXPECT_EQ("com.android.foo", ApexNameFromLocation("/apex/com.android.foo/"));
337 EXPECT_EQ("", ApexNameFromLocation("apex/com.android.foo/"));
338 EXPECT_EQ("foo", ApexNameFromLocation("/apex/foo/something.jar"));
339 EXPECT_EQ("", ApexNameFromLocation("/bar/foo/baz"));
340 EXPECT_EQ("", ApexNameFromLocation("/apexx/foo/baz"));
341 }
342
343 } // namespace art
344