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