blob: 8af4037be954fa94cb025198eec7f381b47a68b2 [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>
Yi Kongf61e2162019-08-15 13:27:46 -070018#include <fcntl.h>
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070019
Mårten Kongstad02751232018-04-27 13:16:32 +020020#include <set>
21#include <string>
22
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070023#include "TestHelpers.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020024#include "android-base/macros.h"
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010025#include "android-base/stringprintf.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070026#include "gmock/gmock.h"
27#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020028#include "idmap2/FileUtils.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070029#include "private/android_filesystem_config.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020030
31using ::testing::NotNull;
32
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010033namespace android::idmap2::utils {
Mårten Kongstad02751232018-04-27 13:16:32 +020034
35TEST(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());
Winsonb4100202019-02-06 12:05:32 -080041 ASSERT_EQ(v->size(), 7U);
Ryan Mitchell19823452019-01-29 12:01:24 -080042 ASSERT_EQ(std::set<std::string>(v->begin(), v->end()), std::set<std::string>({
43 root + "/.",
44 root + "/..",
45 root + "/overlay",
46 root + "/target",
Winsonb4100202019-02-06 12:05:32 -080047 root + "/signature-overlay",
Ryan Mitchell19823452019-01-29 12:01:24 -080048 root + "/system-overlay",
49 root + "/system-overlay-invalid",
50 }));
Mårten Kongstad02751232018-04-27 13:16:32 +020051}
52
53TEST(FileUtilsTests, FindFilesFindApkFilesRecursive) {
54 const auto& root = GetTestDataPath();
55 auto v = utils::FindFiles(root, true, [](unsigned char type, const std::string& path) -> bool {
Mårten Kongstadb8779022018-11-29 09:53:17 +010056 return type == DT_REG && path.size() > 4 && path.compare(path.size() - 4, 4, ".apk") == 0;
Mårten Kongstad02751232018-04-27 13:16:32 +020057 });
58 ASSERT_THAT(v, NotNull());
Ryan Mitchell5035d662020-01-22 13:19:41 -080059 ASSERT_EQ(v->size(), 11U);
Mårten Kongstad49d835d2019-01-31 10:50:48 +010060 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",
Ryan Mitchell5035d662020-01-22 13:19:41 -080064 root + "/overlay/overlay-no-name-static.apk", root + "/overlay/overlay-shared.apk",
Mårten Kongstad49d835d2019-01-31 10:50:48 +010065 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"}));
Mårten Kongstad02751232018-04-27 13:16:32 +020069}
70
71TEST(FileUtilsTests, ReadFile) {
72 int pipefd[2];
Yi Kongf61e2162019-08-15 13:27:46 -070073 ASSERT_EQ(pipe2(pipefd, O_CLOEXEC), 0);
Mårten Kongstad02751232018-04-27 13:16:32 +020074
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
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010084#ifdef __ANDROID__
85TEST(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
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100105} // namespace android::idmap2::utils