blob: d8fd883ff15c80badecfadebc68e027acf8758ac [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>
Yifan Hongc66ad1e2017-02-08 20:19:45 -080022#include <vintf/KernelInfo.h>
Yifan Hong676447a2016-11-15 12:57:23 -080023#include <vintf/VendorManifest.h>
24
25#include <android-base/logging.h>
Yifan Hong3f5489a2017-02-08 11:14:21 -080026#include <android-base/parseint.h>
Yifan Hong676447a2016-11-15 12:57:23 -080027#include <gtest/gtest.h>
28
Yifan Honga9993572017-01-24 19:33:15 -080029namespace android {
30namespace vintf {
Yifan Hong676447a2016-11-15 12:57:23 -080031
Yifan Honga9993572017-01-24 19:33:15 -080032struct LibVintfTest : public ::testing::Test {
Yifan Hong676447a2016-11-15 12:57:23 -080033public:
34 virtual void SetUp() override {
35 }
36 virtual void TearDown() override {
37 }
Yifan Honga9993572017-01-24 19:33:15 -080038 bool add(CompatibilityMatrix &cm, MatrixHal &&hal) {
39 return cm.add(std::move(hal));
40 }
41 bool add(CompatibilityMatrix &cm, MatrixKernel &&kernel) {
42 return cm.add(std::move(kernel));
43 }
44 bool add(VendorManifest &vm, ManifestHal &&hal) {
45 return vm.add(std::move(hal));
46 }
Yifan Hong558380a2017-02-09 15:37:32 -080047 void set(CompatibilityMatrix &cm, Sepolicy &&sepolicy) {
Yifan Hongec342862017-02-16 17:57:06 -080048 cm.mSepolicy = sepolicy;
Yifan Hong558380a2017-02-09 15:37:32 -080049 }
Yifan Honga9993572017-01-24 19:33:15 -080050 const ManifestHal *getHal(VendorManifest &vm, const std::string &name) {
51 return vm.getHal(name);
52 }
53 ConstMapValueIterable<std::string, ManifestHal> getHals(VendorManifest &vm) {
54 return vm.getHals();
55 }
56 bool isEqual(const CompatibilityMatrix &cm1, const CompatibilityMatrix &cm2) {
Yifan Hongec342862017-02-16 17:57:06 -080057 return cm1.mHals == cm2.mHals && cm1.mKernels == cm2.mKernels;
Yifan Honga9993572017-01-24 19:33:15 -080058 }
Yifan Hong5a06ef72017-01-24 19:54:24 -080059 bool isValid(const ManifestHal &mh) {
60 return mh.isValid();
61 }
Yifan Honga9993572017-01-24 19:33:15 -080062 VendorManifest testVendorManifest() {
63 VendorManifest vm;
64 vm.add(ManifestHal::hal("android.hardware.camera", ImplLevel::SOC, "msm8892",
65 Version(2,0), Transport::HWBINDER));
66 vm.add(ManifestHal::hal("android.hardware.nfc", ImplLevel::GENERIC, "generic",
67 Version(1,0), Transport::PASSTHROUGH));
68
69 return vm;
70 }
Yifan Hongc66ad1e2017-02-08 20:19:45 -080071 KernelInfo testKernelInfo() {
72 KernelInfo info;
73 info.mOsName = "Linux";
74 info.mNodeName = "localhost";
75 info.mOsRelease = "3.18.31-g936f9a479d0f";
76 info.mKernelVersion = {3, 18, 31};
77 info.mOsVersion = "#4 SMP PREEMPT Wed Feb 1 18:10:52 PST 2017";
78 info.mHardwareId = "aarch64";
79 info.mKernelSepolicyVersion = 30;
Yifan Hongf1af7522017-02-16 18:00:55 -080080 info.mKernelConfigs = {
Yifan Hongc66ad1e2017-02-08 20:19:45 -080081 {"CONFIG_64BIT", "y"},
82 {"CONFIG_ANDROID_BINDER_DEVICES", "\"binder,hwbinder\""},
83 {"CONFIG_ARCH_MMAP_RND_BITS", "24"},
84 {"CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES", "\"\""},
85 {"CONFIG_ILLEGAL_POINTER_VALUE", "0xdead000000000000"}
86 };
87 return info;
88 }
Yifan Hong676447a2016-11-15 12:57:23 -080089};
90
Yifan Hongef6d4d32017-01-23 14:12:28 -080091
92TEST_F(LibVintfTest, Stringify) {
93 VendorManifest vm = testVendorManifest();
94 EXPECT_EQ(dump(vm), "hidl/android.hardware.camera/hwbinder/soc/msm8892/2.0:"
95 "hidl/android.hardware.nfc/passthrough/generic/generic/1.0");
Yifan Hong676447a2016-11-15 12:57:23 -080096
97 EXPECT_EQ(to_string(HalFormat::HIDL), "hidl");
98 EXPECT_EQ(to_string(HalFormat::NATIVE), "native");
99
100 VersionRange v(1, 2, 3);
101 EXPECT_EQ(to_string(v), "1.2-3");
102 VersionRange v2;
103 EXPECT_TRUE(parse("1.2-3", &v2));
104 EXPECT_EQ(v, v2);
105}
106
107TEST_F(LibVintfTest, VendorManifestConverter) {
Yifan Hongef6d4d32017-01-23 14:12:28 -0800108 VendorManifest vm = testVendorManifest();
Yifan Hong676447a2016-11-15 12:57:23 -0800109 std::string xml = gVendorManifestConverter(vm);
110 EXPECT_EQ(xml,
111 "<manifest version=\"1.0\">\n"
112 " <hal format=\"hidl\">\n"
Yifan Hongef6d4d32017-01-23 14:12:28 -0800113 " <name>android.hardware.camera</name>\n"
Yifan Hong676447a2016-11-15 12:57:23 -0800114 " <transport>hwbinder</transport>\n"
115 " <impl level=\"soc\">msm8892</impl>\n"
116 " <version>2.0</version>\n"
117 " </hal>\n"
118 " <hal format=\"hidl\">\n"
Yifan Hongef6d4d32017-01-23 14:12:28 -0800119 " <name>android.hardware.nfc</name>\n"
Yifan Hong676447a2016-11-15 12:57:23 -0800120 " <transport>passthrough</transport>\n"
121 " <impl level=\"generic\">generic</impl>\n"
122 " <version>1.0</version>\n"
123 " </hal>\n"
124 "</manifest>\n");
125}
126
127TEST_F(LibVintfTest, VersionConverter) {
128 Version v(3, 6);
129 std::string xml = gVersionConverter(v);
130 EXPECT_EQ(xml, "<version>3.6</version>\n");
131 Version v2;
132 EXPECT_TRUE(gVersionConverter(&v2, xml));
133 EXPECT_EQ(v, v2);
134}
135
136TEST_F(LibVintfTest, HalImplementationConverter) {
137 HalImplementation hl{ImplLevel::SOC, "msm8992"};
138 std::string xml = gHalImplementationConverter(hl);
139 EXPECT_EQ(xml, "<impl level=\"soc\">msm8992</impl>\n");
140 HalImplementation hl2;
141 EXPECT_TRUE(gHalImplementationConverter(&hl2, xml));
142 EXPECT_EQ(hl.impl, hl2.impl);
143 EXPECT_EQ(hl.implLevel, hl2.implLevel);
144}
145
146TEST_F(LibVintfTest, MatrixHalConverter) {
Yifan Hongef6d4d32017-01-23 14:12:28 -0800147 MatrixHal mh{HalFormat::NATIVE, "android.hardware.camera",
Yifan Hong676447a2016-11-15 12:57:23 -0800148 {{VersionRange(1,2,3), VersionRange(4,5,6)}},
149 false /* optional */};
150 std::string xml = gMatrixHalConverter(mh);
151 EXPECT_EQ(xml,
152 "<hal format=\"native\" optional=\"false\">\n"
Yifan Hongef6d4d32017-01-23 14:12:28 -0800153 " <name>android.hardware.camera</name>\n"
Yifan Hong676447a2016-11-15 12:57:23 -0800154 " <version>1.2-3</version>\n"
155 " <version>4.5-6</version>\n"
156 "</hal>\n");
157 MatrixHal mh2;
158 EXPECT_TRUE(gMatrixHalConverter(&mh2, xml));
159 EXPECT_EQ(mh, mh2);
160}
161
Yifan Hong3f5489a2017-02-08 11:14:21 -0800162TEST_F(LibVintfTest, KernelConfigTypedValueConverter) {
163
164 KernelConfigTypedValue converted;
165
166 auto testOne = [] (const KernelConfigTypedValue &original,
167 const std::string &expectXml) {
168 std::string xml;
169 KernelConfigTypedValue converted;
170 xml = gKernelConfigTypedValueConverter(original);
171 EXPECT_EQ(xml, expectXml);
172 EXPECT_TRUE(gKernelConfigTypedValueConverter(&converted, xml));
173 EXPECT_EQ(original, converted);
174 };
175
176 auto testParse = [] (const KernelConfigTypedValue &original,
177 const std::string &xml) {
178 KernelConfigTypedValue converted;
179 EXPECT_TRUE(gKernelConfigTypedValueConverter(&converted, xml));
180 EXPECT_EQ(original, converted);
181 };
182
183 testOne(KernelConfigTypedValue("stringvalue"),
184 "<value type=\"string\">stringvalue</value>\n");
185 testOne(KernelConfigTypedValue(""),
186 "<value type=\"string\"></value>\n");
187
188 testOne(KernelConfigTypedValue(Tristate::YES),
189 "<value type=\"tristate\">y</value>\n");
190 testOne(KernelConfigTypedValue(Tristate::NO),
191 "<value type=\"tristate\">n</value>\n");
192 testOne(KernelConfigTypedValue(Tristate::MODULE),
193 "<value type=\"tristate\">m</value>\n");
194 EXPECT_FALSE(gKernelConfigTypedValueConverter(&converted,
195 "<value type=\"tristate\">q</value>\n"));
196
197 testOne(KernelConfigTypedValue(KernelConfigRangeValue{4, 20}),
198 "<value type=\"range\">4-20</value>\n");
199 testOne(KernelConfigTypedValue(KernelConfigRangeValue{0, UINT64_MAX}),
200 "<value type=\"range\">0-18446744073709551615</value>\n");
201 testParse(KernelConfigTypedValue(KernelConfigRangeValue{0, UINT64_MAX}),
202 "<value type=\"range\">0x0-0xffffffffffffffff</value>\n");
203
204 EXPECT_FALSE(gKernelConfigTypedValueConverter(&converted,
205 "<value type=\"int\">-18446744073709551616</value>\n"));
206
207 testOne(KernelConfigTypedValue(INT64_MIN),
208 "<value type=\"int\">-9223372036854775808</value>\n");
209 testParse(KernelConfigTypedValue(INT64_MIN),
210 "<value type=\"int\">0x8000000000000000</value>\n");
211 testParse(KernelConfigTypedValue(INT64_MIN),
212 "<value type=\"int\">-0X8000000000000000</value>\n");
213
214 testParse(KernelConfigTypedValue(INT64_MIN + 1),
215 "<value type=\"int\">-0X7FFFFFFFFFFFFFFF</value>\n");
216
217 testParse(KernelConfigTypedValue(-0x50),
218 "<value type=\"int\">-0x50</value>\n");
219
220 testOne(KernelConfigTypedValue(0),
221 "<value type=\"int\">0</value>\n");
222
223 // Truncation for underflow.
224 testParse(KernelConfigTypedValue(1),
225 "<value type=\"int\">-0xffffffffffffffff</value>\n");
226 testParse(KernelConfigTypedValue(1),
227 "<value type=\"int\">-18446744073709551615</value>\n");
228
229 testOne(KernelConfigTypedValue(INT64_MAX),
230 "<value type=\"int\">9223372036854775807</value>\n");
231 testParse(KernelConfigTypedValue(INT64_MAX),
232 "<value type=\"int\">0x7FFFFFFFFFFFFFFF</value>\n");
233 // Truncation for underflow.
234 testParse(KernelConfigTypedValue(INT64_MAX),
235 "<value type=\"int\">-9223372036854775809</value>\n");
236
237 testParse(KernelConfigTypedValue(-1),
238 "<value type=\"int\">18446744073709551615</value>\n");
239 testParse(KernelConfigTypedValue(-1),
240 "<value type=\"int\">0xffffffffffffffff</value>\n");
241
242 EXPECT_FALSE(gKernelConfigTypedValueConverter(&converted,
243 "<value type=\"int\">18446744073709551616</value>\n"));
244}
245
Yifan Hong676447a2016-11-15 12:57:23 -0800246TEST_F(LibVintfTest, CompatibilityMatrixCoverter) {
247 CompatibilityMatrix cm;
Yifan Honga9993572017-01-24 19:33:15 -0800248 EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE, "android.hardware.camera",
Yifan Hong676447a2016-11-15 12:57:23 -0800249 {{VersionRange(1,2,3), VersionRange(4,5,6)}},
Yifan Honga9993572017-01-24 19:33:15 -0800250 false /* optional */}));
251 EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE, "android.hardware.nfc",
Yifan Hong676447a2016-11-15 12:57:23 -0800252 {{VersionRange(4,5,6), VersionRange(10,11,12)}},
Yifan Honga9993572017-01-24 19:33:15 -0800253 true /* optional */}));
Yifan Hong3f5489a2017-02-08 11:14:21 -0800254 EXPECT_TRUE(add(cm, MatrixKernel{KernelVersion(3, 18, 22),
255 {KernelConfig{"CONFIG_FOO", Tristate::YES}, KernelConfig{"CONFIG_BAR", "stringvalue"}}}));
256 EXPECT_TRUE(add(cm, MatrixKernel{KernelVersion(4, 4, 1),
257 {KernelConfig{"CONFIG_BAZ", 20}, KernelConfig{"CONFIG_BAR", KernelConfigRangeValue{3, 5} }}}));
Yifan Hong558380a2017-02-09 15:37:32 -0800258 set(cm, Sepolicy(30, {1, 3}));
Yifan Hong676447a2016-11-15 12:57:23 -0800259 std::string xml = gCompatibilityMatrixConverter(cm);
260 EXPECT_EQ(xml,
Yifan Hong3f5489a2017-02-08 11:14:21 -0800261 "<compatibility-matrix version=\"1.0\">\n"
262 " <hal format=\"native\" optional=\"false\">\n"
263 " <name>android.hardware.camera</name>\n"
264 " <version>1.2-3</version>\n"
265 " <version>4.5-6</version>\n"
266 " </hal>\n"
267 " <hal format=\"native\" optional=\"true\">\n"
268 " <name>android.hardware.nfc</name>\n"
269 " <version>4.5-6</version>\n"
270 " <version>10.11-12</version>\n"
271 " </hal>\n"
272 " <kernel version=\"3.18\" minlts=\"3.18.22\">\n"
273 " <config>\n"
274 " <key>CONFIG_FOO</key>\n"
275 " <value type=\"tristate\">y</value>\n"
276 " </config>\n"
277 " <config>\n"
278 " <key>CONFIG_BAR</key>\n"
279 " <value type=\"string\">stringvalue</value>\n"
280 " </config>\n"
281 " </kernel>\n"
282 " <kernel version=\"4.4\" minlts=\"4.4.1\">\n"
283 " <config>\n"
284 " <key>CONFIG_BAZ</key>\n"
285 " <value type=\"int\">20</value>\n"
286 " </config>\n"
287 " <config>\n"
288 " <key>CONFIG_BAR</key>\n"
289 " <value type=\"range\">3-5</value>\n"
290 " </config>\n"
291 " </kernel>\n"
Yifan Hong558380a2017-02-09 15:37:32 -0800292 " <sepolicy>\n"
293 " <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
294 " <sepolicy-version>1-3</sepolicy-version>\n"
295 " </sepolicy>\n"
Yifan Hong3f5489a2017-02-08 11:14:21 -0800296 "</compatibility-matrix>\n");
Yifan Hong676447a2016-11-15 12:57:23 -0800297 CompatibilityMatrix cm2;
298 EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml));
Yifan Honga9993572017-01-24 19:33:15 -0800299 EXPECT_TRUE(isEqual(cm, cm2));
Yifan Hong676447a2016-11-15 12:57:23 -0800300}
301
302TEST_F(LibVintfTest, IsValid) {
Yifan Hong5a06ef72017-01-24 19:54:24 -0800303 EXPECT_TRUE(isValid(ManifestHal()));
Yifan Hong676447a2016-11-15 12:57:23 -0800304
Yifan Hongef6d4d32017-01-23 14:12:28 -0800305 ManifestHal invalidHal = ManifestHal::hal("android.hardware.camera", ImplLevel::SOC, "msm8892",
Yifan Hong676447a2016-11-15 12:57:23 -0800306 {{Version(2,0), Version(2,1)}}, Transport::PASSTHROUGH);
Yifan Hong5a06ef72017-01-24 19:54:24 -0800307 EXPECT_FALSE(isValid(invalidHal));
Yifan Hong676447a2016-11-15 12:57:23 -0800308 VendorManifest vm2;
Yifan Hong5a06ef72017-01-24 19:54:24 -0800309 EXPECT_FALSE(add(vm2, std::move(invalidHal)));
Yifan Hong676447a2016-11-15 12:57:23 -0800310}
311
Yifan Hongef6d4d32017-01-23 14:12:28 -0800312TEST_F(LibVintfTest, VendorManifestGetHal) {
313 VendorManifest vm = testVendorManifest();
Yifan Honga9993572017-01-24 19:33:15 -0800314 EXPECT_NE(getHal(vm, "android.hardware.camera"), nullptr);
315 EXPECT_EQ(getHal(vm, "non-existent"), nullptr);
Yifan Hongef6d4d32017-01-23 14:12:28 -0800316
317 std::vector<std::string> arr{"android.hardware.camera", "android.hardware.nfc"};
318 size_t i = 0;
Yifan Honga9993572017-01-24 19:33:15 -0800319 for (const auto &hal : getHals(vm)) {
Yifan Hongef6d4d32017-01-23 14:12:28 -0800320 EXPECT_EQ(hal.name, arr[i++]);
321 }
322}
323
Yifan Hongc66ad1e2017-02-08 20:19:45 -0800324TEST_F(LibVintfTest, KernelInfo) {
325 KernelInfo ki = testKernelInfo();
326 using KernelConfigs = std::vector<KernelConfig>;
327 const KernelConfigs configs {
328 KernelConfig{"CONFIG_64BIT", Tristate::YES},
329 KernelConfig{"CONFIG_ANDROID_BINDER_DEVICES", "binder,hwbinder"},
330 KernelConfig{"CONFIG_ARCH_MMAP_RND_BITS", 24},
331 KernelConfig{"CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES", ""},
332 KernelConfig{"CONFIG_ILLEGAL_POINTER_VALUE", 0xdead000000000000},
333 KernelConfig{"CONFIG_NOTEXIST", Tristate::NO},
334 };
335
336 auto testMatrix = [&] (MatrixKernel &&kernel) {
337 CompatibilityMatrix cm;
338 add(cm, std::move(kernel));
339 set(cm, {30, {1, 3}});
340 return cm;
341 };
342
343 std::string error;
344
345 {
346 MatrixKernel kernel(KernelVersion{4, 4, 1}, KernelConfigs(configs));
347 CompatibilityMatrix cm = testMatrix(std::move(kernel));
348 EXPECT_FALSE(ki.checkCompatibility(cm)) << "Kernel version shouldn't match";
349 }
350
351 {
352 MatrixKernel kernel(KernelVersion{3, 18, 22}, KernelConfigs(configs));
353 CompatibilityMatrix cm = testMatrix(std::move(kernel));
354 EXPECT_TRUE(ki.checkCompatibility(cm, &error)) << error;
355 }
356
357 {
358 MatrixKernel kernel(KernelVersion{3, 18, 22}, KernelConfigs(configs));
359 CompatibilityMatrix cm = testMatrix(std::move(kernel));
360 set(cm, Sepolicy{22, {1, 3}});
361 EXPECT_FALSE(ki.checkCompatibility(cm, &error))
362 << "kernel-sepolicy-version shouldn't match";
363 set(cm, Sepolicy{40, {1, 3}});
364 EXPECT_FALSE(ki.checkCompatibility(cm, &error))
365 << "kernel-sepolicy-version shouldn't match";
366 }
367
368 {
369 KernelConfigs newConfigs(configs);
370 newConfigs[0] = KernelConfig{"CONFIG_64BIT", Tristate::NO};
371 MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
372 CompatibilityMatrix cm = testMatrix(std::move(kernel));
373 EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for tristate";
374 }
375
376 {
377 KernelConfigs newConfigs(configs);
378 newConfigs[0] = KernelConfig{"CONFIG_64BIT", 20};
379 MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
380 CompatibilityMatrix cm = testMatrix(std::move(kernel));
381 EXPECT_FALSE(ki.checkCompatibility(cm)) << "Type shouldn't match";
382 }
383
384 {
385 KernelConfigs newConfigs(configs);
386 newConfigs[1] = KernelConfig{"CONFIG_ANDROID_BINDER_DEVICES", "binder"};
387 MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
388 CompatibilityMatrix cm = testMatrix(std::move(kernel));
389 EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for string";
390 }
391
392 {
393 KernelConfigs newConfigs(configs);
394 newConfigs[1] = KernelConfig{"CONFIG_ANDROID_BINDER_DEVICES", Tristate::YES};
395 MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
396 CompatibilityMatrix cm = testMatrix(std::move(kernel));
397 EXPECT_FALSE(ki.checkCompatibility(cm)) << "Type shouldn't match";
398 }
399
400 {
401 KernelConfigs newConfigs(configs);
402 newConfigs[2] = KernelConfig{"CONFIG_ARCH_MMAP_RND_BITS", 30};
403 MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
404 CompatibilityMatrix cm = testMatrix(std::move(kernel));
405 EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for integer";
406 }
407}
408
Yifan Honga9993572017-01-24 19:33:15 -0800409} // namespace vintf
410} // namespace android
411
Yifan Hong676447a2016-11-15 12:57:23 -0800412int main(int argc, char **argv) {
413 ::testing::InitGoogleTest(&argc, argv);
414 return RUN_ALL_TESTS();
415}