1 /*
2  * Copyright (C) 2019 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 #include <EngineConfig.h>
18 #include <ParameterManagerWrapper.h>
19 
20 #include <gtest/gtest.h>
21 
22 #include <unistd.h>
23 #include <string>
24 #include "utility/ValidateXml.h"
25 
26 #include <system/audio_config.h>
27 
28 static const std::string config = "audio_policy_engine_configuration.xml";
29 static const std::string schema =
30         std::string(XSD_DIR) + "/audio_policy_engine_configuration_V1_0.xsd";
31 
32 static const std::string configurableSchemas =
33         std::string(XSD_DIR) + "/audio_policy_engine_configurable_configuration_V1_0.xsd";
34 static const std::string configurableConfig =
35         "parameter-framework/ParameterFrameworkConfigurationPolicy.xml";
36 
37 /**
38  * @brief TEST to ensure the audio policy engine configuration file is validating schemas.
39  * Note: this configuration file is not mandatory, an hardcoded fallback is provided, so
40  * it does not fail if not found.
41  */
TEST(ValidateConfiguration,audioPolicyEngineConfiguration)42 TEST(ValidateConfiguration, audioPolicyEngineConfiguration) {
43     RecordProperty("description",
44                    "Verify that the audio policy engine configuration file "
45                    "is valid according to the schemas");
46     EXPECT_VALID_XML_MULTIPLE_LOCATIONS(config.c_str(), android::audio_get_configuration_paths(),
47                                         schema.c_str());
48 }
49 
50 /**
51  * @brief deviceUsesConfigurableEngine checks if the configuration file for
52  * the engine presents on the device AND
53  * for the configurable engine (aka Parameter-Framework top configuration file) presents.
54  */
deviceUsesConfigurableEngine()55 static bool deviceUsesConfigurableEngine() {
56     return android::hardware::audio::common::test::utility::validateXmlMultipleLocations<true>(
57                    "", "", "", config.c_str(), android::audio_get_configuration_paths(),
58                    schema.c_str()) &&
59            android::hardware::audio::common::test::utility::validateXmlMultipleLocations<true>(
60                    "", "", "", configurableConfig.c_str(), android::audio_get_configuration_paths(),
61                    configurableSchemas.c_str());
62 }
63 
TEST(ValidateConfiguration,audioPolicyEngineConfigurable)64 TEST(ValidateConfiguration, audioPolicyEngineConfigurable) {
65     if (!deviceUsesConfigurableEngine()) {
66         GTEST_SKIP() << "Device using legacy engine without parameter-framework, n-op.";
67     }
68     RecordProperty("description",
69                    "Verify that the audio policy engine PFW configuration files "
70                    "are valid according to the schemas");
71 
72     auto testAudioPolicyEnginePfw = [&](bool validateSchema, const std::string& schemasUri) {
73         auto result = android::engineConfig::parse();
74 
75         ASSERT_NE(nullptr, result.parsedConfig)
76                 << "failed to parse audio policy engine configuration";
77 
78         ASSERT_EQ(result.nbSkippedElement, 0) << "skipped %zu elements " << result.nbSkippedElement;
79 
80         std::unique_ptr<android::audio_policy::ParameterManagerWrapper> policyParameterMgr(
81                 new android::audio_policy::ParameterManagerWrapper(validateSchema, schemasUri));
82         ASSERT_NE(nullptr, policyParameterMgr) << "failed to create Audio Policy Engine PFW";
83 
84         // Load the criterion types and criteria
85         for (auto& criterion : result.parsedConfig->criteria) {
86             android::engineConfig::CriterionType criterionType;
87             for (auto& configCriterionType : result.parsedConfig->criterionTypes) {
88                 if (configCriterionType.name == criterion.typeName) {
89                     criterionType = configCriterionType;
90                     break;
91                 }
92             }
93             ASSERT_FALSE(criterionType.name.empty())
94                     << "Invalid criterion type for " << criterion.name.c_str();
95             policyParameterMgr->addCriterion(criterion.name, criterionType.isInclusive,
96                                              criterionType.valuePairs,
97                                              criterion.defaultLiteralValue);
98         }
99         ASSERT_EQ(0, result.nbSkippedElement) << "failed to parse Audio Policy Engine PFW criteria";
100 
101         // If the PFW cannot validate, it will not start
102         std::string error;
103         auto status = policyParameterMgr->start(error);
104         ASSERT_EQ(status, android::NO_ERROR)
105                 << "failed to " << (validateSchema ? "validate" : "start")
106                 << " Audio Policy Engine PFW: " << error;
107 
108         ASSERT_TRUE(policyParameterMgr->isStarted());
109     };
110 
111     // First round for sanity to ensure we can launch the Audio Policy Engine PFW without
112     // schema validation successfully, otherwise it is not forth going on running validation...
113     testAudioPolicyEnginePfw(false, {});
114 
115     // If second round fails, it means parameter-framework cannot validate schema
116     testAudioPolicyEnginePfw(true, {XSD_PFW_DIR});
117 }
118