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