blob: 4f3951afeb9481a8df71da8fb098e59233b4fcf6 [file] [log] [blame]
Sundong Ahn76839442019-03-20 23:02:57 +09001/*
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
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -080017#include <fstream>
Sundong Ahnef8695e2019-07-30 20:43:43 +090018#include <string>
19
20#include <android-base/file.h>
21#include <android-base/properties.h>
Sundong Ahn76839442019-03-20 23:02:57 +090022#include "utility/ValidateXml.h"
23
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -080024bool isFileReadable(std::string const& path) {
25 std::ifstream f(path);
26 return f.good();
27}
28
Sundong Ahn76839442019-03-20 23:02:57 +090029TEST(CheckConfig, mediaProfilesValidation) {
30 RecordProperty("description",
31 "Verify that the media profiles file "
32 "is valid according to the schema");
33
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -080034 // 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.
Sundong Ahnef8695e2019-07-30 20:43:43 +090038 std::string mediaSettingsPath = android::base::GetProperty("media.settings.xml", "");
39 if (mediaSettingsPath.empty()) {
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -080040 // If "media.settings.xml" is not set, we will search through a list of
41 // file paths.
Sundong Ahn76839442019-03-20 23:02:57 +090042
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -080043 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 }
Sundong Ahn76839442019-03-20 23:02:57 +090081}