Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 <set> |
| 19 | #include <string> |
| 20 | |
| 21 | #include "gmock/gmock.h" |
| 22 | #include "gtest/gtest.h" |
| 23 | |
| 24 | #include "android-base/macros.h" |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
| 26 | #include "private/android_filesystem_config.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 27 | |
| 28 | #include "idmap2/FileUtils.h" |
| 29 | |
| 30 | #include "TestHelpers.h" |
| 31 | |
| 32 | using ::testing::NotNull; |
| 33 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 34 | namespace android::idmap2::utils { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 35 | |
| 36 | TEST(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()); |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 42 | ASSERT_EQ(v->size(), 6U); |
| 43 | ASSERT_EQ(std::set<std::string>(v->begin(), v->end()), |
| 44 | std::set<std::string>({root + "/.", root + "/..", root + "/overlay", root + "/target", |
| 45 | root + "/system-overlay", root + "/system-overlay-invalid"})); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | TEST(FileUtilsTests, FindFilesFindApkFilesRecursive) { |
| 49 | const auto& root = GetTestDataPath(); |
| 50 | auto v = utils::FindFiles(root, true, [](unsigned char type, const std::string& path) -> bool { |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 51 | return type == DT_REG && path.size() > 4 && path.compare(path.size() - 4, 4, ".apk") == 0; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 52 | }); |
| 53 | ASSERT_THAT(v, NotNull()); |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 54 | ASSERT_EQ(v->size(), 6U); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 55 | ASSERT_EQ(std::set<std::string>(v->begin(), v->end()), |
| 56 | std::set<std::string>({root + "/target/target.apk", root + "/overlay/overlay.apk", |
| 57 | root + "/overlay/overlay-static-1.apk", |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 58 | root + "/overlay/overlay-static-2.apk", |
| 59 | root + "/system-overlay/system-overlay.apk", |
| 60 | root + "/system-overlay-invalid/system-overlay-invalid.apk"})); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TEST(FileUtilsTests, ReadFile) { |
| 64 | int pipefd[2]; |
| 65 | ASSERT_EQ(pipe(pipefd), 0); |
| 66 | |
| 67 | ASSERT_EQ(write(pipefd[1], "foobar", 6), 6); |
| 68 | close(pipefd[1]); |
| 69 | |
| 70 | auto data = ReadFile(pipefd[0]); |
| 71 | ASSERT_THAT(data, NotNull()); |
| 72 | ASSERT_EQ(*data, "foobar"); |
| 73 | close(pipefd[0]); |
| 74 | } |
| 75 | |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 76 | #ifdef __ANDROID__ |
| 77 | TEST(FileUtilsTests, UidHasWriteAccessToPath) { |
| 78 | constexpr const char* tmp_path = "/data/local/tmp/test@idmap"; |
| 79 | const std::string cache_path(base::StringPrintf("%s/test@idmap", kIdmapCacheDir)); |
| 80 | const std::string sneaky_cache_path(base::StringPrintf("/data/../%s/test@idmap", kIdmapCacheDir)); |
| 81 | |
| 82 | ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, tmp_path)); |
| 83 | ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, cache_path)); |
| 84 | ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, sneaky_cache_path)); |
| 85 | |
| 86 | ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, tmp_path)); |
| 87 | ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, cache_path)); |
| 88 | ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, sneaky_cache_path)); |
| 89 | |
| 90 | constexpr const uid_t AID_SOME_APP = AID_SYSTEM + 1; |
| 91 | ASSERT_TRUE(UidHasWriteAccessToPath(AID_SOME_APP, tmp_path)); |
| 92 | ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, cache_path)); |
| 93 | ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, sneaky_cache_path)); |
| 94 | } |
| 95 | #endif |
| 96 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 97 | } // namespace android::idmap2::utils |