1 /*
2  * Copyright (C) 2018 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 <dirent.h>
18 #include <fcntl.h>
19 
20 #include <set>
21 #include <string>
22 
23 #include "TestHelpers.h"
24 #include "android-base/macros.h"
25 #include "android-base/stringprintf.h"
26 #include "gmock/gmock.h"
27 #include "gtest/gtest.h"
28 #include "idmap2/FileUtils.h"
29 #include "private/android_filesystem_config.h"
30 
31 using ::testing::NotNull;
32 
33 namespace android::idmap2::utils {
34 
TEST(FileUtilsTests,FindFilesFindEverythingNonRecursive)35 TEST(FileUtilsTests, FindFilesFindEverythingNonRecursive) {
36   const auto& root = GetTestDataPath();
37   auto v = utils::FindFiles(root, false,
38                             [](unsigned char type ATTRIBUTE_UNUSED,
39                                const std::string& path ATTRIBUTE_UNUSED) -> bool { return true; });
40   ASSERT_THAT(v, NotNull());
41   ASSERT_EQ(v->size(), 7U);
42   ASSERT_EQ(std::set<std::string>(v->begin(), v->end()), std::set<std::string>({
43                                                              root + "/.",
44                                                              root + "/..",
45                                                              root + "/overlay",
46                                                              root + "/target",
47                                                              root + "/signature-overlay",
48                                                              root + "/system-overlay",
49                                                              root + "/system-overlay-invalid",
50                                                          }));
51 }
52 
TEST(FileUtilsTests,FindFilesFindApkFilesRecursive)53 TEST(FileUtilsTests, FindFilesFindApkFilesRecursive) {
54   const auto& root = GetTestDataPath();
55   auto v = utils::FindFiles(root, true, [](unsigned char type, const std::string& path) -> bool {
56     return type == DT_REG && path.size() > 4 && path.compare(path.size() - 4, 4, ".apk") == 0;
57   });
58   ASSERT_THAT(v, NotNull());
59   ASSERT_EQ(v->size(), 10U);
60   ASSERT_EQ(std::set<std::string>(v->begin(), v->end()),
61             std::set<std::string>(
62                 {root + "/target/target.apk", root + "/target/target-no-overlayable.apk",
63                  root + "/overlay/overlay.apk", root + "/overlay/overlay-no-name.apk",
64                  root + "/overlay/overlay-no-name-static.apk",
65                  root + "/overlay/overlay-static-1.apk", root + "/overlay/overlay-static-2.apk",
66                  root + "/signature-overlay/signature-overlay.apk",
67                  root + "/system-overlay/system-overlay.apk",
68                  root + "/system-overlay-invalid/system-overlay-invalid.apk"}));
69 }
70 
TEST(FileUtilsTests,ReadFile)71 TEST(FileUtilsTests, ReadFile) {
72   int pipefd[2];
73   ASSERT_EQ(pipe2(pipefd, O_CLOEXEC), 0);
74 
75   ASSERT_EQ(write(pipefd[1], "foobar", 6), 6);
76   close(pipefd[1]);
77 
78   auto data = ReadFile(pipefd[0]);
79   ASSERT_THAT(data, NotNull());
80   ASSERT_EQ(*data, "foobar");
81   close(pipefd[0]);
82 }
83 
84 #ifdef __ANDROID__
TEST(FileUtilsTests,UidHasWriteAccessToPath)85 TEST(FileUtilsTests, UidHasWriteAccessToPath) {
86   constexpr const char* tmp_path = "/data/local/tmp/test@idmap";
87   const std::string cache_path(base::StringPrintf("%s/test@idmap", kIdmapCacheDir));
88   const std::string sneaky_cache_path(base::StringPrintf("/data/../%s/test@idmap", kIdmapCacheDir));
89 
90   ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, tmp_path));
91   ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, cache_path));
92   ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, sneaky_cache_path));
93 
94   ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, tmp_path));
95   ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, cache_path));
96   ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, sneaky_cache_path));
97 
98   constexpr const uid_t AID_SOME_APP = AID_SYSTEM + 1;
99   ASSERT_TRUE(UidHasWriteAccessToPath(AID_SOME_APP, tmp_path));
100   ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, cache_path));
101   ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, sneaky_cache_path));
102 }
103 #endif
104 
105 }  // namespace android::idmap2::utils
106