Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "Lshal" |
| 18 | #include <android-base/logging.h> |
| 19 | |
| 20 | #include <sstream> |
| 21 | #include <string> |
| 22 | #include <thread> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <gmock/gmock.h> |
| 27 | #include <android/hardware/tests/baz/1.0/IQuux.h> |
| 28 | #include <hidl/HidlTransportSupport.h> |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 29 | #include <vintf/parse_xml.h> |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 30 | |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 31 | #include "ListCommand.h" |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 32 | #include "Lshal.h" |
| 33 | |
| 34 | #define NELEMS(array) static_cast<int>(sizeof(array) / sizeof(array[0])) |
| 35 | |
| 36 | using namespace testing; |
| 37 | |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 38 | using ::android::hidl::base::V1_0::DebugInfo; |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 39 | using ::android::hidl::base::V1_0::IBase; |
| 40 | using ::android::hidl::manager::V1_0::IServiceManager; |
| 41 | using ::android::hidl::manager::V1_0::IServiceNotification; |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 42 | using ::android::hardware::hidl_array; |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 43 | using ::android::hardware::hidl_death_recipient; |
| 44 | using ::android::hardware::hidl_handle; |
| 45 | using ::android::hardware::hidl_string; |
| 46 | using ::android::hardware::hidl_vec; |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 47 | using android::vintf::Arch; |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 48 | using android::vintf::CompatibilityMatrix; |
| 49 | using android::vintf::gCompatibilityMatrixConverter; |
| 50 | using android::vintf::gHalManifestConverter; |
| 51 | using android::vintf::HalManifest; |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 52 | using android::vintf::Transport; |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 53 | using android::vintf::VintfObject; |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 54 | |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 55 | using InstanceDebugInfo = IServiceManager::InstanceDebugInfo; |
| 56 | |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 57 | using hidl_hash = hidl_array<uint8_t, 32>; |
| 58 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 59 | namespace android { |
| 60 | namespace hardware { |
| 61 | namespace tests { |
| 62 | namespace baz { |
| 63 | namespace V1_0 { |
| 64 | namespace implementation { |
| 65 | struct Quux : android::hardware::tests::baz::V1_0::IQuux { |
| 66 | ::android::hardware::Return<void> debug(const hidl_handle& hh, const hidl_vec<hidl_string>& options) override { |
| 67 | const native_handle_t *handle = hh.getNativeHandle(); |
| 68 | if (handle->numFds < 1) { |
| 69 | return Void(); |
| 70 | } |
| 71 | int fd = handle->data[0]; |
| 72 | std::string content{descriptor}; |
| 73 | for (const auto &option : options) { |
| 74 | content += "\n"; |
| 75 | content += option.c_str(); |
| 76 | } |
| 77 | ssize_t written = write(fd, content.c_str(), content.size()); |
| 78 | if (written != (ssize_t)content.size()) { |
| 79 | LOG(WARNING) << "SERVER(Quux) debug writes " << written << " bytes < " |
| 80 | << content.size() << " bytes, errno = " << errno; |
| 81 | } |
| 82 | return Void(); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | } // namespace implementation |
| 87 | } // namespace V1_0 |
| 88 | } // namespace baz |
| 89 | } // namespace tests |
| 90 | } // namespace hardware |
| 91 | |
| 92 | namespace lshal { |
| 93 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 94 | class MockServiceManager : public IServiceManager { |
| 95 | public: |
| 96 | template<typename T> |
| 97 | using R = ::android::hardware::Return<T>; |
| 98 | using String = const hidl_string&; |
| 99 | ~MockServiceManager() = default; |
| 100 | |
| 101 | #define MOCK_METHOD_CB(name) MOCK_METHOD1(name, R<void>(IServiceManager::name##_cb)) |
| 102 | |
| 103 | MOCK_METHOD2(get, R<sp<IBase>>(String, String)); |
| 104 | MOCK_METHOD2(add, R<bool>(String, const sp<IBase>&)); |
| 105 | MOCK_METHOD2(getTransport, R<IServiceManager::Transport>(String, String)); |
| 106 | MOCK_METHOD_CB(list); |
| 107 | MOCK_METHOD2(listByInterface, R<void>(String, listByInterface_cb)); |
| 108 | MOCK_METHOD3(registerForNotifications, R<bool>(String, String, const sp<IServiceNotification>&)); |
| 109 | MOCK_METHOD_CB(debugDump); |
| 110 | MOCK_METHOD2(registerPassthroughClient, R<void>(String, String)); |
| 111 | MOCK_METHOD_CB(interfaceChain); |
| 112 | MOCK_METHOD2(debug, R<void>(const hidl_handle&, const hidl_vec<hidl_string>&)); |
| 113 | MOCK_METHOD_CB(interfaceDescriptor); |
| 114 | MOCK_METHOD_CB(getHashChain); |
| 115 | MOCK_METHOD0(setHalInstrumentation, R<void>()); |
| 116 | MOCK_METHOD2(linkToDeath, R<bool>(const sp<hidl_death_recipient>&, uint64_t)); |
| 117 | MOCK_METHOD0(ping, R<void>()); |
| 118 | MOCK_METHOD_CB(getDebugInfo); |
| 119 | MOCK_METHOD0(notifySyspropsChanged, R<void>()); |
| 120 | MOCK_METHOD1(unlinkToDeath, R<bool>(const sp<hidl_death_recipient>&)); |
| 121 | |
| 122 | }; |
| 123 | |
Yifan Hong | bf20a26 | 2017-09-07 11:10:58 -0700 | [diff] [blame] | 124 | class DebugTest : public ::testing::Test { |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 125 | public: |
| 126 | void SetUp() override { |
| 127 | using ::android::hardware::tests::baz::V1_0::IQuux; |
| 128 | using ::android::hardware::tests::baz::V1_0::implementation::Quux; |
| 129 | |
| 130 | err.str(""); |
| 131 | out.str(""); |
| 132 | serviceManager = new testing::NiceMock<MockServiceManager>(); |
| 133 | ON_CALL(*serviceManager, get(_, _)).WillByDefault(Invoke( |
| 134 | [](const auto &iface, const auto &inst) -> ::android::hardware::Return<sp<IBase>> { |
| 135 | if (iface == IQuux::descriptor && inst == "default") |
| 136 | return new Quux(); |
| 137 | return nullptr; |
| 138 | })); |
Yifan Hong | bf20a26 | 2017-09-07 11:10:58 -0700 | [diff] [blame] | 139 | |
| 140 | lshal = std::make_unique<Lshal>(out, err, serviceManager, serviceManager); |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 141 | } |
| 142 | void TearDown() override {} |
| 143 | |
| 144 | std::stringstream err; |
| 145 | std::stringstream out; |
| 146 | sp<MockServiceManager> serviceManager; |
Yifan Hong | bf20a26 | 2017-09-07 11:10:58 -0700 | [diff] [blame] | 147 | |
| 148 | std::unique_ptr<Lshal> lshal; |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
Yifan Hong | bf20a26 | 2017-09-07 11:10:58 -0700 | [diff] [blame] | 151 | static Arg createArg(const std::vector<const char*>& args) { |
| 152 | return Arg{static_cast<int>(args.size()), const_cast<char**>(args.data())}; |
| 153 | } |
| 154 | |
| 155 | template<typename T> |
| 156 | static Status callMain(const std::unique_ptr<T>& lshal, const std::vector<const char*>& args) { |
| 157 | return lshal->main(createArg(args)); |
| 158 | } |
| 159 | |
| 160 | TEST_F(DebugTest, Debug) { |
| 161 | EXPECT_EQ(0u, callMain(lshal, { |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 162 | "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux/default", "foo", "bar" |
Yifan Hong | bf20a26 | 2017-09-07 11:10:58 -0700 | [diff] [blame] | 163 | })); |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 164 | EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nfoo\nbar")); |
| 165 | EXPECT_THAT(err.str(), IsEmpty()); |
| 166 | } |
| 167 | |
Yifan Hong | bf20a26 | 2017-09-07 11:10:58 -0700 | [diff] [blame] | 168 | TEST_F(DebugTest, Debug2) { |
| 169 | EXPECT_EQ(0u, callMain(lshal, { |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 170 | "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux", "baz", "quux" |
Yifan Hong | bf20a26 | 2017-09-07 11:10:58 -0700 | [diff] [blame] | 171 | })); |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 172 | EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nbaz\nquux")); |
| 173 | EXPECT_THAT(err.str(), IsEmpty()); |
| 174 | } |
| 175 | |
Yifan Hong | bf20a26 | 2017-09-07 11:10:58 -0700 | [diff] [blame] | 176 | TEST_F(DebugTest, Debug3) { |
| 177 | EXPECT_NE(0u, callMain(lshal, { |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 178 | "lshal", "debug", "android.hardware.tests.doesnotexist@1.0::IDoesNotExist", |
Yifan Hong | bf20a26 | 2017-09-07 11:10:58 -0700 | [diff] [blame] | 179 | })); |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 180 | EXPECT_THAT(err.str(), HasSubstr("does not exist")); |
| 181 | } |
| 182 | |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 183 | class MockLshal : public Lshal { |
| 184 | public: |
| 185 | MockLshal() {} |
| 186 | ~MockLshal() = default; |
| 187 | MOCK_CONST_METHOD0(out, NullableOStream<std::ostream>()); |
| 188 | MOCK_CONST_METHOD0(err, NullableOStream<std::ostream>()); |
| 189 | }; |
| 190 | |
| 191 | // expose protected fields and methods for ListCommand |
| 192 | class MockListCommand : public ListCommand { |
| 193 | public: |
| 194 | MockListCommand(Lshal* lshal) : ListCommand(*lshal) {} |
| 195 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 196 | Status parseArgs(const Arg& arg) { return ListCommand::parseArgs(arg); } |
| 197 | Status main(const Arg& arg) { return ListCommand::main(arg); } |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 198 | void forEachTable(const std::function<void(Table &)> &f) { |
| 199 | return ListCommand::forEachTable(f); |
| 200 | } |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 201 | void forEachTable(const std::function<void(const Table &)> &f) const { |
| 202 | return ListCommand::forEachTable(f); |
| 203 | } |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 204 | Status fetch() { return ListCommand::fetch(); } |
| 205 | void dumpVintf(const NullableOStream<std::ostream>& out) { |
| 206 | return ListCommand::dumpVintf(out); |
| 207 | } |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 208 | void internalPostprocess() { ListCommand::postprocess(); } |
Yifan Hong | 1243dde | 2017-09-14 17:49:30 -0700 | [diff] [blame] | 209 | const PidInfo* getPidInfoCached(pid_t serverPid) { |
| 210 | return ListCommand::getPidInfoCached(serverPid); |
| 211 | } |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 212 | |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 213 | MOCK_METHOD0(postprocess, void()); |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 214 | MOCK_CONST_METHOD2(getPidInfo, bool(pid_t, PidInfo*)); |
| 215 | MOCK_CONST_METHOD1(parseCmdline, std::string(pid_t)); |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 216 | MOCK_METHOD1(getPartition, Partition(pid_t)); |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 217 | |
| 218 | MOCK_CONST_METHOD0(getDeviceManifest, std::shared_ptr<const vintf::HalManifest>()); |
| 219 | MOCK_CONST_METHOD0(getDeviceMatrix, std::shared_ptr<const vintf::CompatibilityMatrix>()); |
| 220 | MOCK_CONST_METHOD0(getFrameworkManifest, std::shared_ptr<const vintf::HalManifest>()); |
| 221 | MOCK_CONST_METHOD0(getFrameworkMatrix, std::shared_ptr<const vintf::CompatibilityMatrix>()); |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | class ListParseArgsTest : public ::testing::Test { |
| 225 | public: |
| 226 | void SetUp() override { |
| 227 | mockLshal = std::make_unique<NiceMock<MockLshal>>(); |
| 228 | mockList = std::make_unique<MockListCommand>(mockLshal.get()); |
Yifan Hong | c443091 | 2018-06-27 16:48:34 -0700 | [diff] [blame^] | 229 | ON_CALL(*mockLshal, err()).WillByDefault(Return(NullableOStream<std::ostream>(err))); |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 230 | // ListCommand::parseArgs should parse arguments from the second element |
| 231 | optind = 1; |
| 232 | } |
| 233 | std::unique_ptr<MockLshal> mockLshal; |
| 234 | std::unique_ptr<MockListCommand> mockList; |
Yifan Hong | c443091 | 2018-06-27 16:48:34 -0700 | [diff] [blame^] | 235 | std::stringstream err; |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 238 | TEST_F(ListParseArgsTest, Args) { |
| 239 | EXPECT_EQ(0u, mockList->parseArgs(createArg({"lshal", "-p", "-i", "-a", "-c"}))); |
| 240 | mockList->forEachTable([](const Table& table) { |
| 241 | EXPECT_EQ(SelectedColumns({TableColumnType::SERVER_PID, TableColumnType::INTERFACE_NAME, |
| 242 | TableColumnType::SERVER_ADDR, TableColumnType::CLIENT_PIDS}), |
| 243 | table.getSelectedColumns()); |
| 244 | }); |
Yifan Hong | c443091 | 2018-06-27 16:48:34 -0700 | [diff] [blame^] | 245 | EXPECT_EQ("", err.str()); |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | TEST_F(ListParseArgsTest, Cmds) { |
| 249 | EXPECT_EQ(0u, mockList->parseArgs(createArg({"lshal", "-m"}))); |
| 250 | mockList->forEachTable([](const Table& table) { |
Yifan Hong | 7a3b46c | 2017-09-14 18:18:53 -0700 | [diff] [blame] | 251 | EXPECT_THAT(table.getSelectedColumns(), Not(Contains(TableColumnType::SERVER_PID))) |
| 252 | << "should not print server PID with -m"; |
| 253 | EXPECT_THAT(table.getSelectedColumns(), Not(Contains(TableColumnType::CLIENT_PIDS))) |
| 254 | << "should not print client PIDs with -m"; |
| 255 | EXPECT_THAT(table.getSelectedColumns(), Contains(TableColumnType::SERVER_CMD)) |
| 256 | << "should print server cmd with -m"; |
| 257 | EXPECT_THAT(table.getSelectedColumns(), Contains(TableColumnType::CLIENT_CMDS)) |
| 258 | << "should print client cmds with -m"; |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 259 | }); |
Yifan Hong | c443091 | 2018-06-27 16:48:34 -0700 | [diff] [blame^] | 260 | EXPECT_EQ("", err.str()); |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | TEST_F(ListParseArgsTest, DebugAndNeat) { |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 264 | EXPECT_NE(0u, mockList->parseArgs(createArg({"lshal", "--neat", "-d"}))); |
Yifan Hong | c443091 | 2018-06-27 16:48:34 -0700 | [diff] [blame^] | 265 | EXPECT_THAT(err.str(), HasSubstr("--neat should not be used with --debug.")); |
Yifan Hong | b2a2ecb | 2017-09-07 15:08:22 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 268 | /// Fetch Test |
| 269 | |
| 270 | // A set of deterministic functions to generate fake debug infos. |
| 271 | static uint64_t getPtr(pid_t serverId) { return 10000 + serverId; } |
| 272 | static std::vector<pid_t> getClients(pid_t serverId) { |
| 273 | return {serverId + 1, serverId + 3}; |
| 274 | } |
| 275 | static PidInfo getPidInfoFromId(pid_t serverId) { |
| 276 | PidInfo info; |
| 277 | info.refPids[getPtr(serverId)] = getClients(serverId); |
| 278 | info.threadUsage = 10 + serverId; |
| 279 | info.threadCount = 20 + serverId; |
| 280 | return info; |
| 281 | } |
| 282 | static std::string getInterfaceName(pid_t serverId) { |
| 283 | return "a.h.foo" + std::to_string(serverId) + "@" + std::to_string(serverId) + ".0::IFoo"; |
| 284 | } |
| 285 | static std::string getInstanceName(pid_t serverId) { |
| 286 | return std::to_string(serverId); |
| 287 | } |
| 288 | static pid_t getIdFromInstanceName(const hidl_string& instance) { |
| 289 | return atoi(instance.c_str()); |
| 290 | } |
| 291 | static std::string getFqInstanceName(pid_t serverId) { |
| 292 | return getInterfaceName(serverId) + "/" + getInstanceName(serverId); |
| 293 | } |
| 294 | static std::string getCmdlineFromId(pid_t serverId) { |
| 295 | if (serverId == NO_PID) return ""; |
| 296 | return "command_line_" + std::to_string(serverId); |
| 297 | } |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 298 | static bool getIsReleasedFromId(pid_t p) { return p % 2 == 0; } |
| 299 | static hidl_hash getHashFromId(pid_t serverId) { |
| 300 | hidl_hash hash; |
| 301 | bool isReleased = getIsReleasedFromId(serverId); |
| 302 | for (size_t i = 0; i < hash.size(); ++i) { |
| 303 | hash[i] = isReleased ? static_cast<uint8_t>(serverId) : 0u; |
| 304 | } |
| 305 | return hash; |
| 306 | } |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 307 | |
| 308 | // Fake service returned by mocked IServiceManager::get. |
| 309 | class TestService : public IBase { |
| 310 | public: |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 311 | TestService(pid_t id) : mId(id) {} |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 312 | hardware::Return<void> getDebugInfo(getDebugInfo_cb cb) override { |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 313 | cb({ mId /* pid */, getPtr(mId), DebugInfo::Architecture::IS_64BIT }); |
| 314 | return hardware::Void(); |
| 315 | } |
| 316 | hardware::Return<void> interfaceChain(interfaceChain_cb cb) override { |
| 317 | cb({getInterfaceName(mId), IBase::descriptor}); |
| 318 | return hardware::Void(); |
| 319 | } |
| 320 | hardware::Return<void> getHashChain(getHashChain_cb cb) override { |
| 321 | cb({getHashFromId(mId), getHashFromId(0xff)}); |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 322 | return hardware::Void(); |
| 323 | } |
| 324 | private: |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 325 | pid_t mId; |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | class ListTest : public ::testing::Test { |
| 329 | public: |
| 330 | void SetUp() override { |
| 331 | initMockServiceManager(); |
| 332 | lshal = std::make_unique<Lshal>(out, err, serviceManager, passthruManager); |
| 333 | initMockList(); |
| 334 | } |
| 335 | |
| 336 | void initMockList() { |
| 337 | mockList = std::make_unique<NiceMock<MockListCommand>>(lshal.get()); |
| 338 | ON_CALL(*mockList, getPidInfo(_,_)).WillByDefault(Invoke( |
| 339 | [](pid_t serverPid, PidInfo* info) { |
| 340 | *info = getPidInfoFromId(serverPid); |
| 341 | return true; |
| 342 | })); |
| 343 | ON_CALL(*mockList, parseCmdline(_)).WillByDefault(Invoke(&getCmdlineFromId)); |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 344 | ON_CALL(*mockList, postprocess()).WillByDefault(Invoke([&]() { |
| 345 | mockList->internalPostprocess(); |
| 346 | size_t i = 0; |
| 347 | mockList->forEachTable([&](Table& table) { |
| 348 | table.setDescription("[fake description " + std::to_string(i++) + "]"); |
| 349 | }); |
| 350 | })); |
Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 351 | ON_CALL(*mockList, getPartition(_)).WillByDefault(Return(Partition::VENDOR)); |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 352 | |
| 353 | ON_CALL(*mockList, getDeviceManifest()) |
| 354 | .WillByDefault(Return(VintfObject::GetDeviceHalManifest())); |
| 355 | ON_CALL(*mockList, getDeviceMatrix()) |
| 356 | .WillByDefault(Return(VintfObject::GetDeviceCompatibilityMatrix())); |
| 357 | ON_CALL(*mockList, getFrameworkManifest()) |
| 358 | .WillByDefault(Return(VintfObject::GetFrameworkHalManifest())); |
| 359 | ON_CALL(*mockList, getFrameworkMatrix()) |
| 360 | .WillByDefault(Return(VintfObject::GetFrameworkCompatibilityMatrix())); |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void initMockServiceManager() { |
| 364 | serviceManager = new testing::NiceMock<MockServiceManager>(); |
| 365 | passthruManager = new testing::NiceMock<MockServiceManager>(); |
| 366 | using A = DebugInfo::Architecture; |
| 367 | ON_CALL(*serviceManager, list(_)).WillByDefault(Invoke( |
| 368 | [] (IServiceManager::list_cb cb) { |
| 369 | cb({ getFqInstanceName(1), getFqInstanceName(2) }); |
| 370 | return hardware::Void(); |
| 371 | })); |
| 372 | |
| 373 | ON_CALL(*serviceManager, get(_, _)).WillByDefault(Invoke( |
| 374 | [&](const hidl_string&, const hidl_string& instance) { |
| 375 | int id = getIdFromInstanceName(instance); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 376 | return sp<IBase>(new TestService(id)); |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 377 | })); |
| 378 | |
| 379 | ON_CALL(*serviceManager, debugDump(_)).WillByDefault(Invoke( |
| 380 | [] (IServiceManager::debugDump_cb cb) { |
| 381 | cb({InstanceDebugInfo{getInterfaceName(3), getInstanceName(3), 3, |
| 382 | getClients(3), A::IS_32BIT}, |
| 383 | InstanceDebugInfo{getInterfaceName(4), getInstanceName(4), 4, |
| 384 | getClients(4), A::IS_32BIT}}); |
| 385 | return hardware::Void(); |
| 386 | })); |
| 387 | |
| 388 | ON_CALL(*passthruManager, debugDump(_)).WillByDefault(Invoke( |
| 389 | [] (IServiceManager::debugDump_cb cb) { |
| 390 | cb({InstanceDebugInfo{getInterfaceName(5), getInstanceName(5), 5, |
| 391 | getClients(5), A::IS_32BIT}, |
| 392 | InstanceDebugInfo{getInterfaceName(6), getInstanceName(6), 6, |
| 393 | getClients(6), A::IS_32BIT}}); |
| 394 | return hardware::Void(); |
| 395 | })); |
| 396 | } |
| 397 | |
| 398 | std::stringstream err; |
| 399 | std::stringstream out; |
| 400 | std::unique_ptr<Lshal> lshal; |
| 401 | std::unique_ptr<MockListCommand> mockList; |
| 402 | sp<MockServiceManager> serviceManager; |
| 403 | sp<MockServiceManager> passthruManager; |
| 404 | }; |
| 405 | |
Yifan Hong | 1243dde | 2017-09-14 17:49:30 -0700 | [diff] [blame] | 406 | TEST_F(ListTest, GetPidInfoCached) { |
| 407 | EXPECT_CALL(*mockList, getPidInfo(5, _)).Times(1); |
| 408 | |
| 409 | EXPECT_NE(nullptr, mockList->getPidInfoCached(5)); |
| 410 | EXPECT_NE(nullptr, mockList->getPidInfoCached(5)); |
| 411 | } |
| 412 | |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 413 | TEST_F(ListTest, Fetch) { |
| 414 | EXPECT_EQ(0u, mockList->fetch()); |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 415 | vintf::TransportArch hwbinder{Transport::HWBINDER, Arch::ARCH_64}; |
| 416 | vintf::TransportArch passthrough{Transport::PASSTHROUGH, Arch::ARCH_32}; |
| 417 | std::array<vintf::TransportArch, 6> transportArchs{{hwbinder, hwbinder, passthrough, |
| 418 | passthrough, passthrough, passthrough}}; |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 419 | int id = 1; |
| 420 | mockList->forEachTable([&](const Table& table) { |
| 421 | ASSERT_EQ(2u, table.size()); |
| 422 | for (const auto& entry : table) { |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 423 | auto transport = transportArchs.at(id - 1).transport; |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 424 | TableEntry expected{ |
| 425 | .interfaceName = getFqInstanceName(id), |
| 426 | .transport = transport, |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 427 | .serverPid = transport == Transport::HWBINDER ? id : NO_PID, |
| 428 | .threadUsage = |
| 429 | transport == Transport::HWBINDER ? getPidInfoFromId(id).threadUsage : 0, |
| 430 | .threadCount = |
| 431 | transport == Transport::HWBINDER ? getPidInfoFromId(id).threadCount : 0, |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 432 | .serverCmdline = {}, |
Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 433 | .serverObjectAddress = transport == Transport::HWBINDER ? getPtr(id) : NO_PTR, |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 434 | .clientPids = getClients(id), |
| 435 | .clientCmdlines = {}, |
Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 436 | .arch = transportArchs.at(id - 1).arch, |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 437 | }; |
| 438 | EXPECT_EQ(expected, entry) << expected.to_string() << " vs. " << entry.to_string(); |
| 439 | |
| 440 | ++id; |
| 441 | } |
| 442 | }); |
| 443 | |
| 444 | } |
| 445 | |
| 446 | TEST_F(ListTest, DumpVintf) { |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 447 | const std::string expected = "<manifest version=\"1.0\" type=\"device\">\n" |
| 448 | " <hal format=\"hidl\">\n" |
| 449 | " <name>a.h.foo1</name>\n" |
| 450 | " <transport>hwbinder</transport>\n" |
| 451 | " <fqname>@1.0::IFoo/1</fqname>\n" |
| 452 | " </hal>\n" |
| 453 | " <hal format=\"hidl\">\n" |
| 454 | " <name>a.h.foo2</name>\n" |
| 455 | " <transport>hwbinder</transport>\n" |
| 456 | " <fqname>@2.0::IFoo/2</fqname>\n" |
| 457 | " </hal>\n" |
| 458 | " <hal format=\"hidl\">\n" |
| 459 | " <name>a.h.foo3</name>\n" |
| 460 | " <transport arch=\"32\">passthrough</transport>\n" |
| 461 | " <fqname>@3.0::IFoo/3</fqname>\n" |
| 462 | " </hal>\n" |
| 463 | " <hal format=\"hidl\">\n" |
| 464 | " <name>a.h.foo4</name>\n" |
| 465 | " <transport arch=\"32\">passthrough</transport>\n" |
| 466 | " <fqname>@4.0::IFoo/4</fqname>\n" |
| 467 | " </hal>\n" |
| 468 | "</manifest>"; |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 469 | |
| 470 | optind = 1; // mimic Lshal::parseArg() |
| 471 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "--init-vintf"}))); |
Yifan Hong | b2d096a | 2018-05-01 15:25:23 -0700 | [diff] [blame] | 472 | auto output = out.str(); |
| 473 | EXPECT_THAT(output, HasSubstr(expected)); |
| 474 | EXPECT_THAT(output, HasSubstr("a.h.foo5@5.0::IFoo/5")); |
| 475 | EXPECT_THAT(output, HasSubstr("a.h.foo6@6.0::IFoo/6")); |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 476 | EXPECT_EQ("", err.str()); |
| 477 | |
| 478 | vintf::HalManifest m; |
| 479 | EXPECT_EQ(true, vintf::gHalManifestConverter(&m, out.str())) |
| 480 | << "--init-vintf does not emit valid HAL manifest: " |
| 481 | << vintf::gHalManifestConverter.lastError(); |
| 482 | } |
| 483 | |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 484 | // test default columns |
| 485 | TEST_F(ListTest, DumpDefault) { |
| 486 | const std::string expected = |
| 487 | "[fake description 0]\n" |
| 488 | "R Interface Thread Use Server Clients\n" |
Yifan Hong | d5ee11a | 2018-05-25 12:57:04 -0700 | [diff] [blame] | 489 | "N a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n" |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 490 | "Y a.h.foo2@2.0::IFoo/2 12/22 2 3 5\n" |
| 491 | "\n" |
| 492 | "[fake description 1]\n" |
| 493 | "R Interface Thread Use Server Clients\n" |
Yifan Hong | d5ee11a | 2018-05-25 12:57:04 -0700 | [diff] [blame] | 494 | "? a.h.foo3@3.0::IFoo/3 N/A N/A 4 6\n" |
| 495 | "? a.h.foo4@4.0::IFoo/4 N/A N/A 5 7\n" |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 496 | "\n" |
| 497 | "[fake description 2]\n" |
| 498 | "R Interface Thread Use Server Clients\n" |
Yifan Hong | d5ee11a | 2018-05-25 12:57:04 -0700 | [diff] [blame] | 499 | "? a.h.foo5@5.0::IFoo/5 N/A N/A 6 8\n" |
| 500 | "? a.h.foo6@6.0::IFoo/6 N/A N/A 7 9\n" |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 501 | "\n"; |
| 502 | |
| 503 | optind = 1; // mimic Lshal::parseArg() |
| 504 | EXPECT_EQ(0u, mockList->main(createArg({"lshal"}))); |
| 505 | EXPECT_EQ(expected, out.str()); |
| 506 | EXPECT_EQ("", err.str()); |
| 507 | } |
| 508 | |
| 509 | TEST_F(ListTest, DumpHash) { |
| 510 | const std::string expected = |
| 511 | "[fake description 0]\n" |
| 512 | "Interface R Hash\n" |
Yifan Hong | d5ee11a | 2018-05-25 12:57:04 -0700 | [diff] [blame] | 513 | "a.h.foo1@1.0::IFoo/1 N 0000000000000000000000000000000000000000000000000000000000000000\n" |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 514 | "a.h.foo2@2.0::IFoo/2 Y 0202020202020202020202020202020202020202020202020202020202020202\n" |
| 515 | "\n" |
| 516 | "[fake description 1]\n" |
| 517 | "Interface R Hash\n" |
Yifan Hong | d5ee11a | 2018-05-25 12:57:04 -0700 | [diff] [blame] | 518 | "a.h.foo3@3.0::IFoo/3 ? \n" |
| 519 | "a.h.foo4@4.0::IFoo/4 ? \n" |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 520 | "\n" |
| 521 | "[fake description 2]\n" |
| 522 | "Interface R Hash\n" |
Yifan Hong | d5ee11a | 2018-05-25 12:57:04 -0700 | [diff] [blame] | 523 | "a.h.foo5@5.0::IFoo/5 ? \n" |
| 524 | "a.h.foo6@6.0::IFoo/6 ? \n" |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 525 | "\n"; |
| 526 | |
| 527 | optind = 1; // mimic Lshal::parseArg() |
| 528 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-ils"}))); |
| 529 | EXPECT_EQ(expected, out.str()); |
| 530 | EXPECT_EQ("", err.str()); |
| 531 | } |
| 532 | |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 533 | TEST_F(ListTest, Dump) { |
| 534 | const std::string expected = |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 535 | "[fake description 0]\n" |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 536 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 537 | "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n" |
| 538 | "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n" |
| 539 | "\n" |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 540 | "[fake description 1]\n" |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 541 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 542 | "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n" |
| 543 | "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n" |
| 544 | "\n" |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 545 | "[fake description 2]\n" |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 546 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 547 | "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n" |
| 548 | "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n" |
| 549 | "\n"; |
| 550 | |
| 551 | optind = 1; // mimic Lshal::parseArg() |
| 552 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac"}))); |
| 553 | EXPECT_EQ(expected, out.str()); |
| 554 | EXPECT_EQ("", err.str()); |
| 555 | } |
| 556 | |
| 557 | TEST_F(ListTest, DumpCmdline) { |
| 558 | const std::string expected = |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 559 | "[fake description 0]\n" |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 560 | "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n" |
| 561 | "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 command_line_1 0000000000002711 command_line_2;command_line_4\n" |
| 562 | "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 command_line_2 0000000000002712 command_line_3;command_line_5\n" |
| 563 | "\n" |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 564 | "[fake description 1]\n" |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 565 | "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n" |
| 566 | "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A command_line_4;command_line_6\n" |
| 567 | "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A command_line_5;command_line_7\n" |
| 568 | "\n" |
Yifan Hong | 93b8bff | 2017-09-14 16:02:52 -0700 | [diff] [blame] | 569 | "[fake description 2]\n" |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 570 | "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n" |
| 571 | "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A command_line_6;command_line_8\n" |
| 572 | "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A command_line_7;command_line_9\n" |
| 573 | "\n"; |
| 574 | |
| 575 | optind = 1; // mimic Lshal::parseArg() |
| 576 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepacm"}))); |
| 577 | EXPECT_EQ(expected, out.str()); |
| 578 | EXPECT_EQ("", err.str()); |
| 579 | } |
| 580 | |
| 581 | TEST_F(ListTest, DumpNeat) { |
| 582 | const std::string expected = |
| 583 | "a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n" |
| 584 | "a.h.foo2@2.0::IFoo/2 12/22 2 3 5\n" |
| 585 | "a.h.foo3@3.0::IFoo/3 N/A N/A 4 6\n" |
| 586 | "a.h.foo4@4.0::IFoo/4 N/A N/A 5 7\n" |
| 587 | "a.h.foo5@5.0::IFoo/5 N/A N/A 6 8\n" |
| 588 | "a.h.foo6@6.0::IFoo/6 N/A N/A 7 9\n"; |
| 589 | |
| 590 | optind = 1; // mimic Lshal::parseArg() |
Yifan Hong | 7a3b46c | 2017-09-14 18:18:53 -0700 | [diff] [blame] | 591 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-iepc", "--neat"}))); |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 592 | EXPECT_EQ(expected, out.str()); |
| 593 | EXPECT_EQ("", err.str()); |
| 594 | } |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 595 | |
Nirav Atre | cce988d | 2018-05-16 11:14:46 -0700 | [diff] [blame] | 596 | TEST_F(ListTest, DumpSingleHalType) { |
| 597 | const std::string expected = |
| 598 | "[fake description 0]\n" |
| 599 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 600 | "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n" |
| 601 | "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n" |
| 602 | "\n"; |
| 603 | |
| 604 | optind = 1; // mimic Lshal::parseArg() |
| 605 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=binderized"}))); |
| 606 | EXPECT_EQ(expected, out.str()); |
| 607 | EXPECT_EQ("", err.str()); |
| 608 | } |
| 609 | |
| 610 | TEST_F(ListTest, DumpReorderedHalTypes) { |
| 611 | const std::string expected = |
| 612 | "[fake description 0]\n" |
| 613 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 614 | "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n" |
| 615 | "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n" |
| 616 | "\n" |
| 617 | "[fake description 1]\n" |
| 618 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 619 | "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n" |
| 620 | "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n" |
| 621 | "\n" |
| 622 | "[fake description 2]\n" |
| 623 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 624 | "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n" |
| 625 | "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n" |
| 626 | "\n"; |
| 627 | |
| 628 | optind = 1; // mimic Lshal::parseArg() |
| 629 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=passthrough_clients", |
| 630 | "--types=passthrough_libs", "--types=binderized"}))); |
| 631 | EXPECT_EQ(expected, out.str()); |
| 632 | EXPECT_EQ("", err.str()); |
| 633 | } |
| 634 | |
| 635 | TEST_F(ListTest, DumpAbbreviatedHalTypes) { |
| 636 | const std::string expected = |
| 637 | "[fake description 0]\n" |
| 638 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 639 | "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n" |
| 640 | "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n" |
| 641 | "\n" |
| 642 | "[fake description 1]\n" |
| 643 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 644 | "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n" |
| 645 | "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n" |
| 646 | "\n"; |
| 647 | |
| 648 | optind = 1; // mimic Lshal::parseArg() |
| 649 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,l"}))); |
| 650 | EXPECT_EQ(expected, out.str()); |
| 651 | EXPECT_EQ("", err.str()); |
| 652 | } |
| 653 | |
| 654 | TEST_F(ListTest, DumpEmptyAndDuplicateHalTypes) { |
| 655 | const std::string expected = |
| 656 | "[fake description 0]\n" |
| 657 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 658 | "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n" |
| 659 | "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n" |
| 660 | "\n" |
| 661 | "[fake description 1]\n" |
| 662 | "Interface Transport Arch Thread Use Server PTR Clients\n" |
| 663 | "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n" |
| 664 | "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n" |
| 665 | "\n"; |
| 666 | |
| 667 | optind = 1; // mimic Lshal::parseArg() |
| 668 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,l,,,l,l,c,", |
| 669 | "--types=passthrough_libs,passthrough_clients"}))); |
| 670 | EXPECT_EQ(expected, out.str()); |
| 671 | EXPECT_EQ("", err.str()); |
| 672 | } |
| 673 | |
| 674 | TEST_F(ListTest, UnknownHalType) { |
| 675 | optind = 1; // mimic Lshal::parseArg() |
| 676 | EXPECT_EQ(1u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,a"}))); |
| 677 | EXPECT_THAT(err.str(), HasSubstr("Unrecognized HAL type: a")); |
| 678 | } |
| 679 | |
Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 680 | TEST_F(ListTest, Vintf) { |
| 681 | std::string deviceManifestXml = |
| 682 | "<manifest version=\"1.0\" type=\"device\">\n" |
| 683 | " <hal>\n" |
| 684 | " <name>a.h.foo1</name>\n" |
| 685 | " <transport>hwbinder</transport>\n" |
| 686 | " <fqname>@1.0::IFoo/1</fqname>\n" |
| 687 | " </hal>\n" |
| 688 | " <hal>\n" |
| 689 | " <name>a.h.foo3</name>\n" |
| 690 | " <transport arch=\"32+64\">passthrough</transport>\n" |
| 691 | " <fqname>@3.0::IFoo/3</fqname>\n" |
| 692 | " </hal>\n" |
| 693 | "</manifest>\n"; |
| 694 | std::string frameworkManifestXml = |
| 695 | "<manifest version=\"1.0\" type=\"framework\">\n" |
| 696 | " <hal>\n" |
| 697 | " <name>a.h.foo5</name>\n" |
| 698 | " <transport arch=\"32\">passthrough</transport>\n" |
| 699 | " <fqname>@5.0::IFoo/5</fqname>\n" |
| 700 | " </hal>\n" |
| 701 | "</manifest>\n"; |
| 702 | std::string deviceMatrixXml = |
| 703 | "<compatibility-matrix version=\"1.0\" type=\"device\">\n" |
| 704 | " <hal>\n" |
| 705 | " <name>a.h.foo5</name>\n" |
| 706 | " <version>5.0</version>\n" |
| 707 | " <interface>\n" |
| 708 | " <name>IFoo</name>\n" |
| 709 | " <instance>5</instance>\n" |
| 710 | " </interface>\n" |
| 711 | " </hal>\n" |
| 712 | "</compatibility-matrix>\n"; |
| 713 | std::string frameworkMatrixXml = |
| 714 | "<compatibility-matrix version=\"1.0\" type=\"framework\">\n" |
| 715 | " <hal>\n" |
| 716 | " <name>a.h.foo1</name>\n" |
| 717 | " <version>1.0</version>\n" |
| 718 | " <interface>\n" |
| 719 | " <name>IFoo</name>\n" |
| 720 | " <instance>1</instance>\n" |
| 721 | " </interface>\n" |
| 722 | " </hal>\n" |
| 723 | " <hal>\n" |
| 724 | " <name>a.h.foo3</name>\n" |
| 725 | " <version>3.0</version>\n" |
| 726 | " <interface>\n" |
| 727 | " <name>IFoo</name>\n" |
| 728 | " <instance>3</instance>\n" |
| 729 | " </interface>\n" |
| 730 | " </hal>\n" |
| 731 | "</compatibility-matrix>\n"; |
| 732 | |
| 733 | std::string expected = "DM,FC a.h.foo1@1.0::IFoo/1\n" |
| 734 | "X a.h.foo2@2.0::IFoo/2\n" |
| 735 | "DM,FC a.h.foo3@3.0::IFoo/3\n" |
| 736 | "X a.h.foo4@4.0::IFoo/4\n" |
| 737 | "DC,FM a.h.foo5@5.0::IFoo/5\n" |
| 738 | "X a.h.foo6@6.0::IFoo/6\n"; |
| 739 | |
| 740 | auto deviceManifest = std::make_shared<HalManifest>(); |
| 741 | auto frameworkManifest = std::make_shared<HalManifest>(); |
| 742 | auto deviceMatrix = std::make_shared<CompatibilityMatrix>(); |
| 743 | auto frameworkMatrix = std::make_shared<CompatibilityMatrix>(); |
| 744 | |
| 745 | ASSERT_TRUE(gHalManifestConverter(deviceManifest.get(), deviceManifestXml)); |
| 746 | ASSERT_TRUE(gHalManifestConverter(frameworkManifest.get(), frameworkManifestXml)); |
| 747 | ASSERT_TRUE(gCompatibilityMatrixConverter(deviceMatrix.get(), deviceMatrixXml)); |
| 748 | ASSERT_TRUE(gCompatibilityMatrixConverter(frameworkMatrix.get(), frameworkMatrixXml)); |
| 749 | |
| 750 | ON_CALL(*mockList, getDeviceManifest()).WillByDefault(Return(deviceManifest)); |
| 751 | ON_CALL(*mockList, getDeviceMatrix()).WillByDefault(Return(deviceMatrix)); |
| 752 | ON_CALL(*mockList, getFrameworkManifest()).WillByDefault(Return(frameworkManifest)); |
| 753 | ON_CALL(*mockList, getFrameworkMatrix()).WillByDefault(Return(frameworkMatrix)); |
| 754 | |
| 755 | optind = 1; // mimic Lshal::parseArg() |
| 756 | EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-Vi", "--neat"}))); |
| 757 | EXPECT_THAT(out.str(), HasSubstr(expected)); |
| 758 | EXPECT_EQ("", err.str()); |
| 759 | } |
| 760 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame] | 761 | class HelpTest : public ::testing::Test { |
| 762 | public: |
| 763 | void SetUp() override { |
| 764 | lshal = std::make_unique<Lshal>(out, err, new MockServiceManager() /* serviceManager */, |
| 765 | new MockServiceManager() /* passthruManager */); |
| 766 | } |
| 767 | |
| 768 | std::stringstream err; |
| 769 | std::stringstream out; |
| 770 | std::unique_ptr<Lshal> lshal; |
| 771 | }; |
| 772 | |
| 773 | TEST_F(HelpTest, GlobalUsage) { |
| 774 | (void)callMain(lshal, {"lshal", "--help"}); // ignore return |
| 775 | std::string errStr = err.str(); |
| 776 | EXPECT_THAT(errStr, ContainsRegex("(^|\n)commands:($|\n)")) |
| 777 | << "`lshal --help` does not contain global usage"; |
| 778 | EXPECT_THAT(errStr, ContainsRegex("(^|\n)list:($|\n)")) |
| 779 | << "`lshal --help` does not contain usage for 'list' command"; |
| 780 | EXPECT_THAT(errStr, ContainsRegex("(^|\n)debug:($|\n)")) |
| 781 | << "`lshal --help` does not contain usage for 'debug' command"; |
| 782 | EXPECT_THAT(errStr, ContainsRegex("(^|\n)help:($|\n)")) |
| 783 | << "`lshal --help` does not contain usage for 'help' command"; |
| 784 | |
| 785 | err.str(""); |
| 786 | (void)callMain(lshal, {"lshal", "help"}); // ignore return |
| 787 | EXPECT_EQ(errStr, err.str()) << "`lshal help` should have the same output as `lshal --help`"; |
| 788 | |
| 789 | err.str(""); |
| 790 | EXPECT_NE(0u, callMain(lshal, {"lshal", "--unknown-option"})); |
| 791 | EXPECT_THAT(err.str(), ContainsRegex("unrecognized option")); |
| 792 | EXPECT_THAT(err.str(), EndsWith(errStr)) |
| 793 | << "`lshal --unknown-option` should have the same output as `lshal --help`"; |
| 794 | EXPECT_EQ("", out.str()); |
| 795 | } |
| 796 | |
| 797 | TEST_F(HelpTest, UnknownOptionList1) { |
| 798 | (void)callMain(lshal, {"lshal", "help", "list"}); |
| 799 | EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)")) |
| 800 | << "`lshal help list` does not contain usage for 'list' command"; |
| 801 | } |
| 802 | |
| 803 | TEST_F(HelpTest, UnknownOptionList2) { |
| 804 | EXPECT_NE(0u, callMain(lshal, {"lshal", "list", "--unknown-option"})); |
| 805 | EXPECT_THAT(err.str(), ContainsRegex("unrecognized option")); |
| 806 | EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)")) |
| 807 | << "`lshal list --unknown-option` does not contain usage for 'list' command"; |
| 808 | EXPECT_EQ("", out.str()); |
| 809 | } |
| 810 | |
| 811 | TEST_F(HelpTest, UnknownOptionHelp1) { |
| 812 | (void)callMain(lshal, {"lshal", "help", "help"}); |
| 813 | EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)")) |
| 814 | << "`lshal help help` does not contain usage for 'help' command"; |
| 815 | } |
| 816 | |
| 817 | TEST_F(HelpTest, UnknownOptionHelp2) { |
| 818 | (void)callMain(lshal, {"lshal", "help", "--unknown-option"}); |
| 819 | EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)")) |
| 820 | << "`lshal help --unknown-option` does not contain usage for 'help' command"; |
| 821 | EXPECT_EQ("", out.str()); |
| 822 | } |
| 823 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 824 | } // namespace lshal |
| 825 | } // namespace android |
| 826 | |
| 827 | int main(int argc, char **argv) { |
| 828 | ::testing::InitGoogleMock(&argc, argv); |
| 829 | return RUN_ALL_TESTS(); |
| 830 | } |