xref: /aosp_15_r20/system/libvintf/xsd/halManifest/vts/ValidateHalManifest.cpp (revision 70a7ec852fcefd15a4fb57f8f183a8b1c3aacb08)
1*70a7ec85SAndroid Build Coastguard Worker /*
2*70a7ec85SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*70a7ec85SAndroid Build Coastguard Worker  *
4*70a7ec85SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*70a7ec85SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*70a7ec85SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*70a7ec85SAndroid Build Coastguard Worker  *
8*70a7ec85SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*70a7ec85SAndroid Build Coastguard Worker  *
10*70a7ec85SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*70a7ec85SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*70a7ec85SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*70a7ec85SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*70a7ec85SAndroid Build Coastguard Worker  * limitations under the License.
15*70a7ec85SAndroid Build Coastguard Worker  */
16*70a7ec85SAndroid Build Coastguard Worker 
17*70a7ec85SAndroid Build Coastguard Worker #include <dirent.h>
18*70a7ec85SAndroid Build Coastguard Worker #include <glob.h>
19*70a7ec85SAndroid Build Coastguard Worker #include <string>
20*70a7ec85SAndroid Build Coastguard Worker 
21*70a7ec85SAndroid Build Coastguard Worker #include <android-base/properties.h>
22*70a7ec85SAndroid Build Coastguard Worker #include <android-base/scopeguard.h>
23*70a7ec85SAndroid Build Coastguard Worker #include <android-base/strings.h>
24*70a7ec85SAndroid Build Coastguard Worker #include "utility/ValidateXml.h"
25*70a7ec85SAndroid Build Coastguard Worker 
26*70a7ec85SAndroid Build Coastguard Worker using std::string_literals::operator""s;
27*70a7ec85SAndroid Build Coastguard Worker 
get_files_in_dirs(const char * dir_path,std::vector<std::string> & files)28*70a7ec85SAndroid Build Coastguard Worker static void get_files_in_dirs(const char* dir_path, std::vector<std::string>& files) {
29*70a7ec85SAndroid Build Coastguard Worker     DIR* d;
30*70a7ec85SAndroid Build Coastguard Worker     struct dirent* de;
31*70a7ec85SAndroid Build Coastguard Worker 
32*70a7ec85SAndroid Build Coastguard Worker     d = opendir(dir_path);
33*70a7ec85SAndroid Build Coastguard Worker     if (d == nullptr) {
34*70a7ec85SAndroid Build Coastguard Worker         return;
35*70a7ec85SAndroid Build Coastguard Worker     }
36*70a7ec85SAndroid Build Coastguard Worker 
37*70a7ec85SAndroid Build Coastguard Worker     while ((de = readdir(d))) {
38*70a7ec85SAndroid Build Coastguard Worker         if (de->d_type != DT_REG) {
39*70a7ec85SAndroid Build Coastguard Worker             continue;
40*70a7ec85SAndroid Build Coastguard Worker         }
41*70a7ec85SAndroid Build Coastguard Worker         files.push_back(de->d_name);
42*70a7ec85SAndroid Build Coastguard Worker     }
43*70a7ec85SAndroid Build Coastguard Worker     closedir(d);
44*70a7ec85SAndroid Build Coastguard Worker }
45*70a7ec85SAndroid Build Coastguard Worker 
glob(const std::string & pattern)46*70a7ec85SAndroid Build Coastguard Worker static std::vector<std::string> glob(const std::string& pattern) {
47*70a7ec85SAndroid Build Coastguard Worker     glob_t glob_result;
48*70a7ec85SAndroid Build Coastguard Worker     auto ret = glob(pattern.c_str(), GLOB_MARK, nullptr, &glob_result);
49*70a7ec85SAndroid Build Coastguard Worker     auto guard = android::base::make_scope_guard([&glob_result] { globfree(&glob_result); });
50*70a7ec85SAndroid Build Coastguard Worker 
51*70a7ec85SAndroid Build Coastguard Worker     std::vector<std::string> files;
52*70a7ec85SAndroid Build Coastguard Worker     if (ret == 0) {
53*70a7ec85SAndroid Build Coastguard Worker         for (size_t i = 0; i < glob_result.gl_pathc; i++) {
54*70a7ec85SAndroid Build Coastguard Worker             files.emplace_back(glob_result.gl_pathv[i]);
55*70a7ec85SAndroid Build Coastguard Worker         }
56*70a7ec85SAndroid Build Coastguard Worker     }
57*70a7ec85SAndroid Build Coastguard Worker     return files;
58*70a7ec85SAndroid Build Coastguard Worker }
59*70a7ec85SAndroid Build Coastguard Worker 
TEST(CheckConfig,halManifestValidation)60*70a7ec85SAndroid Build Coastguard Worker TEST(CheckConfig, halManifestValidation) {
61*70a7ec85SAndroid Build Coastguard Worker     if (android::base::GetIntProperty("ro.product.first_api_level", INT64_MAX) <= 28) {
62*70a7ec85SAndroid Build Coastguard Worker         GTEST_SKIP();
63*70a7ec85SAndroid Build Coastguard Worker     }
64*70a7ec85SAndroid Build Coastguard Worker 
65*70a7ec85SAndroid Build Coastguard Worker     RecordProperty("description",
66*70a7ec85SAndroid Build Coastguard Worker                    "Verify that the hal manifest file "
67*70a7ec85SAndroid Build Coastguard Worker                    "is valid according to the schema");
68*70a7ec85SAndroid Build Coastguard Worker 
69*70a7ec85SAndroid Build Coastguard Worker     constexpr const char* xsd = "/data/local/tmp/hal_manifest.xsd";
70*70a7ec85SAndroid Build Coastguard Worker 
71*70a7ec85SAndroid Build Coastguard Worker     // There may be compatibility matrices in .../etc/vintf. Manifests are only loaded from
72*70a7ec85SAndroid Build Coastguard Worker     // manifest.xml and manifest_*.xml, so only check those.
73*70a7ec85SAndroid Build Coastguard Worker     std::vector<const char*> vintf_locations = {"/vendor/etc/vintf", "/odm/etc/vintf"};
74*70a7ec85SAndroid Build Coastguard Worker     for (const char* dir_path : vintf_locations) {
75*70a7ec85SAndroid Build Coastguard Worker         std::vector<std::string> files;
76*70a7ec85SAndroid Build Coastguard Worker         get_files_in_dirs(dir_path, files);
77*70a7ec85SAndroid Build Coastguard Worker         for (std::string file_name : files) {
78*70a7ec85SAndroid Build Coastguard Worker             if (android::base::StartsWith(file_name, "manifest")) {
79*70a7ec85SAndroid Build Coastguard Worker                 EXPECT_VALID_XML((dir_path + "/"s + file_name).c_str(), xsd);
80*70a7ec85SAndroid Build Coastguard Worker             }
81*70a7ec85SAndroid Build Coastguard Worker         }
82*70a7ec85SAndroid Build Coastguard Worker     }
83*70a7ec85SAndroid Build Coastguard Worker 
84*70a7ec85SAndroid Build Coastguard Worker     // .../etc/vintf/manifest should only contain manifest fragments, so all of them must match the
85*70a7ec85SAndroid Build Coastguard Worker     // schema.
86*70a7ec85SAndroid Build Coastguard Worker     std::vector<const char*> fragment_locations = {"/vendor/etc/vintf/manifest",
87*70a7ec85SAndroid Build Coastguard Worker                                                    "/odm/etc/vintf/manifest"};
88*70a7ec85SAndroid Build Coastguard Worker     for (const char* dir_path : fragment_locations) {
89*70a7ec85SAndroid Build Coastguard Worker         std::vector<std::string> files;
90*70a7ec85SAndroid Build Coastguard Worker         get_files_in_dirs(dir_path, files);
91*70a7ec85SAndroid Build Coastguard Worker         for (std::string file_name : files) {
92*70a7ec85SAndroid Build Coastguard Worker             EXPECT_VALID_XML((dir_path + "/"s + file_name).c_str(), xsd);
93*70a7ec85SAndroid Build Coastguard Worker         }
94*70a7ec85SAndroid Build Coastguard Worker     }
95*70a7ec85SAndroid Build Coastguard Worker 
96*70a7ec85SAndroid Build Coastguard Worker     // APEXes contain fragments as well.
97*70a7ec85SAndroid Build Coastguard Worker     auto fragments = glob("/apex/*/etc/vintf/*.xml");
98*70a7ec85SAndroid Build Coastguard Worker     for (const auto& fragment : fragments) {
99*70a7ec85SAndroid Build Coastguard Worker         // Skip /apex/name@version paths to avoid double processing
100*70a7ec85SAndroid Build Coastguard Worker         auto parts = android::base::Split(fragment, "/");
101*70a7ec85SAndroid Build Coastguard Worker         if (parts.size() < 3 || parts[2].find('@') != std::string::npos) continue;
102*70a7ec85SAndroid Build Coastguard Worker         EXPECT_VALID_XML(fragment.c_str(), xsd);
103*70a7ec85SAndroid Build Coastguard Worker     }
104*70a7ec85SAndroid Build Coastguard Worker }
105