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 #ifndef ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_VALIDATE_XML_H
18 #define ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_VALIDATE_XML_H
19 
20 #include <gtest/gtest.h>
21 
22 namespace android {
23 namespace hardware {
24 namespace audio {
25 namespace common {
26 namespace test {
27 namespace utility {
28 
29 /** Validate the provided XmlFile with the provided xsdFile.
30  * Intended to use with ASSERT_PRED_FORMAT2 as such:
31  *   ASSERT_PRED_FORMAT2(validateXml, pathToXml, pathToXsd);
32  * See ASSERT_VALID_XML for a helper macro.
33  */
34 ::testing::AssertionResult validateXml(const char* xmlFilePathExpr, const char* xsdFilePathExpr,
35                                        const char* xmlFilePath, const char* xsdFilePath);
36 
37 /** Helper gtest ASSERT to test XML validity against an XSD. */
38 #define ASSERT_VALID_XML(xmlFilePath, xsdFilePath)                                      \
39     ASSERT_PRED_FORMAT2(::android::hardware::audio::common::test::utility::validateXml, \
40                         xmlFilePath, xsdFilePath)
41 
42 /** Helper gtest EXPECT to test XML validity against an XSD. */
43 #define EXPECT_VALID_XML(xmlFilePath, xsdFilePath)                                      \
44     EXPECT_PRED_FORMAT2(::android::hardware::audio::common::test::utility::validateXml, \
45                         xmlFilePath, xsdFilePath)
46 
47 /** Validate an XML according to an xsd.
48  * All file named xmlFileName in each xmlFileLocations folder must be valid if present.
49  * @tparam atLeastOneRequired If true, at least one file has to be found.
50  *                           If false, no found file is a success.
51  */
52 template <bool atLeastOneRequired = true>
53 ::testing::AssertionResult validateXmlMultipleLocations(
54         const char* xmlFileNameExpr, const char* xmlFileLocationsExpr, const char* xsdFilePathExpr,
55         const char* xmlFileName, const std::vector<std::string>& xmlFileLocations,
56         const char* xsdFilePath);
57 template <bool atLeastOneRequired = true>
validateXmlMultipleLocations(const char * xmlFileNameExpr,const char * xmlFileLocationsExpr,const char * xsdFilePathExpr,const char * xmlFileName,std::initializer_list<const char * > xmlFileLocations,const char * xsdFilePath)58 ::testing::AssertionResult validateXmlMultipleLocations(
59         const char* xmlFileNameExpr, const char* xmlFileLocationsExpr, const char* xsdFilePathExpr,
60         const char* xmlFileName, std::initializer_list<const char*> xmlFileLocations,
61         const char* xsdFilePath) {
62     return validateXmlMultipleLocations<atLeastOneRequired>(
63             xmlFileNameExpr, xmlFileLocationsExpr, xsdFilePathExpr, xmlFileName,
64             std::vector<std::string>(xmlFileLocations.begin(), xmlFileLocations.end()),
65             xsdFilePath);
66 }
67 template <bool atLeastOneRequired = true>
validateXmlMultipleLocations(const char * xmlFileNameExpr,const char * xmlFileLocationsExpr,const char * xsdFilePathExpr,const char * xmlFileName,std::vector<const char * > xmlFileLocations,const char * xsdFilePath)68 ::testing::AssertionResult validateXmlMultipleLocations(const char* xmlFileNameExpr,
69                                                         const char* xmlFileLocationsExpr,
70                                                         const char* xsdFilePathExpr,
71                                                         const char* xmlFileName,
72                                                         std::vector<const char*> xmlFileLocations,
73                                                         const char* xsdFilePath) {
74     return validateXmlMultipleLocations<atLeastOneRequired>(
75             xmlFileNameExpr, xmlFileLocationsExpr, xsdFilePathExpr, xmlFileName,
76             std::vector<std::string>(xmlFileLocations.begin(), xmlFileLocations.end()),
77             xsdFilePath);
78 }
79 
80 /** ASSERT that all found XML are valid according to an xsd. */
81 #define ASSERT_VALID_XML_MULTIPLE_LOCATIONS(xmlFileName, xmlFileLocations, xsdFilePath)         \
82     ASSERT_PRED_FORMAT3(                                                                        \
83         ::android::hardware::audio::common::test::utility::validateXmlMultipleLocations<false>, \
84         xmlFileName, xmlFileLocations, xsdFilePath)
85 
86 /** EXPECT that all found XML are valid according to an xsd. */
87 #define EXPECT_VALID_XML_MULTIPLE_LOCATIONS(xmlFileName, xmlFileLocations, xsdFilePath)         \
88     EXPECT_PRED_FORMAT3(                                                                        \
89         ::android::hardware::audio::common::test::utility::validateXmlMultipleLocations<false>, \
90         xmlFileName, xmlFileLocations, xsdFilePath)
91 
92 /** ASSERT that all found XML are valid according to an xsd. At least one must be found. */
93 #define ASSERT_ONE_VALID_XML_MULTIPLE_LOCATIONS(xmlFileName, xmlFileLocations, xsdFilePath)    \
94     ASSERT_PRED_FORMAT3(                                                                       \
95         ::android::hardware::audio::common::test::utility::validateXmlMultipleLocations<true>, \
96         xmlFileName, xmlFileLocations, xsdFilePath)
97 
98 /** EXPECT that all found XML are valid according to an xsd. At least one must be found. */
99 #define EXPECT_ONE_VALID_XML_MULTIPLE_LOCATIONS(xmlFileName, xmlFileLocations, xsdFilePath)    \
100     EXPECT_PRED_FORMAT3(                                                                       \
101         ::android::hardware::audio::common::test::utility::validateXmlMultipleLocations<true>, \
102         xmlFileName, xmlFileLocations, xsdFilePath)
103 
104 }  // namespace utility
105 }  // namespace test
106 }  // namespace common
107 }  // namespace audio
108 }  // namespace hardware
109 }  // namespace android
110 
111 #endif  // ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_VALIDATE_XML_H
112