xref: /aosp_15_r20/system/apex/tests/vts/VtsApexTest.cpp (revision 33f3758387333dbd2962d7edbd98681940d895da)
1 /*
2  * Copyright (C) 2024 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 #define LOG_TAG "VtsApexTest"
18 
19 #include <android-base/file.h>
20 #include <fcntl.h>
21 #include <gtest/gtest.h>
22 
23 #include <filesystem>
24 
25 #include "apex_constants.h"
26 
27 using android::base::unique_fd;
28 
29 namespace android::apex {
30 
ForEachPreinstalledApex(auto fn)31 static void ForEachPreinstalledApex(auto fn) {
32   namespace fs = std::filesystem;
33   std::error_code ec;
34   for (const auto &dir : kApexPackageBuiltinDirs) {
35     if (!fs::exists(dir, ec)) {
36       if (ec) {
37         FAIL() << "Can't to access " << dir << ": " << ec.message();
38       }
39       continue;
40     }
41     auto it = fs::directory_iterator(dir, ec);
42     auto end = fs::directory_iterator();
43     for (; !ec && it != end; it.increment(ec)) {
44       fs::path path = *it;
45       if (path.extension() != kApexPackageSuffix) {
46         continue;
47       }
48       fn(path);
49     }
50     if (ec) {
51       FAIL() << "Can't read " << dir << ": " << ec.message();
52     }
53   }
54 }
55 
56 // Preinstalled APEX files (.apex) should be okay when opening with O_DIRECT
TEST(VtsApexTest,OpenPreinstalledApex)57 TEST(VtsApexTest, OpenPreinstalledApex) {
58   ForEachPreinstalledApex([](auto path) {
59     unique_fd fd(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_DIRECT));
60     ASSERT_NE(fd.get(), -1)
61         << "Can't open an APEX file " << path << ": " << strerror(errno);
62   });
63 }
64 
65 }  // namespace android::apex
66