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 #define LOG_TAG "ValidateAudioConfig"
18 #include <utils/Log.h>
19 
20 #include <numeric>
21 
22 #define LIBXML_SCHEMAS_ENABLED
23 #include <libxml/xmlschemastypes.h>
24 #define LIBXML_XINCLUDE_ENABLED
25 #include <libxml/xinclude.h>
26 
27 #include <memory>
28 #include <string>
29 
30 #include "ValidateXml.h"
31 
32 namespace android {
33 namespace hardware {
34 namespace audio {
35 namespace common {
36 namespace test {
37 namespace utility {
38 
39 /** Map libxml2 structures to their corresponding deleters. */
40 template <class T>
41 constexpr void (*xmlDeleter)(T* t);
42 template <>
43 constexpr auto xmlDeleter<xmlSchema> = xmlSchemaFree;
44 template <>
45 constexpr auto xmlDeleter<xmlDoc> = xmlFreeDoc;
46 template <>
47 constexpr auto xmlDeleter<xmlSchemaParserCtxt> = xmlSchemaFreeParserCtxt;
48 template <>
49 constexpr auto xmlDeleter<xmlSchemaValidCtxt> = xmlSchemaFreeValidCtxt;
50 
51 /** @return a unique_ptr with the correct deleter for the libxml2 object. */
52 template <class T>
make_xmlUnique(T * t)53 constexpr auto make_xmlUnique(T* t) {
54     // Wrap deleter in lambda to enable empty base optimization
55     auto deleter = [](T* t) { xmlDeleter<T>(t); };
56     return std::unique_ptr<T, decltype(deleter)>{t, deleter};
57 }
58 
59 /** Class that handles libxml2 initialization and cleanup. NOT THREAD SAFE*/
60 struct Libxml2Global {
Libxml2Globalandroid::hardware::audio::common::test::utility::Libxml2Global61     Libxml2Global() {
62         xmlLineNumbersDefault(1);  // Better error message
63         xmlSetGenericErrorFunc(this, errorCb);
64     }
~Libxml2Globalandroid::hardware::audio::common::test::utility::Libxml2Global65     ~Libxml2Global() {
66         // TODO: check if all those cleanup are needed
67         xmlSetGenericErrorFunc(nullptr, nullptr);
68         xmlSchemaCleanupTypes();
69         xmlCleanupParser();
70         xmlCleanupThreads();
71     }
72 
getErrorsandroid::hardware::audio::common::test::utility::Libxml2Global73     const std::string& getErrors() { return errors; }
74 
75    private:
errorCbandroid::hardware::audio::common::test::utility::Libxml2Global76     static void errorCb(void* ctxt, const char* msg, ...) {
77         auto* self = static_cast<Libxml2Global*>(ctxt);
78         va_list args;
79         va_start(args, msg);
80 
81         char* formatedMsg;
82         if (vasprintf(&formatedMsg, msg, args) >= 0) {
83             LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", formatedMsg);
84             self->errors += "Error: ";
85             self->errors += formatedMsg;
86         }
87         free(formatedMsg);
88 
89         va_end(args);
90     }
91     std::string errors;
92 };
93 
validateXml(const char * xmlFilePathExpr,const char * xsdFilePathExpr,const char * xmlFilePath,const char * xsdFilePath)94 ::testing::AssertionResult validateXml(const char* xmlFilePathExpr, const char* xsdFilePathExpr,
95                                        const char* xmlFilePath, const char* xsdFilePath) {
96     Libxml2Global libxml2;
97 
98     auto context = [&]() {
99         return std::string() + "  While validating: " + xmlFilePathExpr +
100                "\n          Which is: " + xmlFilePath + "\nAgainst the schema: " + xsdFilePathExpr +
101                "\n          Which is: " + xsdFilePath + "\nLibxml2 errors:\n" + libxml2.getErrors();
102     };
103 
104     auto schemaParserCtxt = make_xmlUnique(xmlSchemaNewParserCtxt(xsdFilePath));
105     auto schema = make_xmlUnique(xmlSchemaParse(schemaParserCtxt.get()));
106     if (schema == nullptr) {
107         return ::testing::AssertionFailure() << "Failed to parse schema (xsd)\n" << context();
108     }
109 
110     auto doc = make_xmlUnique(xmlReadFile(xmlFilePath, nullptr, 0));
111     if (doc == nullptr) {
112         return ::testing::AssertionFailure() << "Failed to parse xml\n" << context();
113     }
114 
115     // Process 'include' directives w/o modifying elements loaded from included files.
116     if (xmlXIncludeProcessFlags(doc.get(), XML_PARSE_NOBASEFIX) == -1) {
117         return ::testing::AssertionFailure() << "Failed to resolve xincludes in xml\n" << context();
118     }
119 
120     auto schemaCtxt = make_xmlUnique(xmlSchemaNewValidCtxt(schema.get()));
121     int ret = xmlSchemaValidateDoc(schemaCtxt.get(), doc.get());
122     if (ret > 0) {
123         return ::testing::AssertionFailure() << "XML is not valid according to the xsd\n"
124                                              << context();
125     }
126     if (ret < 0) {
127         return ::testing::AssertionFailure() << "Internal or API error\n" << context();
128     }
129 
130     return ::testing::AssertionSuccess();
131 }
132 
133 template <bool atLeastOneRequired>
validateXmlMultipleLocations(const char * xmlFileNameExpr,const char * xmlFileLocationsExpr,const char * xsdFilePathExpr,const char * xmlFileName,const std::vector<std::string> & xmlFileLocations,const char * xsdFilePath)134 ::testing::AssertionResult validateXmlMultipleLocations(
135         const char* xmlFileNameExpr, const char* xmlFileLocationsExpr, const char* xsdFilePathExpr,
136         const char* xmlFileName, const std::vector<std::string>& xmlFileLocations,
137         const char* xsdFilePath) {
138     using namespace std::string_literals;
139 
140     std::vector<std::string> errors;
141     std::vector<std::string> foundFiles;
142 
143     for (const auto& location : xmlFileLocations) {
144         std::string xmlFilePath = location + "/"s + xmlFileName;
145         if (access(xmlFilePath.c_str(), F_OK) != 0) {
146             // If the file does not exist ignore this location and fallback on the next one
147             continue;
148         }
149         foundFiles.push_back("    " + xmlFilePath + '\n');
150         auto result = validateXml("xmlFilePath", xsdFilePathExpr, xmlFilePath.c_str(), xsdFilePath);
151         if (!result) {
152             errors.push_back(result.message());
153         }
154     }
155 
156     if (atLeastOneRequired && foundFiles.empty()) {
157         errors.push_back("No xml file found in provided locations.\n");
158     }
159 
160     return ::testing::AssertionResult(errors.empty())
161            << errors.size() << " error" << (errors.size() == 1 ? " " : "s ")
162            << std::accumulate(begin(errors), end(errors), "occurred during xml validation:\n"s)
163            << "     While validating all: " << xmlFileNameExpr
164            << "\n                 Which is: " << xmlFileName
165            << "\n In the following folders: " << xmlFileLocationsExpr
166            << "\n                 Which is: " << ::testing::PrintToString(xmlFileLocations)
167            << (atLeastOneRequired ? "\nWhere at least one file must be found."
168                                   : "\nWhere no file might exist.");
169 }
170 
171 template ::testing::AssertionResult validateXmlMultipleLocations<true>(
172         const char*, const char*, const char*, const char*, const std::vector<std::string>&,
173         const char*);
174 template ::testing::AssertionResult validateXmlMultipleLocations<false>(
175         const char*, const char*, const char*, const char*, const std::vector<std::string>&,
176         const char*);
177 
178 }  // namespace utility
179 }  // namespace test
180 }  // namespace common
181 }  // namespace audio
182 }  // namespace hardware
183 }  // namespace android
184