blob: 784f9401ecc7432d1232f493e7128f2a45372e46 [file] [log] [blame]
Kevin Rocard06a2a402017-05-30 17:15:28 -07001/*
2 * Copyright (C) 2017 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#define LIBXML_SCHEMAS_ENABLED
21#include <libxml/xmlschemastypes.h>
22#define LIBXML_XINCLUDE_ENABLED
23#include <libxml/xinclude.h>
24
25#include <memory>
26#include <string>
27
28#include "ValidateXml.h"
29
30namespace android {
31namespace hardware {
32namespace audio {
Kevin Rocardb6d79ff2017-06-02 11:44:06 -070033namespace common {
Kevin Rocard06a2a402017-05-30 17:15:28 -070034namespace test {
Kevin Rocardb6d79ff2017-06-02 11:44:06 -070035namespace utility {
Kevin Rocard06a2a402017-05-30 17:15:28 -070036
37/** Map libxml2 structures to their corresponding deleters. */
38template <class T>
39constexpr void (*xmlDeleter)(T* t);
40template <>
41constexpr auto xmlDeleter<xmlSchema> = xmlSchemaFree;
42template <>
43constexpr auto xmlDeleter<xmlDoc> = xmlFreeDoc;
44template <>
45constexpr auto xmlDeleter<xmlSchemaParserCtxt> = xmlSchemaFreeParserCtxt;
46template <>
47constexpr auto xmlDeleter<xmlSchemaValidCtxt> = xmlSchemaFreeValidCtxt;
48
49/** @return a unique_ptr with the correct deleter for the libxml2 object. */
50template <class T>
51constexpr auto make_xmlUnique(T* t) {
52 // Wrap deleter in lambda to enable empty base optimization
53 auto deleter = [](T* t) { xmlDeleter<T>(t); };
54 return std::unique_ptr<T, decltype(deleter)>{t, deleter};
55}
56
57/** Class that handles libxml2 initialization and cleanup. NOT THREAD SAFE*/
58struct Libxml2Global {
59 Libxml2Global() {
60 xmlLineNumbersDefault(1); // Better error message
61 xmlSetGenericErrorFunc(this, errorCb);
62 }
63 ~Libxml2Global() {
64 // TODO: check if all those cleanup are needed
65 xmlSetGenericErrorFunc(nullptr, nullptr);
66 xmlSchemaCleanupTypes();
67 xmlCleanupParser();
68 xmlCleanupThreads();
69 }
70
71 const std::string& getErrors() { return errors; }
72
73 private:
74 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
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 + "Libxml2 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 if (xmlXIncludeProcess(doc.get()) == -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
Kevin Rocardb6d79ff2017-06-02 11:44:06 -0700130} // utility
131} // test
132} // common
133} // audio
134} // test
135} // utility