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