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 <dirent.h>
18 #include <string>
19 
20 #include <android-base/properties.h>
21 #include <android-base/strings.h>
22 #include "utility/ValidateXml.h"
23 
24 using std::string_literals::operator""s;
25 
get_files_in_dirs(const char * dir_path,std::vector<std::string> & files)26 static void get_files_in_dirs(const char* dir_path, std::vector<std::string>& files) {
27     DIR* d;
28     struct dirent* de;
29 
30     d = opendir(dir_path);
31     if (d == nullptr) {
32         return;
33     }
34 
35     while ((de = readdir(d))) {
36         if (de->d_type != DT_REG) {
37             continue;
38         }
39         files.push_back(de->d_name);
40     }
41     closedir(d);
42 }
43 
TEST(CheckConfig,halManifestValidation)44 TEST(CheckConfig, halManifestValidation) {
45     if (android::base::GetIntProperty("ro.product.first_api_level", INT64_MAX) <= 28) {
46         GTEST_SKIP();
47     }
48 
49     RecordProperty("description",
50                    "Verify that the hal manifest file "
51                    "is valid according to the schema");
52 
53     constexpr const char* xsd = "/data/local/tmp/hal_manifest.xsd";
54 
55     // There may be compatibility matrices in .../etc/vintf. Manifests are only loaded from
56     // manifest.xml and manifest_*.xml, so only check those.
57     std::vector<const char*> vintf_locations = {"/vendor/etc/vintf", "/odm/etc/vintf"};
58     for (const char* dir_path : vintf_locations) {
59         std::vector<std::string> files;
60         get_files_in_dirs(dir_path, files);
61         for (std::string file_name : files) {
62             if (android::base::StartsWith(file_name, "manifest")) {
63                 EXPECT_VALID_XML((dir_path + "/"s + file_name).c_str(), xsd);
64             }
65         }
66     }
67 
68     // .../etc/vintf/manifest should only contain manifest fragments, so all of them must match the
69     // schema.
70     std::vector<const char*> fragment_locations = {"/vendor/etc/vintf/manifest",
71                                                    "/odm/etc/vintf/manifest"};
72     for (const char* dir_path : fragment_locations) {
73         std::vector<std::string> files;
74         get_files_in_dirs(dir_path, files);
75         for (std::string file_name : files) {
76             EXPECT_VALID_XML((dir_path + "/"s + file_name).c_str(), xsd);
77         }
78     }
79 }
80