David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 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" |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 23 | #include "common_art_test.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 27 | class FileUtilsTest : public CommonArtTest {}; |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 28 | |
| 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 | |
| 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 | |
| 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 | TEST_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 Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 66 | // CommonArtTest sets ANDROID_ROOT, so expect this to be the same. |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 67 | std::string android_root = GetAndroidRootSafe(&error_msg); |
| 68 | std::string android_root_env = getenv("ANDROID_ROOT"); |
Roland Levillain | 1ea8a62 | 2019-03-29 19:08:56 +0000 | [diff] [blame] | 69 | EXPECT_EQ(android_root, android_root_env) << error_msg; |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 70 | |
| 71 | // Set ANDROID_ROOT to something else (but the directory must exist). So use dirname. |
Andreas Gampe | dbf5403 | 2018-06-18 14:47:01 -0700 | [diff] [blame] | 72 | UniqueCPtr<char> root_dup(strdup(android_root_env.c_str())); |
| 73 | char* dir = dirname(root_dup.get()); |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 74 | ASSERT_EQ(0, setenv("ANDROID_ROOT", dir, /* overwrite */ 1)); |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 75 | std::string android_root2 = GetAndroidRootSafe(&error_msg); |
Roland Levillain | 1ea8a62 | 2019-03-29 19:08:56 +0000 | [diff] [blame] | 76 | EXPECT_STREQ(dir, android_root2.c_str()) << error_msg; |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 77 | |
| 78 | // Set a bogus value for ANDROID_ROOT. This should be an error. |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 79 | ASSERT_EQ(0, setenv("ANDROID_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1)); |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 80 | EXPECT_EQ(GetAndroidRootSafe(&error_msg), ""); |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 81 | |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 82 | // 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 Light | 680cbf2 | 2018-10-31 11:00:19 -0700 | [diff] [blame] | 91 | #if !defined(__BIONIC__ ) || defined(__ANDROID__) |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 92 | UniqueCPtr<char> real_root(realpath(android_root.c_str(), nullptr)); |
| 93 | EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg; |
Alex Light | 680cbf2 | 2018-10-31 11:00:19 -0700 | [diff] [blame] | 94 | #else |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 95 | EXPECT_STRNE(real_root3.get(), "") << error_msg; |
Alex Light | 680cbf2 | 2018-10-31 11:00:19 -0700 | [diff] [blame] | 96 | #endif |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 97 | } |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 98 | |
| 99 | // Reset ANDROID_ROOT, as other things may depend on it. |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 100 | ASSERT_EQ(0, setenv("ANDROID_ROOT", android_root_env.c_str(), /* overwrite */ 1)); |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Roland Levillain | 1ea8a62 | 2019-03-29 19:08:56 +0000 | [diff] [blame] | 103 | TEST_F(FileUtilsTest, GetAndroidRuntimeRootSafe) { |
| 104 | std::string error_msg; |
| 105 | |
| 106 | // We don't expect null returns for most cases, so don't check and let std::string crash. |
| 107 | |
| 108 | // CommonArtTest sets ANDROID_RUNTIME_ROOT, so expect this to be the same. |
| 109 | std::string android_runtime_root = GetAndroidRuntimeRootSafe(&error_msg); |
| 110 | std::string android_runtime_root_env = getenv("ANDROID_RUNTIME_ROOT"); |
| 111 | EXPECT_EQ(android_runtime_root, android_runtime_root_env) << error_msg; |
| 112 | |
| 113 | // Set ANDROID_RUNTIME_ROOT to something else (but the directory must exist). So use dirname. |
| 114 | UniqueCPtr<char> root_dup(strdup(android_runtime_root_env.c_str())); |
| 115 | char* dir = dirname(root_dup.get()); |
| 116 | ASSERT_EQ(0, setenv("ANDROID_RUNTIME_ROOT", dir, /* overwrite */ 1)); |
| 117 | std::string android_runtime_root2 = GetAndroidRuntimeRootSafe(&error_msg); |
| 118 | EXPECT_STREQ(dir, android_runtime_root2.c_str()) << error_msg; |
| 119 | |
| 120 | // Set a bogus value for ANDROID_RUNTIME_ROOT. This should be an error. |
| 121 | ASSERT_EQ(0, setenv("ANDROID_RUNTIME_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1)); |
| 122 | EXPECT_EQ(GetAndroidRuntimeRootSafe(&error_msg), ""); |
| 123 | |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 124 | // Inferring the Android Runtime Root from the location of libartbase only works on target. |
| 125 | if (kIsTargetBuild) { |
| 126 | // Disabled for now, as we cannot reliably use `GetRootContainingLibartbase` |
| 127 | // to find the Android Runtime Root on target yet (see comment in |
| 128 | // `GetAndroidRuntimeRootSafe`). |
| 129 | // |
| 130 | // TODO(b/129534335): Re-enable this part of the test on target when the |
| 131 | // only instance of libartbase is the one from the Runtime APEX. |
| 132 | if ((false)) { |
| 133 | // Unset ANDROID_RUNTIME_ROOT and see that it still returns something (as |
| 134 | // libartbase code is running). |
| 135 | ASSERT_EQ(0, unsetenv("ANDROID_RUNTIME_ROOT")); |
| 136 | std::string android_runtime_root3 = GetAndroidRuntimeRootSafe(&error_msg); |
| 137 | // This should be the same as the other root (modulo realpath), otherwise |
| 138 | // the test setup is broken. On non-bionic. On bionic we can be running |
| 139 | // with a different libartbase that lives outside of ANDROID_RUNTIME_ROOT. |
| 140 | UniqueCPtr<char> real_root3(realpath(android_runtime_root3.c_str(), nullptr)); |
| 141 | #if !defined(__BIONIC__ ) || defined(__ANDROID__) |
| 142 | UniqueCPtr<char> real_root(realpath(android_runtime_root.c_str(), nullptr)); |
| 143 | EXPECT_STREQ(real_root.get(), real_root3.get()) << error_msg; |
| 144 | #else |
| 145 | EXPECT_STRNE(real_root3.get(), "") << error_msg; |
| 146 | #endif |
| 147 | } |
| 148 | } |
| 149 | |
Roland Levillain | 1ea8a62 | 2019-03-29 19:08:56 +0000 | [diff] [blame] | 150 | // Reset ANDROID_RUNTIME_ROOT, as other things may depend on it. |
| 151 | ASSERT_EQ(0, setenv("ANDROID_RUNTIME_ROOT", android_runtime_root_env.c_str(), /* overwrite */ 1)); |
| 152 | } |
| 153 | |
Vladimir Marko | 62c2d71 | 2018-03-09 12:54:05 +0000 | [diff] [blame] | 154 | TEST_F(FileUtilsTest, ReplaceFileExtension) { |
| 155 | EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file.oat", "vdex")); |
| 156 | EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file.oat", "vdex")); |
| 157 | EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file", "vdex")); |
| 158 | EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file", "vdex")); |
| 159 | } |
| 160 | |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 161 | } // namespace art |