1 /*
2 * Copyright (C) 2019 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 <android-base/file.h>
18 #include <android-base/logging.h>
19 #include <android-base/strings.h>
20
21 #include <gtest/gtest.h>
22 #include <filesystem>
23 #include <memory>
24
25 #include <ApexProperties.sysprop.h>
26 #include "apex_constants.h"
27 #include "apex_shim.h"
28 #include "apexd_utils.h"
29 #include "string_log.h"
30
31 namespace android {
32 namespace apex {
33
TEST(FlattenedApexTest,SysPropIsFalse)34 TEST(FlattenedApexTest, SysPropIsFalse) {
35 bool value = android::sysprop::ApexProperties::updatable().value_or(false);
36 ASSERT_FALSE(value);
37 }
38
TEST(FlattenedApexTest,ApexFilesAreFlattened)39 TEST(FlattenedApexTest, ApexFilesAreFlattened) {
40 namespace fs = std::filesystem;
41 auto assert_is_dir = [&](const fs::directory_entry& entry) {
42 if (entry.path().filename() == shim::kSystemShimApexName) {
43 return;
44 }
45 std::error_code ec;
46 bool is_dir = entry.is_directory(ec);
47 ASSERT_FALSE(ec) << ec.message();
48 ASSERT_TRUE(is_dir) << entry.path() << " is not a directory";
49 };
50 WalkDir(kApexPackageSystemDir, assert_is_dir);
51 }
52
TEST(FlattenedApexTest,MountsAreCorrect)53 TEST(FlattenedApexTest, MountsAreCorrect) {
54 using android::base::ReadFileToString;
55 using android::base::Split;
56 std::string mounts;
57 ASSERT_TRUE(ReadFileToString("/proc/self/mountinfo", &mounts));
58 bool has_apex_mount = false;
59 for (const auto& mount : Split(mounts, "\n")) {
60 const std::vector<std::string>& tokens = Split(mount, " ");
61 // line format:
62 // mnt_id parent_mnt_id major:minor source target option propagation_type
63 if (tokens.size() < 7) {
64 continue;
65 }
66 const std::string& source = tokens[3];
67 const std::string& target = tokens[4];
68 if (source == kApexPackageSystemDir && target == "/apex") {
69 has_apex_mount = true;
70 }
71 }
72 ASSERT_TRUE(has_apex_mount) << "Failed to find apex mount point";
73 }
74
TEST(FlattenedApexTest,ApexdIsNotRunning)75 TEST(FlattenedApexTest, ApexdIsNotRunning) {
76 constexpr const char* kCmd = "pidof -s apexd";
77 std::unique_ptr<FILE, int (*)(FILE*)> pipe(popen(kCmd, "r"), pclose);
78 ASSERT_NE(nullptr, pipe) << "Failed to open pipe for: " << kCmd;
79 char buf[1024];
80 if (fgets(buf, 1024, pipe.get()) != nullptr) {
81 FAIL() << "apexd is running and has pid " << buf;
82 }
83 }
84
85 } // namespace apex
86 } // namespace android
87
main(int argc,char ** argv)88 int main(int argc, char** argv) {
89 ::testing::InitGoogleTest(&argc, argv);
90 android::base::InitLogging(argv, &android::base::StderrLogger);
91 return RUN_ALL_TESTS();
92 }
93