• 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 #include <android-base/logging.h>
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 
24 #include <android-base/strings.h>
25 #include <vintf/VintfObject.h>
26 #include <vintf/parse_string.h>
27 #include "utils-fake.h"
28 
29 #include <hidl-util/FQName.h>
30 
31 using namespace ::testing;
32 
33 using android::FqInstance;
34 
In(const std::string & sub,const std::string & str)35 static AssertionResult In(const std::string& sub, const std::string& str) {
36     return (str.find(sub) != std::string::npos ? AssertionSuccess() : AssertionFailure())
37            << "Value is " << str;
38 }
39 #define EXPECT_IN(sub, str) EXPECT_TRUE(In((sub), (str)))
40 #define EXPECT_NOT_IN(sub, str) EXPECT_FALSE(In((sub), (str)))
41 
42 namespace android {
43 namespace vintf {
44 namespace testing {
45 
46 using namespace ::android::vintf::details;
47 
48 //
49 // Set of Xml1 metadata compatible with each other.
50 //
51 
52 const std::string systemMatrixXml1 =
53     "<compatibility-matrix version=\"1.0\" type=\"framework\">\n"
54     "    <hal format=\"hidl\" optional=\"false\">\n"
55     "        <name>android.hardware.camera</name>\n"
56     "        <version>2.0-5</version>\n"
57     "        <version>3.4-16</version>\n"
58     "    </hal>\n"
59     "    <hal format=\"hidl\" optional=\"false\">\n"
60     "        <name>android.hardware.nfc</name>\n"
61     "        <version>1.0</version>\n"
62     "        <version>2.0</version>\n"
63     "    </hal>\n"
64     "    <hal format=\"hidl\" optional=\"true\">\n"
65     "        <name>android.hardware.foo</name>\n"
66     "        <version>1.0</version>\n"
67     "    </hal>\n"
68     "    <kernel version=\"3.18.31\"></kernel>\n"
69     "    <sepolicy>\n"
70     "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
71     "        <sepolicy-version>25.5</sepolicy-version>\n"
72     "        <sepolicy-version>26.0-3</sepolicy-version>\n"
73     "    </sepolicy>\n"
74     "    <avb>\n"
75     "        <vbmeta-version>0.0</vbmeta-version>\n"
76     "    </avb>\n"
77     "</compatibility-matrix>\n";
78 
79 const std::string vendorManifestXml1 =
80     "<manifest version=\"1.0\" type=\"device\">\n"
81     "    <hal format=\"hidl\">\n"
82     "        <name>android.hardware.camera</name>\n"
83     "        <transport>hwbinder</transport>\n"
84     "        <version>3.5</version>\n"
85     "        <interface>\n"
86     "            <name>IBetterCamera</name>\n"
87     "            <instance>camera</instance>\n"
88     "        </interface>\n"
89     "        <interface>\n"
90     "            <name>ICamera</name>\n"
91     "            <instance>default</instance>\n"
92     "            <instance>legacy/0</instance>\n"
93     "        </interface>\n"
94     "    </hal>\n"
95     "    <hal format=\"hidl\">\n"
96     "        <name>android.hardware.nfc</name>\n"
97     "        <transport>hwbinder</transport>\n"
98     "        <version>1.0</version>\n"
99     "        <interface>\n"
100     "            <name>INfc</name>\n"
101     "            <instance>nfc_nci</instance>\n"
102     "        </interface>\n"
103     "    </hal>\n"
104     "    <hal format=\"hidl\">\n"
105     "        <name>android.hardware.nfc</name>\n"
106     "        <transport>hwbinder</transport>\n"
107     "        <version>2.0</version>\n"
108     "        <interface>\n"
109     "            <name>INfc</name>\n"
110     "            <instance>default</instance>\n"
111     "            <instance>nfc_nci</instance>\n"
112     "        </interface>\n"
113     "    </hal>\n"
114     "    <sepolicy>\n"
115     "        <version>25.5</version>\n"
116     "    </sepolicy>\n"
117     "</manifest>\n";
118 
119 const std::string systemManifestXml1 =
120     "<manifest version=\"1.0\" type=\"framework\">\n"
121     "    <hal format=\"hidl\">\n"
122     "        <name>android.hidl.manager</name>\n"
123     "        <transport>hwbinder</transport>\n"
124     "        <version>1.0</version>\n"
125     "        <interface>\n"
126     "            <name>IServiceManager</name>\n"
127     "            <instance>default</instance>\n"
128     "        </interface>\n"
129     "    </hal>\n"
130     "    <vndk>\n"
131     "        <version>25.0.5</version>\n"
132     "        <library>libbase.so</library>\n"
133     "        <library>libjpeg.so</library>\n"
134     "    </vndk>\n"
135     "</manifest>\n";
136 
137 const std::string vendorMatrixXml1 =
138     "<compatibility-matrix version=\"1.0\" type=\"device\">\n"
139     "    <hal format=\"hidl\" optional=\"false\">\n"
140     "        <name>android.hidl.manager</name>\n"
141     "        <version>1.0</version>\n"
142     "    </hal>\n"
143     "    <vndk>\n"
144     "        <version>25.0.1-5</version>\n"
145     "        <library>libbase.so</library>\n"
146     "        <library>libjpeg.so</library>\n"
147     "    </vndk>\n"
148     "</compatibility-matrix>\n";
149 
150 //
151 // Set of Xml2 metadata compatible with each other.
152 //
153 
154 const std::string systemMatrixXml2 =
155     "<compatibility-matrix version=\"1.0\" type=\"framework\">\n"
156     "    <hal format=\"hidl\">\n"
157     "        <name>android.hardware.foo</name>\n"
158     "        <version>1.0</version>\n"
159     "    </hal>\n"
160     "    <kernel version=\"3.18.31\"></kernel>\n"
161     "    <sepolicy>\n"
162     "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
163     "        <sepolicy-version>25.5</sepolicy-version>\n"
164     "        <sepolicy-version>26.0-3</sepolicy-version>\n"
165     "    </sepolicy>\n"
166     "    <avb>\n"
167     "        <vbmeta-version>0.0</vbmeta-version>\n"
168     "    </avb>\n"
169     "</compatibility-matrix>\n";
170 
171 const std::string vendorManifestXml2 =
172     "<manifest version=\"1.0\" type=\"device\">"
173     "    <hal>"
174     "        <name>android.hardware.foo</name>"
175     "        <transport>hwbinder</transport>"
176     "        <version>1.0</version>"
177     "    </hal>"
178     "    <sepolicy>\n"
179     "        <version>25.5</version>\n"
180     "    </sepolicy>\n"
181     "</manifest>";
182 
183 //
184 // Set of framework matrices of different FCM version.
185 //
186 
187 const std::string systemMatrixLevel1 =
188     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"1\">\n"
189     "    <hal format=\"hidl\" optional=\"true\">\n"
190     "        <name>android.hardware.major</name>\n"
191     "        <version>1.0</version>\n"
192     "        <interface>\n"
193     "            <name>IMajor</name>\n"
194     "            <instance>default</instance>\n"
195     "        </interface>\n"
196     "    </hal>\n"
197     "    <hal format=\"hidl\" optional=\"true\">\n"
198     "        <name>android.hardware.removed</name>\n"
199     "        <version>1.0</version>\n"
200     "        <interface>\n"
201     "            <name>IRemoved</name>\n"
202     "            <instance>default</instance>\n"
203     "        </interface>\n"
204     "    </hal>\n"
205     "    <hal format=\"hidl\" optional=\"true\">\n"
206     "        <name>android.hardware.minor</name>\n"
207     "        <version>1.0</version>\n"
208     "        <interface>\n"
209     "            <name>IMinor</name>\n"
210     "            <instance>default</instance>\n"
211     "            <instance>legacy</instance>\n"
212     "        </interface>\n"
213     "    </hal>\n"
214     "</compatibility-matrix>\n";
215 
216 const std::string systemMatrixLevel2 =
217     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"2\">\n"
218     "    <hal format=\"hidl\" optional=\"true\">\n"
219     "        <name>android.hardware.major</name>\n"
220     "        <version>2.0</version>\n"
221     "        <interface>\n"
222     "            <name>IMajor</name>\n"
223     "            <instance>default</instance>\n"
224     "        </interface>\n"
225     "    </hal>\n"
226     "    <hal format=\"hidl\" optional=\"true\">\n"
227     "        <name>android.hardware.minor</name>\n"
228     "        <version>1.1</version>\n"
229     "        <interface>\n"
230     "            <name>IMinor</name>\n"
231     "            <instance>default</instance>\n"
232     "        </interface>\n"
233     "    </hal>\n"
234     "</compatibility-matrix>\n";
235 
236 //
237 // Set of framework matrices of different FCM version with regex.
238 //
239 
240 const static std::vector<std::string> systemMatrixRegexXmls = {
241     // 1.xml
242     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"1\">\n"
243     "    <hal format=\"hidl\" optional=\"false\">\n"
244     "        <name>android.hardware.regex</name>\n"
245     "        <version>1.0-1</version>\n"
246     "        <interface>\n"
247     "            <name>IRegex</name>\n"
248     "            <instance>default</instance>\n"
249     "            <instance>special/1.0</instance>\n"
250     "            <regex-instance>regex/1.0/[0-9]+</regex-instance>\n"
251     "            <regex-instance>regex_common/[0-9]+</regex-instance>\n"
252     "        </interface>\n"
253     "    </hal>\n"
254     "</compatibility-matrix>\n",
255     // 2.xml
256     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"2\">\n"
257     "    <hal format=\"hidl\" optional=\"false\">\n"
258     "        <name>android.hardware.regex</name>\n"
259     "        <version>1.1-2</version>\n"
260     "        <interface>\n"
261     "            <name>IRegex</name>\n"
262     "            <instance>default</instance>\n"
263     "            <instance>special/1.1</instance>\n"
264     "            <regex-instance>regex/1.1/[0-9]+</regex-instance>\n"
265     "            <regex-instance>[a-z]+_[a-z]+/[0-9]+</regex-instance>\n"
266     "        </interface>\n"
267     "    </hal>\n"
268     "</compatibility-matrix>\n",
269     // 3.xml
270     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"3\">\n"
271     "    <hal format=\"hidl\" optional=\"false\">\n"
272     "        <name>android.hardware.regex</name>\n"
273     "        <version>2.0</version>\n"
274     "        <interface>\n"
275     "            <name>IRegex</name>\n"
276     "            <instance>default</instance>\n"
277     "            <instance>special/2.0</instance>\n"
278     "            <regex-instance>regex/2.0/[0-9]+</regex-instance>\n"
279     "            <regex-instance>regex_[a-z]+/[0-9]+</regex-instance>\n"
280     "        </interface>\n"
281     "    </hal>\n"
282     "</compatibility-matrix>\n"};
283 
284 //
285 // Set of metadata at different FCM version that has requirements
286 //
287 
288 const std::vector<std::string> systemMatrixRequire = {
289     // 1.xml
290     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"1\">\n"
291     "    <hal format=\"hidl\" optional=\"false\">\n"
292     "        <name>android.hardware.foo</name>\n"
293     "        <version>1.0</version>\n"
294     "        <interface>\n"
295     "            <name>IFoo</name>\n"
296     "            <instance>default</instance>\n"
297     "        </interface>\n"
298     "    </hal>\n"
299     "</compatibility-matrix>\n",
300     // 2.xml
301     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"2\">\n"
302     "    <hal format=\"hidl\" optional=\"false\">\n"
303     "        <name>android.hardware.bar</name>\n"
304     "        <version>1.0</version>\n"
305     "        <interface>\n"
306     "            <name>IBar</name>\n"
307     "            <instance>default</instance>\n"
308     "        </interface>\n"
309     "    </hal>\n"
310     "</compatibility-matrix>\n"};
311 
312 const std::string vendorManifestRequire1 =
313     "<manifest version=\"1.0\" type=\"device\" target-level=\"1\">\n"
314     "    <hal format=\"hidl\">\n"
315     "        <name>android.hardware.foo</name>\n"
316     "        <transport>hwbinder</transport>\n"
317     "        <fqname>@1.0::IFoo/default</fqname>\n"
318     "    </hal>\n"
319     "</manifest>\n";
320 
321 const std::string vendorManifestRequire2 =
322     "<manifest version=\"1.0\" type=\"device\" target-level=\"2\">\n"
323     "    <hal format=\"hidl\">\n"
324     "        <name>android.hardware.bar</name>\n"
325     "        <transport>hwbinder</transport>\n"
326     "        <fqname>@1.0::IBar/default</fqname>\n"
327     "    </hal>\n"
328     "</manifest>\n";
329 
330 //
331 // Set of metadata for kernel requirements
332 //
333 
334 const std::string vendorManifestKernel318 =
335     "<manifest version=\"1.0\" type=\"device\">\n"
336     "    <kernel version=\"3.18.999\" />\n"
337     "    <sepolicy>\n"
338     "        <version>25.5</version>\n"
339     "    </sepolicy>\n"
340     "</manifest>\n";
341 
342 const std::string systemMatrixKernel318 =
343     "<compatibility-matrix version=\"1.0\" type=\"framework\">\n"
344     "    <kernel version=\"3.18.999\"></kernel>\n"
345     "    <sepolicy>\n"
346     "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
347     "        <sepolicy-version>25.5</sepolicy-version>\n"
348     "    </sepolicy>\n"
349     "</compatibility-matrix>\n";
350 
351 class VintfObjectTestBase : public ::testing::Test {
352    protected:
fetcher()353     MockFileSystem& fetcher() {
354         return static_cast<MockFileSystem&>(*vintfObject->getFileSystem());
355     }
propertyFetcher()356     MockPropertyFetcher& propertyFetcher() {
357         return static_cast<MockPropertyFetcher&>(*vintfObject->getPropertyFetcher());
358     }
359 
360     // Setup the MockFileSystem used by the fetchAllInformation template
361     // so it returns the given metadata info instead of fetching from device.
setupMockFetcher(const std::string & vendorManifestXml,const std::string & systemMatrixXml,const std::string & systemManifestXml,const std::string & vendorMatrixXml,const std::string & productModel)362     void setupMockFetcher(const std::string& vendorManifestXml, const std::string& systemMatrixXml,
363                           const std::string& systemManifestXml, const std::string& vendorMatrixXml,
364                           const std::string& productModel) {
365         ON_CALL(fetcher(), listFiles(StrEq(kVendorManifestFragmentDir), _, _))
366             .WillByDefault(Return(::android::OK));
367         ON_CALL(fetcher(), listFiles(StrEq(kSystemManifestFragmentDir), _, _))
368             .WillByDefault(Return(::android::OK));
369         ON_CALL(fetcher(), listFiles(StrEq(kOdmManifestFragmentDir), _, _))
370             .WillByDefault(Return(::android::OK));
371         ON_CALL(fetcher(), listFiles(StrEq(kProductManifestFragmentDir), _, _))
372             .WillByDefault(Return(::android::OK));
373 
374         if (!productModel.empty()) {
375             ON_CALL(fetcher(),
376                     fetch(StrEq(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml"), _))
377                 .WillByDefault(Return(::android::NAME_NOT_FOUND));
378             ON_CALL(fetcher(), fetch(StrEq(kOdmVintfDir + "manifest_" + productModel + ".xml"), _))
379                 .WillByDefault(Return(::android::NAME_NOT_FOUND));
380         }
381         ON_CALL(fetcher(), fetch(StrEq(kOdmLegacyManifest), _))
382             .WillByDefault(Return(::android::NAME_NOT_FOUND));
383         ON_CALL(fetcher(), fetch(StrEq(kOdmManifest), _))
384             .WillByDefault(Return(::android::NAME_NOT_FOUND));
385         ON_CALL(fetcher(), fetch(StrEq(kVendorManifest), _))
386             .WillByDefault(Return(::android::NAME_NOT_FOUND));
387         ON_CALL(fetcher(), fetch(StrEq(kVendorLegacyManifest), _))
388             .WillByDefault(
389                 Invoke([vendorManifestXml](const std::string& path, std::string& fetched) {
390                     (void)path;
391                     fetched = vendorManifestXml;
392                     return 0;
393                 }));
394         ON_CALL(fetcher(), fetch(StrEq(kSystemManifest), _))
395             .WillByDefault(
396                 Invoke([systemManifestXml](const std::string& path, std::string& fetched) {
397                     (void)path;
398                     fetched = systemManifestXml;
399                     return 0;
400                 }));
401         ON_CALL(fetcher(), fetch(StrEq(kVendorMatrix), _))
402             .WillByDefault(Return(::android::NAME_NOT_FOUND));
403         ON_CALL(fetcher(), fetch(StrEq(kVendorLegacyMatrix), _))
404             .WillByDefault(Invoke([vendorMatrixXml](const std::string& path, std::string& fetched) {
405                 (void)path;
406                 fetched = vendorMatrixXml;
407                 return 0;
408             }));
409         ON_CALL(fetcher(), fetch(StrEq(kSystemLegacyMatrix), _))
410             .WillByDefault(Invoke([systemMatrixXml](const std::string& path, std::string& fetched) {
411                 (void)path;
412                 fetched = systemMatrixXml;
413                 return 0;
414             }));
415         // Don't list /system/etc/vintf unless otherwise specified.
416         ON_CALL(fetcher(), listFiles(StrEq(kSystemVintfDir), _, _))
417             .WillByDefault(Return(::android::OK));
418         // Don't fetch product matrix unless otherwise specified.
419         ON_CALL(fetcher(), fetch(StrEq(kProductMatrix), _)).WillByDefault(Return(NAME_NOT_FOUND));
420         // Don't fetch product manifest unless otherwise specified.
421         ON_CALL(fetcher(), fetch(StrEq(kProductManifest), _)).WillByDefault(Return(NAME_NOT_FOUND));
422     }
423 
setFakeProperties()424     void setFakeProperties() {
425         productModel = "fake_sku";
426         ON_CALL(propertyFetcher(), getProperty("ro.boot.product.hardware.sku", _))
427             .WillByDefault(Return(productModel));
428     }
429 
SetUp()430     virtual void SetUp() {
431         vintfObject = VintfObject::Builder()
432                           .setFileSystem(std::make_unique<NiceMock<MockFileSystem>>())
433                           .setRuntimeInfoFactory(std::make_unique<NiceMock<MockRuntimeInfoFactory>>(
434                               std::make_shared<NiceMock<MockRuntimeInfo>>()))
435                           .setPropertyFetcher(std::make_unique<NiceMock<MockPropertyFetcher>>())
436                           .build();
437     }
TearDown()438     virtual void TearDown() {
439         Mock::VerifyAndClear(&fetcher());
440     }
441 
expectVendorManifest(size_t times=1)442     void expectVendorManifest(size_t times = 1) {
443         EXPECT_CALL(fetcher(), fetch(StrEq(kVendorManifest), _)).Times(times);
444         if (!productModel.empty()) {
445             EXPECT_CALL(fetcher(),
446                         fetch(StrEq(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml"), _))
447                 .Times(times);
448             EXPECT_CALL(fetcher(),
449                         fetch(StrEq(kOdmVintfDir + "manifest_" + productModel + ".xml"), _))
450                 .Times(times);
451         }
452         EXPECT_CALL(fetcher(), fetch(StrEq(kOdmLegacyManifest), _)).Times(times);
453         EXPECT_CALL(fetcher(), fetch(StrEq(kOdmManifest), _)).Times(times);
454         EXPECT_CALL(fetcher(), fetch(StrEq(kVendorLegacyManifest), _)).Times(times);
455     }
456 
expectSystemManifest(size_t times=1)457     void expectSystemManifest(size_t times = 1) {
458         EXPECT_CALL(fetcher(), fetch(StrEq(kSystemManifest), _)).Times(times);
459         EXPECT_CALL(fetcher(), fetch(StrEq(kProductManifest), _)).Times(times);
460     }
461 
expectVendorMatrix(size_t times=1)462     void expectVendorMatrix(size_t times = 1) {
463         EXPECT_CALL(fetcher(), fetch(StrEq(kVendorMatrix), _)).Times(times);
464         EXPECT_CALL(fetcher(), fetch(StrEq(kVendorLegacyMatrix), _)).Times(times);
465     }
466 
expectSystemMatrix(size_t times=1)467     void expectSystemMatrix(size_t times = 1) {
468         EXPECT_CALL(fetcher(), fetch(StrEq(kProductMatrix), _)).Times(times);
469         EXPECT_CALL(fetcher(), fetch(StrEq(kSystemLegacyMatrix), _)).Times(times);
470     }
471 
472     // Expect that a file exist and should be fetched once.
expectFetch(const std::string & path,const std::string & content)473     void expectFetch(const std::string& path, const std::string& content) {
474         EXPECT_CALL(fetcher(), fetch(StrEq(path), _))
475             .WillOnce(Invoke([content](const auto&, auto& out) {
476                 out = content;
477                 return ::android::OK;
478             }));
479     }
480 
481     // Expect that a file exist and can be fetched 0 or more times.
expectFetchRepeatedly(const std::string & path,const std::string & content)482     void expectFetchRepeatedly(const std::string& path, const std::string& content) {
483         EXPECT_CALL(fetcher(), fetch(StrEq(path), _))
484             .Times(AnyNumber())
485             .WillRepeatedly(Invoke([content](const auto&, auto& out) {
486                 out = content;
487                 return ::android::OK;
488             }));
489     }
490 
491     // Expect that the file should never be fetched (whether it exists or not).
expectNeverFetch(const std::string & path)492     void expectNeverFetch(const std::string& path) {
493         EXPECT_CALL(fetcher(), fetch(StrEq(path), _)).Times(0);
494     }
495 
496     // Expect that the file does not exist, and can be fetched 0 or more times.
497     template <typename Matcher>
expectFileNotExist(const Matcher & matcher)498     void expectFileNotExist(const Matcher& matcher) {
499         EXPECT_CALL(fetcher(), fetch(matcher, _))
500             .Times(AnyNumber())
501             .WillRepeatedly(Return(::android::NAME_NOT_FOUND));
502     }
503 
504     // Access to private method.
checkCompatibility(const std::vector<std::string> & xmls,std::string * error)505     int checkCompatibility(const std::vector<std::string>& xmls, std::string* error) {
506         return vintfObject->checkCompatibility(xmls, error);
507     }
508 
509     std::string productModel;
510     std::unique_ptr<VintfObject> vintfObject;
511 };
512 
513 // Test fixture that provides compatible metadata from the mock device.
514 class VintfObjectCompatibleTest : public VintfObjectTestBase {
515    protected:
SetUp()516     virtual void SetUp() {
517         VintfObjectTestBase::SetUp();
518         setFakeProperties();
519         setupMockFetcher(vendorManifestXml1, systemMatrixXml1, systemManifestXml1, vendorMatrixXml1,
520                          productModel);
521     }
522 };
523 
524 // Tests that local info is checked.
TEST_F(VintfObjectCompatibleTest,TestDeviceCompatibility)525 TEST_F(VintfObjectCompatibleTest, TestDeviceCompatibility) {
526     std::string error;
527     std::vector<std::string> packageInfo;
528 
529     expectVendorManifest();
530     expectSystemManifest();
531     expectVendorMatrix();
532     expectSystemMatrix();
533 
534     int result = checkCompatibility(packageInfo, &error);
535 
536     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
537     // Check that nothing was ignored.
538     ASSERT_STREQ(error.c_str(), "");
539 }
540 
541 // Tests that input info is checked against device and passes.
TEST_F(VintfObjectCompatibleTest,TestInputVsDeviceSuccess)542 TEST_F(VintfObjectCompatibleTest, TestInputVsDeviceSuccess) {
543     std::string error;
544     std::vector<std::string> packageInfo = {systemMatrixXml1};
545 
546     expectVendorManifest();
547     expectSystemManifest();
548     expectVendorMatrix();
549     expectSystemMatrix(0);
550 
551     int result = checkCompatibility(packageInfo, &error);
552 
553     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
554     ASSERT_STREQ(error.c_str(), "");
555 }
556 
557 // Tests that input info is checked against device and fails.
TEST_F(VintfObjectCompatibleTest,TestInputVsDeviceFail)558 TEST_F(VintfObjectCompatibleTest, TestInputVsDeviceFail) {
559     std::string error;
560     std::vector<std::string> packageInfo = {systemMatrixXml2};
561 
562     int result = checkCompatibility(packageInfo, &error);
563 
564     ASSERT_EQ(result, 1) << "Should have failed:" << error.c_str();
565     EXPECT_IN(
566         "Device manifest and framework compatibility matrix are incompatible: HALs "
567         "incompatible.",
568         error);
569     EXPECT_IN("android.hardware.foo", error);
570 }
571 
572 // Tests that complementary info is checked against itself.
TEST_F(VintfObjectCompatibleTest,TestInputSuccess)573 TEST_F(VintfObjectCompatibleTest, TestInputSuccess) {
574     std::string error;
575     std::vector<std::string> packageInfo = {systemMatrixXml2, vendorManifestXml2};
576 
577     int result = checkCompatibility(packageInfo, &error);
578 
579     ASSERT_EQ(result, 0) << "Failed message:" << error.c_str();
580     ASSERT_STREQ(error.c_str(), "");
581 }
582 
TEST_F(VintfObjectCompatibleTest,TestFrameworkOnlyOta)583 TEST_F(VintfObjectCompatibleTest, TestFrameworkOnlyOta) {
584     std::string error;
585     std::vector<std::string> packageInfo = {systemMatrixXml1, systemManifestXml1};
586 
587     expectVendorManifest();
588     expectSystemManifest(0);
589     expectVendorMatrix();
590     expectSystemMatrix(0);
591 
592     int result = checkCompatibility(packageInfo, &error);
593 
594     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
595     ASSERT_STREQ(error.c_str(), "");
596 }
597 
TEST_F(VintfObjectCompatibleTest,TestFullOta)598 TEST_F(VintfObjectCompatibleTest, TestFullOta) {
599     std::string error;
600     std::vector<std::string> packageInfo = {systemMatrixXml1, systemManifestXml1,
601             vendorMatrixXml1, vendorManifestXml1};
602 
603     expectVendorManifest(0);
604     expectSystemManifest(0);
605     expectVendorMatrix(0);
606     expectSystemMatrix(0);
607 
608     int result = checkCompatibility(packageInfo, &error);
609 
610     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
611     ASSERT_STREQ(error.c_str(), "");
612 }
613 
614 // Test that framework-only OTA fails when kernel is not compatible with incoming system.
TEST_F(VintfObjectCompatibleTest,KernelInfoIncompatible)615 TEST_F(VintfObjectCompatibleTest, KernelInfoIncompatible) {
616     std::string error;
617     std::vector<std::string> packageInfo = {systemMatrixKernel318};
618 
619     int result = checkCompatibility(packageInfo, &error);
620 
621     ASSERT_EQ(result, INCOMPATIBLE) << "Should have failed.";
622     EXPECT_IN("Framework is incompatible with kernel version 3.18.31", error);
623 }
624 
625 // Test that full OTA is successful when the OTA package provides a compatible kernel.
TEST_F(VintfObjectCompatibleTest,UpdateKernel)626 TEST_F(VintfObjectCompatibleTest, UpdateKernel) {
627     std::string error;
628     std::vector<std::string> packageInfo = {vendorManifestKernel318, systemMatrixKernel318};
629 
630     int result = checkCompatibility(packageInfo, &error);
631 
632     ASSERT_EQ(result, COMPATIBLE) << "Fail message:" << error;
633     ASSERT_STREQ(error.c_str(), "");
634 }
635 
636 // Test fixture that provides incompatible metadata from the mock device.
637 class VintfObjectIncompatibleTest : public VintfObjectTestBase {
638    protected:
SetUp()639     virtual void SetUp() {
640         VintfObjectTestBase::SetUp();
641         setFakeProperties();
642         setupMockFetcher(vendorManifestXml1, systemMatrixXml2, systemManifestXml1, vendorMatrixXml1,
643                          productModel);
644     }
645 };
646 
647 // Fetch all metadata from device and ensure that it fails.
TEST_F(VintfObjectIncompatibleTest,TestDeviceCompatibility)648 TEST_F(VintfObjectIncompatibleTest, TestDeviceCompatibility) {
649     std::string error;
650     std::vector<std::string> packageInfo;
651 
652     expectVendorManifest();
653     expectSystemManifest();
654     expectVendorMatrix();
655     expectSystemMatrix();
656 
657     int result = checkCompatibility(packageInfo, &error);
658 
659     ASSERT_EQ(result, 1) << "Should have failed:" << error.c_str();
660 }
661 
662 // Pass in new metadata that fixes the incompatibility.
TEST_F(VintfObjectIncompatibleTest,TestInputVsDeviceSuccess)663 TEST_F(VintfObjectIncompatibleTest, TestInputVsDeviceSuccess) {
664     std::string error;
665     std::vector<std::string> packageInfo = {systemMatrixXml1};
666 
667     expectVendorManifest();
668     expectSystemManifest();
669     expectVendorMatrix();
670     expectSystemMatrix(0);
671 
672     int result = checkCompatibility(packageInfo, &error);
673 
674     ASSERT_EQ(result, 0) << "Failed message:" << error.c_str();
675     ASSERT_STREQ(error.c_str(), "");
676 }
677 
678 // Test fixture that provides compatible metadata from the mock device.
679 class VintfObjectRuntimeInfoTest : public VintfObjectTestBase {
680    protected:
SetUp()681     virtual void SetUp() {
682         VintfObjectTestBase::SetUp();
683         // clear fetch flags
684         runtimeInfoFactory().getInfo()->failNextFetch();
685         vintfObject->getRuntimeInfo(true /* skipCache */, RuntimeInfo::FetchFlag::ALL);
686     }
TearDown()687     virtual void TearDown() {
688         Mock::VerifyAndClear(&runtimeInfoFactory());
689         Mock::VerifyAndClear(runtimeInfoFactory().getInfo().get());
690     }
runtimeInfoFactory()691     MockRuntimeInfoFactory& runtimeInfoFactory() {
692         return static_cast<MockRuntimeInfoFactory&>(*vintfObject->getRuntimeInfoFactory());
693     }
694 };
695 
TEST_F(VintfObjectRuntimeInfoTest,GetRuntimeInfo)696 TEST_F(VintfObjectRuntimeInfoTest, GetRuntimeInfo) {
697     InSequence s;
698 
699     EXPECT_CALL(*runtimeInfoFactory().getInfo(),
700                 fetchAllInformation(RuntimeInfo::FetchFlag::CPU_VERSION));
701     EXPECT_CALL(*runtimeInfoFactory().getInfo(), fetchAllInformation(RuntimeInfo::FetchFlag::NONE));
702     EXPECT_CALL(*runtimeInfoFactory().getInfo(),
703                 fetchAllInformation(RuntimeInfo::FetchFlag::CPU_VERSION));
704     EXPECT_CALL(
705         *runtimeInfoFactory().getInfo(),
706         fetchAllInformation(RuntimeInfo::FetchFlag::ALL & ~RuntimeInfo::FetchFlag::CPU_VERSION));
707     EXPECT_CALL(*runtimeInfoFactory().getInfo(), fetchAllInformation(RuntimeInfo::FetchFlag::ALL));
708     EXPECT_CALL(*runtimeInfoFactory().getInfo(), fetchAllInformation(RuntimeInfo::FetchFlag::NONE));
709 
710     vintfObject->getRuntimeInfo(false /* skipCache */, RuntimeInfo::FetchFlag::CPU_VERSION);
711     vintfObject->getRuntimeInfo(false /* skipCache */, RuntimeInfo::FetchFlag::CPU_VERSION);
712     vintfObject->getRuntimeInfo(true /* skipCache */, RuntimeInfo::FetchFlag::CPU_VERSION);
713     vintfObject->getRuntimeInfo(false /* skipCache */, RuntimeInfo::FetchFlag::ALL);
714     vintfObject->getRuntimeInfo(true /* skipCache */, RuntimeInfo::FetchFlag::ALL);
715     vintfObject->getRuntimeInfo(false /* skipCache */, RuntimeInfo::FetchFlag::ALL);
716 }
717 
718 // Test fixture that provides incompatible metadata from the mock device.
719 class VintfObjectTest : public VintfObjectTestBase {
720    protected:
SetUp()721     virtual void SetUp() {
722         VintfObjectTestBase::SetUp();
723 
724         // By default use empty filesystem
725         EXPECT_CALL(fetcher(), listFiles(_, _, _))
726             .Times(AnyNumber())
727             .WillRepeatedly(Return(::android::OK));
728         EXPECT_CALL(fetcher(), fetch(_, _))
729             .Times(AnyNumber())
730             .WillRepeatedly(Return(::android::NAME_NOT_FOUND));
731     }
732 };
733 
734 // Test framework compatibility matrix is combined at runtime
TEST_F(VintfObjectTest,FrameworkCompatibilityMatrixCombine)735 TEST_F(VintfObjectTest, FrameworkCompatibilityMatrixCombine) {
736     EXPECT_CALL(fetcher(), listFiles(StrEq(kSystemVintfDir), _, _))
737         .WillOnce(Invoke([](const auto&, auto* out, auto*) {
738             *out = {
739                 "compatibility_matrix.1.xml",
740                 "compatibility_matrix.empty.xml",
741             };
742             return ::android::OK;
743         }));
744     expectFetch(kSystemVintfDir + "compatibility_matrix.1.xml",
745                 "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"1\"/>");
746     expectFetch(kSystemVintfDir + "compatibility_matrix.empty.xml",
747                 "<compatibility-matrix version=\"1.0\" type=\"framework\"/>");
748     expectFileNotExist(StrEq(kProductMatrix));
749     expectFetch(kVendorManifest, "<manifest version=\"1.0\" type=\"device\" />\n");
750     expectNeverFetch(kSystemLegacyMatrix);
751 
752     EXPECT_NE(nullptr, vintfObject->getFrameworkCompatibilityMatrix(true /* skipCache */));
753 }
754 
755 // Test product compatibility matrix is fetched
TEST_F(VintfObjectTest,ProductCompatibilityMatrix)756 TEST_F(VintfObjectTest, ProductCompatibilityMatrix) {
757     EXPECT_CALL(fetcher(), listFiles(StrEq(kSystemVintfDir), _, _))
758         .WillOnce(Invoke([](const auto&, auto* out, auto*) {
759             *out = {
760                 "compatibility_matrix.1.xml",
761                 "compatibility_matrix.empty.xml",
762             };
763             return ::android::OK;
764         }));
765     expectFetch(kSystemVintfDir + "compatibility_matrix.1.xml",
766                 "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"1\"/>");
767     expectFetch(kSystemVintfDir + "compatibility_matrix.empty.xml",
768                 "<compatibility-matrix version=\"1.0\" type=\"framework\"/>");
769     expectFetch(kProductMatrix,
770                 "<compatibility-matrix version=\"1.0\" type=\"framework\">\n"
771                 "    <hal format=\"hidl\" optional=\"true\">\n"
772                 "        <name>android.hardware.foo</name>\n"
773                 "        <version>1.0</version>\n"
774                 "        <interface>\n"
775                 "            <name>IFoo</name>\n"
776                 "            <instance>default</instance>\n"
777                 "        </interface>\n"
778                 "    </hal>\n"
779                 "</compatibility-matrix>\n");
780     expectFetch(kVendorManifest, "<manifest version=\"1.0\" type=\"device\" />\n");
781     expectNeverFetch(kSystemLegacyMatrix);
782 
783     auto fcm = vintfObject->getFrameworkCompatibilityMatrix(true /* skipCache */);
784     ASSERT_NE(nullptr, fcm);
785 
786     FqInstance expectInstance;
787     EXPECT_TRUE(expectInstance.setTo("android.hardware.foo@1.0::IFoo/default"));
788     bool found = false;
789     fcm->forEachInstance([&found, &expectInstance](const auto& matrixInstance) {
790         found |= matrixInstance.isSatisfiedBy(expectInstance);
791         return !found;  // continue if not found
792     });
793     EXPECT_TRUE(found) << "android.hardware.foo@1.0::IFoo/default should be found in matrix:\n"
794                        << gCompatibilityMatrixConverter(*fcm);
795 }
796 
797 const std::string vendorEtcManifest =
798     "<manifest version=\"1.0\" type=\"device\">\n"
799     "    <hal format=\"hidl\">\n"
800     "        <name>android.hardware.foo</name>\n"
801     "        <transport>hwbinder</transport>\n"
802     "        <version>1.0</version>\n"
803     "        <version>2.0</version>\n"
804     "        <interface>\n"
805     "            <name>IVendorEtc</name>\n"
806     "            <instance>default</instance>\n"
807     "        </interface>\n"
808     "    </hal>\n"
809     "</manifest>\n";
810 
811 const std::string vendorManifest =
812     "<manifest version=\"1.0\" type=\"device\">\n"
813     "    <hal format=\"hidl\">\n"
814     "        <name>android.hardware.foo</name>\n"
815     "        <transport>hwbinder</transport>\n"
816     "        <version>1.0</version>\n"
817     "        <interface>\n"
818     "            <name>IVendor</name>\n"
819     "            <instance>default</instance>\n"
820     "        </interface>\n"
821     "    </hal>\n"
822     "</manifest>\n";
823 
824 const std::string odmProductManifest =
825     "<manifest version=\"1.0\" type=\"device\">\n"
826     "    <hal format=\"hidl\" override=\"true\">\n"
827     "        <name>android.hardware.foo</name>\n"
828     "        <transport>hwbinder</transport>\n"
829     "        <version>1.1</version>\n"
830     "        <interface>\n"
831     "            <name>IOdmProduct</name>\n"
832     "            <instance>default</instance>\n"
833     "        </interface>\n"
834     "    </hal>\n"
835     "</manifest>\n";
836 
837 const std::string odmManifest =
838     "<manifest version=\"1.0\" type=\"device\">\n"
839     "    <hal format=\"hidl\" override=\"true\">\n"
840     "        <name>android.hardware.foo</name>\n"
841     "        <transport>hwbinder</transport>\n"
842     "        <version>1.1</version>\n"
843     "        <interface>\n"
844     "            <name>IOdm</name>\n"
845     "            <instance>default</instance>\n"
846     "        </interface>\n"
847     "    </hal>\n"
848     "</manifest>\n";
849 
containsVendorManifest(const std::shared_ptr<const HalManifest> & p)850 bool containsVendorManifest(const std::shared_ptr<const HalManifest>& p) {
851     return !p->getInstances("android.hardware.foo", {1, 0}, "IVendor").empty();
852 }
853 
containsVendorEtcManifest(const std::shared_ptr<const HalManifest> & p)854 bool containsVendorEtcManifest(const std::shared_ptr<const HalManifest>& p) {
855     return !p->getInstances("android.hardware.foo", {2, 0}, "IVendorEtc").empty();
856 }
857 
vendorEtcManifestOverridden(const std::shared_ptr<const HalManifest> & p)858 bool vendorEtcManifestOverridden(const std::shared_ptr<const HalManifest>& p) {
859     return p->getInstances("android.hardware.foo", {1, 0}, "IVendorEtc").empty();
860 }
861 
containsOdmManifest(const std::shared_ptr<const HalManifest> & p)862 bool containsOdmManifest(const std::shared_ptr<const HalManifest>& p) {
863     return !p->getInstances("android.hardware.foo", {1, 1}, "IOdm").empty();
864 }
865 
containsOdmProductManifest(const std::shared_ptr<const HalManifest> & p)866 bool containsOdmProductManifest(const std::shared_ptr<const HalManifest>& p) {
867     return !p->getInstances("android.hardware.foo", {1, 1}, "IOdmProduct").empty();
868 }
869 
870 class DeviceManifestTest : public VintfObjectTestBase {
871    protected:
SetUp()872     virtual void SetUp() {
873         VintfObjectTestBase::SetUp();
874         setFakeProperties();
875     }
876 
877     // Expect that /vendor/etc/vintf/manifest.xml is fetched.
expectVendorManifest()878     void expectVendorManifest() { expectFetch(kVendorManifest, vendorEtcManifest); }
879     // /vendor/etc/vintf/manifest.xml does not exist.
noVendorManifest()880     void noVendorManifest() { expectFileNotExist(StrEq(kVendorManifest)); }
881     // Expect some ODM manifest is fetched.
expectOdmManifest()882     void expectOdmManifest() {
883         if (!productModel.empty()) {
884             expectFileNotExist(StrEq(kOdmVintfDir + "manifest_" + productModel + ".xml"));
885         }
886         expectFetch(kOdmManifest, odmManifest);
887     }
noOdmManifest()888     void noOdmManifest() { expectFileNotExist(StartsWith("/odm/")); }
get()889     std::shared_ptr<const HalManifest> get() {
890         return vintfObject->getDeviceHalManifest(true /* skipCache */);
891     }
892 };
893 
894 // Test /vendor/etc/vintf/manifest.xml + ODM manifest
TEST_F(DeviceManifestTest,Combine1)895 TEST_F(DeviceManifestTest, Combine1) {
896     expectVendorManifest();
897     expectOdmManifest();
898     auto p = get();
899     ASSERT_NE(nullptr, p);
900     EXPECT_TRUE(containsVendorEtcManifest(p));
901     EXPECT_TRUE(vendorEtcManifestOverridden(p));
902     EXPECT_TRUE(containsOdmManifest(p));
903     EXPECT_FALSE(containsVendorManifest(p));
904 }
905 
906 // Test /vendor/etc/vintf/manifest.xml
TEST_F(DeviceManifestTest,Combine2)907 TEST_F(DeviceManifestTest, Combine2) {
908     expectVendorManifest();
909     noOdmManifest();
910     auto p = get();
911     ASSERT_NE(nullptr, p);
912     EXPECT_TRUE(containsVendorEtcManifest(p));
913     EXPECT_FALSE(vendorEtcManifestOverridden(p));
914     EXPECT_FALSE(containsOdmManifest(p));
915     EXPECT_FALSE(containsVendorManifest(p));
916 }
917 
918 // Test ODM manifest
TEST_F(DeviceManifestTest,Combine3)919 TEST_F(DeviceManifestTest, Combine3) {
920     noVendorManifest();
921     expectOdmManifest();
922     auto p = get();
923     ASSERT_NE(nullptr, p);
924     EXPECT_FALSE(containsVendorEtcManifest(p));
925     EXPECT_TRUE(vendorEtcManifestOverridden(p));
926     EXPECT_TRUE(containsOdmManifest(p));
927     EXPECT_FALSE(containsVendorManifest(p));
928 }
929 
930 // Test /vendor/manifest.xml
TEST_F(DeviceManifestTest,Combine4)931 TEST_F(DeviceManifestTest, Combine4) {
932     noVendorManifest();
933     noOdmManifest();
934     expectFetch(kVendorLegacyManifest, vendorManifest);
935     auto p = get();
936     ASSERT_NE(nullptr, p);
937     EXPECT_FALSE(containsVendorEtcManifest(p));
938     EXPECT_TRUE(vendorEtcManifestOverridden(p));
939     EXPECT_FALSE(containsOdmManifest(p));
940     EXPECT_TRUE(containsVendorManifest(p));
941 }
942 
943 class OdmManifestTest : public VintfObjectTestBase {
944    protected:
SetUp()945     virtual void SetUp() override {
946         VintfObjectTestBase::SetUp();
947         // Assume /vendor/etc/vintf/manifest.xml does not exist to simplify
948         // testing logic.
949         expectFileNotExist(StrEq(kVendorManifest));
950         // Expect that the legacy /vendor/manifest.xml is never fetched.
951         expectNeverFetch(kVendorLegacyManifest);
952         // Assume no files exist under /odm/ unless otherwise specified.
953         expectFileNotExist(StartsWith("/odm/"));
954     }
get()955     std::shared_ptr<const HalManifest> get() {
956         return vintfObject->getDeviceHalManifest(true /* skipCache */);
957     }
958 };
959 
TEST_F(OdmManifestTest,OdmProductManifest)960 TEST_F(OdmManifestTest, OdmProductManifest) {
961     if (productModel.empty()) return;
962     expectFetch(kOdmVintfDir + "manifest_" + productModel + ".xml", odmProductManifest);
963     // /odm/etc/vintf/manifest.xml should not be fetched when the product variant exists.
964     expectNeverFetch(kOdmManifest);
965     auto p = get();
966     ASSERT_NE(nullptr, p);
967     EXPECT_TRUE(containsOdmProductManifest(p));
968 }
969 
TEST_F(OdmManifestTest,OdmManifest)970 TEST_F(OdmManifestTest, OdmManifest) {
971     expectFetch(kOdmManifest, odmManifest);
972     auto p = get();
973     ASSERT_NE(nullptr, p);
974     EXPECT_TRUE(containsOdmManifest(p));
975 }
976 
TEST_F(OdmManifestTest,OdmLegacyProductManifest)977 TEST_F(OdmManifestTest, OdmLegacyProductManifest) {
978     if (productModel.empty()) return;
979     expectFetch(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml", odmProductManifest);
980     // /odm/manifest.xml should not be fetched when the product variant exists.
981     expectNeverFetch(kOdmLegacyManifest);
982     auto p = get();
983     ASSERT_NE(nullptr, p);
984     EXPECT_TRUE(containsOdmProductManifest(p));
985 }
986 
TEST_F(OdmManifestTest,OdmLegacyManifest)987 TEST_F(OdmManifestTest, OdmLegacyManifest) {
988     expectFetch(kOdmLegacyManifest, odmManifest);
989     auto p = get();
990     ASSERT_NE(nullptr, p);
991     EXPECT_TRUE(containsOdmManifest(p));
992 }
993 
994 struct CheckedFqInstance : FqInstance {
CheckedFqInstanceandroid::vintf::testing::CheckedFqInstance995     CheckedFqInstance(const char* s) : CheckedFqInstance(std::string(s)) {}
CheckedFqInstanceandroid::vintf::testing::CheckedFqInstance996     CheckedFqInstance(const std::string& s) { CHECK(setTo(s)) << s; }
997 
getVersionandroid::vintf::testing::CheckedFqInstance998     Version getVersion() const { return FqInstance::getVersion(); }
999 };
1000 
getInstanceListFunc(const std::vector<CheckedFqInstance> & instances)1001 static VintfObject::ListInstances getInstanceListFunc(
1002     const std::vector<CheckedFqInstance>& instances) {
1003     return [instances](const std::string& package, Version version, const std::string& interface,
1004                        const auto& /* instanceHint */) {
1005         std::vector<std::pair<std::string, Version>> ret;
1006         for (auto&& existing : instances) {
1007             if (existing.getPackage() == package && existing.getVersion().minorAtLeast(version) &&
1008                 existing.getInterface() == interface) {
1009                 ret.push_back(std::make_pair(existing.getInstance(), existing.getVersion()));
1010             }
1011         }
1012 
1013         return ret;
1014     };
1015 }
1016 
1017 class DeprecateTest : public VintfObjectTestBase {
1018    protected:
SetUp()1019     virtual void SetUp() override {
1020         VintfObjectTestBase::SetUp();
1021         EXPECT_CALL(fetcher(), listFiles(StrEq(kSystemVintfDir), _, _))
1022             .WillRepeatedly(Invoke([](const auto&, auto* out, auto*) {
1023                 *out = {
1024                     "compatibility_matrix.1.xml",
1025                     "compatibility_matrix.2.xml",
1026                 };
1027                 return ::android::OK;
1028             }));
1029         EXPECT_CALL(fetcher(), listFiles(StrEq(kVendorManifestFragmentDir), _, _))
1030             .WillOnce(Return(::android::OK));
1031         EXPECT_CALL(fetcher(), listFiles(StrEq(kOdmManifestFragmentDir), _, _))
1032             .WillOnce(Return(::android::OK));
1033         expectFetch(kSystemVintfDir + "compatibility_matrix.1.xml", systemMatrixLevel1);
1034         expectFetch(kSystemVintfDir + "compatibility_matrix.2.xml", systemMatrixLevel2);
1035         expectFileNotExist(StrEq(kProductMatrix));
1036         expectNeverFetch(kSystemLegacyMatrix);
1037 
1038         expectFetch(kVendorManifest,
1039                     "<manifest version=\"1.0\" type=\"device\" target-level=\"2\"/>");
1040         expectFileNotExist(StartsWith("/odm/"));
1041 
1042         // Update the device manifest cache because CheckDeprecate does not fetch
1043         // device manifest again if cache exist.
1044         vintfObject->getDeviceHalManifest(true /* skipCache */);
1045     }
1046 
1047 };
1048 
TEST_F(DeprecateTest,CheckNoDeprecate)1049 TEST_F(DeprecateTest, CheckNoDeprecate) {
1050     auto pred = getInstanceListFunc({
1051         "android.hardware.minor@1.1::IMinor/default",
1052         "android.hardware.major@2.0::IMajor/default",
1053     });
1054     std::string error;
1055     EXPECT_EQ(NO_DEPRECATED_HALS, vintfObject->checkDeprecation(pred, &error)) << error;
1056 }
1057 
TEST_F(DeprecateTest,CheckRemoved)1058 TEST_F(DeprecateTest, CheckRemoved) {
1059     auto pred = getInstanceListFunc({
1060         "android.hardware.removed@1.0::IRemoved/default",
1061         "android.hardware.minor@1.1::IMinor/default",
1062         "android.hardware.major@2.0::IMajor/default",
1063     });
1064     std::string error;
1065     EXPECT_EQ(DEPRECATED, vintfObject->checkDeprecation(pred, &error))
1066         << "removed@1.0 should be deprecated. " << error;
1067 }
1068 
TEST_F(DeprecateTest,CheckMinor)1069 TEST_F(DeprecateTest, CheckMinor) {
1070     auto pred = getInstanceListFunc({
1071         "android.hardware.minor@1.0::IMinor/default",
1072         "android.hardware.major@2.0::IMajor/default",
1073     });
1074     std::string error;
1075     EXPECT_EQ(DEPRECATED, vintfObject->checkDeprecation(pred, &error))
1076         << "minor@1.0 should be deprecated. " << error;
1077 }
1078 
TEST_F(DeprecateTest,CheckMinorDeprecatedInstance1)1079 TEST_F(DeprecateTest, CheckMinorDeprecatedInstance1) {
1080     auto pred = getInstanceListFunc({
1081         "android.hardware.minor@1.0::IMinor/legacy",
1082         "android.hardware.minor@1.1::IMinor/default",
1083         "android.hardware.major@2.0::IMajor/default",
1084     });
1085     std::string error;
1086     EXPECT_EQ(DEPRECATED, vintfObject->checkDeprecation(pred, &error))
1087         << "minor@1.0::IMinor/legacy should be deprecated. " << error;
1088 }
1089 
TEST_F(DeprecateTest,CheckMinorDeprecatedInstance2)1090 TEST_F(DeprecateTest, CheckMinorDeprecatedInstance2) {
1091     auto pred = getInstanceListFunc({
1092         "android.hardware.minor@1.1::IMinor/default",
1093         "android.hardware.minor@1.1::IMinor/legacy",
1094         "android.hardware.major@2.0::IMajor/default",
1095     });
1096     std::string error;
1097     EXPECT_EQ(DEPRECATED, vintfObject->checkDeprecation(pred, &error))
1098         << "minor@1.1::IMinor/legacy should be deprecated. " << error;
1099 }
1100 
TEST_F(DeprecateTest,CheckMajor1)1101 TEST_F(DeprecateTest, CheckMajor1) {
1102     auto pred = getInstanceListFunc({
1103         "android.hardware.minor@1.1::IMinor/default",
1104         "android.hardware.major@1.0::IMajor/default",
1105         "android.hardware.major@2.0::IMajor/default",
1106     });
1107     std::string error;
1108     EXPECT_EQ(DEPRECATED, vintfObject->checkDeprecation(pred, &error))
1109         << "major@1.0 should be deprecated. " << error;
1110 }
1111 
TEST_F(DeprecateTest,CheckMajor2)1112 TEST_F(DeprecateTest, CheckMajor2) {
1113     auto pred = getInstanceListFunc({
1114         "android.hardware.minor@1.1::IMinor/default",
1115         "android.hardware.major@1.0::IMajor/default",
1116     });
1117     std::string error;
1118     EXPECT_EQ(DEPRECATED, vintfObject->checkDeprecation(pred, &error))
1119         << "major@1.0 should be deprecated. " << error;
1120 }
1121 
1122 class MultiMatrixTest : public VintfObjectTestBase {
1123    protected:
getFileName(size_t i)1124     static std::string getFileName(size_t i) {
1125         return "compatibility_matrix." + std::to_string(static_cast<Level>(i)) + ".xml";
1126     }
SetUpMockSystemMatrices(const std::vector<std::string> & xmls)1127     void SetUpMockSystemMatrices(const std::vector<std::string>& xmls) {
1128         EXPECT_CALL(fetcher(), listFiles(_, _, _))
1129             .Times(AnyNumber())
1130             .WillRepeatedly(Return(::android::OK));
1131         EXPECT_CALL(fetcher(), listFiles(StrEq(kSystemVintfDir), _, _))
1132             .WillRepeatedly(Invoke([=](const auto&, auto* out, auto*) {
1133                 size_t i = 1;
1134                 for (const auto& content : xmls) {
1135                     (void)content;
1136                     out->push_back(getFileName(i));
1137                     ++i;
1138                 }
1139                 return ::android::OK;
1140             }));
1141         size_t i = 1;
1142         for (const auto& content : xmls) {
1143             expectFetchRepeatedly(kSystemVintfDir + getFileName(i), content);
1144             ++i;
1145         }
1146         expectFileNotExist(kProductMatrix);
1147         expectNeverFetch(kSystemLegacyMatrix);
1148         expectFileNotExist(StartsWith("/odm/"));
1149     }
expectTargetFcmVersion(size_t level)1150     void expectTargetFcmVersion(size_t level) {
1151         expectFetch(kVendorManifest, "<manifest version=\"1.0\" type=\"device\" target-level=\"" +
1152                                          to_string(static_cast<Level>(level)) + "\"/>");
1153         vintfObject->getDeviceHalManifest(true /* skipCache */);
1154     }
1155 };
1156 
1157 class RegexTest : public MultiMatrixTest {
1158    protected:
SetUp()1159     virtual void SetUp() {
1160         MultiMatrixTest::SetUp();
1161         SetUpMockSystemMatrices(systemMatrixRegexXmls);
1162     }
1163 };
1164 
TEST_F(RegexTest,CombineLevel1)1165 TEST_F(RegexTest, CombineLevel1) {
1166     expectTargetFcmVersion(1);
1167     auto matrix = vintfObject->getFrameworkCompatibilityMatrix(true /* skipCache */);
1168     ASSERT_NE(nullptr, matrix);
1169     std::string xml = gCompatibilityMatrixConverter(*matrix);
1170 
1171     EXPECT_IN(
1172         "    <hal format=\"hidl\" optional=\"false\">\n"
1173         "        <name>android.hardware.regex</name>\n"
1174         "        <version>1.0-2</version>\n"
1175         "        <version>2.0</version>\n"
1176         "        <interface>\n"
1177         "            <name>IRegex</name>\n"
1178         "            <instance>default</instance>\n"
1179         "        </interface>\n"
1180         "    </hal>\n",
1181         xml);
1182     EXPECT_IN(
1183         "    <hal format=\"hidl\" optional=\"false\">\n"
1184         "        <name>android.hardware.regex</name>\n"
1185         "        <version>1.0-1</version>\n"
1186         "        <interface>\n"
1187         "            <name>IRegex</name>\n"
1188         "            <instance>special/1.0</instance>\n"
1189         "            <regex-instance>regex/1.0/[0-9]+</regex-instance>\n"
1190         "            <regex-instance>regex_common/[0-9]+</regex-instance>\n"
1191         "        </interface>\n"
1192         "    </hal>\n",
1193         xml);
1194     EXPECT_IN(
1195         "    <hal format=\"hidl\" optional=\"true\">\n"
1196         "        <name>android.hardware.regex</name>\n"
1197         "        <version>1.1-2</version>\n"
1198         "        <interface>\n"
1199         "            <name>IRegex</name>\n"
1200         "            <instance>special/1.1</instance>\n"
1201         "            <regex-instance>[a-z]+_[a-z]+/[0-9]+</regex-instance>\n"
1202         "            <regex-instance>regex/1.1/[0-9]+</regex-instance>\n"
1203         "        </interface>\n"
1204         "    </hal>\n",
1205         xml);
1206     EXPECT_IN(
1207         "    <hal format=\"hidl\" optional=\"true\">\n"
1208         "        <name>android.hardware.regex</name>\n"
1209         "        <version>2.0</version>\n"
1210         "        <interface>\n"
1211         "            <name>IRegex</name>\n"
1212         "            <instance>special/2.0</instance>\n"
1213         "            <regex-instance>regex/2.0/[0-9]+</regex-instance>\n"
1214         "            <regex-instance>regex_[a-z]+/[0-9]+</regex-instance>\n"
1215         "        </interface>\n"
1216         "    </hal>\n",
1217         xml);
1218 }
1219 
TEST_F(RegexTest,CombineLevel2)1220 TEST_F(RegexTest, CombineLevel2) {
1221     expectTargetFcmVersion(2);
1222     auto matrix = vintfObject->getFrameworkCompatibilityMatrix(true /* skipCache */);
1223     ASSERT_NE(nullptr, matrix);
1224     std::string xml = gCompatibilityMatrixConverter(*matrix);
1225 
1226     EXPECT_IN(
1227         "    <hal format=\"hidl\" optional=\"false\">\n"
1228         "        <name>android.hardware.regex</name>\n"
1229         "        <version>1.1-2</version>\n"
1230         "        <version>2.0</version>\n"
1231         "        <interface>\n"
1232         "            <name>IRegex</name>\n"
1233         "            <instance>default</instance>\n"
1234         "        </interface>\n"
1235         "    </hal>\n",
1236         xml);
1237     EXPECT_IN(
1238         "    <hal format=\"hidl\" optional=\"false\">\n"
1239         "        <name>android.hardware.regex</name>\n"
1240         "        <version>1.1-2</version>\n"
1241         "        <interface>\n"
1242         "            <name>IRegex</name>\n"
1243         "            <instance>special/1.1</instance>\n"
1244         "            <regex-instance>[a-z]+_[a-z]+/[0-9]+</regex-instance>\n"
1245         "            <regex-instance>regex/1.1/[0-9]+</regex-instance>\n"
1246         "        </interface>\n"
1247         "    </hal>\n",
1248         xml);
1249     EXPECT_IN(
1250         "    <hal format=\"hidl\" optional=\"true\">\n"
1251         "        <name>android.hardware.regex</name>\n"
1252         "        <version>2.0</version>\n"
1253         "        <interface>\n"
1254         "            <name>IRegex</name>\n"
1255         "            <instance>special/2.0</instance>\n"
1256         "            <regex-instance>regex/2.0/[0-9]+</regex-instance>\n"
1257         "            <regex-instance>regex_[a-z]+/[0-9]+</regex-instance>\n"
1258         "        </interface>\n"
1259         "    </hal>\n",
1260         xml);
1261 }
1262 
TEST_F(RegexTest,DeprecateLevel2)1263 TEST_F(RegexTest, DeprecateLevel2) {
1264     std::string error;
1265     expectTargetFcmVersion(2);
1266 
1267     auto pred = getInstanceListFunc({
1268         "android.hardware.regex@1.1::IRegex/default",
1269         "android.hardware.regex@1.1::IRegex/special/1.1",
1270         "android.hardware.regex@1.1::IRegex/regex/1.1/1",
1271         "android.hardware.regex@1.1::IRegex/regex_common/0",
1272         "android.hardware.regex@2.0::IRegex/default",
1273     });
1274     EXPECT_EQ(NO_DEPRECATED_HALS, vintfObject->checkDeprecation(pred, &error)) << error;
1275 
1276     for (const auto& deprecated : {
1277              "android.hardware.regex@1.0::IRegex/default",
1278              "android.hardware.regex@1.0::IRegex/special/1.0",
1279              "android.hardware.regex@1.0::IRegex/regex/1.0/1",
1280              "android.hardware.regex@1.0::IRegex/regex_common/0",
1281              "android.hardware.regex@1.1::IRegex/special/1.0",
1282              "android.hardware.regex@1.1::IRegex/regex/1.0/1",
1283          }) {
1284         // 2.0/default ensures compatibility.
1285         pred = getInstanceListFunc({
1286             deprecated,
1287             "android.hardware.regex@2.0::IRegex/default",
1288         });
1289         error.clear();
1290         EXPECT_EQ(DEPRECATED, vintfObject->checkDeprecation(pred, &error))
1291             << deprecated << " should be deprecated. " << error;
1292     }
1293 }
1294 
TEST_F(RegexTest,DeprecateLevel3)1295 TEST_F(RegexTest, DeprecateLevel3) {
1296     std::string error;
1297     expectTargetFcmVersion(3);
1298 
1299     auto pred = getInstanceListFunc({
1300         "android.hardware.regex@2.0::IRegex/special/2.0",
1301         "android.hardware.regex@2.0::IRegex/regex/2.0/1",
1302         "android.hardware.regex@2.0::IRegex/default",
1303     });
1304     EXPECT_EQ(NO_DEPRECATED_HALS, vintfObject->checkDeprecation(pred, &error)) << error;
1305 
1306     for (const auto& deprecated : {
1307              "android.hardware.regex@1.0::IRegex/default",
1308              "android.hardware.regex@1.0::IRegex/special/1.0",
1309              "android.hardware.regex@1.0::IRegex/regex/1.0/1",
1310              "android.hardware.regex@1.0::IRegex/regex_common/0",
1311              "android.hardware.regex@1.1::IRegex/special/1.0",
1312              "android.hardware.regex@1.1::IRegex/regex/1.0/1",
1313              "android.hardware.regex@1.1::IRegex/special/1.1",
1314              "android.hardware.regex@1.1::IRegex/regex/1.1/1",
1315              "android.hardware.regex@1.1::IRegex/regex_common/0",
1316          }) {
1317         // 2.0/default ensures compatibility.
1318         pred = getInstanceListFunc({
1319             deprecated,
1320             "android.hardware.regex@2.0::IRegex/default",
1321         });
1322 
1323         error.clear();
1324         EXPECT_EQ(DEPRECATED, vintfObject->checkDeprecation(pred, &error))
1325             << deprecated << " should be deprecated.";
1326     }
1327 }
1328 
1329 //
1330 // Set of framework matrices of different FCM version with <kernel>.
1331 //
1332 
1333 #define FAKE_KERNEL(__version__, __key__)                   \
1334     "    <kernel version=\"" __version__ "\">\n"            \
1335     "        <config>\n"                                    \
1336     "            <key>CONFIG_" __key__ "</key>\n"           \
1337     "            <value type=\"tristate\">y</value>\n"      \
1338     "        </config>\n"                                   \
1339     "    </kernel>\n"
1340 
1341 const static std::vector<std::string> systemMatrixKernelXmls = {
1342     // 1.xml
1343     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"1\">\n"
1344     FAKE_KERNEL("1.0.0", "A1")
1345     FAKE_KERNEL("2.0.0", "B1")
1346     "</compatibility-matrix>\n",
1347     // 2.xml
1348     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"2\">\n"
1349     FAKE_KERNEL("2.0.0", "B2")
1350     FAKE_KERNEL("3.0.0", "C2")
1351     FAKE_KERNEL("4.0.0", "D2")
1352     "</compatibility-matrix>\n",
1353     // 3.xml
1354     "<compatibility-matrix version=\"1.0\" type=\"framework\" level=\"3\">\n"
1355     FAKE_KERNEL("4.0.0", "D3")
1356     FAKE_KERNEL("5.0.0", "E3")
1357     "</compatibility-matrix>\n",
1358 };
1359 
1360 class KernelTest : public MultiMatrixTest {};
1361 
1362 // Assume that we are developing level 2. Test that old <kernel> requirements should
1363 // not change and new <kernel> versions are added.
TEST_F(KernelTest,Level1AndLevel2)1364 TEST_F(KernelTest, Level1AndLevel2) {
1365     SetUpMockSystemMatrices({systemMatrixKernelXmls[0], systemMatrixKernelXmls[1]});
1366 
1367     expectTargetFcmVersion(1);
1368     auto matrix = vintfObject->getFrameworkCompatibilityMatrix(true /* skipCache */);
1369     ASSERT_NE(nullptr, matrix);
1370     std::string xml = gCompatibilityMatrixConverter(*matrix);
1371 
1372     EXPECT_IN(FAKE_KERNEL("1.0.0", "A1"), xml) << "\nOld requirements must not change.";
1373     EXPECT_IN(FAKE_KERNEL("2.0.0", "B1"), xml) << "\nOld requirements must not change.";
1374     EXPECT_IN(FAKE_KERNEL("3.0.0", "C2"), xml) << "\nShould see <kernel> from new matrices";
1375     EXPECT_IN(FAKE_KERNEL("4.0.0", "D2"), xml) << "\nShould see <kernel> from new matrices";
1376 
1377     EXPECT_NOT_IN(FAKE_KERNEL("2.0.0", "B2"), xml) << "\nOld requirements must not change";
1378 }
1379 
1380 // Assume that we are developing level 3. Test that old <kernel> requirements should
1381 // not change and new <kernel> versions are added.
TEST_F(KernelTest,Level1AndMore)1382 TEST_F(KernelTest, Level1AndMore) {
1383     SetUpMockSystemMatrices({systemMatrixKernelXmls});
1384 
1385     expectTargetFcmVersion(1);
1386     auto matrix = vintfObject->getFrameworkCompatibilityMatrix(true /* skipCache */);
1387     ASSERT_NE(nullptr, matrix);
1388     std::string xml = gCompatibilityMatrixConverter(*matrix);
1389 
1390     EXPECT_IN(FAKE_KERNEL("1.0.0", "A1"), xml) << "\nOld requirements must not change.";
1391     EXPECT_IN(FAKE_KERNEL("2.0.0", "B1"), xml) << "\nOld requirements must not change.";
1392     EXPECT_IN(FAKE_KERNEL("3.0.0", "C2"), xml) << "\nOld requirements must not change.";
1393     EXPECT_IN(FAKE_KERNEL("4.0.0", "D2"), xml) << "\nOld requirements must not change.";
1394     EXPECT_IN(FAKE_KERNEL("5.0.0", "E3"), xml) << "\nShould see <kernel> from new matrices";
1395 
1396     EXPECT_NOT_IN(FAKE_KERNEL("2.0.0", "B2"), xml) << "\nOld requirements must not change";
1397     EXPECT_NOT_IN(FAKE_KERNEL("4.0.0", "D3"), xml) << "\nOld requirements must not change";
1398 }
1399 
1400 class VintfObjectPartialUpdateTest : public MultiMatrixTest {
1401    protected:
SetUp()1402     void SetUp() override {
1403         MultiMatrixTest::SetUp();
1404         setFakeProperties();
1405     }
1406 };
1407 
TEST_F(VintfObjectPartialUpdateTest,DeviceCompatibility)1408 TEST_F(VintfObjectPartialUpdateTest, DeviceCompatibility) {
1409     setupMockFetcher(vendorManifestRequire1, "", systemManifestXml1, vendorMatrixXml1,
1410                      productModel);
1411     SetUpMockSystemMatrices(systemMatrixRequire);
1412 
1413     expectSystemManifest();
1414     expectVendorMatrix();
1415     expectVendorManifest();
1416 
1417     std::string error;
1418     EXPECT_TRUE(checkCompatibility({}, &error)) << error;
1419 }
1420 
TEST_F(VintfObjectPartialUpdateTest,VendorOnlyCompatible)1421 TEST_F(VintfObjectPartialUpdateTest, VendorOnlyCompatible) {
1422     setupMockFetcher("", "", systemManifestXml1, vendorMatrixXml1, productModel);
1423     SetUpMockSystemMatrices(systemMatrixRequire);
1424 
1425     expectSystemManifest();
1426     expectVendorMatrix();
1427     // Should not load vendor manifest from device
1428     expectVendorManifest(0);
1429 
1430     std::string error;
1431     EXPECT_TRUE(checkCompatibility({vendorManifestRequire2}, &error)) << error;
1432 }
1433 
1434 const std::string systemEtcManifest =
1435     "<manifest version=\"1.0\" type=\"framework\">\n"
1436     "    <hal format=\"hidl\">\n"
1437     "        <name>android.hardware.foo</name>\n"
1438     "        <transport>hwbinder</transport>\n"
1439     "        <fqname>@1.0::ISystemEtc/default</fqname>\n"
1440     "    </hal>\n"
1441     "</manifest>\n";
1442 
1443 const std::string systemEtcManifestFrag =
1444     "<manifest version=\"1.0\" type=\"framework\">\n"
1445     "    <hal format=\"hidl\">\n"
1446     "        <name>android.hardware.foo</name>\n"
1447     "        <transport>hwbinder</transport>\n"
1448     "        <fqname>@1.0::ISystemEtcFragment/default</fqname>\n"
1449     "    </hal>\n"
1450     "</manifest>\n";
1451 
1452 const std::string productEtcManifest =
1453     "<manifest version=\"1.0\" type=\"framework\">\n"
1454     "    <hal format=\"hidl\">\n"
1455     "        <name>android.hardware.foo</name>\n"
1456     "        <transport>hwbinder</transport>\n"
1457     "        <fqname>@1.0::IProductEtc/default</fqname>\n"
1458     "    </hal>\n"
1459     "</manifest>\n";
1460 
1461 const std::string productEtcManifestFrag =
1462     "<manifest version=\"1.0\" type=\"framework\">\n"
1463     "    <hal format=\"hidl\">\n"
1464     "        <name>android.hardware.foo</name>\n"
1465     "        <transport>hwbinder</transport>\n"
1466     "        <fqname>@1.0::IProductEtcFragment/default</fqname>\n"
1467     "    </hal>\n"
1468     "</manifest>\n";
1469 
1470 using FrameworkManifestTestParam =
1471     std::tuple<bool /* Existence of /system/etc/vintf/manifest.xml */,
1472                bool /* Existence of /system/etc/vintf/manifest/fragment.xml */,
1473                bool /* Existence of /product/etc/vintf/manifest.xml */,
1474                bool /* Existence of /product/etc/vintf/manifest/fragment.xml */>;
1475 class FrameworkManifestTest : public VintfObjectTestBase,
1476                               public ::testing::WithParamInterface<FrameworkManifestTestParam> {
1477    protected:
SetUp()1478     virtual void SetUp() {
1479         VintfObjectTestBase::SetUp();
1480         setFakeProperties();
1481     }
1482 
1483     // Set the existence of /system/etc/vintf/manifest.xml
expectSystemManifest(bool exists)1484     void expectSystemManifest(bool exists) {
1485         if (exists) {
1486             expectFetchRepeatedly(kSystemManifest, systemEtcManifest);
1487         } else {
1488             expectFileNotExist(StrEq(kSystemManifest));
1489         }
1490         expectFileNotExist(StrEq(kSystemLegacyManifest));
1491     }
1492     // Set the existence of /system/etc/vintf/manifest/fragment.xml
expectSystemManifestFragment(bool exists)1493     void expectSystemManifestFragment(bool exists) {
1494         if (exists) {
1495             EXPECT_CALL(fetcher(), listFiles(StrEq(kSystemManifestFragmentDir), _, _))
1496                 .Times(AnyNumber())
1497                 .WillRepeatedly(Invoke([](const auto&, auto* out, auto*) {
1498                     *out = {"fragment.xml"};
1499                     return ::android::OK;
1500                 }));
1501             expectFetchRepeatedly(kSystemManifestFragmentDir + "fragment.xml",
1502                                   systemEtcManifestFrag);
1503         } else {
1504             EXPECT_CALL(fetcher(), listFiles(StrEq(kSystemManifestFragmentDir), _, _))
1505                 .Times(AnyNumber())
1506                 .WillRepeatedly(Return(::android::OK));
1507             expectFileNotExist(kSystemManifestFragmentDir + "fragment.xml");
1508         }
1509     }
1510     // Set the existence of /product/etc/vintf/manifest.xml
expectProductManifest(bool exists)1511     void expectProductManifest(bool exists) {
1512         if (exists) {
1513             expectFetchRepeatedly(kProductManifest, productEtcManifest);
1514         } else {
1515             expectFileNotExist(kProductManifest);
1516         }
1517     }
1518     // Set the existence of /product/etc/vintf/manifest/fragment.xml
expectProductManifestFragment(bool exists)1519     void expectProductManifestFragment(bool exists) {
1520         if (exists) {
1521             EXPECT_CALL(fetcher(), listFiles(StrEq(kProductManifestFragmentDir), _, _))
1522                 .Times(AnyNumber())
1523                 .WillRepeatedly(Invoke([](const auto&, auto* out, auto*) {
1524                     *out = {"fragment.xml"};
1525                     return ::android::OK;
1526                 }));
1527             expectFetchRepeatedly(kProductManifestFragmentDir + "fragment.xml",
1528                                   productEtcManifestFrag);
1529         } else {
1530             EXPECT_CALL(fetcher(), listFiles(StrEq(kProductManifestFragmentDir), _, _))
1531                 .Times(AnyNumber())
1532                 .WillRepeatedly(Return(::android::OK));
1533             expectFileNotExist(kProductManifestFragmentDir + "fragment.xml");
1534         }
1535     }
1536 
expectContainsInterface(const std::string & interface,bool contains=true)1537     void expectContainsInterface(const std::string& interface, bool contains = true) {
1538         auto manifest = vintfObject->getFrameworkHalManifest();
1539         ASSERT_NE(nullptr, manifest);
1540         EXPECT_NE(manifest->getInstances("android.hardware.foo", {1, 0}, interface).empty(),
1541                   contains)
1542             << interface << " is missing.";
1543     }
1544 };
1545 
TEST_P(FrameworkManifestTest,Existence)1546 TEST_P(FrameworkManifestTest, Existence) {
1547     expectSystemManifest(std::get<0>(GetParam()));
1548     expectSystemManifestFragment(std::get<1>(GetParam()));
1549     expectProductManifest(std::get<2>(GetParam()));
1550     expectProductManifestFragment(std::get<3>(GetParam()));
1551 
1552     if (!std::get<0>(GetParam())) {
1553         EXPECT_EQ(nullptr, vintfObject->getFrameworkHalManifest())
1554             << "getFrameworkHalManifest must return nullptr if " << kSystemManifest
1555             << " does not exist";
1556     } else {
1557         expectContainsInterface("ISystemEtc", std::get<0>(GetParam()));
1558         expectContainsInterface("ISystemEtcFragment", std::get<1>(GetParam()));
1559         expectContainsInterface("IProductEtc", std::get<2>(GetParam()));
1560         expectContainsInterface("IProductEtcFragment", std::get<3>(GetParam()));
1561     }
1562 }
1563 INSTANTIATE_TEST_SUITE_P(, FrameworkManifestTest,
1564                          ::testing::Combine(Bool(), Bool(), Bool(), Bool()));
1565 
1566 }  // namespace testing
1567 }  // namespace vintf
1568 }  // namespace android
1569 
main(int argc,char ** argv)1570 int main(int argc, char** argv) {
1571     ::testing::InitGoogleMock(&argc, argv);
1572     return RUN_ALL_TESTS();
1573 }
1574