• 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  #include <vector>
22  
23  #include <android-base/logging.h>
24  #include <android-base/parseint.h>
25  #include <android-base/stringprintf.h>
26  #include <android-base/strings.h>
27  #include <gmock/gmock.h>
28  #include <gtest/gtest.h>
29  
30  #include <vintf/CompatibilityMatrix.h>
31  #include <vintf/KernelConfigParser.h>
32  #include <vintf/VintfObject.h>
33  #include <vintf/parse_string.h>
34  #include <vintf/parse_xml.h>
35  #include "constants-private.h"
36  #include "parse_xml_for_test.h"
37  #include "parse_xml_internal.h"
38  #include "test_constants.h"
39  
40  using android::base::StringPrintf;
41  using ::testing::Combine;
42  using ::testing::ElementsAre;
43  using ::testing::Eq;
44  using ::testing::HasSubstr;
45  using ::testing::IsEmpty;
46  using ::testing::Optional;
47  using ::testing::Property;
48  using ::testing::Range;
49  using ::testing::SizeIs;
50  using ::testing::TestParamInfo;
51  
52  using std::string_literals::operator""s;
53  
54  namespace android {
55  namespace vintf {
56  
57  #define EXPECT_IN(sub, str) EXPECT_THAT(str, HasSubstr(sub))
58  
59  struct LibVintfTest : public ::testing::Test {
60  public:
SetUpandroid::vintf::LibVintfTest61      virtual void SetUp() override {
62      }
TearDownandroid::vintf::LibVintfTest63      virtual void TearDown() override {
64      }
addandroid::vintf::LibVintfTest65      bool add(CompatibilityMatrix &cm, MatrixHal &&hal) {
66          return cm.add(std::move(hal));
67      }
addandroid::vintf::LibVintfTest68      bool add(CompatibilityMatrix &cm, MatrixKernel &&kernel) {
69          std::string error;
70          bool success = cm.addKernel(std::move(kernel), &error);
71          EXPECT_EQ(success, error == "") << "success: " << success << ", error: " << error;
72          return success;
73      }
addandroid::vintf::LibVintfTest74      bool add(HalManifest& vm, ManifestHal&& hal) { return vm.add(std::move(hal), nullptr); }
addXmlFileandroid::vintf::LibVintfTest75      void addXmlFile(CompatibilityMatrix& cm, std::string name, VersionRange range) {
76          MatrixXmlFile f;
77          f.mName = name;
78          f.mVersionRange = range;
79          f.mFormat = XmlSchemaFormat::DTD;
80          f.mOptional = true;
81          cm.addXmlFile(std::move(f));
82      }
setandroid::vintf::LibVintfTest83      void set(CompatibilityMatrix &cm, Sepolicy &&sepolicy) {
84          cm.framework.mSepolicy = sepolicy;
85      }
setandroid::vintf::LibVintfTest86      void set(CompatibilityMatrix &cm, SchemaType type) {
87          cm.mType = type;
88      }
setandroid::vintf::LibVintfTest89      void set(CompatibilityMatrix &cm, VndkVersionRange &&range, std::set<std::string> &&libs) {
90          cm.device.mVndk.mVersionRange = range;
91          cm.device.mVndk.mLibraries = libs;
92      }
setAvbandroid::vintf::LibVintfTest93      void setAvb(RuntimeInfo &ki, Version vbmeta, Version boot) {
94          ki.mBootVbmetaAvbVersion = vbmeta;
95          ki.mBootAvbVersion = boot;
96      }
setAvbandroid::vintf::LibVintfTest97      void setAvb(CompatibilityMatrix &cm, Version &&avbVersion) {
98          cm.framework.mAvbMetaVersion = avbVersion;
99      }
getAvbandroid::vintf::LibVintfTest100      Version getAvb(CompatibilityMatrix &cm) {
101          return cm.framework.mAvbMetaVersion;
102      }
getAnyHalandroid::vintf::LibVintfTest103      const ManifestHal *getAnyHal(HalManifest &vm, const std::string &name) {
104          return vm.getAnyHal(name);
105      }
getAnyHalandroid::vintf::LibVintfTest106      MatrixHal *getAnyHal(CompatibilityMatrix &cm, const std::string &name) {
107          return cm.getAnyHal(name);
108      }
getHalsandroid::vintf::LibVintfTest109      ConstMultiMapValueIterable<std::string, ManifestHal> getHals(const HalManifest& vm) {
110          return vm.getHals();
111      }
getHalsandroid::vintf::LibVintfTest112      std::vector<const ManifestHal*> getHals(const HalManifest& vm, const std::string& name) {
113          return vm.getHals(name);
114      }
getHalsandroid::vintf::LibVintfTest115      std::vector<const MatrixHal*> getHals(const CompatibilityMatrix& cm, const std::string& name) {
116          return cm.getHals(name);
117      }
isValidandroid::vintf::LibVintfTest118      bool isValid(const ManifestHal &mh) {
119          return mh.isValid();
120      }
getKernelsandroid::vintf::LibVintfTest121      std::vector<MatrixKernel>& getKernels(CompatibilityMatrix& cm) { return cm.framework.mKernels; }
addAllHalsAsOptionalandroid::vintf::LibVintfTest122      bool addAllHalsAsOptional(CompatibilityMatrix* cm1, CompatibilityMatrix* cm2, std::string* e) {
123          return cm1->addAllHalsAsOptional(cm2, e);
124      }
addAllXmlFilesAsOptionalandroid::vintf::LibVintfTest125      bool addAllXmlFilesAsOptional(CompatibilityMatrix* cm1, CompatibilityMatrix* cm2,
126                                    std::string* e) {
127          return cm1->addAllXmlFilesAsOptional(cm2, e);
128      }
checkUnusedHalsandroid::vintf::LibVintfTest129      std::set<std::string> checkUnusedHals(const HalManifest& m, const CompatibilityMatrix& cm) {
130          return m.checkUnusedHals(cm, {});
131      }
getLevelandroid::vintf::LibVintfTest132      Level getLevel(const KernelInfo& ki) { return ki.level(); }
parseGkiKernelReleaseandroid::vintf::LibVintfTest133      static status_t parseGkiKernelRelease(RuntimeInfo::FetchFlags flags,
134                                            const std::string& kernelRelease, KernelVersion* version,
135                                            Level* kernelLevel) {
136          return RuntimeInfo::parseGkiKernelRelease(flags, kernelRelease, version, kernelLevel);
137      }
138  
testHalInterfacesandroid::vintf::LibVintfTest139      std::map<std::string, HalInterface> testHalInterfaces() {
140          HalInterface intf("IFoo", {"default"});
141          std::map<std::string, HalInterface> map;
142          map[intf.name()] = intf;
143          return map;
144      }
145  
createManifestHalandroid::vintf::LibVintfTest146      ManifestHal createManifestHal(HalFormat format, std::string name, TransportArch ta,
147                                    const std::set<FqInstance>& fqInstances) {
148          ManifestHal ret;
149          ret.format = format;
150          ret.name = std::move(name);
151          ret.transportArch = ta;
152          std::string error;
153          EXPECT_TRUE(ret.insertInstances(fqInstances, false, &error)) << error;
154          return ret;
155      }
156  
testDeviceManifestandroid::vintf::LibVintfTest157      HalManifest testDeviceManifest() {
158          HalManifest vm;
159          vm.mType = SchemaType::DEVICE;
160          vm.device.mSepolicyVersion = {25, 0};
161          vm.add(createManifestHal(HalFormat::HIDL, "android.hardware.camera",
162                                   {Transport::HWBINDER, Arch::ARCH_EMPTY},
163                                   {
164                                       *FqInstance::from(2, 0, "ICamera", "legacy/0"),
165                                       *FqInstance::from(2, 0, "ICamera", "default"),
166                                       *FqInstance::from(2, 0, "IBetterCamera", "camera"),
167                                   }));
168          vm.add(createManifestHal(HalFormat::HIDL, "android.hardware.nfc",
169                                   {Transport::PASSTHROUGH, Arch::ARCH_32_64},
170                                   std::set({*FqInstance::from(1, 0, "INfc", "default")})));
171  
172          return vm;
173      }
testDeviceManifestWithXmlFileandroid::vintf::LibVintfTest174      HalManifest testDeviceManifestWithXmlFile() {
175          HalManifest vm = testDeviceManifest();
176          ManifestXmlFile xmlFile;
177          xmlFile.mName = "media_profile";
178          xmlFile.mVersion = {1, 0};
179          vm.addXmlFile(std::move(xmlFile));
180          return vm;
181      }
testFrameworkManfiestandroid::vintf::LibVintfTest182      HalManifest testFrameworkManfiest() {
183          HalManifest vm;
184          vm.mType = SchemaType::FRAMEWORK;
185          vm.add(createManifestHal(
186              HalFormat::HIDL, "android.hidl.manager", {Transport::HWBINDER, Arch::ARCH_EMPTY},
187              std::set({*FqInstance::from(1, 0, "IServiceManager", "default")})));
188          Vndk vndk2505;
189          vndk2505.mVersionRange = {25, 0, 5};
190          vndk2505.mLibraries = {"libjpeg.so", "libbase.so"};
191          Vndk vndk2513;
192          vndk2513.mVersionRange = {25, 1, 3};
193          vndk2513.mLibraries = {"libjpeg.so", "libbase.so", "libtinyxml2.so"};
194          vm.framework.mVndks = { std::move(vndk2505), std::move(vndk2513) };
195  
196          return vm;
197      }
testRuntimeInfoandroid::vintf::LibVintfTest198      RuntimeInfo testRuntimeInfo() {
199          RuntimeInfo info;
200          info.mOsName = "Linux";
201          info.mNodeName = "localhost";
202          info.mOsRelease = "3.18.31-g936f9a479d0f";
203          info.mOsVersion = "#4 SMP PREEMPT Wed Feb 1 18:10:52 PST 2017";
204          info.mHardwareId = "aarch64";
205          info.mKernelSepolicyVersion = 30;
206          info.mKernel = testKernelInfo();
207          setAvb(info, {2, 1}, {2, 1});
208          return info;
209      }
testKernelInfoandroid::vintf::LibVintfTest210      KernelInfo testKernelInfo() {
211          KernelInfo info;
212          info.mVersion = {3, 18, 31};
213          info.mConfigs = {{"CONFIG_64BIT", "y"},
214                           {"CONFIG_ANDROID_BINDER_DEVICES", "\"binder,hwbinder\""},
215                           {"CONFIG_ARCH_MMAP_RND_BITS", "24"},
216                           {"CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES", "\"\""},
217                           {"CONFIG_ILLEGAL_POINTER_VALUE", "0xdead000000000000"}};
218          return info;
219      }
fetchManifestandroid::vintf::LibVintfTest220      status_t fetchManifest(HalManifest& manifest, FileSystem* files, const std::string& path,
221                             std::string* error) {
222          return manifest.fetchAllInformation(files, path, error);
223      }
224  };
225  
226  // clang-format off
227  
TEST_F(LibVintfTest,ArchOperatorOr)228  TEST_F(LibVintfTest, ArchOperatorOr) {
229      Arch a = Arch::ARCH_EMPTY;
230      a |= Arch::ARCH_32;
231      EXPECT_EQ(Arch::ARCH_32, a);
232  
233      a |= Arch::ARCH_64;
234      EXPECT_EQ(Arch::ARCH_32_64, a);
235  
236      a = Arch::ARCH_EMPTY;
237      a |= Arch::ARCH_64;
238      EXPECT_EQ(Arch::ARCH_64, a);
239  }
240  
TEST_F(LibVintfTest,Stringify)241  TEST_F(LibVintfTest, Stringify) {
242      HalManifest vm = testDeviceManifest();
243      EXPECT_EQ(dump(vm), "hidl/android.hardware.camera/hwbinder/:"
244                          "hidl/android.hardware.nfc/passthrough32+64/");
245  
246      EXPECT_EQ(to_string(HalFormat::HIDL), "hidl");
247      EXPECT_EQ(to_string(HalFormat::NATIVE), "native");
248  
249      VersionRange v(1, 2, 3);
250      EXPECT_EQ(to_string(v), "1.2-3");
251      VersionRange v2;
252      EXPECT_TRUE(parse("1.2-3", &v2));
253      EXPECT_EQ(v, v2);
254  }
255  
TEST_F(LibVintfTest,GetTransport)256  TEST_F(LibVintfTest, GetTransport) {
257      HalManifest vm = testDeviceManifest();
258      EXPECT_EQ(Transport::HWBINDER, vm.getHidlTransport("android.hardware.camera",
259              {2, 0}, "ICamera", "default"));
260  }
261  
TEST_F(LibVintfTest,FutureManifestCompatible)262  TEST_F(LibVintfTest, FutureManifestCompatible) {
263      HalManifest expectedManifest;
264      expectedManifest.add(createManifestHal(HalFormat::HIDL,
265                                       "android.hardware.foo",
266                                       {Transport::HWBINDER, Arch::ARCH_EMPTY},
267                                       {*FqInstance::from(1, 0, "IFoo", "default")}));
268      std::string manifestXml =
269          "<manifest " + kMetaVersionStr + " type=\"device\" might_add=\"true\">\n"
270          "    <hal format=\"hidl\" attribuet_might_be_added=\"value\">\n"
271          "        <name>android.hardware.foo</name>\n"
272          "        <transport>hwbinder</transport>\n"
273          "        <version>1.0</version>\n"
274          "        <interface>\n"
275          "            <name>IFoo</name>\n"
276          "            <instance>default</instance>\n"
277          "        </interface>\n"
278          "    </hal>\n"
279          "    <tag_might_be_added/>\n"
280          "</manifest>\n";
281      HalManifest manifest;
282      EXPECT_TRUE(fromXml(&manifest, manifestXml));
283      EXPECT_EQ(expectedManifest, manifest);
284  }
285  
TEST_F(LibVintfTest,HalManifestConverter)286  TEST_F(LibVintfTest, HalManifestConverter) {
287      HalManifest vm = testDeviceManifest();
288      std::string xml =
289          toXml(vm, SerializeFlags::HALS_ONLY.enableSepolicy());
290      EXPECT_EQ(xml,
291          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
292          "    <hal format=\"hidl\">\n"
293          "        <name>android.hardware.camera</name>\n"
294          "        <transport>hwbinder</transport>\n"
295          "        <fqname>@2.0::IBetterCamera/camera</fqname>\n"
296          "        <fqname>@2.0::ICamera/default</fqname>\n"
297          "        <fqname>@2.0::ICamera/legacy/0</fqname>\n"
298          "    </hal>\n"
299          "    <hal format=\"hidl\">\n"
300          "        <name>android.hardware.nfc</name>\n"
301          "        <transport arch=\"32+64\">passthrough</transport>\n"
302          "        <fqname>@1.0::INfc/default</fqname>\n"
303          "    </hal>\n"
304          "    <sepolicy>\n"
305          "        <version>25.0</version>\n"
306          "    </sepolicy>\n"
307          "</manifest>\n");
308      HalManifest vm2;
309      EXPECT_TRUE(fromXml(&vm2, xml));
310      EXPECT_EQ(vm, vm2);
311  }
312  
TEST_F(LibVintfTest,HalManifestConverterWithInterface)313  TEST_F(LibVintfTest, HalManifestConverterWithInterface) {
314      HalManifest vm = testDeviceManifest();
315      std::string xml =
316          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
317          "    <hal format=\"hidl\">\n"
318          "        <name>android.hardware.camera</name>\n"
319          "        <transport>hwbinder</transport>\n"
320          "        <version>2.0</version>\n"
321          "        <interface>\n"
322          "            <name>IBetterCamera</name>\n"
323          "            <instance>camera</instance>\n"
324          "        </interface>\n"
325          "        <interface>\n"
326          "            <name>ICamera</name>\n"
327          "            <instance>default</instance>\n"
328          "            <instance>legacy/0</instance>\n"
329          "        </interface>\n"
330          "    </hal>\n"
331          "    <hal format=\"hidl\">\n"
332          "        <name>android.hardware.nfc</name>\n"
333          "        <transport arch=\"32+64\">passthrough</transport>\n"
334          "        <version>1.0</version>\n"
335          "        <interface>\n"
336          "            <name>INfc</name>\n"
337          "            <instance>default</instance>\n"
338          "        </interface>\n"
339          "    </hal>\n"
340          "    <sepolicy>\n"
341          "        <version>25.0</version>\n"
342          "    </sepolicy>\n"
343          "</manifest>\n";
344      HalManifest vm2;
345      EXPECT_TRUE(fromXml(&vm2, xml));
346      EXPECT_EQ(vm, vm2);
347  }
348  
TEST_F(LibVintfTest,HalManifestConverterFramework)349  TEST_F(LibVintfTest, HalManifestConverterFramework) {
350      HalManifest vm = testFrameworkManfiest();
351      std::string xml = toXml(vm, SerializeFlags::HALS_ONLY.enableVndk());
352      EXPECT_EQ(xml,
353          "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
354          "    <hal format=\"hidl\">\n"
355          "        <name>android.hidl.manager</name>\n"
356          "        <transport>hwbinder</transport>\n"
357          "        <fqname>@1.0::IServiceManager/default</fqname>\n"
358          "    </hal>\n"
359          "    <vndk>\n"
360          "        <version>25.0.5</version>\n"
361          "        <library>libbase.so</library>\n"
362          "        <library>libjpeg.so</library>\n"
363          "    </vndk>\n"
364          "    <vndk>\n"
365          "        <version>25.1.3</version>\n"
366          "        <library>libbase.so</library>\n"
367          "        <library>libjpeg.so</library>\n"
368          "        <library>libtinyxml2.so</library>\n"
369          "    </vndk>\n"
370          "</manifest>\n");
371      HalManifest vm2;
372      EXPECT_TRUE(fromXml(&vm2, xml));
373      EXPECT_EQ(vm, vm2);
374  }
375  
TEST_F(LibVintfTest,HalManifestConverterFrameworkWithInterface)376  TEST_F(LibVintfTest, HalManifestConverterFrameworkWithInterface) {
377      HalManifest vm = testFrameworkManfiest();
378      std::string xml =
379          "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
380          "    <hal format=\"hidl\">\n"
381          "        <name>android.hidl.manager</name>\n"
382          "        <transport>hwbinder</transport>\n"
383          "        <version>1.0</version>\n"
384          "        <interface>\n"
385          "            <name>IServiceManager</name>\n"
386          "            <instance>default</instance>\n"
387          "        </interface>\n"
388          "    </hal>\n"
389          "    <vndk>\n"
390          "        <version>25.0.5</version>\n"
391          "        <library>libbase.so</library>\n"
392          "        <library>libjpeg.so</library>\n"
393          "    </vndk>\n"
394          "    <vndk>\n"
395          "        <version>25.1.3</version>\n"
396          "        <library>libbase.so</library>\n"
397          "        <library>libjpeg.so</library>\n"
398          "        <library>libtinyxml2.so</library>\n"
399          "    </vndk>\n"
400          "</manifest>\n";
401      HalManifest vm2;
402      EXPECT_TRUE(fromXml(&vm2, xml));
403      EXPECT_EQ(vm, vm2);
404  }
405  
TEST_F(LibVintfTest,HalManifestOptional)406  TEST_F(LibVintfTest, HalManifestOptional) {
407      HalManifest vm;
408      EXPECT_TRUE(fromXml(&vm,
409              "<manifest " + kMetaVersionStr + " type=\"device\"></manifest>"));
410      EXPECT_TRUE(fromXml(&vm,
411              "<manifest version=\"5.0\" type=\"device\">"
412              "    <hal>"
413              "        <name>android.hidl.manager</name>"
414              "        <transport>hwbinder</transport>"
415              "        <version>1.0</version>"
416              "    </hal>"
417              "</manifest>"));
418      EXPECT_FALSE(fromXml(&vm,
419              "<manifest version=\"5.0\" type=\"device\">"
420              "    <hal>"
421              "        <name>android.hidl.manager</name>"
422              "        <version>1.0</version>"
423              "    </hal>"
424              "</manifest>"));
425  }
426  
TEST_F(LibVintfTest,HalManifestNativeNoInstance)427  TEST_F(LibVintfTest, HalManifestNativeNoInstance) {
428      std::string error;
429      HalManifest vm;
430      EXPECT_TRUE(fromXml(&vm,
431                                        "<manifest " + kMetaVersionStr + " type=\"device\">"
432                                        "    <hal format=\"native\">"
433                                        "        <name>foo</name>"
434                                        "        <version>1.0</version>"
435                                        "    </hal>"
436                                        "</manifest>", &error)) << error;
437  }
438  
TEST_F(LibVintfTest,HalManifestNativeWithTransport)439  TEST_F(LibVintfTest, HalManifestNativeWithTransport) {
440      std::string error;
441      HalManifest vm;
442      EXPECT_FALSE(fromXml(&vm,
443                                         "<manifest " + kMetaVersionStr + " type=\"device\">"
444                                         "    <hal format=\"native\">"
445                                         "        <name>foo</name>"
446                                         "        <version>1.0</version>"
447                                         "        <transport>hwbinder</transport>"
448                                         "    </hal>"
449                                         "</manifest>", &error));
450      EXPECT_THAT(error, HasSubstr("Native HAL 'foo' should not have <transport> defined"));
451  }
452  
453  // clang-format on
454  
TEST_F(LibVintfTest,HalManifestNativeInstancesWithInterface)455  TEST_F(LibVintfTest, HalManifestNativeInstancesWithInterface) {
456      std::string error;
457      HalManifest manifest;
458      std::string xml = "<manifest " + kMetaVersionStr + R"( type="device">
459              <hal format="native">
460                  <name>foo</name>
461                  <version>1.0</version>
462                  <interface>
463                      <name>IFoo</name>
464                      <instance>inst</instance>
465                  </interface>
466             </hal>
467          </manifest>
468      )";
469  
470      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
471  
472      manifest.forEachInstance([](const auto& manifestInstance) {
473          EXPECT_EQ(manifestInstance.package(), "foo");
474          EXPECT_EQ(manifestInstance.version(), Version(1, 0));
475          EXPECT_EQ(manifestInstance.interface(), "IFoo");
476          EXPECT_EQ(manifestInstance.instance(), "inst");
477          return true;  // continue
478      });
479  }
480  
TEST_F(LibVintfTest,HalManifestNativeFqInstancesWithInterface)481  TEST_F(LibVintfTest, HalManifestNativeFqInstancesWithInterface) {
482      std::string error;
483      HalManifest manifest;
484      std::string xml = "<manifest " + kMetaVersionStr + R"( type="device">
485              <hal format="native">
486                  <name>foo</name>
487                  <fqname>@1.0::IFoo/inst</fqname>
488             </hal>
489          </manifest>
490      )";
491  
492      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
493  
494      manifest.forEachInstance([](const auto& manifestInstance) {
495          EXPECT_EQ(manifestInstance.package(), "foo");
496          EXPECT_EQ(manifestInstance.version(), Version(1, 0));
497          EXPECT_EQ(manifestInstance.interface(), "IFoo");
498          EXPECT_EQ(manifestInstance.instance(), "inst");
499          return true;  // continue
500      });
501  }
502  
TEST_F(LibVintfTest,HalManifestNativeInstancesNoInterface)503  TEST_F(LibVintfTest, HalManifestNativeInstancesNoInterface) {
504      std::string error;
505      HalManifest manifest;
506      std::string xml = "<manifest " + kMetaVersionStr + R"( type="device">
507              <hal format="native">
508                  <name>foo</name>
509                  <version>1.0</version>
510                  <interface>
511                      <instance>inst</instance>
512                  </interface>
513             </hal>
514          </manifest>
515      )";
516  
517      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
518  
519      manifest.forEachInstance([](const auto& manifestInstance) {
520          EXPECT_EQ(manifestInstance.package(), "foo");
521          EXPECT_EQ(manifestInstance.version(), Version(1, 0));
522          EXPECT_EQ(manifestInstance.interface(), "");
523          EXPECT_EQ(manifestInstance.instance(), "inst");
524          return true;  // continue
525      });
526  }
527  
TEST_F(LibVintfTest,HalManifestNativeFqInstancesNoInterface)528  TEST_F(LibVintfTest, HalManifestNativeFqInstancesNoInterface) {
529      std::string error;
530      HalManifest manifest;
531      std::string xml = "<manifest " + kMetaVersionStr + R"( type="device">
532              <hal format="native">
533                  <name>foo</name>
534                  <fqname>@1.0/inst</fqname>
535             </hal>
536          </manifest>
537      )";
538  
539      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
540  
541      manifest.forEachInstance([](const auto& manifestInstance) {
542          EXPECT_EQ(manifestInstance.package(), "foo");
543          EXPECT_EQ(manifestInstance.version(), Version(1, 0));
544          EXPECT_EQ(manifestInstance.interface(), "");
545          EXPECT_EQ(manifestInstance.instance(), "inst");
546          return true;  // continue
547      });
548  }
549  
550  // clang-format off
551  
TEST_F(LibVintfTest,HalManifestDuplicate)552  TEST_F(LibVintfTest, HalManifestDuplicate) {
553      HalManifest vm;
554      EXPECT_FALSE(fromXml(&vm,
555                                         "<manifest " + kMetaVersionStr + " type=\"device\">"
556                                         "    <hal>"
557                                         "        <name>android.hidl.manager</name>"
558                                         "        <transport>hwbinder</transport>"
559                                         "        <version>1.0</version>"
560                                         "        <version>1.1</version>"
561                                         "    </hal>"
562                                         "</manifest>"))
563          << "Should not allow duplicated major version in <hal>";
564      EXPECT_FALSE(fromXml(&vm,
565                                         "<manifest " + kMetaVersionStr + " type=\"device\">"
566                                         "    <hal>"
567                                         "        <name>android.hidl.manager</name>"
568                                         "        <transport>hwbinder</transport>"
569                                         "        <version>1.0</version>"
570                                         "    </hal>"
571                                         "    <hal>"
572                                         "        <name>android.hidl.manager</name>"
573                                         "        <transport arch=\"32+64\">passthrough</transport>"
574                                         "        <version>1.1</version>"
575                                         "    </hal>"
576                                         "</manifest>"))
577          << "Should not allow duplicated major version across <hal>";
578  }
579  
TEST_F(LibVintfTest,HalManifestGetTransport)580  TEST_F(LibVintfTest, HalManifestGetTransport) {
581      HalManifest vm;
582      EXPECT_TRUE(fromXml(&vm,
583                                        "<manifest " + kMetaVersionStr + " type=\"device\">"
584                                        "    <hal>"
585                                        "        <name>android.hidl.manager</name>"
586                                        "        <transport>hwbinder</transport>"
587                                        "        <version>1.0</version>"
588                                        "        <interface>"
589                                        "            <name>IServiceManager</name>"
590                                        "            <instance>default</instance>"
591                                        "        </interface>"
592                                        "    </hal>"
593                                        "    <hal>"
594                                        "        <name>android.hidl.manager</name>"
595                                        "        <transport arch=\"32+64\">passthrough</transport>"
596                                        "        <version>2.1</version>"
597                                        "        <interface>"
598                                        "            <name>IServiceManager</name>"
599                                        "            <instance>default</instance>"
600                                        "        </interface>"
601                                        "    </hal>"
602                                        "</manifest>"));
603      EXPECT_EQ(Transport::PASSTHROUGH,
604                vm.getHidlTransport("android.hidl.manager", {2, 1}, "IServiceManager", "default"));
605      EXPECT_EQ(Transport::PASSTHROUGH,
606                vm.getHidlTransport("android.hidl.manager", {2, 0}, "IServiceManager", "default"));
607      EXPECT_EQ(Transport::EMPTY,
608                vm.getHidlTransport("android.hidl.manager", {2, 2}, "IServiceManager", "default"));
609      EXPECT_EQ(Transport::HWBINDER,
610                vm.getHidlTransport("android.hidl.manager", {1, 0}, "IServiceManager", "default"));
611  }
612  
TEST_F(LibVintfTest,HalManifestInstances)613  TEST_F(LibVintfTest, HalManifestInstances) {
614      HalManifest vm = testDeviceManifest();
615      EXPECT_EQ(vm.getHidlInstances("android.hardware.camera", {2, 0}, "ICamera"),
616                std::set<std::string>({"default", "legacy/0"}));
617      EXPECT_EQ(vm.getHidlInstances("android.hardware.camera", {2, 0}, "IBetterCamera"),
618                std::set<std::string>({"camera"}));
619      EXPECT_EQ(vm.getHidlInstances("android.hardware.camera", {2, 0}, "INotExist"),
620                std::set<std::string>({}));
621      EXPECT_EQ(vm.getHidlInstances("android.hardware.nfc", {1, 0}, "INfc"),
622                std::set<std::string>({"default"}));
623  
624      EXPECT_TRUE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "ICamera", "default"));
625      EXPECT_TRUE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "ICamera", "legacy/0"));
626      EXPECT_TRUE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "IBetterCamera", "camera"));
627      EXPECT_TRUE(vm.hasHidlInstance("android.hardware.nfc", {1, 0}, "INfc", "default"));
628  
629      EXPECT_FALSE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "INotExist", "default"));
630      EXPECT_FALSE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "ICamera", "notexist"));
631      EXPECT_FALSE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "IBetterCamera", "default"));
632      EXPECT_FALSE(vm.hasHidlInstance("android.hardware.camera", {2, 0}, "INotExist", "notexist"));
633      EXPECT_FALSE(vm.hasHidlInstance("android.hardware.nfc", {1, 0}, "INfc", "notexist"));
634  }
635  
TEST_F(LibVintfTest,VersionConverter)636  TEST_F(LibVintfTest, VersionConverter) {
637      Version v(3, 6);
638      std::string xml = toXml(v);
639      EXPECT_EQ(xml, "<version>3.6</version>\n");
640      Version v2;
641      EXPECT_TRUE(fromXml(&v2, xml));
642      EXPECT_EQ(v, v2);
643  }
644  
insert(std::map<std::string,HalInterface> * map,HalInterface && intf)645  static bool insert(std::map<std::string, HalInterface>* map, HalInterface&& intf) {
646      std::string name{intf.name()};
647      return map->emplace(std::move(name), std::move(intf)).second;
648  }
649  
TEST_F(LibVintfTest,MatrixHalConverter)650  TEST_F(LibVintfTest, MatrixHalConverter) {
651      MatrixHal mh{HalFormat::NATIVE, "android.hardware.camera",
652              {{VersionRange(1,2,3), VersionRange(4,5,6)}},
653              false /* optional */, false /* updatableViaApex */, {}};
654      EXPECT_TRUE(insert(&mh.interfaces, {"IBetterCamera", {"default", "great"}}));
655      EXPECT_TRUE(insert(&mh.interfaces, {"ICamera", {"default"}}));
656      std::string xml = toXml(mh);
657      EXPECT_EQ(xml,
658          "<hal format=\"native\" optional=\"false\">\n"
659          "    <name>android.hardware.camera</name>\n"
660          "    <version>1.2-3</version>\n"
661          "    <version>4.5-6</version>\n"
662          "    <interface>\n"
663          "        <name>IBetterCamera</name>\n"
664          "        <instance>default</instance>\n"
665          "        <instance>great</instance>\n"
666          "    </interface>\n"
667          "    <interface>\n"
668          "        <name>ICamera</name>\n"
669          "        <instance>default</instance>\n"
670          "    </interface>\n"
671          "</hal>\n");
672      MatrixHal mh2;
673      EXPECT_TRUE(fromXml(&mh2, xml));
674      EXPECT_EQ(mh, mh2);
675  }
676  
TEST_F(LibVintfTest,KernelConfigTypedValueConverter)677  TEST_F(LibVintfTest, KernelConfigTypedValueConverter) {
678  
679      KernelConfigTypedValue converted;
680  
681      auto testOne = [] (const KernelConfigTypedValue &original,
682                      const std::string &expectXml) {
683          std::string xml;
684          KernelConfigTypedValue converted;
685          xml = toXml(original);
686          EXPECT_EQ(xml, expectXml);
687          EXPECT_TRUE(fromXml(&converted, xml));
688          EXPECT_EQ(original, converted);
689      };
690  
691      auto testParse = [] (const KernelConfigTypedValue &original,
692                      const std::string &xml) {
693          KernelConfigTypedValue converted;
694          EXPECT_TRUE(fromXml(&converted, xml));
695          EXPECT_EQ(original, converted);
696      };
697  
698      testOne(KernelConfigTypedValue("stringvalue"),
699          "<value type=\"string\">stringvalue</value>\n");
700      testOne(KernelConfigTypedValue(""),
701          "<value type=\"string\"></value>\n");
702  
703      testOne(KernelConfigTypedValue(Tristate::YES),
704          "<value type=\"tristate\">y</value>\n");
705      testOne(KernelConfigTypedValue(Tristate::NO),
706          "<value type=\"tristate\">n</value>\n");
707      testOne(KernelConfigTypedValue(Tristate::MODULE),
708          "<value type=\"tristate\">m</value>\n");
709      EXPECT_FALSE(fromXml(&converted,
710          "<value type=\"tristate\">q</value>\n"));
711  
712      testOne(KernelConfigTypedValue(KernelConfigRangeValue{4, 20}),
713          "<value type=\"range\">4-20</value>\n");
714      testOne(KernelConfigTypedValue(KernelConfigRangeValue{0, UINT64_MAX}),
715          "<value type=\"range\">0-18446744073709551615</value>\n");
716      testParse(KernelConfigTypedValue(KernelConfigRangeValue{0, UINT64_MAX}),
717              "<value type=\"range\">0x0-0xffffffffffffffff</value>\n");
718  
719      EXPECT_FALSE(fromXml(&converted,
720              "<value type=\"int\">-18446744073709551616</value>\n"));
721  
722      testOne(KernelConfigTypedValue(INT64_MIN),
723           "<value type=\"int\">-9223372036854775808</value>\n");
724      testParse(KernelConfigTypedValue(INT64_MIN),
725              "<value type=\"int\">0x8000000000000000</value>\n");
726      testParse(KernelConfigTypedValue(INT64_MIN),
727              "<value type=\"int\">-0X8000000000000000</value>\n");
728  
729      testParse(KernelConfigTypedValue(INT64_MIN + 1),
730              "<value type=\"int\">-0X7FFFFFFFFFFFFFFF</value>\n");
731  
732      testParse(KernelConfigTypedValue(-0x50),
733              "<value type=\"int\">-0x50</value>\n");
734  
735      testOne(KernelConfigTypedValue(0),
736           "<value type=\"int\">0</value>\n");
737  
738      // Truncation for underflow.
739      testParse(KernelConfigTypedValue(1),
740              "<value type=\"int\">-0xffffffffffffffff</value>\n");
741      testParse(KernelConfigTypedValue(1),
742              "<value type=\"int\">-18446744073709551615</value>\n");
743  
744      testOne(KernelConfigTypedValue(INT64_MAX),
745           "<value type=\"int\">9223372036854775807</value>\n");
746      testParse(KernelConfigTypedValue(INT64_MAX),
747              "<value type=\"int\">0x7FFFFFFFFFFFFFFF</value>\n");
748      // Truncation for underflow.
749      testParse(KernelConfigTypedValue(INT64_MAX),
750              "<value type=\"int\">-9223372036854775809</value>\n");
751  
752      testParse(KernelConfigTypedValue(-1),
753              "<value type=\"int\">18446744073709551615</value>\n");
754      testParse(KernelConfigTypedValue(-1),
755              "<value type=\"int\">0xffffffffffffffff</value>\n");
756  
757      EXPECT_FALSE(fromXml(&converted,
758              "<value type=\"int\">18446744073709551616</value>\n"));
759  }
760  
TEST_F(LibVintfTest,CompatibilityMatrixConverter)761  TEST_F(LibVintfTest, CompatibilityMatrixConverter) {
762      CompatibilityMatrix cm;
763      EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE, "android.hardware.camera",
764              {{VersionRange(1,2,3), VersionRange(4,5,6)}},
765              false /* optional */,  false /* updatableViaApex */, testHalInterfaces()}));
766      EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE, "android.hardware.nfc",
767              {{VersionRange(4,5,6), VersionRange(10,11,12)}},
768              true /* optional */,  false /* updatableViaApex */, testHalInterfaces()}));
769      EXPECT_TRUE(add(cm, MatrixKernel{KernelVersion(3, 18, 22),
770              {KernelConfig{"CONFIG_FOO", Tristate::YES}, KernelConfig{"CONFIG_BAR", "stringvalue"}}}));
771      EXPECT_TRUE(add(cm, MatrixKernel{KernelVersion(4, 4, 1),
772              {KernelConfig{"CONFIG_BAZ", 20}, KernelConfig{"CONFIG_BAR", KernelConfigRangeValue{3, 5} }}}));
773      set(cm, Sepolicy(30, {{25, 0}, {26, 0, 3}}));
774      setAvb(cm, Version{2, 1});
775      std::string xml = toXml(cm);
776      EXPECT_EQ(xml,
777              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
778              "    <hal format=\"native\" optional=\"false\">\n"
779              "        <name>android.hardware.camera</name>\n"
780              "        <version>1.2-3</version>\n"
781              "        <version>4.5-6</version>\n"
782              "        <interface>\n"
783              "            <name>IFoo</name>\n"
784              "            <instance>default</instance>\n"
785              "        </interface>\n"
786              "    </hal>\n"
787              "    <hal format=\"native\" optional=\"true\">\n"
788              "        <name>android.hardware.nfc</name>\n"
789              "        <version>4.5-6</version>\n"
790              "        <version>10.11-12</version>\n"
791              "        <interface>\n"
792              "            <name>IFoo</name>\n"
793              "            <instance>default</instance>\n"
794              "        </interface>\n"
795              "    </hal>\n"
796              "    <kernel version=\"3.18.22\">\n"
797              "        <config>\n"
798              "            <key>CONFIG_FOO</key>\n"
799              "            <value type=\"tristate\">y</value>\n"
800              "        </config>\n"
801              "        <config>\n"
802              "            <key>CONFIG_BAR</key>\n"
803              "            <value type=\"string\">stringvalue</value>\n"
804              "        </config>\n"
805              "    </kernel>\n"
806              "    <kernel version=\"4.4.1\">\n"
807              "        <config>\n"
808              "            <key>CONFIG_BAZ</key>\n"
809              "            <value type=\"int\">20</value>\n"
810              "        </config>\n"
811              "        <config>\n"
812              "            <key>CONFIG_BAR</key>\n"
813              "            <value type=\"range\">3-5</value>\n"
814              "        </config>\n"
815              "    </kernel>\n"
816              "    <sepolicy>\n"
817              "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
818              "        <sepolicy-version>25.0</sepolicy-version>\n"
819              "        <sepolicy-version>26.0-3</sepolicy-version>\n"
820              "    </sepolicy>\n"
821              "    <avb>\n"
822              "        <vbmeta-version>2.1</vbmeta-version>\n"
823              "    </avb>\n"
824              "</compatibility-matrix>\n");
825      CompatibilityMatrix cm2;
826      EXPECT_TRUE(fromXml(&cm2, xml));
827      EXPECT_EQ(cm, cm2);
828  }
829  
TEST_F(LibVintfTest,DeviceCompatibilityMatrixCoverter)830  TEST_F(LibVintfTest, DeviceCompatibilityMatrixCoverter) {
831      CompatibilityMatrix cm;
832      EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE, "android.hidl.manager",
833              {{VersionRange(1,0)}},
834              false /* optional */,  false /* updatableViaApex */, testHalInterfaces()}));
835      set(cm, SchemaType::DEVICE);
836      set(cm, VndkVersionRange{25,0,1,5}, {"libjpeg.so", "libbase.so"});
837      std::string xml = toXml(cm);
838      EXPECT_EQ(xml,
839          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
840          "    <hal format=\"native\" optional=\"false\">\n"
841          "        <name>android.hidl.manager</name>\n"
842          "        <version>1.0</version>\n"
843          "        <interface>\n"
844          "            <name>IFoo</name>\n"
845          "            <instance>default</instance>\n"
846          "        </interface>\n"
847          "    </hal>\n"
848          "    <vndk>\n"
849          "        <version>25.0.1-5</version>\n"
850          "        <library>libbase.so</library>\n"
851          "        <library>libjpeg.so</library>\n"
852          "    </vndk>\n"
853          "</compatibility-matrix>\n");
854      CompatibilityMatrix cm2;
855      EXPECT_TRUE(fromXml(&cm2, xml));
856      EXPECT_EQ(cm, cm2);
857  }
858  
859  // clang-format on
TEST_F(LibVintfTest,IsValid)860  TEST_F(LibVintfTest, IsValid) {
861      EXPECT_TRUE(isValid(ManifestHal()));
862  
863      auto invalidHal = createManifestHal(HalFormat::HIDL, "android.hardware.camera",
864                                          {Transport::PASSTHROUGH, Arch::ARCH_32_64}, {});
865      invalidHal.versions = {{Version(2, 0), Version(2, 1)}};
866  
867      EXPECT_FALSE(isValid(invalidHal));
868      HalManifest vm2;
869      EXPECT_FALSE(add(vm2, std::move(invalidHal)));
870  }
871  // clang-format off
872  
TEST_F(LibVintfTest,HalManifestGetHalNames)873  TEST_F(LibVintfTest, HalManifestGetHalNames) {
874      HalManifest vm = testDeviceManifest();
875      EXPECT_EQ(vm.getHalNames(), std::set<std::string>(
876                    {"android.hardware.camera", "android.hardware.nfc"}));
877  }
878  
TEST_F(LibVintfTest,HalManifestGetAllHals)879  TEST_F(LibVintfTest, HalManifestGetAllHals) {
880      HalManifest vm = testDeviceManifest();
881      EXPECT_NE(getAnyHal(vm, "android.hardware.camera"), nullptr);
882      EXPECT_EQ(getAnyHal(vm, "non-existent"), nullptr);
883  
884      std::vector<std::string> arr{"android.hardware.camera", "android.hardware.nfc"};
885      size_t i = 0;
886      for (const auto &hal : getHals(vm)) {
887          EXPECT_EQ(hal.name, arr[i++]);
888      }
889  }
890  
891  // clang-format on
TEST_F(LibVintfTest,HalManifestGetHals)892  TEST_F(LibVintfTest, HalManifestGetHals) {
893      HalManifest vm;
894  
895      EXPECT_TRUE(add(vm, createManifestHal(HalFormat::HIDL, "android.hardware.camera",
896                                            {Transport::HWBINDER, Arch::ARCH_EMPTY},
897                                            {
898                                                *FqInstance::from(1, 2, "ICamera", "legacy/0"),
899                                                *FqInstance::from(1, 2, "ICamera", "default"),
900                                                *FqInstance::from(1, 2, "IBetterCamera", "camera"),
901                                            })));
902      EXPECT_TRUE(add(vm, createManifestHal(HalFormat::HIDL, "android.hardware.camera",
903                                            {Transport::HWBINDER, Arch::ARCH_EMPTY},
904                                            {
905                                                *FqInstance::from(2, 0, "ICamera", "legacy/0"),
906                                                *FqInstance::from(2, 0, "ICamera", "default"),
907                                                *FqInstance::from(2, 0, "IBetterCamera", "camera"),
908                                            })));
909  
910      EXPECT_TRUE(add(vm, createManifestHal(HalFormat::HIDL, "android.hardware.nfc",
911                                            {Transport::PASSTHROUGH, Arch::ARCH_32_64},
912                                            {*FqInstance::from(1, 0, "INfc", "default"),
913                                             *FqInstance::from(2, 1, "INfc", "default")})));
914  
915      ManifestHal expectedCameraHalV1_2 = createManifestHal(
916          HalFormat::HIDL, "android.hardware.camera", {Transport::HWBINDER, Arch::ARCH_EMPTY},
917          {
918              *FqInstance::from(1, 2, "ICamera", "legacy/0"),
919              *FqInstance::from(1, 2, "ICamera", "default"),
920              *FqInstance::from(1, 2, "IBetterCamera", "camera"),
921          });
922      ManifestHal expectedCameraHalV2_0 = createManifestHal(
923          HalFormat::HIDL, "android.hardware.camera", {Transport::HWBINDER, Arch::ARCH_EMPTY},
924          {
925              *FqInstance::from(2, 0, "ICamera", "legacy/0"),
926              *FqInstance::from(2, 0, "ICamera", "default"),
927              *FqInstance::from(2, 0, "IBetterCamera", "camera"),
928          });
929      ManifestHal expectedNfcHal = createManifestHal(
930          HalFormat::HIDL, "android.hardware.nfc", {Transport::PASSTHROUGH, Arch::ARCH_32_64},
931          {*FqInstance::from(1, 0, "INfc", "default"), *FqInstance::from(2, 1, "INfc", "default")});
932  
933      auto cameraHals = getHals(vm, "android.hardware.camera");
934      EXPECT_EQ((int)cameraHals.size(), 2);
935      EXPECT_EQ(*cameraHals[0], expectedCameraHalV1_2);
936      EXPECT_EQ(*cameraHals[1], expectedCameraHalV2_0);
937      auto nfcHals = getHals(vm, "android.hardware.nfc");
938      EXPECT_EQ((int)nfcHals.size(), 1);
939      EXPECT_EQ(*nfcHals[0], expectedNfcHal);
940  }
941  // clang-format off
942  
TEST_F(LibVintfTest,CompatibilityMatrixGetHals)943  TEST_F(LibVintfTest, CompatibilityMatrixGetHals) {
944      CompatibilityMatrix cm;
945      EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE,
946                                    "android.hardware.camera",
947                                    {{VersionRange(1, 2, 3), VersionRange(4, 5, 6)}},
948                                    false /* optional */,
949                                    false /* updatableViaApex */,
950                                    testHalInterfaces()}));
951      EXPECT_TRUE(add(cm, MatrixHal{HalFormat::NATIVE,
952                                    "android.hardware.nfc",
953                                    {{VersionRange(4, 5, 6), VersionRange(10, 11, 12)}},
954                                    true /* optional */,
955                                    false /* updatableViaApex */,
956                                    testHalInterfaces()}));
957  
958      MatrixHal expectedCameraHal = MatrixHal{
959          HalFormat::NATIVE,
960          "android.hardware.camera",
961          {{VersionRange(1, 2, 3), VersionRange(4, 5, 6)}},
962          false /* optional */,
963          false /* updatableViaApex */,
964          testHalInterfaces(),
965      };
966      MatrixHal expectedNfcHal = MatrixHal{HalFormat::NATIVE,
967                                           "android.hardware.nfc",
968                                           {{VersionRange(4, 5, 6), VersionRange(10, 11, 12)}},
969                                           true /* optional */,
970                                           false /* updatableViaApex */,
971                                           testHalInterfaces()};
972      auto cameraHals = getHals(cm, "android.hardware.camera");
973      EXPECT_EQ((int)cameraHals.size(), 1);
974      EXPECT_EQ(*cameraHals[0], expectedCameraHal);
975      auto nfcHals = getHals(cm, "android.hardware.nfc");
976      EXPECT_EQ((int)nfcHals.size(), 1);
977      EXPECT_EQ(*nfcHals[0], expectedNfcHal);
978  }
979  
TEST_F(LibVintfTest,RuntimeInfo)980  TEST_F(LibVintfTest, RuntimeInfo) {
981      RuntimeInfo ki = testRuntimeInfo();
982      using KernelConfigs = std::vector<KernelConfig>;
983      const KernelConfigs configs {
984              KernelConfig{"CONFIG_64BIT", Tristate::YES},
985              KernelConfig{"CONFIG_ANDROID_BINDER_DEVICES", "binder,hwbinder"},
986              KernelConfig{"CONFIG_ARCH_MMAP_RND_BITS", 24},
987              KernelConfig{"CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES", ""},
988              KernelConfig{"CONFIG_ILLEGAL_POINTER_VALUE", 0xdead000000000000},
989              KernelConfig{"CONFIG_NOTEXIST", Tristate::NO},
990      };
991  
992      auto testMatrix = [&] (MatrixKernel &&kernel) {
993          CompatibilityMatrix cm;
994          add(cm, std::move(kernel));
995          set(cm, {30, {{25, 0}}});
996          setAvb(cm, {2, 1});
997          return cm;
998      };
999  
1000      std::string error;
1001  
1002      {
1003          MatrixKernel kernel(KernelVersion{4, 4, 1}, KernelConfigs(configs));
1004          CompatibilityMatrix cm = testMatrix(std::move(kernel));
1005          EXPECT_FALSE(ki.checkCompatibility(cm)) << "Kernel version shouldn't match";
1006      }
1007  
1008      {
1009          MatrixKernel kernel(KernelVersion{3, 18, 60}, KernelConfigs(configs));
1010          CompatibilityMatrix cm = testMatrix(std::move(kernel));
1011          EXPECT_FALSE(ki.checkCompatibility(cm)) << "Kernel version shouldn't match";
1012      }
1013  
1014      {
1015          MatrixKernel kernel(KernelVersion{3, 18, 22}, KernelConfigs(configs));
1016          CompatibilityMatrix cm = testMatrix(std::move(kernel));
1017          EXPECT_TRUE(ki.checkCompatibility(cm, &error)) << error;
1018      }
1019  
1020      {
1021          MatrixKernel kernel(KernelVersion{3, 18, 22}, KernelConfigs(configs));
1022          CompatibilityMatrix cm = testMatrix(std::move(kernel));
1023          set(cm, Sepolicy{22, {{25, 0}}});
1024          EXPECT_TRUE(ki.checkCompatibility(cm, &error)) << error;
1025          set(cm, Sepolicy{40, {{25, 0}}});
1026          EXPECT_FALSE(ki.checkCompatibility(cm, &error))
1027              << "kernel-sepolicy-version shouldn't match";
1028          EXPECT_IN("kernelSepolicyVersion = 30 but required >= 40", error);
1029      }
1030  
1031      {
1032          KernelConfigs newConfigs(configs);
1033          newConfigs[0] = KernelConfig{"CONFIG_64BIT", Tristate::NO};
1034          MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
1035          CompatibilityMatrix cm = testMatrix(std::move(kernel));
1036          EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for tristate";
1037      }
1038  
1039      {
1040          KernelConfigs newConfigs(configs);
1041          newConfigs[0] = KernelConfig{"CONFIG_64BIT", 20};
1042          MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
1043          CompatibilityMatrix cm = testMatrix(std::move(kernel));
1044          EXPECT_FALSE(ki.checkCompatibility(cm)) << "Type shouldn't match";
1045      }
1046  
1047      {
1048          KernelConfigs newConfigs(configs);
1049          newConfigs[1] = KernelConfig{"CONFIG_ANDROID_BINDER_DEVICES", "binder"};
1050          MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
1051          CompatibilityMatrix cm = testMatrix(std::move(kernel));
1052          EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for string";
1053      }
1054  
1055      {
1056          KernelConfigs newConfigs(configs);
1057          newConfigs[1] = KernelConfig{"CONFIG_ANDROID_BINDER_DEVICES", Tristate::YES};
1058          MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
1059          CompatibilityMatrix cm = testMatrix(std::move(kernel));
1060          EXPECT_FALSE(ki.checkCompatibility(cm)) << "Type shouldn't match";
1061      }
1062  
1063      {
1064          KernelConfigs newConfigs(configs);
1065          newConfigs[2] = KernelConfig{"CONFIG_ARCH_MMAP_RND_BITS", 30};
1066          MatrixKernel kernel(KernelVersion{3, 18, 22}, std::move(newConfigs));
1067          CompatibilityMatrix cm = testMatrix(std::move(kernel));
1068          EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for integer";
1069      }
1070  
1071      RuntimeInfo badAvb = testRuntimeInfo();
1072      CompatibilityMatrix cm = testMatrix(MatrixKernel(KernelVersion{3, 18, 31}, {}));
1073      {
1074          setAvb(badAvb, {1, 0}, {2, 1});
1075          EXPECT_FALSE(badAvb.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
1076          EXPECT_STREQ(error.c_str(), "Vbmeta version 1.0 does not match framework matrix 2.1");
1077      }
1078      {
1079          setAvb(badAvb, {2, 1}, {3, 0});
1080          EXPECT_FALSE(badAvb.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
1081      }
1082      {
1083          setAvb(badAvb, {2, 1}, {2, 3});
1084          EXPECT_TRUE(badAvb.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
1085      }
1086      {
1087          setAvb(badAvb, {2, 3}, {2, 1});
1088          EXPECT_TRUE(badAvb.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
1089      }
1090  }
1091  
TEST_F(LibVintfTest,MissingAvb)1092  TEST_F(LibVintfTest, MissingAvb) {
1093      std::string xml =
1094          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1095          "    <kernel version=\"3.18.31\"></kernel>"
1096          "    <sepolicy>\n"
1097          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1098          "        <sepolicy-version>25.5</sepolicy-version>\n"
1099          "    </sepolicy>\n"
1100          "</compatibility-matrix>\n";
1101      CompatibilityMatrix cm;
1102      EXPECT_TRUE(fromXml(&cm, xml));
1103      EXPECT_EQ(getAvb(cm), Version(0, 0));
1104  }
1105  
TEST_F(LibVintfTest,DisableAvb)1106  TEST_F(LibVintfTest, DisableAvb) {
1107      std::string xml =
1108          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1109          "    <kernel version=\"3.18.31\"></kernel>"
1110          "    <sepolicy>\n"
1111          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1112          "        <sepolicy-version>25.5</sepolicy-version>\n"
1113          "    </sepolicy>\n"
1114          "    <avb>\n"
1115          "        <vbmeta-version>1.0</vbmeta-version>\n"
1116          "    </avb>\n"
1117          "</compatibility-matrix>\n";
1118      CompatibilityMatrix cm;
1119      EXPECT_TRUE(fromXml(&cm, xml));
1120      RuntimeInfo ki = testRuntimeInfo();
1121      std::string error;
1122      EXPECT_FALSE(ki.checkCompatibility(cm, &error, CheckFlags::ENABLE_ALL_CHECKS));
1123      EXPECT_STREQ(error.c_str(), "AVB version 2.1 does not match framework matrix 1.0");
1124      EXPECT_TRUE(ki.checkCompatibility(cm, &error, CheckFlags::DISABLE_AVB_CHECK)) << error;
1125  }
1126  
1127  // This is the test extracted from VINTF Object doc
TEST_F(LibVintfTest,HalCompat)1128  TEST_F(LibVintfTest, HalCompat) {
1129      CompatibilityMatrix matrix;
1130      std::string error;
1131  
1132      std::string matrixXml =
1133              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1134              "    <hal format=\"hidl\" optional=\"false\">\n"
1135              "        <name>android.hardware.foo</name>\n"
1136              "        <version>1.0</version>\n"
1137              "        <version>3.1-2</version>\n"
1138              "        <interface>\n"
1139              "            <name>IFoo</name>\n"
1140              "            <instance>default</instance>\n"
1141              "            <instance>specific</instance>\n"
1142              "        </interface>\n"
1143              "    </hal>\n"
1144              "    <hal format=\"hidl\" optional=\"false\">\n"
1145              "        <name>android.hardware.foo</name>\n"
1146              "        <version>2.0</version>\n"
1147              "        <interface>\n"
1148              "            <name>IBar</name>\n"
1149              "            <instance>default</instance>\n"
1150              "        </interface>\n"
1151              "    </hal>\n"
1152              "    <sepolicy>\n"
1153              "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1154              "        <sepolicy-version>25.5</sepolicy-version>\n"
1155              "    </sepolicy>\n"
1156              "</compatibility-matrix>\n";
1157      EXPECT_TRUE(fromXml(&matrix, matrixXml, &error)) << error;
1158  
1159      {
1160          std::string manifestXml =
1161                  "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1162                  "    <hal format=\"hidl\">\n"
1163                  "        <name>android.hardware.foo</name>\n"
1164                  "        <transport>hwbinder</transport>\n"
1165                  "        <version>1.0</version>\n"
1166                  "        <interface>\n"
1167                  "            <name>IFoo</name>\n"
1168                  "            <instance>default</instance>\n"
1169                  "            <instance>specific</instance>\n"
1170                  "        </interface>\n"
1171                  "    </hal>\n"
1172                  "    <hal format=\"hidl\">\n"
1173                  "        <name>android.hardware.foo</name>\n"
1174                  "        <transport>hwbinder</transport>\n"
1175                  "        <version>2.0</version>\n"
1176                  "        <interface>\n"
1177                  "            <name>IBar</name>\n"
1178                  "            <instance>default</instance>\n"
1179                  "        </interface>\n"
1180                  "    </hal>\n"
1181                  "    <sepolicy>\n"
1182                  "        <version>25.5</version>\n"
1183                  "    </sepolicy>\n"
1184                  "</manifest>\n";
1185  
1186          HalManifest manifest;
1187          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
1188          EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1189      }
1190  
1191      {
1192          std::string manifestXml =
1193                  "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1194                  "    <hal format=\"hidl\">\n"
1195                  "        <name>android.hardware.foo</name>\n"
1196                  "        <transport>hwbinder</transport>\n"
1197                  "        <version>1.0</version>\n"
1198                  "        <interface>\n"
1199                  "            <name>IFoo</name>\n"
1200                  "            <instance>default</instance>\n"
1201                  "            <instance>specific</instance>\n"
1202                  "        </interface>\n"
1203                  "    </hal>\n"
1204                  "    <sepolicy>\n"
1205                  "        <version>25.5</version>\n"
1206                  "    </sepolicy>\n"
1207                  "</manifest>\n";
1208          HalManifest manifest;
1209          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
1210          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
1211                  << "should not be compatible because IBar is missing";
1212      }
1213  
1214      {
1215          std::string manifestXml =
1216                  "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1217                  "    <hal format=\"hidl\">\n"
1218                  "        <name>android.hardware.foo</name>\n"
1219                  "        <transport>hwbinder</transport>\n"
1220                  "        <version>1.0</version>\n"
1221                  "        <interface>\n"
1222                  "            <name>IFoo</name>\n"
1223                  "            <instance>default</instance>\n"
1224                  "        </interface>\n"
1225                  "    </hal>\n"
1226                  "    <hal format=\"hidl\">\n"
1227                  "        <name>android.hardware.foo</name>\n"
1228                  "        <transport>hwbinder</transport>\n"
1229                  "        <version>2.0</version>\n"
1230                  "        <interface>\n"
1231                  "            <name>IBar</name>\n"
1232                  "            <instance>default</instance>\n"
1233                  "        </interface>\n"
1234                  "    </hal>\n"
1235                  "    <sepolicy>\n"
1236                  "        <version>25.5</version>\n"
1237                  "    </sepolicy>\n"
1238                  "</manifest>\n";
1239          HalManifest manifest;
1240          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
1241          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
1242              << "should not be compatible because IFoo/specific is missing";
1243      }
1244  
1245      {
1246          std::string manifestXml =
1247                  "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1248                  "    <hal format=\"hidl\">\n"
1249                  "        <name>android.hardware.foo</name>\n"
1250                  "        <transport>hwbinder</transport>\n"
1251                  "        <version>3.3</version>\n"
1252                  "        <interface>\n"
1253                  "            <name>IFoo</name>\n"
1254                  "            <instance>default</instance>\n"
1255                  "            <instance>specific</instance>\n"
1256                  "        </interface>\n"
1257                  "    </hal>\n"
1258                  "    <hal format=\"hidl\">\n"
1259                  "        <name>android.hardware.foo</name>\n"
1260                  "        <transport>hwbinder</transport>\n"
1261                  "        <version>2.0</version>\n"
1262                  "        <interface>\n"
1263                  "            <name>IBar</name>\n"
1264                  "            <instance>default</instance>\n"
1265                  "        </interface>\n"
1266                  "    </hal>\n"
1267                  "    <sepolicy>\n"
1268                  "        <version>25.5</version>\n"
1269                  "    </sepolicy>\n"
1270                  "</manifest>\n";
1271          HalManifest manifest;
1272          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
1273          EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1274      }
1275  
1276      {
1277          std::string manifestXml =
1278                  "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1279                  "    <hal format=\"hidl\">\n"
1280                  "        <name>android.hardware.foo</name>\n"
1281                  "        <transport>hwbinder</transport>\n"
1282                  "        <version>1.0</version>\n"
1283                  "        <interface>\n"
1284                  "            <name>IFoo</name>\n"
1285                  "            <instance>default</instance>\n"
1286                  "        </interface>\n"
1287                  "    </hal>\n"
1288                  "    <hal format=\"hidl\">\n"
1289                  "        <name>android.hardware.foo</name>\n"
1290                  "        <transport>hwbinder</transport>\n"
1291                  "        <version>3.2</version>\n"
1292                  "        <interface>\n"
1293                  "            <name>IFoo</name>\n"
1294                  "            <instance>specific</instance>\n"
1295                  "        </interface>\n"
1296                  "    </hal>\n"
1297                  "    <hal format=\"hidl\">\n"
1298                  "        <name>android.hardware.foo</name>\n"
1299                  "        <transport>hwbinder</transport>\n"
1300                  "        <version>2.0</version>\n"
1301                  "        <interface>\n"
1302                  "            <name>IBar</name>\n"
1303                  "            <instance>default</instance>\n"
1304                  "        </interface>\n"
1305                  "    </hal>\n"
1306                  "    <sepolicy>\n"
1307                  "        <version>25.5</version>\n"
1308                  "    </sepolicy>\n"
1309                  "</manifest>\n";
1310          HalManifest manifest;
1311          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
1312          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
1313                  << "should not be compatible even though @1.0::IFoo/default "
1314                  << "and @3.2::IFoo/specific present";
1315      }
1316  
1317      {
1318          std::string manifestXml =
1319                  "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1320                  "    <hal format=\"hidl\">\n"
1321                  "        <name>android.hardware.foo</name>\n"
1322                  "        <transport>hwbinder</transport>\n"
1323                  "        <version>1.0</version>\n"
1324                  "        <interface>\n"
1325                  "            <name>IFoo</name>\n"
1326                  "            <instance>default</instance>\n"
1327                  "            <instance>specific</instance>\n"
1328                  "        </interface>\n"
1329                  "    </hal>\n"
1330                  "    <hal format=\"hidl\">\n"
1331                  "        <name>android.hardware.foo</name>\n"
1332                  "        <transport>hwbinder</transport>\n"
1333                  "        <version>2.0</version>\n"
1334                  "        <interface>\n"
1335                  "            <name>IBar</name>\n"
1336                  "            <instance>default</instance>\n"
1337                  "        </interface>\n"
1338                  "    </hal>\n"
1339                  "    <sepolicy>\n"
1340                  "        <version>25.5</version>\n"
1341                  "    </sepolicy>\n"
1342                  "</manifest>\n";
1343          HalManifest manifest;
1344          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
1345          EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1346      }
1347  }
1348  
TEST_F(LibVintfTest,FullCompat)1349  TEST_F(LibVintfTest, FullCompat) {
1350      std::string manifestXml =
1351          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1352          "    <hal format=\"hidl\">\n"
1353          "        <name>android.hardware.camera</name>\n"
1354          "        <transport>hwbinder</transport>\n"
1355          "        <version>3.5</version>\n"
1356          "        <interface>\n"
1357          "            <name>IBetterCamera</name>\n"
1358          "            <instance>camera</instance>\n"
1359          "        </interface>\n"
1360          "        <interface>\n"
1361          "            <name>ICamera</name>\n"
1362          "            <instance>default</instance>\n"
1363          "            <instance>legacy/0</instance>\n"
1364          "        </interface>\n"
1365          "    </hal>\n"
1366          "    <hal format=\"hidl\">\n"
1367          "        <name>android.hardware.nfc</name>\n"
1368          "        <transport>hwbinder</transport>\n"
1369          "        <version>1.0</version>\n"
1370          "        <interface>\n"
1371          "            <name>INfc</name>\n"
1372          "            <instance>nfc_nci</instance>\n"
1373          "        </interface>\n"
1374          "    </hal>\n"
1375          "    <hal format=\"hidl\">\n"
1376          "        <name>android.hardware.nfc</name>\n"
1377          "        <transport>hwbinder</transport>\n"
1378          "        <version>2.0</version>\n"
1379          "        <interface>\n"
1380          "            <name>INfc</name>\n"
1381          "            <instance>default</instance>\n"
1382          "            <instance>nfc_nci</instance>\n"
1383          "        </interface>\n"
1384          "    </hal>\n"
1385          "    <sepolicy>\n"
1386          "        <version>25.5</version>\n"
1387          "    </sepolicy>\n"
1388          "</manifest>\n";
1389  
1390      std::string matrixXml =
1391          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1392          "    <hal format=\"hidl\" optional=\"false\">\n"
1393          "        <name>android.hardware.camera</name>\n"
1394          "        <version>2.0-5</version>\n"
1395          "        <version>3.4-16</version>\n"
1396          "        <interface>\n"
1397          "            <name>IBetterCamera</name>\n"
1398          "            <instance>camera</instance>\n"
1399          "        </interface>\n"
1400          "        <interface>\n"
1401          "            <name>ICamera</name>\n"
1402          "            <instance>default</instance>\n"
1403          "            <instance>legacy/0</instance>\n"
1404          "        </interface>\n"
1405          "    </hal>\n"
1406          "    <hal format=\"hidl\" optional=\"false\">\n"
1407          "        <name>android.hardware.nfc</name>\n"
1408          "        <version>1.0</version>\n"
1409          "        <version>2.0</version>\n"
1410          "        <interface>\n"
1411          "            <name>INfc</name>\n"
1412          "            <instance>nfc_nci</instance>\n"
1413          "        </interface>\n"
1414          "    </hal>\n"
1415          "    <hal format=\"hidl\" optional=\"true\">\n"
1416          "        <name>android.hardware.foo</name>\n"
1417          "        <version>1.0</version>\n"
1418          "    </hal>\n"
1419          "    <sepolicy>\n"
1420          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1421          "        <sepolicy-version>25.5</sepolicy-version>\n"
1422          "        <sepolicy-version>26.0-3</sepolicy-version>\n"
1423          "    </sepolicy>\n"
1424          "    <avb>\n"
1425          "        <vbmeta-version>2.1</vbmeta-version>\n"
1426          "    </avb>\n"
1427          "</compatibility-matrix>\n";
1428  
1429      HalManifest manifest;
1430      CompatibilityMatrix matrix;
1431      std::string error;
1432      EXPECT_TRUE(fromXml(&manifest, manifestXml));
1433      EXPECT_TRUE(fromXml(&matrix, matrixXml));
1434      EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1435  
1436      // some smaller test cases
1437      matrixXml =
1438          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1439          "    <hal format=\"hidl\" optional=\"false\">\n"
1440          "        <name>android.hardware.camera</name>\n"
1441          "        <version>3.4</version>\n"
1442          "    </hal>\n"
1443          "    <sepolicy>\n"
1444          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
1445          "        <sepolicy-version>25.5</sepolicy-version>\n"
1446          "    </sepolicy>\n"
1447          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
1448          "</compatibility-matrix>\n";
1449      matrix = {};
1450      EXPECT_TRUE(fromXml(&matrix, matrixXml));
1451      EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1452      MatrixHal *camera = getAnyHal(matrix, "android.hardware.camera");
1453      EXPECT_NE(camera, nullptr);
1454      camera->versionRanges[0] = {3, 5};
1455      EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1456      camera->versionRanges[0] = {3, 6};
1457      EXPECT_FALSE(manifest.checkCompatibility(matrix));
1458  
1459      // reset it
1460      matrix = {};
1461      EXPECT_TRUE(fromXml(&matrix, matrixXml));
1462      set(matrix, Sepolicy{30, {{26, 0}}});
1463      EXPECT_FALSE(manifest.checkCompatibility(matrix));
1464      set(matrix, Sepolicy{30, {{25, 6}}});
1465      EXPECT_FALSE(manifest.checkCompatibility(matrix));
1466      set(matrix, Sepolicy{30, {{25, 4}}});
1467      EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
1468  }
1469  
1470  // clang-format on
1471  
TEST_F(LibVintfTest,ApexInterfaceShouldBeOkayWithoutApexInfoList)1472  TEST_F(LibVintfTest, ApexInterfaceShouldBeOkayWithoutApexInfoList) {
1473      details::FileSystemNoOp fs;
1474      details::Apex apex;
1475      ASSERT_FALSE(apex.HasUpdate(&fs));
1476      std::vector<std::string> dirs;
1477      ASSERT_EQ(OK, apex.DeviceVintfDirs(&fs, &dirs, nullptr));
1478  }
1479  
1480  struct NativeHalCompatTestParam {
1481      std::string matrixXml;
1482      std::string manifestXml;
1483      bool compatible;
1484      std::string expectedError;
1485  };
1486  
1487  class NativeHalCompatTest : public LibVintfTest,
1488                              public ::testing::WithParamInterface<NativeHalCompatTestParam> {
1489     public:
createParams()1490      static std::vector<NativeHalCompatTestParam> createParams() {
1491          std::string matrixIntf = "<compatibility-matrix " + kMetaVersionStr + R"( type="device">
1492                  <hal format="native" optional="false">
1493                      <name>foo</name>
1494                      <version>1.0</version>
1495                      <interface>
1496                          <name>IFoo</name>
1497                          <instance>default</instance>
1498                      </interface>
1499                 </hal>
1500              </compatibility-matrix>
1501          )";
1502          std::string matrixNoIntf = "<compatibility-matrix " + kMetaVersionStr + R"( type="device">
1503                  <hal format="native" optional="false">
1504                      <name>foo</name>
1505                      <version>1.0</version>
1506                      <interface>
1507                          <instance>default</instance>
1508                      </interface>
1509                 </hal>
1510              </compatibility-matrix>
1511          )";
1512          std::string matrixNoInst = "<compatibility-matrix " + kMetaVersionStr + R"( type="device">
1513                  <hal format="native" optional="false">
1514                      <name>foo</name>
1515                      <version>1.0</version>
1516                 </hal>
1517              </compatibility-matrix>
1518          )";
1519          std::string manifestFqnameIntf = "<manifest " + kMetaVersionStr + R"( type="framework">
1520                  <hal format="native">
1521                      <name>foo</name>
1522                      <fqname>@1.0::IFoo/default</fqname>
1523                 </hal>
1524              </manifest>
1525          )";
1526          std::string manifestLegacyIntf = "<manifest " + kMetaVersionStr + R"( type="framework">
1527                  <hal format="native">
1528                      <name>foo</name>
1529                      <version>1.0</version>
1530                      <interface>
1531                          <name>IFoo</name>
1532                          <instance>default</instance>
1533                      </interface>
1534                 </hal>
1535              </manifest>
1536          )";
1537          std::string manifestFqnameNoIntf = "<manifest " + kMetaVersionStr + R"( type="framework">
1538                  <hal format="native">
1539                      <name>foo</name>
1540                      <fqname>@1.0/default</fqname>
1541                 </hal>
1542              </manifest>
1543          )";
1544          std::string manifestLegacyNoIntf = "<manifest " + kMetaVersionStr + R"( type="framework">
1545                  <hal format="native">
1546                      <name>foo</name>
1547                      <version>1.0</version>
1548                      <interface>
1549                          <instance>default</instance>
1550                      </interface>
1551                 </hal>
1552              </manifest>
1553          )";
1554          std::string manifestNoInst = "<manifest " + kMetaVersionStr + R"( type="framework">
1555                  <hal format="native">
1556                      <name>foo</name>
1557                      <version>1.0</version>
1558                 </hal>
1559              </manifest>
1560          )";
1561  
1562          std::vector<NativeHalCompatTestParam> ret;
1563  
1564          // If the matrix specifies interface name, the manifest must also do.
1565          ret.emplace_back(NativeHalCompatTestParam{matrixIntf, manifestFqnameIntf, true, ""});
1566          ret.emplace_back(NativeHalCompatTestParam{matrixIntf, manifestLegacyIntf, true, ""});
1567          ret.emplace_back(NativeHalCompatTestParam{matrixIntf, manifestFqnameNoIntf, false,
1568                                                    "required: @1.0::IFoo/default"});
1569          ret.emplace_back(NativeHalCompatTestParam{matrixIntf, manifestLegacyNoIntf, false,
1570                                                    "required: @1.0::IFoo/default"});
1571          ret.emplace_back(NativeHalCompatTestParam{matrixIntf, manifestNoInst, false,
1572                                                    "required: @1.0::IFoo/default"});
1573  
1574          // If the matrix does not specify an interface name, the manifest must not do that either.
1575          ret.emplace_back(NativeHalCompatTestParam{matrixNoIntf, manifestFqnameIntf, false,
1576                                                    "required: @1.0/default"});
1577          ret.emplace_back(NativeHalCompatTestParam{matrixNoIntf, manifestLegacyIntf, false,
1578                                                    "required: @1.0/default"});
1579          ret.emplace_back(NativeHalCompatTestParam{matrixNoIntf, manifestFqnameNoIntf, true, ""});
1580          ret.emplace_back(NativeHalCompatTestParam{matrixNoIntf, manifestLegacyNoIntf, true, ""});
1581          ret.emplace_back(NativeHalCompatTestParam{matrixNoIntf, manifestNoInst, false,
1582                                                    "required: @1.0/default"});
1583  
1584          // If the matrix does not specify interface name nor instances, the manifest may either
1585          // provide instances of that version, or just a version number with no instances.
1586          ret.emplace_back(NativeHalCompatTestParam{matrixNoInst, manifestFqnameIntf, true, ""});
1587          ret.emplace_back(NativeHalCompatTestParam{matrixNoInst, manifestLegacyIntf, true, ""});
1588          ret.emplace_back(NativeHalCompatTestParam{matrixNoInst, manifestFqnameNoIntf, true, ""});
1589          ret.emplace_back(NativeHalCompatTestParam{matrixNoInst, manifestLegacyNoIntf, true, ""});
1590          ret.emplace_back(NativeHalCompatTestParam{matrixNoInst, manifestNoInst, true, ""});
1591  
1592          return ret;
1593      }
1594  };
1595  
TEST_P(NativeHalCompatTest,Compat)1596  TEST_P(NativeHalCompatTest, Compat) {
1597      auto params = GetParam();
1598      std::string error;
1599      HalManifest manifest;
1600      ASSERT_TRUE(fromXml(&manifest, params.manifestXml, &error)) << error;
1601      CompatibilityMatrix matrix;
1602      ASSERT_TRUE(fromXml(&matrix, params.matrixXml, &error)) << error;
1603      EXPECT_EQ(params.compatible, manifest.checkCompatibility(matrix, &error)) << error;
1604      if (!params.expectedError.empty()) {
1605          EXPECT_THAT(error, HasSubstr(params.expectedError));
1606      } else {
1607          EXPECT_THAT(error, IsEmpty());
1608      }
1609  }
1610  
1611  INSTANTIATE_TEST_CASE_P(LibVintfTest, NativeHalCompatTest,
1612                          ::testing::ValuesIn(NativeHalCompatTest::createParams()));
1613  
1614  // clang-format off
1615  
1616  /////////////////// xmlfile tests
1617  
TEST_F(LibVintfTest,HalManifestConverterXmlFile)1618  TEST_F(LibVintfTest, HalManifestConverterXmlFile) {
1619      HalManifest vm = testDeviceManifestWithXmlFile();
1620      std::string xml = toXml(
1621          vm, SerializeFlags::HALS_ONLY.enableSepolicy().enableXmlFiles());
1622      EXPECT_EQ(xml,
1623                "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1624                "    <hal format=\"hidl\">\n"
1625                "        <name>android.hardware.camera</name>\n"
1626                "        <transport>hwbinder</transport>\n"
1627                "        <fqname>@2.0::IBetterCamera/camera</fqname>\n"
1628                "        <fqname>@2.0::ICamera/default</fqname>\n"
1629                "        <fqname>@2.0::ICamera/legacy/0</fqname>\n"
1630                "    </hal>\n"
1631                "    <hal format=\"hidl\">\n"
1632                "        <name>android.hardware.nfc</name>\n"
1633                "        <transport arch=\"32+64\">passthrough</transport>\n"
1634                "        <fqname>@1.0::INfc/default</fqname>\n"
1635                "    </hal>\n"
1636                "    <sepolicy>\n"
1637                "        <version>25.0</version>\n"
1638                "    </sepolicy>\n"
1639                "    <xmlfile>\n"
1640                "        <name>media_profile</name>\n"
1641                "        <version>1.0</version>\n"
1642                "    </xmlfile>\n"
1643                "</manifest>\n");
1644      HalManifest vm2;
1645      EXPECT_TRUE(fromXml(&vm2, xml));
1646      EXPECT_EQ(vm, vm2);
1647  }
1648  
TEST_F(LibVintfTest,HalManifestConverterXmlFileWithInterface)1649  TEST_F(LibVintfTest, HalManifestConverterXmlFileWithInterface) {
1650      HalManifest vm = testDeviceManifestWithXmlFile();
1651      std::string xml =
1652                "<manifest " + kMetaVersionStr + " type=\"device\">\n"
1653                "    <hal format=\"hidl\">\n"
1654                "        <name>android.hardware.camera</name>\n"
1655                "        <transport>hwbinder</transport>\n"
1656                "        <version>2.0</version>\n"
1657                "        <interface>\n"
1658                "            <name>IBetterCamera</name>\n"
1659                "            <instance>camera</instance>\n"
1660                "        </interface>\n"
1661                "        <interface>\n"
1662                "            <name>ICamera</name>\n"
1663                "            <instance>default</instance>\n"
1664                "            <instance>legacy/0</instance>\n"
1665                "        </interface>\n"
1666                "    </hal>\n"
1667                "    <hal format=\"hidl\">\n"
1668                "        <name>android.hardware.nfc</name>\n"
1669                "        <transport arch=\"32+64\">passthrough</transport>\n"
1670                "        <version>1.0</version>\n"
1671                "        <interface>\n"
1672                "            <name>INfc</name>\n"
1673                "            <instance>default</instance>\n"
1674                "        </interface>\n"
1675                "    </hal>\n"
1676                "    <sepolicy>\n"
1677                "        <version>25.0</version>\n"
1678                "    </sepolicy>\n"
1679                "    <xmlfile>\n"
1680                "        <name>media_profile</name>\n"
1681                "        <version>1.0</version>\n"
1682                "    </xmlfile>\n"
1683                "</manifest>\n";
1684      HalManifest vm2;
1685      EXPECT_TRUE(fromXml(&vm2, xml));
1686      EXPECT_EQ(vm, vm2);
1687  }
1688  
TEST_F(LibVintfTest,CompatibilityMatrixConverterXmlFile)1689  TEST_F(LibVintfTest, CompatibilityMatrixConverterXmlFile) {
1690      CompatibilityMatrix cm;
1691      addXmlFile(cm, "media_profile", {1, 0});
1692      std::string xml = toXml(cm, SerializeFlags::XMLFILES_ONLY);
1693      EXPECT_EQ(xml,
1694                "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1695                "    <xmlfile format=\"dtd\" optional=\"true\">\n"
1696                "        <name>media_profile</name>\n"
1697                "        <version>1.0</version>\n"
1698                "    </xmlfile>\n"
1699                "</compatibility-matrix>\n");
1700      CompatibilityMatrix cm2;
1701      EXPECT_TRUE(fromXml(&cm2, xml));
1702      EXPECT_EQ(cm, cm2);
1703  }
1704  
TEST_F(LibVintfTest,CompatibilityMatrixConverterXmlFile2)1705  TEST_F(LibVintfTest, CompatibilityMatrixConverterXmlFile2) {
1706      std::string error;
1707      std::string xml =
1708          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
1709          "    <xmlfile format=\"dtd\" optional=\"false\">\n"
1710          "        <name>media_profile</name>\n"
1711          "        <version>1.0</version>\n"
1712          "    </xmlfile>\n"
1713          "</compatibility-matrix>\n";
1714      CompatibilityMatrix cm;
1715      EXPECT_FALSE(fromXml(&cm, xml, &error));
1716      EXPECT_EQ("compatibility-matrix.xmlfile entry media_profile has to be optional for "
1717                "compatibility matrix version 1.0", error);
1718  }
1719  
TEST_F(LibVintfTest,ManifestXmlFilePathDevice)1720  TEST_F(LibVintfTest, ManifestXmlFilePathDevice) {
1721      std::string manifestXml =
1722          "<manifest " + kMetaVersionStr + " type=\"device\">"
1723          "    <xmlfile>"
1724          "        <name>media_profile</name>"
1725          "        <version>1.0</version>"
1726          "    </xmlfile>"
1727          "</manifest>";
1728      HalManifest manifest;
1729      EXPECT_TRUE(fromXml(&manifest, manifestXml));
1730      EXPECT_EQ(manifest.getXmlFilePath("media_profile", {1, 0}),
1731                "/vendor/etc/media_profile_V1_0.xml");
1732  }
1733  
TEST_F(LibVintfTest,ManifestXmlFilePathFramework)1734  TEST_F(LibVintfTest, ManifestXmlFilePathFramework) {
1735      std::string manifestXml =
1736          "<manifest " + kMetaVersionStr + " type=\"framework\">"
1737          "    <xmlfile>"
1738          "        <name>media_profile</name>"
1739          "        <version>1.0</version>"
1740          "    </xmlfile>"
1741          "</manifest>";
1742      HalManifest manifest;
1743      EXPECT_TRUE(fromXml(&manifest, manifestXml));
1744      EXPECT_EQ(manifest.getXmlFilePath("media_profile", {1, 0}),
1745                "/system/etc/media_profile_V1_0.xml");
1746  }
1747  
TEST_F(LibVintfTest,ManifestXmlFilePathOverride)1748  TEST_F(LibVintfTest, ManifestXmlFilePathOverride) {
1749      std::string manifestXml =
1750          "<manifest " + kMetaVersionStr + " type=\"device\">"
1751          "    <xmlfile>"
1752          "        <name>media_profile</name>"
1753          "        <version>1.0</version>"
1754          "        <path>/vendor/etc/foo.xml</path>"
1755          "    </xmlfile>"
1756          "</manifest>";
1757      HalManifest manifest;
1758      EXPECT_TRUE(fromXml(&manifest, manifestXml));
1759      EXPECT_EQ(manifest.getXmlFilePath("media_profile", {1, 0}), "/vendor/etc/foo.xml");
1760  }
1761  
TEST_F(LibVintfTest,ManifestXmlFilePathMissing)1762  TEST_F(LibVintfTest, ManifestXmlFilePathMissing) {
1763      std::string manifestXml =
1764          "<manifest " + kMetaVersionStr + " type=\"device\">"
1765          "    <xmlfile>"
1766          "        <name>media_profile</name>"
1767          "        <version>1.1</version>"
1768          "    </xmlfile>"
1769          "</manifest>";
1770      HalManifest manifest;
1771      EXPECT_TRUE(fromXml(&manifest, manifestXml));
1772      EXPECT_EQ(manifest.getXmlFilePath("media_profile", {1, 0}), "");
1773  }
1774  
TEST_F(LibVintfTest,MatrixXmlFilePathFramework)1775  TEST_F(LibVintfTest, MatrixXmlFilePathFramework) {
1776      std::string matrixXml =
1777          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">"
1778          "    <xmlfile format=\"dtd\" optional=\"true\">"
1779          "        <name>media_profile</name>"
1780          "        <version>2.0-1</version>"
1781          "    </xmlfile>"
1782          "</compatibility-matrix>";
1783      CompatibilityMatrix matrix;
1784      EXPECT_TRUE(fromXml(&matrix, matrixXml));
1785      EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 1}),
1786                "/system/etc/media_profile_V2_1.dtd");
1787  }
1788  
TEST_F(LibVintfTest,MatrixXmlFilePathDevice)1789  TEST_F(LibVintfTest, MatrixXmlFilePathDevice) {
1790      std::string matrixXml =
1791          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1792          "    <xmlfile format=\"xsd\" optional=\"true\">"
1793          "        <name>media_profile</name>"
1794          "        <version>2.0-1</version>"
1795          "    </xmlfile>"
1796          "</compatibility-matrix>";
1797      CompatibilityMatrix matrix;
1798      EXPECT_TRUE(fromXml(&matrix, matrixXml));
1799      EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 0}),
1800                "/vendor/etc/media_profile_V2_1.xsd");
1801  }
1802  
TEST_F(LibVintfTest,MatrixXmlFilePathOverride)1803  TEST_F(LibVintfTest, MatrixXmlFilePathOverride) {
1804      std::string matrixXml =
1805          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">"
1806          "    <xmlfile format=\"xsd\" optional=\"true\">"
1807          "        <name>media_profile</name>"
1808          "        <version>2.0-1</version>"
1809          "        <path>/system/etc/foo.xsd</path>"
1810          "    </xmlfile>"
1811          "</compatibility-matrix>";
1812      CompatibilityMatrix matrix;
1813      EXPECT_TRUE(fromXml(&matrix, matrixXml));
1814      EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 0}), "/system/etc/foo.xsd");
1815  }
1816  
TEST_F(LibVintfTest,MatrixXmlFilePathMissing)1817  TEST_F(LibVintfTest, MatrixXmlFilePathMissing) {
1818      std::string matrixXml =
1819          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">"
1820          "    <xmlfile format=\"dtd\" optional=\"true\">"
1821          "        <name>media_profile</name>"
1822          "        <version>2.1</version>"
1823          "    </xmlfile>"
1824          "</compatibility-matrix>";
1825      CompatibilityMatrix matrix;
1826      EXPECT_TRUE(fromXml(&matrix, matrixXml));
1827      EXPECT_EQ(matrix.getXmlSchemaPath("media_profile", {2, 0}), "");
1828  }
1829  
processData(const std::string & data,bool processComments,bool relaxedFormat=false)1830  std::pair<KernelConfigParser, status_t> processData(const std::string& data, bool processComments,
1831                                                      bool relaxedFormat = false) {
1832      KernelConfigParser parser(processComments, relaxedFormat);
1833      const char* p = data.c_str();
1834      size_t n = 0;
1835      size_t chunkSize;
1836      status_t status = OK;
1837      for (; n < data.size(); p += chunkSize, n += chunkSize) {
1838          chunkSize = std::min<size_t>(5, data.size() - n);
1839          if ((status = parser.process(p, chunkSize)) != OK) {
1840              break;
1841          }
1842      }
1843      return {std::move(parser), status};
1844  }
1845  
TEST_F(LibVintfTest,KernelConfigParser)1846  TEST_F(LibVintfTest, KernelConfigParser) {
1847      // usage in /proc/config.gz
1848      const std::string data =
1849          "# CONFIG_NOT_SET is not set\n"
1850          "CONFIG_ONE=1\n"
1851          "CONFIG_Y=y\n"
1852          "CONFIG_STR=\"string\"\n";
1853      auto pair = processData(data, false /* processComments */);
1854      ASSERT_EQ(OK, pair.second) << pair.first.error();
1855      const auto& configs = pair.first.configs();
1856  
1857      EXPECT_EQ(configs.find("CONFIG_ONE")->second, "1");
1858      EXPECT_EQ(configs.find("CONFIG_Y")->second, "y");
1859      EXPECT_EQ(configs.find("CONFIG_STR")->second, "\"string\"");
1860      EXPECT_EQ(configs.find("CONFIG_NOT_SET"), configs.end());
1861  }
1862  
TEST_F(LibVintfTest,KernelConfigParser2)1863  TEST_F(LibVintfTest, KernelConfigParser2) {
1864      // usage in android-base.config
1865      const std::string data =
1866          "# CONFIG_NOT_SET is not set\n"
1867          "CONFIG_ONE=1\n"
1868          "CONFIG_Y=y\n"
1869          "CONFIG_STR=string\n"
1870          "# ignore_thiscomment\n"
1871          "# CONFIG_NOT_SET2 is not set\n";
1872      auto pair = processData(data, true /* processComments */);
1873      ASSERT_EQ(OK, pair.second) << pair.first.error();
1874      const auto& configs = pair.first.configs();
1875  
1876      EXPECT_EQ(configs.find("CONFIG_ONE")->second, "1");
1877      EXPECT_EQ(configs.find("CONFIG_Y")->second, "y");
1878      EXPECT_EQ(configs.find("CONFIG_STR")->second, "string");
1879      EXPECT_EQ(configs.find("CONFIG_NOT_SET")->second, "n");
1880      EXPECT_EQ(configs.find("CONFIG_NOT_SET2")->second, "n");
1881  }
1882  
TEST_F(LibVintfTest,KernelConfigParserSpace)1883  TEST_F(LibVintfTest, KernelConfigParserSpace) {
1884      // usage in android-base.config
1885      const std::string data =
1886          "   #   CONFIG_NOT_SET is not set   \n"
1887          "  CONFIG_ONE=1   # 'tis a one!\n"
1888          " CONFIG_TWO=2 #'tis a two!   \n"
1889          " CONFIG_THREE=3#'tis a three!   \n"
1890          " CONFIG_233=233#'tis a three!   \n"
1891          "#yey! random comments\n"
1892          "CONFIG_Y=y   \n"
1893          " CONFIG_YES=y#YES!   \n"
1894          "CONFIG_STR=string\n"
1895          "CONFIG_HELLO=hello world!  #still works\n"
1896          "CONFIG_WORLD=hello world!       \n"
1897          "CONFIG_GOOD   =   good morning!  #comments here\n"
1898          "    CONFIG_MORNING   =   good morning!  \n";
1899      auto pair = processData(data, true /* processComments */, true /* relaxedFormat */);
1900      ASSERT_EQ(OK, pair.second) << pair.first.error();
1901      const auto& configs = pair.first.configs();
1902  
1903      EXPECT_EQ(configs.find("CONFIG_ONE")->second, "1");
1904      EXPECT_EQ(configs.find("CONFIG_TWO")->second, "2");
1905      EXPECT_EQ(configs.find("CONFIG_THREE")->second, "3");
1906      EXPECT_EQ(configs.find("CONFIG_Y")->second, "y");
1907      EXPECT_EQ(configs.find("CONFIG_STR")->second, "string");
1908      EXPECT_EQ(configs.find("CONFIG_HELLO")->second, "hello world!")
1909          << "Value should be \"hello world!\" without trailing spaces";
1910      EXPECT_EQ(configs.find("CONFIG_WORLD")->second, "hello world!")
1911          << "Value should be \"hello world!\" without trailing spaces";
1912      EXPECT_EQ(configs.find("CONFIG_GOOD")->second, "good morning!")
1913          << "Value should be \"good morning!\" without leading or trailing spaces";
1914      EXPECT_EQ(configs.find("CONFIG_MORNING")->second, "good morning!")
1915          << "Value should be \"good morning!\" without leading or trailing spaces";
1916      EXPECT_EQ(configs.find("CONFIG_NOT_SET")->second, "n");
1917  }
1918  
TEST_F(LibVintfTest,NetutilsWrapperMatrix)1919  TEST_F(LibVintfTest, NetutilsWrapperMatrix) {
1920      std::string matrixXml;
1921      CompatibilityMatrix matrix;
1922      std::string error;
1923  
1924      matrixXml =
1925          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1926          "    <hal format=\"native\" optional=\"false\">"
1927          "        <name>netutils-wrapper</name>"
1928          "        <version>1.0</version>"
1929          "    </hal>"
1930          "</compatibility-matrix>";
1931      EXPECT_TRUE(fromXml(&matrix, matrixXml, &error)) << error;
1932  
1933  // only host libvintf hardcodes netutils-wrapper version requirements
1934  #ifndef LIBVINTF_TARGET
1935  
1936      matrixXml =
1937          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1938          "    <hal format=\"native\" optional=\"false\">"
1939          "        <name>netutils-wrapper</name>"
1940          "        <version>1.0-1</version>"
1941          "    </hal>"
1942          "</compatibility-matrix>";
1943      EXPECT_FALSE(fromXml(&matrix, matrixXml, &error));
1944      EXPECT_THAT(error, HasSubstr(
1945          "netutils-wrapper HAL must specify exactly one version x.0, but a range is provided. "
1946          "Perhaps you mean '1.0'?"));
1947  
1948      matrixXml =
1949          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1950          "    <hal format=\"native\" optional=\"false\">"
1951          "        <name>netutils-wrapper</name>"
1952          "        <version>1.1</version>"
1953          "    </hal>"
1954          "</compatibility-matrix>";
1955      EXPECT_FALSE(fromXml(&matrix, matrixXml, &error));
1956      EXPECT_THAT(error, HasSubstr(
1957          "netutils-wrapper HAL must specify exactly one version x.0, but minor version is not 0. "
1958          "Perhaps you mean '1.0'?"));
1959  
1960      matrixXml =
1961          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">"
1962          "    <hal format=\"native\" optional=\"false\">"
1963          "        <name>netutils-wrapper</name>"
1964          "        <version>1.0</version>"
1965          "        <version>2.0</version>"
1966          "    </hal>"
1967          "</compatibility-matrix>";
1968      EXPECT_FALSE(fromXml(&matrix, matrixXml, &error));
1969      EXPECT_THAT(error, HasSubstr(
1970          "netutils-wrapper HAL must specify exactly one version x.0, but multiple <version> element "
1971          "is specified."));
1972  
1973  #endif  // LIBVINTF_TARGET
1974  }
1975  
TEST_F(LibVintfTest,NetutilsWrapperManifest)1976  TEST_F(LibVintfTest, NetutilsWrapperManifest) {
1977      std::string manifestXml;
1978      HalManifest manifest;
1979      std::string error;
1980  
1981      manifestXml =
1982          "<manifest " + kMetaVersionStr + " type=\"framework\">"
1983          "    <hal format=\"native\">"
1984          "        <name>netutils-wrapper</name>"
1985          "        <version>1.0</version>"
1986          "        <version>2.0</version>"
1987          "    </hal>"
1988          "</manifest>";
1989      EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
1990  
1991  // only host libvintf hardcodes netutils-wrapper version requirements
1992  #ifndef LIBVINTF_TARGET
1993  
1994      manifestXml =
1995          "<manifest " + kMetaVersionStr + " type=\"framework\">"
1996          "    <hal format=\"native\">"
1997          "        <name>netutils-wrapper</name>"
1998          "        <version>1.1</version>"
1999          "    </hal>"
2000          "</manifest>";
2001      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error));
2002      EXPECT_THAT(error, HasSubstr(
2003          "netutils-wrapper HAL must specify exactly one version x.0, but minor version is not 0."));
2004  
2005      manifestXml =
2006          "<manifest " + kMetaVersionStr + " type=\"framework\">"
2007          "    <hal format=\"native\">"
2008          "        <name>netutils-wrapper</name>"
2009          "        <version>1.0</version>"
2010          "        <version>2.1</version>"
2011          "    </hal>"
2012          "</manifest>";
2013      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error));
2014      EXPECT_THAT(error, HasSubstr(
2015          "netutils-wrapper HAL must specify exactly one version x.0, but minor version is not 0."));
2016  
2017  #endif  // LIBVINTF_TARGET
2018  }
2019  
TEST_F(LibVintfTest,KernelConfigConditionTest)2020  TEST_F(LibVintfTest, KernelConfigConditionTest) {
2021      std::string error;
2022      std::string xml =
2023          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2024          "    <kernel version=\"3.18.22\"/>\n"
2025          "    <kernel version=\"3.18.22\">\n"
2026          "        <conditions>\n"
2027          "            <config>\n"
2028          "                <key>CONFIG_ARM</key>\n"
2029          "                <value type=\"tristate\">y</value>\n"
2030          "            </config>\n"
2031          "        </conditions>\n"
2032          "        <config>\n"
2033          "            <key>CONFIG_FOO</key>\n"
2034          "            <value type=\"tristate\">y</value>\n"
2035          "        </config>\n"
2036          "    </kernel>\n"
2037          "    <sepolicy>\n"
2038          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2039          "        <sepolicy-version>25.0</sepolicy-version>\n"
2040          "    </sepolicy>\n"
2041          "    <avb>\n"
2042          "        <vbmeta-version>2.1</vbmeta-version>\n"
2043          "    </avb>\n"
2044          "</compatibility-matrix>\n";
2045  
2046      CompatibilityMatrix cm;
2047      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2048      const auto& kernels = getKernels(cm);
2049      ASSERT_GE(kernels.size(), 2u);
2050      ASSERT_TRUE(kernels[0].conditions().empty());
2051      const auto& kernel = kernels[1];
2052      const auto& cond = kernel.conditions();
2053      ASSERT_FALSE(cond.empty());
2054      EXPECT_EQ("CONFIG_ARM", cond.begin()->first);
2055      EXPECT_EQ(KernelConfigTypedValue(Tristate::YES), cond.begin()->second);
2056      EXPECT_FALSE(kernel.configs().empty());
2057  
2058      EXPECT_EQ(xml, toXml(cm));
2059  }
2060  
TEST_F(LibVintfTest,KernelConfigConditionEmptyTest)2061  TEST_F(LibVintfTest, KernelConfigConditionEmptyTest) {
2062      std::string error;
2063      std::string xml =
2064          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2065          "    <kernel version=\"4.4.0\"/>\n"
2066          "    <kernel version=\"3.18.22\">\n"
2067          "        <conditions>\n"
2068          "            <config>\n"
2069          "                <key>CONFIG_ARM</key>\n"
2070          "                <value type=\"tristate\">y</value>\n"
2071          "            </config>\n"
2072          "        </conditions>\n"
2073          "    </kernel>\n"
2074          "</compatibility-matrix>\n";
2075  
2076      CompatibilityMatrix cm;
2077      EXPECT_FALSE(fromXml(&cm, xml, &error))
2078          << "Should not accept first kernel version with non-empty conditions";
2079      EXPECT_EQ(
2080          "First <kernel> for version 3.18 must have empty <conditions> "
2081          "for backwards compatibility.", error);
2082  }
2083  
TEST_F(LibVintfTest,KernelConfigConditionMatch)2084  TEST_F(LibVintfTest, KernelConfigConditionMatch) {
2085      RuntimeInfo runtime = testRuntimeInfo();
2086      std::string error;
2087      std::string xml;
2088      CompatibilityMatrix cm;
2089  
2090      xml =
2091          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2092          "    <kernel version=\"3.18.22\"/>\n"
2093          "    <kernel version=\"3.18.22\">\n"
2094          "        <conditions>\n"
2095          "            <config>\n"
2096          "                <key>CONFIG_64BIT</key>\n"
2097          "                <value type=\"tristate\">y</value>\n"
2098          "            </config>\n"
2099          "        </conditions>\n"
2100          "        <config>\n"
2101          "            <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2102          "            <value type=\"int\">24</value>\n"
2103          "        </config>\n"
2104          "    </kernel>\n"
2105          "    <sepolicy>\n"
2106          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2107          "    </sepolicy>\n"
2108          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2109          "</compatibility-matrix>\n";
2110  
2111      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2112      EXPECT_TRUE(runtime.checkCompatibility(cm, &error)) << error;
2113  
2114      xml =
2115          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2116          "    <kernel version=\"3.18.22\"/>\n"
2117          "    <kernel version=\"3.18.22\">\n"
2118          "        <conditions>\n"
2119          "            <config>\n"
2120          "                <key>CONFIG_64BIT</key>\n"
2121          "                <value type=\"tristate\">y</value>\n"
2122          "            </config>\n"
2123          "        </conditions>\n"
2124          "        <config>\n"
2125          "            <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2126          "            <value type=\"int\">26</value>\n"
2127          "        </config>\n"
2128          "    </kernel>\n"
2129          "    <sepolicy>\n"
2130          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2131          "    </sepolicy>\n"
2132          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2133          "</compatibility-matrix>\n";
2134  
2135      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2136      EXPECT_FALSE(runtime.checkCompatibility(cm, &error))
2137          << "conditions met, so CONFIG_ARCH_MMAP_RND_BITS should not match";
2138  
2139      xml =
2140          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2141          "    <kernel version=\"3.18.22\"/>\n"
2142          "    <kernel version=\"3.18.22\">\n"
2143          "        <conditions>\n"
2144          "            <config>\n"
2145          "                <key>CONFIG_64BIT</key>\n"
2146          "                <value type=\"tristate\">n</value>\n"
2147          "            </config>\n"
2148          "        </conditions>\n"
2149          "        <config>\n"
2150          "            <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2151          "            <value type=\"int\">26</value>\n"
2152          "        </config>\n"
2153          "    </kernel>\n"
2154          "    <sepolicy>\n"
2155          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2156          "    </sepolicy>\n"
2157          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2158          "</compatibility-matrix>\n";
2159  
2160      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2161      EXPECT_TRUE(runtime.checkCompatibility(cm, &error)) << error;
2162      xml =
2163          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2164          "    <kernel version=\"3.18.22\"/>\n"
2165          "    <kernel version=\"3.18.22\">\n"
2166          "        <conditions>\n"
2167          "            <config>\n"
2168          "                <key>CONFIG_64BIT</key>\n"
2169          "                <value type=\"tristate\">y</value>\n"
2170          "            </config>\n"
2171          "            <config>\n"
2172          "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2173          "                <value type=\"int\">24</value>\n"
2174          "            </config>\n"
2175          "        </conditions>\n"
2176          "        <config>\n"
2177          "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
2178          "            <value type=\"int\">0xdead000000000000</value>\n"
2179          "        </config>\n"
2180          "    </kernel>\n"
2181          "    <sepolicy>\n"
2182          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2183          "    </sepolicy>\n"
2184          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2185          "</compatibility-matrix>\n";
2186  
2187      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2188      EXPECT_TRUE(runtime.checkCompatibility(cm, &error));
2189  
2190      xml =
2191          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2192          "    <kernel version=\"3.18.22\"/>\n"
2193          "    <kernel version=\"3.18.22\">\n"
2194          "        <conditions>\n"
2195          "            <config>\n"
2196          "                <key>CONFIG_64BIT</key>\n"
2197          "                <value type=\"tristate\">y</value>\n"
2198          "            </config>\n"
2199          "            <config>\n"
2200          "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2201          "                <value type=\"int\">24</value>\n"
2202          "            </config>\n"
2203          "        </conditions>\n"
2204          "        <config>\n"
2205          "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
2206          "            <value type=\"int\">0xbeaf000000000000</value>\n"
2207          "        </config>\n"
2208          "    </kernel>\n"
2209          "    <sepolicy>\n"
2210          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2211          "    </sepolicy>\n"
2212          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2213          "</compatibility-matrix>\n";
2214  
2215      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2216      EXPECT_FALSE(runtime.checkCompatibility(cm, &error))
2217          << "conditions have 'and' relationship, so CONFIG_ILLEGAL_POINTER_VALUE should not match";
2218  
2219      xml =
2220          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2221          "    <kernel version=\"3.18.22\"/>\n"
2222          "    <kernel version=\"3.18.22\">\n"
2223          "        <conditions>\n"
2224          "            <config>\n"
2225          "                <key>CONFIG_64BIT</key>\n"
2226          "                <value type=\"tristate\">y</value>\n"
2227          "            </config>\n"
2228          "            <config>\n"
2229          "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2230          "                <value type=\"int\">26</value>\n"
2231          "            </config>\n"
2232          "        </conditions>\n"
2233          "        <config>\n"
2234          "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
2235          "            <value type=\"int\">0xbeaf000000000000</value>\n"
2236          "        </config>\n"
2237          "    </kernel>\n"
2238          "    <sepolicy>\n"
2239          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2240          "    </sepolicy>\n"
2241          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2242          "</compatibility-matrix>\n";
2243  
2244      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2245      EXPECT_TRUE(runtime.checkCompatibility(cm, &error)) << error;
2246  
2247      xml =
2248          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2249          "    <kernel version=\"3.18.22\">\n"
2250          "        <config>\n"
2251          "            <key>CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES</key>\n"
2252          "            <value type=\"string\"/>\n"
2253          "        </config>\n"
2254          "    </kernel>\n"
2255          "    <kernel version=\"3.18.22\">\n"
2256          "        <conditions>\n"
2257          "            <config>\n"
2258          "                <key>CONFIG_64BIT</key>\n"
2259          "                <value type=\"tristate\">y</value>\n"
2260          "            </config>\n"
2261          "        </conditions>\n"
2262          "        <config>\n"
2263          "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
2264          "            <value type=\"int\">0xdead000000000000</value>\n"
2265          "        </config>\n"
2266          "    </kernel>\n"
2267          "    <kernel version=\"3.18.22\">\n"
2268          "        <conditions>\n"
2269          "            <config>\n"
2270          "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2271          "                <value type=\"int\">24</value>\n"
2272          "            </config>\n"
2273          "        </conditions>\n"
2274          "        <config>\n"
2275          "            <key>CONFIG_ANDROID_BINDER_DEVICES</key>\n"
2276          "            <value type=\"string\">binder,hwbinder</value>\n"
2277          "        </config>\n"
2278          "    </kernel>\n"
2279          "    <sepolicy>\n"
2280          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2281          "    </sepolicy>\n"
2282          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2283          "</compatibility-matrix>\n";
2284  
2285      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2286      EXPECT_TRUE(runtime.checkCompatibility(cm, &error)) << error;
2287  
2288      xml =
2289          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2290          "    <kernel version=\"3.18.22\">\n"
2291          "        <config>\n"
2292          "            <key>CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES</key>\n"
2293          "            <value type=\"string\"/>\n"
2294          "        </config>\n"
2295          "    </kernel>\n"
2296          "    <kernel version=\"3.18.22\">\n"
2297          "        <conditions>\n"
2298          "            <config>\n"
2299          "                <key>CONFIG_64BIT</key>\n"
2300          "                <value type=\"tristate\">y</value>\n"
2301          "            </config>\n"
2302          "        </conditions>\n"
2303          "        <config>\n"
2304          "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
2305          "            <value type=\"int\">0xbeaf000000000000</value>\n"
2306          "        </config>\n"
2307          "    </kernel>\n"
2308          "    <kernel version=\"3.18.22\">\n"
2309          "        <conditions>\n"
2310          "            <config>\n"
2311          "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2312          "                <value type=\"int\">24</value>\n"
2313          "            </config>\n"
2314          "        </conditions>\n"
2315          "        <config>\n"
2316          "            <key>CONFIG_ANDROID_BINDER_DEVICES</key>\n"
2317          "            <value type=\"string\">binder,hwbinder</value>\n"
2318          "        </config>\n"
2319          "    </kernel>\n"
2320          "    <sepolicy>\n"
2321          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2322          "    </sepolicy>\n"
2323          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2324          "</compatibility-matrix>\n";
2325  
2326      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2327      EXPECT_FALSE(runtime.checkCompatibility(cm, &error)) << "all fragments should be used.";
2328  
2329      xml =
2330          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
2331          "    <kernel version=\"3.18.22\">\n"
2332          "        <config>\n"
2333          "            <key>CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES</key>\n"
2334          "            <value type=\"string\"/>\n"
2335          "        </config>\n"
2336          "    </kernel>\n"
2337          "    <kernel version=\"3.18.22\">\n"
2338          "        <conditions>\n"
2339          "            <config>\n"
2340          "                <key>CONFIG_64BIT</key>\n"
2341          "                <value type=\"tristate\">y</value>\n"
2342          "            </config>\n"
2343          "        </conditions>\n"
2344          "        <config>\n"
2345          "            <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
2346          "            <value type=\"int\">0xdead000000000000</value>\n"
2347          "        </config>\n"
2348          "    </kernel>\n"
2349          "    <kernel version=\"3.18.22\">\n"
2350          "        <conditions>\n"
2351          "            <config>\n"
2352          "                <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
2353          "                <value type=\"int\">24</value>\n"
2354          "            </config>\n"
2355          "        </conditions>\n"
2356          "        <config>\n"
2357          "            <key>CONFIG_ANDROID_BINDER_DEVICES</key>\n"
2358          "            <value type=\"string\">binder</value>\n"
2359          "        </config>\n"
2360          "    </kernel>\n"
2361          "    <sepolicy>\n"
2362          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
2363          "    </sepolicy>\n"
2364          "    <avb><vbmeta-version>2.1</vbmeta-version></avb>\n"
2365          "</compatibility-matrix>\n";
2366  
2367      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2368      EXPECT_FALSE(runtime.checkCompatibility(cm, &error)) << "all fragments should be used";
2369  }
2370  
2371  // Run KernelConfigParserInvalidTest on processComments = {true, false}
2372  class KernelConfigParserInvalidTest : public ::testing::TestWithParam<bool> {};
2373  
TEST_P(KernelConfigParserInvalidTest,NonSet1)2374  TEST_P(KernelConfigParserInvalidTest, NonSet1) {
2375      const std::string data = "# CONFIG_NOT_EXIST is not sat\n";
2376      auto pair = processData(data, GetParam() /* processComments */, true /* relaxedFormat */);
2377      ASSERT_EQ(OK, pair.second) << pair.first.error();
2378      const auto& configs = pair.first.configs();
2379      EXPECT_EQ(configs.find("CONFIG_NOT_EXIST"), configs.end())
2380          << "CONFIG_NOT_EXIST should not exist because of typo";
2381  }
2382  
TEST_P(KernelConfigParserInvalidTest,InvalidLine1)2383  TEST_P(KernelConfigParserInvalidTest, InvalidLine1) {
2384      const std::string data = "FOO_CONFIG=foo\n";
2385      ASSERT_NE(OK,
2386                processData(data, GetParam() /* processComments */, true /* relaxedFormat */).second);
2387  }
2388  
TEST_P(KernelConfigParserInvalidTest,InvalidLine2)2389  TEST_P(KernelConfigParserInvalidTest, InvalidLine2) {
2390      const std::string data = "CONFIG_BAR-BAZ=foo\n";
2391      ASSERT_NE(OK,
2392                processData(data, GetParam() /* processComments */, true /* relaxedFormat */).second);
2393  }
2394  
2395  INSTANTIATE_TEST_CASE_P(KernelConfigParser, KernelConfigParserInvalidTest, ::testing::Bool());
2396  
TEST_F(LibVintfTest,MatrixLevel)2397  TEST_F(LibVintfTest, MatrixLevel) {
2398      std::string error;
2399      CompatibilityMatrix cm;
2400      std::string xml;
2401  
2402      xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\"/>";
2403      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2404      EXPECT_EQ(Level::UNSPECIFIED, cm.level());
2405  
2406      xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"legacy\"/>";
2407      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2408      EXPECT_EQ(Level::LEGACY, cm.level());
2409  
2410      xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\"/>";
2411      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2412      EXPECT_EQ(1u, cm.level());
2413  }
2414  
TEST_F(LibVintfTest,ManifestLevel)2415  TEST_F(LibVintfTest, ManifestLevel) {
2416      std::string error;
2417      HalManifest manifest;
2418      std::string xml;
2419  
2420      xml = "<manifest " + kMetaVersionStr + " type=\"device\"/>";
2421      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
2422      EXPECT_EQ(Level::UNSPECIFIED, manifest.level());
2423  
2424      xml = "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"legacy\"/>";
2425      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
2426      EXPECT_EQ(Level::LEGACY, manifest.level());
2427  
2428      xml = "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\"/>";
2429      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
2430      EXPECT_EQ(1u, manifest.level());
2431  }
2432  
TEST_F(LibVintfTest,AddOptionalHal)2433  TEST_F(LibVintfTest, AddOptionalHal) {
2434      CompatibilityMatrix cm1;
2435      CompatibilityMatrix cm2;
2436      std::string error;
2437      std::string xml;
2438  
2439      xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\"/>";
2440      EXPECT_TRUE(fromXml(&cm1, xml, &error)) << error;
2441  
2442      xml =
2443          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2444          "    <hal format=\"hidl\" optional=\"false\">\n"
2445          "        <name>android.hardware.foo</name>\n"
2446          "        <version>1.0-1</version>\n"
2447          "        <interface>\n"
2448          "            <name>IFoo</name>\n"
2449          "            <instance>default</instance>\n"
2450          "        </interface>\n"
2451          "    </hal>\n"
2452          "</compatibility-matrix>\n";
2453      EXPECT_TRUE(fromXml(&cm2, xml, &error)) << error;
2454  
2455      EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2456      xml = toXml(cm1, SerializeFlags::HALS_ONLY);
2457      EXPECT_EQ(xml,
2458                "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2459                "    <hal format=\"hidl\" optional=\"true\">\n"
2460                "        <name>android.hardware.foo</name>\n"
2461                "        <version>1.0-1</version>\n"
2462                "        <interface>\n"
2463                "            <name>IFoo</name>\n"
2464                "            <instance>default</instance>\n"
2465                "        </interface>\n"
2466                "    </hal>\n"
2467                "</compatibility-matrix>\n");
2468  }
2469  
TEST_F(LibVintfTest,AddOptionalHalMinorVersion)2470  TEST_F(LibVintfTest, AddOptionalHalMinorVersion) {
2471      CompatibilityMatrix cm1;
2472      CompatibilityMatrix cm2;
2473      std::string error;
2474      std::string xml;
2475  
2476      xml =
2477          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2478          "    <hal format=\"hidl\" optional=\"false\">\n"
2479          "        <name>android.hardware.foo</name>\n"
2480          "        <version>1.2-3</version>\n"
2481          "        <interface>\n"
2482          "            <name>IFoo</name>\n"
2483          "            <instance>default</instance>\n"
2484          "        </interface>\n"
2485          "    </hal>\n"
2486          "</compatibility-matrix>\n";
2487      EXPECT_TRUE(fromXml(&cm1, xml, &error)) << error;
2488  
2489      xml =
2490          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2491          "    <hal format=\"hidl\" optional=\"false\">\n"
2492          "        <name>android.hardware.foo</name>\n"
2493          "        <version>1.0-4</version>\n"
2494          "        <interface>\n"
2495          "            <name>IFoo</name>\n"
2496          "            <instance>default</instance>\n"
2497          "        </interface>\n"
2498          "    </hal>\n"
2499          "</compatibility-matrix>\n";
2500      EXPECT_TRUE(fromXml(&cm2, xml, &error)) << error;
2501  
2502      EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2503      xml = toXml(cm1, SerializeFlags::HALS_ONLY);
2504      EXPECT_EQ(xml,
2505                "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2506                "    <hal format=\"hidl\" optional=\"false\">\n"
2507                "        <name>android.hardware.foo</name>\n"
2508                "        <version>1.0-4</version>\n"
2509                "        <interface>\n"
2510                "            <name>IFoo</name>\n"
2511                "            <instance>default</instance>\n"
2512                "        </interface>\n"
2513                "    </hal>\n"
2514                "</compatibility-matrix>\n");
2515  }
2516  
TEST_F(LibVintfTest,AddOptionalHalMajorVersion)2517  TEST_F(LibVintfTest, AddOptionalHalMajorVersion) {
2518      CompatibilityMatrix cm1;
2519      CompatibilityMatrix cm2;
2520      std::string error;
2521      std::string xml;
2522  
2523      xml =
2524          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2525          "    <hal format=\"hidl\" optional=\"false\">\n"
2526          "        <name>android.hardware.foo</name>\n"
2527          "        <version>1.2-3</version>\n"
2528          "        <interface>\n"
2529          "            <name>IFoo</name>\n"
2530          "            <instance>default</instance>\n"
2531          "        </interface>\n"
2532          "    </hal>\n"
2533          "</compatibility-matrix>\n";
2534      EXPECT_TRUE(fromXml(&cm1, xml, &error)) << error;
2535  
2536      xml =
2537          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2538          "    <hal format=\"hidl\" optional=\"false\">\n"
2539          "        <name>android.hardware.foo</name>\n"
2540          "        <version>1.2-3</version>\n"
2541          "        <version>2.0-4</version>\n"
2542          "        <interface>\n"
2543          "            <name>IFoo</name>\n"
2544          "            <instance>default</instance>\n"
2545          "        </interface>\n"
2546          "    </hal>\n"
2547          "</compatibility-matrix>\n";
2548      EXPECT_TRUE(fromXml(&cm2, xml, &error)) << error;
2549  
2550      EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2551      xml = toXml(cm1, SerializeFlags::HALS_ONLY);
2552      EXPECT_EQ(xml,
2553                "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2554                "    <hal format=\"hidl\" optional=\"false\">\n"
2555                "        <name>android.hardware.foo</name>\n"
2556                "        <version>1.2-3</version>\n"
2557                "        <version>2.0-4</version>\n"
2558                "        <interface>\n"
2559                "            <name>IFoo</name>\n"
2560                "            <instance>default</instance>\n"
2561                "        </interface>\n"
2562                "    </hal>\n"
2563                "</compatibility-matrix>\n");
2564  }
2565  
TEST_F(LibVintfTest,AddOptionalHalMinorVersionDiffInstance)2566  TEST_F(LibVintfTest, AddOptionalHalMinorVersionDiffInstance) {
2567      CompatibilityMatrix cm1;
2568      CompatibilityMatrix cm2;
2569      std::string error;
2570      std::string xml;
2571  
2572      xml =
2573          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2574          "    <hal format=\"hidl\" optional=\"false\">\n"
2575          "        <name>android.hardware.foo</name>\n"
2576          "        <version>1.0-1</version>\n"
2577          "        <interface>\n"
2578          "            <name>IFoo</name>\n"
2579          "            <instance>default</instance>\n"
2580          "        </interface>\n"
2581          "    </hal>\n"
2582          "</compatibility-matrix>\n";
2583      EXPECT_TRUE(fromXml(&cm1, xml, &error)) << error;
2584  
2585      xml =
2586          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2587          "    <hal format=\"hidl\" optional=\"false\">\n"
2588          "        <name>android.hardware.foo</name>\n"
2589          "        <version>1.1-2</version>\n"
2590          "        <interface>\n"
2591          "            <name>IFoo</name>\n"
2592          "            <instance>custom</instance>\n"
2593          "        </interface>\n"
2594          "    </hal>\n"
2595          "</compatibility-matrix>\n";
2596      EXPECT_TRUE(fromXml(&cm2, xml, &error)) << error;
2597  
2598      EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2599      xml = toXml(cm1, SerializeFlags::HALS_ONLY);
2600      EXPECT_EQ(xml,
2601                "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2602                "    <hal format=\"hidl\" optional=\"false\">\n"
2603                "        <name>android.hardware.foo</name>\n"
2604                "        <version>1.0-1</version>\n"
2605                "        <interface>\n"
2606                "            <name>IFoo</name>\n"
2607                "            <instance>default</instance>\n"
2608                "        </interface>\n"
2609                "    </hal>\n"
2610                "    <hal format=\"hidl\" optional=\"true\">\n"
2611                "        <name>android.hardware.foo</name>\n"
2612                "        <version>1.1-2</version>\n"
2613                "        <interface>\n"
2614                "            <name>IFoo</name>\n"
2615                "            <instance>custom</instance>\n"
2616                "        </interface>\n"
2617                "    </hal>\n"
2618                "</compatibility-matrix>\n");
2619  }
2620  
TEST_F(LibVintfTest,AddRequiredHalOverlapInstance)2621  TEST_F(LibVintfTest, AddRequiredHalOverlapInstance) {
2622      CompatibilityMatrix cm1;
2623      std::string error;
2624      std::string xml;
2625  
2626      xml =
2627          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2628          "    <hal format=\"hidl\" optional=\"false\">\n"
2629          "        <name>android.hardware.foo</name>\n"
2630          "        <version>1.0</version>\n"
2631          "        <interface>\n"
2632          "            <name>IFoo</name>\n"
2633          "            <instance>default</instance>\n"
2634          "            <instance>custom</instance>\n"
2635          "        </interface>\n"
2636          "    </hal>\n"
2637          "</compatibility-matrix>\n";
2638      EXPECT_TRUE(fromXml(&cm1, xml, &error)) << error;
2639  
2640      {
2641          // Test that 2.0 should be added to IFoo/default, so 1.0::IFoo/custom
2642          // should be in a new <hal> tag
2643          CompatibilityMatrix cm2;
2644          xml =
2645              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2646              "    <hal format=\"hidl\" optional=\"false\">\n"
2647              "        <name>android.hardware.foo</name>\n"
2648              "        <version>2.0</version>\n"
2649              "        <interface>\n"
2650              "            <name>IFoo</name>\n"
2651              "            <instance>default</instance>\n"
2652              "        </interface>\n"
2653              "    </hal>\n"
2654              "</compatibility-matrix>\n";
2655          EXPECT_TRUE(fromXml(&cm2, xml, &error)) << error;
2656  
2657          EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2658  
2659          xml = toXml(cm1, SerializeFlags::HALS_ONLY);
2660          EXPECT_EQ(xml,
2661                    "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2662                    "    <hal format=\"hidl\" optional=\"false\">\n"
2663                    "        <name>android.hardware.foo</name>\n"
2664                    "        <version>1.0</version>\n"
2665                    "        <interface>\n"
2666                    "            <name>IFoo</name>\n"
2667                    "            <instance>custom</instance>\n"
2668                    "        </interface>\n"
2669                    "    </hal>\n"
2670                    "    <hal format=\"hidl\" optional=\"false\">\n"
2671                    "        <name>android.hardware.foo</name>\n"
2672                    "        <version>1.0</version>\n"
2673                    "        <version>2.0</version>\n"
2674                    "        <interface>\n"
2675                    "            <name>IFoo</name>\n"
2676                    "            <instance>default</instance>\n"
2677                    "        </interface>\n"
2678                    "    </hal>\n"
2679                    "</compatibility-matrix>\n");
2680      }
2681  
2682      {
2683          // Test that 2.0::IFoo/strong should be added as an optional <hal> tag.
2684          CompatibilityMatrix cm2;
2685          xml =
2686              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2687              "    <hal format=\"hidl\" optional=\"false\">\n"
2688              "        <name>android.hardware.foo</name>\n"
2689              "        <version>2.0</version>\n"
2690              "        <interface>\n"
2691              "            <name>IFoo</name>\n"
2692              "            <instance>default</instance>\n"
2693              "            <instance>strong</instance>\n"
2694              "        </interface>\n"
2695              "    </hal>\n"
2696              "</compatibility-matrix>\n";
2697          EXPECT_TRUE(fromXml(&cm2, xml, &error)) << error;
2698  
2699          EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2700  
2701          xml = toXml(cm1, SerializeFlags::HALS_ONLY);
2702          EXPECT_EQ(xml,
2703                    "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2704                    "    <hal format=\"hidl\" optional=\"false\">\n"
2705                    "        <name>android.hardware.foo</name>\n"
2706                    "        <version>1.0</version>\n"
2707                    "        <interface>\n"
2708                    "            <name>IFoo</name>\n"
2709                    "            <instance>custom</instance>\n"
2710                    "        </interface>\n"
2711                    "    </hal>\n"
2712                    "    <hal format=\"hidl\" optional=\"false\">\n"
2713                    "        <name>android.hardware.foo</name>\n"
2714                    "        <version>1.0</version>\n"
2715                    "        <version>2.0</version>\n"
2716                    "        <interface>\n"
2717                    "            <name>IFoo</name>\n"
2718                    "            <instance>default</instance>\n"
2719                    "        </interface>\n"
2720                    "    </hal>\n"
2721                    "    <hal format=\"hidl\" optional=\"true\">\n"
2722                    "        <name>android.hardware.foo</name>\n"
2723                    "        <version>2.0</version>\n"
2724                    "        <interface>\n"
2725                    "            <name>IFoo</name>\n"
2726                    "            <instance>strong</instance>\n"
2727                    "        </interface>\n"
2728                    "    </hal>\n"
2729                    "</compatibility-matrix>\n");
2730      }
2731  }
2732  
TEST_F(LibVintfTest,AddRequiredHalOverlapInstanceSplit)2733  TEST_F(LibVintfTest, AddRequiredHalOverlapInstanceSplit) {
2734      CompatibilityMatrix cm1;
2735      CompatibilityMatrix cm2;
2736      std::string error;
2737      std::string xml;
2738  
2739      xml =
2740          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2741          "    <hal format=\"hidl\" optional=\"false\">\n"
2742          "        <name>android.hardware.foo</name>\n"
2743          "        <version>1.0</version>\n"
2744          "        <interface>\n"
2745          "            <name>IFoo</name>\n"
2746          "            <instance>default</instance>\n"
2747          "        </interface>\n"
2748          "    </hal>\n"
2749          "    <hal format=\"hidl\" optional=\"false\">\n"
2750          "        <name>android.hardware.foo</name>\n"
2751          "        <version>1.0</version>\n"
2752          "        <interface>\n"
2753          "            <name>IFoo</name>\n"
2754          "            <instance>custom</instance>\n"
2755          "        </interface>\n"
2756          "    </hal>\n"
2757          "</compatibility-matrix>\n";
2758      EXPECT_TRUE(fromXml(&cm1, xml, &error)) << error;
2759  
2760      xml =
2761          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2762          "    <hal format=\"hidl\" optional=\"false\">\n"
2763          "        <name>android.hardware.foo</name>\n"
2764          "        <version>2.0</version>\n"
2765          "        <interface>\n"
2766          "            <name>IFoo</name>\n"
2767          "            <instance>default</instance>\n"
2768          "        </interface>\n"
2769          "    </hal>\n"
2770          "    <hal format=\"hidl\" optional=\"false\">\n"
2771          "        <name>android.hardware.foo</name>\n"
2772          "        <version>2.0</version>\n"
2773          "        <interface>\n"
2774          "            <name>IFoo</name>\n"
2775          "            <instance>strong</instance>\n"
2776          "        </interface>\n"
2777          "    </hal>\n"
2778          "</compatibility-matrix>\n";
2779      EXPECT_TRUE(fromXml(&cm2, xml, &error)) << error;
2780  
2781      EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2782      xml = toXml(cm1, SerializeFlags::HALS_ONLY);
2783      EXPECT_EQ(
2784          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2785          "    <hal format=\"hidl\" optional=\"false\">\n"
2786          "        <name>android.hardware.foo</name>\n"
2787          "        <version>1.0</version>\n"
2788          "        <version>2.0</version>\n"
2789          "        <interface>\n"
2790          "            <name>IFoo</name>\n"
2791          "            <instance>default</instance>\n"
2792          "        </interface>\n"
2793          "    </hal>\n"
2794          "    <hal format=\"hidl\" optional=\"false\">\n"
2795          "        <name>android.hardware.foo</name>\n"
2796          "        <version>1.0</version>\n"
2797          "        <interface>\n"
2798          "            <name>IFoo</name>\n"
2799          "            <instance>custom</instance>\n"
2800          "        </interface>\n"
2801          "    </hal>\n"
2802          "    <hal format=\"hidl\" optional=\"true\">\n"
2803          "        <name>android.hardware.foo</name>\n"
2804          "        <version>2.0</version>\n"
2805          "        <interface>\n"
2806          "            <name>IFoo</name>\n"
2807          "            <instance>strong</instance>\n"
2808          "        </interface>\n"
2809          "    </hal>\n"
2810          "</compatibility-matrix>\n",
2811          xml);
2812  }
2813  
TEST_F(LibVintfTest,AddOptionalHalUpdatableViaApex)2814  TEST_F(LibVintfTest, AddOptionalHalUpdatableViaApex) {
2815      CompatibilityMatrix cm1;
2816      CompatibilityMatrix cm2;
2817      std::string error;
2818      std::string xml;
2819  
2820      xml =
2821          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2822          "    <hal format=\"aidl\">\n"
2823          "        <name>android.hardware.foo</name>\n"
2824          "        <interface>\n"
2825          "            <name>IFoo</name>\n"
2826          "            <instance>default</instance>\n"
2827          "        </interface>\n"
2828          "    </hal>\n"
2829          "</compatibility-matrix>\n";
2830      EXPECT_TRUE(fromXml(&cm1, xml, &error)) << error;
2831  
2832      xml =
2833          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2834          "    <hal format=\"aidl\" updatable-via-apex=\"true\">\n"
2835          "        <name>android.hardware.foo</name>\n"
2836          "        <interface>\n"
2837          "            <name>IFoo</name>\n"
2838          "            <instance>default</instance>\n"
2839          "        </interface>\n"
2840          "    </hal>\n"
2841          "</compatibility-matrix>\n";
2842      EXPECT_TRUE(fromXml(&cm2, xml, &error)) << error;
2843  
2844      EXPECT_TRUE(addAllHalsAsOptional(&cm1, &cm2, &error)) << error;
2845      xml = toXml(cm1, SerializeFlags::HALS_ONLY);
2846      EXPECT_EQ(xml,
2847                "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2848                "    <hal format=\"aidl\" optional=\"false\" updatable-via-apex=\"true\">\n"
2849                "        <name>android.hardware.foo</name>\n"
2850                "        <interface>\n"
2851                "            <name>IFoo</name>\n"
2852                "            <instance>default</instance>\n"
2853                "        </interface>\n"
2854                "    </hal>\n"
2855                "</compatibility-matrix>\n");
2856  }
2857  
TEST_F(LibVintfTest,AddOptionalXmlFile)2858  TEST_F(LibVintfTest, AddOptionalXmlFile) {
2859      CompatibilityMatrix cm1;
2860      CompatibilityMatrix cm2;
2861      std::string error;
2862      std::string xml;
2863  
2864      xml =
2865          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2866          "    <xmlfile format=\"xsd\" optional=\"true\">\n"
2867          "        <name>foo</name>\n"
2868          "        <version>1.0-2</version>\n"
2869          "        <path>/foo/bar/baz.xsd</path>\n"
2870          "    </xmlfile>\n"
2871          "</compatibility-matrix>\n";
2872      EXPECT_TRUE(fromXml(&cm1, xml, &error)) << error;
2873  
2874      xml =
2875          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"
2876          "    <xmlfile format=\"xsd\" optional=\"true\">\n"
2877          "        <name>foo</name>\n"
2878          "        <version>1.1-3</version>\n"
2879          "        <path>/foo/bar/quux.xsd</path>\n"
2880          "    </xmlfile>\n"
2881          "</compatibility-matrix>\n";
2882      EXPECT_TRUE(fromXml(&cm2, xml, &error)) << error;
2883  
2884      EXPECT_TRUE(addAllXmlFilesAsOptional(&cm1, &cm2, &error)) << error;
2885      xml = toXml(cm1, SerializeFlags::XMLFILES_ONLY);
2886      EXPECT_EQ(xml,
2887                "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
2888                "    <xmlfile format=\"xsd\" optional=\"true\">\n"
2889                "        <name>foo</name>\n"
2890                "        <version>1.0-2</version>\n"
2891                "        <path>/foo/bar/baz.xsd</path>\n"
2892                "    </xmlfile>\n"
2893                "    <xmlfile format=\"xsd\" optional=\"true\">\n"
2894                "        <name>foo</name>\n"
2895                "        <version>1.1-3</version>\n"
2896                "        <path>/foo/bar/quux.xsd</path>\n"
2897                "    </xmlfile>\n"
2898                "</compatibility-matrix>\n");
2899  }
2900  
TEST_F(LibVintfTest,VendorNdk)2901  TEST_F(LibVintfTest, VendorNdk) {
2902      CompatibilityMatrix cm;
2903      std::string error;
2904      std::string xml;
2905  
2906      xml =
2907          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
2908          "    <vendor-ndk>\n"
2909          "        <version>P</version>\n"
2910          "        <library>libbase.so</library>\n"
2911          "        <library>libjpeg.so</library>\n"
2912          "    </vendor-ndk>\n"
2913          "</compatibility-matrix>\n";
2914      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2915      EXPECT_EQ(xml, toXml(cm));
2916  
2917      EXPECT_EQ("P", cm.getVendorNdkVersion());
2918  
2919      {
2920          HalManifest manifest;
2921          xml =
2922              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2923              "    <vendor-ndk>\n"
2924              "        <version>27</version>\n"
2925              "        <library>libbase.so</library>\n"
2926              "        <library>libjpeg.so</library>\n"
2927              "    </vendor-ndk>\n"
2928              "    <vendor-ndk>\n"
2929              "        <version>P</version>\n"
2930              "        <library>libbase.so</library>\n"
2931              "        <library>libjpeg.so</library>\n"
2932              "        <library>libtinyxml2.so</library>\n"
2933              "    </vendor-ndk>\n"
2934              "</manifest>\n";
2935  
2936          EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
2937          EXPECT_EQ(xml, toXml(manifest));
2938          EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
2939      }
2940  
2941      {
2942          HalManifest manifest;
2943          xml =
2944              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2945              "    <vendor-ndk>\n"
2946              "        <version>27</version>\n"
2947              "        <library>libbase.so</library>\n"
2948              "        <library>libjpeg.so</library>\n"
2949              "    </vendor-ndk>\n"
2950              "</manifest>\n";
2951  
2952          EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
2953          EXPECT_EQ(xml, toXml(manifest));
2954          EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
2955          EXPECT_IN("Vndk version P is not supported.", error);
2956      }
2957  
2958      {
2959          HalManifest manifest;
2960          xml =
2961              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2962              "    <vendor-ndk>\n"
2963              "        <version>P</version>\n"
2964              "        <library>libbase.so</library>\n"
2965              "    </vendor-ndk>\n"
2966              "</manifest>\n";
2967  
2968          EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
2969          EXPECT_EQ(xml, toXml(manifest));
2970          EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
2971          EXPECT_IN("Vndk libs incompatible for version P.", error);
2972          EXPECT_IN("libjpeg.so", error);
2973      }
2974  }
2975  
TEST_F(LibVintfTest,MissingVendorNdkInMatrix)2976  TEST_F(LibVintfTest, MissingVendorNdkInMatrix) {
2977      CompatibilityMatrix cm;
2978      std::string xml;
2979      std::string error;
2980  
2981      xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"device\"/>\n";
2982      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
2983  
2984      {
2985          HalManifest manifest;
2986          xml = "<manifest " + kMetaVersionStr + " type=\"framework\"/>\n";
2987          EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
2988  
2989          EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
2990      }
2991  
2992      {
2993          HalManifest manifest;
2994          xml =
2995              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
2996              "    <vendor-ndk>\n"
2997              "        <version>P</version>\n"
2998              "        <library>libbase.so</library>\n"
2999              "    </vendor-ndk>\n"
3000              "</manifest>\n";
3001          EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3002  
3003          EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
3004      }
3005  }
3006  
TEST_F(LibVintfTest,DuplicatedVendorNdkVersion)3007  TEST_F(LibVintfTest, DuplicatedVendorNdkVersion) {
3008      std::string error;
3009      HalManifest manifest;
3010      std::string xml =
3011          "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3012          "    <vendor-ndk>\n"
3013          "        <version>27</version>\n"
3014          "    </vendor-ndk>\n"
3015          "    <vendor-ndk>\n"
3016          "        <version>27</version>\n"
3017          "    </vendor-ndk>\n"
3018          "</manifest>\n";
3019  
3020      EXPECT_FALSE(fromXml(&manifest, xml, &error));
3021      EXPECT_EQ("Duplicated manifest.vendor-ndk.version 27", error);
3022  }
3023  
TEST_F(LibVintfTest,ManifestHalOverride)3024  TEST_F(LibVintfTest, ManifestHalOverride) {
3025      std::string error;
3026      HalManifest manifest;
3027      std::string xml =
3028          "<manifest version=\"5.0\" type=\"device\">\n"
3029          "    <hal format=\"hidl\" override=\"true\">\n"
3030          "        <name>android.hardware.foo</name>\n"
3031          "        <transport>hwbinder</transport>\n"
3032          "        <version>1.0</version>\n"
3033          "    </hal>\n"
3034          "    <hal format=\"hidl\">\n"
3035          "        <name>android.hardware.bar</name>\n"
3036          "        <transport>hwbinder</transport>\n"
3037          "        <version>1.0</version>\n"
3038          "    </hal>\n"
3039          "</manifest>\n";
3040      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3041      const auto& foo = getHals(manifest, "android.hardware.foo");
3042      ASSERT_FALSE(foo.empty());
3043      EXPECT_TRUE(foo.front()->isOverride());
3044      const auto& bar = getHals(manifest, "android.hardware.bar");
3045      ASSERT_FALSE(bar.empty());
3046      EXPECT_FALSE(bar.front()->isOverride());
3047  }
3048  
TEST_F(LibVintfTest,ManifestHalOverrideLatest)3049  TEST_F(LibVintfTest, ManifestHalOverrideLatest) {
3050      std::string error;
3051      HalManifest manifest;
3052      std::string xml =
3053          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3054          "    <hal format=\"hidl\" override=\"true\">\n"
3055          "        <name>android.hardware.foo</name>\n"
3056          "        <transport>hwbinder</transport>\n"
3057          "        <version>1.0</version>\n"
3058          "    </hal>\n"
3059          "</manifest>\n";
3060      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3061      const auto& foo = getHals(manifest, "android.hardware.foo");
3062      ASSERT_FALSE(foo.empty());
3063      EXPECT_TRUE(foo.front()->isOverride());
3064  }
3065  
3066  // Test functionality of override="true" tag
TEST_F(LibVintfTest,ManifestAddOverrideHalSimple)3067  TEST_F(LibVintfTest, ManifestAddOverrideHalSimple) {
3068      std::string error;
3069      HalManifest manifest;
3070      std::string xml = "<manifest " + kMetaVersionStr + " type=\"device\"/>\n";
3071      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3072  
3073      HalManifest newManifest;
3074      xml =
3075          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3076          "    <hal format=\"hidl\" override=\"true\">\n"
3077          "        <name>android.hardware.foo</name>\n"
3078          "        <transport>hwbinder</transport>\n"
3079          "        <fqname>@1.1::IFoo/default</fqname>\n"
3080          "    </hal>\n"
3081          "</manifest>\n";
3082      EXPECT_TRUE(fromXml(&newManifest, xml, &error)) << error;
3083  
3084      manifest.addAllHals(&newManifest);
3085      EXPECT_EQ(xml, toXml(manifest, SerializeFlags::HALS_ONLY));
3086  }
3087  
3088  // Test functionality of override="true" tag
TEST_F(LibVintfTest,ManifestAddOverrideHalSimpleWithInterface)3089  TEST_F(LibVintfTest, ManifestAddOverrideHalSimpleWithInterface) {
3090      std::string error;
3091      HalManifest manifest;
3092      std::string xml = "<manifest " + kMetaVersionStr + " type=\"device\"/>\n";
3093      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3094  
3095      HalManifest newManifest;
3096      xml =
3097          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3098          "    <hal format=\"hidl\" override=\"true\">\n"
3099          "        <name>android.hardware.foo</name>\n"
3100          "        <transport>hwbinder</transport>\n"
3101          "        <version>1.1</version>\n"
3102          "        <interface>\n"
3103          "            <name>IFoo</name>\n"
3104          "            <instance>default</instance>\n"
3105          "        </interface>\n"
3106          "    </hal>\n"
3107          "</manifest>\n";
3108      EXPECT_TRUE(fromXml(&newManifest, xml, &error)) << error;
3109  
3110      manifest.addAllHals(&newManifest);
3111      EXPECT_EQ(
3112          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3113          "    <hal format=\"hidl\" override=\"true\">\n"
3114          "        <name>android.hardware.foo</name>\n"
3115          "        <transport>hwbinder</transport>\n"
3116          "        <fqname>@1.1::IFoo/default</fqname>\n"
3117          "    </hal>\n"
3118          "</manifest>\n",
3119          toXml(manifest, SerializeFlags::HALS_ONLY));
3120  }
3121  
TEST_F(LibVintfTest,ManifestAddOverrideHalSimpleOverride)3122  TEST_F(LibVintfTest, ManifestAddOverrideHalSimpleOverride) {
3123      std::string error;
3124      HalManifest manifest;
3125      std::string xml =
3126          "<manifest version=\"5.0\" type=\"device\">\n"
3127          "    <hal format=\"hidl\">\n"
3128          "        <name>android.hardware.foo</name>\n"
3129          "        <transport>hwbinder</transport>\n"
3130          "        <version>1.0</version>\n"
3131          "    </hal>\n"
3132          "</manifest>\n";
3133      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3134  
3135      HalManifest newManifest;
3136      xml =
3137          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3138          "    <hal format=\"hidl\" override=\"true\">\n"
3139          "        <name>android.hardware.foo</name>\n"
3140          "        <transport>hwbinder</transport>\n"
3141          "        <fqname>@1.1::IFoo/default</fqname>\n"
3142          "    </hal>\n"
3143          "</manifest>\n";
3144      EXPECT_TRUE(fromXml(&newManifest, xml, &error)) << error;
3145  
3146      manifest.addAllHals(&newManifest);
3147      EXPECT_EQ(xml, toXml(manifest, SerializeFlags::HALS_ONLY));
3148  }
3149  
TEST_F(LibVintfTest,ManifestAddOverrideHalSimpleOverrideWithInterface)3150  TEST_F(LibVintfTest, ManifestAddOverrideHalSimpleOverrideWithInterface) {
3151      std::string error;
3152      HalManifest manifest;
3153      std::string xml =
3154          "<manifest version=\"5.0\" type=\"device\">\n"
3155          "    <hal format=\"hidl\">\n"
3156          "        <name>android.hardware.foo</name>\n"
3157          "        <transport>hwbinder</transport>\n"
3158          "        <version>1.0</version>\n"
3159          "    </hal>\n"
3160          "</manifest>\n";
3161      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3162  
3163      HalManifest newManifest;
3164      xml =
3165          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3166          "    <hal format=\"hidl\" override=\"true\">\n"
3167          "        <name>android.hardware.foo</name>\n"
3168          "        <transport>hwbinder</transport>\n"
3169          "        <version>1.1</version>\n"
3170          "        <interface>\n"
3171          "            <name>IFoo</name>\n"
3172          "            <instance>default</instance>\n"
3173          "        </interface>\n"
3174          "    </hal>\n"
3175          "</manifest>\n";
3176      EXPECT_TRUE(fromXml(&newManifest, xml, &error)) << error;
3177  
3178      manifest.addAllHals(&newManifest);
3179      EXPECT_EQ(
3180          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3181          "    <hal format=\"hidl\" override=\"true\">\n"
3182          "        <name>android.hardware.foo</name>\n"
3183          "        <transport>hwbinder</transport>\n"
3184          "        <fqname>@1.1::IFoo/default</fqname>\n"
3185          "    </hal>\n"
3186          "</manifest>\n",
3187          toXml(manifest, SerializeFlags::HALS_ONLY));
3188  }
3189  
3190  // Existing major versions should be removed.
TEST_F(LibVintfTest,ManifestAddOverrideHalMultiVersion)3191  TEST_F(LibVintfTest, ManifestAddOverrideHalMultiVersion) {
3192      std::string error;
3193      HalManifest manifest;
3194      std::string xml =
3195          "<manifest version=\"5.0\" type=\"device\">\n"
3196          "    <hal format=\"hidl\">\n"
3197          "        <name>android.hardware.foo</name>\n"
3198          "        <transport>hwbinder</transport>\n"
3199          "        <version>1.3</version>\n"
3200          "        <version>2.4</version>\n"
3201          "        <interface>\n"
3202          "            <name>IFoo</name>\n"
3203          "            <instance>slot1</instance>\n"
3204          "        </interface>\n"
3205          "    </hal>\n"
3206          "    <hal format=\"hidl\">\n"
3207          "        <name>android.hardware.bar</name>\n"
3208          "        <transport>hwbinder</transport>\n"
3209          "        <version>1.3</version>\n"
3210          "    </hal>\n"
3211          "</manifest>\n";
3212      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3213  
3214      HalManifest newManifest;
3215      xml =
3216          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3217          "    <hal format=\"hidl\" override=\"true\">\n"
3218          "        <name>android.hardware.foo</name>\n"
3219          "        <transport>hwbinder</transport>\n"
3220          "        <version>1.1</version>\n"
3221          "        <version>3.1</version>\n"
3222          "        <interface>\n"
3223          "            <name>IFoo</name>\n"
3224          "            <instance>slot2</instance>\n"
3225          "        </interface>\n"
3226          "    </hal>\n"
3227          "</manifest>\n";
3228      EXPECT_TRUE(fromXml(&newManifest, xml, &error)) << error;
3229  
3230      manifest.addAllHals(&newManifest);
3231      EXPECT_EQ(
3232          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3233          "    <hal format=\"hidl\">\n"
3234          "        <name>android.hardware.bar</name>\n"
3235          "        <transport>hwbinder</transport>\n"
3236          "        <version>1.3</version>\n"
3237          "    </hal>\n"
3238          "    <hal format=\"hidl\">\n"
3239          "        <name>android.hardware.foo</name>\n"
3240          "        <transport>hwbinder</transport>\n"
3241          "        <fqname>@2.4::IFoo/slot1</fqname>\n"
3242          "    </hal>\n"
3243          "    <hal format=\"hidl\" override=\"true\">\n"
3244          "        <name>android.hardware.foo</name>\n"
3245          "        <transport>hwbinder</transport>\n"
3246          "        <fqname>@1.1::IFoo/slot2</fqname>\n"
3247          "        <fqname>@3.1::IFoo/slot2</fqname>\n"
3248          "    </hal>\n"
3249          "</manifest>\n",
3250          toXml(manifest, SerializeFlags::HALS_ONLY));
3251  }
3252  
TEST_F(LibVintfTest,ManifestAddOverrideHalMultiVersion2)3253  TEST_F(LibVintfTest, ManifestAddOverrideHalMultiVersion2) {
3254      std::string error;
3255      HalManifest manifest;
3256      std::string xml =
3257          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3258          "    <hal format=\"hidl\">\n"
3259          "        <name>android.hardware.foo</name>\n"
3260          "        <transport>hwbinder</transport>\n"
3261          "        <version>1.3</version>\n"
3262          "        <version>2.4</version>\n"
3263          "        <interface>\n"
3264          "            <name>IFoo</name>\n"
3265          "            <instance>slot1</instance>\n"
3266          "        </interface>\n"
3267          "    </hal>\n"
3268          "</manifest>\n";
3269      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3270  
3271      HalManifest newManifest;
3272      xml =
3273          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3274          "    <hal format=\"hidl\" override=\"true\">\n"
3275          "        <name>android.hardware.foo</name>\n"
3276          "        <transport>hwbinder</transport>\n"
3277          "        <fqname>@1.1::IFoo/slot2</fqname>\n"
3278          "        <fqname>@2.1::IFoo/slot2</fqname>\n"
3279          "    </hal>\n"
3280          "</manifest>\n";
3281  
3282      EXPECT_TRUE(fromXml(&newManifest, xml, &error)) << error;
3283  
3284      manifest.addAllHals(&newManifest);
3285      EXPECT_EQ(xml, toXml(manifest, SerializeFlags::HALS_ONLY));
3286  }
3287  
TEST_F(LibVintfTest,ManifestAddOverrideHalMultiVersion2WithInterface)3288  TEST_F(LibVintfTest, ManifestAddOverrideHalMultiVersion2WithInterface) {
3289      std::string error;
3290      HalManifest manifest;
3291      std::string xml =
3292          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3293          "    <hal format=\"hidl\">\n"
3294          "        <name>android.hardware.foo</name>\n"
3295          "        <transport>hwbinder</transport>\n"
3296          "        <version>1.3</version>\n"
3297          "        <version>2.4</version>\n"
3298          "        <interface>\n"
3299          "            <name>IFoo</name>\n"
3300          "            <instance>slot1</instance>\n"
3301          "        </interface>\n"
3302          "    </hal>\n"
3303          "</manifest>\n";
3304      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3305  
3306      HalManifest newManifest;
3307      xml =
3308          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3309          "    <hal format=\"hidl\" override=\"true\">\n"
3310          "        <name>android.hardware.foo</name>\n"
3311          "        <transport>hwbinder</transport>\n"
3312          "        <version>1.1</version>\n"
3313          "        <version>2.1</version>\n"
3314          "        <interface>\n"
3315          "            <name>IFoo</name>\n"
3316          "            <instance>slot2</instance>\n"
3317          "        </interface>\n"
3318          "    </hal>\n"
3319          "</manifest>\n";
3320  
3321      EXPECT_TRUE(fromXml(&newManifest, xml, &error)) << error;
3322  
3323      manifest.addAllHals(&newManifest);
3324      EXPECT_EQ(
3325          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3326          "    <hal format=\"hidl\" override=\"true\">\n"
3327          "        <name>android.hardware.foo</name>\n"
3328          "        <transport>hwbinder</transport>\n"
3329          "        <fqname>@1.1::IFoo/slot2</fqname>\n"
3330          "        <fqname>@2.1::IFoo/slot2</fqname>\n"
3331          "    </hal>\n"
3332          "</manifest>\n",
3333          toXml(manifest, SerializeFlags::HALS_ONLY));
3334  }
3335  
3336  // if no <versions>, remove all existing <hal> with given <name>.
TEST_F(LibVintfTest,ManifestAddOverrideHalRemoveAll)3337  TEST_F(LibVintfTest, ManifestAddOverrideHalRemoveAll) {
3338      std::string error;
3339      HalManifest manifest;
3340      std::string xml =
3341          "<manifest version=\"5.0\" type=\"device\">\n"
3342          "    <hal format=\"hidl\">\n"
3343          "        <name>android.hardware.foo</name>\n"
3344          "        <transport>hwbinder</transport>\n"
3345          "        <version>1.3</version>\n"
3346          "        <version>2.4</version>\n"
3347          "        <interface>\n"
3348          "            <name>IFoo</name>\n"
3349          "            <instance>slot1</instance>\n"
3350          "        </interface>\n"
3351          "    </hal>\n"
3352          "    <hal format=\"hidl\">\n"
3353          "        <name>android.hardware.foo</name>\n"
3354          "        <transport>hwbinder</transport>\n"
3355          "        <version>3.1</version>\n"
3356          "        <version>4.3</version>\n"
3357          "        <interface>\n"
3358          "            <name>IBar</name>\n"
3359          "            <instance>slot2</instance>\n"
3360          "        </interface>\n"
3361          "    </hal>\n"
3362          "    <hal format=\"hidl\">\n"
3363          "        <name>android.hardware.bar</name>\n"
3364          "        <transport>hwbinder</transport>\n"
3365          "        <version>1.3</version>\n"
3366          "    </hal>\n"
3367          "</manifest>\n";
3368      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3369  
3370      HalManifest newManifest;
3371      xml =
3372          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3373          "    <hal format=\"hidl\" override=\"true\">\n"
3374          "        <name>android.hardware.foo</name>\n"
3375          "        <transport>hwbinder</transport>\n"
3376          "    </hal>\n"
3377          "</manifest>\n";
3378  
3379      EXPECT_TRUE(fromXml(&newManifest, xml, &error)) << error;
3380  
3381      manifest.addAllHals(&newManifest);
3382      EXPECT_EQ(
3383          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3384          "    <hal format=\"hidl\">\n"
3385          "        <name>android.hardware.bar</name>\n"
3386          "        <transport>hwbinder</transport>\n"
3387          "        <version>1.3</version>\n"
3388          "    </hal>\n"
3389          "    <hal format=\"hidl\" override=\"true\">\n"
3390          "        <name>android.hardware.foo</name>\n"
3391          "        <transport>hwbinder</transport>\n"
3392          "    </hal>\n"
3393          "</manifest>\n",
3394          toXml(manifest, SerializeFlags::HALS_ONLY));
3395  }
3396  
3397  // Make sure missing tags in old VINTF files does not cause incompatibilities.
TEST_F(LibVintfTest,Empty)3398  TEST_F(LibVintfTest, Empty) {
3399      CompatibilityMatrix cm;
3400      HalManifest manifest;
3401      std::string xml;
3402      std::string error;
3403  
3404      xml = "<compatibility-matrix " + kMetaVersionStr + " type=\"device\"/>\n";
3405      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
3406  
3407      xml = "<manifest " + kMetaVersionStr + " type=\"framework\"/>\n";
3408      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3409  
3410      EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
3411  }
3412  
TEST_F(LibVintfTest,ParsingUpdatableHals)3413  TEST_F(LibVintfTest, ParsingUpdatableHals) {
3414      std::string error;
3415  
3416      HalManifest manifest;
3417      std::string manifestXml =
3418          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3419          "    <hal format=\"aidl\" updatable-via-apex=\"com.android.foo\">\n"
3420          "        <name>android.hardware.foo</name>\n"
3421          "        <fqname>IFoo/default</fqname>\n"
3422          "    </hal>\n"
3423          "</manifest>\n";
3424      EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
3425      EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
3426  
3427      // check by calling the API: updatableViaApex()
3428      auto foo = getHals(manifest, "android.hardware.foo");
3429      ASSERT_EQ(1u, foo.size());
3430      EXPECT_THAT(foo.front()->updatableViaApex(), Optional(Eq("com.android.foo")));
3431  }
3432  
TEST_F(LibVintfTest,ParsingUpdatableViaApex_EmptyIsValidForNonUpdatableHal)3433  TEST_F(LibVintfTest, ParsingUpdatableViaApex_EmptyIsValidForNonUpdatableHal) {
3434      std::string error;
3435  
3436      HalManifest manifest;
3437      manifest.setFileName("/apex/com.foo/etc/vintf/manifest.xml");
3438      std::string manifestXml =
3439          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3440          "    <hal format=\"aidl\" updatable-via-apex=\"\">\n"
3441          "        <name>android.hardware.foo</name>\n"
3442          "        <fqname>IFoo/default</fqname>\n"
3443          "    </hal>\n"
3444          "</manifest>\n";
3445      EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
3446      EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
3447  
3448      // check by calling the API: updatableViaApex()
3449      auto foo = getHals(manifest, "android.hardware.foo");
3450      ASSERT_EQ(1u, foo.size());
3451      EXPECT_THAT(foo.front()->updatableViaApex(), Optional(Eq("")));
3452  }
3453  
TEST_F(LibVintfTest,ParsingUpdatableViaApex_UpdatableHalCanExplicitlySet)3454  TEST_F(LibVintfTest, ParsingUpdatableViaApex_UpdatableHalCanExplicitlySet) {
3455      std::string error;
3456  
3457      HalManifest manifest;
3458      manifest.setFileName("/apex/com.foo/etc/vintf/manifest.xml");
3459      std::string manifestXml =
3460          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3461          "    <hal format=\"aidl\" updatable-via-apex=\"com.foo\">\n"
3462          "        <name>android.hardware.foo</name>\n"
3463          "        <fqname>IFoo/default</fqname>\n"
3464          "    </hal>\n"
3465          "</manifest>\n";
3466      EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
3467      EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
3468  
3469      // check by calling the API: updatableViaApex()
3470      auto foo = getHals(manifest, "android.hardware.foo");
3471      ASSERT_EQ(1u, foo.size());
3472      EXPECT_THAT(foo.front()->updatableViaApex(), Optional(Eq("com.foo")));
3473  }
3474  
TEST_F(LibVintfTest,ParsingUpdatableViaApex_ErrorIfExplicitValueMismatch)3475  TEST_F(LibVintfTest, ParsingUpdatableViaApex_ErrorIfExplicitValueMismatch) {
3476      std::string error;
3477  
3478      HalManifest manifest;
3479      manifest.setFileName("/apex/com.bar/etc/vintf/manifest.xml");
3480      std::string manifestXml =
3481          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3482          "    <hal format=\"aidl\" updatable-via-apex=\"com.foo\">\n"
3483          "        <name>android.hardware.foo</name>\n"
3484          "        <fqname>IFoo/default</fqname>\n"
3485          "    </hal>\n"
3486          "</manifest>\n";
3487      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error));
3488      EXPECT_IN("updatable-via-apex com.foo doesn't match", error);
3489  }
3490  
TEST_F(LibVintfTest,ParsingUpdatableViaApex_SetToCurrentApex)3491  TEST_F(LibVintfTest, ParsingUpdatableViaApex_SetToCurrentApex) {
3492      std::string error;
3493  
3494      HalManifest manifest;
3495      manifest.setFileName("/apex/com.foo/etc/vintf/manifest.xml");
3496      std::string manifestXml =
3497          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3498          "    <hal format=\"aidl\">\n"
3499          "        <name>android.hardware.foo</name>\n"
3500          "        <fqname>IFoo/default</fqname>\n"
3501          "    </hal>\n"
3502          "</manifest>\n";
3503      EXPECT_TRUE(fromXml(&manifest, manifestXml, &error));
3504      EXPECT_IN("updatable-via-apex=\"com.foo\"", toXml(manifest, SerializeFlags::HALS_ONLY));
3505  
3506      // check by calling the API: updatableViaApex()
3507      auto foo = getHals(manifest, "android.hardware.foo");
3508      ASSERT_EQ(1u, foo.size());
3509      EXPECT_THAT(foo.front()->updatableViaApex(), Optional(Eq("com.foo")));
3510  }
3511  
TEST_F(LibVintfTest,ParsingUpdatableHalsWithInterface)3512  TEST_F(LibVintfTest, ParsingUpdatableHalsWithInterface) {
3513      std::string error;
3514  
3515      HalManifest manifest;
3516      std::string manifestXml =
3517          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3518          "    <hal format=\"aidl\" updatable-via-apex=\"com.android.foo\">\n"
3519          "        <name>android.hardware.foo</name>\n"
3520          "        <interface>\n"
3521          "            <name>IFoo</name>\n"
3522          "            <instance>default</instance>\n"
3523          "        </interface>\n"
3524          "    </hal>\n"
3525          "</manifest>\n";
3526      EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
3527      EXPECT_EQ(
3528          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3529          "    <hal format=\"aidl\" updatable-via-apex=\"com.android.foo\">\n"
3530          "        <name>android.hardware.foo</name>\n"
3531          "        <fqname>IFoo/default</fqname>\n"
3532          "    </hal>\n"
3533          "</manifest>\n",
3534          toXml(manifest, SerializeFlags::HALS_ONLY));
3535  
3536      // check by calling the API: updatableViaApex()
3537      auto foo = getHals(manifest, "android.hardware.foo");
3538      ASSERT_EQ(1u, foo.size());
3539      EXPECT_THAT(foo.front()->updatableViaApex(), Optional(Eq("com.android.foo")));
3540  }
3541  
TEST_F(LibVintfTest,ParsingHalsInetTransport)3542  TEST_F(LibVintfTest, ParsingHalsInetTransport) {
3543      std::string error;
3544  
3545      HalManifest manifest;
3546      std::string manifestXml =
3547          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3548          "    <hal format=\"aidl\">\n"
3549          "        <name>android.hardware.foo</name>\n"
3550          "        <transport ip=\"1.2.3.4\" port=\"12\">inet</transport>\n"
3551          "        <fqname>IFoo/default</fqname>\n"
3552          "    </hal>\n"
3553          "</manifest>\n";
3554      EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
3555      EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
3556  
3557      auto foo = getHals(manifest, "android.hardware.foo");
3558      ASSERT_EQ(1u, foo.size());
3559      ASSERT_TRUE(foo.front()->ip().has_value());
3560      ASSERT_TRUE(foo.front()->port().has_value());
3561      EXPECT_EQ("1.2.3.4", *foo.front()->ip());
3562      EXPECT_EQ(12, *foo.front()->port());
3563  }
3564  
TEST_F(LibVintfTest,ParsingHalsInetTransportWithInterface)3565  TEST_F(LibVintfTest, ParsingHalsInetTransportWithInterface) {
3566      std::string error;
3567  
3568      HalManifest manifest;
3569      std::string manifestXml =
3570          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3571          "    <hal format=\"aidl\">\n"
3572          "        <name>android.hardware.foo</name>\n"
3573          "        <transport ip=\"1.2.3.4\" port=\"12\">inet</transport>\n"
3574          "        <interface>\n"
3575          "            <name>IFoo</name>\n"
3576          "            <instance>default</instance>\n"
3577          "        </interface>\n"
3578          "    </hal>\n"
3579          "</manifest>\n";
3580      EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
3581      EXPECT_EQ(
3582          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3583          "    <hal format=\"aidl\">\n"
3584          "        <name>android.hardware.foo</name>\n"
3585          "        <transport ip=\"1.2.3.4\" port=\"12\">inet</transport>\n"
3586          "        <fqname>IFoo/default</fqname>\n"
3587          "    </hal>\n"
3588          "</manifest>\n",
3589          toXml(manifest, SerializeFlags::HALS_ONLY));
3590  
3591      auto foo = getHals(manifest, "android.hardware.foo");
3592      ASSERT_EQ(1u, foo.size());
3593      ASSERT_TRUE(foo.front()->ip().has_value());
3594      ASSERT_TRUE(foo.front()->port().has_value());
3595      EXPECT_EQ("1.2.3.4", *foo.front()->ip());
3596      EXPECT_EQ(12, *foo.front()->port());
3597  }
3598  
TEST_F(LibVintfTest,RejectHalsInetTransportNoAttrs)3599  TEST_F(LibVintfTest, RejectHalsInetTransportNoAttrs) {
3600      std::string error;
3601  
3602      HalManifest manifest;
3603      std::string manifestXml =
3604          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3605          "    <hal format=\"aidl\">\n"
3606          "        <name>android.hardware.foo</name>\n"
3607          "        <transport>inet</transport>\n"
3608          "        <interface>\n"
3609          "            <name>IFoo</name>\n"
3610          "            <instance>default</instance>\n"
3611          "        </interface>\n"
3612          "    </hal>\n"
3613          "</manifest>\n";
3614      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error));
3615      EXPECT_IN("Transport inet requires ip and port attributes", error);
3616  }
3617  
TEST_F(LibVintfTest,RejectHalsInetTransportMissingAttrs)3618  TEST_F(LibVintfTest, RejectHalsInetTransportMissingAttrs) {
3619      std::string error;
3620  
3621      HalManifest manifest;
3622      std::string manifestXml =
3623          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3624          "    <hal format=\"aidl\">\n"
3625          "        <name>android.hardware.foo</name>\n"
3626          "        <transport ip=\"1.2.3.4\">inet</transport>\n"
3627          "        <interface>\n"
3628          "            <name>IFoo</name>\n"
3629          "            <instance>default</instance>\n"
3630          "        </interface>\n"
3631          "    </hal>\n"
3632          "</manifest>\n";
3633      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error));
3634      EXPECT_IN("Transport inet requires ip and port", error);
3635  }
3636  
TEST_F(LibVintfTest,RejectHalsEmptyTransportWithInetAttrs)3637  TEST_F(LibVintfTest, RejectHalsEmptyTransportWithInetAttrs) {
3638      std::string error;
3639  
3640      HalManifest manifest;
3641      std::string manifestXml =
3642          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3643          "    <hal format=\"aidl\">\n"
3644          "        <name>android.hardware.foo</name>\n"
3645          "        <transport ip=\"1.2.3.4\" port=\"12\"></transport>\n"
3646          "        <interface>\n"
3647          "            <name>IFoo</name>\n"
3648          "            <instance>default</instance>\n"
3649          "        </interface>\n"
3650          "    </hal>\n"
3651          "</manifest>\n";
3652      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error));
3653      EXPECT_IN("Transport  requires empty ip and port attributes", error);
3654  }
3655  
TEST_F(LibVintfTest,RejectHidlHalsInetTransport)3656  TEST_F(LibVintfTest, RejectHidlHalsInetTransport) {
3657      std::string error;
3658  
3659      HalManifest manifest;
3660      std::string manifestXml =
3661          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3662          "    <hal format=\"hidl\">\n"
3663          "        <name>android.hardware.foo</name>\n"
3664          "        <transport ip=\"1.2.3.4\" port=\"12\">inet</transport>\n"
3665          "        <interface>\n"
3666          "            <name>IFoo</name>\n"
3667          "            <instance>default</instance>\n"
3668          "        </interface>\n"
3669          "    </hal>\n"
3670          "</manifest>\n";
3671      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error));
3672      EXPECT_IN(
3673              "HIDL HAL 'android.hardware.foo' should not have <transport> \"inet\" or ip or port",
3674              error);
3675  }
3676  
TEST_F(LibVintfTest,RejectHidlHalsHwbinderInetAttrs)3677  TEST_F(LibVintfTest, RejectHidlHalsHwbinderInetAttrs) {
3678      std::string error;
3679  
3680      HalManifest manifest;
3681      std::string manifestXml =
3682          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
3683          "    <hal format=\"hidl\">\n"
3684          "        <name>android.hardware.foo</name>\n"
3685          "        <transport ip=\"1.2.3.4\" port=\"12\">hwbinder</transport>\n"
3686          "        <interface>\n"
3687          "            <name>IFoo</name>\n"
3688          "            <instance>default</instance>\n"
3689          "        </interface>\n"
3690          "    </hal>\n"
3691          "</manifest>\n";
3692      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error));
3693      EXPECT_IN("Transport hwbinder requires empty ip and port attributes", error);
3694  }
3695  
TEST_F(LibVintfTest,SystemSdk)3696  TEST_F(LibVintfTest, SystemSdk) {
3697      CompatibilityMatrix cm;
3698      std::string xml;
3699      std::string error;
3700  
3701      xml =
3702          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
3703          "    <system-sdk>\n"
3704          "        <version>1</version>\n"
3705          "        <version>P</version>\n"
3706          "    </system-sdk>\n"
3707          "</compatibility-matrix>\n";
3708      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
3709      EXPECT_EQ(xml, toXml(cm, SerializeFlags::SSDK_ONLY));
3710  
3711      {
3712          HalManifest manifest;
3713          xml =
3714              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3715              "    <system-sdk>\n"
3716              "        <version>1</version>\n"
3717              "        <version>P</version>\n"
3718              "    </system-sdk>\n"
3719              "</manifest>\n";
3720          EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3721          EXPECT_EQ(xml, toXml(manifest, SerializeFlags::SSDK_ONLY));
3722  
3723          EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
3724      }
3725  
3726      {
3727          HalManifest manifest;
3728          xml =
3729              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3730              "    <system-sdk>\n"
3731              "        <version>1</version>\n"
3732              "        <version>3</version>\n"
3733              "        <version>P</version>\n"
3734              "    </system-sdk>\n"
3735              "</manifest>\n";
3736          EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3737          EXPECT_TRUE(manifest.checkCompatibility(cm, &error));
3738      }
3739  
3740      {
3741          HalManifest manifest;
3742          xml =
3743              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3744              "    <system-sdk>\n"
3745              "        <version>1</version>\n"
3746              "    </system-sdk>\n"
3747              "</manifest>\n";
3748          EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
3749          EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
3750          EXPECT_TRUE(error.find("System SDK") != std::string::npos) << error;
3751      }
3752  }
3753  
TEST_F(LibVintfTest,ManifestEmpty)3754  TEST_F(LibVintfTest, ManifestEmpty) {
3755      std::string error;
3756      HalManifest e;
3757      EXPECT_FALSE(fromXml(&e, "<manifest/>", &error));
3758      EXPECT_NE("Not a valid XML", error);
3759  
3760      std::string prevError = error;
3761      EXPECT_FALSE(fromXml(&e, "", &error));
3762      EXPECT_EQ("Not a valid XML", error);
3763  }
3764  
TEST_F(LibVintfTest,MatrixEmpty)3765  TEST_F(LibVintfTest, MatrixEmpty) {
3766      std::string error;
3767      CompatibilityMatrix e;
3768      EXPECT_FALSE(fromXml(&e, "<compatibility-matrix/>", &error));
3769      EXPECT_NE("Not a valid XML", error);
3770  
3771      std::string prevError = error;
3772      EXPECT_FALSE(fromXml(&e, "", &error));
3773      EXPECT_EQ("Not a valid XML", error);
3774  }
3775  
TEST_F(LibVintfTest,MatrixDetailErrorMsg)3776  TEST_F(LibVintfTest, MatrixDetailErrorMsg) {
3777      std::string error;
3778      std::string xml;
3779  
3780      HalManifest manifest;
3781      xml =
3782          "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"103\">\n"
3783          "    <hal format=\"hidl\">\n"
3784          "        <name>android.hardware.foo</name>\n"
3785          "        <transport>hwbinder</transport>\n"
3786          "        <version>1.0</version>\n"
3787          "        <interface>\n"
3788          "            <name>IFoo</name>\n"
3789          "            <instance>default</instance>\n"
3790          "        </interface>\n"
3791          "    </hal>\n"
3792          "</manifest>\n";
3793      ASSERT_TRUE(fromXml(&manifest, xml, &error)) << error;
3794  
3795      {
3796          CompatibilityMatrix cm;
3797          xml =
3798              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"100\">\n"
3799              "    <hal format=\"hidl\" optional=\"false\">\n"
3800              "        <name>android.hardware.foo</name>\n"
3801              "        <version>1.2-3</version>\n"
3802              "        <version>4.5</version>\n"
3803              "        <interface>\n"
3804              "            <name>IFoo</name>\n"
3805              "            <instance>default</instance>\n"
3806              "            <instance>slot1</instance>\n"
3807              "        </interface>\n"
3808              "        <interface>\n"
3809              "            <name>IBar</name>\n"
3810              "            <instance>default</instance>\n"
3811              "        </interface>\n"
3812              "    </hal>\n"
3813              "</compatibility-matrix>\n";
3814          EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
3815          EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
3816          EXPECT_IN("Manifest level = 103", error);
3817          EXPECT_IN("Matrix level = 100", error);
3818          EXPECT_IN(
3819              "android.hardware.foo:\n"
3820              "    required: \n"
3821              "        (@1.2-3::IBar/default AND @1.2-3::IFoo/default AND @1.2-3::IFoo/slot1) OR\n"
3822              "        (@4.5::IBar/default AND @4.5::IFoo/default AND @4.5::IFoo/slot1)\n"
3823              "    provided: @1.0::IFoo/default",
3824              error);
3825      }
3826  
3827      {
3828          CompatibilityMatrix cm;
3829          xml =
3830              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
3831              "    <hal format=\"hidl\" optional=\"false\">\n"
3832              "        <name>android.hardware.foo</name>\n"
3833              "        <version>1.2-3</version>\n"
3834              "        <interface>\n"
3835              "            <name>IFoo</name>\n"
3836              "            <instance>default</instance>\n"
3837              "            <instance>slot1</instance>\n"
3838              "        </interface>\n"
3839              "    </hal>\n"
3840              "</compatibility-matrix>\n";
3841          EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
3842          EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
3843          EXPECT_IN(
3844              "android.hardware.foo:\n"
3845              "    required: (@1.2-3::IFoo/default AND @1.2-3::IFoo/slot1)\n"
3846              "    provided: @1.0::IFoo/default",
3847              error);
3848      }
3849  
3850      // the most frequent use case.
3851      {
3852          CompatibilityMatrix cm;
3853          xml =
3854              "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
3855              "    <hal format=\"hidl\" optional=\"false\">\n"
3856              "        <name>android.hardware.foo</name>\n"
3857              "        <version>1.2-3</version>\n"
3858              "        <interface>\n"
3859              "            <name>IFoo</name>\n"
3860              "            <instance>default</instance>\n"
3861              "        </interface>\n"
3862              "    </hal>\n"
3863              "</compatibility-matrix>\n";
3864          EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
3865          EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
3866          EXPECT_IN(
3867              "android.hardware.foo:\n"
3868              "    required: @1.2-3::IFoo/default\n"
3869              "    provided: @1.0::IFoo/default",
3870              error);
3871      }
3872  }
3873  
TEST_F(LibVintfTest,DisabledHal)3874  TEST_F(LibVintfTest, DisabledHal) {
3875      std::string error;
3876      std::string xml;
3877      HalManifest manifest;
3878      xml =
3879          "<manifest version=\"5.0\" type=\"framework\">\n"
3880          "    <hal format=\"hidl\" override=\"true\">\n"
3881          "        <transport>hwbinder</transport>\n"
3882          "        <name>android.hardware.foo</name>\n"
3883          "        <transport>hwbinder</transport>\n"
3884          "    </hal>\n"
3885          "    <hal format=\"hidl\" override=\"true\">\n"
3886          "        <name>android.hardware.bar</name>\n"
3887          "        <transport>hwbinder</transport>\n"
3888          "        <fqname>@1.1::IFoo/custom</fqname>\n"
3889          "    </hal>\n"
3890          "    <hal format=\"hidl\">\n"
3891          "        <name>android.hardware.baz</name>\n"
3892          "        <transport>hwbinder</transport>\n"
3893          "    </hal>\n"
3894          "</manifest>\n";
3895      ASSERT_TRUE(fromXml(&manifest, xml, &error)) << error;
3896  
3897      auto foo = getHals(manifest, "android.hardware.foo");
3898      ASSERT_EQ(1u, foo.size());
3899      EXPECT_TRUE(foo.front()->isDisabledHal());
3900      auto bar = getHals(manifest, "android.hardware.bar");
3901      ASSERT_EQ(1u, bar.size());
3902      EXPECT_FALSE(bar.front()->isDisabledHal());
3903      auto baz = getHals(manifest, "android.hardware.baz");
3904      ASSERT_EQ(1u, baz.size());
3905      EXPECT_FALSE(baz.front()->isDisabledHal());
3906  }
3907  
TEST_F(LibVintfTest,FqNameValid)3908  TEST_F(LibVintfTest, FqNameValid) {
3909      std::string error;
3910      std::string xml;
3911  
3912      CompatibilityMatrix cm;
3913      xml =
3914          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
3915          "    <hal format=\"hidl\" optional=\"false\">\n"
3916          "        <name>android.hardware.foo</name>\n"
3917          "        <version>1.0</version>\n"
3918          "        <interface>\n"
3919          "            <name>IFoo</name>\n"
3920          "            <instance>default</instance>\n"
3921          "        </interface>\n"
3922          "    </hal>\n"
3923          "    <hal format=\"hidl\" optional=\"false\">\n"
3924          "        <name>android.hardware.foo</name>\n"
3925          "        <version>1.1</version>\n"
3926          "        <interface>\n"
3927          "            <name>IFoo</name>\n"
3928          "            <instance>custom</instance>\n"
3929          "        </interface>\n"
3930          "    </hal>\n"
3931          "</compatibility-matrix>\n";
3932      EXPECT_TRUE(fromXml(&cm, xml, &error)) << error;
3933  
3934      {
3935          HalManifest manifest;
3936          xml =
3937              "<manifest version=\"5.0\" type=\"framework\">\n"
3938              "    <hal format=\"hidl\">\n"
3939              "        <name>android.hardware.foo</name>\n"
3940              "        <transport>hwbinder</transport>\n"
3941              "        <version>1.0</version>\n"
3942              "        <interface>\n"
3943              "            <name>IFoo</name>\n"
3944              "            <instance>default</instance>\n"
3945              "            <instance>custom</instance>\n"
3946              "        </interface>\n"
3947              "        <fqname>@1.1::IFoo/custom</fqname>\n"
3948              "    </hal>\n"
3949              "</manifest>\n";
3950          ASSERT_TRUE(fromXml(&manifest, xml, &error)) << error;
3951          EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
3952  
3953          EXPECT_EQ(Transport::HWBINDER,
3954                    manifest.getHidlTransport("android.hardware.foo", {1, 1}, "IFoo", "custom"));
3955      }
3956  
3957      {
3958          HalManifest manifest;
3959          xml =
3960              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3961              "    <hal format=\"hidl\">\n"
3962              "        <name>android.hardware.foo</name>\n"
3963              "        <transport>hwbinder</transport>\n"
3964              "        <fqname>@1.0::IFoo/default</fqname>\n"
3965              "        <fqname>@1.1::IFoo/custom</fqname>\n"
3966              "    </hal>\n"
3967              "</manifest>\n";
3968          ASSERT_TRUE(fromXml(&manifest, xml, &error)) << error;
3969          EXPECT_TRUE(manifest.checkCompatibility(cm, &error)) << error;
3970      }
3971  
3972      {
3973          HalManifest manifest;
3974          xml =
3975              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
3976              "    <hal format=\"hidl\">\n"
3977              "        <name>android.hardware.foo</name>\n"
3978              "        <transport>hwbinder</transport>\n"
3979              "        <version>1.0</version>\n"
3980              "        <interface>\n"
3981              "            <name>IFoo</name>\n"
3982              "            <instance>default</instance>\n"
3983              "            <instance>custom</instance>\n"
3984              "        </interface>\n"
3985              "    </hal>\n"
3986              "</manifest>\n";
3987          ASSERT_TRUE(fromXml(&manifest, xml, &error)) << error;
3988          EXPECT_FALSE(manifest.checkCompatibility(cm, &error));
3989          EXPECT_IN(
3990              "android.hardware.foo:\n"
3991              "    required: @1.1::IFoo/custom\n"
3992              "    provided: \n"
3993              "        @1.0::IFoo/custom\n"
3994              "        @1.0::IFoo/default",
3995              error);
3996      }
3997  
3998      {
3999          HalManifest manifest;
4000          xml =
4001              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4002              "    <hal format=\"hidl\">\n"
4003              "        <name>android.hardware.foo</name>\n"
4004              "        <transport>hwbinder</transport>\n"
4005              "        <fqname>@1.0::IFoo/default</fqname>\n"
4006              "        <fqname>@1.0::IFoo/custom</fqname>\n"
4007              "    </hal>\n"
4008              "</manifest>\n";
4009          ASSERT_TRUE(fromXml(&manifest, xml, &error)) << error;
4010      }
4011  }
4012  
TEST_F(LibVintfTest,FqNameInvalid)4013  TEST_F(LibVintfTest, FqNameInvalid) {
4014      std::string error;
4015      std::string xml;
4016      {
4017          ManifestHal hal;
4018          xml =
4019              "<hal format=\"hidl\">\n"
4020              "    <name>android.hardware.foo</name>\n"
4021              "    <transport>hwbinder</transport>\n"
4022              "    <fqname>@1.1::IFoo/custom</fqname>\n"
4023              "</hal>\n";
4024          EXPECT_TRUE(fromXml(&hal, xml, &error)) << error;
4025      }
4026      ManifestHal hal;
4027      xml =
4028          "<hal format=\"hidl\">\n"
4029          "    <name>android.hardware.foo</name>\n"
4030          "    <transport>hwbinder</transport>\n"
4031          "    <fqname>1.1::IFoo/custom</fqname>\n"
4032          "</hal>\n";
4033      ASSERT_FALSE(fromXml(&hal, xml, &error));
4034      EXPECT_IN("Could not parse text \"1.1::IFoo/custom\" in element <fqname>", error);
4035      xml =
4036          "<hal format=\"hidl\">\n"
4037          "    <name>android.hardware.foo</name>\n"
4038          "    <transport>hwbinder</transport>\n"
4039          "    <fqname>android.hardware.foo@1.1::IFoo/custom</fqname>\n"
4040          "</hal>\n";
4041      ASSERT_FALSE(fromXml(&hal, xml, &error));
4042      EXPECT_IN("Should not specify package", error);
4043      xml =
4044          "<hal format=\"hidl\">\n"
4045          "    <name>android.hardware.foo</name>\n"
4046          "    <transport>hwbinder</transport>\n"
4047          "    <fqname>IFoo/custom</fqname>\n"
4048          "</hal>\n";
4049      ASSERT_FALSE(fromXml(&hal, xml, &error));
4050      EXPECT_IN("Should specify version", error);
4051      xml =
4052          "<hal format=\"hidl\">\n"
4053          "    <name>android.hardware.foo</name>\n"
4054          "    <transport>hwbinder</transport>\n"
4055          "    <fqname>@1.0::IFoo</fqname>\n"
4056          "</hal>\n";
4057      ASSERT_FALSE(fromXml(&hal, xml, &error));
4058      EXPECT_IN("Could not parse text \"@1.0::IFoo\" in element <fqname>", error);
4059      xml =
4060          "<hal format=\"hidl\">\n"
4061          "    <name>n07 4 v4l1d 1n73rf4c3</name>\n"
4062          "    <transport>hwbinder</transport>\n"
4063          "    <fqname>@1.0::IFoo/custom</fqname>\n"
4064          "</hal>\n";
4065      ASSERT_FALSE(fromXml(&hal, xml, &error));
4066      EXPECT_IN("Cannot create FqInstance", error);
4067      EXPECT_IN("n07 4 v4l1d 1n73rf4c3", error);
4068  }
4069  
TEST_F(LibVintfTest,RegexInstanceValid)4070  TEST_F(LibVintfTest, RegexInstanceValid) {
4071      CompatibilityMatrix matrix;
4072      std::string error;
4073  
4074      std::string xml =
4075          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
4076          "    <hal format=\"hidl\" optional=\"false\">\n"
4077          "        <name>android.hardware.foo</name>\n"
4078          "        <version>1.0</version>\n"
4079          "        <interface>\n"
4080          "            <name>IFoo</name>\n"
4081          "            <regex-instance>legacy/[0-9]+</regex-instance>\n"
4082          "            <regex-instance>slot[0-9]+</regex-instance>\n"
4083          "            <regex-instance>.*</regex-instance>\n"
4084          "        </interface>\n"
4085          "    </hal>\n"
4086          "</compatibility-matrix>\n";
4087      EXPECT_TRUE(fromXml(&matrix, xml, &error)) << error;
4088  }
4089  
TEST_F(LibVintfTest,RegexInstanceInvalid)4090  TEST_F(LibVintfTest, RegexInstanceInvalid) {
4091      CompatibilityMatrix matrix;
4092      std::string error;
4093      std::string xml =
4094          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
4095          "    <hal format=\"hidl\" optional=\"false\">\n"
4096          "        <name>android.hardware.foo</name>\n"
4097          "        <version>1.0</version>\n"
4098          "        <interface>\n"
4099          "            <name>IFoo</name>\n"
4100          "            <regex-instance>e{1,2,3}</regex-instance>\n"
4101          "            <regex-instance>*</regex-instance>\n"
4102          "            <regex-instance>+</regex-instance>\n"
4103          "            <regex-instance>[0-9]+</regex-instance>\n"
4104          "            <regex-instance>[0-9]+</regex-instance>\n"
4105          "        </interface>\n"
4106          "    </hal>\n"
4107          "</compatibility-matrix>\n";
4108      EXPECT_FALSE(fromXml(&matrix, xml, &error));
4109      EXPECT_IN("Invalid regular expression 'e{1,2,3}'", error);
4110      EXPECT_IN("Invalid regular expression '*'", error);
4111      EXPECT_IN("Invalid regular expression '+'", error);
4112      EXPECT_IN("Duplicated regex-instance '[0-9]+'", error);
4113  }
4114  
TEST_F(LibVintfTest,RegexInstanceCompat)4115  TEST_F(LibVintfTest, RegexInstanceCompat) {
4116      CompatibilityMatrix matrix;
4117      std::string error;
4118  
4119      std::string matrixXml =
4120          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\">\n"
4121          "    <hal format=\"hidl\" optional=\"false\">\n"
4122          "        <name>android.hardware.foo</name>\n"
4123          "        <version>1.0</version>\n"
4124          "        <version>3.1-2</version>\n"
4125          "        <interface>\n"
4126          "            <name>IFoo</name>\n"
4127          "            <instance>default</instance>\n"
4128          "            <regex-instance>legacy/[0-9]+</regex-instance>\n"
4129          "        </interface>\n"
4130          "    </hal>\n"
4131          "    <sepolicy>\n"
4132          "        <kernel-sepolicy-version>0</kernel-sepolicy-version>\n"
4133          "        <sepolicy-version>0.0</sepolicy-version>\n"
4134          "    </sepolicy>\n"
4135          "</compatibility-matrix>\n";
4136      EXPECT_TRUE(fromXml(&matrix, matrixXml, &error)) << error;
4137  
4138      {
4139          std::string xml =
4140              "<manifest " + kMetaVersionStr + " type=\"device\">\n"
4141              "    <hal format=\"hidl\">\n"
4142              "        <name>android.hardware.foo</name>\n"
4143              "        <transport>hwbinder</transport>\n"
4144              "        <version>1.0</version>\n"
4145              "        <interface>\n"
4146              "            <name>IFoo</name>\n"
4147              "            <instance>default</instance>\n"
4148              "            <instance>legacy/0</instance>\n"
4149              "            <instance>legacy/1</instance>\n"
4150              "        </interface>\n"
4151              "    </hal>\n"
4152              "</manifest>\n";
4153  
4154          HalManifest manifest;
4155          EXPECT_TRUE(fromXml(&manifest, xml));
4156          EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
4157  
4158          auto unused = checkUnusedHals(manifest, matrix);
4159          EXPECT_TRUE(unused.empty())
4160              << "Contains unused HALs: " << android::base::Join(unused, "\n");
4161      }
4162  
4163      {
4164          std::string xml =
4165              "<manifest " + kMetaVersionStr + " type=\"device\">\n"
4166              "    <hal format=\"hidl\">\n"
4167              "        <name>android.hardware.foo</name>\n"
4168              "        <transport>hwbinder</transport>\n"
4169              "        <version>1.0</version>\n"
4170              "        <interface>\n"
4171              "            <name>IFoo</name>\n"
4172              "            <instance>default</instance>\n"
4173              "            <instance>legacy0</instance>\n"
4174              "            <instance>nonmatch/legacy/0</instance>\n"
4175              "            <instance>legacy/0/nonmatch</instance>\n"
4176              "        </interface>\n"
4177              "    </hal>\n"
4178              "</manifest>\n";
4179  
4180          HalManifest manifest;
4181          EXPECT_TRUE(fromXml(&manifest, xml));
4182          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
4183              << "Should not be compatible because no legacy/[0-9]+ is provided.";
4184  
4185          auto unused = checkUnusedHals(manifest, matrix);
4186          EXPECT_EQ((std::set<std::string>{"android.hardware.foo@1.0::IFoo/nonmatch/legacy/0",
4187                                           "android.hardware.foo@1.0::IFoo/legacy/0/nonmatch",
4188                                           "android.hardware.foo@1.0::IFoo/legacy0"}),
4189                    unused);
4190      }
4191  }
4192  
TEST_F(LibVintfTest,Regex)4193  TEST_F(LibVintfTest, Regex) {
4194      details::Regex regex;
4195  
4196      EXPECT_FALSE(regex.compile("+"));
4197      EXPECT_FALSE(regex.compile("*"));
4198  
4199      ASSERT_TRUE(regex.compile("legacy/[0-9]+"));
4200      EXPECT_TRUE(regex.matches("legacy/0"));
4201      EXPECT_TRUE(regex.matches("legacy/000"));
4202      EXPECT_FALSE(regex.matches("legacy/"));
4203      EXPECT_FALSE(regex.matches("ssslegacy/0"));
4204      EXPECT_FALSE(regex.matches("legacy/0sss"));
4205  }
4206  
TEST_F(LibVintfTest,ManifestGetHalNamesAndVersions)4207  TEST_F(LibVintfTest, ManifestGetHalNamesAndVersions) {
4208      HalManifest vm = testDeviceManifest();
4209      EXPECT_EQ(vm.getHalNamesAndVersions(),
4210                std::set<std::string>({"android.hardware.camera@2.0", "android.hardware.nfc@1.0"}));
4211  }
4212  
TEST_F(LibVintfTest,KernelInfo)4213  TEST_F(LibVintfTest, KernelInfo) {
4214      KernelInfo ki = testKernelInfo();
4215  
4216      EXPECT_EQ(
4217          "<kernel version=\"3.18.31\">\n"
4218          "    <config>\n"
4219          "        <key>CONFIG_64BIT</key>\n"
4220          "        <value>y</value>\n"
4221          "    </config>\n"
4222          "    <config>\n"
4223          "        <key>CONFIG_ANDROID_BINDER_DEVICES</key>\n"
4224          "        <value>\"binder,hwbinder\"</value>\n"
4225          "    </config>\n"
4226          "    <config>\n"
4227          "        <key>CONFIG_ARCH_MMAP_RND_BITS</key>\n"
4228          "        <value>24</value>\n"
4229          "    </config>\n"
4230          "    <config>\n"
4231          "        <key>CONFIG_BUILD_ARM64_APPENDED_DTB_IMAGE_NAMES</key>\n"
4232          "        <value>\"\"</value>\n"
4233          "    </config>\n"
4234          "    <config>\n"
4235          "        <key>CONFIG_ILLEGAL_POINTER_VALUE</key>\n"
4236          "        <value>0xdead000000000000</value>\n"
4237          "    </config>\n"
4238          "</kernel>\n",
4239          toXml(ki, SerializeFlags::NO_TAGS.enableKernelConfigs()));
4240  }
4241  
TEST_F(LibVintfTest,ManifestAddAllDeviceManifest)4242  TEST_F(LibVintfTest, ManifestAddAllDeviceManifest) {
4243      std::string xml1 = "<manifest " + kMetaVersionStr + " type=\"device\" />\n";
4244      std::string xml2 =
4245          "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"3\">\n"
4246          "    <hal format=\"hidl\">\n"
4247          "        <name>android.hardware.foo</name>\n"
4248          "        <transport>hwbinder</transport>\n"
4249          "        <fqname>@1.0::IFoo/default</fqname>\n"
4250          "    </hal>\n"
4251          "    <sepolicy>\n"
4252          "        <version>25.5</version>\n"
4253          "    </sepolicy>\n"
4254          "    <kernel version=\"3.18.31\">\n"
4255          "        <config>\n"
4256          "            <key>CONFIG_64BIT</key>\n"
4257          "            <value>y</value>\n"
4258          "        </config>\n"
4259          "    </kernel>\n"
4260          "    <xmlfile>\n"
4261          "        <name>media_profile</name>\n"
4262          "        <version>1.0</version>\n"
4263          "    </xmlfile>\n"
4264          "</manifest>\n";
4265  
4266      std::string error;
4267      HalManifest manifest1;
4268      ASSERT_TRUE(fromXml(&manifest1, xml1, &error)) << error;
4269      HalManifest manifest2;
4270      ASSERT_TRUE(fromXml(&manifest2, xml2, &error)) << error;
4271  
4272      ASSERT_TRUE(manifest1.addAll(&manifest2, &error)) << error;
4273  
4274      EXPECT_EQ(xml2, toXml(manifest1));
4275  }
4276  
TEST_F(LibVintfTest,ManifestAddAllFrameworkManifest)4277  TEST_F(LibVintfTest, ManifestAddAllFrameworkManifest) {
4278      std::string xml1 = "<manifest " + kMetaVersionStr + " type=\"framework\" />\n";
4279      std::string xml2 =
4280          "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4281          "    <hal format=\"hidl\">\n"
4282          "        <name>android.hardware.foo</name>\n"
4283          "        <transport>hwbinder</transport>\n"
4284          "        <fqname>@1.0::IFoo/default</fqname>\n"
4285          "    </hal>\n"
4286          "    <vendor-ndk>\n"
4287          "        <version>P</version>\n"
4288          "        <library>libbase.so</library>\n"
4289          "    </vendor-ndk>\n"
4290          "    <system-sdk>\n"
4291          "        <version>1</version>\n"
4292          "    </system-sdk>\n"
4293          "    <xmlfile>\n"
4294          "        <name>media_profile</name>\n"
4295          "        <version>1.0</version>\n"
4296          "    </xmlfile>\n"
4297          "</manifest>\n";
4298  
4299      std::string error;
4300      HalManifest manifest1;
4301      ASSERT_TRUE(fromXml(&manifest1, xml1, &error)) << error;
4302      HalManifest manifest2;
4303      ASSERT_TRUE(fromXml(&manifest2, xml2, &error)) << error;
4304  
4305      ASSERT_TRUE(manifest1.addAll(&manifest2, &error)) << error;
4306  
4307      EXPECT_EQ(xml2, toXml(manifest1));
4308  }
4309  
TEST_F(LibVintfTest,ManifestAddAllConflictMajorVersion)4310  TEST_F(LibVintfTest, ManifestAddAllConflictMajorVersion) {
4311      std::string head =
4312              "<manifest " + kMetaVersionStr + " type=\"device\">\n"
4313              "    <hal format=\"hidl\">\n"
4314              "        <name>android.hardware.foo</name>\n"
4315              "        <transport>hwbinder</transport>\n"
4316              "        <version>";
4317      std::string tail =
4318              "</version>\n"
4319              "        <interface>\n"
4320              "            <name>IFoo</name>\n"
4321              "            <instance>default</instance>\n"
4322              "        </interface>\n"
4323              "    </hal>\n"
4324              "</manifest>\n";
4325  
4326      std::string xml1 = head + "1.0" + tail;
4327      std::string xml2 = head + "1.1" + tail;
4328  
4329      std::string error;
4330      HalManifest manifest1;
4331      manifest1.setFileName("1.xml");
4332      ASSERT_TRUE(fromXml(&manifest1, xml1, &error)) << error;
4333      HalManifest manifest2;
4334      manifest2.setFileName("2.xml");
4335      ASSERT_TRUE(fromXml(&manifest2, xml2, &error)) << error;
4336  
4337      ASSERT_FALSE(manifest1.addAll(&manifest2, &error));
4338  
4339      EXPECT_IN("android.hardware.foo", error);
4340      EXPECT_IN("@1.0::IFoo/default (from 1.xml)", error);
4341      EXPECT_IN("@1.1::IFoo/default (from 2.xml)", error);
4342  }
4343  
TEST_F(LibVintfTest,ManifestAddAllConflictLevel)4344  TEST_F(LibVintfTest, ManifestAddAllConflictLevel) {
4345      std::string xml1 = "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"2\" />\n";
4346      std::string xml2 = "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"3\" />\n";
4347  
4348      std::string error;
4349      HalManifest manifest1;
4350      ASSERT_TRUE(fromXml(&manifest1, xml1, &error)) << error;
4351      HalManifest manifest2;
4352      ASSERT_TRUE(fromXml(&manifest2, xml2, &error)) << error;
4353  
4354      ASSERT_FALSE(manifest1.addAll(&manifest2, &error));
4355      EXPECT_IN("Conflicting target-level", error);
4356  }
4357  
TEST_F(LibVintfTest,ManifestAddAllConflictSepolicy)4358  TEST_F(LibVintfTest, ManifestAddAllConflictSepolicy) {
4359      std::string xml1 =
4360          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
4361          "    <sepolicy>\n"
4362          "        <version>25.5</version>\n"
4363          "    </sepolicy>\n"
4364          "</manifest>\n";
4365      std::string xml2 =
4366          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
4367          "    <sepolicy>\n"
4368          "        <version>30.0</version>\n"
4369          "    </sepolicy>\n"
4370          "</manifest>\n";
4371  
4372      std::string error;
4373      HalManifest manifest1;
4374      ASSERT_TRUE(fromXml(&manifest1, xml1, &error)) << error;
4375      HalManifest manifest2;
4376      ASSERT_TRUE(fromXml(&manifest2, xml2, &error)) << error;
4377  
4378      ASSERT_FALSE(manifest1.addAll(&manifest2, &error));
4379      EXPECT_IN("Conflicting sepolicy version", error);
4380  }
4381  
TEST_F(LibVintfTest,ManifestAddAllConflictKernel)4382  TEST_F(LibVintfTest, ManifestAddAllConflictKernel) {
4383      std::string xml1 =
4384          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
4385          "    <kernel version=\"3.18.0\" />\n"
4386          "</manifest>\n";
4387      std::string xml2 =
4388          "<manifest " + kMetaVersionStr + " type=\"device\">\n"
4389          "    <kernel version=\"3.18.1\" />\n"
4390          "</manifest>\n";
4391  
4392      std::string error;
4393      HalManifest manifest1;
4394      ASSERT_TRUE(fromXml(&manifest1, xml1, &error)) << error;
4395      HalManifest manifest2;
4396      ASSERT_TRUE(fromXml(&manifest2, xml2, &error)) << error;
4397  
4398      ASSERT_FALSE(manifest1.addAll(&manifest2, &error));
4399      EXPECT_IN("Conflicting kernel", error);
4400  }
4401  
TEST_F(LibVintfTest,ManifestMetaVersionCompat)4402  TEST_F(LibVintfTest, ManifestMetaVersionCompat) {
4403      std::string xml = "<manifest version=\"2.0\" type=\"device\" />";
4404      std::string error;
4405      HalManifest manifest;
4406      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
4407  }
4408  
TEST_F(LibVintfTest,ManifestMetaVersionIncompat)4409  TEST_F(LibVintfTest, ManifestMetaVersionIncompat) {
4410      std::string xml = "<manifest version=\"10000.0\" type=\"device\" />";
4411      std::string error;
4412      HalManifest manifest;
4413      EXPECT_FALSE(fromXml(&manifest, xml, &error))
4414          << "Should not parse metaversion 10000.0";
4415  }
4416  
TEST_F(LibVintfTest,ManifestMetaVersionWriteLatest)4417  TEST_F(LibVintfTest, ManifestMetaVersionWriteLatest) {
4418      std::string xml = "<manifest version=\"1.0\" type=\"device\" />";
4419      std::string error;
4420      HalManifest manifest;
4421      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
4422      EXPECT_IN(kMetaVersionStr, toXml(manifest, SerializeFlags::NO_TAGS));
4423  }
4424  
TEST_F(LibVintfTest,MatrixMetaVersionCompat)4425  TEST_F(LibVintfTest, MatrixMetaVersionCompat) {
4426      std::string xml = "<compatibility-matrix version=\"2.0\" type=\"framework\" />";
4427      std::string error;
4428      CompatibilityMatrix matrix;
4429      EXPECT_TRUE(fromXml(&matrix, xml, &error)) << error;
4430  }
4431  
TEST_F(LibVintfTest,MatrixMetaVersionIncompat)4432  TEST_F(LibVintfTest, MatrixMetaVersionIncompat) {
4433      std::string xml = "<compatibility-matrix version=\"10000.0\" type=\"framework\" />";
4434      std::string error;
4435      CompatibilityMatrix matrix;
4436      EXPECT_FALSE(fromXml(&matrix, xml, &error))
4437          << "Should not parse metaversion 10000.0";
4438  }
4439  
TEST_F(LibVintfTest,MatrixMetaVersionWriteLatest)4440  TEST_F(LibVintfTest, MatrixMetaVersionWriteLatest) {
4441      std::string xml = "<compatibility-matrix version=\"1.0\" type=\"framework\" />";
4442      std::string error;
4443      CompatibilityMatrix matrix;
4444      EXPECT_TRUE(fromXml(&matrix, xml, &error)) << error;
4445      EXPECT_IN(kMetaVersionStr, toXml(matrix, SerializeFlags::NO_TAGS));
4446  }
4447  
4448  // clang-format on
4449  
4450  struct InMemoryFileSystem : FileSystem {
4451      std::map<std::string, std::string> files;
InMemoryFileSystemandroid::vintf::InMemoryFileSystem4452      InMemoryFileSystem(std::map<std::string, std::string> files) : files(std::move(files)) {}
fetchandroid::vintf::InMemoryFileSystem4453      status_t fetch(const std::string& path, std::string* fetched,
4454                     std::string* error) const override {
4455          (void)error;
4456          if (auto it = files.find(path); it != files.end()) {
4457              *fetched = it->second;
4458              return OK;
4459          }
4460          return NAME_NOT_FOUND;
4461      }
listFilesandroid::vintf::InMemoryFileSystem4462      status_t listFiles(const std::string& path, std::vector<std::string>* out,
4463                         std::string* error) const override {
4464          (void)error;
4465          std::set<std::string> entries;
4466          for (const auto& pair : files) {
4467              std::string_view entry{pair.first};
4468              if (android::base::ConsumePrefix(&entry, path)) {
4469                  android::base::ConsumePrefix(&entry, "/");
4470                  entries.emplace(entry.substr(0, entry.find('/')));
4471              }
4472          }
4473          *out = std::vector<std::string>{begin(entries), end(entries)};
4474          return OK;
4475      }
modifiedTimeandroid::vintf::InMemoryFileSystem4476      status_t modifiedTime(const std::string& path, int64_t* mtime, std::string* error) const {
4477          (void)error;
4478          if (auto it = files.find(path); it != files.end()) {
4479              *mtime = 0;
4480              return OK;
4481          }
4482          return NAME_NOT_FOUND;
4483      }
4484  };
4485  
TEST_F(LibVintfTest,HalManifestWithMultipleFiles)4486  TEST_F(LibVintfTest, HalManifestWithMultipleFiles) {
4487      std::string vendorXmlPath = "/vendor/etc/vintf/manifest.xml";
4488      std::string vendorXml = "<manifest " + kMetaVersionStr +
4489                              " type=\"device\">\n"
4490                              "    <hal format=\"aidl\">\n"
4491                              "        <name>android.hardware.foo</name>\n"
4492                              "        <fqname>IFoo/default</fqname>\n"
4493                              "    </hal>\n"
4494                              "</manifest>";
4495      std::string apexXmlPath = "/apex/com.android.bar/etc/vintf/manifest.xml";
4496      std::string apexXml = "<manifest " + kMetaVersionStr +
4497                            " type=\"device\">\n"
4498                            "    <hal format=\"aidl\">\n"
4499                            "        <name>android.hardware.bar</name>\n"
4500                            "        <fqname>IBar/default</fqname>\n"
4501                            "    </hal>\n"
4502                            "</manifest>";
4503      InMemoryFileSystem files{{
4504          {vendorXmlPath, vendorXml},
4505          {apexXmlPath, apexXml},
4506      }};
4507      // Read apexXml later. This shouldn't affect the result except HalManifest::fileName.
4508      {
4509          std::string error;
4510          HalManifest manifest;
4511          EXPECT_EQ(OK, fetchManifest(manifest, &files, vendorXmlPath, &error)) << error;
4512          EXPECT_EQ(OK, fetchManifest(manifest, &files, apexXmlPath, &error)) << error;
4513          EXPECT_EQ(vendorXmlPath + ":" + apexXmlPath, manifest.fileName());
4514          EXPECT_EQ(std::nullopt, getAnyHal(manifest, "android.hardware.foo")->updatableViaApex());
4515          EXPECT_EQ(std::make_optional("com.android.bar"s),
4516                    getAnyHal(manifest, "android.hardware.bar")->updatableViaApex());
4517      }
4518      // Read apexXml first. This shouldn't affect the result except HalManifest::fileName.
4519      {
4520          std::string error;
4521          HalManifest manifest;
4522          EXPECT_EQ(OK, fetchManifest(manifest, &files, apexXmlPath, &error)) << error;
4523          EXPECT_EQ(OK, fetchManifest(manifest, &files, vendorXmlPath, &error)) << error;
4524          EXPECT_EQ(apexXmlPath + ":" + vendorXmlPath, manifest.fileName());
4525          EXPECT_EQ(std::nullopt, getAnyHal(manifest, "android.hardware.foo")->updatableViaApex());
4526          EXPECT_EQ(std::make_optional("com.android.bar"s),
4527                    getAnyHal(manifest, "android.hardware.bar")->updatableViaApex());
4528      }
4529  }
4530  
4531  // clang-format off
4532  
TEST_F(LibVintfTest,Aidl)4533  TEST_F(LibVintfTest, Aidl) {
4534      std::string xml =
4535          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
4536          "    <hal format=\"aidl\" optional=\"false\">\n"
4537          "        <name>android.system.foo</name>\n"
4538          "        <interface>\n"
4539          "            <name>IFoo</name>\n"
4540          "            <instance>default</instance>\n"
4541          "            <regex-instance>test.*</regex-instance>\n"
4542          "        </interface>\n"
4543          "    </hal>\n"
4544          "</compatibility-matrix>\n";
4545      std::string error;
4546      CompatibilityMatrix matrix;
4547      EXPECT_TRUE(fromXml(&matrix, xml, &error)) << error;
4548      EXPECT_EQ(xml, toXml(matrix, SerializeFlags::HALS_NO_FQNAME));
4549  
4550      {
4551          HalManifest manifest;
4552          std::string manifestXml =
4553              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4554              "    <hal format=\"aidl\">\n"
4555              "        <name>android.system.foo</name>\n"
4556              "        <interface>\n"
4557              "            <name>IFoo</name>\n"
4558              "            <instance>default</instance>\n"
4559              "            <instance>test0</instance>\n"
4560              "        </interface>\n"
4561              "    </hal>\n"
4562              "</manifest>\n";
4563          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4564          EXPECT_EQ(
4565              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4566              "    <hal format=\"aidl\">\n"
4567              "        <name>android.system.foo</name>\n"
4568              "        <fqname>IFoo/default</fqname>\n"
4569              "        <fqname>IFoo/test0</fqname>\n"
4570              "    </hal>\n"
4571              "</manifest>\n",
4572              toXml(manifest, SerializeFlags::HALS_ONLY));
4573          EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
4574          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "default"));
4575          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "test0"));
4576          EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IFoo", "does_not_exist"));
4577          EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IDoesNotExist", "default"));
4578          EXPECT_FALSE(manifest.hasAidlInstance("android.system.does_not_exist", "IFoo", "default"));
4579          EXPECT_EQ(manifest.getAidlInstances("android.system.foo", "IFoo"),
4580                    std::set<std::string>({"default", "test0"}));
4581          EXPECT_EQ(manifest.getAidlInstances("android.system.does_not_exist", "IFoo"),
4582                    std::set<std::string>({}));
4583      }
4584  
4585      {
4586          HalManifest manifest;
4587          std::string manifestXml =
4588              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4589              "    <hal format=\"aidl\">\n"
4590              "        <name>android.system.foo</name>\n"
4591              "        <fqname>IFoo/default</fqname>\n"
4592              "        <fqname>IFoo/test0</fqname>\n"
4593              "    </hal>\n"
4594              "</manifest>\n";
4595          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4596          EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
4597          EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
4598          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "default"));
4599          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "test0"));
4600          EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IFoo", "does_not_exist"));
4601          EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IDoesNotExist", "default"));
4602          EXPECT_FALSE(manifest.hasAidlInstance("android.system.does_not_exist", "IFoo", "default"));
4603          EXPECT_EQ(manifest.getAidlInstances("android.system.foo", "IFoo"),
4604                    std::set<std::string>({"default", "test0"}));
4605          EXPECT_EQ(manifest.getAidlInstances("android.system.does_not_exist", "IFoo"),
4606                    std::set<std::string>({}));
4607      }
4608  
4609      {
4610          HalManifest manifest;
4611          std::string manifestXml =
4612              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4613              "    <hal format=\"aidl\">\n"
4614              "        <name>android.system.foo</name>\n"
4615              "        <fqname>IFoo/incompat_instance</fqname>\n"
4616              "        <fqname>IFoo/test0</fqname>\n"
4617              "    </hal>\n"
4618              "</manifest>\n";
4619          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4620          EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
4621          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
4622              << "Should not be compatible because default instance is missing";
4623          EXPECT_IN("required: (IFoo/default (@1) AND IFoo/test.* (@1))", error);
4624          EXPECT_IN("provided: \n"
4625                    "        IFoo/incompat_instance (@1)\n"
4626                    "        IFoo/test0 (@1)",
4627                    error);
4628      }
4629  
4630      {
4631          HalManifest manifest;
4632          std::string manifestXml =
4633              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4634              "    <hal format=\"aidl\">\n"
4635              "        <name>android.system.foo</name>\n"
4636              "        <interface>\n"
4637              "            <name>IFoo</name>\n"
4638              "            <instance>incompat_instance</instance>\n"
4639              "            <instance>test0</instance>\n"
4640              "        </interface>\n"
4641              "    </hal>\n"
4642              "</manifest>\n";
4643          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4644          EXPECT_EQ(
4645              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4646              "    <hal format=\"aidl\">\n"
4647              "        <name>android.system.foo</name>\n"
4648              "        <fqname>IFoo/incompat_instance</fqname>\n"
4649              "        <fqname>IFoo/test0</fqname>\n"
4650              "    </hal>\n"
4651              "</manifest>\n",
4652              toXml(manifest, SerializeFlags::HALS_ONLY));
4653          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
4654              << "Should not be compatible because default instance is missing";
4655          EXPECT_IN("required: (IFoo/default (@1) AND IFoo/test.* (@1))", error);
4656          EXPECT_IN("provided: \n"
4657                    "        IFoo/incompat_instance (@1)\n"
4658                    "        IFoo/test0 (@1)",
4659                    error);
4660      }
4661  
4662      {
4663          HalManifest manifest;
4664          std::string manifestXml =
4665              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4666              "    <hal format=\"aidl\">\n"
4667              "        <name>android.system.foo</name>\n"
4668              "        <fqname>IFoo/default</fqname>\n"
4669              "        <fqname>IFoo/incompat_instance</fqname>\n"
4670              "    </hal>\n"
4671              "</manifest>\n";
4672          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4673          EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
4674          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
4675              << "Should not be compatible because test.* instance is missing";
4676          EXPECT_IN("required: (IFoo/default (@1) AND IFoo/test.* (@1))", error);
4677          EXPECT_IN("provided: \n"
4678                    "        IFoo/default (@1)\n"
4679                    "        IFoo/incompat_instance (@1)\n",
4680                    error);
4681      }
4682  
4683      {
4684          HalManifest manifest;
4685          std::string manifestXml =
4686              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4687              "    <hal format=\"aidl\">\n"
4688              "        <name>android.system.foo</name>\n"
4689              "        <interface>\n"
4690              "            <name>IFoo</name>\n"
4691              "            <instance>default</instance>\n"
4692              "            <instance>incompat_instance</instance>\n"
4693              "        </interface>\n"
4694              "    </hal>\n"
4695              "</manifest>\n";
4696          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4697          EXPECT_EQ(
4698              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4699              "    <hal format=\"aidl\">\n"
4700              "        <name>android.system.foo</name>\n"
4701              "        <fqname>IFoo/default</fqname>\n"
4702              "        <fqname>IFoo/incompat_instance</fqname>\n"
4703              "    </hal>\n"
4704              "</manifest>\n",
4705              toXml(manifest, SerializeFlags::HALS_ONLY));
4706          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
4707              << "Should not be compatible because test.* instance is missing";
4708          EXPECT_IN("required: (IFoo/default (@1) AND IFoo/test.* (@1))", error);
4709          EXPECT_IN("provided: \n"
4710                    "        IFoo/default (@1)\n"
4711                    "        IFoo/incompat_instance (@1)\n",
4712                    error);
4713      }
4714  }
4715  
TEST_F(LibVintfTest,AidlAndHidlNamesMatrix)4716  TEST_F(LibVintfTest, AidlAndHidlNamesMatrix) {
4717      std::string xml =
4718          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
4719          "    <hal format=\"aidl\" optional=\"true\">\n"
4720          "        <name>android.system.foo</name>\n"
4721          "        <interface>\n"
4722          "            <name>IFoo</name>\n"
4723          "            <instance>default</instance>\n"
4724          "        </interface>\n"
4725          "    </hal>\n"
4726          "    <hal format=\"hidl\" optional=\"true\">\n"
4727          "        <name>android.system.foo</name>\n"
4728          "        <version>1.0</version>\n"
4729          "        <interface>\n"
4730          "            <name>IFoo</name>\n"
4731          "            <instance>default</instance>\n"
4732          "        </interface>\n"
4733          "    </hal>\n"
4734          "</compatibility-matrix>\n";
4735      std::string error;
4736      CompatibilityMatrix matrix;
4737      EXPECT_TRUE(fromXml(&matrix, xml, &error)) << error;
4738      EXPECT_EQ(xml, toXml(matrix, SerializeFlags::HALS_ONLY));
4739  }
4740  
TEST_F(LibVintfTest,AidlAndHidlNamesManifest)4741  TEST_F(LibVintfTest, AidlAndHidlNamesManifest) {
4742      std::string xml =
4743          "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4744          "    <hal format=\"aidl\">\n"
4745          "        <name>android.system.foo</name>\n"
4746          "        <fqname>IFoo/default</fqname>\n"
4747          "    </hal>\n"
4748          "    <hal format=\"hidl\">\n"
4749          "        <name>android.system.foo</name>\n"
4750          "        <transport>hwbinder</transport>\n"
4751          "        <fqname>@1.0::IFoo/default</fqname>\n"
4752          "    </hal>\n"
4753          "</manifest>\n";
4754      std::string error;
4755      HalManifest manifest;
4756      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
4757      EXPECT_EQ(xml, toXml(manifest, SerializeFlags::HALS_ONLY));
4758  }
4759  
TEST_F(LibVintfTest,AidlAndHidlCheckUnused)4760  TEST_F(LibVintfTest, AidlAndHidlCheckUnused) {
4761      std::string manifestXml =
4762          "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4763          "    <hal format=\"aidl\">\n"
4764          "        <name>android.system.foo</name>\n"
4765          "        <fqname>IFoo/default</fqname>\n"
4766          "    </hal>\n"
4767          "    <hal format=\"hidl\">\n"
4768          "        <name>android.system.foo</name>\n"
4769          "        <transport>hwbinder</transport>\n"
4770          "        <fqname>@1.0::IFoo/default</fqname>\n"
4771          "    </hal>\n"
4772          "</manifest>\n";
4773      std::string matrixXml =
4774          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
4775          "    <hal format=\"aidl\" optional=\"true\">\n"
4776          "        <name>android.system.foo</name>\n"
4777          "        <interface>\n"
4778          "            <name>IFoo</name>\n"
4779          "            <instance>default</instance>\n"
4780          "        </interface>\n"
4781          "    </hal>\n"
4782          "    <hal format=\"hidl\" optional=\"true\">\n"
4783          "        <name>android.system.foo</name>\n"
4784          "        <version>1.0</version>\n"
4785          "        <interface>\n"
4786          "            <name>IFoo</name>\n"
4787          "            <instance>default</instance>\n"
4788          "        </interface>\n"
4789          "    </hal>\n"
4790          "</compatibility-matrix>\n";
4791      std::string error;
4792      HalManifest manifest;
4793      CompatibilityMatrix matrix;
4794  
4795      EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4796      EXPECT_TRUE(fromXml(&matrix, matrixXml, &error)) << error;
4797      auto unused = checkUnusedHals(manifest, matrix);
4798      EXPECT_TRUE(unused.empty()) << android::base::Join(unused, "\n");
4799  }
4800  
TEST_F(LibVintfTest,AidlVersion)4801  TEST_F(LibVintfTest, AidlVersion) {
4802      std::string xml =
4803          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
4804          "    <hal format=\"aidl\" optional=\"false\">\n"
4805          "        <name>android.system.foo</name>\n"
4806          "        <version>4-100</version>\n"
4807          "        <interface>\n"
4808          "            <name>IFoo</name>\n"
4809          "            <instance>default</instance>\n"
4810          "            <regex-instance>test.*</regex-instance>\n"
4811          "        </interface>\n"
4812          "    </hal>\n"
4813          "</compatibility-matrix>\n";
4814      std::string error;
4815      CompatibilityMatrix matrix;
4816      EXPECT_TRUE(fromXml(&matrix, xml, &error)) << error;
4817      EXPECT_EQ(xml, toXml(matrix, SerializeFlags::HALS_NO_FQNAME));
4818  
4819      {
4820          std::vector<std::string> matrixInstances;
4821          (void)matrix.forEachInstance([&](const MatrixInstance& matrixInstance) {
4822              EXPECT_EQ(matrixInstance.versionRange(),
4823                        VersionRange(details::kFakeAidlMajorVersion, 4, 100));
4824              matrixInstances.push_back(matrixInstance.description(
4825                  matrixInstance.versionRange().minVer()));
4826              return true;
4827          });
4828          EXPECT_THAT(matrixInstances, SizeIs(2)) << android::base::Join(matrixInstances, ", ");
4829      }
4830  
4831      {
4832          HalManifest manifest;
4833          std::string manifestXml =
4834              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4835              "    <hal format=\"aidl\">\n"
4836              "        <name>android.system.foo</name>\n"
4837              "        <version>5</version>\n"
4838              "        <interface>\n"
4839              "            <name>IFoo</name>\n"
4840              "            <instance>default</instance>\n"
4841              "            <instance>test0</instance>\n"
4842              "        </interface>\n"
4843              "    </hal>\n"
4844              "</manifest>\n";
4845          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4846          EXPECT_EQ(
4847              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4848              "    <hal format=\"aidl\">\n"
4849              "        <name>android.system.foo</name>\n"
4850              "        <version>5</version>\n"
4851              "        <fqname>IFoo/default</fqname>\n"
4852              "        <fqname>IFoo/test0</fqname>\n"
4853              "    </hal>\n"
4854              "</manifest>\n",
4855              toXml(manifest, SerializeFlags::HALS_ONLY));
4856          EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
4857          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "default"));
4858          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "test0"));
4859          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", 5, "IFoo", "default"));
4860          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", 5, "IFoo", "test0"));
4861          EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IFoo", "does_not_exist"));
4862          EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IDoesNotExist", "default"));
4863          EXPECT_FALSE(manifest.hasAidlInstance("android.system.does_not_exist", "IFoo", "default"));
4864          EXPECT_EQ(manifest.getAidlInstances("android.system.foo", "IFoo"),
4865                    std::set<std::string>({"default", "test0"}));
4866          EXPECT_EQ(manifest.getAidlInstances("android.system.foo", 5, "IFoo"),
4867                    std::set<std::string>({"default", "test0"}));
4868          EXPECT_EQ(manifest.getAidlInstances("android.system.does_not_exist", "IFoo"),
4869                    std::set<std::string>({}));
4870      }
4871  
4872      {
4873          HalManifest manifest;
4874          std::string manifestXml =
4875              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4876              "    <hal format=\"aidl\">\n"
4877              "        <name>android.system.foo</name>\n"
4878              "        <version>5</version>\n"
4879              "        <fqname>IFoo/default</fqname>\n"
4880              "        <fqname>IFoo/test0</fqname>\n"
4881              "    </hal>\n"
4882              "</manifest>\n";
4883          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4884          EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
4885          EXPECT_TRUE(manifest.checkCompatibility(matrix, &error)) << error;
4886          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "default"));
4887          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", "IFoo", "test0"));
4888          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", 5, "IFoo", "default"));
4889          EXPECT_TRUE(manifest.hasAidlInstance("android.system.foo", 5, "IFoo", "test0"));
4890          EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IFoo", "does_not_exist"));
4891          EXPECT_FALSE(manifest.hasAidlInstance("android.system.foo", "IDoesNotExist", "default"));
4892          EXPECT_FALSE(manifest.hasAidlInstance("android.system.does_not_exist", "IFoo", "default"));
4893          EXPECT_EQ(manifest.getAidlInstances("android.system.foo", "IFoo"),
4894                    std::set<std::string>({"default", "test0"}));
4895          EXPECT_EQ(manifest.getAidlInstances("android.system.foo", 5, "IFoo"),
4896                    std::set<std::string>({"default", "test0"}));
4897          EXPECT_EQ(manifest.getAidlInstances("android.system.does_not_exist", "IFoo"),
4898                    std::set<std::string>({}));
4899      }
4900  
4901      {
4902          HalManifest manifest;
4903          std::string manifestXml =
4904              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4905              "    <hal format=\"aidl\">\n"
4906              "        <name>android.system.foo</name>\n"
4907              "        <version>5</version>\n"
4908              "        <fqname>IFoo/incompat_instance</fqname>\n"
4909              "        <fqname>IFoo/test0</fqname>\n"
4910              "    </hal>\n"
4911              "</manifest>\n";
4912          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4913          EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
4914          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
4915              << "Should not be compatible because default instance is missing";
4916          EXPECT_IN("required: (IFoo/default (@4-100) AND IFoo/test.* (@4-100))", error);
4917          EXPECT_IN("provided: \n"
4918                    "        IFoo/incompat_instance (@5)\n"
4919                    "        IFoo/test0 (@5)",
4920                    error);
4921      }
4922  
4923      {
4924          HalManifest manifest;
4925          std::string manifestXml =
4926              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4927              "    <hal format=\"aidl\">\n"
4928              "        <name>android.system.foo</name>\n"
4929              "        <version>5</version>\n"
4930              "        <interface>\n"
4931              "            <name>IFoo</name>\n"
4932              "            <instance>incompat_instance</instance>\n"
4933              "            <instance>test0</instance>\n"
4934              "        </interface>\n"
4935              "    </hal>\n"
4936              "</manifest>\n";
4937          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4938          EXPECT_EQ(
4939              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4940              "    <hal format=\"aidl\">\n"
4941              "        <name>android.system.foo</name>\n"
4942              "        <version>5</version>\n"
4943              "        <fqname>IFoo/incompat_instance</fqname>\n"
4944              "        <fqname>IFoo/test0</fqname>\n"
4945              "    </hal>\n"
4946              "</manifest>\n",
4947              toXml(manifest, SerializeFlags::HALS_ONLY));
4948          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
4949              << "Should not be compatible because default instance is missing";
4950          EXPECT_IN("required: (IFoo/default (@4-100) AND IFoo/test.* (@4-100))", error);
4951          EXPECT_IN("provided: \n"
4952                    "        IFoo/incompat_instance (@5)\n"
4953                    "        IFoo/test0 (@5)",
4954                    error);
4955      }
4956  
4957      {
4958          HalManifest manifest;
4959          std::string manifestXml =
4960              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4961              "    <hal format=\"aidl\">\n"
4962              "        <name>android.system.foo</name>\n"
4963              "        <version>5</version>\n"
4964              "        <fqname>IFoo/default</fqname>\n"
4965              "        <fqname>IFoo/incompat_instance</fqname>\n"
4966              "    </hal>\n"
4967              "</manifest>\n";
4968          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4969          EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
4970          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
4971              << "Should not be compatible because test.* instance is missing";
4972          EXPECT_IN("required: (IFoo/default (@4-100) AND IFoo/test.* (@4-100))", error);
4973          EXPECT_IN("provided: \n"
4974                    "        IFoo/default (@5)\n"
4975                    "        IFoo/incompat_instance (@5)",
4976                    error);
4977      }
4978  
4979      {
4980          HalManifest manifest;
4981          std::string manifestXml =
4982              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4983              "    <hal format=\"aidl\">\n"
4984              "        <name>android.system.foo</name>\n"
4985              "        <version>5</version>\n"
4986              "        <interface>\n"
4987              "            <name>IFoo</name>\n"
4988              "            <instance>default</instance>\n"
4989              "            <instance>incompat_instance</instance>\n"
4990              "        </interface>\n"
4991              "    </hal>\n"
4992              "</manifest>\n";
4993          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
4994          EXPECT_EQ(
4995              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
4996              "    <hal format=\"aidl\">\n"
4997              "        <name>android.system.foo</name>\n"
4998              "        <version>5</version>\n"
4999              "        <fqname>IFoo/default</fqname>\n"
5000              "        <fqname>IFoo/incompat_instance</fqname>\n"
5001              "    </hal>\n"
5002              "</manifest>\n",
5003              toXml(manifest, SerializeFlags::HALS_ONLY));
5004          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
5005              << "Should not be compatible because test.* instance is missing";
5006          EXPECT_IN("required: (IFoo/default (@4-100) AND IFoo/test.* (@4-100))", error);
5007          EXPECT_IN("provided: \n"
5008                    "        IFoo/default (@5)\n"
5009                    "        IFoo/incompat_instance (@5)",
5010                    error);
5011      }
5012  
5013      {
5014          HalManifest manifest;
5015          std::string manifestXml =
5016              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
5017              "    <hal format=\"aidl\">\n"
5018              "        <name>android.system.foo</name>\n"
5019              "        <version>3</version>\n"
5020              "        <fqname>IFoo/default</fqname>\n"
5021              "        <fqname>IFoo/test0</fqname>\n"
5022              "    </hal>\n"
5023              "</manifest>\n";
5024          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
5025          EXPECT_EQ(manifestXml, toXml(manifest, SerializeFlags::HALS_ONLY));
5026          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
5027              << "Should not be compatible because version 3 cannot satisfy version 4-100";
5028          EXPECT_IN("required: (IFoo/default (@4-100) AND IFoo/test.* (@4-100))", error);
5029          EXPECT_IN("provided: \n"
5030                    "        IFoo/default (@3)\n"
5031                    "        IFoo/test0 (@3)",
5032                    error);
5033  
5034      }
5035  
5036      {
5037          HalManifest manifest;
5038          std::string manifestXml =
5039              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
5040              "    <hal format=\"aidl\">\n"
5041              "        <name>android.system.foo</name>\n"
5042              "        <version>3</version>\n"
5043              "        <interface>\n"
5044              "            <name>IFoo</name>\n"
5045              "            <instance>default</instance>\n"
5046              "            <instance>test0</instance>\n"
5047              "        </interface>\n"
5048              "    </hal>\n"
5049              "</manifest>\n";
5050          EXPECT_TRUE(fromXml(&manifest, manifestXml, &error)) << error;
5051          EXPECT_EQ(
5052              "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
5053              "    <hal format=\"aidl\">\n"
5054              "        <name>android.system.foo</name>\n"
5055              "        <version>3</version>\n"
5056              "        <fqname>IFoo/default</fqname>\n"
5057              "        <fqname>IFoo/test0</fqname>\n"
5058              "    </hal>\n"
5059              "</manifest>\n",
5060              toXml(manifest, SerializeFlags::HALS_ONLY));
5061          EXPECT_FALSE(manifest.checkCompatibility(matrix, &error))
5062              << "Should not be compatible because version 3 cannot satisfy version 4-100";
5063          EXPECT_IN("required: (IFoo/default (@4-100) AND IFoo/test.* (@4-100))", error);
5064          EXPECT_IN("provided: \n"
5065                    "        IFoo/default (@3)\n"
5066                    "        IFoo/test0 (@3)",
5067                    error);
5068      }
5069  }
5070  
TEST_F(LibVintfTest,AidlFqnameNoVersion)5071  TEST_F(LibVintfTest, AidlFqnameNoVersion) {
5072      std::string error;
5073      HalManifest manifest;
5074      std::string manifestXml =
5075          "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
5076          "    <hal format=\"aidl\">\n"
5077          "        <name>android.system.foo</name>\n"
5078          "        <fqname>@1.0::IFoo/default</fqname>\n"
5079          "    </hal>\n"
5080          "</manifest>\n";
5081      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error)) << error;
5082      EXPECT_IN("Should not specify version in <fqname> for AIDL HAL: \"@1.0::IFoo/default\"", error);
5083  }
5084  
TEST_F(LibVintfTest,GetTransportHidlHalWithFakeAidlVersion)5085  TEST_F(LibVintfTest, GetTransportHidlHalWithFakeAidlVersion) {
5086      std::string xml =
5087          "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
5088          "    <hal format=\"hidl\">\n"
5089          "        <name>android.system.foo</name>\n"
5090          "        <transport>hwbinder</transport>\n"
5091          "        <fqname>@" + to_string(details::kDefaultAidlVersion) + "::IFoo/default</fqname>\n"
5092          "    </hal>\n"
5093          "</manifest>\n";
5094      std::string error;
5095      HalManifest manifest;
5096      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
5097      EXPECT_EQ(Transport::HWBINDER,
5098                manifest.getHidlTransport("android.system.foo", details::kDefaultAidlVersion, "IFoo",
5099                                          "default"));
5100  }
5101  
TEST_F(LibVintfTest,RejectAidlHalsWithUnsupportedTransport)5102  TEST_F(LibVintfTest, RejectAidlHalsWithUnsupportedTransport) {
5103      std::string error;
5104      HalManifest manifest;
5105      std::string manifestXml =
5106          "<manifest " + kMetaVersionStr + R"( type="framework">"
5107               <hal format="aidl">
5108                   <name>android.system.foo</name>
5109                   <transport>hwbinder</transport>
5110                   <fqname>IFoo/default</fqname>
5111               </hal>
5112           </manifest>)";
5113      EXPECT_FALSE(fromXml(&manifest, manifestXml, &error));
5114      EXPECT_IN("android.system.foo", error);
5115      EXPECT_IN("hwbinder", error);
5116  }
5117  
5118  TEST_F(LibVintfTest, GetTransportAidlHalWithDummyTransport) {
5119      // Check that even if <transport> is specified for AIDL, it is ignored and getHidlTransport
5120      // will return EMPTY.
5121      // This is only supported for libvintf 4.0 and below.
5122      constexpr Version kLegacyMetaVersion{4, 0};
5123      ASSERT_GE(kMetaVersionAidlInet, kLegacyMetaVersion);
5124      std::string xml =
5125          "<manifest version=\"" + to_string(kLegacyMetaVersion) + "\" type=\"framework\">\n"
5126          "    <hal format=\"aidl\">\n"
5127          "        <name>android.system.foo</name>\n"
5128          "        <transport>hwbinder</transport>\n"
5129          "        <fqname>IFoo/default</fqname>\n"
5130          "    </hal>\n"
5131          "</manifest>\n";
5132      std::string error;
5133      HalManifest manifest;
5134      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
5135      EXPECT_EQ(Transport::EMPTY,
5136                manifest.getHidlTransport("android.system.foo", details::kDefaultAidlVersion, "IFoo",
5137                                          "default"));
5138  }
5139  
TEST_F(LibVintfTest,AidlGetHalNamesAndVersions)5140  TEST_F(LibVintfTest, AidlGetHalNamesAndVersions) {
5141      HalManifest manifest;
5142      std::string xml =
5143          "<manifest " + kMetaVersionStr + " type=\"framework\">\n"
5144          "    <hal format=\"aidl\">\n"
5145          "        <name>android.system.foo</name>\n"
5146          "        <fqname>IFoo/default</fqname>\n"
5147          "    </hal>\n"
5148          "</manifest>\n";
5149      std::string error;
5150      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
5151      auto names = manifest.getHalNamesAndVersions();
5152      ASSERT_EQ(1u, names.size());
5153      EXPECT_EQ("android.system.foo@1", *names.begin());
5154  }
5155  
TEST_F(LibVintfTest,ManifestAddAidl)5156  TEST_F(LibVintfTest, ManifestAddAidl) {
5157      std::string head =
5158              "<manifest " + kMetaVersionStr + " type=\"device\">\n"
5159              "    <hal format=\"aidl\">\n"
5160              "        <name>android.hardware.foo</name>\n"
5161              "        <fqname>";
5162      std::string tail =
5163              "</fqname>\n"
5164              "    </hal>\n"
5165              "</manifest>\n";
5166  
5167      std::string xml1 = head + "IFoo/default" + tail;
5168      std::string xml2 = head + "IFoo/another" + tail;
5169  
5170      std::string error;
5171      HalManifest manifest1;
5172      manifest1.setFileName("1.xml");
5173      ASSERT_TRUE(fromXml(&manifest1, xml1, &error)) << error;
5174      HalManifest manifest2;
5175      manifest2.setFileName("2.xml");
5176      ASSERT_TRUE(fromXml(&manifest2, xml2, &error)) << error;
5177  
5178      ASSERT_TRUE(manifest1.addAll(&manifest2, &error)) << error;
5179  }
5180  
5181  // clang-format on
5182  
TEST_F(LibVintfTest,NativeGetHalNamesAndVersions)5183  TEST_F(LibVintfTest, NativeGetHalNamesAndVersions) {
5184      HalManifest manifest;
5185      std::string xml = "<manifest " + kMetaVersionStr + R"( type="device">
5186              <hal format="native">
5187                  <name>foo</name>
5188                  <version>1.0</version>
5189                  <interface>
5190                      <instance>inst</instance>
5191                  </interface>
5192             </hal>
5193          </manifest>
5194      )";
5195      std::string error;
5196      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
5197      auto names = manifest.getHalNamesAndVersions();
5198      ASSERT_EQ(1u, names.size());
5199      EXPECT_EQ("foo@1.0", *names.begin());
5200  }
5201  
TEST_F(LibVintfTest,NativeGetHalNamesAndVersionsFqName)5202  TEST_F(LibVintfTest, NativeGetHalNamesAndVersionsFqName) {
5203      HalManifest manifest;
5204      std::string xml = "<manifest " + kMetaVersionStr + R"( type="device">
5205              <hal format="native">
5206                  <name>foo</name>
5207                  <fqname>@1.0/inst</fqname>
5208             </hal>
5209          </manifest>
5210      )";
5211      std::string error;
5212      EXPECT_TRUE(fromXml(&manifest, xml, &error)) << error;
5213      auto names = manifest.getHalNamesAndVersions();
5214      ASSERT_EQ(1u, names.size());
5215      EXPECT_EQ("foo@1.0", *names.begin());
5216  }
5217  
5218  // clang-format off
5219  
TEST_F(LibVintfTest,KernelInfoLevel)5220  TEST_F(LibVintfTest, KernelInfoLevel) {
5221      std::string error;
5222      std::string xml = "<kernel version=\"3.18.31\" target-level=\"1\"/>\n";
5223      KernelInfo ki;
5224      ASSERT_TRUE(fromXml(&ki, xml, &error)) << error;
5225      EXPECT_EQ(Level{1}, getLevel(ki));
5226      EXPECT_EQ(xml, toXml(ki));
5227  }
5228  
5229  // Test merge of <kernel target-level=""> with autogenerated <kernel> by parsing
5230  // kernel prebuilt.
TEST_F(LibVintfTest,HalManifestMergeKernel)5231  TEST_F(LibVintfTest, HalManifestMergeKernel) {
5232      std::string head =
5233          "<manifest " + kMetaVersionStr + " type=\"device\" target-level=\"1\">\n";
5234      std::string tail =
5235          "</manifest>\n";
5236      std::string xml1 =
5237          "    <kernel target-level=\"2\"/>\n";
5238      std::string xml2 =
5239          "    <kernel version=\"3.18.31\">\n"
5240          "        <config>\n"
5241          "            <key>CONFIG_64BIT</key>\n"
5242          "            <value>y</value>\n"
5243          "        </config>\n"
5244          "    </kernel>\n";
5245  
5246      std::string error;
5247      HalManifest manifest1;
5248      HalManifest manifest2;
5249      ASSERT_TRUE(fromXml(&manifest1, head + xml1 + tail, &error)) << error;
5250      ASSERT_TRUE(fromXml(&manifest2, head + xml2 + tail, &error)) << error;
5251      ASSERT_TRUE(manifest1.addAll(&manifest2, &error)) << error;
5252      std::string merged_xml = toXml(manifest1);
5253      EXPECT_IN(head, merged_xml);
5254      EXPECT_IN("target-level=\"2\"", merged_xml);
5255      EXPECT_IN("version=\"3.18.31\"", merged_xml);
5256      EXPECT_IN("CONFIG_64BIT", merged_xml);
5257  }
5258  
5259  // clang-format on
5260  
TEST_F(LibVintfTest,FrameworkManifestHalMaxLevel)5261  TEST_F(LibVintfTest, FrameworkManifestHalMaxLevel) {
5262      std::string xml = "<manifest " + kMetaVersionStr + R"( type="framework">
5263                             <hal max-level="3">
5264                                 <name>android.frameworks.schedulerservice</name>
5265                                 <transport>hwbinder</transport>
5266                                 <fqname>@1.0::ISchedulingPolicyService/default</fqname>
5267                             </hal>
5268                             <hal format="aidl" max-level="4">
5269                                 <name>android.frameworks.myaidl</name>
5270                                 <fqname>IAidl/default</fqname>
5271                             </hal>
5272                             <hal format="native" max-level="5">
5273                                 <name>some-native-hal</name>
5274                                 <version>1.0</version>
5275                             </hal>
5276                         </manifest>)";
5277  
5278      std::string error;
5279      HalManifest manifest;
5280      ASSERT_TRUE(fromXml(&manifest, xml, &error)) << error;
5281  
5282      auto hals = getHals(manifest, "android.frameworks.schedulerservice");
5283      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMaxLevel, Eq(static_cast<Level>(3)))));
5284  
5285      hals = getHals(manifest, "android.frameworks.myaidl");
5286      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMaxLevel, Eq(static_cast<Level>(4)))));
5287  
5288      hals = getHals(manifest, "some-native-hal");
5289      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMaxLevel, Eq(static_cast<Level>(5)))));
5290  }
5291  
TEST_F(LibVintfTest,FrameworkManifestHalMinLevel)5292  TEST_F(LibVintfTest, FrameworkManifestHalMinLevel) {
5293      std::string xml = "<manifest " + kMetaVersionStr + R"( type="framework">
5294                             <hal min-level="3">
5295                                 <name>android.frameworks.schedulerservice</name>
5296                                 <transport>hwbinder</transport>
5297                                 <fqname>@1.0::ISchedulingPolicyService/default</fqname>
5298                             </hal>
5299                             <hal format="aidl" min-level="4">
5300                                 <name>android.frameworks.myaidl</name>
5301                                 <fqname>IAidl/default</fqname>
5302                             </hal>
5303                             <hal format="native" min-level="5">
5304                                 <name>some-native-hal</name>
5305                                 <version>1.0</version>
5306                             </hal>
5307                         </manifest>)";
5308  
5309      std::string error;
5310      HalManifest manifest;
5311      ASSERT_TRUE(fromXml(&manifest, xml, &error)) << error;
5312  
5313      auto hals = getHals(manifest, "android.frameworks.schedulerservice");
5314      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMinLevel, Eq(static_cast<Level>(3)))));
5315  
5316      hals = getHals(manifest, "android.frameworks.myaidl");
5317      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMinLevel, Eq(static_cast<Level>(4)))));
5318  
5319      hals = getHals(manifest, "some-native-hal");
5320      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMinLevel, Eq(static_cast<Level>(5)))));
5321  }
5322  
TEST_F(LibVintfTest,FrameworkManifestHalMinMaxLevel)5323  TEST_F(LibVintfTest, FrameworkManifestHalMinMaxLevel) {
5324      std::string xml = "<manifest " + kMetaVersionStr + R"( type="framework">
5325                             <hal min-level="2" max-level="5">
5326                                 <name>android.frameworks.schedulerservice</name>
5327                                 <transport>hwbinder</transport>
5328                                 <fqname>@1.0::ISchedulingPolicyService/default</fqname>
5329                             </hal>
5330                             <hal format="aidl" min-level="3" max-level="6">
5331                                 <name>android.frameworks.myaidl</name>
5332                                 <fqname>IAidl/default</fqname>
5333                             </hal>
5334                             <hal format="native" min-level="4" max-level="7">
5335                                 <name>some-native-hal</name>
5336                                 <version>1.0</version>
5337                             </hal>
5338                         </manifest>)";
5339  
5340      std::string error;
5341      HalManifest manifest;
5342      ASSERT_TRUE(fromXml(&manifest, xml, &error)) << error;
5343  
5344      auto hals = getHals(manifest, "android.frameworks.schedulerservice");
5345      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMinLevel, Eq(static_cast<Level>(2)))));
5346      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMaxLevel, Eq(static_cast<Level>(5)))));
5347  
5348      hals = getHals(manifest, "android.frameworks.myaidl");
5349      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMinLevel, Eq(static_cast<Level>(3)))));
5350      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMaxLevel, Eq(static_cast<Level>(6)))));
5351  
5352      hals = getHals(manifest, "some-native-hal");
5353      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMinLevel, Eq(static_cast<Level>(4)))));
5354      EXPECT_THAT(hals, ElementsAre(Property(&ManifestHal::getMaxLevel, Eq(static_cast<Level>(7)))));
5355  }
5356  
TEST_F(LibVintfTest,RuntimeInfoParseGkiKernelReleaseOk)5357  TEST_F(LibVintfTest, RuntimeInfoParseGkiKernelReleaseOk) {
5358      KernelVersion version;
5359      Level level = Level::UNSPECIFIED;
5360      EXPECT_EQ(OK, parseGkiKernelRelease(RuntimeInfo::FetchFlag::ALL, "5.4.42-android12-0-something",
5361                                          &version, &level));
5362      EXPECT_EQ(KernelVersion(5, 4, 42), version);
5363      EXPECT_EQ(Level::S, level);
5364  }
5365  
TEST_F(LibVintfTest,RuntimeInfoParseGkiKernelReleaseVersionOnly)5366  TEST_F(LibVintfTest, RuntimeInfoParseGkiKernelReleaseVersionOnly) {
5367      KernelVersion version;
5368      EXPECT_EQ(OK, parseGkiKernelRelease(RuntimeInfo::FetchFlag::CPU_VERSION,
5369                                          "5.4.42-android12-0-something", &version, nullptr));
5370      EXPECT_EQ(KernelVersion(5, 4, 42), version);
5371  }
5372  
TEST_F(LibVintfTest,RuntimeInfoParseGkiKernelReleaseLevelOnly)5373  TEST_F(LibVintfTest, RuntimeInfoParseGkiKernelReleaseLevelOnly) {
5374      Level level = Level::UNSPECIFIED;
5375      EXPECT_EQ(OK, parseGkiKernelRelease(RuntimeInfo::FetchFlag::KERNEL_FCM,
5376                                          "5.4.42-android12-0-something", nullptr, &level));
5377      EXPECT_EQ(Level::S, level);
5378  }
5379  
TEST_F(LibVintfTest,RuntimeInfoParseGkiKernelReleaseLevelConsistent)5380  TEST_F(LibVintfTest, RuntimeInfoParseGkiKernelReleaseLevelConsistent) {
5381      Level level = Level::S;
5382      EXPECT_EQ(OK, parseGkiKernelRelease(RuntimeInfo::FetchFlag::KERNEL_FCM,
5383                                          "5.4.42-android12-0-something", nullptr, &level));
5384      EXPECT_EQ(Level::S, level);
5385  }
5386  
TEST_F(LibVintfTest,RuntimeInfoParseGkiKernelReleaseLevelInconsistent)5387  TEST_F(LibVintfTest, RuntimeInfoParseGkiKernelReleaseLevelInconsistent) {
5388      Level level = Level::R;
5389      EXPECT_EQ(UNKNOWN_ERROR,
5390                parseGkiKernelRelease(RuntimeInfo::FetchFlag::KERNEL_FCM,
5391                                      "5.4.42-android12-0-something", nullptr, &level));
5392  }
5393  
5394  class ManifestMissingITest : public LibVintfTest,
5395                               public ::testing::WithParamInterface<std::string> {
5396     public:
createParams()5397      static std::vector<std::string> createParams() {
5398          std::vector<std::string> ret;
5399  
5400          ret.push_back("<manifest " + kMetaVersionStr + R"( type="device">
5401              <hal format="aidl">
5402                  <name>android.hardware.foo</name>
5403                  <version>1</version>
5404                  <interface>
5405                      <name>MyFoo</name>
5406                      <instance>default</instance>
5407                  </interface>
5408              </hal>
5409          </manifest>)");
5410  
5411          ret.push_back("<manifest " + kMetaVersionStr + R"( type="device">
5412              <hal format="hidl">
5413                  <name>android.hardware.foo</name>
5414                  <transport>hwbinder</transport>
5415                  <version>1.0</version>
5416                  <interface>
5417                      <name>MyFoo</name>
5418                      <instance>default</instance>
5419                  </interface>
5420              </hal>
5421          </manifest>)");
5422  
5423          ret.push_back("<manifest " + kMetaVersionStr + R"( type="device">
5424              <hal format="native">
5425                  <name>android.hardware.foo</name>
5426                  <version>1.0</version>
5427                  <interface>
5428                      <name>MyFoo</name>
5429                      <instance>default</instance>
5430                  </interface>
5431              </hal>
5432          </manifest>)");
5433  
5434          return ret;
5435      }
5436  };
5437  
TEST_P(ManifestMissingITest,CheckErrorMsg)5438  TEST_P(ManifestMissingITest, CheckErrorMsg) {
5439      std::string xml = GetParam();
5440      HalManifest manifest;
5441      std::string error;
5442      ASSERT_FALSE(fromXml(&manifest, xml, &error)) << "Should not be valid:\n" << xml;
5443      EXPECT_THAT(error, HasSubstr("Interface 'MyFoo' should have the format I[a-zA-Z0-9_]*")) << "\n"
5444                                                                                               << xml;
5445  }
5446  
5447  INSTANTIATE_TEST_SUITE_P(LibVintfTest, ManifestMissingITest,
5448                           ::testing::ValuesIn(ManifestMissingITest::createParams()));
5449  
5450  struct ManifestMissingInterfaceTestParam {
5451      std::string xml;
5452      std::string expectedError;
5453  };
5454  
5455  class ManifestMissingInterfaceTest
5456      : public LibVintfTest,
5457        public ::testing::WithParamInterface<ManifestMissingInterfaceTestParam> {
5458     public:
createParams()5459      static std::vector<ManifestMissingInterfaceTestParam> createParams() {
5460          std::vector<ManifestMissingInterfaceTestParam> ret;
5461  
5462          ret.emplace_back(ManifestMissingInterfaceTestParam{
5463              "<manifest " + kMetaVersionStr + R"( type="device">
5464                  <hal format="aidl">
5465                      <name>android.hardware.foo</name>
5466                      <version>1</version>
5467                      <interface>
5468                          <instance>default</instance>
5469                      </interface>
5470                  </hal>
5471              </manifest>)",
5472              "Interface '' should have the format I[a-zA-Z0-9_]*",
5473          });
5474  
5475          ret.emplace_back(ManifestMissingInterfaceTestParam{
5476              "<manifest " + kMetaVersionStr + R"( type="device">
5477                  <hal format="aidl">
5478                      <name>android.hardware.foo</name>
5479                      <version>1</version>
5480                      <fqname>/default</fqname>
5481                  </hal>
5482              </manifest>)",
5483              "Could not parse text \"/default\" in element <fqname>",
5484          });
5485  
5486          ret.emplace_back(ManifestMissingInterfaceTestParam{
5487              "<manifest " + kMetaVersionStr + R"( type="device">
5488                  <hal format="hidl">
5489                      <name>android.hardware.foo</name>
5490                      <transport>hwbinder</transport>
5491                      <version>1.0</version>
5492                      <interface>
5493                          <instance>default</instance>
5494                      </interface>
5495                  </hal>
5496              </manifest>)",
5497              "Interface '' should have the format I[a-zA-Z0-9_]*",
5498          });
5499  
5500          ret.emplace_back(ManifestMissingInterfaceTestParam{
5501              "<manifest " + kMetaVersionStr + R"( type="device">
5502                  <hal format="hidl">
5503                      <name>android.hardware.foo</name>
5504                      <transport>hwbinder</transport>
5505                      <fqname>@1.0/default</fqname>
5506                  </hal>
5507              </manifest>)",
5508              "Should specify interface: \"@1.0/default\"",
5509          });
5510  
5511          return ret;
5512      }
5513  };
5514  
TEST_P(ManifestMissingInterfaceTest,CheckErrorMsg)5515  TEST_P(ManifestMissingInterfaceTest, CheckErrorMsg) {
5516      auto&& [xml, expectedError] = GetParam();
5517      HalManifest manifest;
5518      std::string error;
5519      ASSERT_FALSE(fromXml(&manifest, xml, &error)) << "Should not be valid:\n" << xml;
5520      EXPECT_THAT(error, HasSubstr(expectedError)) << "\n" << xml;
5521  }
5522  
5523  INSTANTIATE_TEST_SUITE_P(LibVintfTest, ManifestMissingInterfaceTest,
5524                           ::testing::ValuesIn(ManifestMissingInterfaceTest::createParams()));
5525  
TEST_F(LibVintfTest,HalManifestInvalidPackage)5526  TEST_F(LibVintfTest, HalManifestInvalidPackage) {
5527      // If package name, interface or instance contains characters invalid to FqInstance,
5528      // it must be rejected because forEachInstance requires them to fit into FqInstance.
5529      std::string xml = "<manifest " + kMetaVersionStr + R"( type="framework">
5530                             <hal format="aidl">
5531                                 <name>not_a_valid_package!</name>
5532                                 <version>1</version>
5533                                 <interface>
5534                                     <name>MyFoo</name>
5535                                     <instance>default</instance>
5536                                 </interface>
5537                             </hal>
5538                         </manifest>)";
5539      HalManifest manifest;
5540      std::string error;
5541      ASSERT_FALSE(fromXml(&manifest, xml, &error)) << "Should not be valid:\n" << xml;
5542      EXPECT_THAT(error, HasSubstr("not_a_valid_package!"));
5543  }
5544  
5545  class MatrixMissingITest : public LibVintfTest, public ::testing::WithParamInterface<std::string> {
5546     public:
createParams()5547      static std::vector<std::string> createParams() {
5548          std::vector<std::string> ret;
5549  
5550          ret.push_back("<compatibility-matrix " + kMetaVersionStr + R"( type="device">
5551              <hal format="aidl">
5552                  <name>android.hardware.foo</name>
5553                  <version>1</version>
5554                  <interface>
5555                      <name>MyFoo</name>
5556                      <instance>default</instance>
5557                  </interface>
5558              </hal>
5559          </compatibility-matrix>)");
5560  
5561          ret.push_back("<compatibility-matrix " + kMetaVersionStr + R"( type="device">
5562              <hal format="hidl">
5563                  <name>android.hardware.foo</name>
5564                  <version>1.0</version>
5565                  <interface>
5566                      <name>MyFoo</name>
5567                      <instance>default</instance>
5568                  </interface>
5569              </hal>
5570          </compatibility-matrix>)");
5571  
5572          ret.push_back("<compatibility-matrix " + kMetaVersionStr + R"( type="device">
5573              <hal format="native">
5574                  <name>android.hardware.foo</name>
5575                  <version>1.0</version>
5576                  <interface>
5577                      <name>MyFoo</name>
5578                      <instance>default</instance>
5579                  </interface>
5580              </hal>
5581          </compatibility-matrix>)");
5582  
5583          return ret;
5584      }
5585  };
5586  
TEST_P(MatrixMissingITest,CheckErrorMsg)5587  TEST_P(MatrixMissingITest, CheckErrorMsg) {
5588      std::string xml = GetParam();
5589      CompatibilityMatrix matrix;
5590      std::string error;
5591      ASSERT_FALSE(fromXml(&matrix, xml, &error)) << "Should not be valid:\n" << xml;
5592      EXPECT_THAT(error, HasSubstr("Interface 'MyFoo' should have the format I[a-zA-Z0-9_]*"));
5593  }
5594  
5595  INSTANTIATE_TEST_SUITE_P(LibVintfTest, MatrixMissingITest,
5596                           ::testing::ValuesIn(MatrixMissingITest::createParams()));
5597  
5598  struct MatrixMissingInterfaceTestParam {
5599      std::string xml;
5600      std::string expectedError;
5601  };
5602  
5603  class MatrixMissingInterfaceTest
5604      : public LibVintfTest,
5605        public ::testing::WithParamInterface<MatrixMissingInterfaceTestParam> {
5606     public:
createParams()5607      static std::vector<MatrixMissingInterfaceTestParam> createParams() {
5608          std::vector<MatrixMissingInterfaceTestParam> ret;
5609  
5610          ret.emplace_back(MatrixMissingInterfaceTestParam{
5611              "<compatibility-matrix " + kMetaVersionStr + R"( type="device">
5612                  <hal format="aidl">
5613                      <name>android.hardware.foo</name>
5614                      <version>1</version>
5615                      <interface>
5616                          <instance>default</instance>
5617                      </interface>
5618                  </hal>
5619              </compatibility-matrix>)",
5620              "Interface '' should have the format I[a-zA-Z0-9_]*",
5621          });
5622  
5623          ret.emplace_back(MatrixMissingInterfaceTestParam{
5624              "<compatibility-matrix " + kMetaVersionStr + R"( type="device">
5625                  <hal format="hidl">
5626                      <name>android.hardware.foo</name>
5627                      <version>1.0</version>
5628                      <interface>
5629                          <instance>default</instance>
5630                      </interface>
5631                  </hal>
5632              </compatibility-matrix>)",
5633              "Interface '' should have the format I[a-zA-Z0-9_]*",
5634          });
5635  
5636          return ret;
5637      }
5638  };
5639  
TEST_P(MatrixMissingInterfaceTest,CheckErrorMsg)5640  TEST_P(MatrixMissingInterfaceTest, CheckErrorMsg) {
5641      auto&& [xml, expectedError] = GetParam();
5642      CompatibilityMatrix matrix;
5643      std::string error;
5644      ASSERT_FALSE(fromXml(&matrix, xml, &error)) << "Should not be valid:\n" << xml;
5645      EXPECT_THAT(error, HasSubstr(expectedError)) << "\n" << xml;
5646  }
5647  
5648  INSTANTIATE_TEST_SUITE_P(LibVintfTest, MatrixMissingInterfaceTest,
5649                           ::testing::ValuesIn(MatrixMissingInterfaceTest::createParams()));
5650  
TEST_F(LibVintfTest,CompatibilityMatrixInvalidPackage)5651  TEST_F(LibVintfTest, CompatibilityMatrixInvalidPackage) {
5652      // If package name, interface or instance contains characters invalid to FqInstance,
5653      // it must be rejected because forEachInstance requires them to fit into FqInstance.
5654      std::string xml = "<compatibility-matrix " + kMetaVersionStr + R"( type="framework">
5655                             <hal format="aidl">
5656                                 <name>not_a_valid_package!</name>
5657                                 <version>1-2</version>
5658                                 <interface>
5659                                     <name>MyFoo</name>
5660                                     <instance>default</instance>
5661                                 </interface>
5662                             </hal>
5663                         </compatibility-matrix>)";
5664      CompatibilityMatrix matrix;
5665      std::string error;
5666      ASSERT_FALSE(fromXml(&matrix, xml, &error)) << "Should not be valid:\n" << xml;
5667      EXPECT_THAT(error, HasSubstr("not_a_valid_package!"));
5668  }
5669  
5670  struct DupInterfaceAndFqnameTestParam {
5671      HalFormat format;
5672      std::string footer;
5673      std::string halName;
5674  };
5675  
5676  class DupInterfaceAndFqnameTest
5677      : public LibVintfTest,
5678        public ::testing::WithParamInterface<DupInterfaceAndFqnameTestParam> {
5679     public:
createParams()5680      static std::vector<DupInterfaceAndFqnameTestParam> createParams() {
5681          std::vector<DupInterfaceAndFqnameTestParam> ret;
5682  
5683          std::string hidlFooter = R"(
5684      <hal>
5685          <name>android.hardware.nfc</name>
5686          <transport>hwbinder</transport>
5687          <version>1.0</version>
5688          <interface>
5689              <name>INfc</name>
5690              <instance>default</instance>
5691          </interface>
5692          <fqname>@1.0::INfc/default</fqname>
5693      </hal>
5694  </manifest>
5695  )";
5696  
5697          std::string aidlFooter = R"(
5698      <hal format="aidl">
5699          <name>android.hardware.nfc</name>
5700          <interface>
5701              <name>INfc</name>
5702              <instance>default</instance>
5703          </interface>
5704          <fqname>INfc/default</fqname>
5705      </hal>
5706  </manifest>
5707  )";
5708  
5709          return {
5710              {HalFormat::HIDL, hidlFooter, "android.hardware.nfc@1.0::INfc/default"},
5711              {HalFormat::AIDL, aidlFooter, "android.hardware.nfc.INfc/default"},
5712          };
5713      }
getTestSuffix(const TestParamInfo<ParamType> & info)5714      static std::string getTestSuffix(const TestParamInfo<ParamType>& info) {
5715          return to_string(info.param.format);
5716      }
5717  };
5718  
TEST_P(DupInterfaceAndFqnameTest,Test5_0)5719  TEST_P(DupInterfaceAndFqnameTest, Test5_0) {
5720      auto&& [_, footer, halName] = GetParam();
5721      std::string xml = R"(<manifest version="5.0" type="device">)" + footer;
5722      HalManifest vm;
5723      std::string error;
5724      EXPECT_TRUE(fromXml(&vm, xml, &error))
5725          << "<fqname> and <interface> are allowed to exist "
5726             "together for the same instance for libvintf 5.0, but error is: "
5727          << error;
5728  }
5729  
TEST_P(DupInterfaceAndFqnameTest,Test6_0)5730  TEST_P(DupInterfaceAndFqnameTest, Test6_0) {
5731      auto&& [_, footer, halName] = GetParam();
5732      std::string xml = R"(<manifest version=")" + to_string(kMetaVersionNoHalInterfaceInstance) +
5733                        R"(" type="device">)" + footer;
5734      HalManifest vm;
5735      std::string error;
5736      EXPECT_FALSE(fromXml(&vm, xml, &error));
5737      EXPECT_THAT(error,
5738                  HasSubstr("Duplicated " + halName + " in <interface><instance> and <fqname>."))
5739          << "<fqname> and <interface> are not allowed to exist "
5740             "together for the same instance for libvintf "
5741          << kMetaVersionNoHalInterfaceInstance << ".";
5742  }
5743  
5744  INSTANTIATE_TEST_SUITE_P(LibVintfTest, DupInterfaceAndFqnameTest,
5745                           ::testing::ValuesIn(DupInterfaceAndFqnameTest::createParams()),
5746                           &DupInterfaceAndFqnameTest::getTestSuffix);
5747  
5748  struct AllowDupMajorVersionTestParam {
5749      std::string testName;
5750      std::string expectedError;
5751      std::string footer;
5752  };
5753  
5754  class AllowDupMajorVersionTest
5755      : public LibVintfTest,
5756        public ::testing::WithParamInterface<AllowDupMajorVersionTestParam> {
5757     public:
createParams()5758      static std::vector<AllowDupMajorVersionTestParam> createParams() {
5759          std::vector<AllowDupMajorVersionTestParam> ret;
5760          ret.push_back({"HidlInterfaceAndFqName", "Duplicated major version", R"(
5761                  <hal>
5762                      <name>android.hardware.nfc</name>
5763                      <transport>hwbinder</transport>
5764                      <version>1.0</version>
5765                      <interface>
5766                          <name>INfc</name>
5767                          <instance>default</instance>
5768                      </interface>
5769                      <fqname>@1.1::INfc/default</fqname>
5770                  </hal>
5771              </manifest>
5772              )"});
5773          ret.push_back({"HidlFqNameInTheSameHal", "Duplicated major version", R"(
5774                  <hal>
5775                      <name>android.hardware.nfc</name>
5776                      <transport>hwbinder</transport>
5777                      <fqname>@1.0::INfc/default</fqname>
5778                      <fqname>@1.1::INfc/default</fqname>
5779                  </hal>
5780              </manifest>
5781              )"});
5782          ret.push_back({"HidlFqNameInDifferentHals", "Conflicting FqInstance", R"(
5783                  <hal>
5784                      <name>android.hardware.nfc</name>
5785                      <transport>hwbinder</transport>
5786                      <fqname>@1.0::INfc/default</fqname>
5787                  </hal>
5788                  <hal>
5789                      <name>android.hardware.nfc</name>
5790                      <transport>hwbinder</transport>
5791                      <fqname>@1.1::INfc/default</fqname>
5792                  </hal>
5793              </manifest>
5794              )"});
5795          ret.push_back({"HidlInterfaceAndFqNameInDifferentHals", "Conflicting FqInstance", R"(
5796                  <hal>
5797                      <name>android.hardware.nfc</name>
5798                      <transport>hwbinder</transport>
5799                      <version>1.0</version>
5800                      <interface>
5801                          <name>INfc</name>
5802                          <instance>default</instance>
5803                      </interface>
5804                  </hal>
5805                  <hal>
5806                      <name>android.hardware.nfc</name>
5807                      <transport>hwbinder</transport>
5808                      <fqname>@1.1::INfc/default</fqname>
5809                  </hal>
5810              </manifest>
5811              )"});
5812          ret.push_back({"AidlInterfaceInDifferentHals", "Conflicting FqInstance", R"(
5813                  <hal format="aidl">
5814                      <name>android.hardware.nfc</name>
5815                      <version>1</version>
5816                      <interface>
5817                          <name>INfc</name>
5818                          <instance>default</instance>
5819                      </interface>
5820                  </hal>
5821                  <hal format="aidl">
5822                      <name>android.hardware.nfc</name>
5823                      <version>2</version>
5824                      <interface>
5825                          <name>INfc</name>
5826                          <instance>default</instance>
5827                      </interface>
5828                  </hal>
5829              </manifest>
5830              )"});
5831          ret.push_back({"AidlFqNameInDifferentHals", "Conflicting FqInstance", R"(
5832                  <hal format="aidl">
5833                      <name>android.hardware.nfc</name>
5834                      <version>1</version>
5835                      <fqname>INfc/default</fqname>
5836                  </hal>
5837                  <hal format="aidl">
5838                      <name>android.hardware.nfc</name>
5839                      <version>2</version>
5840                      <fqname>INfc/default</fqname>
5841                  </hal>
5842              </manifest>
5843              )"});
5844          ret.push_back({"AidlInterfaceAndFqNameInDifferentHals", "Conflicting FqInstance", R"(
5845                  <hal format="aidl">
5846                      <name>android.hardware.nfc</name>
5847                      <version>1</version>
5848                      <interface>
5849                          <name>INfc</name>
5850                          <instance>default</instance>
5851                      </interface>
5852                  </hal>
5853                  <hal format="aidl">
5854                      <name>android.hardware.nfc</name>
5855                      <version>2</version>
5856                      <fqname>INfc/default</fqname>
5857                  </hal>
5858              </manifest>
5859              )"});
5860          return ret;
5861      }
getTestSuffix(const TestParamInfo<ParamType> & info)5862      static std::string getTestSuffix(const TestParamInfo<ParamType>& info) {
5863          return info.param.testName;
5864      }
5865  };
5866  
TEST_P(AllowDupMajorVersionTest,Allow5_0)5867  TEST_P(AllowDupMajorVersionTest, Allow5_0) {
5868      auto&& [_, expectedError, footer] = GetParam();
5869      std::string xml = R"(<manifest version="5.0" type="device">)" + footer;
5870      HalManifest vm;
5871      std::string error;
5872      EXPECT_TRUE(fromXml(&vm, xml, &error))
5873          << "Conflicting major version in <fqname> is allowed in libvintf 5.0. However, error is: "
5874          << error;
5875  }
5876  
TEST_P(AllowDupMajorVersionTest,DoNotAllow6_0)5877  TEST_P(AllowDupMajorVersionTest, DoNotAllow6_0) {
5878      auto&& [_, expectedError, footer] = GetParam();
5879      std::string xml = R"(<manifest version=")" + to_string(kMetaVersionNoHalInterfaceInstance) +
5880                        R"(" type="device">)" + footer;
5881      HalManifest vm;
5882      std::string error;
5883      EXPECT_FALSE(fromXml(&vm, xml, &error));
5884      EXPECT_THAT(error, HasSubstr(expectedError));
5885  }
5886  
5887  INSTANTIATE_TEST_SUITE_P(LibVintfTest, AllowDupMajorVersionTest,
5888                           ::testing::ValuesIn(AllowDupMajorVersionTest::createParams()),
5889                           &AllowDupMajorVersionTest::getTestSuffix);
5890  
5891  struct InterfaceMissingInstanceTestParam {
5892      HalFormat format;
5893      std::string footer;
5894  };
5895  
5896  class InterfaceMissingInstanceTest
5897      : public LibVintfTest,
5898        public ::testing::WithParamInterface<InterfaceMissingInstanceTestParam> {
5899     public:
createParams()5900      static std::vector<InterfaceMissingInstanceTestParam> createParams() {
5901          std::vector<InterfaceMissingInstanceTestParam> ret;
5902  
5903          std::string hidlFooter = R"(
5904      <hal>
5905          <name>android.hardware.nfc</name>
5906          <transport>hwbinder</transport>
5907          <version>1.0</version>
5908          <interface>
5909              <name>INfc</name>
5910          </interface>
5911      </hal>
5912  </manifest>
5913  )";
5914          std::string aidlFooter = R"(
5915      <hal format="aidl">
5916          <name>android.hardware.nfc</name>
5917          <interface>
5918              <name>INfc</name>
5919          </interface>
5920      </hal>
5921  </manifest>
5922  )";
5923  
5924          return {{HalFormat::HIDL, hidlFooter}, {HalFormat::AIDL, aidlFooter}};
5925      }
getTestSuffix(const TestParamInfo<ParamType> & info)5926      static std::string getTestSuffix(const TestParamInfo<ParamType>& info) {
5927          return to_string(info.param.format);
5928      }
5929  };
5930  
TEST_P(InterfaceMissingInstanceTest,Test5_0)5931  TEST_P(InterfaceMissingInstanceTest, Test5_0) {
5932      auto&& [testName, footer] = GetParam();
5933      std::string header = R"(<manifest version="5.0" type="device">)";
5934      std::string xml = header + footer;
5935      HalManifest vm;
5936      std::string error;
5937      EXPECT_TRUE(fromXml(&vm, xml, &error)) << error;
5938  }
5939  
TEST_P(InterfaceMissingInstanceTest,Test6_0)5940  TEST_P(InterfaceMissingInstanceTest, Test6_0) {
5941      auto&& [testName, footer] = GetParam();
5942      std::string header = R"(<manifest version=")" + to_string(kMetaVersionNoHalInterfaceInstance) +
5943                           R"(" type="device">)";
5944      std::string xml = header + footer;
5945      HalManifest vm;
5946      std::string error;
5947      EXPECT_FALSE(fromXml(&vm, xml, &error));
5948      EXPECT_THAT(error, HasSubstr("<hal> android.hardware.nfc <interface> INfc has no <instance>."));
5949  }
5950  
5951  INSTANTIATE_TEST_SUITE_P(LibVintfTest, InterfaceMissingInstanceTest,
5952                           ::testing::ValuesIn(InterfaceMissingInstanceTest::createParams()),
5953                           &InterfaceMissingInstanceTest::getTestSuffix);
5954  
5955  struct ManifestHalNoInstanceTestParam {
5956      HalFormat format;
5957      std::string footer;
5958  };
5959  
5960  class ManifestHalNoInstanceTest
5961      : public LibVintfTest,
5962        public ::testing::WithParamInterface<ManifestHalNoInstanceTestParam> {
5963     public:
createParams()5964      static std::vector<ManifestHalNoInstanceTestParam> createParams() {
5965          std::vector<ManifestHalNoInstanceTestParam> ret;
5966  
5967          std::string hidlFooter = R"(
5968      <hal>
5969          <name>android.hardware.nfc</name>
5970          <transport>hwbinder</transport>
5971          <version>1.0</version>
5972      </hal>
5973  </manifest>
5974  )";
5975          std::string aidlFooter = R"(
5976      <hal format="aidl">
5977          <name>android.hardware.nfc</name>
5978      </hal>
5979  </manifest>
5980  )";
5981  
5982          return {{HalFormat::HIDL, hidlFooter}, {HalFormat::AIDL, aidlFooter}};
5983      }
getTestSuffix(const TestParamInfo<ParamType> & info)5984      static std::string getTestSuffix(const TestParamInfo<ParamType>& info) {
5985          return to_string(info.param.format);
5986      }
5987  };
5988  
TEST_P(ManifestHalNoInstanceTest,Test5_0)5989  TEST_P(ManifestHalNoInstanceTest, Test5_0) {
5990      auto&& [testName, footer] = GetParam();
5991      std::string header = R"(<manifest version="5.0" type="device">)";
5992      std::string xml = header + footer;
5993      HalManifest vm;
5994      std::string error;
5995      EXPECT_TRUE(fromXml(&vm, xml, &error)) << error;
5996  }
5997  
TEST_P(ManifestHalNoInstanceTest,Test6_0)5998  TEST_P(ManifestHalNoInstanceTest, Test6_0) {
5999      auto&& [testName, footer] = GetParam();
6000      std::string header = R"(<manifest version=")" + to_string(kMetaVersionNoHalInterfaceInstance) +
6001                           R"(" type="device">)";
6002      std::string xml = header + footer;
6003      HalManifest vm;
6004      std::string error;
6005      EXPECT_FALSE(fromXml(&vm, xml, &error));
6006      EXPECT_THAT(error,
6007                  HasSubstr("<hal> android.hardware.nfc has no instance. Fix by adding <fqname>."));
6008  }
6009  
6010  INSTANTIATE_TEST_SUITE_P(LibVintfTest, ManifestHalNoInstanceTest,
6011                           ::testing::ValuesIn(ManifestHalNoInstanceTest::createParams()),
6012                           &ManifestHalNoInstanceTest::getTestSuffix);
6013  
6014  // clang-format off
6015  
6016  struct FrameworkCompatibilityMatrixCombineTest : public LibVintfTest {
SetUpandroid::vintf::FrameworkCompatibilityMatrixCombineTest6017      virtual void SetUp() override {
6018          matrices.resize(2);
6019          matrices[0].setFileName("compatibility_matrix.1_1.xml");
6020          matrices[1].setFileName("compatibility_matrix.1_2.xml");
6021      }
6022      // Access to private methods.
combineandroid::vintf::FrameworkCompatibilityMatrixCombineTest6023      std::unique_ptr<CompatibilityMatrix> combine(Level deviceLevel,
6024                                                   std::vector<CompatibilityMatrix>* theMatrices,
6025                                                   std::string* errorPtr) {
6026          return CompatibilityMatrix::combine(deviceLevel, Level::UNSPECIFIED, theMatrices, errorPtr);
6027      }
combineandroid::vintf::FrameworkCompatibilityMatrixCombineTest6028      std::unique_ptr<CompatibilityMatrix> combine(Level deviceLevel, Level kernellevel,
6029                                                   std::vector<CompatibilityMatrix>* theMatrices,
6030                                                   std::string* errorPtr) {
6031          return CompatibilityMatrix::combine(deviceLevel, kernellevel, theMatrices, errorPtr);
6032      }
6033  
6034      std::vector<CompatibilityMatrix> matrices;
6035      std::string error;
6036  };
6037  
6038  // Combining framework compatibility matrix with conflicting minlts fails
TEST_F(FrameworkCompatibilityMatrixCombineTest,ConflictMinlts)6039  TEST_F(FrameworkCompatibilityMatrixCombineTest, ConflictMinlts) {
6040      ASSERT_TRUE(fromXml(
6041          &matrices[0],
6042          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
6043          "    <kernel version=\"3.18.5\" />\n"
6044          "</compatibility-matrix>\n",
6045          &error))
6046          << error;
6047      ASSERT_TRUE(fromXml(
6048          &matrices[1],
6049          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
6050          "    <kernel version=\"3.18.6\" />\n"
6051          "</compatibility-matrix>\n",
6052          &error))
6053          << error;
6054  
6055      auto combined = combine(Level{1}, &matrices, &error);
6056      ASSERT_EQ(nullptr, combined) << toXml(*combined);
6057      EXPECT_IN("Kernel version mismatch", error);
6058  }
6059  
6060  // <kernel> without <conditions> always comes first
TEST_F(FrameworkCompatibilityMatrixCombineTest,KernelNoConditions)6061  TEST_F(FrameworkCompatibilityMatrixCombineTest, KernelNoConditions) {
6062      std::string conditionedKernel =
6063          "    <kernel version=\"3.18.5\" level=\"1\">\n"
6064          "        <conditions>\n"
6065          "            <config>\n"
6066          "                <key>CONFIG_ARM</key>\n"
6067          "                <value type=\"tristate\">y</value>\n"
6068          "            </config>\n"
6069          "        </conditions>\n"
6070          "        <config>\n"
6071          "            <key>CONFIG_FOO</key>\n"
6072          "            <value type=\"tristate\">y</value>\n"
6073          "        </config>\n"
6074          "    </kernel>\n";
6075      std::string simpleKernel =
6076          "    <kernel version=\"3.18.5\" level=\"1\">\n"
6077          "        <config>\n"
6078          "            <key>CONFIG_BAR</key>\n"
6079          "            <value type=\"tristate\">y</value>\n"
6080          "        </config>\n"
6081          "    </kernel>\n";
6082  
6083      ASSERT_TRUE(fromXml(
6084          &matrices[0],
6085          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
6086          "    <kernel version=\"3.18.5\" />\n" +
6087              conditionedKernel + "</compatibility-matrix>\n",
6088          &error))
6089          << error;
6090      ASSERT_TRUE(fromXml(
6091          &matrices[1],
6092          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n" + simpleKernel +
6093              "</compatibility-matrix>\n",
6094          &error))
6095          << error;
6096  
6097      auto combined = combine(Level{1}, &matrices, &error);
6098      ASSERT_NE(nullptr, combined);
6099      EXPECT_EQ("", error);
6100      EXPECT_EQ("<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n" +
6101                    simpleKernel + conditionedKernel + "</compatibility-matrix>\n",
6102                toXml(*combined));
6103  }
6104  
6105  // Combining framework compatibility matrix with conflicting sepolicy fails
TEST_F(FrameworkCompatibilityMatrixCombineTest,ConflictSepolicy)6106  TEST_F(FrameworkCompatibilityMatrixCombineTest, ConflictSepolicy) {
6107      ASSERT_TRUE(fromXml(
6108          &matrices[0],
6109          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
6110          "    <sepolicy>\n"
6111          "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
6112          "    </sepolicy>\n"
6113          "</compatibility-matrix>\n",
6114          &error))
6115          << error;
6116      ASSERT_TRUE(fromXml(
6117          &matrices[1],
6118          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
6119          "    <sepolicy>\n"
6120          "        <kernel-sepolicy-version>29</kernel-sepolicy-version>\n"
6121          "    </sepolicy>\n"
6122          "</compatibility-matrix>\n",
6123          &error))
6124          << error;
6125  
6126      auto combined = combine(Level{1}, &matrices, &error);
6127      ASSERT_EQ(nullptr, combined) << toXml(*combined);
6128      EXPECT_IN("<sepolicy> is already defined", error);
6129  }
6130  
6131  // Combining framework compatibility matrix with conflicting avb fails
TEST_F(FrameworkCompatibilityMatrixCombineTest,ConflictAvb)6132  TEST_F(FrameworkCompatibilityMatrixCombineTest, ConflictAvb) {
6133      ASSERT_TRUE(fromXml(
6134          &matrices[0],
6135          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
6136          "    <avb>\n"
6137          "        <vbmeta-version>1.1</vbmeta-version>\n"
6138          "    </avb>\n"
6139          "</compatibility-matrix>\n",
6140          &error))
6141          << error;
6142      ASSERT_TRUE(fromXml(
6143          &matrices[1],
6144          "<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"
6145          "    <avb>\n"
6146          "        <vbmeta-version>1.0</vbmeta-version>\n"
6147          "    </avb>\n"
6148          "</compatibility-matrix>\n",
6149          &error))
6150          << error;
6151  
6152      auto combined = combine(Level{1}, &matrices, &error);
6153      ASSERT_EQ(nullptr, combined) << toXml(*combined);
6154      EXPECT_IN("<avb><vbmeta-version> is already defined", error);
6155  }
6156  
TEST_F(FrameworkCompatibilityMatrixCombineTest,AidlAndHidlNames)6157  TEST_F(FrameworkCompatibilityMatrixCombineTest, AidlAndHidlNames) {
6158      std::string head1{"<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"1\">\n"};
6159      std::string head2{"<compatibility-matrix " + kMetaVersionStr + " type=\"framework\" level=\"2\">\n"};
6160      std::string tail{"</compatibility-matrix>\n"};
6161      std::string aidl =
6162          "    <hal format=\"aidl\" optional=\"false\">\n"
6163          "        <name>android.system.foo</name>\n"
6164          "        <interface>\n"
6165          "            <name>IFoo</name>\n"
6166          "            <instance>default</instance>\n"
6167          "        </interface>\n"
6168          "    </hal>\n";
6169      std::string hidl =
6170          "    <hal format=\"hidl\" optional=\"false\">\n"
6171          "        <name>android.system.foo</name>\n"
6172          "        <version>1.0</version>\n"
6173          "        <interface>\n"
6174          "            <name>IFoo</name>\n"
6175          "            <instance>default</instance>\n"
6176          "        </interface>\n"
6177          "    </hal>\n";
6178      std::string aidlOptional = std::string(aidl).replace(hidl.find("false"), 5, "true");
6179      std::string hidlOptional = std::string(hidl).replace(hidl.find("false"), 5, "true");
6180      std::string error;
6181      {
6182          ASSERT_TRUE(fromXml(&matrices[0], head1 + aidl + tail, &error))
6183              << error;
6184          ASSERT_TRUE(fromXml(&matrices[1], head1 + hidl + tail, &error))
6185              << error;
6186  
6187          auto combined = combine(Level{1}, &matrices, &error);
6188          ASSERT_NE(nullptr, combined) << error;
6189  
6190          auto combinedXml = toXml(*combined);
6191          EXPECT_IN(aidl, combinedXml);
6192          EXPECT_IN(hidl, combinedXml);
6193      }
6194      {
6195          ASSERT_TRUE(fromXml(&matrices[0], head1 + aidl + tail, &error))
6196              << error;
6197          ASSERT_TRUE(fromXml(&matrices[1], head2 + hidl + tail, &error))
6198              << error;
6199  
6200          auto combined = combine(Level{1}, &matrices, &error);
6201          ASSERT_NE(nullptr, combined) << error;
6202  
6203          auto combinedXml = toXml(*combined);
6204          EXPECT_IN(aidl, combinedXml);
6205          EXPECT_IN(hidlOptional, combinedXml);
6206      }
6207      {
6208          ASSERT_TRUE(fromXml(&matrices[0], head2 + aidl + tail, &error))
6209              << error;
6210          ASSERT_TRUE(fromXml(&matrices[1], head1 + hidl + tail, &error))
6211              << error;
6212  
6213          auto combined = combine(Level{1}, &matrices, &error);
6214          ASSERT_NE(nullptr, combined) << error;
6215  
6216          auto combinedXml = toXml(*combined);
6217          EXPECT_IN(aidlOptional, combinedXml);
6218          EXPECT_IN(hidl, combinedXml);
6219      }
6220  }
6221  
6222  // clang-format on
6223  
6224  class FcmCombineKernelTest : public FrameworkCompatibilityMatrixCombineTest,
6225                               public ::testing::WithParamInterface<std::tuple<size_t, size_t>> {
6226     public:
PrintTestParams(const TestParamInfo<FcmCombineKernelTest::ParamType> & info)6227      static std::string PrintTestParams(const TestParamInfo<FcmCombineKernelTest::ParamType>& info) {
6228          auto [deviceLevelNum, kernelLevelNum] = info.param;
6229          return "device_" + std::to_string(deviceLevelNum) + "_kernel_" +
6230                 std::to_string(kernelLevelNum);
6231      }
6232      static constexpr size_t kMinLevel = 1;
6233      static constexpr size_t kMaxLevel = 5;
6234  };
6235  
TEST_P(FcmCombineKernelTest,OlderKernel)6236  TEST_P(FcmCombineKernelTest, OlderKernel) {
6237      auto [deviceLevelNum, kernelLevelNum] = GetParam();
6238  
6239      std::vector<size_t> levelNums;
6240      for (size_t i = kMinLevel; i <= kMaxLevel; ++i) levelNums.push_back(i);
6241  
6242      constexpr auto fmt = R"(
6243          <compatibility-matrix %s type="framework" level="%s">
6244              <hal format="hidl" optional="false">
6245                  <name>android.system.foo</name>
6246                  <version>%zu.0</version>
6247                  <interface>
6248                      <name>IFoo</name>
6249                      <instance>default</instance>
6250                  </interface>
6251              </hal>
6252              <kernel version="%zu.0.0">
6253                  <config>
6254                      <key>CONFIG_%zu</key>
6255                      <value type="tristate">y</value>
6256                  </config>
6257              </kernel>
6258          </compatibility-matrix>
6259      )";
6260      std::string error;
6261      std::vector<CompatibilityMatrix> matrices;
6262      for (size_t levelNum : levelNums) {
6263          auto levelStr = android::vintf::to_string((Level)levelNum);
6264          auto xml = StringPrintf(fmt, kMetaVersionStr.c_str(), levelStr.c_str(), levelNum, levelNum,
6265                                  levelNum);
6266          CompatibilityMatrix& matrix = matrices.emplace_back();
6267          ASSERT_TRUE(fromXml(&matrix, xml, &error)) << error;
6268      }
6269      ASSERT_FALSE(matrices.empty());
6270  
6271      auto combined = combine(Level(deviceLevelNum), Level(kernelLevelNum), &matrices, &error);
6272      ASSERT_NE(nullptr, combined);
6273      auto combinedXml = toXml(*combined);
6274  
6275      // Check that HALs are combined correctly.
6276      for (size_t i = kMinLevel; i < deviceLevelNum; ++i)
6277          EXPECT_THAT(combinedXml, Not(HasSubstr(StringPrintf("<version>%zu.0</version>", i))));
6278  
6279      for (size_t i = deviceLevelNum; i <= kMaxLevel; ++i)
6280          EXPECT_THAT(combinedXml, HasSubstr(StringPrintf("<version>%zu.0</version>", i)));
6281  
6282      // Check that kernels are combined correctly. <kernel> tags from
6283      // matrices with level >= min(kernelLevel, deviceLevel) are added.
6284      // The "level" tag on <kernel> must also be set properly so that old kernel requirements from
6285      // deviceLevel <= x < kernelLevel won't be used.
6286      auto hasKernelFrom = std::min(kernelLevelNum, deviceLevelNum);
6287      for (size_t i = kMinLevel; i < hasKernelFrom; ++i) {
6288          EXPECT_THAT(combinedXml,
6289                      Not(HasSubstr(StringPrintf(R"(<kernel version="%zu.0.0" level="%zu")", i, i))));
6290          EXPECT_THAT(combinedXml, Not(HasSubstr(StringPrintf("CONFIG_%zu", i))));
6291      }
6292  
6293      for (size_t i = hasKernelFrom; i <= kMaxLevel; ++i) {
6294          EXPECT_THAT(combinedXml,
6295                      HasSubstr(StringPrintf(R"(<kernel version="%zu.0.0" level="%zu")", i, i)));
6296          EXPECT_THAT(combinedXml, HasSubstr(StringPrintf("CONFIG_%zu", i)));
6297      }
6298  
6299      if (::testing::Test::HasFailure()) ADD_FAILURE() << "Resulting matrix is \n" << combinedXml;
6300  }
6301  
6302  INSTANTIATE_TEST_CASE_P(
6303      FrameworkCompatibilityMatrixCombineTest, FcmCombineKernelTest,
6304      Combine(Range(FcmCombineKernelTest::kMinLevel, FcmCombineKernelTest::kMaxLevel + 1),
6305              Range(FcmCombineKernelTest::kMinLevel, FcmCombineKernelTest::kMaxLevel + 1)),
6306      FcmCombineKernelTest::PrintTestParams);
6307  
6308  // clang-format off
6309  
6310  struct DeviceCompatibilityMatrixCombineTest : public LibVintfTest {
SetUpandroid::vintf::DeviceCompatibilityMatrixCombineTest6311      virtual void SetUp() override {
6312          matrices.resize(2);
6313          matrices[0].setFileName("compatibility_matrix.1.xml");
6314          matrices[1].setFileName("compatibility_matrix.2.xml");
6315      }
6316      // Access to private methods.
combineandroid::vintf::DeviceCompatibilityMatrixCombineTest6317      std::unique_ptr<CompatibilityMatrix> combine(std::vector<CompatibilityMatrix>* theMatrices,
6318                                                   std::string* errorPtr) {
6319          return CompatibilityMatrix::combineDeviceMatrices(theMatrices, errorPtr);
6320      }
6321  
6322      std::vector<CompatibilityMatrix> matrices;
6323      std::string error;
6324  };
6325  
TEST_F(DeviceCompatibilityMatrixCombineTest,Success)6326  TEST_F(DeviceCompatibilityMatrixCombineTest, Success) {
6327      std::string head{"<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"};
6328      std::string tail{"</compatibility-matrix>\n"};
6329      std::string halFoo{
6330          "    <hal format=\"hidl\" optional=\"false\">\n"
6331          "        <name>android.hardware.foo</name>\n"
6332          "        <version>1.0</version>\n"
6333          "        <interface>\n"
6334          "            <name>IFoo</name>\n"
6335          "            <instance>default</instance>\n"
6336          "        </interface>\n"
6337          "    </hal>\n"};
6338      std::string halBar{
6339          "    <hal format=\"hidl\" optional=\"false\">\n"
6340          "        <name>android.hardware.bar</name>\n"
6341          "        <version>1.0</version>\n"
6342          "        <interface>\n"
6343          "            <name>IBar</name>\n"
6344          "            <instance>default</instance>\n"
6345          "        </interface>\n"
6346          "    </hal>\n"};
6347      ASSERT_TRUE(fromXml(&matrices[0], head + halFoo + tail, &error))
6348          << error;
6349      ASSERT_TRUE(fromXml(&matrices[1], head + halBar + tail, &error))
6350          << error;
6351  
6352      auto combined = combine(&matrices, &error);
6353      ASSERT_NE(nullptr, combined) << error;
6354      EXPECT_EQ("", error);
6355      auto combinedXml = toXml(*combined);
6356      EXPECT_IN(halFoo, combinedXml);
6357      EXPECT_IN(halBar, combinedXml);
6358  }
6359  
TEST_F(DeviceCompatibilityMatrixCombineTest,ConflictVendorNdk)6360  TEST_F(DeviceCompatibilityMatrixCombineTest, ConflictVendorNdk) {
6361      std::string vendorNdkP{
6362          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
6363          "    <vendor-ndk>\n"
6364          "        <version>P</version>\n"
6365          "    </vendor-ndk>\n"
6366          "</compatibility-matrix>\n"};
6367      std::string vendorNdkQ{
6368          "<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"
6369          "    <vendor-ndk>\n"
6370          "        <version>Q</version>\n"
6371          "    </vendor-ndk>\n"
6372          "</compatibility-matrix>\n"};
6373      ASSERT_TRUE(fromXml(&matrices[0], vendorNdkP, &error)) << error;
6374      ASSERT_TRUE(fromXml(&matrices[1], vendorNdkQ, &error)) << error;
6375  
6376      auto combined = combine(&matrices, &error);
6377      ASSERT_EQ(nullptr, combined) << toXml(*combined);
6378      EXPECT_IN("<vendor-ndk> is already defined", error);
6379  }
6380  
TEST_F(DeviceCompatibilityMatrixCombineTest,AidlAndHidlNames)6381  TEST_F(DeviceCompatibilityMatrixCombineTest, AidlAndHidlNames) {
6382      std::string head{"<compatibility-matrix " + kMetaVersionStr + " type=\"device\">\n"};
6383      std::string tail{"</compatibility-matrix>\n"};
6384      std::string aidl =
6385          "    <hal format=\"aidl\" optional=\"true\">\n"
6386          "        <name>android.system.foo</name>\n"
6387          "        <interface>\n"
6388          "            <name>IFoo</name>\n"
6389          "            <instance>default</instance>\n"
6390          "        </interface>\n"
6391          "    </hal>\n";
6392      std::string hidl =
6393          "    <hal format=\"hidl\" optional=\"true\">\n"
6394          "        <name>android.system.foo</name>\n"
6395          "        <version>1.0</version>\n"
6396          "        <interface>\n"
6397          "            <name>IFoo</name>\n"
6398          "            <instance>default</instance>\n"
6399          "        </interface>\n"
6400          "    </hal>\n";
6401      ASSERT_TRUE(fromXml(&matrices[0], head + aidl + tail, &error))
6402          << error;
6403      ASSERT_TRUE(fromXml(&matrices[1], head + hidl + tail, &error))
6404          << error;
6405  
6406      auto combined = combine(&matrices, &error);
6407      ASSERT_NE(nullptr, combined) << error;
6408  
6409      auto combinedXml = toXml(*combined);
6410      EXPECT_IN(aidl, combinedXml);
6411      EXPECT_IN(hidl, combinedXml);
6412  }
6413  
6414  // clang-format on
6415  
6416  } // namespace vintf
6417  } // namespace android
6418  
main(int argc,char ** argv)6419  int main(int argc, char **argv) {
6420      ::testing::InitGoogleMock(&argc, argv);
6421      return RUN_ALL_TESTS();
6422  }
6423