1 /*
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 "LibVintfTest"
18 
19 #include <algorithm>
20 #include <functional>
21 
22 #include <android-base/logging.h>
23 #include <android-base/parseint.h>
24 #include <android-base/strings.h>
25 #include <gtest/gtest.h>
26 
27 #include <vintf/CompatibilityMatrix.h>
28 #include <vintf/KernelConfigParser.h>
29 #include <vintf/VintfObject.h>
30 #include <vintf/parse_string.h>
31 #include <vintf/parse_xml.h>
32 #include "constants-private.h"
33 #include "test_constants.h"
34 
35 namespace android {
36 namespace vintf {
37 
38 extern XmlConverter<Version>& gVersionConverter;
39 extern XmlConverter<ManifestHal>& gManifestHalConverter;
40 extern XmlConverter<MatrixHal>& gMatrixHalConverter;
41 extern XmlConverter<KernelConfigTypedValue>& gKernelConfigTypedValueConverter;
42 extern XmlConverter<KernelInfo>& gKernelInfoConverter;
43 extern XmlConverter<HalManifest>& gHalManifestConverter;
44 extern XmlConverter<CompatibilityMatrix>& gCompatibilityMatrixConverter;
45 
46 static bool In(const std::string& sub, const std::string& str) {
47     return str.find(sub) != std::string::npos;
48 }
49 #define EXPECT_IN(sub, str) EXPECT_TRUE(In((sub), (str))) << (str);
50 
51 #ifndef LIBVINTF_TARGET
52 #define EXPECT_CONTAINS(str, sub) EXPECT_IN(sub, str);
53 #endif
54 
55 struct LibVintfTest : public ::testing::Test {
56 public:
57     virtual void SetUp() override {
58     }
59     virtual void TearDown() override {
60     }
61     bool add(CompatibilityMatrix &cm, MatrixHal &&hal) {
62         return cm.add(std::move(hal));
63     }
64     bool add(CompatibilityMatrix &cm, MatrixKernel &&kernel) {
65         std::string error;
66         bool success = cm.addKernel(std::move(kernel), &error);
67         EXPECT_EQ(success, error == "") << "success: " << success << ", error: " << error;
68         return success;
69     }
70     bool add(HalManifest &vm, ManifestHal &&hal) {
71         return vm.add(std::move(hal));
72     }
73     void addXmlFile(CompatibilityMatrix& cm, std::string name, VersionRange range) {
74         MatrixXmlFile f;
75         f.mName = name;
76         f.mVersionRange = range;
77         f.mFormat = XmlSchemaFormat::DTD;
78         f.mOptional = true;
79         cm.addXmlFile(std::move(f));
80     }
81     void set(CompatibilityMatrix &cm, Sepolicy &&sepolicy) {
82         cm.framework.mSepolicy = sepolicy;
83     }
84     void set(CompatibilityMatrix &cm, SchemaType type) {
85         cm.mType = type;
86     }
87     void set(CompatibilityMatrix &cm, VndkVersionRange &&range, std::set<std::string> &&libs) {
88         cm.device.mVndk.mVersionRange = range;
89         cm.device.mVndk.mLibraries = libs;
90     }
91     void setAvb(RuntimeInfo &ki, Version vbmeta, Version boot) {
92         ki.mBootVbmetaAvbVersion = vbmeta;
93         ki.mBootAvbVersion = boot;
94     }
95     void setAvb(CompatibilityMatrix &cm, Version &&avbVersion) {
96         cm.framework.mAvbMetaVersion = avbVersion;
97     }
98     Version getAvb(CompatibilityMatrix &cm) {
99         return cm.framework.mAvbMetaVersion;
100     }
101     const ManifestHal *getAnyHal(HalManifest &vm, const std::string &name) {
102         return vm.getAnyHal(name);
103     }
104     MatrixHal *getAnyHal(CompatibilityMatrix &cm, const std::string &name) {
105         return cm.getAnyHal(name);
106     }
107     ConstMultiMapValueIterable<std::string, ManifestHal> getHals(const HalManifest& vm) {
108         return vm.getHals();
109     }
110     std::vector<const ManifestHal*> getHals(const HalManifest& vm, const std::string& name) {
111         return vm.getHals(name);
112     }
113     std::vector<const MatrixHal*> getHals(const CompatibilityMatrix& cm, const std::string& name) {
114         return cm.getHals(name);
115     }
116     bool isValid(const ManifestHal &mh) {
117         return mh.isValid();
118     }
119     std::vector<MatrixKernel>& getKernels(CompatibilityMatrix& cm) { return cm.framework.mKernels; }
120     bool addAllHalsAsOptional(CompatibilityMatrix* cm1, CompatibilityMatrix* cm2, std::string* e) {
121         return cm1->addAllHalsAsOptional(cm2, e);
122     }
123     bool addAllXmlFilesAsOptional(CompatibilityMatrix* cm1, CompatibilityMatrix* cm2,
124                                   std::string* e) {
125         return cm1->addAllXmlFilesAsOptional(cm2, e);
126     }
127     std::set<std::string> checkUnusedHals(const HalManifest& m, const CompatibilityMatrix& cm) {
128         return m.checkUnusedHals(cm, {});
129     }
130 
131     std::map<std::string, HalInterface> testHalInterfaces() {
132         HalInterface intf("IFoo", {"default"});
133         std::map<std::string, HalInterface> map;
134         map[intf.name()] = intf;
135         return map;
136     }
137 
138     HalManifest testDeviceManifest() {
139         HalManifest vm;
140         vm.mType = SchemaType::DEVICE;
141         vm.device.mSepolicyVersion = {25, 0};
142         vm.add(ManifestHal{HalFormat::HIDL,
143                            "android.hardware.camera",
144                            {Version(2, 0)},
145                            {Transport::HWBINDER, Arch::ARCH_EMPTY},
146                            {{"ICamera", {"ICamera", {"legacy/0", "default"}}},
147                             {"IBetterCamera", {"IBetterCamera", {"camera"}}}}});
148         vm.add(ManifestHal{HalFormat::HIDL,
149                            "android.hardware.nfc",
150                            {Version(1, 0)},
151                            {Transport::PASSTHROUGH, Arch::ARCH_32_64},
152                            {{"INfc", {"INfc", {"default"}}}}});
153 
154         return vm;
155     }
156     HalManifest testDeviceManifestWithXmlFile() {
157         HalManifest vm = testDeviceManifest();
158         ManifestXmlFile xmlFile;
159         xmlFile.mName = "media_profile";
160         xmlFile.mVersion = {1, 0};
161         vm.addXmlFile(std::move(xmlFile));
162         return vm;
163     }
164     HalManifest testFrameworkManfiest() {
165         HalManifest vm;
166         vm.mType = SchemaType::FRAMEWORK;
167         vm.add(ManifestHal{HalFormat::HIDL,
168                            "android.hidl.manager",
169                            {Version(1, 0)},
170                            {Transport::HWBINDER, Arch::ARCH_EMPTY},
171                            {
172                                {"IServiceManager", {"IServiceManager", {"default"}}},
173                            }});
174         Vndk vndk2505;
175         vndk2505.mVersionRange = {25, 0, 5};
176         vndk2505.mLibraries = { "libjpeg.so", "libbase.so" };
177         Vndk vndk2513;
178         vndk2513.mVersionRange = {25, 1, 3};
179         vndk2513.mLibraries = { "libjpeg.so", "libbase.so", "libtinyxml2.so" };
180         vm.framework.mVndks = { std::move(vndk2505), std::move(vndk2513) };
181 
182         return vm;
183     }
184     RuntimeInfo testRuntimeInfo() {
185         RuntimeInfo info;
186         info.mOsName = "Linux";
187         info.mNodeName = "localhost";
188         info.mOsRelease = "3.18.31-g936f9a479d0f";
189         info.mOsVersion = "#4 SMP PREEMPT Wed Feb 1 18:10:52 PST 2017";
190         info.mHardwareId = "aarch64";
191         info.mKernelSepolicyVersion = 30;
192         info.mKernel = testKernelInfo();
193         setAvb(info, {2, 1}, {2, 1});
194         return info;
195     }
196     KernelInfo testKernelInfo() {
197         KernelInfo info;
198         info.mVersion = {3, 18, 31};
199         info.mConfigs = {{"CONFIG_64BIT", "y"},
200                          {"CONFIG_ANDROID_BINDER_DEVICES", "\"binder,hwbinder\""},
201                          {"CONFIG_ARCH_MMAP_RND_BITS", "24"},
202                          {"CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES", "\"\""},
203                          {"CONFIG_ILLEGAL_POINTER_VALUE", "0xdead000000000000"}};
204         return info;
205     }
206 };
207 
208 // clang-format off
209 
210 TEST_F(LibVintfTest, ArchOperatorOr) {
211     Arch a = Arch::ARCH_EMPTY;
212     a |= Arch::ARCH_32;
213     EXPECT_EQ(Arch::ARCH_32, a);
214 
215     a |= Arch::ARCH_64;
216     EXPECT_EQ(Arch::ARCH_32_64, a);
217 
218     a = Arch::ARCH_EMPTY;
219     a |= Arch::ARCH_64;
220     EXPECT_EQ(Arch::ARCH_64, a);
221 }
222 
223 TEST_F(LibVintfTest, Stringify) {
224     HalManifest vm = testDeviceManifest();
225     EXPECT_EQ(dump(vm), "hidl/android.hardware.camera/hwbinder/2.0:"
226                         "hidl/android.hardware.nfc/passthrough32+64/1.0");
227 
228     EXPECT_EQ(to_string(HalFormat::HIDL), "hidl");
229     EXPECT_EQ(to_string(HalFormat::NATIVE), "native");
230 
231     VersionRange v(1, 2, 3);
232     EXPECT_EQ(to_string(v), "1.2-3");
233     VersionRange v2;
234     EXPECT_TRUE(parse("1.2-3", &v2));
235     EXPECT_EQ(v, v2);
236 }
237 
238 TEST_F(LibVintfTest, GetTransport) {
239     HalManifest vm = testDeviceManifest();
240     EXPECT_EQ(Transport::HWBINDER, vm.getHidlTransport("android.hardware.camera",
241             {2, 0}, "ICamera", "default"));
242 }
243 
244 TEST_F(LibVintfTest, FutureManifestCompatible) {
245     HalManifest expectedManifest;
246     expectedManifest.add(ManifestHal{HalFormat::HIDL,
247                                      "android.hardware.foo",
248                                      {Version(1, 0)},
249                                      {Transport::HWBINDER, Arch::ARCH_EMPTY},
250                                      {
251                                          {"IFoo", {"IFoo", {"default"}}},
252                                      }});
253     std::string manifestXml =
254         "<manifest " + kMetaVersionStr + " type=\"device\" might_add=\"true\">\n"
255         "    <hal format=\"hidl\" attribuet_might_be_added=\"value\">\n"
256         "        <name>android.hardware.foo</name>\n"
257         "        <transport>hwbinder</transport>\n"
258         "        <version>1.0</version>\n"
259         "        <interface>\n"
260         "            <name>IFoo</name>\n"
261         "            <instance>default</instance>\n"
262         "        </interface>\n"
263         "    </hal>\n"
264         "    <tag_might_be_added/>\n"
265         "</manifest>\n";
266     HalManifest manifest;
267     EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
268     EXPECT_EQ(expectedManifest, manifest);
269 }
270 
271 TEST_F(LibVintfTest, HalManifestConverter) {
272     HalManifest vm = testDeviceManifest();
273     std::string xml =
274         gHalManifestConverter(vm, SerializeFlags::NO_TAGS.enableHals().enableSepolicy());
275     EXPECT_EQ(xml,
276         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
277         "    <hal format=\"hidl\">\n"
278         "        <name>android.hardware.camera</name>\n"
279         "        <transport>hwbinder</transport>\n"
280         "        <version>2.0</version>\n"
281         "        <interface>\n"
282         "            <name>IBetterCamera</name>\n"
283         "            <instance>camera</instance>\n"
284         "        </interface>\n"
285         "        <interface>\n"
286         "            <name>ICamera</name>\n"
287         "            <instance>default</instance>\n"
288         "            <instance>legacy/0</instance>\n"
289         "        </interface>\n"
290         "    </hal>\n"
291         "    <hal format=\"hidl\">\n"
292         "        <name>android.hardware.nfc</name>\n"
293         "        <transport arch=\"32+64\">passthrough</transport>\n"
294         "        <version>1.0</version>\n"
295         "        <interface>\n"
296         "            <name>INfc</name>\n"
297         "            <instance>default</instance>\n"
298         "        </interface>\n"
299         "    </hal>\n"
300         "    <sepolicy>\n"
301         "        <version>25.0</version>\n"
302         "    </sepolicy>\n"
303         "</manifest>\n");
304     HalManifest vm2;
305     EXPECT_TRUE(gHalManifestConverter(&vm2, xml));
306     EXPECT_EQ(vm, vm2);
307 }
308 
309 TEST_F(LibVintfTest, HalManifestConverterFramework) {
310     HalManifest vm = testFrameworkManfiest();
311     std::string xml = gHalManifestConverter(vm, SerializeFlags::NO_TAGS.enableHals().enableVndk());
312     EXPECT_EQ(xml,
313         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
314         "    <hal format=\"hidl\">\n"
315         "        <name>android.hidl.manager</name>\n"
316         "        <transport>hwbinder</transport>\n"
317         "        <version>1.0</version>\n"
318         "        <interface>\n"
319         "            <name>IServiceManager</name>\n"
320         "            <instance>default</instance>\n"
321         "        </interface>\n"
322         "    </hal>\n"
323         "    <vndk>\n"
324         "        <version>25.0.5</version>\n"
325         "        <library>libbase.so</library>\n"
326         "        <library>libjpeg.so</library>\n"
327         "    </vndk>\n"
328         "    <vndk>\n"
329         "        <version>25.1.3</version>\n"
330         "        <library>libbase.so</library>\n"
331         "        <library>libjpeg.so</library>\n"
332         "        <library>libtinyxml2.so</library>\n"
333         "    </vndk>\n"
334         "</manifest>\n");
335     HalManifest vm2;
336     EXPECT_TRUE(gHalManifestConverter(&vm2, xml));
337     EXPECT_EQ(vm, vm2);
338 }
339 
340 TEST_F(LibVintfTest, HalManifestOptional) {
341     HalManifest vm;
342     EXPECT_TRUE(gHalManifestConverter(&vm,
343             "<manifest " + kMetaVersionStr + " type=\"device\"></manifest>"));
344     EXPECT_TRUE(gHalManifestConverter(&vm,
345             "<manifest " + kMetaVersionStr + " type=\"device\">"
346             "    <hal>"
347             "        <name>android.hidl.manager</name>"
348             "        <transport>hwbinder</transport>"
349             "        <version>1.0</version>"
350             "    </hal>"
351             "</manifest>"));
352     EXPECT_FALSE(gHalManifestConverter(&vm,
353             "<manifest " + kMetaVersionStr + " type=\"device\">"
354             "    <hal>"
355             "        <name>android.hidl.manager</name>"
356             "        <version>1.0</version>"
357             "    </hal>"
358             "</manifest>"));
359 }
360 
361 TEST_F(LibVintfTest, HalManifestNative) {
362     HalManifest vm;
363     EXPECT_TRUE(gHalManifestConverter(&vm,
364                                       "<manifest " + kMetaVersionStr + " type=\"device\">"
365                                       "    <hal format=\"native\">"
366                                       "        <name>foo</name>"
367                                       "        <version>1.0</version>"
368                                       "    </hal>"
369                                       "</manifest>"))
370         << gHalManifestConverter.lastError();
371     EXPECT_FALSE(gHalManifestConverter(&vm,
372                                        "<manifest " + kMetaVersionStr + " type=\"device\">"
373                                        "    <hal format=\"native\">"
374                                        "        <name>foo</name>"
375                                        "        <version>1.0</version>"
376                                        "        <transport>hwbinder</transport>"
377                                        "    </hal>"
378                                        "</manifest>"));
379     EXPECT_TRUE(gHalManifestConverter.lastError().find(
380                     "Native HAL 'foo' should not have <transport> defined") != std::string::npos);
381 }
382 
383 TEST_F(LibVintfTest, HalManifestDuplicate) {
384     HalManifest vm;
385     EXPECT_FALSE(gHalManifestConverter(&vm,
386                                        "<manifest " + kMetaVersionStr + " type=\"device\">"
387                                        "    <hal>"
388                                        "        <name>android.hidl.manager</name>"
389                                        "        <transport>hwbinder</transport>"
390                                        "        <version>1.0</version>"
391                                        "        <version>1.1</version>"
392                                        "    </hal>"
393                                        "</manifest>"))
394         << "Should not allow duplicated major version in <hal>";
395     EXPECT_FALSE(gHalManifestConverter(&vm,
396                                        "<manifest " + kMetaVersionStr + " type=\"device\">"
397                                        "    <hal>"
398                                        "        <name>android.hidl.manager</name>"
399                                        "        <transport>hwbinder</transport>"
400                                        "        <version>1.0</version>"
401                                        "    </hal>"
402                                        "    <hal>"
403                                        "        <name>android.hidl.manager</name>"
404                                        "        <transport arch=\"32+64\">passthrough</transport>"
405                                        "        <version>1.1</version>"
406                                        "    </hal>"
407                                        "</manifest>"))
408         << "Should not allow duplicated major version across <hal>";
409 }
410 
411 TEST_F(LibVintfTest, HalManifestGetTransport) {
412     HalManifest vm;
413     EXPECT_TRUE(gHalManifestConverter(&vm,
414                                       "<manifest " + kMetaVersionStr + " type=\"device\">"
415                                       "    <hal>"
416                                       "        <name>android.hidl.manager</name>"
417                                       "        <transport>hwbinder</transport>"
418                                       "        <version>1.0</version>"
419                                       "        <interface>"
420                                       "            <name>IServiceManager</name>"
421                                       "            <instance>default</instance>"
422                                       "        </interface>"
423                                       "    </hal>"
424                                       "    <hal>"
425                                       "        <name>android.hidl.manager</name>"
426                                       "        <transport arch=\"32+64\">passthrough</transport>"
427                                       "        <version>2.1</version>"
428                                       "        <interface>"
429                                       "            <name>IServiceManager</name>"
430                                       "            <instance>default</instance>"
431                                       "        </interface>"
432                                       "    </hal>"
433                                       "</manifest>"));
434     EXPECT_EQ(Transport::PASSTHROUGH,
435               vm.getHidlTransport("android.hidl.manager", {2, 1}, "IServiceManager", "default"));
436     EXPECT_EQ(Transport::PASSTHROUGH,
437               vm.getHidlTransport("android.hidl.manager", {2, 0}, "IServiceManager", "default"));
438     EXPECT_EQ(Transport::EMPTY,
439               vm.getHidlTransport("android.hidl.manager", {2, 2}, "IServiceManager", "default"));
440     EXPECT_EQ(Transport::HWBINDER,
441               vm.getHidlTransport("android.hidl.manager", {1, 0}, "IServiceManager", "default"));
442 }
443 
444 TEST_F(LibVintfTest, HalManifestInstances) {
445     HalManifest vm = testDeviceManifest();
446     EXPECT_EQ(vm.getHidlInstances("android.hardware.camera", {2, 0}, "ICamera"),
447               std::set<std::string>({"default", "legacy/0"}));
448     EXPECT_EQ(vm.getHidlInstances("android.hardware.camera", {2, 0}, "IBetterCamera"),
449               std::set<std::string>({"camera"}));
450     EXPECT_EQ(vm.getHidlInstances("android.hardware.camera", {2, 0}, "INotExist"),
451               std::set<std::string>({}));
452     EXPECT_EQ(vm.getHidlInstances("android.hardware.nfc", {1, 0}, "INfc"),
453               std::set<std::string>({"default"}));
454 
455     EXPECT_TRUE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "ICamera", "default"));
456     EXPECT_TRUE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "ICamera", "legacy/0"));
457     EXPECT_TRUE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "IBetterCamera", "camera"));
458     EXPECT_TRUE(vm.hasHidlInstance("android.hardware.nfc", {1, 0}, "INfc", "default"));
459 
460     EXPECT_FALSE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "INotExist", "default"));
461     EXPECT_FALSE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "ICamera", "notexist"));
462     EXPECT_FALSE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "IBetterCamera", "default"));
463     EXPECT_FALSE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "INotExist", "notexist"));
464     EXPECT_FALSE(vm.hasHidlInstance("android.hardware.nfc", {1, 0}, "INfc", "notexist"));
465 }
466 
467 TEST_F(LibVintfTest, VersionConverter) {
468     Version v(3, 6);
469     std::string xml = gVersionConverter(v);
470     EXPECT_EQ(xml, "<version>3.6</version>\n");
471     Version v2;
472     EXPECT_TRUE(gVersionConverter(&v2, xml));
473     EXPECT_EQ(v, v2);
474 }
475 
476 static bool insert(std::map<std::string, HalInterface>* map, HalInterface&& intf) {
477     std::string name{intf.name()};
478     return map->emplace(std::move(name), std::move(intf)).second;
479 }
480 
481 TEST_F(LibVintfTest, MatrixHalConverter) {
482     MatrixHal mh{HalFormat::NATIVE, "android.hardware.camera",
483             {{VersionRange(1,2,3), VersionRange(4,5,6)}},
484             false /* optional */, {}};
485     EXPECT_TRUE(insert(&mh.interfaces, {"IBetterCamera", {"default", "great"}}));
486     EXPECT_TRUE(insert(&mh.interfaces, {"ICamera", {"default"}}));
487     std::string xml = gMatrixHalConverter(mh);
488     EXPECT_EQ(xml,
489         "<hal format=\"native\" optional=\"false\">\n"
490         "    <name>android.hardware.camera</name>\n"
491         "    <version>1.2-3</version>\n"
492         "    <version>4.5-6</version>\n"
493         "    <interface>\n"
494         "        <name>IBetterCamera</name>\n"
495         "        <instance>default</instance>\n"
496         "        <instance>great</instance>\n"
497         "    </interface>\n"
498         "    <interface>\n"
499         "        <name>ICamera</name>\n"
500         "        <instance>default</instance>\n"
501         "    </interface>\n"
502         "</hal>\n");
503     MatrixHal mh2;
504     EXPECT_TRUE(gMatrixHalConverter(&mh2, xml));
505     EXPECT_EQ(mh, mh2);
506 }
507 
508 TEST_F(LibVintfTest, KernelConfigTypedValueConverter) {
509 
510     KernelConfigTypedValue converted;
511 
512     auto testOne = [] (const KernelConfigTypedValue &original,
513                     const std::string &expectXml) {
514         std::string xml;
515         KernelConfigTypedValue converted;
516         xml = gKernelConfigTypedValueConverter(original);
517         EXPECT_EQ(xml, expectXml);
518         EXPECT_TRUE(gKernelConfigTypedValueConverter(&converted, xml));
519         EXPECT_EQ(original, converted);
520     };
521 
522     auto testParse = [] (const KernelConfigTypedValue &original,
523                     const std::string &xml) {
524         KernelConfigTypedValue converted;
525         EXPECT_TRUE(gKernelConfigTypedValueConverter(&converted, xml));
526         EXPECT_EQ(original, converted);
527     };
528 
529     testOne(KernelConfigTypedValue("stringvalue"),
530         "<value type=\"string\">stringvalue</value>\n");
531     testOne(KernelConfigTypedValue(""),
532         "<value type=\"string\"></value>\n");
533 
534     testOne(KernelConfigTypedValue(Tristate::YES),
535         "<value type=\"tristate\">y</value>\n");
536     testOne(KernelConfigTypedValue(Tristate::NO),
537         "<value type=\"tristate\">n</value>\n");
538     testOne(KernelConfigTypedValue(Tristate::MODULE),
539         "<value type=\"tristate\">m</value>\n");
540     EXPECT_FALSE(gKernelConfigTypedValueConverter(&converted,
541         "<value type=\"tristate\">q</value>\n"));
542 
543     testOne(KernelConfigTypedValue(KernelConfigRangeValue{4, 20}),
544         "<value type=\"range\">4-20</value>\n");
545     testOne(KernelConfigTypedValue(KernelConfigRangeValue{0, UINT64_MAX}),
546         "<value type=\"range\">0-18446744073709551615</value>\n");
547     testParse(KernelConfigTypedValue(KernelConfigRangeValue{0, UINT64_MAX}),
548             "<value type=\"range\">0x0-0xffffffffffffffff</value>\n");
549 
550     EXPECT_FALSE(gKernelConfigTypedValueConverter(&converted,
551             "<value type=\"int\">-18446744073709551616</value>\n"));
552 
553     testOne(KernelConfigTypedValue(INT64_MIN),
554          "<value type=\"int\">-9223372036854775808</value>\n");
555     testParse(KernelConfigTypedValue(INT64_MIN),
556             "<value type=\"int\">0x8000000000000000</value>\n");
557     testParse(KernelConfigTypedValue(INT64_MIN),
558             "<value type=\"int\">-0X8000000000000000</value>\n");
559 
560     testParse(KernelConfigTypedValue(INT64_MIN + 1),
561             "<value type=\"int\">-0X7FFFFFFFFFFFFFFF</value>\n");
562 
563     testParse(KernelConfigTypedValue(-0x50),
564             "<value type=\"int\">-0x50</value>\n");
565 
566     testOne(KernelConfigTypedValue(0),
567          "<value type=\"int\">0</value>\n");
568 
569     // Truncation for underflow.
570     testParse(KernelConfigTypedValue(1),
571             "<value type=\"int\">-0xffffffffffffffff</value>\n");
572     testParse(KernelConfigTypedValue(1),
573             "<value type=\"int\">-18446744073709551615</value>\n");
574 
575     testOne(KernelConfigTypedValue(INT64_MAX),
576          "<value type=\"int\">9223372036854775807</value>\n");
577     testParse(KernelConfigTypedValue(INT64_MAX),
578             "<value type=\"int\">0x7FFFFFFFFFFFFFFF</value>\n");
579     // Truncation for underflow.
580     testParse(KernelConfigTypedValue(INT64_MAX),
581             "<value type=\"int\">-9223372036854775809</value>\n");
582 
583     testParse(KernelConfigTypedValue(-1),
584             "<value type=\"int\">18446744073709551615</value>\n");
585     testParse(KernelConfigTypedValue(-1),
586             "<value type=\"int\">0xffffffffffffffff</value>\n");
587 
588     EXPECT_FALSE(gKernelConfigTypedValueConverter(&converted,
589             "<value type=\"int\">18446744073709551616</value>\n"));
590 }
591 
592 TEST_F(LibVintfTest, CompatibilityMatrixConverter) {
593     CompatibilityMatrix cm;
594     EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE, "android.hardware.camera",
595             {{VersionRange(1,2,3), VersionRange(4,5,6)}},
596             false /* optional */, testHalInterfaces()}));
597     EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE, "android.hardware.nfc",
598             {{VersionRange(4,5,6), VersionRange(10,11,12)}},
599             true /* optional */, testHalInterfaces()}));
600     EXPECT_TRUE(add(cm, MatrixKernel{KernelVersion(3, 18, 22),
601             {KernelConfig{"CONFIG_FOO", Tristate::YES}, KernelConfig{"CONFIG_BAR", "stringvalue"}}}));
602     EXPECT_TRUE(add(cm, MatrixKernel{KernelVersion(4, 4, 1),
603             {KernelConfig{"CONFIG_BAZ", 20}, KernelConfig{"CONFIG_BAR", KernelConfigRangeValue{3, 5} }}}));
604     set(cm, Sepolicy(30, {{25, 0}, {26, 0, 3}}));
605     setAvb(cm, Version{2, 1});
606     std::string xml = gCompatibilityMatrixConverter(cm);
607     EXPECT_EQ(xml,
608             "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
609             "    <hal format=\"native\" optional=\"false\">\n"
610             "        <name>android.hardware.camera</name>\n"
611             "        <version>1.2-3</version>\n"
612             "        <version>4.5-6</version>\n"
613             "        <interface>\n"
614             "            <name>IFoo</name>\n"
615             "            <instance>default</instance>\n"
616             "        </interface>\n"
617             "    </hal>\n"
618             "    <hal format=\"native\" optional=\"true\">\n"
619             "        <name>android.hardware.nfc</name>\n"
620             "        <version>4.5-6</version>\n"
621             "        <version>10.11-12</version>\n"
622             "        <interface>\n"
623             "            <name>IFoo</name>\n"
624             "            <instance>default</instance>\n"
625             "        </interface>\n"
626             "    </hal>\n"
627             "    <kernel version=\"3.18.22\">\n"
628             "        <config>\n"
629             "            <key>CONFIG_FOO</key>\n"
630             "            <value type=\"tristate\">y</value>\n"
631             "        </config>\n"
632             "        <config>\n"
633             "            <key>CONFIG_BAR</key>\n"
634             "            <value type=\"string\">stringvalue</value>\n"
635             "        </config>\n"
636             "    </kernel>\n"
637             "    <kernel version=\"4.4.1\">\n"
638             "        <config>\n"
639             "            <key>CONFIG_BAZ</key>\n"
640             "            <value type=\"int\">20</value>\n"
641             "        </config>\n"
642             "        <config>\n"
643             "            <key>CONFIG_BAR</key>\n"
644             "            <value type=\"range\">3-5</value>\n"
645             "        </config>\n"
646             "    </kernel>\n"
647             "    <sepolicy>\n"
648             "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
649             "        <sepolicy-version>25.0</sepolicy-version>\n"
650             "        <sepolicy-version>26.0-3</sepolicy-version>\n"
651             "    </sepolicy>\n"
652             "    <avb>\n"
653             "        <vbmeta-version>2.1</vbmeta-version>\n"
654             "    </avb>\n"
655             "</compatibility-matrix>\n");
656     CompatibilityMatrix cm2;
657     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml));
658     EXPECT_EQ(cm, cm2);
659 }
660 
661 TEST_F(LibVintfTest, DeviceCompatibilityMatrixCoverter) {
662     CompatibilityMatrix cm;
663     EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE, "android.hidl.manager",
664             {{VersionRange(1,0)}},
665             false /* optional */, testHalInterfaces()}));
666     set(cm, SchemaType::DEVICE);
667     set(cm, VndkVersionRange{25,0,1,5}, {"libjpeg.so", "libbase.so"});
668     std::string xml = gCompatibilityMatrixConverter(cm);
669     EXPECT_EQ(xml,
670         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
671         "    <hal format=\"native\" optional=\"false\">\n"
672         "        <name>android.hidl.manager</name>\n"
673         "        <version>1.0</version>\n"
674         "        <interface>\n"
675         "            <name>IFoo</name>\n"
676         "            <instance>default</instance>\n"
677         "        </interface>\n"
678         "    </hal>\n"
679         "    <vndk>\n"
680         "        <version>25.0.1-5</version>\n"
681         "        <library>libbase.so</library>\n"
682         "        <library>libjpeg.so</library>\n"
683         "    </vndk>\n"
684         "</compatibility-matrix>\n");
685     CompatibilityMatrix cm2;
686     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml));
687     EXPECT_EQ(cm, cm2);
688 }
689 
690 TEST_F(LibVintfTest, IsValid) {
691     EXPECT_TRUE(isValid(ManifestHal()));
692 
693     ManifestHal invalidHal{HalFormat::HIDL,
694                            "android.hardware.camera",
695                            {{Version(2, 0), Version(2, 1)}},
696                            {Transport::PASSTHROUGH, Arch::ARCH_32_64},
697                            {}};
698 
699     EXPECT_FALSE(isValid(invalidHal));
700     HalManifest vm2;
701     EXPECT_FALSE(add(vm2, std::move(invalidHal)));
702 }
703 
704 TEST_F(LibVintfTest, HalManifestGetHalNames) {
705     HalManifest vm = testDeviceManifest();
706     EXPECT_EQ(vm.getHalNames(), std::set<std::string>(
707                   {"android.hardware.camera", "android.hardware.nfc"}));
708 }
709 
710 TEST_F(LibVintfTest, HalManifestGetAllHals) {
711     HalManifest vm = testDeviceManifest();
712     EXPECT_NE(getAnyHal(vm, "android.hardware.camera"), nullptr);
713     EXPECT_EQ(getAnyHal(vm, "non-existent"), nullptr);
714 
715     std::vector<std::string> arr{"android.hardware.camera", "android.hardware.nfc"};
716     size_t i = 0;
717     for (const auto &hal : getHals(vm)) {
718         EXPECT_EQ(hal.name, arr[i++]);
719     }
720 }
721 
722 TEST_F(LibVintfTest, HalManifestGetHals) {
723     HalManifest vm;
724     EXPECT_TRUE(add(vm, ManifestHal{HalFormat::HIDL,
725                                     "android.hardware.camera",
726                                     {Version(1, 2)},
727                                     {Transport::HWBINDER, Arch::ARCH_EMPTY},
728                                     {{"ICamera", {"ICamera", {"legacy/0", "default"}}},
729                                      {"IBetterCamera", {"IBetterCamera", {"camera"}}}}}));
730     EXPECT_TRUE(add(vm, ManifestHal{HalFormat::HIDL,
731                                     "android.hardware.camera",
732                                     {Version(2, 0)},
733                                     {Transport::HWBINDER, Arch::ARCH_EMPTY},
734                                     {{"ICamera", {"ICamera", {"legacy/0", "default"}}},
735                                      {"IBetterCamera", {"IBetterCamera", {"camera"}}}}}));
736     EXPECT_TRUE(add(vm, ManifestHal{HalFormat::HIDL,
737                                     "android.hardware.nfc",
738                                     {Version(1, 0), Version(2, 1)},
739                                     {Transport::PASSTHROUGH, Arch::ARCH_32_64},
740                                     {{"INfc", {"INfc", {"default"}}}}}));
741     ManifestHal expectedCameraHalV1_2 =
742         ManifestHal{HalFormat::HIDL,
743                     "android.hardware.camera",
744                     {Version(1, 2)},
745                     {Transport::HWBINDER, Arch::ARCH_EMPTY},
746                     {{"ICamera", {"ICamera", {"legacy/0", "default"}}},
747                      {"IBetterCamera", {"IBetterCamera", {"camera"}}}}};
748     ManifestHal expectedCameraHalV2_0 =
749         ManifestHal{HalFormat::HIDL,
750                     "android.hardware.camera",
751                     {Version(2, 0)},
752                     {Transport::HWBINDER, Arch::ARCH_EMPTY},
753                     {{"ICamera", {"ICamera", {"legacy/0", "default"}}},
754                      {"IBetterCamera", {"IBetterCamera", {"camera"}}}}};
755     ManifestHal expectedNfcHal = ManifestHal{HalFormat::HIDL,
756                                              "android.hardware.nfc",
757                                              {Version(1, 0), Version(2, 1)},
758                                              {Transport::PASSTHROUGH, Arch::ARCH_32_64},
759                                              {{"INfc", {"INfc", {"default"}}}}};
760     auto cameraHals = getHals(vm, "android.hardware.camera");
761     EXPECT_EQ((int)cameraHals.size(), 2);
762     EXPECT_EQ(*cameraHals[0], expectedCameraHalV1_2);
763     EXPECT_EQ(*cameraHals[1], expectedCameraHalV2_0);
764     auto nfcHals = getHals(vm, "android.hardware.nfc");
765     EXPECT_EQ((int)nfcHals.size(), 1);
766     EXPECT_EQ(*nfcHals[0], expectedNfcHal);
767 }
768 
769 TEST_F(LibVintfTest, CompatibilityMatrixGetHals) {
770     CompatibilityMatrix cm;
771     EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE,
772                                   "android.hardware.camera",
773                                   {{VersionRange(1, 2, 3), VersionRange(4, 5, 6)}},
774                                   false /* optional */,
775                                   testHalInterfaces()}));
776     EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE,
777                                   "android.hardware.nfc",
778                                   {{VersionRange(4, 5, 6), VersionRange(10, 11, 12)}},
779                                   true /* optional */,
780                                   testHalInterfaces()}));
781 
782     MatrixHal expectedCameraHal = MatrixHal{
783         HalFormat::NATIVE,
784         "android.hardware.camera",
785         {{VersionRange(1, 2, 3), VersionRange(4, 5, 6)}},
786         false /* optional */,
787         testHalInterfaces(),
788     };
789     MatrixHal expectedNfcHal = MatrixHal{HalFormat::NATIVE,
790                                          "android.hardware.nfc",
791                                          {{VersionRange(4, 5, 6), VersionRange(10, 11, 12)}},
792                                          true /* optional */,
793                                          testHalInterfaces()};
794     auto cameraHals = getHals(cm, "android.hardware.camera");
795     EXPECT_EQ((int)cameraHals.size(), 1);
796     EXPECT_EQ(*cameraHals[0], expectedCameraHal);
797     auto nfcHals = getHals(cm, "android.hardware.nfc");
798     EXPECT_EQ((int)nfcHals.size(), 1);
799     EXPECT_EQ(*nfcHals[0], expectedNfcHal);
800 }
801 
802 TEST_F(LibVintfTest, RuntimeInfo) {
803     RuntimeInfo ki = testRuntimeInfo();
804     using KernelConfigs = std::vector<KernelConfig>;
805     const KernelConfigs configs {
806             KernelConfig{"CONFIG_64BIT", Tristate::YES},
807             KernelConfig{"CONFIG_ANDROID_BINDER_DEVICES", "binder,hwbinder"},
808             KernelConfig{"CONFIG_ARCH_MMAP_RND_BITS", 24},
809             KernelConfig{"CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES", ""},
810             KernelConfig{"CONFIG_ILLEGAL_POINTER_VALUE", 0xdead000000000000},
811             KernelConfig{"CONFIG_NOTEXIST", Tristate::NO},
812     };
813 
814     auto testMatrix = [&] (MatrixKernel &&kernel) {
815         CompatibilityMatrix cm;
816         add(cm, std::move(kernel));
817         set(cm, {30, {{25, 0}}});
818         setAvb(cm, {2, 1});
819         return cm;
820     };
821 
822     std::string error;
823 
824     {
825         MatrixKernel kernel(KernelVersion{4, 4, 1}, KernelConfigs(configs));
826         CompatibilityMatrix cm = testMatrix(std::move(kernel));
827         EXPECT_FALSE(ki.checkCompatibility(cm)) << "Kernel version shouldn't match";
828     }
829 
830     {
831         MatrixKernel kernel(KernelVersion{3, 18, 60}, KernelConfigs(configs));
832         CompatibilityMatrix cm = testMatrix(std::move(kernel));
833         EXPECT_FALSE(ki.checkCompatibility(cm)) << "Kernel version shouldn't match";
834     }
835 
836     {
837         MatrixKernel kernel(KernelVersion{3, 18, 22}, KernelConfigs(configs));
838         CompatibilityMatrix cm = testMatrix(std::move(kernel));
839         EXPECT_TRUE(ki.checkCompatibility(cm, &error)) << error;
840     }
841 
842     {
843         MatrixKernel kernel(KernelVersion{3, 18, 22}, KernelConfigs(configs));
844         CompatibilityMatrix cm = testMatrix(std::move(kernel));
845         set(cm, Sepolicy{22, {{25, 0}}});
846         EXPECT_TRUE(ki.checkCompatibility(cm, &error)) << error;
847         set(cm, Sepolicy{40, {{25, 0}}});
848         EXPECT_FALSE(ki.checkCompatibility(cm, &error))
849             << "kernel-sepolicy-version shouldn't match";
850         EXPECT_IN("kernelSepolicyVersion = 30 but required >= 40", error);
851     }
852 
853     {
854         KernelConfigs newConfigs(configs);
855         newConfigs[0] = KernelConfig{"CONFIG_64BIT", Tristate::NO};
856         MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
857         CompatibilityMatrix cm = testMatrix(std::move(kernel));
858         EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for tristate";
859     }
860 
861     {
862         KernelConfigs newConfigs(configs);
863         newConfigs[0] = KernelConfig{"CONFIG_64BIT", 20};
864         MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
865         CompatibilityMatrix cm = testMatrix(std::move(kernel));
866         EXPECT_FALSE(ki.checkCompatibility(cm)) << "Type shouldn't match";
867     }
868 
869     {
870         KernelConfigs newConfigs(configs);
871         newConfigs[1] = KernelConfig{"CONFIG_ANDROID_BINDER_DEVICES", "binder"};
872         MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
873         CompatibilityMatrix cm = testMatrix(std::move(kernel));
874         EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for string";
875     }
876 
877     {
878         KernelConfigs newConfigs(configs);
879         newConfigs[1] = KernelConfig{"CONFIG_ANDROID_BINDER_DEVICES", Tristate::YES};
880         MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
881         CompatibilityMatrix cm = testMatrix(std::move(kernel));
882         EXPECT_FALSE(ki.checkCompatibility(cm)) << "Type shouldn't match";
883     }
884 
885     {
886         KernelConfigs newConfigs(configs);
887         newConfigs[2] = KernelConfig{"CONFIG_ARCH_MMAP_RND_BITS", 30};
888         MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
889         CompatibilityMatrix cm = testMatrix(std::move(kernel));
890         EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for integer";
891     }
892 
893     RuntimeInfo badAvb = testRuntimeInfo();
894     CompatibilityMatrix cm = testMatrix(MatrixKernel(KernelVersion{3, 18, 31}, {}));
895     {
896         setAvb(badAvb, {1, 0}, {2, 1});
897         EXPECT_FALSE(badAvb.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
898         EXPECT_STREQ(error.c_str(), "Vbmeta version 1.0 does not match framework matrix 2.1");
899     }
900     {
901         setAvb(badAvb, {2, 1}, {3, 0});
902         EXPECT_FALSE(badAvb.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
903     }
904     {
905         setAvb(badAvb, {2, 1}, {2, 3});
906         EXPECT_TRUE(badAvb.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
907     }
908     {
909         setAvb(badAvb, {2, 3}, {2, 1});
910         EXPECT_TRUE(badAvb.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
911     }
912 }
913 
914 TEST_F(LibVintfTest, MissingAvb) {
915     std::string xml =
916         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
917         "    <kernel version=\"3.18.31\"></kernel>"
918         "    <sepolicy>\n"
919         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
920         "        <sepolicy-version>25.5</sepolicy-version>\n"
921         "    </sepolicy>\n"
922         "</compatibility-matrix>\n";
923     CompatibilityMatrix cm;
924     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml));
925     EXPECT_EQ(getAvb(cm), Version(0, 0));
926 }
927 
928 TEST_F(LibVintfTest, DisableAvb) {
929     std::string xml =
930         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
931         "    <kernel version=\"3.18.31\"></kernel>"
932         "    <sepolicy>\n"
933         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
934         "        <sepolicy-version>25.5</sepolicy-version>\n"
935         "    </sepolicy>\n"
936         "    <avb>\n"
937         "        <vbmeta-version>1.0</vbmeta-version>\n"
938         "    </avb>\n"
939         "</compatibility-matrix>\n";
940     CompatibilityMatrix cm;
941     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml));
942     RuntimeInfo ki = testRuntimeInfo();
943     std::string error;
944     EXPECT_FALSE(ki.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
945     EXPECT_STREQ(error.c_str(), "AVB version 2.1 does not match framework matrix 1.0");
946     EXPECT_TRUE(ki.checkCompatibility(cm, &error, CheckFlags::DISABLE_AVB_CHECK)) << error;
947 }
948 
949 // This is the test extracted from VINTF Object doc
950 TEST_F(LibVintfTest, HalCompat) {
951     CompatibilityMatrix matrix;
952     std::string error;
953 
954     std::string matrixXml =
955             "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
956             "    <hal format=\"hidl\" optional=\"false\">\n"
957             "        <name>android.hardware.foo</name>\n"
958             "        <version>1.0</version>\n"
959             "        <version>3.1-2</version>\n"
960             "        <interface>\n"
961             "            <name>IFoo</name>\n"
962             "            <instance>default</instance>\n"
963             "            <instance>specific</instance>\n"
964             "        </interface>\n"
965             "    </hal>\n"
966             "    <hal format=\"hidl\" optional=\"false\">\n"
967             "        <name>android.hardware.foo</name>\n"
968             "        <version>2.0</version>\n"
969             "        <interface>\n"
970             "            <name>IBar</name>\n"
971             "            <instance>default</instance>\n"
972             "        </interface>\n"
973             "    </hal>\n"
974             "    <sepolicy>\n"
975             "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
976             "        <sepolicy-version>25.5</sepolicy-version>\n"
977             "    </sepolicy>\n"
978             "</compatibility-matrix>\n";
979     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml))
980             << gCompatibilityMatrixConverter.lastError();
981 
982     {
983         std::string manifestXml =
984                 "<manifest " + kMetaVersionStr + " type=\"device\">\n"
985                 "    <hal format=\"hidl\">\n"
986                 "        <name>android.hardware.foo</name>\n"
987                 "        <transport>hwbinder</transport>\n"
988                 "        <version>1.0</version>\n"
989                 "        <interface>\n"
990                 "            <name>IFoo</name>\n"
991                 "            <instance>default</instance>\n"
992                 "            <instance>specific</instance>\n"
993                 "        </interface>\n"
994                 "    </hal>\n"
995                 "    <hal format=\"hidl\">\n"
996                 "        <name>android.hardware.foo</name>\n"
997                 "        <transport>hwbinder</transport>\n"
998                 "        <version>2.0</version>\n"
999                 "        <interface>\n"
1000                 "            <name>IBar</name>\n"
1001                 "            <instance>default</instance>\n"
1002                 "        </interface>\n"
1003                 "    </hal>\n"
1004                 "    <sepolicy>\n"
1005                 "        <version>25.5</version>\n"
1006                 "    </sepolicy>\n"
1007                 "</manifest>\n";
1008 
1009         HalManifest manifest;
1010         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1011         EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1012     }
1013 
1014     {
1015         std::string manifestXml =
1016                 "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1017                 "    <hal format=\"hidl\">\n"
1018                 "        <name>android.hardware.foo</name>\n"
1019                 "        <transport>hwbinder</transport>\n"
1020                 "        <version>1.0</version>\n"
1021                 "        <interface>\n"
1022                 "            <name>IFoo</name>\n"
1023                 "            <instance>default</instance>\n"
1024                 "            <instance>specific</instance>\n"
1025                 "        </interface>\n"
1026                 "    </hal>\n"
1027                 "    <sepolicy>\n"
1028                 "        <version>25.5</version>\n"
1029                 "    </sepolicy>\n"
1030                 "</manifest>\n";
1031         HalManifest manifest;
1032         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1033         EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
1034                 << "should not be compatible because IBar is missing";
1035     }
1036 
1037     {
1038         std::string manifestXml =
1039                 "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1040                 "    <hal format=\"hidl\">\n"
1041                 "        <name>android.hardware.foo</name>\n"
1042                 "        <transport>hwbinder</transport>\n"
1043                 "        <version>1.0</version>\n"
1044                 "        <interface>\n"
1045                 "            <name>IFoo</name>\n"
1046                 "            <instance>default</instance>\n"
1047                 "        </interface>\n"
1048                 "    </hal>\n"
1049                 "    <hal format=\"hidl\">\n"
1050                 "        <name>android.hardware.foo</name>\n"
1051                 "        <transport>hwbinder</transport>\n"
1052                 "        <version>2.0</version>\n"
1053                 "        <interface>\n"
1054                 "            <name>IBar</name>\n"
1055                 "            <instance>default</instance>\n"
1056                 "        </interface>\n"
1057                 "    </hal>\n"
1058                 "    <sepolicy>\n"
1059                 "        <version>25.5</version>\n"
1060                 "    </sepolicy>\n"
1061                 "</manifest>\n";
1062         HalManifest manifest;
1063         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1064         EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
1065             << "should not be compatible because IFoo/specific is missing";
1066     }
1067 
1068     {
1069         std::string manifestXml =
1070                 "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1071                 "    <hal format=\"hidl\">\n"
1072                 "        <name>android.hardware.foo</name>\n"
1073                 "        <transport>hwbinder</transport>\n"
1074                 "        <version>3.3</version>\n"
1075                 "        <interface>\n"
1076                 "            <name>IFoo</name>\n"
1077                 "            <instance>default</instance>\n"
1078                 "            <instance>specific</instance>\n"
1079                 "        </interface>\n"
1080                 "    </hal>\n"
1081                 "    <hal format=\"hidl\">\n"
1082                 "        <name>android.hardware.foo</name>\n"
1083                 "        <transport>hwbinder</transport>\n"
1084                 "        <version>2.0</version>\n"
1085                 "        <interface>\n"
1086                 "            <name>IBar</name>\n"
1087                 "            <instance>default</instance>\n"
1088                 "        </interface>\n"
1089                 "    </hal>\n"
1090                 "    <sepolicy>\n"
1091                 "        <version>25.5</version>\n"
1092                 "    </sepolicy>\n"
1093                 "</manifest>\n";
1094         HalManifest manifest;
1095         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1096         EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1097     }
1098 
1099     {
1100         std::string manifestXml =
1101                 "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1102                 "    <hal format=\"hidl\">\n"
1103                 "        <name>android.hardware.foo</name>\n"
1104                 "        <transport>hwbinder</transport>\n"
1105                 "        <version>1.0</version>\n"
1106                 "        <interface>\n"
1107                 "            <name>IFoo</name>\n"
1108                 "            <instance>default</instance>\n"
1109                 "        </interface>\n"
1110                 "    </hal>\n"
1111                 "    <hal format=\"hidl\">\n"
1112                 "        <name>android.hardware.foo</name>\n"
1113                 "        <transport>hwbinder</transport>\n"
1114                 "        <version>3.2</version>\n"
1115                 "        <interface>\n"
1116                 "            <name>IFoo</name>\n"
1117                 "            <instance>specific</instance>\n"
1118                 "        </interface>\n"
1119                 "    </hal>\n"
1120                 "    <hal format=\"hidl\">\n"
1121                 "        <name>android.hardware.foo</name>\n"
1122                 "        <transport>hwbinder</transport>\n"
1123                 "        <version>2.0</version>\n"
1124                 "        <interface>\n"
1125                 "            <name>IBar</name>\n"
1126                 "            <instance>default</instance>\n"
1127                 "        </interface>\n"
1128                 "    </hal>\n"
1129                 "    <sepolicy>\n"
1130                 "        <version>25.5</version>\n"
1131                 "    </sepolicy>\n"
1132                 "</manifest>\n";
1133         HalManifest manifest;
1134         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1135         EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
1136                 << "should not be compatible even though @1.0::IFoo/default "
1137                 << "and @3.2::IFoo/specific present";
1138     }
1139 
1140     {
1141         std::string manifestXml =
1142                 "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1143                 "    <hal format=\"hidl\">\n"
1144                 "        <name>android.hardware.foo</name>\n"
1145                 "        <transport>hwbinder</transport>\n"
1146                 "        <version>1.0</version>\n"
1147                 "        <interface>\n"
1148                 "            <name>IFoo</name>\n"
1149                 "            <instance>default</instance>\n"
1150                 "            <instance>specific</instance>\n"
1151                 "        </interface>\n"
1152                 "    </hal>\n"
1153                 "    <hal format=\"hidl\">\n"
1154                 "        <name>android.hardware.foo</name>\n"
1155                 "        <transport>hwbinder</transport>\n"
1156                 "        <version>2.0</version>\n"
1157                 "        <interface>\n"
1158                 "            <name>IBar</name>\n"
1159                 "            <instance>default</instance>\n"
1160                 "        </interface>\n"
1161                 "    </hal>\n"
1162                 "    <sepolicy>\n"
1163                 "        <version>25.5</version>\n"
1164                 "    </sepolicy>\n"
1165                 "</manifest>\n";
1166         HalManifest manifest;
1167         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml))
1168             << gHalManifestConverter.lastError();
1169         EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1170     }
1171 }
1172 
1173 TEST_F(LibVintfTest, Compat) {
1174     std::string manifestXml =
1175         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1176         "    <hal format=\"hidl\">\n"
1177         "        <name>android.hardware.camera</name>\n"
1178         "        <transport>hwbinder</transport>\n"
1179         "        <version>3.5</version>\n"
1180         "        <interface>\n"
1181         "            <name>IBetterCamera</name>\n"
1182         "            <instance>camera</instance>\n"
1183         "        </interface>\n"
1184         "        <interface>\n"
1185         "            <name>ICamera</name>\n"
1186         "            <instance>default</instance>\n"
1187         "            <instance>legacy/0</instance>\n"
1188         "        </interface>\n"
1189         "    </hal>\n"
1190         "    <hal format=\"hidl\">\n"
1191         "        <name>android.hardware.nfc</name>\n"
1192         "        <transport>hwbinder</transport>\n"
1193         "        <version>1.0</version>\n"
1194         "        <interface>\n"
1195         "            <name>INfc</name>\n"
1196         "            <instance>nfc_nci</instance>\n"
1197         "        </interface>\n"
1198         "    </hal>\n"
1199         "    <hal format=\"hidl\">\n"
1200         "        <name>android.hardware.nfc</name>\n"
1201         "        <transport>hwbinder</transport>\n"
1202         "        <version>2.0</version>\n"
1203         "        <interface>\n"
1204         "            <name>INfc</name>\n"
1205         "            <instance>default</instance>\n"
1206         "            <instance>nfc_nci</instance>\n"
1207         "        </interface>\n"
1208         "    </hal>\n"
1209         "    <sepolicy>\n"
1210         "        <version>25.5</version>\n"
1211         "    </sepolicy>\n"
1212         "</manifest>\n";
1213 
1214     std::string matrixXml =
1215         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1216         "    <hal format=\"hidl\" optional=\"false\">\n"
1217         "        <name>android.hardware.camera</name>\n"
1218         "        <version>2.0-5</version>\n"
1219         "        <version>3.4-16</version>\n"
1220         "        <interface>\n"
1221         "            <name>IBetterCamera</name>\n"
1222         "            <instance>camera</instance>\n"
1223         "        </interface>\n"
1224         "        <interface>\n"
1225         "            <name>ICamera</name>\n"
1226         "            <instance>default</instance>\n"
1227         "            <instance>legacy/0</instance>\n"
1228         "        </interface>\n"
1229         "    </hal>\n"
1230         "    <hal format=\"hidl\" optional=\"false\">\n"
1231         "        <name>android.hardware.nfc</name>\n"
1232         "        <version>1.0</version>\n"
1233         "        <version>2.0</version>\n"
1234         "        <interface>\n"
1235         "            <name>INfc</name>\n"
1236         "            <instance>nfc_nci</instance>\n"
1237         "        </interface>\n"
1238         "    </hal>\n"
1239         "    <hal format=\"hidl\" optional=\"true\">\n"
1240         "        <name>android.hardware.foo</name>\n"
1241         "        <version>1.0</version>\n"
1242         "    </hal>\n"
1243         "    <sepolicy>\n"
1244         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1245         "        <sepolicy-version>25.5</sepolicy-version>\n"
1246         "        <sepolicy-version>26.0-3</sepolicy-version>\n"
1247         "    </sepolicy>\n"
1248         "    <avb>\n"
1249         "        <vbmeta-version>2.1</vbmeta-version>\n"
1250         "    </avb>\n"
1251         "</compatibility-matrix>\n";
1252 
1253     HalManifest manifest;
1254     CompatibilityMatrix matrix;
1255     std::string error;
1256     EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1257     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1258     EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1259 
1260     // some smaller test cases
1261     matrixXml =
1262         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1263         "    <hal format=\"hidl\" optional=\"false\">\n"
1264         "        <name>android.hardware.camera</name>\n"
1265         "        <version>3.4</version>\n"
1266         "    </hal>\n"
1267         "    <sepolicy>\n"
1268         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1269         "        <sepolicy-version>25.5</sepolicy-version>\n"
1270         "    </sepolicy>\n"
1271         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1272         "</compatibility-matrix>\n";
1273     matrix = {};
1274     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1275     EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1276     MatrixHal *camera = getAnyHal(matrix, "android.hardware.camera");
1277     EXPECT_NE(camera, nullptr);
1278     camera->versionRanges[0] = {3, 5};
1279     EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1280     camera->versionRanges[0] = {3, 6};
1281     EXPECT_FALSE(manifest.checkCompatibility(matrix));
1282 
1283     // reset it
1284     matrix = {};
1285     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1286     set(matrix, Sepolicy{30, {{26, 0}}});
1287     EXPECT_FALSE(manifest.checkCompatibility(matrix));
1288     set(matrix, Sepolicy{30, {{25, 6}}});
1289     EXPECT_FALSE(manifest.checkCompatibility(matrix));
1290     set(matrix, Sepolicy{30, {{25, 4}}});
1291     EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1292 }
1293 
1294 /////////////////// xmlfile tests
1295 
1296 TEST_F(LibVintfTest, HalManifestConverterXmlFile) {
1297     HalManifest vm = testDeviceManifestWithXmlFile();
1298     std::string xml = gHalManifestConverter(
1299         vm, SerializeFlags::NO_TAGS.enableHals().enableSepolicy().enableXmlFiles());
1300     EXPECT_EQ(xml,
1301               "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1302               "    <hal format=\"hidl\">\n"
1303               "        <name>android.hardware.camera</name>\n"
1304               "        <transport>hwbinder</transport>\n"
1305               "        <version>2.0</version>\n"
1306               "        <interface>\n"
1307               "            <name>IBetterCamera</name>\n"
1308               "            <instance>camera</instance>\n"
1309               "        </interface>\n"
1310               "        <interface>\n"
1311               "            <name>ICamera</name>\n"
1312               "            <instance>default</instance>\n"
1313               "            <instance>legacy/0</instance>\n"
1314               "        </interface>\n"
1315               "    </hal>\n"
1316               "    <hal format=\"hidl\">\n"
1317               "        <name>android.hardware.nfc</name>\n"
1318               "        <transport arch=\"32+64\">passthrough</transport>\n"
1319               "        <version>1.0</version>\n"
1320               "        <interface>\n"
1321               "            <name>INfc</name>\n"
1322               "            <instance>default</instance>\n"
1323               "        </interface>\n"
1324               "    </hal>\n"
1325               "    <sepolicy>\n"
1326               "        <version>25.0</version>\n"
1327               "    </sepolicy>\n"
1328               "    <xmlfile>\n"
1329               "        <name>media_profile</name>\n"
1330               "        <version>1.0</version>\n"
1331               "    </xmlfile>\n"
1332               "</manifest>\n");
1333     HalManifest vm2;
1334     EXPECT_TRUE(gHalManifestConverter(&vm2, xml));
1335     EXPECT_EQ(vm, vm2);
1336 }
1337 
1338 TEST_F(LibVintfTest, CompatibilityMatrixConverterXmlFile) {
1339     CompatibilityMatrix cm;
1340     addXmlFile(cm, "media_profile", {1, 0});
1341     std::string xml = gCompatibilityMatrixConverter(cm, SerializeFlags::XMLFILES_ONLY);
1342     EXPECT_EQ(xml,
1343               "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1344               "    <xmlfile format=\"dtd\" optional=\"true\">\n"
1345               "        <name>media_profile</name>\n"
1346               "        <version>1.0</version>\n"
1347               "    </xmlfile>\n"
1348               "</compatibility-matrix>\n");
1349     CompatibilityMatrix cm2;
1350     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml));
1351     EXPECT_EQ(cm, cm2);
1352 }
1353 
1354 TEST_F(LibVintfTest, CompatibilityMatrixConverterXmlFile2) {
1355     std::string xml =
1356         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1357         "    <xmlfile format=\"dtd\" optional=\"false\">\n"
1358         "        <name>media_profile</name>\n"
1359         "        <version>1.0</version>\n"
1360         "    </xmlfile>\n"
1361         "</compatibility-matrix>\n";
1362     CompatibilityMatrix cm;
1363     EXPECT_FALSE(gCompatibilityMatrixConverter(&cm, xml));
1364     EXPECT_EQ(
1365         "compatibility-matrix.xmlfile entry media_profile has to be optional for "
1366         "compatibility matrix version 1.0",
1367         gCompatibilityMatrixConverter.lastError());
1368 }
1369 
1370 TEST_F(LibVintfTest, ManifestXmlFilePathDevice) {
1371     std::string manifestXml =
1372         "<manifest " + kMetaVersionStr + " type=\"device\">"
1373         "    <xmlfile>"
1374         "        <name>media_profile</name>"
1375         "        <version>1.0</version>"
1376         "    </xmlfile>"
1377         "</manifest>";
1378     HalManifest manifest;
1379     EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1380     EXPECT_EQ(manifest.getXmlFilePath("media_profile", {1, 0}),
1381               "/vendor/etc/media_profile_V1_0.xml");
1382 }
1383 
1384 TEST_F(LibVintfTest, ManifestXmlFilePathFramework) {
1385     std::string manifestXml =
1386         "<manifest " + kMetaVersionStr + " type=\"framework\">"
1387         "    <xmlfile>"
1388         "        <name>media_profile</name>"
1389         "        <version>1.0</version>"
1390         "    </xmlfile>"
1391         "</manifest>";
1392     HalManifest manifest;
1393     EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1394     EXPECT_EQ(manifest.getXmlFilePath("media_profile", {1, 0}),
1395               "/system/etc/media_profile_V1_0.xml");
1396 }
1397 
1398 TEST_F(LibVintfTest, ManifestXmlFilePathOverride) {
1399     std::string manifestXml =
1400         "<manifest " + kMetaVersionStr + " type=\"device\">"
1401         "    <xmlfile>"
1402         "        <name>media_profile</name>"
1403         "        <version>1.0</version>"
1404         "        <path>/vendor/etc/foo.xml</path>"
1405         "    </xmlfile>"
1406         "</manifest>";
1407     HalManifest manifest;
1408     EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1409     EXPECT_EQ(manifest.getXmlFilePath("media_profile", {1, 0}), "/vendor/etc/foo.xml");
1410 }
1411 
1412 TEST_F(LibVintfTest, ManifestXmlFilePathMissing) {
1413     std::string manifestXml =
1414         "<manifest " + kMetaVersionStr + " type=\"device\">"
1415         "    <xmlfile>"
1416         "        <name>media_profile</name>"
1417         "        <version>1.1</version>"
1418         "    </xmlfile>"
1419         "</manifest>";
1420     HalManifest manifest;
1421     EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml));
1422     EXPECT_EQ(manifest.getXmlFilePath("media_profile", {1, 0}), "");
1423 }
1424 
1425 TEST_F(LibVintfTest, MatrixXmlFilePathFramework) {
1426     std::string matrixXml =
1427         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">"
1428         "    <xmlfile format=\"dtd\" optional=\"true\">"
1429         "        <name>media_profile</name>"
1430         "        <version>2.0-1</version>"
1431         "    </xmlfile>"
1432         "</compatibility-matrix>";
1433     CompatibilityMatrix matrix;
1434     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1435     EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 1}),
1436               "/system/etc/media_profile_V2_1.dtd");
1437 }
1438 
1439 TEST_F(LibVintfTest, MatrixXmlFilePathDevice) {
1440     std::string matrixXml =
1441         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1442         "    <xmlfile format=\"xsd\" optional=\"true\">"
1443         "        <name>media_profile</name>"
1444         "        <version>2.0-1</version>"
1445         "    </xmlfile>"
1446         "</compatibility-matrix>";
1447     CompatibilityMatrix matrix;
1448     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1449     EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 0}),
1450               "/vendor/etc/media_profile_V2_1.xsd");
1451 }
1452 
1453 TEST_F(LibVintfTest, MatrixXmlFilePathOverride) {
1454     std::string matrixXml =
1455         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">"
1456         "    <xmlfile format=\"xsd\" optional=\"true\">"
1457         "        <name>media_profile</name>"
1458         "        <version>2.0-1</version>"
1459         "        <path>/system/etc/foo.xsd</path>"
1460         "    </xmlfile>"
1461         "</compatibility-matrix>";
1462     CompatibilityMatrix matrix;
1463     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1464     EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 0}), "/system/etc/foo.xsd");
1465 }
1466 
1467 TEST_F(LibVintfTest, MatrixXmlFilePathMissing) {
1468     std::string matrixXml =
1469         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">"
1470         "    <xmlfile format=\"dtd\" optional=\"true\">"
1471         "        <name>media_profile</name>"
1472         "        <version>2.1</version>"
1473         "    </xmlfile>"
1474         "</compatibility-matrix>";
1475     CompatibilityMatrix matrix;
1476     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1477     EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 0}), "");
1478 }
1479 
1480 std::pair<KernelConfigParser, status_t> processData(const std::string& data, bool processComments,
1481                                                     bool relaxedFormat = false) {
1482     KernelConfigParser parser(processComments, relaxedFormat);
1483     const char* p = data.c_str();
1484     size_t n = 0;
1485     size_t chunkSize;
1486     status_t status = OK;
1487     for (; n < data.size(); p += chunkSize, n += chunkSize) {
1488         chunkSize = std::min<size_t>(5, data.size() - n);
1489         if ((status = parser.process(p, chunkSize)) != OK) {
1490             break;
1491         }
1492     }
1493     return {std::move(parser), status};
1494 }
1495 
1496 TEST_F(LibVintfTest, KernelConfigParser) {
1497     // usage in /proc/config.gz
1498     const std::string data =
1499         "# CONFIG_NOT_SET is not set\n"
1500         "CONFIG_ONE=1\n"
1501         "CONFIG_Y=y\n"
1502         "CONFIG_STR=\"string\"\n";
1503     auto pair = processData(data, false /* processComments */);
1504     ASSERT_EQ(OK, pair.second) << pair.first.error();
1505     const auto& configs = pair.first.configs();
1506 
1507     EXPECT_EQ(configs.find("CONFIG_ONE")->second, "1");
1508     EXPECT_EQ(configs.find("CONFIG_Y")->second, "y");
1509     EXPECT_EQ(configs.find("CONFIG_STR")->second, "\"string\"");
1510     EXPECT_EQ(configs.find("CONFIG_NOT_SET"), configs.end());
1511 }
1512 
1513 TEST_F(LibVintfTest, KernelConfigParser2) {
1514     // usage in android-base.config
1515     const std::string data =
1516         "# CONFIG_NOT_SET is not set\n"
1517         "CONFIG_ONE=1\n"
1518         "CONFIG_Y=y\n"
1519         "CONFIG_STR=string\n"
1520         "# ignore_thiscomment\n"
1521         "# CONFIG_NOT_SET2 is not set\n";
1522     auto pair = processData(data, true /* processComments */);
1523     ASSERT_EQ(OK, pair.second) << pair.first.error();
1524     const auto& configs = pair.first.configs();
1525 
1526     EXPECT_EQ(configs.find("CONFIG_ONE")->second, "1");
1527     EXPECT_EQ(configs.find("CONFIG_Y")->second, "y");
1528     EXPECT_EQ(configs.find("CONFIG_STR")->second, "string");
1529     EXPECT_EQ(configs.find("CONFIG_NOT_SET")->second, "n");
1530     EXPECT_EQ(configs.find("CONFIG_NOT_SET2")->second, "n");
1531 }
1532 
1533 TEST_F(LibVintfTest, KernelConfigParserSpace) {
1534     // usage in android-base.config
1535     const std::string data =
1536         "   #   CONFIG_NOT_SET is not set   \n"
1537         "  CONFIG_ONE=1   # 'tis a one!\n"
1538         " CONFIG_TWO=2 #'tis a two!   \n"
1539         " CONFIG_THREE=3#'tis a three!   \n"
1540         " CONFIG_233=233#'tis a three!   \n"
1541         "#yey! random comments\n"
1542         "CONFIG_Y=y   \n"
1543         " CONFIG_YES=y#YES!   \n"
1544         "CONFIG_STR=string\n"
1545         "CONFIG_HELLO=hello world!  #still works\n"
1546         "CONFIG_WORLD=hello world!       \n"
1547         "CONFIG_GOOD   =   good morning!  #comments here\n"
1548         "    CONFIG_MORNING   =   good morning!  \n";
1549     auto pair = processData(data, true /* processComments */, true /* relaxedFormat */);
1550     ASSERT_EQ(OK, pair.second) << pair.first.error();
1551     const auto& configs = pair.first.configs();
1552 
1553     EXPECT_EQ(configs.find("CONFIG_ONE")->second, "1");
1554     EXPECT_EQ(configs.find("CONFIG_TWO")->second, "2");
1555     EXPECT_EQ(configs.find("CONFIG_THREE")->second, "3");
1556     EXPECT_EQ(configs.find("CONFIG_Y")->second, "y");
1557     EXPECT_EQ(configs.find("CONFIG_STR")->second, "string");
1558     EXPECT_EQ(configs.find("CONFIG_HELLO")->second, "hello world!")
1559         << "Value should be \"hello world!\" without trailing spaces";
1560     EXPECT_EQ(configs.find("CONFIG_WORLD")->second, "hello world!")
1561         << "Value should be \"hello world!\" without trailing spaces";
1562     EXPECT_EQ(configs.find("CONFIG_GOOD")->second, "good morning!")
1563         << "Value should be \"good morning!\" without leading or trailing spaces";
1564     EXPECT_EQ(configs.find("CONFIG_MORNING")->second, "good morning!")
1565         << "Value should be \"good morning!\" without leading or trailing spaces";
1566     EXPECT_EQ(configs.find("CONFIG_NOT_SET")->second, "n");
1567 }
1568 
1569 TEST_F(LibVintfTest, NetutilsWrapperMatrix) {
1570     std::string matrixXml;
1571     CompatibilityMatrix matrix;
1572 
1573     matrixXml =
1574         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1575         "    <hal format=\"native\" optional=\"false\">"
1576         "        <name>netutils-wrapper</name>"
1577         "        <version>1.0</version>"
1578         "    </hal>"
1579         "</compatibility-matrix>";
1580     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml))
1581         << gCompatibilityMatrixConverter.lastError();
1582 
1583 // only host libvintf hardcodes netutils-wrapper version requirements
1584 #ifndef LIBVINTF_TARGET
1585 
1586     matrixXml =
1587         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1588         "    <hal format=\"native\" optional=\"false\">"
1589         "        <name>netutils-wrapper</name>"
1590         "        <version>1.0-1</version>"
1591         "    </hal>"
1592         "</compatibility-matrix>";
1593     EXPECT_FALSE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1594     EXPECT_CONTAINS(
1595         gCompatibilityMatrixConverter.lastError(),
1596         "netutils-wrapper HAL must specify exactly one version x.0, but a range is provided. "
1597         "Perhaps you mean '1.0'?");
1598 
1599     matrixXml =
1600         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1601         "    <hal format=\"native\" optional=\"false\">"
1602         "        <name>netutils-wrapper</name>"
1603         "        <version>1.1</version>"
1604         "    </hal>"
1605         "</compatibility-matrix>";
1606     EXPECT_FALSE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1607     EXPECT_CONTAINS(
1608         gCompatibilityMatrixConverter.lastError(),
1609         "netutils-wrapper HAL must specify exactly one version x.0, but minor version is not 0. "
1610         "Perhaps you mean '1.0'?");
1611 
1612     matrixXml =
1613         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1614         "    <hal format=\"native\" optional=\"false\">"
1615         "        <name>netutils-wrapper</name>"
1616         "        <version>1.0</version>"
1617         "        <version>2.0</version>"
1618         "    </hal>"
1619         "</compatibility-matrix>";
1620     EXPECT_FALSE(gCompatibilityMatrixConverter(&matrix, matrixXml));
1621     EXPECT_CONTAINS(
1622         gCompatibilityMatrixConverter.lastError(),
1623         "netutils-wrapper HAL must specify exactly one version x.0, but multiple <version> element "
1624         "is specified.");
1625 
1626 #endif  // LIBVINTF_TARGET
1627 }
1628 
1629 TEST_F(LibVintfTest, NetutilsWrapperManifest) {
1630     std::string manifestXml;
1631     HalManifest manifest;
1632 
1633     manifestXml =
1634         "<manifest " + kMetaVersionStr + " type=\"framework\">"
1635         "    <hal format=\"native\">"
1636         "        <name>netutils-wrapper</name>"
1637         "        <version>1.0</version>"
1638         "        <version>2.0</version>"
1639         "    </hal>"
1640         "</manifest>";
1641     EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml)) << gHalManifestConverter.lastError();
1642 
1643 // only host libvintf hardcodes netutils-wrapper version requirements
1644 #ifndef LIBVINTF_TARGET
1645 
1646     manifestXml =
1647         "<manifest " + kMetaVersionStr + " type=\"framework\">"
1648         "    <hal format=\"native\">"
1649         "        <name>netutils-wrapper</name>"
1650         "        <version>1.1</version>"
1651         "    </hal>"
1652         "</manifest>";
1653     EXPECT_FALSE(gHalManifestConverter(&manifest, manifestXml));
1654     EXPECT_CONTAINS(
1655         gCompatibilityMatrixConverter.lastError(),
1656         "netutils-wrapper HAL must specify exactly one version x.0, but multiple <version> element "
1657         "is specified.");
1658 
1659     manifestXml =
1660         "<manifest " + kMetaVersionStr + " type=\"framework\">"
1661         "    <hal format=\"native\">"
1662         "        <name>netutils-wrapper</name>"
1663         "        <version>1.0</version>"
1664         "        <version>2.1</version>"
1665         "    </hal>"
1666         "</manifest>";
1667     EXPECT_FALSE(gHalManifestConverter(&manifest, manifestXml));
1668     EXPECT_CONTAINS(
1669         gCompatibilityMatrixConverter.lastError(),
1670         "netutils-wrapper HAL must specify exactly one version x.0, but multiple <version> element "
1671         "is specified.");
1672 
1673 #endif  // LIBVINTF_TARGET
1674 }
1675 
1676 TEST_F(LibVintfTest, KernelConfigConditionTest) {
1677     std::string xml =
1678         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1679         "    <kernel version=\"3.18.22\"/>\n"
1680         "    <kernel version=\"3.18.22\">\n"
1681         "        <conditions>\n"
1682         "            <config>\n"
1683         "                <key>CONFIG_ARM</key>\n"
1684         "                <value type=\"tristate\">y</value>\n"
1685         "            </config>\n"
1686         "        </conditions>\n"
1687         "        <config>\n"
1688         "            <key>CONFIG_FOO</key>\n"
1689         "            <value type=\"tristate\">y</value>\n"
1690         "        </config>\n"
1691         "    </kernel>\n"
1692         "    <sepolicy>\n"
1693         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1694         "        <sepolicy-version>25.0</sepolicy-version>\n"
1695         "    </sepolicy>\n"
1696         "    <avb>\n"
1697         "        <vbmeta-version>2.1</vbmeta-version>\n"
1698         "    </avb>\n"
1699         "</compatibility-matrix>\n";
1700 
1701     CompatibilityMatrix cm;
1702     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
1703         << gCompatibilityMatrixConverter.lastError();
1704     const auto& kernels = getKernels(cm);
1705     ASSERT_GE(kernels.size(), 2u);
1706     ASSERT_TRUE(kernels[0].conditions().empty());
1707     const auto& kernel = kernels[1];
1708     const auto& cond = kernel.conditions();
1709     ASSERT_FALSE(cond.empty());
1710     EXPECT_EQ("CONFIG_ARM", cond.begin()->first);
1711     EXPECT_EQ(KernelConfigTypedValue(Tristate::YES), cond.begin()->second);
1712     EXPECT_FALSE(kernel.configs().empty());
1713 
1714     EXPECT_EQ(xml, gCompatibilityMatrixConverter(cm));
1715 }
1716 
1717 TEST_F(LibVintfTest, KernelConfigConditionEmptyTest) {
1718     std::string xml =
1719         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1720         "    <kernel version=\"4.4.0\"/>\n"
1721         "    <kernel version=\"3.18.22\">\n"
1722         "        <conditions>\n"
1723         "            <config>\n"
1724         "                <key>CONFIG_ARM</key>\n"
1725         "                <value type=\"tristate\">y</value>\n"
1726         "            </config>\n"
1727         "        </conditions>\n"
1728         "    </kernel>\n"
1729         "</compatibility-matrix>\n";
1730 
1731     CompatibilityMatrix cm;
1732     EXPECT_FALSE(gCompatibilityMatrixConverter(&cm, xml))
1733         << "Should not accept first kernel version with non-empty conditions";
1734     EXPECT_EQ(
1735         "First <kernel> for version 3.18 must have empty <conditions> "
1736         "for backwards compatibility.",
1737         gCompatibilityMatrixConverter.lastError());
1738 }
1739 
1740 TEST_F(LibVintfTest, KernelConfigConditionMatch) {
1741     RuntimeInfo runtime = testRuntimeInfo();
1742     std::string error;
1743     std::string xml;
1744     CompatibilityMatrix cm;
1745 
1746     xml =
1747         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1748         "    <kernel version=\"3.18.22\"/>\n"
1749         "    <kernel version=\"3.18.22\">\n"
1750         "        <conditions>\n"
1751         "            <config>\n"
1752         "                <key>CONFIG_64BIT</key>\n"
1753         "                <value type=\"tristate\">y</value>\n"
1754         "            </config>\n"
1755         "        </conditions>\n"
1756         "        <config>\n"
1757         "            <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
1758         "            <value type=\"int\">24</value>\n"
1759         "        </config>\n"
1760         "    </kernel>\n"
1761         "    <sepolicy>\n"
1762         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1763         "    </sepolicy>\n"
1764         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1765         "</compatibility-matrix>\n";
1766 
1767     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
1768         << gCompatibilityMatrixConverter.lastError();
1769     EXPECT_TRUE(runtime.checkCompatibility(cm, &error)) << error;
1770 
1771     xml =
1772         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1773         "    <kernel version=\"3.18.22\"/>\n"
1774         "    <kernel version=\"3.18.22\">\n"
1775         "        <conditions>\n"
1776         "            <config>\n"
1777         "                <key>CONFIG_64BIT</key>\n"
1778         "                <value type=\"tristate\">y</value>\n"
1779         "            </config>\n"
1780         "        </conditions>\n"
1781         "        <config>\n"
1782         "            <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
1783         "            <value type=\"int\">26</value>\n"
1784         "        </config>\n"
1785         "    </kernel>\n"
1786         "    <sepolicy>\n"
1787         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1788         "    </sepolicy>\n"
1789         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1790         "</compatibility-matrix>\n";
1791 
1792     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
1793         << gCompatibilityMatrixConverter.lastError();
1794     EXPECT_FALSE(runtime.checkCompatibility(cm, &error))
1795         << "conditions met, so CONFIG_ARCH_MMAP_RND_BITS should not match";
1796 
1797     xml =
1798         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1799         "    <kernel version=\"3.18.22\"/>\n"
1800         "    <kernel version=\"3.18.22\">\n"
1801         "        <conditions>\n"
1802         "            <config>\n"
1803         "                <key>CONFIG_64BIT</key>\n"
1804         "                <value type=\"tristate\">n</value>\n"
1805         "            </config>\n"
1806         "        </conditions>\n"
1807         "        <config>\n"
1808         "            <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
1809         "            <value type=\"int\">26</value>\n"
1810         "        </config>\n"
1811         "    </kernel>\n"
1812         "    <sepolicy>\n"
1813         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1814         "    </sepolicy>\n"
1815         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1816         "</compatibility-matrix>\n";
1817 
1818     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
1819         << gCompatibilityMatrixConverter.lastError();
1820     EXPECT_TRUE(runtime.checkCompatibility(cm, &error)) << error;
1821     xml =
1822         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1823         "    <kernel version=\"3.18.22\"/>\n"
1824         "    <kernel version=\"3.18.22\">\n"
1825         "        <conditions>\n"
1826         "            <config>\n"
1827         "                <key>CONFIG_64BIT</key>\n"
1828         "                <value type=\"tristate\">y</value>\n"
1829         "            </config>\n"
1830         "            <config>\n"
1831         "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
1832         "                <value type=\"int\">24</value>\n"
1833         "            </config>\n"
1834         "        </conditions>\n"
1835         "        <config>\n"
1836         "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
1837         "            <value type=\"int\">0xdead000000000000</value>\n"
1838         "        </config>\n"
1839         "    </kernel>\n"
1840         "    <sepolicy>\n"
1841         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1842         "    </sepolicy>\n"
1843         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1844         "</compatibility-matrix>\n";
1845 
1846     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
1847         << gCompatibilityMatrixConverter.lastError();
1848     EXPECT_TRUE(runtime.checkCompatibility(cm, &error));
1849 
1850     xml =
1851         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1852         "    <kernel version=\"3.18.22\"/>\n"
1853         "    <kernel version=\"3.18.22\">\n"
1854         "        <conditions>\n"
1855         "            <config>\n"
1856         "                <key>CONFIG_64BIT</key>\n"
1857         "                <value type=\"tristate\">y</value>\n"
1858         "            </config>\n"
1859         "            <config>\n"
1860         "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
1861         "                <value type=\"int\">24</value>\n"
1862         "            </config>\n"
1863         "        </conditions>\n"
1864         "        <config>\n"
1865         "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
1866         "            <value type=\"int\">0xbeaf000000000000</value>\n"
1867         "        </config>\n"
1868         "    </kernel>\n"
1869         "    <sepolicy>\n"
1870         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1871         "    </sepolicy>\n"
1872         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1873         "</compatibility-matrix>\n";
1874 
1875     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
1876         << gCompatibilityMatrixConverter.lastError();
1877     EXPECT_FALSE(runtime.checkCompatibility(cm, &error))
1878         << "conditions have 'and' relationship, so CONFIG_ILLEGAL_POINTER_VALUE should not match";
1879 
1880     xml =
1881         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1882         "    <kernel version=\"3.18.22\"/>\n"
1883         "    <kernel version=\"3.18.22\">\n"
1884         "        <conditions>\n"
1885         "            <config>\n"
1886         "                <key>CONFIG_64BIT</key>\n"
1887         "                <value type=\"tristate\">y</value>\n"
1888         "            </config>\n"
1889         "            <config>\n"
1890         "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
1891         "                <value type=\"int\">26</value>\n"
1892         "            </config>\n"
1893         "        </conditions>\n"
1894         "        <config>\n"
1895         "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
1896         "            <value type=\"int\">0xbeaf000000000000</value>\n"
1897         "        </config>\n"
1898         "    </kernel>\n"
1899         "    <sepolicy>\n"
1900         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1901         "    </sepolicy>\n"
1902         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1903         "</compatibility-matrix>\n";
1904 
1905     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
1906         << gCompatibilityMatrixConverter.lastError();
1907     EXPECT_TRUE(runtime.checkCompatibility(cm, &error)) << error;
1908 
1909     xml =
1910         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1911         "    <kernel version=\"3.18.22\">\n"
1912         "        <config>\n"
1913         "            <key>CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES</key>\n"
1914         "            <value type=\"string\"/>\n"
1915         "        </config>\n"
1916         "    </kernel>\n"
1917         "    <kernel version=\"3.18.22\">\n"
1918         "        <conditions>\n"
1919         "            <config>\n"
1920         "                <key>CONFIG_64BIT</key>\n"
1921         "                <value type=\"tristate\">y</value>\n"
1922         "            </config>\n"
1923         "        </conditions>\n"
1924         "        <config>\n"
1925         "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
1926         "            <value type=\"int\">0xdead000000000000</value>\n"
1927         "        </config>\n"
1928         "    </kernel>\n"
1929         "    <kernel version=\"3.18.22\">\n"
1930         "        <conditions>\n"
1931         "            <config>\n"
1932         "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
1933         "                <value type=\"int\">24</value>\n"
1934         "            </config>\n"
1935         "        </conditions>\n"
1936         "        <config>\n"
1937         "            <key>CONFIG_ANDROID_BINDER_DEVICES</key>\n"
1938         "            <value type=\"string\">binder,hwbinder</value>\n"
1939         "        </config>\n"
1940         "    </kernel>\n"
1941         "    <sepolicy>\n"
1942         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1943         "    </sepolicy>\n"
1944         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1945         "</compatibility-matrix>\n";
1946 
1947     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
1948         << gCompatibilityMatrixConverter.lastError();
1949     EXPECT_TRUE(runtime.checkCompatibility(cm, &error)) << error;
1950 
1951     xml =
1952         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1953         "    <kernel version=\"3.18.22\">\n"
1954         "        <config>\n"
1955         "            <key>CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES</key>\n"
1956         "            <value type=\"string\"/>\n"
1957         "        </config>\n"
1958         "    </kernel>\n"
1959         "    <kernel version=\"3.18.22\">\n"
1960         "        <conditions>\n"
1961         "            <config>\n"
1962         "                <key>CONFIG_64BIT</key>\n"
1963         "                <value type=\"tristate\">y</value>\n"
1964         "            </config>\n"
1965         "        </conditions>\n"
1966         "        <config>\n"
1967         "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
1968         "            <value type=\"int\">0xbeaf000000000000</value>\n"
1969         "        </config>\n"
1970         "    </kernel>\n"
1971         "    <kernel version=\"3.18.22\">\n"
1972         "        <conditions>\n"
1973         "            <config>\n"
1974         "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
1975         "                <value type=\"int\">24</value>\n"
1976         "            </config>\n"
1977         "        </conditions>\n"
1978         "        <config>\n"
1979         "            <key>CONFIG_ANDROID_BINDER_DEVICES</key>\n"
1980         "            <value type=\"string\">binder,hwbinder</value>\n"
1981         "        </config>\n"
1982         "    </kernel>\n"
1983         "    <sepolicy>\n"
1984         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1985         "    </sepolicy>\n"
1986         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1987         "</compatibility-matrix>\n";
1988 
1989     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
1990         << gCompatibilityMatrixConverter.lastError();
1991     EXPECT_FALSE(runtime.checkCompatibility(cm, &error)) << "all fragments should be used.";
1992 
1993     xml =
1994         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1995         "    <kernel version=\"3.18.22\">\n"
1996         "        <config>\n"
1997         "            <key>CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES</key>\n"
1998         "            <value type=\"string\"/>\n"
1999         "        </config>\n"
2000         "    </kernel>\n"
2001         "    <kernel version=\"3.18.22\">\n"
2002         "        <conditions>\n"
2003         "            <config>\n"
2004         "                <key>CONFIG_64BIT</key>\n"
2005         "                <value type=\"tristate\">y</value>\n"
2006         "            </config>\n"
2007         "        </conditions>\n"
2008         "        <config>\n"
2009         "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
2010         "            <value type=\"int\">0xdead000000000000</value>\n"
2011         "        </config>\n"
2012         "    </kernel>\n"
2013         "    <kernel version=\"3.18.22\">\n"
2014         "        <conditions>\n"
2015         "            <config>\n"
2016         "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2017         "                <value type=\"int\">24</value>\n"
2018         "            </config>\n"
2019         "        </conditions>\n"
2020         "        <config>\n"
2021         "            <key>CONFIG_ANDROID_BINDER_DEVICES</key>\n"
2022         "            <value type=\"string\">binder</value>\n"
2023         "        </config>\n"
2024         "    </kernel>\n"
2025         "    <sepolicy>\n"
2026         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2027         "    </sepolicy>\n"
2028         "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2029         "</compatibility-matrix>\n";
2030 
2031     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
2032         << gCompatibilityMatrixConverter.lastError();
2033     EXPECT_FALSE(runtime.checkCompatibility(cm, &error)) << "all fragments should be used";
2034 }
2035 
2036 // Run KernelConfigParserInvalidTest on processComments = {true, false}
2037 class KernelConfigParserInvalidTest : public ::testing::TestWithParam<bool> {};
2038 
2039 TEST_P(KernelConfigParserInvalidTest, NonSet1) {
2040     const std::string data = "# CONFIG_NOT_EXIST is not sat\n";
2041     auto pair = processData(data, GetParam() /* processComments */, true /* relaxedFormat */);
2042     ASSERT_EQ(OK, pair.second) << pair.first.error();
2043     const auto& configs = pair.first.configs();
2044     EXPECT_EQ(configs.find("CONFIG_NOT_EXIST"), configs.end())
2045         << "CONFIG_NOT_EXIST should not exist because of typo";
2046 }
2047 
2048 TEST_P(KernelConfigParserInvalidTest, InvalidLine1) {
2049     const std::string data = "FOO_CONFIG=foo\n";
2050     ASSERT_NE(OK,
2051               processData(data, GetParam() /* processComments */, true /* relaxedFormat */).second);
2052 }
2053 
2054 TEST_P(KernelConfigParserInvalidTest, InvalidLine2) {
2055     const std::string data = "CONFIG_BAR-BAZ=foo\n";
2056     ASSERT_NE(OK,
2057               processData(data, GetParam() /* processComments */, true /* relaxedFormat */).second);
2058 }
2059 
2060 INSTANTIATE_TEST_CASE_P(KernelConfigParser, KernelConfigParserInvalidTest, ::testing::Bool());
2061 
2062 TEST_F(LibVintfTest, MatrixLevel) {
2063     CompatibilityMatrix cm;
2064     std::string xml;
2065 
2066     xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\"/>";
2067     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
2068         << gCompatibilityMatrixConverter.lastError();
2069     EXPECT_EQ(Level::UNSPECIFIED, cm.level());
2070 
2071     xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"legacy\"/>";
2072     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
2073         << gCompatibilityMatrixConverter.lastError();
2074     EXPECT_EQ(Level::LEGACY, cm.level());
2075 
2076     xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\"/>";
2077     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
2078         << gCompatibilityMatrixConverter.lastError();
2079     EXPECT_EQ(1u, cm.level());
2080 }
2081 
2082 TEST_F(LibVintfTest, ManifestLevel) {
2083     HalManifest manifest;
2084     std::string xml;
2085 
2086     xml = "<manifest " + kMetaVersionStr + " type=\"device\"/>";
2087     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2088     EXPECT_EQ(Level::UNSPECIFIED, manifest.level());
2089 
2090     xml = "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"legacy\"/>";
2091     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2092     EXPECT_EQ(Level::LEGACY, manifest.level());
2093 
2094     xml = "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\"/>";
2095     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2096     EXPECT_EQ(1u, manifest.level());
2097 }
2098 
2099 TEST_F(LibVintfTest, AddOptionalHal) {
2100     CompatibilityMatrix cm1;
2101     CompatibilityMatrix cm2;
2102     std::string error;
2103     std::string xml;
2104 
2105     xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\"/>";
2106     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm1, xml))
2107         << gCompatibilityMatrixConverter.lastError();
2108 
2109     xml =
2110         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2111         "    <hal format=\"hidl\" optional=\"false\">\n"
2112         "        <name>android.hardware.foo</name>\n"
2113         "        <version>1.0-1</version>\n"
2114         "        <interface>\n"
2115         "            <name>IFoo</name>\n"
2116         "            <instance>default</instance>\n"
2117         "        </interface>\n"
2118         "    </hal>\n"
2119         "</compatibility-matrix>\n";
2120     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml))
2121         << gCompatibilityMatrixConverter.lastError();
2122 
2123     EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2124     xml = gCompatibilityMatrixConverter(cm1, SerializeFlags::HALS_ONLY);
2125     EXPECT_EQ(xml,
2126               "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2127               "    <hal format=\"hidl\" optional=\"true\">\n"
2128               "        <name>android.hardware.foo</name>\n"
2129               "        <version>1.0-1</version>\n"
2130               "        <interface>\n"
2131               "            <name>IFoo</name>\n"
2132               "            <instance>default</instance>\n"
2133               "        </interface>\n"
2134               "    </hal>\n"
2135               "</compatibility-matrix>\n");
2136 }
2137 
2138 TEST_F(LibVintfTest, AddOptionalHalMinorVersion) {
2139     CompatibilityMatrix cm1;
2140     CompatibilityMatrix cm2;
2141     std::string error;
2142     std::string xml;
2143 
2144     xml =
2145         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2146         "    <hal format=\"hidl\" optional=\"false\">\n"
2147         "        <name>android.hardware.foo</name>\n"
2148         "        <version>1.2-3</version>\n"
2149         "        <interface>\n"
2150         "            <name>IFoo</name>\n"
2151         "            <instance>default</instance>\n"
2152         "        </interface>\n"
2153         "    </hal>\n"
2154         "</compatibility-matrix>\n";
2155     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm1, xml))
2156         << gCompatibilityMatrixConverter.lastError();
2157 
2158     xml =
2159         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2160         "    <hal format=\"hidl\" optional=\"false\">\n"
2161         "        <name>android.hardware.foo</name>\n"
2162         "        <version>1.0-4</version>\n"
2163         "        <interface>\n"
2164         "            <name>IFoo</name>\n"
2165         "            <instance>default</instance>\n"
2166         "        </interface>\n"
2167         "    </hal>\n"
2168         "</compatibility-matrix>\n";
2169     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml))
2170         << gCompatibilityMatrixConverter.lastError();
2171 
2172     EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2173     xml = gCompatibilityMatrixConverter(cm1, SerializeFlags::HALS_ONLY);
2174     EXPECT_EQ(xml,
2175               "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2176               "    <hal format=\"hidl\" optional=\"false\">\n"
2177               "        <name>android.hardware.foo</name>\n"
2178               "        <version>1.0-4</version>\n"
2179               "        <interface>\n"
2180               "            <name>IFoo</name>\n"
2181               "            <instance>default</instance>\n"
2182               "        </interface>\n"
2183               "    </hal>\n"
2184               "</compatibility-matrix>\n");
2185 }
2186 
2187 TEST_F(LibVintfTest, AddOptionalHalMajorVersion) {
2188     CompatibilityMatrix cm1;
2189     CompatibilityMatrix cm2;
2190     std::string error;
2191     std::string xml;
2192 
2193     xml =
2194         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2195         "    <hal format=\"hidl\" optional=\"false\">\n"
2196         "        <name>android.hardware.foo</name>\n"
2197         "        <version>1.2-3</version>\n"
2198         "        <interface>\n"
2199         "            <name>IFoo</name>\n"
2200         "            <instance>default</instance>\n"
2201         "        </interface>\n"
2202         "    </hal>\n"
2203         "</compatibility-matrix>\n";
2204     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm1, xml))
2205         << gCompatibilityMatrixConverter.lastError();
2206 
2207     xml =
2208         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2209         "    <hal format=\"hidl\" optional=\"false\">\n"
2210         "        <name>android.hardware.foo</name>\n"
2211         "        <version>1.2-3</version>\n"
2212         "        <version>2.0-4</version>\n"
2213         "        <interface>\n"
2214         "            <name>IFoo</name>\n"
2215         "            <instance>default</instance>\n"
2216         "        </interface>\n"
2217         "    </hal>\n"
2218         "</compatibility-matrix>\n";
2219     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml))
2220         << gCompatibilityMatrixConverter.lastError();
2221 
2222     EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2223     xml = gCompatibilityMatrixConverter(cm1, SerializeFlags::HALS_ONLY);
2224     EXPECT_EQ(xml,
2225               "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2226               "    <hal format=\"hidl\" optional=\"false\">\n"
2227               "        <name>android.hardware.foo</name>\n"
2228               "        <version>1.2-3</version>\n"
2229               "        <version>2.0-4</version>\n"
2230               "        <interface>\n"
2231               "            <name>IFoo</name>\n"
2232               "            <instance>default</instance>\n"
2233               "        </interface>\n"
2234               "    </hal>\n"
2235               "</compatibility-matrix>\n");
2236 }
2237 
2238 TEST_F(LibVintfTest, AddOptionalHalMinorVersionDiffInstance) {
2239     CompatibilityMatrix cm1;
2240     CompatibilityMatrix cm2;
2241     std::string error;
2242     std::string xml;
2243 
2244     xml =
2245         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2246         "    <hal format=\"hidl\" optional=\"false\">\n"
2247         "        <name>android.hardware.foo</name>\n"
2248         "        <version>1.0-1</version>\n"
2249         "        <interface>\n"
2250         "            <name>IFoo</name>\n"
2251         "            <instance>default</instance>\n"
2252         "        </interface>\n"
2253         "    </hal>\n"
2254         "</compatibility-matrix>\n";
2255     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm1, xml))
2256         << gCompatibilityMatrixConverter.lastError();
2257 
2258     xml =
2259         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2260         "    <hal format=\"hidl\" optional=\"false\">\n"
2261         "        <name>android.hardware.foo</name>\n"
2262         "        <version>1.1-2</version>\n"
2263         "        <interface>\n"
2264         "            <name>IFoo</name>\n"
2265         "            <instance>custom</instance>\n"
2266         "        </interface>\n"
2267         "    </hal>\n"
2268         "</compatibility-matrix>\n";
2269     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml))
2270         << gCompatibilityMatrixConverter.lastError();
2271 
2272     EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2273     xml = gCompatibilityMatrixConverter(cm1, SerializeFlags::HALS_ONLY);
2274     EXPECT_EQ(xml,
2275               "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2276               "    <hal format=\"hidl\" optional=\"false\">\n"
2277               "        <name>android.hardware.foo</name>\n"
2278               "        <version>1.0-1</version>\n"
2279               "        <interface>\n"
2280               "            <name>IFoo</name>\n"
2281               "            <instance>default</instance>\n"
2282               "        </interface>\n"
2283               "    </hal>\n"
2284               "    <hal format=\"hidl\" optional=\"true\">\n"
2285               "        <name>android.hardware.foo</name>\n"
2286               "        <version>1.1-2</version>\n"
2287               "        <interface>\n"
2288               "            <name>IFoo</name>\n"
2289               "            <instance>custom</instance>\n"
2290               "        </interface>\n"
2291               "    </hal>\n"
2292               "</compatibility-matrix>\n");
2293 }
2294 
2295 TEST_F(LibVintfTest, AddRequiredHalOverlapInstance) {
2296     CompatibilityMatrix cm1;
2297     std::string error;
2298     std::string xml;
2299 
2300     xml =
2301         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2302         "    <hal format=\"hidl\" optional=\"false\">\n"
2303         "        <name>android.hardware.foo</name>\n"
2304         "        <version>1.0</version>\n"
2305         "        <interface>\n"
2306         "            <name>IFoo</name>\n"
2307         "            <instance>default</instance>\n"
2308         "            <instance>custom</instance>\n"
2309         "        </interface>\n"
2310         "    </hal>\n"
2311         "</compatibility-matrix>\n";
2312     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm1, xml))
2313         << gCompatibilityMatrixConverter.lastError();
2314 
2315     {
2316         // Test that 2.0 should be added to IFoo/default, so 1.0::IFoo/custom
2317         // should be in a new <hal> tag
2318         CompatibilityMatrix cm2;
2319         xml =
2320             "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2321             "    <hal format=\"hidl\" optional=\"false\">\n"
2322             "        <name>android.hardware.foo</name>\n"
2323             "        <version>2.0</version>\n"
2324             "        <interface>\n"
2325             "            <name>IFoo</name>\n"
2326             "            <instance>default</instance>\n"
2327             "        </interface>\n"
2328             "    </hal>\n"
2329             "</compatibility-matrix>\n";
2330         EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml))
2331             << gCompatibilityMatrixConverter.lastError();
2332 
2333         EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2334 
2335         xml = gCompatibilityMatrixConverter(cm1, SerializeFlags::HALS_ONLY);
2336         EXPECT_EQ(xml,
2337                   "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2338                   "    <hal format=\"hidl\" optional=\"false\">\n"
2339                   "        <name>android.hardware.foo</name>\n"
2340                   "        <version>1.0</version>\n"
2341                   "        <interface>\n"
2342                   "            <name>IFoo</name>\n"
2343                   "            <instance>custom</instance>\n"
2344                   "        </interface>\n"
2345                   "    </hal>\n"
2346                   "    <hal format=\"hidl\" optional=\"false\">\n"
2347                   "        <name>android.hardware.foo</name>\n"
2348                   "        <version>1.0</version>\n"
2349                   "        <version>2.0</version>\n"
2350                   "        <interface>\n"
2351                   "            <name>IFoo</name>\n"
2352                   "            <instance>default</instance>\n"
2353                   "        </interface>\n"
2354                   "    </hal>\n"
2355                   "</compatibility-matrix>\n");
2356     }
2357 
2358     {
2359         // Test that 2.0::IFoo/strong should be added as an optional <hal> tag.
2360         CompatibilityMatrix cm2;
2361         xml =
2362             "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2363             "    <hal format=\"hidl\" optional=\"false\">\n"
2364             "        <name>android.hardware.foo</name>\n"
2365             "        <version>2.0</version>\n"
2366             "        <interface>\n"
2367             "            <name>IFoo</name>\n"
2368             "            <instance>default</instance>\n"
2369             "            <instance>strong</instance>\n"
2370             "        </interface>\n"
2371             "    </hal>\n"
2372             "</compatibility-matrix>\n";
2373         EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml))
2374             << gCompatibilityMatrixConverter.lastError();
2375 
2376         EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2377 
2378         xml = gCompatibilityMatrixConverter(cm1, SerializeFlags::HALS_ONLY);
2379         EXPECT_EQ(xml,
2380                   "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2381                   "    <hal format=\"hidl\" optional=\"false\">\n"
2382                   "        <name>android.hardware.foo</name>\n"
2383                   "        <version>1.0</version>\n"
2384                   "        <interface>\n"
2385                   "            <name>IFoo</name>\n"
2386                   "            <instance>custom</instance>\n"
2387                   "        </interface>\n"
2388                   "    </hal>\n"
2389                   "    <hal format=\"hidl\" optional=\"false\">\n"
2390                   "        <name>android.hardware.foo</name>\n"
2391                   "        <version>1.0</version>\n"
2392                   "        <version>2.0</version>\n"
2393                   "        <interface>\n"
2394                   "            <name>IFoo</name>\n"
2395                   "            <instance>default</instance>\n"
2396                   "        </interface>\n"
2397                   "    </hal>\n"
2398                   "    <hal format=\"hidl\" optional=\"true\">\n"
2399                   "        <name>android.hardware.foo</name>\n"
2400                   "        <version>2.0</version>\n"
2401                   "        <interface>\n"
2402                   "            <name>IFoo</name>\n"
2403                   "            <instance>strong</instance>\n"
2404                   "        </interface>\n"
2405                   "    </hal>\n"
2406                   "</compatibility-matrix>\n");
2407     }
2408 }
2409 
2410 TEST_F(LibVintfTest, AddRequiredHalOverlapInstanceSplit) {
2411     CompatibilityMatrix cm1;
2412     CompatibilityMatrix cm2;
2413     std::string error;
2414     std::string xml;
2415 
2416     xml =
2417         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2418         "    <hal format=\"hidl\" optional=\"false\">\n"
2419         "        <name>android.hardware.foo</name>\n"
2420         "        <version>1.0</version>\n"
2421         "        <interface>\n"
2422         "            <name>IFoo</name>\n"
2423         "            <instance>default</instance>\n"
2424         "        </interface>\n"
2425         "    </hal>\n"
2426         "    <hal format=\"hidl\" optional=\"false\">\n"
2427         "        <name>android.hardware.foo</name>\n"
2428         "        <version>1.0</version>\n"
2429         "        <interface>\n"
2430         "            <name>IFoo</name>\n"
2431         "            <instance>custom</instance>\n"
2432         "        </interface>\n"
2433         "    </hal>\n"
2434         "</compatibility-matrix>\n";
2435     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm1, xml))
2436         << gCompatibilityMatrixConverter.lastError();
2437 
2438     xml =
2439         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2440         "    <hal format=\"hidl\" optional=\"false\">\n"
2441         "        <name>android.hardware.foo</name>\n"
2442         "        <version>2.0</version>\n"
2443         "        <interface>\n"
2444         "            <name>IFoo</name>\n"
2445         "            <instance>default</instance>\n"
2446         "        </interface>\n"
2447         "    </hal>\n"
2448         "    <hal format=\"hidl\" optional=\"false\">\n"
2449         "        <name>android.hardware.foo</name>\n"
2450         "        <version>2.0</version>\n"
2451         "        <interface>\n"
2452         "            <name>IFoo</name>\n"
2453         "            <instance>strong</instance>\n"
2454         "        </interface>\n"
2455         "    </hal>\n"
2456         "</compatibility-matrix>\n";
2457     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml))
2458         << gCompatibilityMatrixConverter.lastError();
2459 
2460     EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2461     xml = gCompatibilityMatrixConverter(cm1, SerializeFlags::HALS_ONLY);
2462     EXPECT_EQ(
2463         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2464         "    <hal format=\"hidl\" optional=\"false\">\n"
2465         "        <name>android.hardware.foo</name>\n"
2466         "        <version>1.0</version>\n"
2467         "        <version>2.0</version>\n"
2468         "        <interface>\n"
2469         "            <name>IFoo</name>\n"
2470         "            <instance>default</instance>\n"
2471         "        </interface>\n"
2472         "    </hal>\n"
2473         "    <hal format=\"hidl\" optional=\"false\">\n"
2474         "        <name>android.hardware.foo</name>\n"
2475         "        <version>1.0</version>\n"
2476         "        <interface>\n"
2477         "            <name>IFoo</name>\n"
2478         "            <instance>custom</instance>\n"
2479         "        </interface>\n"
2480         "    </hal>\n"
2481         "    <hal format=\"hidl\" optional=\"true\">\n"
2482         "        <name>android.hardware.foo</name>\n"
2483         "        <version>2.0</version>\n"
2484         "        <interface>\n"
2485         "            <name>IFoo</name>\n"
2486         "            <instance>strong</instance>\n"
2487         "        </interface>\n"
2488         "    </hal>\n"
2489         "</compatibility-matrix>\n",
2490         xml);
2491 }
2492 TEST_F(LibVintfTest, AddOptionalXmlFile) {
2493     CompatibilityMatrix cm1;
2494     CompatibilityMatrix cm2;
2495     std::string error;
2496     std::string xml;
2497 
2498     xml =
2499         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2500         "    <xmlfile format=\"xsd\" optional=\"true\">\n"
2501         "        <name>foo</name>\n"
2502         "        <version>1.0-2</version>\n"
2503         "        <path>/foo/bar/baz.xsd</path>\n"
2504         "    </xmlfile>\n"
2505         "</compatibility-matrix>\n";
2506     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm1, xml))
2507         << gCompatibilityMatrixConverter.lastError();
2508 
2509     xml =
2510         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2511         "    <xmlfile format=\"xsd\" optional=\"true\">\n"
2512         "        <name>foo</name>\n"
2513         "        <version>1.1-3</version>\n"
2514         "        <path>/foo/bar/quux.xsd</path>\n"
2515         "    </xmlfile>\n"
2516         "</compatibility-matrix>\n";
2517     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm2, xml))
2518         << gCompatibilityMatrixConverter.lastError();
2519 
2520     EXPECT_TRUE(addAllXmlFilesAsOptional(&cm1, &cm2, &error)) << error;
2521     xml = gCompatibilityMatrixConverter(cm1, SerializeFlags::XMLFILES_ONLY);
2522     EXPECT_EQ(xml,
2523               "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2524               "    <xmlfile format=\"xsd\" optional=\"true\">\n"
2525               "        <name>foo</name>\n"
2526               "        <version>1.0-2</version>\n"
2527               "        <path>/foo/bar/baz.xsd</path>\n"
2528               "    </xmlfile>\n"
2529               "    <xmlfile format=\"xsd\" optional=\"true\">\n"
2530               "        <name>foo</name>\n"
2531               "        <version>1.1-3</version>\n"
2532               "        <path>/foo/bar/quux.xsd</path>\n"
2533               "    </xmlfile>\n"
2534               "</compatibility-matrix>\n");
2535 }
2536 
2537 TEST_F(LibVintfTest, VendorNdk) {
2538     CompatibilityMatrix cm;
2539     std::string error;
2540     std::string xml;
2541 
2542     xml =
2543         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
2544         "    <vendor-ndk>\n"
2545         "        <version>P</version>\n"
2546         "        <library>libbase.so</library>\n"
2547         "        <library>libjpeg.so</library>\n"
2548         "    </vendor-ndk>\n"
2549         "</compatibility-matrix>\n";
2550     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
2551         << gCompatibilityMatrixConverter.lastError();
2552     EXPECT_EQ(xml, gCompatibilityMatrixConverter(cm));
2553 
2554     EXPECT_EQ("P", cm.getVendorNdkVersion());
2555 
2556     {
2557         HalManifest manifest;
2558         xml =
2559             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2560             "    <vendor-ndk>\n"
2561             "        <version>27</version>\n"
2562             "        <library>libbase.so</library>\n"
2563             "        <library>libjpeg.so</library>\n"
2564             "    </vendor-ndk>\n"
2565             "    <vendor-ndk>\n"
2566             "        <version>P</version>\n"
2567             "        <library>libbase.so</library>\n"
2568             "        <library>libjpeg.so</library>\n"
2569             "        <library>libtinyxml2.so</library>\n"
2570             "    </vendor-ndk>\n"
2571             "</manifest>\n";
2572 
2573         EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2574         EXPECT_EQ(xml, gHalManifestConverter(manifest));
2575         EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
2576     }
2577 
2578     {
2579         HalManifest manifest;
2580         xml =
2581             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2582             "    <vendor-ndk>\n"
2583             "        <version>27</version>\n"
2584             "        <library>libbase.so</library>\n"
2585             "        <library>libjpeg.so</library>\n"
2586             "    </vendor-ndk>\n"
2587             "</manifest>\n";
2588 
2589         EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2590         EXPECT_EQ(xml, gHalManifestConverter(manifest));
2591         EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
2592         EXPECT_IN("Vndk version P is not supported.", error);
2593     }
2594 
2595     {
2596         HalManifest manifest;
2597         xml =
2598             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2599             "    <vendor-ndk>\n"
2600             "        <version>P</version>\n"
2601             "        <library>libbase.so</library>\n"
2602             "    </vendor-ndk>\n"
2603             "</manifest>\n";
2604 
2605         EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2606         EXPECT_EQ(xml, gHalManifestConverter(manifest));
2607         EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
2608         EXPECT_IN("Vndk libs incompatible for version P.", error);
2609         EXPECT_IN("libjpeg.so", error);
2610     }
2611 }
2612 
2613 TEST_F(LibVintfTest, MissingVendorNdkInMatrix) {
2614     CompatibilityMatrix cm;
2615     std::string xml;
2616     std::string error;
2617 
2618     xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"device\"/>\n";
2619     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
2620         << gCompatibilityMatrixConverter.lastError();
2621 
2622     {
2623         HalManifest manifest;
2624         xml = "<manifest " + kMetaVersionStr + " type=\"framework\"/>\n";
2625         EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2626 
2627         EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
2628     }
2629 
2630     {
2631         HalManifest manifest;
2632         xml =
2633             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2634             "    <vendor-ndk>\n"
2635             "        <version>P</version>\n"
2636             "        <library>libbase.so</library>\n"
2637             "    </vendor-ndk>\n"
2638             "</manifest>\n";
2639         EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2640 
2641         EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
2642     }
2643 }
2644 
2645 TEST_F(LibVintfTest, DuplicatedVendorNdkVersion) {
2646     std::string error;
2647     HalManifest manifest;
2648     std::string xml =
2649         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2650         "    <vendor-ndk>\n"
2651         "        <version>27</version>\n"
2652         "    </vendor-ndk>\n"
2653         "    <vendor-ndk>\n"
2654         "        <version>27</version>\n"
2655         "    </vendor-ndk>\n"
2656         "</manifest>\n";
2657 
2658     EXPECT_FALSE(gHalManifestConverter(&manifest, xml));
2659     EXPECT_EQ("Duplicated manifest.vendor-ndk.version 27", gHalManifestConverter.lastError());
2660 }
2661 
2662 TEST_F(LibVintfTest, ManifestHalOverride) {
2663     HalManifest manifest;
2664     std::string xml =
2665         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2666         "    <hal format=\"hidl\" override=\"true\">\n"
2667         "        <name>android.hardware.foo</name>\n"
2668         "        <transport>hwbinder</transport>\n"
2669         "        <version>1.0</version>\n"
2670         "    </hal>\n"
2671         "    <hal format=\"hidl\">\n"
2672         "        <name>android.hardware.bar</name>\n"
2673         "        <transport>hwbinder</transport>\n"
2674         "        <version>1.0</version>\n"
2675         "    </hal>\n"
2676         "</manifest>\n";
2677     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2678     const auto& foo = getHals(manifest, "android.hardware.foo");
2679     ASSERT_FALSE(foo.empty());
2680     EXPECT_TRUE(foo.front()->isOverride());
2681     const auto& bar = getHals(manifest, "android.hardware.bar");
2682     ASSERT_FALSE(bar.empty());
2683     EXPECT_FALSE(bar.front()->isOverride());
2684 }
2685 
2686 // Test functionality of override="true" tag
2687 TEST_F(LibVintfTest, ManifestAddOverrideHalSimple) {
2688     HalManifest manifest;
2689     std::string xml = "<manifest " + kMetaVersionStr + " type=\"device\"/>\n";
2690     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2691 
2692     HalManifest newManifest;
2693     xml =
2694         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2695         "    <hal format=\"hidl\" override=\"true\">\n"
2696         "        <name>android.hardware.foo</name>\n"
2697         "        <transport>hwbinder</transport>\n"
2698         "        <version>1.1</version>\n"
2699         "        <interface>\n"
2700         "            <name>IFoo</name>\n"
2701         "            <instance>default</instance>\n"
2702         "        </interface>\n"
2703         "    </hal>\n"
2704         "</manifest>\n";
2705     EXPECT_TRUE(gHalManifestConverter(&newManifest, xml)) << gHalManifestConverter.lastError();
2706 
2707     manifest.addAllHals(&newManifest);
2708     EXPECT_EQ(xml, gHalManifestConverter(manifest, SerializeFlags::HALS_NO_FQNAME));
2709 }
2710 
2711 TEST_F(LibVintfTest, ManifestAddOverrideHalSimpleOverride) {
2712     HalManifest manifest;
2713     std::string xml =
2714         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2715         "    <hal format=\"hidl\">\n"
2716         "        <name>android.hardware.foo</name>\n"
2717         "        <transport>hwbinder</transport>\n"
2718         "        <version>1.0</version>\n"
2719         "    </hal>\n"
2720         "</manifest>\n";
2721     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2722 
2723     HalManifest newManifest;
2724     xml =
2725         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2726         "    <hal format=\"hidl\" override=\"true\">\n"
2727         "        <name>android.hardware.foo</name>\n"
2728         "        <transport>hwbinder</transport>\n"
2729         "        <version>1.1</version>\n"
2730         "        <interface>\n"
2731         "            <name>IFoo</name>\n"
2732         "            <instance>default</instance>\n"
2733         "        </interface>\n"
2734         "    </hal>\n"
2735         "</manifest>\n";
2736     EXPECT_TRUE(gHalManifestConverter(&newManifest, xml)) << gHalManifestConverter.lastError();
2737 
2738     manifest.addAllHals(&newManifest);
2739     EXPECT_EQ(xml, gHalManifestConverter(manifest, SerializeFlags::HALS_NO_FQNAME));
2740 }
2741 
2742 // Existing major versions should be removed.
2743 TEST_F(LibVintfTest, ManifestAddOverrideHalMultiVersion) {
2744     HalManifest manifest;
2745     std::string xml =
2746         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2747         "    <hal format=\"hidl\">\n"
2748         "        <name>android.hardware.foo</name>\n"
2749         "        <transport>hwbinder</transport>\n"
2750         "        <version>1.3</version>\n"
2751         "        <version>2.4</version>\n"
2752         "        <interface>\n"
2753         "            <name>IFoo</name>\n"
2754         "            <instance>slot1</instance>\n"
2755         "        </interface>\n"
2756         "    </hal>\n"
2757         "    <hal format=\"hidl\">\n"
2758         "        <name>android.hardware.bar</name>\n"
2759         "        <transport>hwbinder</transport>\n"
2760         "        <version>1.3</version>\n"
2761         "    </hal>\n"
2762         "</manifest>\n";
2763     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2764 
2765     HalManifest newManifest;
2766     xml =
2767         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2768         "    <hal format=\"hidl\" override=\"true\">\n"
2769         "        <name>android.hardware.foo</name>\n"
2770         "        <transport>hwbinder</transport>\n"
2771         "        <version>1.1</version>\n"
2772         "        <version>3.1</version>\n"
2773         "        <interface>\n"
2774         "            <name>IFoo</name>\n"
2775         "            <instance>slot2</instance>\n"
2776         "        </interface>\n"
2777         "    </hal>\n"
2778         "</manifest>\n";
2779     EXPECT_TRUE(gHalManifestConverter(&newManifest, xml)) << gHalManifestConverter.lastError();
2780 
2781     manifest.addAllHals(&newManifest);
2782     EXPECT_EQ(
2783         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2784         "    <hal format=\"hidl\">\n"
2785         "        <name>android.hardware.bar</name>\n"
2786         "        <transport>hwbinder</transport>\n"
2787         "        <version>1.3</version>\n"
2788         "    </hal>\n"
2789         "    <hal format=\"hidl\">\n"
2790         "        <name>android.hardware.foo</name>\n"
2791         "        <transport>hwbinder</transport>\n"
2792         "        <version>2.4</version>\n"
2793         "        <interface>\n"
2794         "            <name>IFoo</name>\n"
2795         "            <instance>slot1</instance>\n"
2796         "        </interface>\n"
2797         "    </hal>\n"
2798         "    <hal format=\"hidl\" override=\"true\">\n"
2799         "        <name>android.hardware.foo</name>\n"
2800         "        <transport>hwbinder</transport>\n"
2801         "        <version>1.1</version>\n"
2802         "        <version>3.1</version>\n"
2803         "        <interface>\n"
2804         "            <name>IFoo</name>\n"
2805         "            <instance>slot2</instance>\n"
2806         "        </interface>\n"
2807         "    </hal>\n"
2808         "</manifest>\n",
2809         gHalManifestConverter(manifest, SerializeFlags::HALS_NO_FQNAME));
2810 }
2811 
2812 TEST_F(LibVintfTest, ManifestAddOverrideHalMultiVersion2) {
2813     HalManifest manifest;
2814     std::string xml =
2815         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2816         "    <hal format=\"hidl\">\n"
2817         "        <name>android.hardware.foo</name>\n"
2818         "        <transport>hwbinder</transport>\n"
2819         "        <version>1.3</version>\n"
2820         "        <version>2.4</version>\n"
2821         "        <interface>\n"
2822         "            <name>IFoo</name>\n"
2823         "            <instance>slot1</instance>\n"
2824         "        </interface>\n"
2825         "    </hal>\n"
2826         "</manifest>\n";
2827     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2828 
2829     HalManifest newManifest;
2830     xml =
2831         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2832         "    <hal format=\"hidl\" override=\"true\">\n"
2833         "        <name>android.hardware.foo</name>\n"
2834         "        <transport>hwbinder</transport>\n"
2835         "        <version>1.1</version>\n"
2836         "        <version>2.1</version>\n"
2837         "        <interface>\n"
2838         "            <name>IFoo</name>\n"
2839         "            <instance>slot2</instance>\n"
2840         "        </interface>\n"
2841         "    </hal>\n"
2842         "</manifest>\n";
2843     EXPECT_TRUE(gHalManifestConverter(&newManifest, xml)) << gHalManifestConverter.lastError();
2844 
2845     manifest.addAllHals(&newManifest);
2846     EXPECT_EQ(xml, gHalManifestConverter(manifest, SerializeFlags::HALS_NO_FQNAME));
2847 }
2848 
2849 // if no <versions>, remove all existing <hal> with given <name>.
2850 TEST_F(LibVintfTest, ManifestAddOverrideHalRemoveAll) {
2851     HalManifest manifest;
2852     std::string xml =
2853         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2854         "    <hal format=\"hidl\">\n"
2855         "        <name>android.hardware.foo</name>\n"
2856         "        <transport>hwbinder</transport>\n"
2857         "        <version>1.3</version>\n"
2858         "        <version>2.4</version>\n"
2859         "        <interface>\n"
2860         "            <name>IFoo</name>\n"
2861         "            <instance>slot1</instance>\n"
2862         "        </interface>\n"
2863         "    </hal>\n"
2864         "    <hal format=\"hidl\">\n"
2865         "        <name>android.hardware.foo</name>\n"
2866         "        <transport>hwbinder</transport>\n"
2867         "        <version>3.1</version>\n"
2868         "        <version>4.3</version>\n"
2869         "        <interface>\n"
2870         "            <name>IBar</name>\n"
2871         "            <instance>slot2</instance>\n"
2872         "        </interface>\n"
2873         "    </hal>\n"
2874         "    <hal format=\"hidl\">\n"
2875         "        <name>android.hardware.bar</name>\n"
2876         "        <transport>hwbinder</transport>\n"
2877         "        <version>1.3</version>\n"
2878         "    </hal>\n"
2879         "</manifest>\n";
2880     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2881 
2882     HalManifest newManifest;
2883     xml =
2884         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2885         "    <hal format=\"hidl\" override=\"true\">\n"
2886         "        <name>android.hardware.foo</name>\n"
2887         "        <transport>hwbinder</transport>\n"
2888         "    </hal>\n"
2889         "</manifest>\n";
2890     EXPECT_TRUE(gHalManifestConverter(&newManifest, xml)) << gHalManifestConverter.lastError();
2891 
2892     manifest.addAllHals(&newManifest);
2893     EXPECT_EQ(
2894         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
2895         "    <hal format=\"hidl\">\n"
2896         "        <name>android.hardware.bar</name>\n"
2897         "        <transport>hwbinder</transport>\n"
2898         "        <version>1.3</version>\n"
2899         "    </hal>\n"
2900         "    <hal format=\"hidl\" override=\"true\">\n"
2901         "        <name>android.hardware.foo</name>\n"
2902         "        <transport>hwbinder</transport>\n"
2903         "    </hal>\n"
2904         "</manifest>\n",
2905         gHalManifestConverter(manifest, SerializeFlags::HALS_ONLY));
2906 }
2907 
2908 // Make sure missing tags in old VINTF files does not cause incompatibilities.
2909 TEST_F(LibVintfTest, Empty) {
2910     CompatibilityMatrix cm;
2911     HalManifest manifest;
2912     std::string xml;
2913     std::string error;
2914 
2915     xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"device\"/>\n";
2916     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
2917         << gCompatibilityMatrixConverter.lastError();
2918 
2919     xml = "<manifest " + kMetaVersionStr + " type=\"framework\"/>\n";
2920     EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2921 
2922     EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
2923 }
2924 
2925 TEST_F(LibVintfTest, SystemSdk) {
2926     CompatibilityMatrix cm;
2927     std::string xml;
2928     std::string error;
2929 
2930     xml =
2931         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
2932         "    <system-sdk>\n"
2933         "        <version>1</version>\n"
2934         "        <version>P</version>\n"
2935         "    </system-sdk>\n"
2936         "</compatibility-matrix>\n";
2937     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml))
2938         << gCompatibilityMatrixConverter.lastError();
2939     EXPECT_EQ(xml, gCompatibilityMatrixConverter(cm, SerializeFlags::SSDK_ONLY));
2940 
2941     {
2942         HalManifest manifest;
2943         xml =
2944             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2945             "    <system-sdk>\n"
2946             "        <version>1</version>\n"
2947             "        <version>P</version>\n"
2948             "    </system-sdk>\n"
2949             "</manifest>\n";
2950         EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2951         EXPECT_EQ(xml, gHalManifestConverter(manifest, SerializeFlags::SSDK_ONLY));
2952 
2953         EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
2954     }
2955 
2956     {
2957         HalManifest manifest;
2958         xml =
2959             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2960             "    <system-sdk>\n"
2961             "        <version>1</version>\n"
2962             "        <version>3</version>\n"
2963             "        <version>P</version>\n"
2964             "    </system-sdk>\n"
2965             "</manifest>\n";
2966         EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2967         EXPECT_TRUE(manifest.checkCompatibility(cm, &error));
2968     }
2969 
2970     {
2971         HalManifest manifest;
2972         xml =
2973             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2974             "    <system-sdk>\n"
2975             "        <version>1</version>\n"
2976             "    </system-sdk>\n"
2977             "</manifest>\n";
2978         EXPECT_TRUE(gHalManifestConverter(&manifest, xml)) << gHalManifestConverter.lastError();
2979         EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
2980         EXPECT_TRUE(error.find("System SDK") != std::string::npos) << error;
2981     }
2982 }
2983 
2984 TEST_F(LibVintfTest, ManifestLastError) {
2985     HalManifest e;
2986     // Set mLastError to something else before testing.
2987     EXPECT_FALSE(gHalManifestConverter(&e, "<manifest/>"));
2988     EXPECT_NE("Not a valid XML", gHalManifestConverter.lastError());
2989 
2990     std::string error;
2991     std::string prevError = gHalManifestConverter.lastError();
2992     EXPECT_FALSE(gHalManifestConverter(&e, "", &error));
2993     EXPECT_EQ("Not a valid XML", error);
2994     EXPECT_EQ(prevError, gHalManifestConverter.lastError()) << "lastError() should not be modified";
2995 }
2996 
2997 TEST_F(LibVintfTest, MatrixLastError) {
2998     CompatibilityMatrix e;
2999     // Set mLastError to something else before testing.
3000     EXPECT_FALSE(gCompatibilityMatrixConverter(&e, "<compatibility-matrix/>"));
3001     EXPECT_NE("Not a valid XML", gCompatibilityMatrixConverter.lastError());
3002 
3003     std::string error;
3004     std::string prevError = gCompatibilityMatrixConverter.lastError();
3005     EXPECT_FALSE(gCompatibilityMatrixConverter(&e, "", &error));
3006     EXPECT_EQ("Not a valid XML", error);
3007     EXPECT_EQ(prevError, gCompatibilityMatrixConverter.lastError())
3008         << "lastError() should not be modified";
3009 }
3010 
3011 TEST_F(LibVintfTest, MatrixDetailErrorMsg) {
3012     std::string error;
3013     std::string xml;
3014 
3015     HalManifest manifest;
3016     xml =
3017         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"103\">\n"
3018         "    <hal format=\"hidl\">\n"
3019         "        <name>android.hardware.foo</name>\n"
3020         "        <transport>hwbinder</transport>\n"
3021         "        <version>1.0</version>\n"
3022         "        <interface>\n"
3023         "            <name>IFoo</name>\n"
3024         "            <instance>default</instance>\n"
3025         "        </interface>\n"
3026         "    </hal>\n"
3027         "</manifest>\n";
3028     ASSERT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3029 
3030     {
3031         CompatibilityMatrix cm;
3032         xml =
3033             "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"100\">\n"
3034             "    <hal format=\"hidl\" optional=\"false\">\n"
3035             "        <name>android.hardware.foo</name>\n"
3036             "        <version>1.2-3</version>\n"
3037             "        <version>4.5</version>\n"
3038             "        <interface>\n"
3039             "            <name>IFoo</name>\n"
3040             "            <instance>default</instance>\n"
3041             "            <instance>slot1</instance>\n"
3042             "        </interface>\n"
3043             "        <interface>\n"
3044             "            <name>IBar</name>\n"
3045             "            <instance>default</instance>\n"
3046             "        </interface>\n"
3047             "    </hal>\n"
3048             "</compatibility-matrix>\n";
3049         EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml, &error)) << error;
3050         EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
3051         EXPECT_IN("Manifest level = 103", error)
3052         EXPECT_IN("Matrix level = 100", error)
3053         EXPECT_IN(
3054             "android.hardware.foo:\n"
3055             "    required: \n"
3056             "        (@1.2-3::IBar/default AND @1.2-3::IFoo/default AND @1.2-3::IFoo/slot1) OR\n"
3057             "        (@4.5::IBar/default AND @4.5::IFoo/default AND @4.5::IFoo/slot1)\n"
3058             "    provided: @1.0::IFoo/default",
3059             error);
3060     }
3061 
3062     {
3063         CompatibilityMatrix cm;
3064         xml =
3065             "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
3066             "    <hal format=\"hidl\" optional=\"false\">\n"
3067             "        <name>android.hardware.foo</name>\n"
3068             "        <version>1.2-3</version>\n"
3069             "        <interface>\n"
3070             "            <name>IFoo</name>\n"
3071             "            <instance>default</instance>\n"
3072             "            <instance>slot1</instance>\n"
3073             "        </interface>\n"
3074             "    </hal>\n"
3075             "</compatibility-matrix>\n";
3076         EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml, &error)) << error;
3077         EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
3078         EXPECT_IN(
3079             "android.hardware.foo:\n"
3080             "    required: (@1.2-3::IFoo/default AND @1.2-3::IFoo/slot1)\n"
3081             "    provided: @1.0::IFoo/default",
3082             error);
3083     }
3084 
3085     // the most frequent use case.
3086     {
3087         CompatibilityMatrix cm;
3088         xml =
3089             "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
3090             "    <hal format=\"hidl\" optional=\"false\">\n"
3091             "        <name>android.hardware.foo</name>\n"
3092             "        <version>1.2-3</version>\n"
3093             "        <interface>\n"
3094             "            <name>IFoo</name>\n"
3095             "            <instance>default</instance>\n"
3096             "        </interface>\n"
3097             "    </hal>\n"
3098             "</compatibility-matrix>\n";
3099         EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml, &error)) << error;
3100         EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
3101         EXPECT_IN(
3102             "android.hardware.foo:\n"
3103             "    required: @1.2-3::IFoo/default\n"
3104             "    provided: @1.0::IFoo/default",
3105             error);
3106     }
3107 }
3108 
3109 TEST_F(LibVintfTest, DisabledHal) {
3110     std::string error;
3111     std::string xml;
3112     HalManifest manifest;
3113     xml =
3114         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3115         "    <hal format=\"hidl\" override=\"true\">\n"
3116         "        <transport>hwbinder</transport>\n"
3117         "        <name>android.hardware.foo</name>\n"
3118         "        <transport>hwbinder</transport>\n"
3119         "    </hal>\n"
3120         "    <hal format=\"hidl\" override=\"true\">\n"
3121         "        <name>android.hardware.bar</name>\n"
3122         "        <transport>hwbinder</transport>\n"
3123         "        <fqname>@1.1::IFoo/custom</fqname>\n"
3124         "    </hal>\n"
3125         "    <hal format=\"hidl\">\n"
3126         "        <name>android.hardware.baz</name>\n"
3127         "        <transport>hwbinder</transport>\n"
3128         "    </hal>\n"
3129         "</manifest>\n";
3130     ASSERT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3131 
3132     auto foo = getHals(manifest, "android.hardware.foo");
3133     ASSERT_EQ(1u, foo.size());
3134     EXPECT_TRUE(foo.front()->isDisabledHal());
3135     auto bar = getHals(manifest, "android.hardware.bar");
3136     ASSERT_EQ(1u, bar.size());
3137     EXPECT_FALSE(bar.front()->isDisabledHal());
3138     auto baz = getHals(manifest, "android.hardware.baz");
3139     ASSERT_EQ(1u, baz.size());
3140     EXPECT_FALSE(baz.front()->isDisabledHal());
3141 }
3142 
3143 TEST_F(LibVintfTest, FqNameValid) {
3144     std::string error;
3145     std::string xml;
3146 
3147     CompatibilityMatrix cm;
3148     xml =
3149         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
3150         "    <hal format=\"hidl\" optional=\"false\">\n"
3151         "        <name>android.hardware.foo</name>\n"
3152         "        <version>1.0</version>\n"
3153         "        <interface>\n"
3154         "            <name>IFoo</name>\n"
3155         "            <instance>default</instance>\n"
3156         "        </interface>\n"
3157         "    </hal>\n"
3158         "    <hal format=\"hidl\" optional=\"false\">\n"
3159         "        <name>android.hardware.foo</name>\n"
3160         "        <version>1.1</version>\n"
3161         "        <interface>\n"
3162         "            <name>IFoo</name>\n"
3163         "            <instance>custom</instance>\n"
3164         "        </interface>\n"
3165         "    </hal>\n"
3166         "</compatibility-matrix>\n";
3167     EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml, &error)) << error;
3168 
3169     {
3170         HalManifest manifest;
3171         xml =
3172             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3173             "    <hal format=\"hidl\">\n"
3174             "        <name>android.hardware.foo</name>\n"
3175             "        <transport>hwbinder</transport>\n"
3176             "        <version>1.0</version>\n"
3177             "        <interface>\n"
3178             "            <name>IFoo</name>\n"
3179             "            <instance>default</instance>\n"
3180             "            <instance>custom</instance>\n"
3181             "        </interface>\n"
3182             "        <fqname>@1.1::IFoo/custom</fqname>\n"
3183             "    </hal>\n"
3184             "</manifest>\n";
3185         ASSERT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3186         EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
3187 
3188         EXPECT_EQ(Transport::HWBINDER,
3189                   manifest.getHidlTransport("android.hardware.foo", {1, 1}, "IFoo", "custom"));
3190     }
3191 
3192     {
3193         HalManifest manifest;
3194         xml =
3195             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3196             "    <hal format=\"hidl\">\n"
3197             "        <name>android.hardware.foo</name>\n"
3198             "        <transport>hwbinder</transport>\n"
3199             "        <fqname>@1.0::IFoo/default</fqname>\n"
3200             "        <fqname>@1.1::IFoo/custom</fqname>\n"
3201             "    </hal>\n"
3202             "</manifest>\n";
3203         ASSERT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3204         EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
3205     }
3206 
3207     {
3208         HalManifest manifest;
3209         xml =
3210             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3211             "    <hal format=\"hidl\">\n"
3212             "        <name>android.hardware.foo</name>\n"
3213             "        <transport>hwbinder</transport>\n"
3214             "        <version>1.0</version>\n"
3215             "        <interface>\n"
3216             "            <name>IFoo</name>\n"
3217             "            <instance>default</instance>\n"
3218             "            <instance>custom</instance>\n"
3219             "        </interface>\n"
3220             "    </hal>\n"
3221             "</manifest>\n";
3222         ASSERT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3223         EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
3224         EXPECT_IN(
3225             "android.hardware.foo:\n"
3226             "    required: @1.1::IFoo/custom\n"
3227             "    provided: \n"
3228             "        @1.0::IFoo/custom\n"
3229             "        @1.0::IFoo/default",
3230             error);
3231     }
3232 
3233     {
3234         HalManifest manifest;
3235         xml =
3236             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3237             "    <hal format=\"hidl\">\n"
3238             "        <name>android.hardware.foo</name>\n"
3239             "        <transport>hwbinder</transport>\n"
3240             "        <fqname>@1.0::IFoo/default</fqname>\n"
3241             "        <fqname>@1.0::IFoo/custom</fqname>\n"
3242             "    </hal>\n"
3243             "</manifest>\n";
3244         ASSERT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3245         EXPECT_IN(
3246             "android.hardware.foo:\n"
3247             "    required: @1.1::IFoo/custom\n"
3248             "    provided: \n"
3249             "        @1.0::IFoo/custom\n"
3250             "        @1.0::IFoo/default",
3251             error);
3252     }
3253 }
3254 
3255 TEST_F(LibVintfTest, FqNameInvalid) {
3256     std::string error;
3257     std::string xml;
3258     ManifestHal hal;
3259     xml =
3260         "<hal format=\"hidl\">\n"
3261         "    <name>android.hardware.foo</name>\n"
3262         "    <transport>hwbinder</transport>\n"
3263         "    <fqname>@1.1::IFoo/custom</fqname>\n"
3264         "</hal>\n";
3265     EXPECT_TRUE(gManifestHalConverter(&hal, xml, &error)) << error;
3266     xml =
3267         "<hal format=\"hidl\">\n"
3268         "    <name>android.hardware.foo</name>\n"
3269         "    <transport>hwbinder</transport>\n"
3270         "    <fqname>1.1::IFoo/custom</fqname>\n"
3271         "</hal>\n";
3272     ASSERT_FALSE(gManifestHalConverter(&hal, xml, &error));
3273     EXPECT_IN("Could not parse text \"1.1::IFoo/custom\" in element <fqname>", error);
3274     xml =
3275         "<hal format=\"hidl\">\n"
3276         "    <name>android.hardware.foo</name>\n"
3277         "    <transport>hwbinder</transport>\n"
3278         "    <fqname>[email protected]::IFoo/custom</fqname>\n"
3279         "</hal>\n";
3280     ASSERT_FALSE(gManifestHalConverter(&hal, xml, &error));
3281     EXPECT_IN("Should not specify package", error);
3282     xml =
3283         "<hal format=\"hidl\">\n"
3284         "    <name>android.hardware.foo</name>\n"
3285         "    <transport>hwbinder</transport>\n"
3286         "    <fqname>IFoo/custom</fqname>\n"
3287         "</hal>\n";
3288     ASSERT_FALSE(gManifestHalConverter(&hal, xml, &error));
3289     EXPECT_IN("Should specify version", error);
3290     xml =
3291         "<hal format=\"hidl\">\n"
3292         "    <name>android.hardware.foo</name>\n"
3293         "    <transport>hwbinder</transport>\n"
3294         "    <fqname>@1.0::IFoo</fqname>\n"
3295         "</hal>\n";
3296     ASSERT_FALSE(gManifestHalConverter(&hal, xml, &error));
3297     EXPECT_IN("Should specify instance", error);
3298     xml =
3299         "<hal format=\"hidl\">\n"
3300         "    <name>n07 4 v4l1d 1n73rf4c3</name>\n"
3301         "    <transport>hwbinder</transport>\n"
3302         "    <fqname>@1.0::IFoo/custom</fqname>\n"
3303         "</hal>\n";
3304     ASSERT_FALSE(gManifestHalConverter(&hal, xml, &error));
3305     EXPECT_IN("Cannot create FqInstance", error);
3306     EXPECT_IN("n07 4 v4l1d 1n73rf4c3", error);
3307 }
3308 
3309 TEST_F(LibVintfTest, RegexInstanceValid) {
3310     CompatibilityMatrix matrix;
3311     std::string error;
3312 
3313     std::string xml =
3314         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
3315         "    <hal format=\"hidl\" optional=\"false\">\n"
3316         "        <name>android.hardware.foo</name>\n"
3317         "        <version>1.0</version>\n"
3318         "        <interface>\n"
3319         "            <name>IFoo</name>\n"
3320         "            <regex-instance>legacy/[0-9]+</regex-instance>\n"
3321         "            <regex-instance>slot[0-9]+</regex-instance>\n"
3322         "            <regex-instance>.*</regex-instance>\n"
3323         "        </interface>\n"
3324         "    </hal>\n"
3325         "</compatibility-matrix>\n";
3326     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, xml, &error)) << error;
3327 }
3328 
3329 TEST_F(LibVintfTest, RegexInstanceInvalid) {
3330     CompatibilityMatrix matrix;
3331     std::string error;
3332     std::string xml =
3333         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
3334         "    <hal format=\"hidl\" optional=\"false\">\n"
3335         "        <name>android.hardware.foo</name>\n"
3336         "        <version>1.0</version>\n"
3337         "        <interface>\n"
3338         "            <name>IFoo</name>\n"
3339         "            <regex-instance>e{1,2,3}</regex-instance>\n"
3340         "            <regex-instance>*</regex-instance>\n"
3341         "            <regex-instance>+</regex-instance>\n"
3342         "            <regex-instance>[0-9]+</regex-instance>\n"
3343         "            <regex-instance>[0-9]+</regex-instance>\n"
3344         "        </interface>\n"
3345         "    </hal>\n"
3346         "</compatibility-matrix>\n";
3347     EXPECT_FALSE(gCompatibilityMatrixConverter(&matrix, xml, &error));
3348     EXPECT_IN("Invalid regular expression 'e{1,2,3}'", error);
3349     EXPECT_IN("Invalid regular expression '*'", error);
3350     EXPECT_IN("Invalid regular expression '+'", error);
3351     EXPECT_IN("Duplicated regex-instance '[0-9]+'", error);
3352 }
3353 
3354 TEST_F(LibVintfTest, RegexInstanceCompat) {
3355     CompatibilityMatrix matrix;
3356     std::string error;
3357 
3358     std::string xml =
3359         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
3360         "    <hal format=\"hidl\" optional=\"false\">\n"
3361         "        <name>android.hardware.foo</name>\n"
3362         "        <version>1.0</version>\n"
3363         "        <version>3.1-2</version>\n"
3364         "        <interface>\n"
3365         "            <name>IFoo</name>\n"
3366         "            <instance>default</instance>\n"
3367         "            <regex-instance>legacy/[0-9]+</regex-instance>\n"
3368         "        </interface>\n"
3369         "    </hal>\n"
3370         "    <sepolicy>\n"
3371         "        <kernel-sepolicy-version>0</kernel-sepolicy-version>\n"
3372         "        <sepolicy-version>0.0</sepolicy-version>\n"
3373         "    </sepolicy>\n"
3374         "</compatibility-matrix>\n";
3375     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, xml))
3376         << gCompatibilityMatrixConverter.lastError();
3377 
3378     {
3379         std::string xml =
3380             "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3381             "    <hal format=\"hidl\">\n"
3382             "        <name>android.hardware.foo</name>\n"
3383             "        <transport>hwbinder</transport>\n"
3384             "        <version>1.0</version>\n"
3385             "        <interface>\n"
3386             "            <name>IFoo</name>\n"
3387             "            <instance>default</instance>\n"
3388             "            <instance>legacy/0</instance>\n"
3389             "            <instance>legacy/1</instance>\n"
3390             "        </interface>\n"
3391             "    </hal>\n"
3392             "</manifest>\n";
3393 
3394         HalManifest manifest;
3395         EXPECT_TRUE(gHalManifestConverter(&manifest, xml));
3396         EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
3397 
3398         auto unused = checkUnusedHals(manifest, matrix);
3399         EXPECT_TRUE(unused.empty())
3400             << "Contains unused HALs: " << android::base::Join(unused, "\n");
3401     }
3402 
3403     {
3404         std::string xml =
3405             "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3406             "    <hal format=\"hidl\">\n"
3407             "        <name>android.hardware.foo</name>\n"
3408             "        <transport>hwbinder</transport>\n"
3409             "        <version>1.0</version>\n"
3410             "        <interface>\n"
3411             "            <name>IFoo</name>\n"
3412             "            <instance>default</instance>\n"
3413             "            <instance>legacy0</instance>\n"
3414             "            <instance>nonmatch/legacy/0</instance>\n"
3415             "            <instance>legacy/0/nonmatch</instance>\n"
3416             "        </interface>\n"
3417             "    </hal>\n"
3418             "</manifest>\n";
3419 
3420         HalManifest manifest;
3421         EXPECT_TRUE(gHalManifestConverter(&manifest, xml));
3422         EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
3423             << "Should not be compatible because no legacy/[0-9]+ is provided.";
3424 
3425         auto unused = checkUnusedHals(manifest, matrix);
3426         EXPECT_EQ((std::set<std::string>{"[email protected]::IFoo/nonmatch/legacy/0",
3427                                          "[email protected]::IFoo/legacy/0/nonmatch",
3428                                          "[email protected]::IFoo/legacy0"}),
3429                   unused);
3430     }
3431 }
3432 
3433 TEST_F(LibVintfTest, Regex) {
3434     details::Regex regex;
3435 
3436     EXPECT_FALSE(regex.compile("+"));
3437     EXPECT_FALSE(regex.compile("*"));
3438 
3439     ASSERT_TRUE(regex.compile("legacy/[0-9]+"));
3440     EXPECT_TRUE(regex.matches("legacy/0"));
3441     EXPECT_TRUE(regex.matches("legacy/000"));
3442     EXPECT_FALSE(regex.matches("legacy/"));
3443     EXPECT_FALSE(regex.matches("ssslegacy/0"));
3444     EXPECT_FALSE(regex.matches("legacy/0sss"));
3445 }
3446 
3447 TEST_F(LibVintfTest, ManifestGetHalNamesAndVersions) {
3448     HalManifest vm = testDeviceManifest();
3449     EXPECT_EQ(vm.getHalNamesAndVersions(),
3450               std::set<std::string>({"[email protected]", "[email protected]"}));
3451 }
3452 
3453 TEST_F(LibVintfTest, KernelInfo) {
3454     KernelInfo ki = testKernelInfo();
3455 
3456     EXPECT_EQ(
3457         "<kernel version=\"3.18.31\">\n"
3458         "    <config>\n"
3459         "        <key>CONFIG_64BIT</key>\n"
3460         "        <value>y</value>\n"
3461         "    </config>\n"
3462         "    <config>\n"
3463         "        <key>CONFIG_ANDROID_BINDER_DEVICES</key>\n"
3464         "        <value>\"binder,hwbinder\"</value>\n"
3465         "    </config>\n"
3466         "    <config>\n"
3467         "        <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
3468         "        <value>24</value>\n"
3469         "    </config>\n"
3470         "    <config>\n"
3471         "        <key>CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES</key>\n"
3472         "        <value>\"\"</value>\n"
3473         "    </config>\n"
3474         "    <config>\n"
3475         "        <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
3476         "        <value>0xdead000000000000</value>\n"
3477         "    </config>\n"
3478         "</kernel>\n",
3479         gKernelInfoConverter(ki, SerializeFlags::NO_TAGS.enableKernelConfigs()));
3480 }
3481 
3482 TEST_F(LibVintfTest, ManifestAddAllDeviceManifest) {
3483     std::string xml1 = "<manifest " + kMetaVersionStr + " type=\"device\" />\n";
3484     std::string xml2 =
3485         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"3\">\n"
3486         "    <hal format=\"hidl\">\n"
3487         "        <name>android.hardware.foo</name>\n"
3488         "        <transport>hwbinder</transport>\n"
3489         "        <fqname>@1.0::IFoo/default</fqname>\n"
3490         "    </hal>\n"
3491         "    <sepolicy>\n"
3492         "        <version>25.5</version>\n"
3493         "    </sepolicy>\n"
3494         "    <kernel version=\"3.18.31\">\n"
3495         "        <config>\n"
3496         "            <key>CONFIG_64BIT</key>\n"
3497         "            <value>y</value>\n"
3498         "        </config>\n"
3499         "    </kernel>\n"
3500         "    <xmlfile>\n"
3501         "        <name>media_profile</name>\n"
3502         "        <version>1.0</version>\n"
3503         "    </xmlfile>\n"
3504         "</manifest>\n";
3505 
3506     std::string error;
3507     HalManifest manifest1;
3508     ASSERT_TRUE(gHalManifestConverter(&manifest1, xml1, &error)) << error;
3509     HalManifest manifest2;
3510     ASSERT_TRUE(gHalManifestConverter(&manifest2, xml2, &error)) << error;
3511 
3512     ASSERT_TRUE(manifest1.addAll(&manifest2, &error)) << error;
3513 
3514     EXPECT_EQ(xml2, gHalManifestConverter(manifest1));
3515 }
3516 
3517 TEST_F(LibVintfTest, ManifestAddAllFrameworkManifest) {
3518     std::string xml1 = "<manifest " + kMetaVersionStr + " type=\"framework\" />\n";
3519     std::string xml2 =
3520         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3521         "    <hal format=\"hidl\">\n"
3522         "        <name>android.hardware.foo</name>\n"
3523         "        <transport>hwbinder</transport>\n"
3524         "        <fqname>@1.0::IFoo/default</fqname>\n"
3525         "    </hal>\n"
3526         "    <vendor-ndk>\n"
3527         "        <version>P</version>\n"
3528         "        <library>libbase.so</library>\n"
3529         "    </vendor-ndk>\n"
3530         "    <system-sdk>\n"
3531         "        <version>1</version>\n"
3532         "    </system-sdk>\n"
3533         "    <xmlfile>\n"
3534         "        <name>media_profile</name>\n"
3535         "        <version>1.0</version>\n"
3536         "    </xmlfile>\n"
3537         "</manifest>\n";
3538 
3539     std::string error;
3540     HalManifest manifest1;
3541     ASSERT_TRUE(gHalManifestConverter(&manifest1, xml1, &error)) << error;
3542     HalManifest manifest2;
3543     ASSERT_TRUE(gHalManifestConverter(&manifest2, xml2, &error)) << error;
3544 
3545     ASSERT_TRUE(manifest1.addAll(&manifest2, &error)) << error;
3546 
3547     EXPECT_EQ(xml2, gHalManifestConverter(manifest1));
3548 }
3549 
3550 TEST_F(LibVintfTest, ManifestAddAllConflictLevel) {
3551     std::string xml1 = "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"2\" />\n";
3552     std::string xml2 = "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"3\" />\n";
3553 
3554     std::string error;
3555     HalManifest manifest1;
3556     ASSERT_TRUE(gHalManifestConverter(&manifest1, xml1, &error)) << error;
3557     HalManifest manifest2;
3558     ASSERT_TRUE(gHalManifestConverter(&manifest2, xml2, &error)) << error;
3559 
3560     ASSERT_FALSE(manifest1.addAll(&manifest2, &error));
3561     EXPECT_IN("Conflicting target-level", error);
3562 }
3563 
3564 TEST_F(LibVintfTest, ManifestAddAllConflictSepolicy) {
3565     std::string xml1 =
3566         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3567         "    <sepolicy>\n"
3568         "        <version>25.5</version>\n"
3569         "    </sepolicy>\n"
3570         "</manifest>\n";
3571     std::string xml2 =
3572         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3573         "    <sepolicy>\n"
3574         "        <version>30.0</version>\n"
3575         "    </sepolicy>\n"
3576         "</manifest>\n";
3577 
3578     std::string error;
3579     HalManifest manifest1;
3580     ASSERT_TRUE(gHalManifestConverter(&manifest1, xml1, &error)) << error;
3581     HalManifest manifest2;
3582     ASSERT_TRUE(gHalManifestConverter(&manifest2, xml2, &error)) << error;
3583 
3584     ASSERT_FALSE(manifest1.addAll(&manifest2, &error));
3585     EXPECT_IN("Conflicting sepolicy version", error);
3586 }
3587 
3588 TEST_F(LibVintfTest, ManifestAddAllConflictKernel) {
3589     std::string xml1 =
3590         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3591         "    <kernel version=\"3.18.0\" />\n"
3592         "</manifest>\n";
3593     std::string xml2 =
3594         "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3595         "    <kernel version=\"3.18.1\" />\n"
3596         "</manifest>\n";
3597 
3598     std::string error;
3599     HalManifest manifest1;
3600     ASSERT_TRUE(gHalManifestConverter(&manifest1, xml1, &error)) << error;
3601     HalManifest manifest2;
3602     ASSERT_TRUE(gHalManifestConverter(&manifest2, xml2, &error)) << error;
3603 
3604     ASSERT_FALSE(manifest1.addAll(&manifest2, &error));
3605     EXPECT_IN("Conflicting kernel", error);
3606 }
3607 
3608 TEST_F(LibVintfTest, ManifestMetaVersionCompat) {
3609     std::string xml = "<manifest version=\"2.0\" type=\"device\" />";
3610     std::string error;
3611     HalManifest manifest;
3612     EXPECT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3613 }
3614 
3615 TEST_F(LibVintfTest, ManifestMetaVersionIncompat) {
3616     std::string xml = "<manifest version=\"10000.0\" type=\"device\" />";
3617     std::string error;
3618     HalManifest manifest;
3619     EXPECT_FALSE(gHalManifestConverter(&manifest, xml, &error))
3620         << "Should not parse metaversion 10000.0";
3621 }
3622 
3623 TEST_F(LibVintfTest, ManifestMetaVersionWriteLatest) {
3624     std::string xml = "<manifest version=\"1.0\" type=\"device\" />";
3625     std::string error;
3626     HalManifest manifest;
3627     EXPECT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3628     EXPECT_IN(kMetaVersionStr, gHalManifestConverter(manifest, SerializeFlags::NO_TAGS));
3629 }
3630 
3631 TEST_F(LibVintfTest, MatrixMetaVersionCompat) {
3632     std::string xml = "<compatibility-matrix version=\"2.0\" type=\"framework\" />";
3633     std::string error;
3634     CompatibilityMatrix matrix;
3635     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, xml, &error)) << error;
3636 }
3637 
3638 TEST_F(LibVintfTest, MatrixMetaVersionIncompat) {
3639     std::string xml = "<compatibility-matrix version=\"10000.0\" type=\"framework\" />";
3640     std::string error;
3641     CompatibilityMatrix matrix;
3642     EXPECT_FALSE(gCompatibilityMatrixConverter(&matrix, xml, &error))
3643         << "Should not parse metaversion 10000.0";
3644 }
3645 
3646 TEST_F(LibVintfTest, MatrixMetaVersionWriteLatest) {
3647     std::string xml = "<compatibility-matrix version=\"1.0\" type=\"framework\" />";
3648     std::string error;
3649     CompatibilityMatrix matrix;
3650     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, xml, &error)) << error;
3651     EXPECT_IN(kMetaVersionStr, gCompatibilityMatrixConverter(matrix, SerializeFlags::NO_TAGS));
3652 }
3653 
3654 TEST_F(LibVintfTest, Aidl) {
3655     std::string xml =
3656         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
3657         "    <hal format=\"aidl\" optional=\"false\">\n"
3658         "        <name>android.system.foo</name>\n"
3659         "        <interface>\n"
3660         "            <name>IFoo</name>\n"
3661         "            <instance>default</instance>\n"
3662         "            <regex-instance>test.*</regex-instance>\n"
3663         "        </interface>\n"
3664         "    </hal>\n"
3665         "</compatibility-matrix>\n";
3666     std::string error;
3667     CompatibilityMatrix matrix;
3668     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, xml, &error)) << error;
3669     EXPECT_EQ(xml, gCompatibilityMatrixConverter(matrix, SerializeFlags::HALS_NO_FQNAME));
3670 
3671     {
3672         HalManifest manifest;
3673         std::string manifestXml =
3674             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3675             "    <hal format=\"aidl\">\n"
3676             "        <name>android.system.foo</name>\n"
3677             "        <interface>\n"
3678             "            <name>IFoo</name>\n"
3679             "            <instance>default</instance>\n"
3680             "            <instance>test0</instance>\n"
3681             "        </interface>\n"
3682             "    </hal>\n"
3683             "</manifest>\n";
3684         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml, &error)) << error;
3685         EXPECT_EQ(manifestXml, gHalManifestConverter(manifest, SerializeFlags::HALS_NO_FQNAME));
3686         EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
3687         EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "default"));
3688         EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "test0"));
3689         EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IFoo", "does_not_exist"));
3690         EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IDoesNotExist", "default"));
3691         EXPECT_FALSE(manifest.hasAidlInstance("android.system.does_not_exist", "IFoo", "default"));
3692         EXPECT_EQ(manifest.getAidlInstances("android.system.foo", "IFoo"),
3693                   std::set<std::string>({"default", "test0"}));
3694         EXPECT_EQ(manifest.getAidlInstances("android.system.does_not_exist", "IFoo"),
3695                   std::set<std::string>({}));
3696     }
3697 
3698     {
3699         HalManifest manifest;
3700         std::string manifestXml =
3701             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3702             "    <hal format=\"aidl\">\n"
3703             "        <name>android.system.foo</name>\n"
3704             "        <fqname>IFoo/default</fqname>\n"
3705             "        <fqname>IFoo/test0</fqname>\n"
3706             "    </hal>\n"
3707             "</manifest>\n";
3708         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml, &error)) << error;
3709         EXPECT_EQ(manifestXml, gHalManifestConverter(manifest, SerializeFlags::HALS_ONLY));
3710         EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
3711         EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "default"));
3712         EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "test0"));
3713         EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IFoo", "does_not_exist"));
3714         EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IDoesNotExist", "default"));
3715         EXPECT_FALSE(manifest.hasAidlInstance("android.system.does_not_exist", "IFoo", "default"));
3716         EXPECT_EQ(manifest.getAidlInstances("android.system.foo", "IFoo"),
3717                   std::set<std::string>({"default", "test0"}));
3718         EXPECT_EQ(manifest.getAidlInstances("android.system.does_not_exist", "IFoo"),
3719                   std::set<std::string>({}));
3720     }
3721 
3722     {
3723         HalManifest manifest;
3724         std::string manifestXml =
3725             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3726             "    <hal format=\"aidl\">\n"
3727             "        <name>android.system.foo</name>\n"
3728             "        <interface>\n"
3729             "            <name>IFoo</name>\n"
3730             "            <instance>incompat_instance</instance>\n"
3731             "            <instance>test0</instance>\n"
3732             "        </interface>\n"
3733             "    </hal>\n"
3734             "</manifest>\n";
3735         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml, &error)) << error;
3736         EXPECT_EQ(manifestXml, gHalManifestConverter(manifest, SerializeFlags::HALS_NO_FQNAME));
3737         EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
3738             << "Should not be compatible because default instance is missing";
3739         EXPECT_IN("required: (IFoo/default AND IFoo/test.*)", error);
3740         EXPECT_IN("provided: \n"
3741                   "        IFoo/incompat_instance\n"
3742                   "        IFoo/test0",
3743                   error);
3744     }
3745     {
3746         HalManifest manifest;
3747         std::string manifestXml =
3748             "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3749             "    <hal format=\"aidl\">\n"
3750             "        <name>android.system.foo</name>\n"
3751             "        <interface>\n"
3752             "            <name>IFoo</name>\n"
3753             "            <instance>default</instance>\n"
3754             "            <instance>incompat_instance</instance>\n"
3755             "        </interface>\n"
3756             "    </hal>\n"
3757             "</manifest>\n";
3758         EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml, &error)) << error;
3759         EXPECT_EQ(manifestXml, gHalManifestConverter(manifest, SerializeFlags::HALS_NO_FQNAME));
3760         EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
3761             << "Should not be compatible because test.* instance is missing";
3762         EXPECT_IN("required: (IFoo/default AND IFoo/test.*)", error);
3763         EXPECT_IN("provided: \n"
3764                   "        IFoo/default\n"
3765                   "        IFoo/incompat_instance\n",
3766                   error);
3767     }
3768 }
3769 
3770 TEST_F(LibVintfTest, AidlAndHidlNamesMatrix) {
3771     std::string xml =
3772         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
3773         "    <hal format=\"aidl\" optional=\"true\">\n"
3774         "        <name>android.system.foo</name>\n"
3775         "        <interface>\n"
3776         "            <name>IFoo</name>\n"
3777         "            <instance>default</instance>\n"
3778         "        </interface>\n"
3779         "    </hal>\n"
3780         "    <hal format=\"hidl\" optional=\"true\">\n"
3781         "        <name>android.system.foo</name>\n"
3782         "        <version>1.0</version>\n"
3783         "        <interface>\n"
3784         "            <name>IFoo</name>\n"
3785         "            <instance>default</instance>\n"
3786         "        </interface>\n"
3787         "    </hal>\n"
3788         "</compatibility-matrix>\n";
3789     std::string error;
3790     CompatibilityMatrix matrix;
3791     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, xml, &error)) << error;
3792     EXPECT_EQ(xml, gCompatibilityMatrixConverter(matrix, SerializeFlags::HALS_ONLY));
3793 }
3794 
3795 TEST_F(LibVintfTest, AidlAndHidlNamesManifest) {
3796     std::string xml =
3797         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3798         "    <hal format=\"aidl\">\n"
3799         "        <name>android.system.foo</name>\n"
3800         "        <fqname>IFoo/default</fqname>\n"
3801         "    </hal>\n"
3802         "    <hal format=\"hidl\">\n"
3803         "        <name>android.system.foo</name>\n"
3804         "        <transport>hwbinder</transport>\n"
3805         "        <fqname>@1.0::IFoo/default</fqname>\n"
3806         "    </hal>\n"
3807         "</manifest>\n";
3808     std::string error;
3809     HalManifest manifest;
3810     EXPECT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3811     EXPECT_EQ(xml, gHalManifestConverter(manifest, SerializeFlags::HALS_ONLY));
3812 }
3813 
3814 TEST_F(LibVintfTest, AidlAndHidlCheckUnused) {
3815     std::string manifestXml =
3816         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3817         "    <hal format=\"aidl\">\n"
3818         "        <name>android.system.foo</name>\n"
3819         "        <fqname>IFoo/default</fqname>\n"
3820         "    </hal>\n"
3821         "    <hal format=\"hidl\">\n"
3822         "        <name>android.system.foo</name>\n"
3823         "        <transport>hwbinder</transport>\n"
3824         "        <fqname>@1.0::IFoo/default</fqname>\n"
3825         "    </hal>\n"
3826         "</manifest>\n";
3827     std::string matrixXml =
3828         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
3829         "    <hal format=\"aidl\" optional=\"true\">\n"
3830         "        <name>android.system.foo</name>\n"
3831         "        <interface>\n"
3832         "            <name>IFoo</name>\n"
3833         "            <instance>default</instance>\n"
3834         "        </interface>\n"
3835         "    </hal>\n"
3836         "    <hal format=\"hidl\" optional=\"true\">\n"
3837         "        <name>android.system.foo</name>\n"
3838         "        <version>1.0</version>\n"
3839         "        <interface>\n"
3840         "            <name>IFoo</name>\n"
3841         "            <instance>default</instance>\n"
3842         "        </interface>\n"
3843         "    </hal>\n"
3844         "</compatibility-matrix>\n";
3845     std::string error;
3846     HalManifest manifest;
3847     CompatibilityMatrix matrix;
3848 
3849     EXPECT_TRUE(gHalManifestConverter(&manifest, manifestXml, &error)) << error;
3850     EXPECT_TRUE(gCompatibilityMatrixConverter(&matrix, matrixXml, &error)) << error;
3851     auto unused = checkUnusedHals(manifest, matrix);
3852     EXPECT_TRUE(unused.empty()) << android::base::Join(unused, "\n");
3853 }
3854 
3855 TEST_F(LibVintfTest, GetTransportHidlHalWithFakeAidlVersion) {
3856     std::string xml =
3857         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3858         "    <hal format=\"hidl\">\n"
3859         "        <name>android.system.foo</name>\n"
3860         "        <transport>hwbinder</transport>\n"
3861         "        <fqname>@" + to_string(details::kFakeAidlVersion) + "::IFoo/default</fqname>\n"
3862         "    </hal>\n"
3863         "</manifest>\n";
3864     std::string error;
3865     HalManifest manifest;
3866     EXPECT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3867     EXPECT_EQ(Transport::HWBINDER,
3868               manifest.getHidlTransport("android.system.foo", details::kFakeAidlVersion, "IFoo",
3869                                         "default"));
3870 }
3871 
3872 TEST_F(LibVintfTest, GetTransportAidlHalWithDummyTransport) {
3873     // Check that even if <transport> is specified for AIDL, it is ignored and getHidlTransport
3874     // will return EMPTY.
3875     std::string xml =
3876         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3877         "    <hal format=\"aidl\">\n"
3878         "        <name>android.system.foo</name>\n"
3879         "        <transport>hwbinder</transport>\n"
3880         "        <fqname>IFoo/default</fqname>\n"
3881         "    </hal>\n"
3882         "</manifest>\n";
3883     std::string error;
3884     HalManifest manifest;
3885     EXPECT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3886     EXPECT_EQ(Transport::EMPTY,
3887               manifest.getHidlTransport("android.system.foo", details::kFakeAidlVersion, "IFoo",
3888                                         "default"));
3889 }
3890 
3891 TEST_F(LibVintfTest, AidlGetHalNamesAndVersions) {
3892     HalManifest manifest;
3893     std::string xml =
3894         "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3895         "    <hal format=\"aidl\">\n"
3896         "        <name>android.system.foo</name>\n"
3897         "        <fqname>IFoo/default</fqname>\n"
3898         "    </hal>\n"
3899         "</manifest>\n";
3900     std::string error;
3901     EXPECT_TRUE(gHalManifestConverter(&manifest, xml, &error)) << error;
3902     auto names = manifest.getHalNamesAndVersions();
3903     ASSERT_EQ(1u, names.size());
3904     EXPECT_EQ("android.system.foo", *names.begin());
3905 }
3906 
3907 TEST_F(LibVintfTest, KernelInfoLevel) {
3908     std::string error;
3909     std::string xml = "<kernel version=\"3.18.31\" target-level=\"1\"/>\n";
3910     KernelInfo ki;
3911     ASSERT_TRUE(gKernelInfoConverter(&ki, xml, &error)) << error;
3912     EXPECT_EQ(Level{1}, ki.level());
3913     EXPECT_EQ(xml, gKernelInfoConverter(ki));
3914 }
3915 
3916 // Test merge of <kernel target-level=""> with autogenerated <kernel> by parsing
3917 // kernel prebuilt.
3918 TEST_F(LibVintfTest, HalManifestMergeKernel) {
3919     std::string head =
3920         "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\">\n";
3921     std::string tail =
3922         "</manifest>\n";
3923     std::string xml1 =
3924         "    <kernel target-level=\"2\"/>\n";
3925     std::string xml2 =
3926         "    <kernel version=\"3.18.31\">\n"
3927         "        <config>\n"
3928         "            <key>CONFIG_64BIT</key>\n"
3929         "            <value>y</value>\n"
3930         "        </config>\n"
3931         "    </kernel>\n";
3932 
3933     std::string error;
3934     HalManifest manifest1;
3935     HalManifest manifest2;
3936     ASSERT_TRUE(gHalManifestConverter(&manifest1, head + xml1 + tail, &error)) << error;
3937     ASSERT_TRUE(gHalManifestConverter(&manifest2, head + xml2 + tail, &error)) << error;
3938     ASSERT_TRUE(manifest1.addAll(&manifest2, &error)) << error;
3939     std::string merged_xml = gHalManifestConverter(manifest1);
3940     EXPECT_IN(head, merged_xml);
3941     EXPECT_IN("target-level=\"2\"", merged_xml);
3942     EXPECT_IN("version=\"3.18.31\"", merged_xml);
3943     EXPECT_IN("CONFIG_64BIT", merged_xml);
3944 }
3945 
3946 struct FrameworkCompatibilityMatrixCombineTest : public LibVintfTest {
3947     virtual void SetUp() override {
3948         matrices = {
3949             {"compatibility_matrix.1_1.xml", CompatibilityMatrix{}},
3950             {"compatibility_matrix.1_2.xml", CompatibilityMatrix{}},
3951         };
3952     }
3953     // Access to private methods.
3954     std::unique_ptr<CompatibilityMatrix> combine(Level deviceLevel,
3955                                                  std::vector<Named<CompatibilityMatrix>>* matrices,
3956                                                  std::string* error) {
3957         return CompatibilityMatrix::combine(deviceLevel, matrices, error);
3958     }
3959 
3960     std::vector<Named<CompatibilityMatrix>> matrices;
3961     std::string error;
3962 };
3963 
3964 // Combining framework compatibility matrix with conflicting minlts fails
3965 TEST_F(FrameworkCompatibilityMatrixCombineTest, ConflictMinlts) {
3966     ASSERT_TRUE(gCompatibilityMatrixConverter(
3967         &matrices[0].object,
3968         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
3969         "    <kernel version=\"3.18.5\" />\n"
3970         "</compatibility-matrix>\n",
3971         &error))
3972         << error;
3973     ASSERT_TRUE(gCompatibilityMatrixConverter(
3974         &matrices[1].object,
3975         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
3976         "    <kernel version=\"3.18.6\" />\n"
3977         "</compatibility-matrix>\n",
3978         &error))
3979         << error;
3980 
3981     auto combined = combine(Level{1}, &matrices, &error);
3982     ASSERT_EQ(nullptr, combined) << gCompatibilityMatrixConverter(*combined);
3983     EXPECT_IN("Kernel version mismatch", error);
3984 }
3985 
3986 // <kernel> without <conditions> always comes first
3987 TEST_F(FrameworkCompatibilityMatrixCombineTest, KernelNoConditions) {
3988     std::string conditionedKernel =
3989         "    <kernel version=\"3.18.5\" level=\"1\">\n"
3990         "        <conditions>\n"
3991         "            <config>\n"
3992         "                <key>CONFIG_ARM</key>\n"
3993         "                <value type=\"tristate\">y</value>\n"
3994         "            </config>\n"
3995         "        </conditions>\n"
3996         "        <config>\n"
3997         "            <key>CONFIG_FOO</key>\n"
3998         "            <value type=\"tristate\">y</value>\n"
3999         "        </config>\n"
4000         "    </kernel>\n";
4001     std::string simpleKernel =
4002         "    <kernel version=\"3.18.5\" level=\"1\">\n"
4003         "        <config>\n"
4004         "            <key>CONFIG_BAR</key>\n"
4005         "            <value type=\"tristate\">y</value>\n"
4006         "        </config>\n"
4007         "    </kernel>\n";
4008 
4009     ASSERT_TRUE(gCompatibilityMatrixConverter(
4010         &matrices[0].object,
4011         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
4012         "    <kernel version=\"3.18.5\" />\n" +
4013             conditionedKernel + "</compatibility-matrix>\n",
4014         &error))
4015         << error;
4016     ASSERT_TRUE(gCompatibilityMatrixConverter(
4017         &matrices[1].object,
4018         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n" + simpleKernel +
4019             "</compatibility-matrix>\n",
4020         &error))
4021         << error;
4022 
4023     auto combined = combine(Level{1}, &matrices, &error);
4024     ASSERT_NE(nullptr, combined);
4025     EXPECT_EQ("", error);
4026     EXPECT_EQ("<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n" +
4027                   simpleKernel + conditionedKernel + "</compatibility-matrix>\n",
4028               gCompatibilityMatrixConverter(*combined));
4029 }
4030 
4031 // Combining framework compatibility matrix with conflicting sepolicy fails
4032 TEST_F(FrameworkCompatibilityMatrixCombineTest, ConflictSepolicy) {
4033     ASSERT_TRUE(gCompatibilityMatrixConverter(
4034         &matrices[0].object,
4035         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
4036         "    <sepolicy>\n"
4037         "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
4038         "    </sepolicy>\n"
4039         "</compatibility-matrix>\n",
4040         &error))
4041         << error;
4042     ASSERT_TRUE(gCompatibilityMatrixConverter(
4043         &matrices[1].object,
4044         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
4045         "    <sepolicy>\n"
4046         "        <kernel-sepolicy-version>29</kernel-sepolicy-version>\n"
4047         "    </sepolicy>\n"
4048         "</compatibility-matrix>\n",
4049         &error))
4050         << error;
4051 
4052     auto combined = combine(Level{1}, &matrices, &error);
4053     ASSERT_EQ(nullptr, combined) << gCompatibilityMatrixConverter(*combined);
4054     EXPECT_IN("<sepolicy> is already defined", error);
4055 }
4056 
4057 // Combining framework compatibility matrix with conflicting avb fails
4058 TEST_F(FrameworkCompatibilityMatrixCombineTest, ConflictAvb) {
4059     ASSERT_TRUE(gCompatibilityMatrixConverter(
4060         &matrices[0].object,
4061         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
4062         "    <avb>\n"
4063         "        <vbmeta-version>1.1</vbmeta-version>\n"
4064         "    </avb>\n"
4065         "</compatibility-matrix>\n",
4066         &error))
4067         << error;
4068     ASSERT_TRUE(gCompatibilityMatrixConverter(
4069         &matrices[1].object,
4070         "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
4071         "    <avb>\n"
4072         "        <vbmeta-version>1.0</vbmeta-version>\n"
4073         "    </avb>\n"
4074         "</compatibility-matrix>\n",
4075         &error))
4076         << error;
4077 
4078     auto combined = combine(Level{1}, &matrices, &error);
4079     ASSERT_EQ(nullptr, combined) << gCompatibilityMatrixConverter(*combined);
4080     EXPECT_IN("<avb><vbmeta-version> is already defined", error);
4081 }
4082 
4083 TEST_F(FrameworkCompatibilityMatrixCombineTest, AidlAndHidlNames) {
4084     std::string head1{"<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"};
4085     std::string head2{"<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"};
4086     std::string tail{"</compatibility-matrix>\n"};
4087     std::string aidl =
4088         "    <hal format=\"aidl\" optional=\"false\">\n"
4089         "        <name>android.system.foo</name>\n"
4090         "        <interface>\n"
4091         "            <name>IFoo</name>\n"
4092         "            <instance>default</instance>\n"
4093         "        </interface>\n"
4094         "    </hal>\n";
4095     std::string hidl =
4096         "    <hal format=\"hidl\" optional=\"false\">\n"
4097         "        <name>android.system.foo</name>\n"
4098         "        <version>1.0</version>\n"
4099         "        <interface>\n"
4100         "            <name>IFoo</name>\n"
4101         "            <instance>default</instance>\n"
4102         "        </interface>\n"
4103         "    </hal>\n";
4104     std::string aidlOptional = std::string(aidl).replace(hidl.find("false"), 5, "true");
4105     std::string hidlOptional = std::string(hidl).replace(hidl.find("false"), 5, "true");
4106     std::string error;
4107     {
4108         ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[0].object, head1 + aidl + tail, &error))
4109             << error;
4110         ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[1].object, head1 + hidl + tail, &error))
4111             << error;
4112 
4113         auto combined = combine(Level{1}, &matrices, &error);
4114         ASSERT_NE(nullptr, combined) << error;
4115 
4116         auto combinedXml = gCompatibilityMatrixConverter(*combined);
4117         EXPECT_IN(aidl, combinedXml);
4118         EXPECT_IN(hidl, combinedXml);
4119     }
4120     {
4121         ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[0].object, head1 + aidl + tail, &error))
4122             << error;
4123         ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[1].object, head2 + hidl + tail, &error))
4124             << error;
4125 
4126         auto combined = combine(Level{1}, &matrices, &error);
4127         ASSERT_NE(nullptr, combined) << error;
4128 
4129         auto combinedXml = gCompatibilityMatrixConverter(*combined);
4130         EXPECT_IN(aidl, combinedXml);
4131         EXPECT_IN(hidlOptional, combinedXml);
4132     }
4133     {
4134         ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[0].object, head2 + aidl + tail, &error))
4135             << error;
4136         ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[1].object, head1 + hidl + tail, &error))
4137             << error;
4138 
4139         auto combined = combine(Level{1}, &matrices, &error);
4140         ASSERT_NE(nullptr, combined) << error;
4141 
4142         auto combinedXml = gCompatibilityMatrixConverter(*combined);
4143         EXPECT_IN(aidlOptional, combinedXml);
4144         EXPECT_IN(hidl, combinedXml);
4145     }
4146 }
4147 
4148 struct DeviceCompatibilityMatrixCombineTest : public LibVintfTest {
4149     virtual void SetUp() override {
4150         matrices = {
4151             {"compatibility_matrix.1.xml", CompatibilityMatrix{}},
4152             {"compatibility_matrix.2.xml", CompatibilityMatrix{}},
4153         };
4154     }
4155     // Access to private methods.
4156     std::unique_ptr<CompatibilityMatrix> combine(std::vector<Named<CompatibilityMatrix>>* matrices,
4157                                                  std::string* error) {
4158         return CompatibilityMatrix::combineDeviceMatrices(matrices, error);
4159     }
4160 
4161     std::vector<Named<CompatibilityMatrix>> matrices;
4162     std::string error;
4163 };
4164 
4165 TEST_F(DeviceCompatibilityMatrixCombineTest, Success) {
4166     std::string head{"<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"};
4167     std::string tail{"</compatibility-matrix>\n"};
4168     std::string halFoo{
4169         "    <hal format=\"hidl\" optional=\"false\">\n"
4170         "        <name>android.hardware.foo</name>\n"
4171         "        <version>1.0</version>\n"
4172         "        <interface>\n"
4173         "            <name>IFoo</name>\n"
4174         "            <instance>default</instance>\n"
4175         "        </interface>\n"
4176         "    </hal>\n"};
4177     std::string halBar{
4178         "    <hal format=\"hidl\" optional=\"false\">\n"
4179         "        <name>android.hardware.bar</name>\n"
4180         "        <version>1.0</version>\n"
4181         "        <interface>\n"
4182         "            <name>IBar</name>\n"
4183         "            <instance>default</instance>\n"
4184         "        </interface>\n"
4185         "    </hal>\n"};
4186     ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[0].object, head + halFoo + tail, &error))
4187         << error;
4188     ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[1].object, head + halBar + tail, &error))
4189         << error;
4190 
4191     auto combined = combine(&matrices, &error);
4192     ASSERT_NE(nullptr, combined) << error;
4193     EXPECT_EQ("", error);
4194     auto combinedXml = gCompatibilityMatrixConverter(*combined);
4195     EXPECT_IN(halFoo, combinedXml);
4196     EXPECT_IN(halBar, combinedXml);
4197 }
4198 
4199 TEST_F(DeviceCompatibilityMatrixCombineTest, ConflictVendorNdk) {
4200     std::string vendorNdkP{
4201         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
4202         "    <vendor-ndk>\n"
4203         "        <version>P</version>\n"
4204         "    </vendor-ndk>\n"
4205         "</compatibility-matrix>\n"};
4206     std::string vendorNdkQ{
4207         "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
4208         "    <vendor-ndk>\n"
4209         "        <version>Q</version>\n"
4210         "    </vendor-ndk>\n"
4211         "</compatibility-matrix>\n"};
4212     ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[0].object, vendorNdkP, &error)) << error;
4213     ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[1].object, vendorNdkQ, &error)) << error;
4214 
4215     auto combined = combine(&matrices, &error);
4216     ASSERT_EQ(nullptr, combined) << gCompatibilityMatrixConverter(*combined);
4217     EXPECT_IN("<vendor-ndk> is already defined", error);
4218 }
4219 
4220 TEST_F(DeviceCompatibilityMatrixCombineTest, AidlAndHidlNames) {
4221     std::string head{"<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"};
4222     std::string tail{"</compatibility-matrix>\n"};
4223     std::string aidl =
4224         "    <hal format=\"aidl\" optional=\"true\">\n"
4225         "        <name>android.system.foo</name>\n"
4226         "        <interface>\n"
4227         "            <name>IFoo</name>\n"
4228         "            <instance>default</instance>\n"
4229         "        </interface>\n"
4230         "    </hal>\n";
4231     std::string hidl =
4232         "    <hal format=\"hidl\" optional=\"true\">\n"
4233         "        <name>android.system.foo</name>\n"
4234         "        <version>1.0</version>\n"
4235         "        <interface>\n"
4236         "            <name>IFoo</name>\n"
4237         "            <instance>default</instance>\n"
4238         "        </interface>\n"
4239         "    </hal>\n";
4240     ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[0].object, head + aidl + tail, &error))
4241         << error;
4242     ASSERT_TRUE(gCompatibilityMatrixConverter(&matrices[1].object, head + hidl + tail, &error))
4243         << error;
4244 
4245     auto combined = combine(&matrices, &error);
4246     ASSERT_NE(nullptr, combined) << error;
4247 
4248     auto combinedXml = gCompatibilityMatrixConverter(*combined);
4249     EXPECT_IN(aidl, combinedXml);
4250     EXPECT_IN(hidl, combinedXml);
4251 }
4252 
4253 // clang-format on
4254 
4255 } // namespace vintf
4256 } // namespace android
4257 
4258 int main(int argc, char **argv) {
4259     ::testing::InitGoogleTest(&argc, argv);
4260     return RUN_ALL_TESTS();
4261 }
4262