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 
TEST_F(FileUtilsTest,GetAndroidRootSafe)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 
66   // CommonArtTest sets ANDROID_ROOT, so expect this to be the same.
67   std::string android_root = GetAndroidRootSafe(&error_msg);
68   std::string android_root_env = getenv("ANDROID_ROOT");
69   EXPECT_EQ(android_root, android_root_env) << error_msg;
70 
71   // Set ANDROID_ROOT to something else (but the directory must exist). So use dirname.
72   UniqueCPtr<char> root_dup(strdup(android_root_env.c_str()));
73   char* dir = dirname(root_dup.get());
74   ASSERT_EQ(0, setenv("ANDROID_ROOT", dir, /* overwrite */ 1));
75   std::string android_root2 = GetAndroidRootSafe(&error_msg);
76   EXPECT_STREQ(dir, android_root2.c_str()) << error_msg;
77 
78   // Set a bogus value for ANDROID_ROOT. This should be an error.
79   ASSERT_EQ(0, setenv("ANDROID_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1));
80   EXPECT_EQ(GetAndroidRootSafe(&error_msg), "");
81 
82   // Unset ANDROID_ROOT and see that it still returns something (as libart 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 libart that lives outside
87   // 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   // Reset ANDROID_ROOT, as other things may depend on it.
97   ASSERT_EQ(0, setenv("ANDROID_ROOT", android_root_env.c_str(), /* overwrite */ 1));
98 }
99 
TEST_F(FileUtilsTest,GetAndroidRuntimeRootSafe)100 TEST_F(FileUtilsTest, GetAndroidRuntimeRootSafe) {
101   std::string error_msg;
102 
103   // We don't expect null returns for most cases, so don't check and let std::string crash.
104 
105   // CommonArtTest sets ANDROID_RUNTIME_ROOT, so expect this to be the same.
106   std::string android_runtime_root = GetAndroidRuntimeRootSafe(&error_msg);
107   std::string android_runtime_root_env = getenv("ANDROID_RUNTIME_ROOT");
108   EXPECT_EQ(android_runtime_root, android_runtime_root_env) << error_msg;
109 
110   // Set ANDROID_RUNTIME_ROOT to something else (but the directory must exist). So use dirname.
111   UniqueCPtr<char> root_dup(strdup(android_runtime_root_env.c_str()));
112   char* dir = dirname(root_dup.get());
113   ASSERT_EQ(0, setenv("ANDROID_RUNTIME_ROOT", dir, /* overwrite */ 1));
114   std::string android_runtime_root2 = GetAndroidRuntimeRootSafe(&error_msg);
115   EXPECT_STREQ(dir, android_runtime_root2.c_str()) << error_msg;
116 
117   // Set a bogus value for ANDROID_RUNTIME_ROOT. This should be an error.
118   ASSERT_EQ(0, setenv("ANDROID_RUNTIME_ROOT", "/this/is/obviously/bogus", /* overwrite */ 1));
119   EXPECT_EQ(GetAndroidRuntimeRootSafe(&error_msg), "");
120 
121   // Reset ANDROID_RUNTIME_ROOT, as other things may depend on it.
122   ASSERT_EQ(0, setenv("ANDROID_RUNTIME_ROOT", android_runtime_root_env.c_str(), /* overwrite */ 1));
123 }
124 
TEST_F(FileUtilsTest,ReplaceFileExtension)125 TEST_F(FileUtilsTest, ReplaceFileExtension) {
126   EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file.oat", "vdex"));
127   EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file.oat", "vdex"));
128   EXPECT_EQ("/directory/file.vdex", ReplaceFileExtension("/directory/file", "vdex"));
129   EXPECT_EQ("/.directory/file.vdex", ReplaceFileExtension("/.directory/file", "vdex"));
130 }
131 
132 }  // namespace art
133