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@app@Foo.apk@classes.dex", name);
54 
55   EXPECT_TRUE(GetDalvikCacheFilename("/data/app/foo-1.apk", "/foo", &name, &error)) << error;
56   EXPECT_EQ("/foo/data@app@foo-1.apk@classes.dex", name);
57 
58   EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/core.jar", "/foo", &name, &error)) << error;
59   EXPECT_EQ("/foo/system@framework@core.jar@classes.dex", name);
60 
61   EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.art", "/foo", &name, &error)) << error;
62   EXPECT_EQ("/foo/system@framework@boot.art", name);
63 
64   EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.oat", "/foo", &name, &error)) << error;
65   EXPECT_EQ("/foo/system@framework@boot.oat", 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 }
179 
TEST_F(FileUtilsTest,ArtApexDataPath)180 TEST_F(FileUtilsTest, ArtApexDataPath) {
181   ScopedUnsetEnvironmentVariable no_env("ART_APEX_DATA");
182   EXPECT_EQ(kArtApexDataDefaultPath, GetArtApexData());
183 
184   setenv("ART_APEX_DATA", "/path/from/env", /* overwrite */ 1);
185   EXPECT_EQ("/path/from/env", GetArtApexData());
186 }
187 
TEST_F(FileUtilsTest,GetApexDataOatFilename)188 TEST_F(FileUtilsTest, GetApexDataOatFilename) {
189   ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
190   ScopedUnsetEnvironmentVariable i18n_root("ANDROID_I18N_ROOT");
191   ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
192 
193   EXPECT_EQ(GetArtApexData() + "/dalvik-cache/arm/boot-beep.oat",
194             GetApexDataOatFilename("/product/javalib/beep.jar", InstructionSet::kArm));
195 
196   const std::string art_apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar";
197   EXPECT_EQ(std::string{}, GetApexDataOatFilename(art_apex_jar, InstructionSet::kArm));
198 
199   const std::string i18n_jar = std::string{kAndroidI18nApexDefaultPath} + "/javalib/core-icu4j.jar";
200   EXPECT_EQ(std::string{}, GetApexDataOatFilename(i18n_jar, InstructionSet::kArm));
201 
202   const std::string system_jar_apexdata_oat = GetArtApexData() + "/dalvik-cache/x86/boot-lace.oat";
203   EXPECT_EQ(system_jar_apexdata_oat,
204             GetApexDataOatFilename("/system/framework/lace.jar", InstructionSet::kX86));
205 }
206 
TEST_F(FileUtilsTest,GetApexDataOdexFilename)207 TEST_F(FileUtilsTest, GetApexDataOdexFilename) {
208   ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
209   ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
210 
211   EXPECT_EQ(GetArtApexData() + "/dalvik-cache/arm/data@some@code.odex",
212             GetApexDataOdexFilename("/data/some/code.dex", InstructionSet::kArm));
213 
214   const std::string art_apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar";
215   EXPECT_EQ(
216       GetArtApexData() + "/dalvik-cache/arm/apex@com.android.art@javalib@some.jar@classes.odex",
217       GetApexDataOdexFilename(art_apex_jar, InstructionSet::kArm));
218 
219   const std::string i18n_jar = std::string{kAndroidI18nApexDefaultPath} + "/javalib/core-icu4j.jar";
220   EXPECT_EQ(GetArtApexData() +
221                 "/dalvik-cache/arm/apex@com.android.i18n@javalib@core-icu4j.jar@classes.odex",
222             GetApexDataOdexFilename(i18n_jar, InstructionSet::kArm));
223 
224   const std::string system_jar_apexdata_odex =
225       GetArtApexData() + "/dalvik-cache/x86/system@framework@cookie.jar@classes.odex";
226   EXPECT_EQ(system_jar_apexdata_odex,
227             GetApexDataOdexFilename("/system/framework/cookie.jar", InstructionSet::kX86));
228 }
229 
TEST_F(FileUtilsTest,GetApexDataBootImage)230 TEST_F(FileUtilsTest, GetApexDataBootImage) {
231   ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
232   ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
233 
234   EXPECT_EQ(std::string{},
235             GetApexDataBootImage(std::string{kAndroidI18nApexDefaultPath} + "/javalib/bar.jar"));
236 
237   // Check image location has the prefix "boot-" in front of the basename of dex location and
238   // that image suffix is .art.
239   const std::string system_jar = "/system/framework/disk.jar";
240   const std::string boot_image = GetApexDataBootImage(system_jar);
241   EXPECT_EQ(GetArtApexData() + "/dalvik-cache/boot-disk.art", boot_image);
242 
243   // Check the image filename corresponds to the oat file for the same system jar.
244   const InstructionSet isa = InstructionSet::kArm64;
245   const std::string boot_image_filename = GetSystemImageFilename(boot_image.c_str(), isa);
246   const std::string accompanying_oat_file = ReplaceFileExtension(boot_image_filename, "oat");
247   EXPECT_EQ(accompanying_oat_file, GetApexDataOatFilename(system_jar, isa));
248 }
249 
TEST_F(FileUtilsTest,GetApexDataImage)250 TEST_F(FileUtilsTest, GetApexDataImage) {
251   ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
252   ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
253 
254   EXPECT_EQ(
255       GetArtApexData() + "/dalvik-cache/apex@com.android.wifi@lib@javalib@bar.jar@classes.art",
256       GetApexDataImage(std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar"));
257 
258   // Check image has basename of dex location with the .art suffix.
259   const char* jar = "/system/framework/mcguffin/test.jar";
260   const std::string image = GetApexDataImage(jar);
261   EXPECT_EQ(GetArtApexData() + "/dalvik-cache/system@framework@mcguffin@test.jar@classes.art",
262             image);
263 
264   // Check the image filename corresponds to the .odex file for the same system jar.
265   const InstructionSet isa = InstructionSet::kX86_64;
266   const std::string image_filename = GetSystemImageFilename(image.c_str(), isa);
267   const std::string accompanying_odex_file = ReplaceFileExtension(image_filename, "odex");
268   EXPECT_EQ(accompanying_odex_file, GetApexDataOdexFilename(jar, isa));
269 }
270 
TEST_F(FileUtilsTest,GetApexDataDalvikCacheFilename)271 TEST_F(FileUtilsTest, GetApexDataDalvikCacheFilename) {
272   const std::string apex_jar = std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar";
273   EXPECT_EQ(GetArtApexData() +
274                 "/dalvik-cache/x86_64/apex@com.android.wifi@lib@javalib@bar.jar@classes.art",
275             GetApexDataDalvikCacheFilename(apex_jar, InstructionSet::kX86_64, "art"));
276 
277   // Check dalvik-cache filename follows convention.
278   const std::string non_apex_jar = "/vendor/javalib/test.jar";
279   const std::string art_filename =
280       GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "art");
281   CHECK_EQ(GetArtApexData() + "/dalvik-cache/arm/vendor@javalib@test.jar@classes.art",
282            art_filename);
283 
284   // Check ".art", ".odex" and ".vdex" filenames are the same with the appropriate extensions
285   // substituted.
286   const std::string odex_filename =
287       GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "odex");
288   CHECK_EQ(odex_filename, ReplaceFileExtension(art_filename, "odex"));
289   const std::string vdex_filename =
290       GetApexDataDalvikCacheFilename(non_apex_jar, InstructionSet::kArm, "vdex");
291   CHECK_EQ(vdex_filename, ReplaceFileExtension(art_filename, "vdex"));
292 }
293 
TEST_F(FileUtilsTest,OverrideDalvikCacheSubDirectory)294 TEST_F(FileUtilsTest, OverrideDalvikCacheSubDirectory) {
295   ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
296   ScopedUnsetEnvironmentVariable i18n_root("ANDROID_I18N_ROOT");
297   ScopedUnsetEnvironmentVariable art_apex_data("ART_APEX_DATA");
298 
299   ScopedOverrideDalvikCacheSubDirectory dalvik_cache("overridden-cache");
300 
301   EXPECT_EQ(GetArtApexData() + "/overridden-cache/arm/boot-beep.oat",
302             GetApexDataOatFilename("/product/javalib/beep.jar", InstructionSet::kArm));
303 
304   EXPECT_EQ(GetArtApexData() + "/overridden-cache/arm/data@some@code.odex",
305             GetApexDataOdexFilename("/data/some/code.dex", InstructionSet::kArm));
306 
307   const std::string system_jar = "/system/framework/disk.jar";
308   const std::string boot_image = GetApexDataBootImage(system_jar);
309   EXPECT_EQ(GetArtApexData() + "/overridden-cache/boot-disk.art", boot_image);
310 
311   EXPECT_EQ(
312       GetArtApexData() + "/overridden-cache/apex@com.android.wifi@lib@javalib@bar.jar@classes.art",
313       GetApexDataImage(std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar"));
314 
315   const std::string apex_jar = std::string {kAndroidWifiApexDefaultPath} + "/lib/javalib/bar.jar";
316   EXPECT_EQ(GetArtApexData() +
317                 "/overridden-cache/x86_64/apex@com.android.wifi@lib@javalib@bar.jar@classes.art",
318             GetApexDataDalvikCacheFilename(apex_jar, InstructionSet::kX86_64, "art"));
319 }
320 
TEST_F(FileUtilsTest,GetSystemOdexFilenameForApex)321 TEST_F(FileUtilsTest, GetSystemOdexFilenameForApex) {
322   ScopedUnsetEnvironmentVariable android_root("ANDROID_ROOT");
323 
324   const std::string apex_jar = std::string {kAndroidArtApexDefaultPath} + "/javalib/some.jar";
325   EXPECT_EQ(
326       GetAndroidRoot() + "/framework/oat/arm/apex@com.android.art@javalib@some.jar@classes.odex",
327       GetSystemOdexFilenameForApex(apex_jar, InstructionSet::kArm));
328 }
329 
TEST_F(FileUtilsTest,ApexNameFromLocation)330 TEST_F(FileUtilsTest, ApexNameFromLocation) {
331   EXPECT_EQ("", ApexNameFromLocation(""));
332   EXPECT_EQ("", ApexNameFromLocation("/apex/com.android.foo"));
333   EXPECT_EQ("", ApexNameFromLocation("/apex//something"));
334   EXPECT_EQ("com.android.foo", ApexNameFromLocation("/apex/com.android.foo/"));
335   EXPECT_EQ("", ApexNameFromLocation("apex/com.android.foo/"));
336   EXPECT_EQ("foo", ApexNameFromLocation("/apex/foo/something.jar"));
337   EXPECT_EQ("", ApexNameFromLocation("/bar/foo/baz"));
338   EXPECT_EQ("", ApexNameFromLocation("/apexx/foo/baz"));
339 }
340 
341 }  // namespace art
342