blob: 2fb828e30788da78af78fa93399049213ee813cb [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 {
33namespace test {
34
35/** Map libxml2 structures to their corresponding deleters. */
36template <class T>
37constexpr void (*xmlDeleter)(T* t);
38template <>
39constexpr auto xmlDeleter<xmlSchema> = xmlSchemaFree;
40template <>
41constexpr auto xmlDeleter<xmlDoc> = xmlFreeDoc;
42template <>
43constexpr auto xmlDeleter<xmlSchemaParserCtxt> = xmlSchemaFreeParserCtxt;
44template <>
45constexpr auto xmlDeleter<xmlSchemaValidCtxt> = xmlSchemaFreeValidCtxt;
46
47/** @return a unique_ptr with the correct deleter for the libxml2 object. */
48template <class T>
49constexpr auto make_xmlUnique(T* t) {
50 // Wrap deleter in lambda to enable empty base optimization
51 auto deleter = [](T* t) { xmlDeleter<T>(t); };
52 return std::unique_ptr<T, decltype(deleter)>{t, deleter};
53}
54
55/** Class that handles libxml2 initialization and cleanup. NOT THREAD SAFE*/
56struct Libxml2Global {
57 Libxml2Global() {
58 xmlLineNumbersDefault(1); // Better error message
59 xmlSetGenericErrorFunc(this, errorCb);
60 }
61 ~Libxml2Global() {
62 // TODO: check if all those cleanup are needed
63 xmlSetGenericErrorFunc(nullptr, nullptr);
64 xmlSchemaCleanupTypes();
65 xmlCleanupParser();
66 xmlCleanupThreads();
67 }
68
69 const std::string& getErrors() { return errors; }
70
71 private:
72 static void errorCb(void* ctxt, const char* msg, ...) {
73 auto* self = static_cast<Libxml2Global*>(ctxt);
74 va_list args;
75 va_start(args, msg);
76
77 char* formatedMsg;
78 if (vasprintf(&formatedMsg, msg, args) >= 0) {
79 LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", formatedMsg);
80 self->errors += "Error: ";
81 self->errors += formatedMsg;
82 }
83 free(formatedMsg);
84
85 va_end(args);
86 }
87 std::string errors;
88};
89
90::testing::AssertionResult validateXml(const char* xmlFilePathExpr, const char* xsdFilePathExpr,
91 const char* xmlFilePath, const char* xsdFilePath) {
92 Libxml2Global libxml2;
93
94 auto context = [&]() {
95 return std::string() + " While validating: " + xmlFilePathExpr +
96 "\n Which is: " + xmlFilePath + "\nAgainst the schema: " + xsdFilePathExpr +
97 "\n Which is: " + xsdFilePath + "Libxml2 errors\n" + libxml2.getErrors();
98 };
99
100 auto schemaParserCtxt = make_xmlUnique(xmlSchemaNewParserCtxt(xsdFilePath));
101 auto schema = make_xmlUnique(xmlSchemaParse(schemaParserCtxt.get()));
102 if (schema == nullptr) {
103 return ::testing::AssertionFailure() << "Failed to parse schema (xsd)\n" << context();
104 }
105
106 auto doc = make_xmlUnique(xmlReadFile(xmlFilePath, nullptr, 0));
107 if (doc == nullptr) {
108 return ::testing::AssertionFailure() << "Failed to parse xml\n" << context();
109 }
110
111 if (xmlXIncludeProcess(doc.get()) == -1) {
112 return ::testing::AssertionFailure() << "Failed to resolve xincludes in xml\n" << context();
113 }
114
115 auto schemaCtxt = make_xmlUnique(xmlSchemaNewValidCtxt(schema.get()));
116 int ret = xmlSchemaValidateDoc(schemaCtxt.get(), doc.get());
117 if (ret > 0) {
118 return ::testing::AssertionFailure() << "xml is not valid according to the xsd.\n"
119 << context();
120 }
121 if (ret < 0) {
122 return ::testing::AssertionFailure() << "Internal or API error\n" << context();
123 }
124
125 return ::testing::AssertionSuccess();
126}
127
128} // namespace test
129} // namespace audio
130} // namespace hardware
131} // namespace android