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