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" |
| 25 | |
| 26 | #include "idmap2/FileUtils.h" |
| 27 | |
| 28 | #include "TestHelpers.h" |
| 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()); |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 40 | ASSERT_EQ(v->size(), 6U); |
| 41 | ASSERT_EQ(std::set<std::string>(v->begin(), v->end()), |
| 42 | std::set<std::string>({root + "/.", root + "/..", root + "/overlay", root + "/target", |
| 43 | root + "/system-overlay", root + "/system-overlay-invalid"})); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | TEST(FileUtilsTests, FindFilesFindApkFilesRecursive) { |
| 47 | const auto& root = GetTestDataPath(); |
| 48 | 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] | 49 | 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] | 50 | }); |
| 51 | ASSERT_THAT(v, NotNull()); |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 52 | ASSERT_EQ(v->size(), 6U); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 53 | ASSERT_EQ(std::set<std::string>(v->begin(), v->end()), |
| 54 | std::set<std::string>({root + "/target/target.apk", root + "/overlay/overlay.apk", |
| 55 | root + "/overlay/overlay-static-1.apk", |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame^] | 56 | root + "/overlay/overlay-static-2.apk", |
| 57 | root + "/system-overlay/system-overlay.apk", |
| 58 | root + "/system-overlay-invalid/system-overlay-invalid.apk"})); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST(FileUtilsTests, ReadFile) { |
| 62 | int pipefd[2]; |
| 63 | ASSERT_EQ(pipe(pipefd), 0); |
| 64 | |
| 65 | ASSERT_EQ(write(pipefd[1], "foobar", 6), 6); |
| 66 | close(pipefd[1]); |
| 67 | |
| 68 | auto data = ReadFile(pipefd[0]); |
| 69 | ASSERT_THAT(data, NotNull()); |
| 70 | ASSERT_EQ(*data, "foobar"); |
| 71 | close(pipefd[0]); |
| 72 | } |
| 73 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 74 | } // namespace android::idmap2::utils |