blob: 096b67d24e86a47302c804304c9a844e87b512ee [file] [log] [blame]
Yifan Hong9881df92017-05-10 14:33:05 -07001/*
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 Hong8bf73162017-09-07 18:06:13 -070029#include <vintf/parse_xml.h>
Yifan Hong9881df92017-05-10 14:33:05 -070030
Yifan Hongb2a2ecb2017-09-07 15:08:22 -070031#include "ListCommand.h"
Yifan Hong9881df92017-05-10 14:33:05 -070032#include "Lshal.h"
33
34#define NELEMS(array) static_cast<int>(sizeof(array) / sizeof(array[0]))
35
36using namespace testing;
37
Yifan Hong8bf73162017-09-07 18:06:13 -070038using ::android::hidl::base::V1_0::DebugInfo;
Yifan Hong9881df92017-05-10 14:33:05 -070039using ::android::hidl::base::V1_0::IBase;
40using ::android::hidl::manager::V1_0::IServiceManager;
41using ::android::hidl::manager::V1_0::IServiceNotification;
Yifan Hongfee209d2017-09-14 18:23:38 -070042using ::android::hardware::hidl_array;
Yifan Hong9881df92017-05-10 14:33:05 -070043using ::android::hardware::hidl_death_recipient;
44using ::android::hardware::hidl_handle;
45using ::android::hardware::hidl_string;
46using ::android::hardware::hidl_vec;
Yifan Hong0ad64f52018-05-25 15:29:17 -070047using android::vintf::Arch;
Yifan Hongbdf44f82018-05-25 14:20:00 -070048using android::vintf::CompatibilityMatrix;
49using android::vintf::gCompatibilityMatrixConverter;
50using android::vintf::gHalManifestConverter;
51using android::vintf::HalManifest;
Yifan Hong8304e412018-05-25 15:05:36 -070052using android::vintf::Transport;
Yifan Hongbdf44f82018-05-25 14:20:00 -070053using android::vintf::VintfObject;
Yifan Hong9881df92017-05-10 14:33:05 -070054
Yifan Hong8bf73162017-09-07 18:06:13 -070055using InstanceDebugInfo = IServiceManager::InstanceDebugInfo;
56
Yifan Hongfee209d2017-09-14 18:23:38 -070057using hidl_hash = hidl_array<uint8_t, 32>;
58
Yifan Hong9881df92017-05-10 14:33:05 -070059namespace android {
60namespace hardware {
61namespace tests {
62namespace baz {
63namespace V1_0 {
64namespace implementation {
65struct 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
92namespace lshal {
93
Yifan Hong9881df92017-05-10 14:33:05 -070094class MockServiceManager : public IServiceManager {
95public:
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 Hongbf20a262017-09-07 11:10:58 -0700124class DebugTest : public ::testing::Test {
Yifan Hong9881df92017-05-10 14:33:05 -0700125public:
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 Hongbf20a262017-09-07 11:10:58 -0700139
140 lshal = std::make_unique<Lshal>(out, err, serviceManager, serviceManager);
Yifan Hong9881df92017-05-10 14:33:05 -0700141 }
142 void TearDown() override {}
143
144 std::stringstream err;
145 std::stringstream out;
146 sp<MockServiceManager> serviceManager;
Yifan Hongbf20a262017-09-07 11:10:58 -0700147
148 std::unique_ptr<Lshal> lshal;
Yifan Hong9881df92017-05-10 14:33:05 -0700149};
150
Yifan Hongbf20a262017-09-07 11:10:58 -0700151static Arg createArg(const std::vector<const char*>& args) {
152 return Arg{static_cast<int>(args.size()), const_cast<char**>(args.data())};
153}
154
155template<typename T>
156static Status callMain(const std::unique_ptr<T>& lshal, const std::vector<const char*>& args) {
157 return lshal->main(createArg(args));
158}
159
160TEST_F(DebugTest, Debug) {
161 EXPECT_EQ(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700162 "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux/default", "foo", "bar"
Yifan Hongbf20a262017-09-07 11:10:58 -0700163 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700164 EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nfoo\nbar"));
165 EXPECT_THAT(err.str(), IsEmpty());
166}
167
Yifan Hongbf20a262017-09-07 11:10:58 -0700168TEST_F(DebugTest, Debug2) {
169 EXPECT_EQ(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700170 "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux", "baz", "quux"
Yifan Hongbf20a262017-09-07 11:10:58 -0700171 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700172 EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nbaz\nquux"));
173 EXPECT_THAT(err.str(), IsEmpty());
174}
175
Yifan Hongbf20a262017-09-07 11:10:58 -0700176TEST_F(DebugTest, Debug3) {
177 EXPECT_NE(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700178 "lshal", "debug", "android.hardware.tests.doesnotexist@1.0::IDoesNotExist",
Yifan Hongbf20a262017-09-07 11:10:58 -0700179 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700180 EXPECT_THAT(err.str(), HasSubstr("does not exist"));
181}
182
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700183class MockLshal : public Lshal {
184public:
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
192class MockListCommand : public ListCommand {
193public:
194 MockListCommand(Lshal* lshal) : ListCommand(*lshal) {}
195
Yifan Honga8bedc62017-09-08 18:00:31 -0700196 Status parseArgs(const Arg& arg) { return ListCommand::parseArgs(arg); }
197 Status main(const Arg& arg) { return ListCommand::main(arg); }
Yifan Hong93b8bff2017-09-14 16:02:52 -0700198 void forEachTable(const std::function<void(Table &)> &f) {
199 return ListCommand::forEachTable(f);
200 }
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700201 void forEachTable(const std::function<void(const Table &)> &f) const {
202 return ListCommand::forEachTable(f);
203 }
Yifan Hong8bf73162017-09-07 18:06:13 -0700204 Status fetch() { return ListCommand::fetch(); }
205 void dumpVintf(const NullableOStream<std::ostream>& out) {
206 return ListCommand::dumpVintf(out);
207 }
Yifan Hong93b8bff2017-09-14 16:02:52 -0700208 void internalPostprocess() { ListCommand::postprocess(); }
Yifan Hong1243dde2017-09-14 17:49:30 -0700209 const PidInfo* getPidInfoCached(pid_t serverPid) {
210 return ListCommand::getPidInfoCached(serverPid);
211 }
Yifan Hong8bf73162017-09-07 18:06:13 -0700212
Yifan Hong93b8bff2017-09-14 16:02:52 -0700213 MOCK_METHOD0(postprocess, void());
Yifan Hong8bf73162017-09-07 18:06:13 -0700214 MOCK_CONST_METHOD2(getPidInfo, bool(pid_t, PidInfo*));
215 MOCK_CONST_METHOD1(parseCmdline, std::string(pid_t));
Yifan Hongf31aa052018-02-02 15:17:51 -0800216 MOCK_METHOD1(getPartition, Partition(pid_t));
Yifan Hongbdf44f82018-05-25 14:20:00 -0700217
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 Hongb2a2ecb2017-09-07 15:08:22 -0700222};
223
224class ListParseArgsTest : public ::testing::Test {
225public:
226 void SetUp() override {
227 mockLshal = std::make_unique<NiceMock<MockLshal>>();
228 mockList = std::make_unique<MockListCommand>(mockLshal.get());
Yifan Hongc4430912018-06-27 16:48:34 -0700229 ON_CALL(*mockLshal, err()).WillByDefault(Return(NullableOStream<std::ostream>(err)));
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700230 // 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 Hongc4430912018-06-27 16:48:34 -0700235 std::stringstream err;
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700236};
237
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700238TEST_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 Hongc4430912018-06-27 16:48:34 -0700245 EXPECT_EQ("", err.str());
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700246}
247
248TEST_F(ListParseArgsTest, Cmds) {
249 EXPECT_EQ(0u, mockList->parseArgs(createArg({"lshal", "-m"})));
250 mockList->forEachTable([](const Table& table) {
Yifan Hong7a3b46c2017-09-14 18:18:53 -0700251 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 Hongb2a2ecb2017-09-07 15:08:22 -0700259 });
Yifan Hongc4430912018-06-27 16:48:34 -0700260 EXPECT_EQ("", err.str());
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700261}
262
263TEST_F(ListParseArgsTest, DebugAndNeat) {
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700264 EXPECT_NE(0u, mockList->parseArgs(createArg({"lshal", "--neat", "-d"})));
Yifan Hongc4430912018-06-27 16:48:34 -0700265 EXPECT_THAT(err.str(), HasSubstr("--neat should not be used with --debug."));
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700266}
267
Yifan Hong8bf73162017-09-07 18:06:13 -0700268/// Fetch Test
269
270// A set of deterministic functions to generate fake debug infos.
271static uint64_t getPtr(pid_t serverId) { return 10000 + serverId; }
272static std::vector<pid_t> getClients(pid_t serverId) {
273 return {serverId + 1, serverId + 3};
274}
275static 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}
282static std::string getInterfaceName(pid_t serverId) {
283 return "a.h.foo" + std::to_string(serverId) + "@" + std::to_string(serverId) + ".0::IFoo";
284}
285static std::string getInstanceName(pid_t serverId) {
286 return std::to_string(serverId);
287}
288static pid_t getIdFromInstanceName(const hidl_string& instance) {
289 return atoi(instance.c_str());
290}
291static std::string getFqInstanceName(pid_t serverId) {
292 return getInterfaceName(serverId) + "/" + getInstanceName(serverId);
293}
294static std::string getCmdlineFromId(pid_t serverId) {
295 if (serverId == NO_PID) return "";
296 return "command_line_" + std::to_string(serverId);
297}
Yifan Hongfee209d2017-09-14 18:23:38 -0700298static bool getIsReleasedFromId(pid_t p) { return p % 2 == 0; }
299static 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 Hong8bf73162017-09-07 18:06:13 -0700307
308// Fake service returned by mocked IServiceManager::get.
309class TestService : public IBase {
310public:
Yifan Hongfee209d2017-09-14 18:23:38 -0700311 TestService(pid_t id) : mId(id) {}
Yifan Hong8bf73162017-09-07 18:06:13 -0700312 hardware::Return<void> getDebugInfo(getDebugInfo_cb cb) override {
Yifan Hongfee209d2017-09-14 18:23:38 -0700313 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 Hong8bf73162017-09-07 18:06:13 -0700322 return hardware::Void();
323 }
324private:
Yifan Hongfee209d2017-09-14 18:23:38 -0700325 pid_t mId;
Yifan Hong8bf73162017-09-07 18:06:13 -0700326};
327
328class ListTest : public ::testing::Test {
329public:
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 Hong93b8bff2017-09-14 16:02:52 -0700344 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 Hongf31aa052018-02-02 15:17:51 -0800351 ON_CALL(*mockList, getPartition(_)).WillByDefault(Return(Partition::VENDOR));
Yifan Hongbdf44f82018-05-25 14:20:00 -0700352
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 Hong8bf73162017-09-07 18:06:13 -0700361 }
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 Hongfee209d2017-09-14 18:23:38 -0700376 return sp<IBase>(new TestService(id));
Yifan Hong8bf73162017-09-07 18:06:13 -0700377 }));
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 Hong1243dde2017-09-14 17:49:30 -0700406TEST_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 Hong8bf73162017-09-07 18:06:13 -0700413TEST_F(ListTest, Fetch) {
414 EXPECT_EQ(0u, mockList->fetch());
Yifan Hong0ad64f52018-05-25 15:29:17 -0700415 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 Hong8bf73162017-09-07 18:06:13 -0700419 int id = 1;
420 mockList->forEachTable([&](const Table& table) {
421 ASSERT_EQ(2u, table.size());
422 for (const auto& entry : table) {
Yifan Hong0ad64f52018-05-25 15:29:17 -0700423 auto transport = transportArchs.at(id - 1).transport;
Yifan Hong8bf73162017-09-07 18:06:13 -0700424 TableEntry expected{
425 .interfaceName = getFqInstanceName(id),
426 .transport = transport,
Yifan Hong8304e412018-05-25 15:05:36 -0700427 .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 Hong8bf73162017-09-07 18:06:13 -0700432 .serverCmdline = {},
Yifan Hong8304e412018-05-25 15:05:36 -0700433 .serverObjectAddress = transport == Transport::HWBINDER ? getPtr(id) : NO_PTR,
Yifan Hong8bf73162017-09-07 18:06:13 -0700434 .clientPids = getClients(id),
435 .clientCmdlines = {},
Yifan Hong0ad64f52018-05-25 15:29:17 -0700436 .arch = transportArchs.at(id - 1).arch,
Yifan Hong8bf73162017-09-07 18:06:13 -0700437 };
438 EXPECT_EQ(expected, entry) << expected.to_string() << " vs. " << entry.to_string();
439
440 ++id;
441 }
442 });
443
444}
445
446TEST_F(ListTest, DumpVintf) {
Yifan Hongb2d096a2018-05-01 15:25:23 -0700447 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 Hong8bf73162017-09-07 18:06:13 -0700469
470 optind = 1; // mimic Lshal::parseArg()
471 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "--init-vintf"})));
Yifan Hongb2d096a2018-05-01 15:25:23 -0700472 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 Hong8bf73162017-09-07 18:06:13 -0700476 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 Hongfee209d2017-09-14 18:23:38 -0700484// test default columns
485TEST_F(ListTest, DumpDefault) {
486 const std::string expected =
487 "[fake description 0]\n"
488 "R Interface Thread Use Server Clients\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700489 "N a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700490 "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 Hongd5ee11a2018-05-25 12:57:04 -0700494 "? 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 Hongfee209d2017-09-14 18:23:38 -0700496 "\n"
497 "[fake description 2]\n"
498 "R Interface Thread Use Server Clients\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700499 "? 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 Hongfee209d2017-09-14 18:23:38 -0700501 "\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
509TEST_F(ListTest, DumpHash) {
510 const std::string expected =
511 "[fake description 0]\n"
512 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700513 "a.h.foo1@1.0::IFoo/1 N 0000000000000000000000000000000000000000000000000000000000000000\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700514 "a.h.foo2@2.0::IFoo/2 Y 0202020202020202020202020202020202020202020202020202020202020202\n"
515 "\n"
516 "[fake description 1]\n"
517 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700518 "a.h.foo3@3.0::IFoo/3 ? \n"
519 "a.h.foo4@4.0::IFoo/4 ? \n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700520 "\n"
521 "[fake description 2]\n"
522 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700523 "a.h.foo5@5.0::IFoo/5 ? \n"
524 "a.h.foo6@6.0::IFoo/6 ? \n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700525 "\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 Hong8bf73162017-09-07 18:06:13 -0700533TEST_F(ListTest, Dump) {
534 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700535 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700536 "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 Hong93b8bff2017-09-14 16:02:52 -0700540 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700541 "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 Hong93b8bff2017-09-14 16:02:52 -0700545 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700546 "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
557TEST_F(ListTest, DumpCmdline) {
558 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700559 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700560 "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 Hong93b8bff2017-09-14 16:02:52 -0700564 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700565 "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 Hong93b8bff2017-09-14 16:02:52 -0700569 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700570 "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
581TEST_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 Hong7a3b46c2017-09-14 18:18:53 -0700591 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-iepc", "--neat"})));
Yifan Hong8bf73162017-09-07 18:06:13 -0700592 EXPECT_EQ(expected, out.str());
593 EXPECT_EQ("", err.str());
594}
Yifan Honga8bedc62017-09-08 18:00:31 -0700595
Nirav Atrecce988d2018-05-16 11:14:46 -0700596TEST_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
610TEST_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
635TEST_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
654TEST_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
674TEST_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 Hongbdf44f82018-05-25 14:20:00 -0700680TEST_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 Honga8bedc62017-09-08 18:00:31 -0700761class HelpTest : public ::testing::Test {
762public:
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
773TEST_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
797TEST_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
803TEST_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
811TEST_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
817TEST_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 Hong9881df92017-05-10 14:33:05 -0700824} // namespace lshal
825} // namespace android
826
827int main(int argc, char **argv) {
828 ::testing::InitGoogleMock(&argc, argv);
829 return RUN_ALL_TESTS();
830}