blob: 9f39d871ea56fe29e272919df200e740ce3c1a02 [file] [log] [blame]
Yifan Hong676447a2016-11-15 12:57:23 -08001/*
2 * Copyright (C) 2017 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#define LOG_TAG "LibHidlTest"
18
19#include <vintf/parse_string.h>
20#include <vintf/parse_xml.h>
21#include <vintf/CompatibilityMatrix.h>
22#include <vintf/VendorManifest.h>
23
24#include <android-base/logging.h>
25#include <gtest/gtest.h>
26
27using namespace android::vintf;
28
29class LibVintfTest : public ::testing::Test {
30public:
31 virtual void SetUp() override {
32 }
33 virtual void TearDown() override {
34 }
35};
36
Yifan Hongef6d4d32017-01-23 14:12:28 -080037static VendorManifest testVendorManifest() {
Yifan Hong676447a2016-11-15 12:57:23 -080038 VendorManifest vm;
Yifan Hongef6d4d32017-01-23 14:12:28 -080039 vm.add(ManifestHal::hal("android.hardware.camera", ImplLevel::SOC, "msm8892",
Yifan Hong676447a2016-11-15 12:57:23 -080040 Version(2,0), Transport::HWBINDER));
Yifan Hongef6d4d32017-01-23 14:12:28 -080041 vm.add(ManifestHal::hal("android.hardware.nfc", ImplLevel::GENERIC, "generic",
Yifan Hong676447a2016-11-15 12:57:23 -080042 Version(1,0), Transport::PASSTHROUGH));
Yifan Hongef6d4d32017-01-23 14:12:28 -080043
44 return vm;
45}
46
47TEST_F(LibVintfTest, Stringify) {
48 VendorManifest vm = testVendorManifest();
49 EXPECT_EQ(dump(vm), "hidl/android.hardware.camera/hwbinder/soc/msm8892/2.0:"
50 "hidl/android.hardware.nfc/passthrough/generic/generic/1.0");
Yifan Hong676447a2016-11-15 12:57:23 -080051
52 EXPECT_EQ(to_string(HalFormat::HIDL), "hidl");
53 EXPECT_EQ(to_string(HalFormat::NATIVE), "native");
54
55 VersionRange v(1, 2, 3);
56 EXPECT_EQ(to_string(v), "1.2-3");
57 VersionRange v2;
58 EXPECT_TRUE(parse("1.2-3", &v2));
59 EXPECT_EQ(v, v2);
60}
61
62TEST_F(LibVintfTest, VendorManifestConverter) {
Yifan Hongef6d4d32017-01-23 14:12:28 -080063 VendorManifest vm = testVendorManifest();
Yifan Hong676447a2016-11-15 12:57:23 -080064 std::string xml = gVendorManifestConverter(vm);
65 EXPECT_EQ(xml,
66 "<manifest version=\"1.0\">\n"
67 " <hal format=\"hidl\">\n"
Yifan Hongef6d4d32017-01-23 14:12:28 -080068 " <name>android.hardware.camera</name>\n"
Yifan Hong676447a2016-11-15 12:57:23 -080069 " <transport>hwbinder</transport>\n"
70 " <impl level=\"soc\">msm8892</impl>\n"
71 " <version>2.0</version>\n"
72 " </hal>\n"
73 " <hal format=\"hidl\">\n"
Yifan Hongef6d4d32017-01-23 14:12:28 -080074 " <name>android.hardware.nfc</name>\n"
Yifan Hong676447a2016-11-15 12:57:23 -080075 " <transport>passthrough</transport>\n"
76 " <impl level=\"generic\">generic</impl>\n"
77 " <version>1.0</version>\n"
78 " </hal>\n"
79 "</manifest>\n");
80}
81
82TEST_F(LibVintfTest, VersionConverter) {
83 Version v(3, 6);
84 std::string xml = gVersionConverter(v);
85 EXPECT_EQ(xml, "<version>3.6</version>\n");
86 Version v2;
87 EXPECT_TRUE(gVersionConverter(&v2, xml));
88 EXPECT_EQ(v, v2);
89}
90
91TEST_F(LibVintfTest, HalImplementationConverter) {
92 HalImplementation hl{ImplLevel::SOC, "msm8992"};
93 std::string xml = gHalImplementationConverter(hl);
94 EXPECT_EQ(xml, "<impl level=\"soc\">msm8992</impl>\n");
95 HalImplementation hl2;
96 EXPECT_TRUE(gHalImplementationConverter(&hl2, xml));
97 EXPECT_EQ(hl.impl, hl2.impl);
98 EXPECT_EQ(hl.implLevel, hl2.implLevel);
99}
100
101TEST_F(LibVintfTest, MatrixHalConverter) {
Yifan Hongef6d4d32017-01-23 14:12:28 -0800102 MatrixHal mh{HalFormat::NATIVE, "android.hardware.camera",
Yifan Hong676447a2016-11-15 12:57:23 -0800103 {{VersionRange(1,2,3), VersionRange(4,5,6)}},
104 false /* optional */};
105 std::string xml = gMatrixHalConverter(mh);
106 EXPECT_EQ(xml,
107 "<hal format=\"native\" optional=\"false\">\n"
Yifan Hongef6d4d32017-01-23 14:12:28 -0800108 " <name>android.hardware.camera</name>\n"
Yifan Hong676447a2016-11-15 12:57:23 -0800109 " <version>1.2-3</version>\n"
110 " <version>4.5-6</version>\n"
111 "</hal>\n");
112 MatrixHal mh2;
113 EXPECT_TRUE(gMatrixHalConverter(&mh2, xml));
114 EXPECT_EQ(mh, mh2);
115}
116
117TEST_F(LibVintfTest, CompatibilityMatrixCoverter) {
118 CompatibilityMatrix cm;
Yifan Hongef6d4d32017-01-23 14:12:28 -0800119 cm.add(MatrixHal{HalFormat::NATIVE, "android.hardware.camera",
Yifan Hong676447a2016-11-15 12:57:23 -0800120 {{VersionRange(1,2,3), VersionRange(4,5,6)}},
121 false /* optional */});
Yifan Hongef6d4d32017-01-23 14:12:28 -0800122 cm.add(MatrixHal{HalFormat::NATIVE, "android.hardware.nfc",
Yifan Hong676447a2016-11-15 12:57:23 -0800123 {{VersionRange(4,5,6), VersionRange(10,11,12)}},
124 true /* optional */});
125 cm.add(MatrixKernel{Version(3, 18),
126 {{{"CONFIG_FOO"}, {"CONFIG_BAR"}}}});
127 cm.add(MatrixKernel{Version(4, 4),
128 {{{"CONFIG_BAZ"}, {"CONFIG_BAR"}}}});
129 std::string xml = gCompatibilityMatrixConverter(cm);
130 EXPECT_EQ(xml,
131 "<compatibility-matrix version=\"1.0\">\n"
132 " <hal format=\"native\" optional=\"false\">\n"
Yifan Hongef6d4d32017-01-23 14:12:28 -0800133 " <name>android.hardware.camera</name>\n"
Yifan Hong676447a2016-11-15 12:57:23 -0800134 " <version>1.2-3</version>\n"
135 " <version>4.5-6</version>\n"
136 " </hal>\n"
137 " <hal format=\"native\" optional=\"true\">\n"
Yifan Hongef6d4d32017-01-23 14:12:28 -0800138 " <name>android.hardware.nfc</name>\n"
Yifan Hong676447a2016-11-15 12:57:23 -0800139 " <version>4.5-6</version>\n"
140 " <version>10.11-12</version>\n"
141 " </hal>\n"
142 " <kernel version=\"3.18\">\n"
143 " <config>CONFIG_FOO</config>\n"
144 " <config>CONFIG_BAR</config>\n"
145 " </kernel>\n"
146 " <kernel version=\"4.4\">\n"
147 " <config>CONFIG_BAZ</config>\n"
148 " <config>CONFIG_BAR</config>\n"
149 " </kernel>\n"
150 " <sepolicy/>\n"
151 "</compatibility-matrix>\n");
152 CompatibilityMatrix cm2;
153 EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml));
154 EXPECT_EQ(cm.hals, cm2.hals);
155 EXPECT_EQ(cm.kernels, cm2.kernels);
156}
157
158TEST_F(LibVintfTest, IsValid) {
159 EXPECT_TRUE(ManifestHal().isValid());
160 EXPECT_TRUE(VendorManifest().isValid());
161
Yifan Hongef6d4d32017-01-23 14:12:28 -0800162 VendorManifest vm = testVendorManifest();
Yifan Hong676447a2016-11-15 12:57:23 -0800163 EXPECT_TRUE(vm.isValid());
164
Yifan Hongef6d4d32017-01-23 14:12:28 -0800165 ManifestHal invalidHal = ManifestHal::hal("android.hardware.camera", ImplLevel::SOC, "msm8892",
Yifan Hong676447a2016-11-15 12:57:23 -0800166 {{Version(2,0), Version(2,1)}}, Transport::PASSTHROUGH);
167 EXPECT_FALSE(invalidHal.isValid());
168 VendorManifest vm2;
169 vm2.add(std::move(invalidHal));
170 EXPECT_FALSE(vm2.isValid());
171}
172
Yifan Hongef6d4d32017-01-23 14:12:28 -0800173TEST_F(LibVintfTest, VendorManifestGetHal) {
174 VendorManifest vm = testVendorManifest();
175 EXPECT_NE(vm.getHal("android.hardware.camera"), nullptr);
176 EXPECT_EQ(vm.getHal("non-existent"), nullptr);
177
178 std::vector<std::string> arr{"android.hardware.camera", "android.hardware.nfc"};
179 size_t i = 0;
180 for (const auto &hal : vm.getHals()) {
181 EXPECT_EQ(hal.name, arr[i++]);
182 }
183}
184
Yifan Hong676447a2016-11-15 12:57:23 -0800185int main(int argc, char **argv) {
186 ::testing::InitGoogleTest(&argc, argv);
187 return RUN_ALL_TESTS();
188}