• 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 "utils-fake.h"
25 #include "vintf/VintfObject.h"
26 
27 using namespace ::testing;
28 using namespace ::android::vintf;
29 using namespace ::android::vintf::details;
30 
31 //
32 // Set of Xml1 metadata compatible with each other.
33 //
34 
35 const std::string systemMatrixXml1 =
36     "<compatibility-matrix version=\"1.0\" type=\"framework\">\n"
37     "    <hal format=\"hidl\" optional=\"false\">\n"
38     "        <name>android.hardware.camera</name>\n"
39     "        <version>2.0-5</version>\n"
40     "        <version>3.4-16</version>\n"
41     "    </hal>\n"
42     "    <hal format=\"hidl\" optional=\"false\">\n"
43     "        <name>android.hardware.nfc</name>\n"
44     "        <version>1.0</version>\n"
45     "        <version>2.0</version>\n"
46     "    </hal>\n"
47     "    <hal format=\"hidl\" optional=\"true\">\n"
48     "        <name>android.hardware.foo</name>\n"
49     "        <version>1.0</version>\n"
50     "    </hal>\n"
51     "    <kernel version=\"3.18.31\"></kernel>\n"
52     "    <sepolicy>\n"
53     "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
54     "        <sepolicy-version>25.5</sepolicy-version>\n"
55     "        <sepolicy-version>26.0-3</sepolicy-version>\n"
56     "    </sepolicy>\n"
57     "    <avb>\n"
58     "        <vbmeta-version>0.0</vbmeta-version>\n"
59     "    </avb>\n"
60     "</compatibility-matrix>\n";
61 
62 const std::string vendorManifestXml1 =
63     "<manifest version=\"1.0\" type=\"device\">\n"
64     "    <hal format=\"hidl\">\n"
65     "        <name>android.hardware.camera</name>\n"
66     "        <transport>hwbinder</transport>\n"
67     "        <version>3.5</version>\n"
68     "        <interface>\n"
69     "            <name>IBetterCamera</name>\n"
70     "            <instance>camera</instance>\n"
71     "        </interface>\n"
72     "        <interface>\n"
73     "            <name>ICamera</name>\n"
74     "            <instance>default</instance>\n"
75     "            <instance>legacy/0</instance>\n"
76     "        </interface>\n"
77     "    </hal>\n"
78     "    <hal format=\"hidl\">\n"
79     "        <name>android.hardware.nfc</name>\n"
80     "        <transport>hwbinder</transport>\n"
81     "        <version>1.0</version>\n"
82     "        <interface>\n"
83     "            <name>INfc</name>\n"
84     "            <instance>nfc_nci</instance>\n"
85     "        </interface>\n"
86     "    </hal>\n"
87     "    <hal format=\"hidl\">\n"
88     "        <name>android.hardware.nfc</name>\n"
89     "        <transport>hwbinder</transport>\n"
90     "        <version>2.0</version>\n"
91     "        <interface>\n"
92     "            <name>INfc</name>\n"
93     "            <instance>default</instance>\n"
94     "            <instance>nfc_nci</instance>\n"
95     "        </interface>\n"
96     "    </hal>\n"
97     "    <sepolicy>\n"
98     "        <version>25.5</version>\n"
99     "    </sepolicy>\n"
100     "</manifest>\n";
101 
102 const std::string systemManifestXml1 =
103     "<manifest version=\"1.0\" type=\"framework\">\n"
104     "    <hal format=\"hidl\">\n"
105     "        <name>android.hidl.manager</name>\n"
106     "        <transport>hwbinder</transport>\n"
107     "        <version>1.0</version>\n"
108     "        <interface>\n"
109     "            <name>IServiceManager</name>\n"
110     "            <instance>default</instance>\n"
111     "        </interface>\n"
112     "    </hal>\n"
113     "    <vndk>\n"
114     "        <version>25.0.5</version>\n"
115     "        <library>libbase.so</library>\n"
116     "        <library>libjpeg.so</library>\n"
117     "    </vndk>\n"
118     "</manifest>\n";
119 
120 const std::string vendorMatrixXml1 =
121     "<compatibility-matrix version=\"1.0\" type=\"device\">\n"
122     "    <hal format=\"hidl\" optional=\"false\">\n"
123     "        <name>android.hidl.manager</name>\n"
124     "        <version>1.0</version>\n"
125     "    </hal>\n"
126     "    <vndk>\n"
127     "        <version>25.0.1-5</version>\n"
128     "        <library>libbase.so</library>\n"
129     "        <library>libjpeg.so</library>\n"
130     "    </vndk>\n"
131     "</compatibility-matrix>\n";
132 
133 //
134 // Set of Xml2 metadata compatible with each other.
135 //
136 
137 const std::string systemMatrixXml2 =
138     "<compatibility-matrix version=\"1.0\" type=\"framework\">\n"
139     "    <hal format=\"hidl\">\n"
140     "        <name>android.hardware.foo</name>\n"
141     "        <version>1.0</version>\n"
142     "    </hal>\n"
143     "    <kernel version=\"3.18.31\"></kernel>\n"
144     "    <sepolicy>\n"
145     "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
146     "        <sepolicy-version>25.5</sepolicy-version>\n"
147     "        <sepolicy-version>26.0-3</sepolicy-version>\n"
148     "    </sepolicy>\n"
149     "    <avb>\n"
150     "        <vbmeta-version>0.0</vbmeta-version>\n"
151     "    </avb>\n"
152     "</compatibility-matrix>\n";
153 
154 const std::string vendorManifestXml2 =
155     "<manifest version=\"1.0\" type=\"device\">"
156     "    <hal>"
157     "        <name>android.hardware.foo</name>"
158     "        <transport>hwbinder</transport>"
159     "        <version>1.0</version>"
160     "    </hal>"
161     "    <sepolicy>\n"
162     "        <version>25.5</version>\n"
163     "    </sepolicy>\n"
164     "</manifest>";
165 
166 // Setup the MockFileFetcher used by the fetchAllInformation template
167 // 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)168 void setupMockFetcher(const std::string& vendorManifestXml, const std::string& systemMatrixXml,
169                       const std::string& systemManifestXml, const std::string& vendorMatrixXml) {
170     MockFileFetcher* fetcher = static_cast<MockFileFetcher*>(gFetcher);
171 
172     ON_CALL(*fetcher, fetch(StrEq("/vendor/manifest.xml"), _))
173         .WillByDefault(Invoke([vendorManifestXml](const std::string& path, std::string& fetched) {
174             (void)path;
175             fetched = vendorManifestXml;
176             return 0;
177         }));
178     ON_CALL(*fetcher, fetch(StrEq("/system/manifest.xml"), _))
179         .WillByDefault(Invoke([systemManifestXml](const std::string& path, std::string& fetched) {
180             (void)path;
181             fetched = systemManifestXml;
182             return 0;
183         }));
184     ON_CALL(*fetcher, fetch(StrEq("/vendor/compatibility_matrix.xml"), _))
185         .WillByDefault(Invoke([vendorMatrixXml](const std::string& path, std::string& fetched) {
186             (void)path;
187             fetched = vendorMatrixXml;
188             return 0;
189         }));
190     ON_CALL(*fetcher, fetch(StrEq("/system/compatibility_matrix.xml"), _))
191         .WillByDefault(Invoke([systemMatrixXml](const std::string& path, std::string& fetched) {
192             (void)path;
193             fetched = systemMatrixXml;
194             return 0;
195         }));
196 }
197 
mounter()198 static MockPartitionMounter &mounter() {
199     return *static_cast<MockPartitionMounter *>(gPartitionMounter);
200 }
fetcher()201 static MockFileFetcher &fetcher() {
202     return *static_cast<MockFileFetcher*>(gFetcher);
203 }
204 
205 // Test fixture that provides compatible metadata from the mock device.
206 class VintfObjectCompatibleTest : public testing::Test {
207    protected:
SetUp()208     virtual void SetUp() {
209         mounter().reset();
210         setupMockFetcher(vendorManifestXml1, systemMatrixXml1, systemManifestXml1,
211                          vendorMatrixXml1);
212     }
TearDown()213     virtual void TearDown() {
214         Mock::VerifyAndClear(&mounter());
215         Mock::VerifyAndClear(&fetcher());
216     }
217 
218 };
219 
220 // Tests that local info is checked.
TEST_F(VintfObjectCompatibleTest,TestDeviceCompatibility)221 TEST_F(VintfObjectCompatibleTest, TestDeviceCompatibility) {
222     std::string error;
223     std::vector<std::string> packageInfo;
224 
225     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/manifest.xml"), _));
226     EXPECT_CALL(fetcher(), fetch(StrEq("/system/manifest.xml"), _));
227     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/compatibility_matrix.xml"), _));
228     EXPECT_CALL(fetcher(), fetch(StrEq("/system/compatibility_matrix.xml"), _));
229     EXPECT_CALL(mounter(), mountSystem()).Times(0);
230     EXPECT_CALL(mounter(), umountSystem()).Times(0);
231     EXPECT_CALL(mounter(), mountVendor()).Times(0);
232     EXPECT_CALL(mounter(), umountVendor()).Times(0);
233 
234     int result = VintfObject::CheckCompatibility(packageInfo, &error);
235 
236     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
237     // Check that nothing was ignored.
238     ASSERT_STREQ(error.c_str(), "");
239     EXPECT_FALSE(mounter().systemMounted());
240     EXPECT_FALSE(mounter().vendorMounted());
241 }
242 
TEST_F(VintfObjectCompatibleTest,TestDeviceCompatibilityMount)243 TEST_F(VintfObjectCompatibleTest, TestDeviceCompatibilityMount) {
244     std::string error;
245     std::vector<std::string> packageInfo;
246 
247     EXPECT_CALL(mounter(), mountSystem()).Times(2);
248     EXPECT_CALL(mounter(), umountSystem()).Times(1); // Should only umount once
249     EXPECT_CALL(mounter(), mountVendor()).Times(2);
250     EXPECT_CALL(mounter(), umountVendor()).Times(1);
251 
252     int result = details::checkCompatibility(packageInfo, true /* mount */, mounter(), &error);
253 
254     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
255     EXPECT_FALSE(mounter().systemMounted());
256     EXPECT_FALSE(mounter().vendorMounted());
257 }
258 
259 // Tests that input info is checked against device and passes.
TEST_F(VintfObjectCompatibleTest,TestInputVsDeviceSuccess)260 TEST_F(VintfObjectCompatibleTest, TestInputVsDeviceSuccess) {
261     std::string error;
262     std::vector<std::string> packageInfo = {systemMatrixXml1};
263 
264     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/manifest.xml"), _));
265     EXPECT_CALL(fetcher(), fetch(StrEq("/system/manifest.xml"), _));
266     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/compatibility_matrix.xml"), _));
267     EXPECT_CALL(fetcher(), fetch(StrEq("/system/compatibility_matrix.xml"), _)).Times(0);
268     EXPECT_CALL(mounter(), mountSystem()).Times(0);
269     EXPECT_CALL(mounter(), umountSystem()).Times(0);
270     EXPECT_CALL(mounter(), mountVendor()).Times(0);
271     EXPECT_CALL(mounter(), umountVendor()).Times(0);
272 
273     int result = VintfObject::CheckCompatibility(packageInfo, &error);
274 
275     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
276     ASSERT_STREQ(error.c_str(), "");
277     EXPECT_FALSE(mounter().systemMounted());
278     EXPECT_FALSE(mounter().vendorMounted());
279 }
280 
TEST_F(VintfObjectCompatibleTest,TestInputVsDeviceSuccessMount)281 TEST_F(VintfObjectCompatibleTest, TestInputVsDeviceSuccessMount) {
282     std::string error;
283     std::vector<std::string> packageInfo = {systemMatrixXml1};
284 
285     EXPECT_CALL(mounter(), mountSystem()).Times(1); // Should only mount once for manifest
286     EXPECT_CALL(mounter(), umountSystem()).Times(1);
287     EXPECT_CALL(mounter(), mountVendor()).Times(2);
288     EXPECT_CALL(mounter(), umountVendor()).Times(1);
289 
290     int result = details::checkCompatibility(packageInfo, true /* mount */, mounter(), &error);
291 
292     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
293     EXPECT_FALSE(mounter().systemMounted());
294     EXPECT_FALSE(mounter().vendorMounted());
295 }
296 
297 // Tests that input info is checked against device and fails.
TEST_F(VintfObjectCompatibleTest,TestInputVsDeviceFail)298 TEST_F(VintfObjectCompatibleTest, TestInputVsDeviceFail) {
299     std::string error;
300     std::vector<std::string> packageInfo = {systemMatrixXml2};
301 
302     int result = VintfObject::CheckCompatibility(packageInfo, &error);
303 
304     ASSERT_EQ(result, 1) << "Should have failed:" << error.c_str();
305     ASSERT_STREQ(error.c_str(),
306                  "Device manifest and framework compatibility matrix are incompatible: HALs "
307                  "incompatible. android.hardware.foo");
308 }
309 
310 // Tests that complementary info is checked against itself.
TEST_F(VintfObjectCompatibleTest,TestInputSuccess)311 TEST_F(VintfObjectCompatibleTest, TestInputSuccess) {
312     std::string error;
313     std::vector<std::string> packageInfo = {systemMatrixXml2, vendorManifestXml2};
314 
315     int result = VintfObject::CheckCompatibility(packageInfo, &error);
316 
317     ASSERT_EQ(result, 0) << "Failed message:" << error.c_str();
318     ASSERT_STREQ(error.c_str(), "");
319 }
320 
TEST_F(VintfObjectCompatibleTest,TestFrameworkOnlyOta)321 TEST_F(VintfObjectCompatibleTest, TestFrameworkOnlyOta) {
322     std::string error;
323     std::vector<std::string> packageInfo = {systemMatrixXml1, systemManifestXml1};
324 
325     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/manifest.xml"), _));
326     EXPECT_CALL(fetcher(), fetch(StrEq("/system/manifest.xml"), _)).Times(0);
327     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/compatibility_matrix.xml"), _));
328     EXPECT_CALL(fetcher(), fetch(StrEq("/system/compatibility_matrix.xml"), _)).Times(0);
329     EXPECT_CALL(mounter(), mountSystem()).Times(0);
330     EXPECT_CALL(mounter(), umountSystem()).Times(0);
331     EXPECT_CALL(mounter(), mountVendor()).Times(0);
332     EXPECT_CALL(mounter(), umountVendor()).Times(0);
333 
334     int result = VintfObject::CheckCompatibility(packageInfo, &error);
335 
336     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
337     ASSERT_STREQ(error.c_str(), "");
338     EXPECT_FALSE(mounter().systemMounted());
339     EXPECT_FALSE(mounter().vendorMounted());
340 }
341 
TEST_F(VintfObjectCompatibleTest,TestFrameworkOnlyOtaMount)342 TEST_F(VintfObjectCompatibleTest, TestFrameworkOnlyOtaMount) {
343     std::string error;
344     std::vector<std::string> packageInfo = {systemMatrixXml1, systemManifestXml1};
345 
346     EXPECT_CALL(mounter(), mountSystem()).Times(0);
347     EXPECT_CALL(mounter(), umountSystem()).Times(1);
348     EXPECT_CALL(mounter(), mountVendor()).Times(2);
349     EXPECT_CALL(mounter(), umountVendor()).Times(1);
350 
351     int result = details::checkCompatibility(packageInfo, true /* mount */, mounter(), &error);
352 
353     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
354     EXPECT_FALSE(mounter().systemMounted());
355     EXPECT_FALSE(mounter().vendorMounted());
356 }
357 
TEST_F(VintfObjectCompatibleTest,TestFullOta)358 TEST_F(VintfObjectCompatibleTest, TestFullOta) {
359     std::string error;
360     std::vector<std::string> packageInfo = {systemMatrixXml1, systemManifestXml1,
361             vendorMatrixXml1, vendorManifestXml1};
362 
363     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/manifest.xml"), _)).Times(0);
364     EXPECT_CALL(fetcher(), fetch(StrEq("/system/manifest.xml"), _)).Times(0);
365     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/compatibility_matrix.xml"), _)).Times(0);
366     EXPECT_CALL(fetcher(), fetch(StrEq("/system/compatibility_matrix.xml"), _)).Times(0);
367     EXPECT_CALL(mounter(), mountSystem()).Times(0);
368     EXPECT_CALL(mounter(), umountSystem()).Times(0);
369     EXPECT_CALL(mounter(), mountVendor()).Times(0);
370     EXPECT_CALL(mounter(), umountVendor()).Times(0);
371 
372     int result = VintfObject::CheckCompatibility(packageInfo, &error);
373 
374     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
375     ASSERT_STREQ(error.c_str(), "");
376     EXPECT_FALSE(mounter().systemMounted());
377     EXPECT_FALSE(mounter().vendorMounted());
378 }
379 
TEST_F(VintfObjectCompatibleTest,TestFullOnlyOtaMount)380 TEST_F(VintfObjectCompatibleTest, TestFullOnlyOtaMount) {
381     std::string error;
382     std::vector<std::string> packageInfo = {systemMatrixXml1, systemManifestXml1,
383             vendorMatrixXml1, vendorManifestXml1};
384 
385     EXPECT_CALL(mounter(), mountSystem()).Times(0);
386     EXPECT_CALL(mounter(), umountSystem()).Times(1);
387     EXPECT_CALL(mounter(), mountVendor()).Times(0);
388     EXPECT_CALL(mounter(), umountVendor()).Times(1);
389 
390     int result = details::checkCompatibility(packageInfo, true /* mount */, mounter(), &error);
391 
392     ASSERT_EQ(result, 0) << "Fail message:" << error.c_str();
393     EXPECT_FALSE(mounter().systemMounted());
394     EXPECT_FALSE(mounter().vendorMounted());
395 }
396 
397 // Test fixture that provides incompatible metadata from the mock device.
398 class VintfObjectIncompatibleTest : public testing::Test {
399    protected:
SetUp()400     virtual void SetUp() {
401         mounter().reset();
402         setupMockFetcher(vendorManifestXml1, systemMatrixXml2, systemManifestXml1,
403                          vendorMatrixXml1);
404     }
TearDown()405     virtual void TearDown() {
406         Mock::VerifyAndClear(&mounter());
407         Mock::VerifyAndClear(&fetcher());
408     }
409 };
410 
411 // Fetch all metadata from device and ensure that it fails.
TEST_F(VintfObjectIncompatibleTest,TestDeviceCompatibility)412 TEST_F(VintfObjectIncompatibleTest, TestDeviceCompatibility) {
413     std::string error;
414     std::vector<std::string> packageInfo;
415 
416     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/manifest.xml"), _));
417     EXPECT_CALL(fetcher(), fetch(StrEq("/system/manifest.xml"), _));
418     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/compatibility_matrix.xml"), _));
419     EXPECT_CALL(fetcher(), fetch(StrEq("/system/compatibility_matrix.xml"), _));
420 
421     int result = VintfObject::CheckCompatibility(packageInfo, &error);
422 
423     ASSERT_EQ(result, 1) << "Should have failed:" << error.c_str();
424 }
425 
426 // Pass in new metadata that fixes the incompatibility.
TEST_F(VintfObjectIncompatibleTest,TestInputVsDeviceSuccess)427 TEST_F(VintfObjectIncompatibleTest, TestInputVsDeviceSuccess) {
428     std::string error;
429     std::vector<std::string> packageInfo = {systemMatrixXml1};
430 
431     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/manifest.xml"), _));
432     EXPECT_CALL(fetcher(), fetch(StrEq("/system/manifest.xml"), _));
433     EXPECT_CALL(fetcher(), fetch(StrEq("/vendor/compatibility_matrix.xml"), _));
434     EXPECT_CALL(fetcher(), fetch(StrEq("/system/compatibility_matrix.xml"), _)).Times(0);
435 
436     int result = VintfObject::CheckCompatibility(packageInfo, &error);
437 
438     ASSERT_EQ(result, 0) << "Failed message:" << error.c_str();
439     ASSERT_STREQ(error.c_str(), "");
440 }
441 
main(int argc,char ** argv)442 int main(int argc, char** argv) {
443     ::testing::InitGoogleMock(&argc, argv);
444 
445     NiceMock<MockFileFetcher> fetcher;
446     gFetcher = &fetcher;
447 
448     NiceMock<MockPartitionMounter> mounter;
449     gPartitionMounter = &mounter;
450 
451     return RUN_ALL_TESTS();
452 }
453