blob: 85c110465fdd096e9c5e8337795304621331adb7 [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
22#include "base/stl_util.h"
David Sehr7d432422018-05-25 10:49:02 -070023#include "common_art_test.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080024
25namespace art {
26
David Sehr7d432422018-05-25 10:49:02 -070027class FileUtilsTest : public CommonArtTest {};
David Sehrb2ec9f52018-02-21 13:20:31 -080028
29TEST_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
49TEST_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
56TEST_F(FileUtilsTest, GetSystemImageFilename) {
57 EXPECT_STREQ("/system/framework/arm/boot.art",
58 GetSystemImageFilename("/system/framework/boot.art", InstructionSet::kArm).c_str());
59}
60
61TEST_F(FileUtilsTest, GetAndroidRootSafe) {
62 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 Sehrb2ec9f52018-02-21 13:20:31 -080081
Roland Levillain50eec3d2019-04-05 18:53:58 +010082 // Inferring the Android Root from the location of libartbase only works on host.
83 if (!kIsTargetBuild) {
84 // Unset ANDROID_ROOT and see that it still returns something (as libartbase code is running).
85 ASSERT_EQ(0, unsetenv("ANDROID_ROOT"));
86 std::string android_root3 = GetAndroidRootSafe(&error_msg);
87 // This should be the same as the other root (modulo realpath), otherwise the test setup is
88 // broken. On non-bionic. On bionic we can be running with a different libartbase that lives
89 // outside of ANDROID_ROOT.
90 UniqueCPtr<char> real_root3(realpath(android_root3.c_str(), nullptr));
Alex Light680cbf22018-10-31 11:00:19 -070091#if !defined(__BIONIC__ ) || defined(__ANDROID__)
Roland Levillain50eec3d2019-04-05 18:53:58 +010092 UniqueCPtr<char> real_root(realpath(android_root.c_str(), nullptr));
93 EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg;
Alex Light680cbf22018-10-31 11:00:19 -070094#else
Roland Levillain50eec3d2019-04-05 18:53:58 +010095 EXPECT_STRNE(real_root3.get(), "") << error_msg;
Alex Light680cbf22018-10-31 11:00:19 -070096#endif
Roland Levillain50eec3d2019-04-05 18:53:58 +010097 }
David Sehrb2ec9f52018-02-21 13:20:31 -080098
99 // Reset ANDROID_ROOT, as other things may depend on it.
Andreas Gampe0de385f2018-10-11 11:11:13 -0700100 ASSERT_EQ(0, setenv("ANDROID_ROOT", android_root_env.c_str(), /* overwrite */ 1));
David Sehrb2ec9f52018-02-21 13:20:31 -0800101}
102
Martin Stjernholme58624f2019-09-20 15:53:40 +0100103TEST_F(FileUtilsTest, GetArtRootSafe) {
Roland Levillain1ea8a622019-03-29 19:08:56 +0000104 std::string error_msg;
Martin Stjernholme58624f2019-09-20 15:53:40 +0100105 std::string android_art_root;
106 std::string android_art_root_env;
Roland Levillain1ea8a622019-03-29 19:08:56 +0000107
Victor Chang64611242019-07-05 16:32:41 +0100108 // TODO(b/130295968): Re-enable this part when the directory exists on host
109 if (kIsTargetBuild) {
110 // 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 +0000111
Martin Stjernholme58624f2019-09-20 15:53:40 +0100112 // CommonArtTest sets ANDROID_ART_ROOT, so expect this to be the same.
113 android_art_root = GetArtRootSafe(&error_msg);
114 android_art_root_env = getenv("ANDROID_ART_ROOT");
115 EXPECT_EQ(android_art_root, android_art_root_env) << error_msg;
Roland Levillain1ea8a622019-03-29 19:08:56 +0000116
Martin Stjernholme58624f2019-09-20 15:53:40 +0100117 // Set ANDROID_ART_ROOT to something else (but the directory must exist). So use dirname.
118 UniqueCPtr<char> root_dup(strdup(android_art_root_env.c_str()));
Victor Chang64611242019-07-05 16:32:41 +0100119 char* dir = dirname(root_dup.get());
Martin Stjernholme58624f2019-09-20 15:53:40 +0100120 ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", dir, /* overwrite */ 1));
121 std::string android_art_root2 = GetArtRootSafe(&error_msg);
122 EXPECT_STREQ(dir, android_art_root2.c_str()) << error_msg;
Victor Chang64611242019-07-05 16:32:41 +0100123 }
Roland Levillain1ea8a622019-03-29 19:08:56 +0000124
Martin Stjernholme58624f2019-09-20 15:53:40 +0100125 // Set a bogus value for ANDROID_ART_ROOT. This should be an error.
126 ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1));
127 EXPECT_EQ(GetArtRootSafe(&error_msg), "");
Roland Levillain1ea8a622019-03-29 19:08:56 +0000128
Martin Stjernholme58624f2019-09-20 15:53:40 +0100129 // Inferring the ART root from the location of libartbase only works on target.
Roland Levillain50eec3d2019-04-05 18:53:58 +0100130 if (kIsTargetBuild) {
131 // Disabled for now, as we cannot reliably use `GetRootContainingLibartbase`
Martin Stjernholme58624f2019-09-20 15:53:40 +0100132 // to find the ART root on target yet (see comment in `GetArtRootSafe`).
Roland Levillain50eec3d2019-04-05 18:53:58 +0100133 //
134 // TODO(b/129534335): Re-enable this part of the test on target when the
Martin Stjernholme58624f2019-09-20 15:53:40 +0100135 // only instance of libartbase is the one from the ART APEX.
Roland Levillain50eec3d2019-04-05 18:53:58 +0100136 if ((false)) {
Martin Stjernholme58624f2019-09-20 15:53:40 +0100137 // Unset ANDROID_ART_ROOT and see that it still returns something (as
Roland Levillain50eec3d2019-04-05 18:53:58 +0100138 // libartbase code is running).
Martin Stjernholme58624f2019-09-20 15:53:40 +0100139 ASSERT_EQ(0, unsetenv("ANDROID_ART_ROOT"));
140 std::string android_art_root3 = GetArtRootSafe(&error_msg);
Roland Levillain50eec3d2019-04-05 18:53:58 +0100141 // This should be the same as the other root (modulo realpath), otherwise
142 // the test setup is broken. On non-bionic. On bionic we can be running
Martin Stjernholme58624f2019-09-20 15:53:40 +0100143 // with a different libartbase that lives outside of ANDROID_ART_ROOT.
144 UniqueCPtr<char> real_root3(realpath(android_art_root3.c_str(), nullptr));
Roland Levillain50eec3d2019-04-05 18:53:58 +0100145#if !defined(__BIONIC__ ) || defined(__ANDROID__)
Martin Stjernholme58624f2019-09-20 15:53:40 +0100146 UniqueCPtr<char> real_root(realpath(android_art_root.c_str(), nullptr));
Roland Levillain50eec3d2019-04-05 18:53:58 +0100147 EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg;
148#else
149 EXPECT_STRNE(real_root3.get(), "") << error_msg;
150#endif
151 }
152 }
153
Martin Stjernholme58624f2019-09-20 15:53:40 +0100154 // Reset ANDROID_ART_ROOT, as other things may depend on it.
155 ASSERT_EQ(0, setenv("ANDROID_ART_ROOT", android_art_root_env.c_str(), /* overwrite */ 1));
Roland Levillain1ea8a622019-03-29 19:08:56 +0000156}
157
Vladimir Marko62c2d712018-03-09 12:54:05 +0000158TEST_F(FileUtilsTest, ReplaceFileExtension) {
159 EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file.oat", "vdex"));
160 EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file.oat", "vdex"));
161 EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file", "vdex"));
162 EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file", "vdex"));
163}
164
David Sehrb2ec9f52018-02-21 13:20:31 -0800165} // namespace art