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