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