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 <fstream>
18 #include <string>
19
20 #include <android-base/file.h>
21 #include <android-base/properties.h>
22 #include "utility/ValidateXml.h"
23
isFileReadable(std::string const & path)24 bool isFileReadable(std::string const& path) {
25 std::ifstream f(path);
26 return f.good();
27 }
28
TEST(CheckConfig,mediaProfilesValidation)29 TEST(CheckConfig, mediaProfilesValidation) {
30 RecordProperty("description",
31 "Verify that the media profiles file "
32 "is valid according to the schema");
33
34 // Schema path.
35 constexpr char const* xsdPath = "/data/local/tmp/media_profiles.xsd";
36
37 // If "media.settings.xml" is set, it will be used as an absolute path.
38 std::string mediaSettingsPath = android::base::GetProperty("media.settings.xml", "");
39 if (mediaSettingsPath.empty()) {
40 // If "media.settings.xml" is not set, we will search through a list of
41 // file paths.
42
43 constexpr char const* xmlSearchDirs[] = {
44 "/product/etc/",
45 "/odm/etc/",
46 "/vendor/etc/",
47 };
48
49 // The vendor may provide a vendor variant for the file name.
50 std::string variant = android::base::GetProperty(
51 "ro.media.xml_variant.profiles", "_V1_0");
52 std::string fileName = "media_profiles" + variant + ".xml";
53
54 // Fallback path does not depend on the property defined from the vendor
55 // partition.
56 constexpr char const* fallbackXmlPath =
57 "/system/etc/media_profiles_V1_0.xml";
58
59 std::vector<std::string> xmlPaths = {
60 xmlSearchDirs[0] + fileName,
61 xmlSearchDirs[1] + fileName,
62 xmlSearchDirs[2] + fileName,
63 fallbackXmlPath
64 };
65
66 auto findXmlPath =
67 std::find_if(xmlPaths.begin(), xmlPaths.end(), isFileReadable);
68 ASSERT_TRUE(findXmlPath != xmlPaths.end())
69 << "Cannot read from " << fileName
70 << " in any search directories ("
71 << xmlSearchDirs[0] << ", "
72 << xmlSearchDirs[1] << ", "
73 << xmlSearchDirs[2] << ") and from "
74 << fallbackXmlPath << ".";
75
76 char const* xmlPath = findXmlPath->c_str();
77 EXPECT_VALID_XML(xmlPath, xsdPath);
78 } else {
79 EXPECT_VALID_XML(mediaSettingsPath.c_str(), xsdPath);
80 }
81 }
82