• 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 #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         xmlSetGenericErrorFunc(nullptr, nullptr);
67         xmlCleanupParser();
68     }
69 
getErrorsandroid::hardware::audio::common::test::utility::Libxml2Global70     const std::string& getErrors() { return errors; }
71 
72    private:
errorCbandroid::hardware::audio::common::test::utility::Libxml2Global73     static void errorCb(void* ctxt, const char* msg, ...) {
74         auto* self = static_cast<Libxml2Global*>(ctxt);
75         va_list args;
76         va_start(args, msg);
77 
78         char* formatedMsg;
79         if (vasprintf(&formatedMsg, msg, args) >= 0) {
80             LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", formatedMsg);
81             self->errors += "Error: ";
82             self->errors += formatedMsg;
83         }
84         free(formatedMsg);
85 
86         va_end(args);
87     }
88     std::string errors;
89 };
90 
validateXml(const char * xmlFilePathExpr,const char * xsdFilePathExpr,const char * xmlFilePath,const char * xsdFilePath)91 ::testing::AssertionResult validateXml(const char* xmlFilePathExpr, const char* xsdFilePathExpr,
92                                        const char* xmlFilePath, const char* xsdFilePath) {
93     Libxml2Global libxml2;
94 
95     auto context = [&]() {
96         return std::string() + "  While validating: " + xmlFilePathExpr +
97                "\n          Which is: " + xmlFilePath + "\nAgainst the schema: " + xsdFilePathExpr +
98                "\n          Which is: " + xsdFilePath + "\nLibxml2 errors:\n" + libxml2.getErrors();
99     };
100 
101     auto schemaParserCtxt = make_xmlUnique(xmlSchemaNewParserCtxt(xsdFilePath));
102     auto schema = make_xmlUnique(xmlSchemaParse(schemaParserCtxt.get()));
103     if (schema == nullptr) {
104         return ::testing::AssertionFailure() << "Failed to parse schema (xsd)\n" << context();
105     }
106 
107     auto doc = make_xmlUnique(xmlReadFile(xmlFilePath, nullptr, 0));
108     if (doc == nullptr) {
109         return ::testing::AssertionFailure() << "Failed to parse xml\n" << context();
110     }
111 
112     // Process 'include' directives w/o modifying elements loaded from included files.
113     if (xmlXIncludeProcessFlags(doc.get(), XML_PARSE_NOBASEFIX) == -1) {
114         return ::testing::AssertionFailure() << "Failed to resolve xincludes in xml\n" << context();
115     }
116 
117     auto schemaCtxt = make_xmlUnique(xmlSchemaNewValidCtxt(schema.get()));
118     int ret = xmlSchemaValidateDoc(schemaCtxt.get(), doc.get());
119     if (ret > 0) {
120         return ::testing::AssertionFailure() << "XML is not valid according to the xsd\n"
121                                              << context();
122     }
123     if (ret < 0) {
124         return ::testing::AssertionFailure() << "Internal or API error\n" << context();
125     }
126 
127     return ::testing::AssertionSuccess();
128 }
129 
130 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)131 ::testing::AssertionResult validateXmlMultipleLocations(
132         const char* xmlFileNameExpr, const char* xmlFileLocationsExpr, const char* xsdFilePathExpr,
133         const char* xmlFileName, const std::vector<std::string>& xmlFileLocations,
134         const char* xsdFilePath) {
135     using namespace std::string_literals;
136 
137     std::vector<std::string> errors;
138     std::vector<std::string> foundFiles;
139 
140     for (const auto& location : xmlFileLocations) {
141         std::string xmlFilePath = location + "/"s + xmlFileName;
142         if (access(xmlFilePath.c_str(), F_OK) != 0) {
143             // If the file does not exist ignore this location and fallback on the next one
144             continue;
145         }
146         foundFiles.push_back("    " + xmlFilePath + '\n');
147         auto result = validateXml("xmlFilePath", xsdFilePathExpr, xmlFilePath.c_str(), xsdFilePath);
148         if (!result) {
149             errors.push_back(result.message());
150         }
151     }
152 
153     if (atLeastOneRequired && foundFiles.empty()) {
154         errors.push_back("No xml file found in provided locations.\n");
155     }
156 
157     return ::testing::AssertionResult(errors.empty())
158            << errors.size() << " error" << (errors.size() == 1 ? " " : "s ")
159            << std::accumulate(begin(errors), end(errors), "occurred during xml validation:\n"s)
160            << "     While validating all: " << xmlFileNameExpr
161            << "\n                 Which is: " << xmlFileName
162            << "\n In the following folders: " << xmlFileLocationsExpr
163            << "\n                 Which is: " << ::testing::PrintToString(xmlFileLocations)
164            << (atLeastOneRequired ? "\nWhere at least one file must be found."
165                                   : "\nWhere no file might exist.");
166 }
167 
168 template ::testing::AssertionResult validateXmlMultipleLocations<true>(
169         const char*, const char*, const char*, const char*, const std::vector<std::string>&,
170         const char*);
171 template ::testing::AssertionResult validateXmlMultipleLocations<false>(
172         const char*, const char*, const char*, const char*, const std::vector<std::string>&,
173         const char*);
174 
175 }  // namespace utility
176 }  // namespace test
177 }  // namespace common
178 }  // namespace audio
179 }  // namespace hardware
180 }  // namespace android
181