1 /*
2  * Copyright (C) 2018 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 // SystemVendorTest test cases that runs on P+ vendor.
18 
19 #include "SystemVendorTest.h"
20 
21 #include <android-base/logging.h>
22 #include <android-base/strings.h>
23 #include <vintf/VintfObject.h>
24 #include <iostream>
25 
26 #include "SingleManifestTest.h"
27 
28 namespace android {
29 namespace vintf {
30 namespace testing {
31 
32 using std::endl;
33 
34 // Tests that device manifest and framework compatibility matrix are compatible.
TEST_F(SystemVendorTest,DeviceManifestFrameworkMatrixCompatibility)35 TEST_F(SystemVendorTest, DeviceManifestFrameworkMatrixCompatibility) {
36   auto device_manifest = VintfObject::GetDeviceHalManifest();
37   ASSERT_NE(device_manifest, nullptr) << "Failed to get device HAL manifest.";
38   auto fwk_matrix = VintfObject::GetFrameworkCompatibilityMatrix();
39   ASSERT_NE(fwk_matrix, nullptr)
40       << "Failed to get framework compatibility matrix.";
41 
42   string error;
43   EXPECT_TRUE(device_manifest->checkCompatibility(*fwk_matrix, &error))
44       << error;
45 }
46 
47 // Tests that framework manifest and device compatibility matrix are compatible.
TEST_F(SystemVendorTest,FrameworkManifestDeviceMatrixCompatibility)48 TEST_F(SystemVendorTest, FrameworkManifestDeviceMatrixCompatibility) {
49   auto fwk_manifest = VintfObject::GetFrameworkHalManifest();
50   ASSERT_NE(fwk_manifest, nullptr) << "Failed to get framework HAL manifest.";
51   auto device_matrix = VintfObject::GetDeviceCompatibilityMatrix();
52   ASSERT_NE(device_matrix, nullptr)
53       << "Failed to get device compatibility matrix.";
54 
55   string error;
56   EXPECT_TRUE(fwk_manifest->checkCompatibility(*device_matrix, &error))
57       << error;
58 }
59 
60 // Tests that framework compatibility matrix and runtime info are compatible.
61 // AVB version is not a compliance requirement.
TEST_F(SystemVendorTest,FrameworkMatrixDeviceRuntimeCompatibility)62 TEST_F(SystemVendorTest, FrameworkMatrixDeviceRuntimeCompatibility) {
63   auto fwk_matrix = VintfObject::GetFrameworkCompatibilityMatrix();
64   ASSERT_NE(fwk_matrix, nullptr)
65       << "Failed to get framework compatibility matrix.";
66   auto runtime_info = VintfObject::GetRuntimeInfo();
67   ASSERT_NE(nullptr, runtime_info) << "Failed to get runtime info.";
68 
69   string error;
70   EXPECT_TRUE(runtime_info->checkCompatibility(
71       *fwk_matrix, &error,
72       ::android::vintf::CheckFlags::ENABLE_ALL_CHECKS.disableAvb()
73           .disableKernel()))
74       << error;
75 }
76 
77 // Tests that runtime kernel matches requirements in compatibility matrix.
78 // This includes testing kernel version and kernel configurations.
TEST_F(SystemVendorTest,KernelCompatibility)79 TEST_F(SystemVendorTest, KernelCompatibility) {
80   auto fwk_matrix = VintfObject::GetFrameworkCompatibilityMatrix();
81   ASSERT_NE(fwk_matrix, nullptr)
82       << "Failed to get framework compatibility matrix.";
83   auto runtime_info = VintfObject::GetRuntimeInfo();
84   ASSERT_NE(nullptr, runtime_info) << "Failed to get runtime info.";
85 
86   string error;
87   EXPECT_TRUE(runtime_info->checkCompatibility(
88       *fwk_matrix, &error,
89       ::android::vintf::CheckFlags::DISABLE_ALL_CHECKS.enableKernel()))
90       << error;
91 }
92 
TEST_F(SystemVendorTest,NoMainlineKernel)93 TEST_F(SystemVendorTest, NoMainlineKernel) {
94   auto runtime_info = VintfObject::GetRuntimeInfo();
95   ASSERT_NE(nullptr, runtime_info) << "Failed to get runtime info.";
96   ASSERT_FALSE(runtime_info->isMainlineKernel())
97       << "uname returns \"" << runtime_info->osRelease()
98       << "\". Mainline kernel is not allowed.";
99 }
100 
101 // Tests that vendor and framework are compatible.
102 // If any of the other tests in SystemVendorTest fails, this test will fail as
103 // well. This is a double check in case the sub-tests do not cover some
104 // checks.
105 // AVB version is not a compliance requirement.
TEST_F(SystemVendorTest,VendorFrameworkCompatibility)106 TEST_F(SystemVendorTest, VendorFrameworkCompatibility) {
107   string error;
108   EXPECT_EQ(
109       android::vintf::COMPATIBLE,
110       VintfObject::GetInstance()->checkCompatibility(
111           &error, ::android::vintf::CheckFlags::ENABLE_ALL_CHECKS.disableAvb()))
112       << error;
113 }
114 
115 template <typename D, typename S>
insert(D * d,const S & s)116 static void insert(D *d, const S &s) {
117   d->insert(s.begin(), s.end());
118 }
119 
120 // This needs to be tested besides
121 // SingleManifestTest.ServedHwbinderHalsAreInManifest because some HALs may
122 // refuse to provide its PID, and the partition cannot be inferred.
TEST_F(SystemVendorTest,ServedHwbinderHalsAreInManifest)123 TEST_F(SystemVendorTest, ServedHwbinderHalsAreInManifest) {
124   auto device_manifest = VintfObject::GetDeviceHalManifest();
125   ASSERT_NE(device_manifest, nullptr) << "Failed to get device HAL manifest.";
126   auto fwk_manifest = VintfObject::GetFrameworkHalManifest();
127   ASSERT_NE(fwk_manifest, nullptr) << "Failed to get framework HAL manifest.";
128 
129   std::set<std::string> manifest_hwbinder_hals;
130 
131   insert(&manifest_hwbinder_hals, GetHwbinderHals(fwk_manifest));
132   insert(&manifest_hwbinder_hals, GetHwbinderHals(device_manifest));
133 
134   Return<void> ret = default_manager_->list([&](const auto &list) {
135     for (const auto &name : list) {
136       // TODO(b/73774955): use standardized parsing code for fqinstancename
137       if (std::string(name).find(IBase::descriptor) == 0) continue;
138 
139       EXPECT_NE(manifest_hwbinder_hals.find(name), manifest_hwbinder_hals.end())
140           << name << " is being served, but it is not in a manifest.";
141     }
142   });
143   EXPECT_TRUE(ret.isOk());
144 }
145 
GetTestManifests()146 static std::vector<HalManifestPtr> GetTestManifests() {
147   return {
148       VintfObject::GetFrameworkHalManifest(),
149   };
150 }
151 
152 INSTANTIATE_TEST_CASE_P(FrameworkManifest, SingleManifestTest,
153                         ::testing::ValuesIn(GetTestManifests()));
154 
155 }  // namespace testing
156 }  // namespace vintf
157 }  // namespace android
158