blob: 34a0097b03167d6ac2beafdf06a6cd38ebb9cd14 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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 <set>
19#include <string>
20
21#include "gmock/gmock.h"
22#include "gtest/gtest.h"
23
24#include "android-base/macros.h"
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010025#include "android-base/stringprintf.h"
26#include "private/android_filesystem_config.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020027
28#include "idmap2/FileUtils.h"
29
30#include "TestHelpers.h"
31
32using ::testing::NotNull;
33
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010034namespace android::idmap2::utils {
Mårten Kongstad02751232018-04-27 13:16:32 +020035
36TEST(FileUtilsTests, FindFilesFindEverythingNonRecursive) {
37 const auto& root = GetTestDataPath();
38 auto v = utils::FindFiles(root, false,
39 [](unsigned char type ATTRIBUTE_UNUSED,
40 const std::string& path ATTRIBUTE_UNUSED) -> bool { return true; });
41 ASSERT_THAT(v, NotNull());
Winsonb4100202019-02-06 12:05:32 -080042 ASSERT_EQ(v->size(), 7U);
Ryan Mitchell19823452019-01-29 12:01:24 -080043 ASSERT_EQ(std::set<std::string>(v->begin(), v->end()), std::set<std::string>({
44 root + "/.",
45 root + "/..",
46 root + "/overlay",
47 root + "/target",
Winsonb4100202019-02-06 12:05:32 -080048 root + "/signature-overlay",
Ryan Mitchell19823452019-01-29 12:01:24 -080049 root + "/system-overlay",
50 root + "/system-overlay-invalid",
51 }));
Mårten Kongstad02751232018-04-27 13:16:32 +020052}
53
54TEST(FileUtilsTests, FindFilesFindApkFilesRecursive) {
55 const auto& root = GetTestDataPath();
56 auto v = utils::FindFiles(root, true, [](unsigned char type, const std::string& path) -> bool {
Mårten Kongstadb8779022018-11-29 09:53:17 +010057 return type == DT_REG && path.size() > 4 && path.compare(path.size() - 4, 4, ".apk") == 0;
Mårten Kongstad02751232018-04-27 13:16:32 +020058 });
59 ASSERT_THAT(v, NotNull());
Winsonb4100202019-02-06 12:05:32 -080060 ASSERT_EQ(v->size(), 10U);
Mårten Kongstad49d835d2019-01-31 10:50:48 +010061 ASSERT_EQ(std::set<std::string>(v->begin(), v->end()),
62 std::set<std::string>(
63 {root + "/target/target.apk", root + "/target/target-no-overlayable.apk",
64 root + "/overlay/overlay.apk", root + "/overlay/overlay-no-name.apk",
65 root + "/overlay/overlay-no-name-static.apk",
66 root + "/overlay/overlay-static-1.apk", root + "/overlay/overlay-static-2.apk",
67 root + "/signature-overlay/signature-overlay.apk",
68 root + "/system-overlay/system-overlay.apk",
69 root + "/system-overlay-invalid/system-overlay-invalid.apk"}));
Mårten Kongstad02751232018-04-27 13:16:32 +020070}
71
72TEST(FileUtilsTests, ReadFile) {
73 int pipefd[2];
74 ASSERT_EQ(pipe(pipefd), 0);
75
76 ASSERT_EQ(write(pipefd[1], "foobar", 6), 6);
77 close(pipefd[1]);
78
79 auto data = ReadFile(pipefd[0]);
80 ASSERT_THAT(data, NotNull());
81 ASSERT_EQ(*data, "foobar");
82 close(pipefd[0]);
83}
84
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010085#ifdef __ANDROID__
86TEST(FileUtilsTests, UidHasWriteAccessToPath) {
87 constexpr const char* tmp_path = "/data/local/tmp/test@idmap";
88 const std::string cache_path(base::StringPrintf("%s/test@idmap", kIdmapCacheDir));
89 const std::string sneaky_cache_path(base::StringPrintf("/data/../%s/test@idmap", kIdmapCacheDir));
90
91 ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, tmp_path));
92 ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, cache_path));
93 ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, sneaky_cache_path));
94
95 ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, tmp_path));
96 ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, cache_path));
97 ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, sneaky_cache_path));
98
99 constexpr const uid_t AID_SOME_APP = AID_SYSTEM + 1;
100 ASSERT_TRUE(UidHasWriteAccessToPath(AID_SOME_APP, tmp_path));
101 ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, cache_path));
102 ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, sneaky_cache_path));
103}
104#endif
105
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100106} // namespace android::idmap2::utils