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