blob: e9093b249cadc622625ea195a58092e6bc030c9a [file] [log] [blame]
David Sehrb2ec9f52018-02-21 13:20:31 -08001/*
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
Orion Hodsond6e00a72021-02-10 13:52:40 +000022#include <optional>
Jiakai Zhangd61b0212021-07-08 10:20:26 +080023#include <vector>
Orion Hodsond6e00a72021-02-10 13:52:40 +000024
David Sehrb2ec9f52018-02-21 13:20:31 -080025#include "base/stl_util.h"
David Sehr7d432422018-05-25 10:49:02 -070026#include "common_art_test.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080027
28namespace art {
29
Jiakai Zhangd61b0212021-07-08 10:20:26 +080030static constexpr const char kAndroidWifiApexDefaultPath[] = "/apex/com.android.wifi";
31
David Sehr7d432422018-05-25 10:49:02 -070032class FileUtilsTest : public CommonArtTest {};
David Sehrb2ec9f52018-02-21 13:20:31 -080033
34TEST_F(FileUtilsTest, GetDalvikCacheFilename) {
35 std::string name;
36 std::string error;
37
38 EXPECT_TRUE(GetDalvikCacheFilename("/system/app/Foo.apk", "/foo", &name, &error)) << error;
39 EXPECT_EQ("/foo/system@app@Foo.apk@classes.dex", name);
40
41 EXPECT_TRUE(GetDalvikCacheFilename("/data/app/foo-1.apk", "/foo", &name, &error)) << error;
42 EXPECT_EQ("/foo/data@app@foo-1.apk@classes.dex", name);
43
44 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/core.jar", "/foo", &name, &error)) << error;
45 EXPECT_EQ("/foo/system@framework@core.jar@classes.dex", name);
46
47 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.art", "/foo", &name, &error)) << error;
48 EXPECT_EQ("/foo/system@framework@boot.art", name);
49
50 EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.oat", "/foo", &name, &error)) << error;
51 EXPECT_EQ("/foo/system@framework@boot.oat", name);
52}
53
David Sehrb2ec9f52018-02-21 13:20:31 -080054TEST_F(FileUtilsTest, GetSystemImageFilename) {
55 EXPECT_STREQ("/system/framework/arm/boot.art",
56 GetSystemImageFilename("/system/framework/boot.art", InstructionSet::kArm).c_str());
57}
58
David Srbecky7400a542020-07-09 13:40:57 +010059// TODO(dsrbecky): b/160885380: This test is failing in eng-prod because libartbase
60// is loaded from different path (under testcases).
61TEST_F(FileUtilsTest, DISABLED_GetAndroidRootSafe) {
David Sehrb2ec9f52018-02-21 13:20:31 -080062 std::string error_msg;
63
64 // We don't expect null returns for most cases, so don't check and let std::string crash.
65
David Sehr7d432422018-05-25 10:49:02 -070066 // CommonArtTest sets ANDROID_ROOT, so expect this to be the same.
David Sehrb2ec9f52018-02-21 13:20:31 -080067 std::string android_root = GetAndroidRootSafe(&error_msg);
68 std::string android_root_env = getenv("ANDROID_ROOT");
Roland Levillain1ea8a622019-03-29 19:08:56 +000069 EXPECT_EQ(android_root, android_root_env) << error_msg;
David Sehrb2ec9f52018-02-21 13:20:31 -080070
71 // Set ANDROID_ROOT to something else (but the directory must exist). So use dirname.
Andreas Gampedbf54032018-06-18 14:47:01 -070072 UniqueCPtr<char> root_dup(strdup(android_root_env.c_str()));
73 char* dir = dirname(root_dup.get());
Andreas Gampe0de385f2018-10-11 11:11:13 -070074 ASSERT_EQ(0, setenv("ANDROID_ROOT", dir, /* overwrite */ 1));
David Sehrb2ec9f52018-02-21 13:20:31 -080075 std::string android_root2 = GetAndroidRootSafe(&error_msg);
Roland Levillain1ea8a622019-03-29 19:08:56 +000076 EXPECT_STREQ(dir, android_root2.c_str()) << error_msg;
David Sehrb2ec9f52018-02-21 13:20:31 -080077
78 // Set a bogus value for ANDROID_ROOT. This should be an error.
Andreas Gampe0de385f2018-10-11 11:11:13 -070079 ASSERT_EQ(0, setenv("ANDROID_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1));
David Sehr7d432422018-05-25 10:49:02 -070080 EXPECT_EQ(GetAndroidRootSafe(&error_msg), "");
David Srbecky7400a542020-07-09 13:40:57 +010081 error_msg = "";
David Sehrb2ec9f52018-02-21 13:20:31 -080082
Roland Levillain50eec3d2019-04-05 18:53:58 +010083 // Inferring the Android Root from the location of libartbase only works on host.
84 if (!kIsTargetBuild) {
85 // Unset ANDROID_ROOT and see that it still returns something (as libartbase code is running).
86 ASSERT_EQ(0, unsetenv("ANDROID_ROOT"));
87 std::string android_root3 = GetAndroidRootSafe(&error_msg);
88 // This should be the same as the other root (modulo realpath), otherwise the test setup is
89 // broken. On non-bionic. On bionic we can be running with a different libartbase that lives
90 // outside of ANDROID_ROOT.
91 UniqueCPtr<char> real_root3(realpath(android_root3.c_str(), nullptr));
Alex Light680cbf22018-10-31 11:00:19 -070092#if !defined(__BIONIC__ ) || defined(__ANDROID__)
Roland Levillain50eec3d2019-04-05 18:53:58 +010093 UniqueCPtr<char> real_root(realpath(android_root.c_str(), nullptr));
94 EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg;
Alex Light680cbf22018-10-31 11:00:19 -070095#else
Roland Levillain50eec3d2019-04-05 18:53:58 +010096 EXPECT_STRNE(real_root3.get(), "") << error_msg;
Alex Light680cbf22018-10-31 11:00:19 -070097#endif
Roland Levillain50eec3d2019-04-05 18:53:58 +010098 }
David Sehrb2ec9f52018-02-21 13:20:31 -080099
100 // Reset ANDROID_ROOT, as other things may depend on it.
Andreas Gampe0de385f2018-10-11 11:11:13 -0700101 ASSERT_EQ(0, setenv("ANDROID_ROOT", android_root_env.c_str(), /* overwrite */ 1));
David Sehrb2ec9f52018-02-21 13:20:31 -0800102}
103
Martin Stjernholme58624f2019-09-20 15:53:40 +0100104TEST_F(FileUtilsTest, GetArtRootSafe) {
Roland Levillain1ea8a622019-03-29 19:08:56 +0000105 std::string error_msg;
Martin Stjernholme58624f2019-09-20 15:53:40 +0100106 std::string android_art_root;
107 std::string android_art_root_env;
Roland Levillain1ea8a622019-03-29 19:08:56 +0000108
Victor Chang64611242019-07-05 16:32:41 +0100109 // TODO(b/130295968): Re-enable this part when the directory exists on host
110 if (kIsTargetBuild) {
111 // We don't expect null returns for most cases, so don't check and let std::string crash.
Roland Levillain1ea8a622019-03-29 19:08:56 +0000112
Martin Stjernholme58624f2019-09-20 15:53:40 +0100113 // CommonArtTest sets ANDROID_ART_ROOT, so expect this to be the same.
114 android_art_root = GetArtRootSafe(&error_msg);
115 android_art_root_env = getenv("ANDROID_ART_ROOT");
116 EXPECT_EQ(android_art_root, android_art_root_env) << error_msg;
Roland Levillain1ea8a622019-03-29 19:08:56 +0000117
Martin Stjernholme58624f2019-09-20 15:53:40 +0100118 // Set ANDROID_ART_ROOT to something else (but the directory must exist). So use dirname.
119 UniqueCPtr<char> root_dup(strdup(android_art_root_env.c_str()));
Victor Chang64611242019-07-05 16:32:41 +0100120 char* dir = dirname(root_dup.get());
Martin Stjernholme58624f2019-09-20 15:53:40 +0100121 ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", dir, /* overwrite */ 1));
122 std::string android_art_root2 = GetArtRootSafe(&error_msg);
123 EXPECT_STREQ(dir, android_art_root2.c_str()) << error_msg;
Victor Chang64611242019-07-05 16:32:41 +0100124 }
Roland Levillain1ea8a622019-03-29 19:08:56 +0000125
Martin Stjernholme58624f2019-09-20 15:53:40 +0100126 // Set a bogus value for ANDROID_ART_ROOT. This should be an error.
127 ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1));
128 EXPECT_EQ(GetArtRootSafe(&error_msg), "");
Roland Levillain1ea8a622019-03-29 19:08:56 +0000129
Martin Stjernholme58624f2019-09-20 15:53:40 +0100130 // Inferring the ART root from the location of libartbase only works on target.
Roland Levillain50eec3d2019-04-05 18:53:58 +0100131 if (kIsTargetBuild) {
132 // Disabled for now, as we cannot reliably use `GetRootContainingLibartbase`
Martin Stjernholme58624f2019-09-20 15:53:40 +0100133 // to find the ART root on target yet (see comment in `GetArtRootSafe`).
Roland Levillain50eec3d2019-04-05 18:53:58 +0100134 //
135 // TODO(b/129534335): Re-enable this part of the test on target when the
Martin Stjernholme58624f2019-09-20 15:53:40 +0100136 // only instance of libartbase is the one from the ART APEX.
Roland Levillain50eec3d2019-04-05 18:53:58 +0100137 if ((false)) {
Martin Stjernholme58624f2019-09-20 15:53:40 +0100138 // Unset ANDROID_ART_ROOT and see that it still returns something (as
Roland Levillain50eec3d2019-04-05 18:53:58 +0100139 // libartbase code is running).
Martin Stjernholme58624f2019-09-20 15:53:40 +0100140 ASSERT_EQ(0, unsetenv("ANDROID_ART_ROOT"));
141 std::string android_art_root3 = GetArtRootSafe(&error_msg);
Roland Levillain50eec3d2019-04-05 18:53:58 +0100142 // This should be the same as the other root (modulo realpath), otherwise
143 // the test setup is broken. On non-bionic. On bionic we can be running
Martin Stjernholme58624f2019-09-20 15:53:40 +0100144 // with a different libartbase that lives outside of ANDROID_ART_ROOT.
145 UniqueCPtr<char> real_root3(realpath(android_art_root3.c_str(), nullptr));
Roland Levillain50eec3d2019-04-05 18:53:58 +0100146#if !defined(__BIONIC__ ) || defined(__ANDROID__)
Martin Stjernholme58624f2019-09-20 15:53:40 +0100147 UniqueCPtr<char> real_root(realpath(android_art_root.c_str(), nullptr));
Roland Levillain50eec3d2019-04-05 18:53:58 +0100148 EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg;
149#else
150 EXPECT_STRNE(real_root3.get(), "") << error_msg;
151#endif
152 }
153 }
154
Martin Stjernholme58624f2019-09-20 15:53:40 +0100155 // Reset ANDROID_ART_ROOT, as other things may depend on it.
156 ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", android_art_root_env.c_str(), /* overwrite */ 1));
Roland Levillain1ea8a622019-03-29 19:08:56 +0000157}
158
Vladimir Marko62c2d712018-03-09 12:54:05 +0000159TEST_F(FileUtilsTest, ReplaceFileExtension) {
160 EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file.oat", "vdex"));
161 EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file.oat", "vdex"));
162 EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file", "vdex"));
163 EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file", "vdex"));
164}
165
Orion Hodsond6e00a72021-02-10 13:52:40 +0000166TEST_F(FileUtilsTest, GetApexDataOatFilename) {
167 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
168 ScopedUnsetEnvironmentVariable i18n_root("ANDROID_I18N_ROOT");
169 ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
170
171 EXPECT_EQ(GetArtApexData() + "/dalvik-cache/arm/boot-beep.oat",
172 GetApexDataOatFilename("/product/javalib/beep.jar", InstructionSet::kArm));
173
174 const std::string art_apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar";
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800175 EXPECT_EQ(std::string {}, GetApexDataOatFilename(art_apex_jar.c_str(), InstructionSet::kArm));
Orion Hodsond6e00a72021-02-10 13:52:40 +0000176
177 const std::string i18n_jar =
178 std::string {kAndroidI18nApexDefaultPath} + "/javalib/core-icu4j.jar";
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800179 EXPECT_EQ(std::string {}, GetApexDataOatFilename(i18n_jar, InstructionSet::kArm));
Orion Hodsond6e00a72021-02-10 13:52:40 +0000180
181 const std::string system_jar_apexdata_oat = GetArtApexData() + "/dalvik-cache/x86/boot-lace.oat";
182 EXPECT_EQ(system_jar_apexdata_oat,
183 GetApexDataOatFilename("/system/framework/lace.jar", InstructionSet::kX86));
184}
185
186TEST_F(FileUtilsTest, GetApexDataOdexFilename) {
187 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
188 ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
189
190 EXPECT_EQ(GetArtApexData() + "/dalvik-cache/arm/data@some@code.odex",
191 GetApexDataOdexFilename("/data/some/code.dex", InstructionSet::kArm));
192
193 const std::string art_apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar";
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800194 EXPECT_EQ(
195 GetArtApexData() + "/dalvik-cache/arm/apex@com.android.art@javalib@some.jar@classes.odex",
196 GetApexDataOdexFilename(art_apex_jar.c_str(), InstructionSet::kArm));
Orion Hodsond6e00a72021-02-10 13:52:40 +0000197
198 const std::string i18n_jar =
199 std::string {kAndroidI18nApexDefaultPath} + "/javalib/core-icu4j.jar";
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800200 EXPECT_EQ(GetArtApexData() +
201 "/dalvik-cache/arm/apex@com.android.i18n@javalib@core-icu4j.jar@classes.odex",
202 GetApexDataOdexFilename(i18n_jar.c_str(), InstructionSet::kArm));
Orion Hodsond6e00a72021-02-10 13:52:40 +0000203
204 const std::string system_jar_apexdata_odex =
205 GetArtApexData() + "/dalvik-cache/x86/system@framework@cookie.jar@classes.odex";
206 EXPECT_EQ(system_jar_apexdata_odex,
207 GetApexDataOdexFilename("/system/framework/cookie.jar", InstructionSet::kX86));
208}
209
210TEST_F(FileUtilsTest, GetApexDataBootImage) {
211 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
212 ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
213
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800214 EXPECT_EQ(std::string {},
Orion Hodsond6e00a72021-02-10 13:52:40 +0000215 GetApexDataBootImage(std::string {kAndroidI18nApexDefaultPath} + "/javalib/bar.jar"));
216
217 // Check image location has the prefix "boot-" in front of the basename of dex location and
218 // that image suffix is .art.
219 const std::string system_jar = "/system/framework/disk.jar";
220 const std::string boot_image = GetApexDataBootImage(system_jar.c_str());
221 EXPECT_EQ(GetArtApexData() + "/dalvik-cache/boot-disk.art", boot_image);
222
223 // Check the image filename corresponds to the oat file for the same system jar.
224 const InstructionSet isa = InstructionSet::kArm64;
225 const std::string boot_image_filename = GetSystemImageFilename(boot_image.c_str(), isa);
226 const std::string accompanying_oat_file = ReplaceFileExtension(boot_image_filename, "oat");
227 EXPECT_EQ(accompanying_oat_file, GetApexDataOatFilename(system_jar.c_str(), isa));
228}
229
230TEST_F(FileUtilsTest, GetApexDataImage) {
231 ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
232 ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
233
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800234 EXPECT_EQ(
235 GetArtApexData() + "/dalvik-cache/apex@com.android.wifi@lib@javalib@bar.jar@classes.art",
236 GetApexDataImage(std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar"));
Orion Hodsond6e00a72021-02-10 13:52:40 +0000237
238 // Check image has basename of dex location with the .art suffix.
239 const char* jar = "/system/framework/mcguffin/test.jar";
240 const std::string image = GetApexDataImage(jar);
241 EXPECT_EQ(GetArtApexData() + "/dalvik-cache/system@framework@mcguffin@test.jar@classes.art",
242 image);
243
244 // Check the image filename corresponds to the .odex file for the same system jar.
245 const InstructionSet isa = InstructionSet::kX86_64;
246 const std::string image_filename = GetSystemImageFilename(image.c_str(), isa);
247 const std::string accompanying_odex_file = ReplaceFileExtension(image_filename, "odex");
248 EXPECT_EQ(accompanying_odex_file, GetApexDataOdexFilename(jar, isa));
249}
250
251TEST_F(FileUtilsTest, GetApexDataDalvikCacheFilename) {
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800252 const std::string apex_jar = std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar";
253 EXPECT_EQ(GetArtApexData() +
254 "/dalvik-cache/x86_64/apex@com.android.wifi@lib@javalib@bar.jar@classes.art",
Orion Hodsond6e00a72021-02-10 13:52:40 +0000255 GetApexDataDalvikCacheFilename(apex_jar, InstructionSet::kX86_64, "art"));
256
257 // Check dalvik-cache filename follows convention.
258 const std::string non_apex_jar = "/vendor/javalib/test.jar";
259 const std::string art_filename =
260 GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "art");
261 CHECK_EQ(GetArtApexData() + "/dalvik-cache/arm/vendor@javalib@test.jar@classes.art",
262 art_filename);
263
264 // Check ".art", ".odex" and ".vdex" filenames are the same with the appropriate extensions
265 // substituted.
266 const std::string odex_filename =
267 GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "odex");
268 CHECK_EQ(odex_filename, ReplaceFileExtension(art_filename, "odex"));
269 const std::string vdex_filename =
270 GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "vdex");
271 CHECK_EQ(vdex_filename, ReplaceFileExtension(art_filename, "vdex"));
272}
273
David Sehrb2ec9f52018-02-21 13:20:31 -0800274} // namespace art