summaryrefslogtreecommitdiff
path: root/audio/common/all-versions/test/utility/tests/utility_tests.cpp
blob: c52306638f6e76d5e5db18dc916709d5686f2c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android-base/file.h>
#include <gtest/gtest.h>

#include <ValidateXml.h>

using ::android::hardware::audio::common::test::utility::validateXml;

const char* XSD_SOURCE =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        "<xs:schema version=\"2.0\""
        "           elementFormDefault=\"qualified\""
        "           attributeFormDefault=\"unqualified\""
        "           xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
        "  <xs:element name=\"audioPolicyConfiguration\">"
        "    <xs:complexType>"
        "      <xs:sequence>"
        "        <xs:element name=\"modules\">"
        "          <xs:complexType>"
        "            <xs:sequence>"
        "              <xs:element name=\"module\" maxOccurs=\"unbounded\">"
        "                <xs:complexType>"
        "                  <xs:attribute name=\"name\" type=\"xs:string\" use=\"required\"/>"
        "                </xs:complexType>"
        "              </xs:element>"
        "            </xs:sequence>"
        "          </xs:complexType>"
        "        </xs:element>"
        "      </xs:sequence>"
        "    </xs:complexType>"
        "  </xs:element>"
        "</xs:schema>";

const char* INVALID_XML_SOURCE =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
        "<audioPolicyKonfiguration />";

const char* VALID_XML_SOURCE =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
        "<audioPolicyConfiguration>"
        "  <modules>"
        "    <module name=\"aaa\" />"
        "    %s"
        "  </modules>"
        "</audioPolicyConfiguration>";

const char* MODULE_SOURCE = "<module name=\"bbb\" />";

const char* XI_INCLUDE = "<xi:include xmlns:xi=\"http://www.w3.org/2001/XInclude\" href=\"%s\" />";

const char* XML_INCLUDED_SOURCE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>%s";

namespace {

std::string substitute(const char* fmt, const char* param) {
    std::string buffer(static_cast<size_t>(strlen(fmt) + strlen(param)), '\0');
    snprintf(buffer.data(), buffer.size(), fmt, param);
    return buffer;
}

std::string substitute(const char* fmt, const std::string& s) {
    return substitute(fmt, s.c_str());
}

}  // namespace

TEST(ValidateXml, InvalidXml) {
    TemporaryFile xml;
    ASSERT_TRUE(android::base::WriteStringToFile(INVALID_XML_SOURCE, xml.path)) << strerror(errno);
    TemporaryFile xsd;
    ASSERT_TRUE(android::base::WriteStringToFile(XSD_SOURCE, xsd.path)) << strerror(errno);
    EXPECT_FALSE(validateXml("xml", "xsd", xml.path, xsd.path));
}

TEST(ValidateXml, ValidXml) {
    TemporaryFile xml;
    ASSERT_TRUE(
            android::base::WriteStringToFile(substitute(VALID_XML_SOURCE, MODULE_SOURCE), xml.path))
            << strerror(errno);
    TemporaryFile xsd;
    ASSERT_TRUE(android::base::WriteStringToFile(XSD_SOURCE, xsd.path)) << strerror(errno);
    EXPECT_TRUE(validateXml("xml", "xsd", xml.path, xsd.path));
}

TEST(ValidateXml, IncludeAbsolutePath) {
    TemporaryFile xmlInclude;
    ASSERT_TRUE(android::base::WriteStringToFile(substitute(XML_INCLUDED_SOURCE, MODULE_SOURCE),
                                                 xmlInclude.path))
            << strerror(errno);
    TemporaryFile xml;
    ASSERT_TRUE(android::base::WriteStringToFile(
            substitute(VALID_XML_SOURCE, substitute(XI_INCLUDE, xmlInclude.path)), xml.path))
            << strerror(errno);
    TemporaryFile xsd;
    ASSERT_TRUE(android::base::WriteStringToFile(XSD_SOURCE, xsd.path)) << strerror(errno);
    EXPECT_TRUE(validateXml("xml", "xsd", xml.path, xsd.path));
}

TEST(ValidateXml, IncludeSameDirRelativePath) {
    TemporaryFile xmlInclude;
    ASSERT_TRUE(android::base::WriteStringToFile(substitute(XML_INCLUDED_SOURCE, MODULE_SOURCE),
                                                 xmlInclude.path))
            << strerror(errno);
    TemporaryFile xml;
    ASSERT_EQ(android::base::Dirname(xml.path), android::base::Dirname(xmlInclude.path));
    ASSERT_TRUE(android::base::WriteStringToFile(
            substitute(VALID_XML_SOURCE,
                       substitute(XI_INCLUDE, android::base::Basename(xmlInclude.path))),
            xml.path))
            << strerror(errno);
    TemporaryFile xsd;
    ASSERT_TRUE(android::base::WriteStringToFile(XSD_SOURCE, xsd.path)) << strerror(errno);
    EXPECT_TRUE(validateXml("xml", "xsd", xml.path, xsd.path));
}

TEST(ValidateXml, IncludeSubdirRelativePath) {
    TemporaryDir xmlIncludeDir;
    TemporaryFile xmlInclude(xmlIncludeDir.path);
    ASSERT_TRUE(android::base::WriteStringToFile(substitute(XML_INCLUDED_SOURCE, MODULE_SOURCE),
                                                 xmlInclude.path))
            << strerror(errno);
    TemporaryFile xml;
    ASSERT_EQ(android::base::Dirname(xml.path), android::base::Dirname(xmlIncludeDir.path));
    ASSERT_TRUE(android::base::WriteStringToFile(
            substitute(VALID_XML_SOURCE,
                       substitute(XI_INCLUDE, android::base::Basename(xmlIncludeDir.path) + "/" +
                                                      android::base::Basename(xmlInclude.path))),
            xml.path))
            << strerror(errno);
    TemporaryFile xsd;
    ASSERT_TRUE(android::base::WriteStringToFile(XSD_SOURCE, xsd.path)) << strerror(errno);
    EXPECT_TRUE(validateXml("xml", "xsd", xml.path, xsd.path));
}

TEST(ValidateXml, IncludeParentDirRelativePath) {
    // An XML file from a subdirectory includes a file from the parent directory using '..' syntax.
    TemporaryFile xmlInclude;
    ASSERT_TRUE(android::base::WriteStringToFile(substitute(XML_INCLUDED_SOURCE, MODULE_SOURCE),
                                                 xmlInclude.path))
            << strerror(errno);
    TemporaryDir xmlIncludeDir;
    TemporaryFile xmlParentInclude(xmlIncludeDir.path);
    ASSERT_TRUE(android::base::WriteStringToFile(
            substitute(XML_INCLUDED_SOURCE,
                       substitute(XI_INCLUDE, "../" + android::base::Basename(xmlInclude.path))),
            xmlParentInclude.path))
            << strerror(errno);
    TemporaryFile xml;
    ASSERT_EQ(android::base::Dirname(xml.path), android::base::Dirname(xmlInclude.path));
    ASSERT_EQ(android::base::Dirname(xml.path), android::base::Dirname(xmlIncludeDir.path));
    ASSERT_TRUE(android::base::WriteStringToFile(
            substitute(
                    VALID_XML_SOURCE,
                    substitute(XI_INCLUDE, android::base::Basename(xmlIncludeDir.path) + "/" +
                                                   android::base::Basename(xmlParentInclude.path))),
            xml.path))
            << strerror(errno);
    TemporaryFile xsd;
    ASSERT_TRUE(android::base::WriteStringToFile(XSD_SOURCE, xsd.path)) << strerror(errno);
    EXPECT_TRUE(validateXml("xml", "xsd", xml.path, xsd.path));
}