blob: 97971e232095a6b9e461af9f0e883d4a4459e6af [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
37TEST_F(LibVintfTest, Stringify) {
38 VendorManifest vm;
39 vm.add(ManifestHal::hal("camera", ImplLevel::SOC, "msm8892",
40 Version(2,0), Transport::HWBINDER));
41 vm.add(ManifestHal::hal("nfc", ImplLevel::GENERIC, "generic",
42 Version(1,0), Transport::PASSTHROUGH));
43 EXPECT_EQ(dump(vm), "hidl/camera/hwbinder/soc/msm8892/2.0:"
44 "hidl/nfc/passthrough/generic/generic/1.0");
45
46 EXPECT_EQ(to_string(HalFormat::HIDL), "hidl");
47 EXPECT_EQ(to_string(HalFormat::NATIVE), "native");
48
49 VersionRange v(1, 2, 3);
50 EXPECT_EQ(to_string(v), "1.2-3");
51 VersionRange v2;
52 EXPECT_TRUE(parse("1.2-3", &v2));
53 EXPECT_EQ(v, v2);
54}
55
56TEST_F(LibVintfTest, VendorManifestConverter) {
57 VendorManifest vm;
58 vm.add(ManifestHal::hal("camera", ImplLevel::SOC, "msm8892",
59 Version(2,0), Transport::HWBINDER));
60 vm.add(ManifestHal::hal("nfc", ImplLevel::GENERIC, "generic",
61 Version(1,0), Transport::PASSTHROUGH));
62 std::string xml = gVendorManifestConverter(vm);
63 EXPECT_EQ(xml,
64 "<manifest version=\"1.0\">\n"
65 " <hal format=\"hidl\">\n"
66 " <name>camera</name>\n"
67 " <transport>hwbinder</transport>\n"
68 " <impl level=\"soc\">msm8892</impl>\n"
69 " <version>2.0</version>\n"
70 " </hal>\n"
71 " <hal format=\"hidl\">\n"
72 " <name>nfc</name>\n"
73 " <transport>passthrough</transport>\n"
74 " <impl level=\"generic\">generic</impl>\n"
75 " <version>1.0</version>\n"
76 " </hal>\n"
77 "</manifest>\n");
78}
79
80TEST_F(LibVintfTest, VersionConverter) {
81 Version v(3, 6);
82 std::string xml = gVersionConverter(v);
83 EXPECT_EQ(xml, "<version>3.6</version>\n");
84 Version v2;
85 EXPECT_TRUE(gVersionConverter(&v2, xml));
86 EXPECT_EQ(v, v2);
87}
88
89TEST_F(LibVintfTest, HalImplementationConverter) {
90 HalImplementation hl{ImplLevel::SOC, "msm8992"};
91 std::string xml = gHalImplementationConverter(hl);
92 EXPECT_EQ(xml, "<impl level=\"soc\">msm8992</impl>\n");
93 HalImplementation hl2;
94 EXPECT_TRUE(gHalImplementationConverter(&hl2, xml));
95 EXPECT_EQ(hl.impl, hl2.impl);
96 EXPECT_EQ(hl.implLevel, hl2.implLevel);
97}
98
99TEST_F(LibVintfTest, MatrixHalConverter) {
100 MatrixHal mh{HalFormat::NATIVE, "camera",
101 {{VersionRange(1,2,3), VersionRange(4,5,6)}},
102 false /* optional */};
103 std::string xml = gMatrixHalConverter(mh);
104 EXPECT_EQ(xml,
105 "<hal format=\"native\" optional=\"false\">\n"
106 " <name>camera</name>\n"
107 " <version>1.2-3</version>\n"
108 " <version>4.5-6</version>\n"
109 "</hal>\n");
110 MatrixHal mh2;
111 EXPECT_TRUE(gMatrixHalConverter(&mh2, xml));
112 EXPECT_EQ(mh, mh2);
113}
114
115TEST_F(LibVintfTest, CompatibilityMatrixCoverter) {
116 CompatibilityMatrix cm;
117 cm.add(MatrixHal{HalFormat::NATIVE, "camera",
118 {{VersionRange(1,2,3), VersionRange(4,5,6)}},
119 false /* optional */});
120 cm.add(MatrixHal{HalFormat::NATIVE, "nfc",
121 {{VersionRange(4,5,6), VersionRange(10,11,12)}},
122 true /* optional */});
123 cm.add(MatrixKernel{Version(3, 18),
124 {{{"CONFIG_FOO"}, {"CONFIG_BAR"}}}});
125 cm.add(MatrixKernel{Version(4, 4),
126 {{{"CONFIG_BAZ"}, {"CONFIG_BAR"}}}});
127 std::string xml = gCompatibilityMatrixConverter(cm);
128 EXPECT_EQ(xml,
129 "<compatibility-matrix version=\"1.0\">\n"
130 " <hal format=\"native\" optional=\"false\">\n"
131 " <name>camera</name>\n"
132 " <version>1.2-3</version>\n"
133 " <version>4.5-6</version>\n"
134 " </hal>\n"
135 " <hal format=\"native\" optional=\"true\">\n"
136 " <name>nfc</name>\n"
137 " <version>4.5-6</version>\n"
138 " <version>10.11-12</version>\n"
139 " </hal>\n"
140 " <kernel version=\"3.18\">\n"
141 " <config>CONFIG_FOO</config>\n"
142 " <config>CONFIG_BAR</config>\n"
143 " </kernel>\n"
144 " <kernel version=\"4.4\">\n"
145 " <config>CONFIG_BAZ</config>\n"
146 " <config>CONFIG_BAR</config>\n"
147 " </kernel>\n"
148 " <sepolicy/>\n"
149 "</compatibility-matrix>\n");
150 CompatibilityMatrix cm2;
151 EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml));
152 EXPECT_EQ(cm.hals, cm2.hals);
153 EXPECT_EQ(cm.kernels, cm2.kernels);
154}
155
156TEST_F(LibVintfTest, IsValid) {
157 EXPECT_TRUE(ManifestHal().isValid());
158 EXPECT_TRUE(VendorManifest().isValid());
159
160 VendorManifest vm;
161 vm.add(ManifestHal::hal("camera", ImplLevel::SOC, "msm8892",
162 Version(2,0), Transport::HWBINDER));
163 vm.add(ManifestHal::hal("nfc", ImplLevel::GENERIC, "generic",
164 Version(1,0), Transport::PASSTHROUGH));
165 EXPECT_TRUE(vm.isValid());
166
167 ManifestHal invalidHal = ManifestHal::hal("camera", ImplLevel::SOC, "msm8892",
168 {{Version(2,0), Version(2,1)}}, Transport::PASSTHROUGH);
169 EXPECT_FALSE(invalidHal.isValid());
170 VendorManifest vm2;
171 vm2.add(std::move(invalidHal));
172 EXPECT_FALSE(vm2.isValid());
173}
174
175int main(int argc, char **argv) {
176 ::testing::InitGoogleTest(&argc, argv);
177 return RUN_ALL_TESTS();
178}