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 "base/stl_util.h"
23 #include "common_art_test.h"
24 
25 namespace art {
26 
27 class FileUtilsTest : public CommonArtTest {};
28 
TEST_F(FileUtilsTest,GetDalvikCacheFilename)29 TEST_F(FileUtilsTest, GetDalvikCacheFilename) {
30   std::string name;
31   std::string error;
32 
33   EXPECT_TRUE(GetDalvikCacheFilename("/system/app/Foo.apk", "/foo", &name, &error)) << error;
34   EXPECT_EQ("/foo/system@app@Foo.apk@classes.dex", name);
35 
36   EXPECT_TRUE(GetDalvikCacheFilename("/data/app/foo-1.apk", "/foo", &name, &error)) << error;
37   EXPECT_EQ("/foo/data@app@foo-1.apk@classes.dex", name);
38 
39   EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/core.jar", "/foo", &name, &error)) << error;
40   EXPECT_EQ("/foo/system@framework@core.jar@classes.dex", name);
41 
42   EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.art", "/foo", &name, &error)) << error;
43   EXPECT_EQ("/foo/system@framework@boot.art", name);
44 
45   EXPECT_TRUE(GetDalvikCacheFilename("/system/framework/boot.oat", "/foo", &name, &error)) << error;
46   EXPECT_EQ("/foo/system@framework@boot.oat", name);
47 }
48 
TEST_F(FileUtilsTest,GetDalvikCache)49 TEST_F(FileUtilsTest, GetDalvikCache) {
50   EXPECT_STREQ("", GetDalvikCache("should-not-exist123").c_str());
51 
52   EXPECT_STREQ((android_data_ + "/dalvik-cache/.").c_str(), GetDalvikCache(".").c_str());
53 }
54 
55 
TEST_F(FileUtilsTest,GetSystemImageFilename)56 TEST_F(FileUtilsTest, GetSystemImageFilename) {
57   EXPECT_STREQ("/system/framework/arm/boot.art",
58                GetSystemImageFilename("/system/framework/boot.art", InstructionSet::kArm).c_str());
59 }
60 
61 // TODO(dsrbecky): b/160885380: This test is failing in eng-prod because libartbase
62 //                              is loaded from different path (under testcases).
TEST_F(FileUtilsTest,DISABLED_GetAndroidRootSafe)63 TEST_F(FileUtilsTest, DISABLED_GetAndroidRootSafe) {
64   std::string error_msg;
65 
66   // We don't expect null returns for most cases, so don't check and let std::string crash.
67 
68   // CommonArtTest sets ANDROID_ROOT, so expect this to be the same.
69   std::string android_root = GetAndroidRootSafe(&error_msg);
70   std::string android_root_env = getenv("ANDROID_ROOT");
71   EXPECT_EQ(android_root, android_root_env) << error_msg;
72 
73   // Set ANDROID_ROOT to something else (but the directory must exist). So use dirname.
74   UniqueCPtr<char> root_dup(strdup(android_root_env.c_str()));
75   char* dir = dirname(root_dup.get());
76   ASSERT_EQ(0, setenv("ANDROID_ROOT", dir, /* overwrite */ 1));
77   std::string android_root2 = GetAndroidRootSafe(&error_msg);
78   EXPECT_STREQ(dir, android_root2.c_str()) << error_msg;
79 
80   // Set a bogus value for ANDROID_ROOT. This should be an error.
81   ASSERT_EQ(0, setenv("ANDROID_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1));
82   EXPECT_EQ(GetAndroidRootSafe(&error_msg), "");
83   error_msg = "";
84 
85   // Inferring the Android Root from the location of libartbase only works on host.
86   if (!kIsTargetBuild) {
87     // Unset ANDROID_ROOT and see that it still returns something (as libartbase code is running).
88     ASSERT_EQ(0, unsetenv("ANDROID_ROOT"));
89     std::string android_root3 = GetAndroidRootSafe(&error_msg);
90     // This should be the same as the other root (modulo realpath), otherwise the test setup is
91     // broken. On non-bionic. On bionic we can be running with a different libartbase that lives
92     // outside of ANDROID_ROOT.
93     UniqueCPtr<char> real_root3(realpath(android_root3.c_str(), nullptr));
94 #if !defined(__BIONIC__ ) || defined(__ANDROID__)
95     UniqueCPtr<char> real_root(realpath(android_root.c_str(), nullptr));
96     EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg;
97 #else
98     EXPECT_STRNE(real_root3.get(), "") << error_msg;
99 #endif
100   }
101 
102   // Reset ANDROID_ROOT, as other things may depend on it.
103   ASSERT_EQ(0, setenv("ANDROID_ROOT", android_root_env.c_str(), /* overwrite */ 1));
104 }
105 
TEST_F(FileUtilsTest,GetArtRootSafe)106 TEST_F(FileUtilsTest, GetArtRootSafe) {
107   std::string error_msg;
108   std::string android_art_root;
109   std::string android_art_root_env;
110 
111   // TODO(b/130295968): Re-enable this part when the directory exists on host
112   if (kIsTargetBuild) {
113     // We don't expect null returns for most cases, so don't check and let std::string crash.
114 
115     // CommonArtTest sets ANDROID_ART_ROOT, so expect this to be the same.
116     android_art_root = GetArtRootSafe(&error_msg);
117     android_art_root_env = getenv("ANDROID_ART_ROOT");
118     EXPECT_EQ(android_art_root, android_art_root_env) << error_msg;
119 
120     // Set ANDROID_ART_ROOT to something else (but the directory must exist). So use dirname.
121     UniqueCPtr<char> root_dup(strdup(android_art_root_env.c_str()));
122     char* dir = dirname(root_dup.get());
123     ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", dir, /* overwrite */ 1));
124     std::string android_art_root2 = GetArtRootSafe(&error_msg);
125     EXPECT_STREQ(dir, android_art_root2.c_str()) << error_msg;
126   }
127 
128   // Set a bogus value for ANDROID_ART_ROOT. This should be an error.
129   ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1));
130   EXPECT_EQ(GetArtRootSafe(&error_msg), "");
131 
132   // Inferring the ART root from the location of libartbase only works on target.
133   if (kIsTargetBuild) {
134     // Disabled for now, as we cannot reliably use `GetRootContainingLibartbase`
135     // to find the ART root on target yet (see comment in `GetArtRootSafe`).
136     //
137     // TODO(b/129534335): Re-enable this part of the test on target when the
138     // only instance of libartbase is the one from the ART APEX.
139     if ((false)) {
140       // Unset ANDROID_ART_ROOT and see that it still returns something (as
141       // libartbase code is running).
142       ASSERT_EQ(0, unsetenv("ANDROID_ART_ROOT"));
143       std::string android_art_root3 = GetArtRootSafe(&error_msg);
144       // This should be the same as the other root (modulo realpath), otherwise
145       // the test setup is broken. On non-bionic. On bionic we can be running
146       // with a different libartbase that lives outside of ANDROID_ART_ROOT.
147       UniqueCPtr<char> real_root3(realpath(android_art_root3.c_str(), nullptr));
148 #if !defined(__BIONIC__ ) || defined(__ANDROID__)
149       UniqueCPtr<char> real_root(realpath(android_art_root.c_str(), nullptr));
150       EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg;
151 #else
152       EXPECT_STRNE(real_root3.get(), "") << error_msg;
153 #endif
154     }
155   }
156 
157   // Reset ANDROID_ART_ROOT, as other things may depend on it.
158   ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", android_art_root_env.c_str(), /* overwrite */ 1));
159 }
160 
TEST_F(FileUtilsTest,ReplaceFileExtension)161 TEST_F(FileUtilsTest, ReplaceFileExtension) {
162   EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file.oat", "vdex"));
163   EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file.oat", "vdex"));
164   EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file", "vdex"));
165   EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file", "vdex"));
166 }
167 
168 }  // namespace art
169