blob: 2af01996d4c8dac72e66e0171e653167c1fe0517 [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 Hong8304e412018-05-25 15:05:36 -070048using android::vintf::Transport;
Yifan Hong9881df92017-05-10 14:33:05 -070049
Yifan Hong8bf73162017-09-07 18:06:13 -070050using InstanceDebugInfo = IServiceManager::InstanceDebugInfo;
51
Yifan Hongfee209d2017-09-14 18:23:38 -070052using hidl_hash = hidl_array<uint8_t, 32>;
53
Yifan Hong9881df92017-05-10 14:33:05 -070054namespace android {
55namespace hardware {
56namespace tests {
57namespace baz {
58namespace V1_0 {
59namespace implementation {
60struct Quux : android::hardware::tests::baz::V1_0::IQuux {
61 ::android::hardware::Return<void> debug(const hidl_handle& hh, const hidl_vec<hidl_string>& options) override {
62 const native_handle_t *handle = hh.getNativeHandle();
63 if (handle->numFds < 1) {
64 return Void();
65 }
66 int fd = handle->data[0];
67 std::string content{descriptor};
68 for (const auto &option : options) {
69 content += "\n";
70 content += option.c_str();
71 }
72 ssize_t written = write(fd, content.c_str(), content.size());
73 if (written != (ssize_t)content.size()) {
74 LOG(WARNING) << "SERVER(Quux) debug writes " << written << " bytes < "
75 << content.size() << " bytes, errno = " << errno;
76 }
77 return Void();
78 }
79};
80
81} // namespace implementation
82} // namespace V1_0
83} // namespace baz
84} // namespace tests
85} // namespace hardware
86
87namespace lshal {
88
Yifan Hong9881df92017-05-10 14:33:05 -070089class MockServiceManager : public IServiceManager {
90public:
91 template<typename T>
92 using R = ::android::hardware::Return<T>;
93 using String = const hidl_string&;
94 ~MockServiceManager() = default;
95
96#define MOCK_METHOD_CB(name) MOCK_METHOD1(name, R<void>(IServiceManager::name##_cb))
97
98 MOCK_METHOD2(get, R<sp<IBase>>(String, String));
99 MOCK_METHOD2(add, R<bool>(String, const sp<IBase>&));
100 MOCK_METHOD2(getTransport, R<IServiceManager::Transport>(String, String));
101 MOCK_METHOD_CB(list);
102 MOCK_METHOD2(listByInterface, R<void>(String, listByInterface_cb));
103 MOCK_METHOD3(registerForNotifications, R<bool>(String, String, const sp<IServiceNotification>&));
104 MOCK_METHOD_CB(debugDump);
105 MOCK_METHOD2(registerPassthroughClient, R<void>(String, String));
106 MOCK_METHOD_CB(interfaceChain);
107 MOCK_METHOD2(debug, R<void>(const hidl_handle&, const hidl_vec<hidl_string>&));
108 MOCK_METHOD_CB(interfaceDescriptor);
109 MOCK_METHOD_CB(getHashChain);
110 MOCK_METHOD0(setHalInstrumentation, R<void>());
111 MOCK_METHOD2(linkToDeath, R<bool>(const sp<hidl_death_recipient>&, uint64_t));
112 MOCK_METHOD0(ping, R<void>());
113 MOCK_METHOD_CB(getDebugInfo);
114 MOCK_METHOD0(notifySyspropsChanged, R<void>());
115 MOCK_METHOD1(unlinkToDeath, R<bool>(const sp<hidl_death_recipient>&));
116
117};
118
Yifan Hongbf20a262017-09-07 11:10:58 -0700119class DebugTest : public ::testing::Test {
Yifan Hong9881df92017-05-10 14:33:05 -0700120public:
121 void SetUp() override {
122 using ::android::hardware::tests::baz::V1_0::IQuux;
123 using ::android::hardware::tests::baz::V1_0::implementation::Quux;
124
125 err.str("");
126 out.str("");
127 serviceManager = new testing::NiceMock<MockServiceManager>();
128 ON_CALL(*serviceManager, get(_, _)).WillByDefault(Invoke(
129 [](const auto &iface, const auto &inst) -> ::android::hardware::Return<sp<IBase>> {
130 if (iface == IQuux::descriptor && inst == "default")
131 return new Quux();
132 return nullptr;
133 }));
Yifan Hongbf20a262017-09-07 11:10:58 -0700134
135 lshal = std::make_unique<Lshal>(out, err, serviceManager, serviceManager);
Yifan Hong9881df92017-05-10 14:33:05 -0700136 }
137 void TearDown() override {}
138
139 std::stringstream err;
140 std::stringstream out;
141 sp<MockServiceManager> serviceManager;
Yifan Hongbf20a262017-09-07 11:10:58 -0700142
143 std::unique_ptr<Lshal> lshal;
Yifan Hong9881df92017-05-10 14:33:05 -0700144};
145
Yifan Hongbf20a262017-09-07 11:10:58 -0700146static Arg createArg(const std::vector<const char*>& args) {
147 return Arg{static_cast<int>(args.size()), const_cast<char**>(args.data())};
148}
149
150template<typename T>
151static Status callMain(const std::unique_ptr<T>& lshal, const std::vector<const char*>& args) {
152 return lshal->main(createArg(args));
153}
154
155TEST_F(DebugTest, Debug) {
156 EXPECT_EQ(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700157 "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux/default", "foo", "bar"
Yifan Hongbf20a262017-09-07 11:10:58 -0700158 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700159 EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nfoo\nbar"));
160 EXPECT_THAT(err.str(), IsEmpty());
161}
162
Yifan Hongbf20a262017-09-07 11:10:58 -0700163TEST_F(DebugTest, Debug2) {
164 EXPECT_EQ(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700165 "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux", "baz", "quux"
Yifan Hongbf20a262017-09-07 11:10:58 -0700166 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700167 EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nbaz\nquux"));
168 EXPECT_THAT(err.str(), IsEmpty());
169}
170
Yifan Hongbf20a262017-09-07 11:10:58 -0700171TEST_F(DebugTest, Debug3) {
172 EXPECT_NE(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700173 "lshal", "debug", "android.hardware.tests.doesnotexist@1.0::IDoesNotExist",
Yifan Hongbf20a262017-09-07 11:10:58 -0700174 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700175 EXPECT_THAT(err.str(), HasSubstr("does not exist"));
176}
177
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700178class MockLshal : public Lshal {
179public:
180 MockLshal() {}
181 ~MockLshal() = default;
182 MOCK_CONST_METHOD0(out, NullableOStream<std::ostream>());
183 MOCK_CONST_METHOD0(err, NullableOStream<std::ostream>());
184};
185
186// expose protected fields and methods for ListCommand
187class MockListCommand : public ListCommand {
188public:
189 MockListCommand(Lshal* lshal) : ListCommand(*lshal) {}
190
Yifan Honga8bedc62017-09-08 18:00:31 -0700191 Status parseArgs(const Arg& arg) { return ListCommand::parseArgs(arg); }
192 Status main(const Arg& arg) { return ListCommand::main(arg); }
Yifan Hong93b8bff2017-09-14 16:02:52 -0700193 void forEachTable(const std::function<void(Table &)> &f) {
194 return ListCommand::forEachTable(f);
195 }
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700196 void forEachTable(const std::function<void(const Table &)> &f) const {
197 return ListCommand::forEachTable(f);
198 }
Yifan Hong8bf73162017-09-07 18:06:13 -0700199 Status fetch() { return ListCommand::fetch(); }
200 void dumpVintf(const NullableOStream<std::ostream>& out) {
201 return ListCommand::dumpVintf(out);
202 }
Yifan Hong93b8bff2017-09-14 16:02:52 -0700203 void internalPostprocess() { ListCommand::postprocess(); }
Yifan Hong1243dde2017-09-14 17:49:30 -0700204 const PidInfo* getPidInfoCached(pid_t serverPid) {
205 return ListCommand::getPidInfoCached(serverPid);
206 }
Yifan Hong8bf73162017-09-07 18:06:13 -0700207
Yifan Hong93b8bff2017-09-14 16:02:52 -0700208 MOCK_METHOD0(postprocess, void());
Yifan Hong8bf73162017-09-07 18:06:13 -0700209 MOCK_CONST_METHOD2(getPidInfo, bool(pid_t, PidInfo*));
210 MOCK_CONST_METHOD1(parseCmdline, std::string(pid_t));
Yifan Hongf31aa052018-02-02 15:17:51 -0800211 MOCK_METHOD1(getPartition, Partition(pid_t));
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700212};
213
214class ListParseArgsTest : public ::testing::Test {
215public:
216 void SetUp() override {
217 mockLshal = std::make_unique<NiceMock<MockLshal>>();
218 mockList = std::make_unique<MockListCommand>(mockLshal.get());
219 // ListCommand::parseArgs should parse arguments from the second element
220 optind = 1;
221 }
222 std::unique_ptr<MockLshal> mockLshal;
223 std::unique_ptr<MockListCommand> mockList;
224 std::stringstream output;
225};
226
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700227TEST_F(ListParseArgsTest, Args) {
228 EXPECT_EQ(0u, mockList->parseArgs(createArg({"lshal", "-p", "-i", "-a", "-c"})));
229 mockList->forEachTable([](const Table& table) {
230 EXPECT_EQ(SelectedColumns({TableColumnType::SERVER_PID, TableColumnType::INTERFACE_NAME,
231 TableColumnType::SERVER_ADDR, TableColumnType::CLIENT_PIDS}),
232 table.getSelectedColumns());
233 });
234}
235
236TEST_F(ListParseArgsTest, Cmds) {
237 EXPECT_EQ(0u, mockList->parseArgs(createArg({"lshal", "-m"})));
238 mockList->forEachTable([](const Table& table) {
Yifan Hong7a3b46c2017-09-14 18:18:53 -0700239 EXPECT_THAT(table.getSelectedColumns(), Not(Contains(TableColumnType::SERVER_PID)))
240 << "should not print server PID with -m";
241 EXPECT_THAT(table.getSelectedColumns(), Not(Contains(TableColumnType::CLIENT_PIDS)))
242 << "should not print client PIDs with -m";
243 EXPECT_THAT(table.getSelectedColumns(), Contains(TableColumnType::SERVER_CMD))
244 << "should print server cmd with -m";
245 EXPECT_THAT(table.getSelectedColumns(), Contains(TableColumnType::CLIENT_CMDS))
246 << "should print client cmds with -m";
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700247 });
248}
249
250TEST_F(ListParseArgsTest, DebugAndNeat) {
251 ON_CALL(*mockLshal, err()).WillByDefault(Return(NullableOStream<std::ostream>(output)));
252 EXPECT_NE(0u, mockList->parseArgs(createArg({"lshal", "--neat", "-d"})));
253 EXPECT_THAT(output.str(), StrNe(""));
254}
255
Yifan Hong8bf73162017-09-07 18:06:13 -0700256/// Fetch Test
257
258// A set of deterministic functions to generate fake debug infos.
259static uint64_t getPtr(pid_t serverId) { return 10000 + serverId; }
260static std::vector<pid_t> getClients(pid_t serverId) {
261 return {serverId + 1, serverId + 3};
262}
263static PidInfo getPidInfoFromId(pid_t serverId) {
264 PidInfo info;
265 info.refPids[getPtr(serverId)] = getClients(serverId);
266 info.threadUsage = 10 + serverId;
267 info.threadCount = 20 + serverId;
268 return info;
269}
270static std::string getInterfaceName(pid_t serverId) {
271 return "a.h.foo" + std::to_string(serverId) + "@" + std::to_string(serverId) + ".0::IFoo";
272}
273static std::string getInstanceName(pid_t serverId) {
274 return std::to_string(serverId);
275}
276static pid_t getIdFromInstanceName(const hidl_string& instance) {
277 return atoi(instance.c_str());
278}
279static std::string getFqInstanceName(pid_t serverId) {
280 return getInterfaceName(serverId) + "/" + getInstanceName(serverId);
281}
282static std::string getCmdlineFromId(pid_t serverId) {
283 if (serverId == NO_PID) return "";
284 return "command_line_" + std::to_string(serverId);
285}
Yifan Hongfee209d2017-09-14 18:23:38 -0700286static bool getIsReleasedFromId(pid_t p) { return p % 2 == 0; }
287static hidl_hash getHashFromId(pid_t serverId) {
288 hidl_hash hash;
289 bool isReleased = getIsReleasedFromId(serverId);
290 for (size_t i = 0; i < hash.size(); ++i) {
291 hash[i] = isReleased ? static_cast<uint8_t>(serverId) : 0u;
292 }
293 return hash;
294}
Yifan Hong8bf73162017-09-07 18:06:13 -0700295
296// Fake service returned by mocked IServiceManager::get.
297class TestService : public IBase {
298public:
Yifan Hongfee209d2017-09-14 18:23:38 -0700299 TestService(pid_t id) : mId(id) {}
Yifan Hong8bf73162017-09-07 18:06:13 -0700300 hardware::Return<void> getDebugInfo(getDebugInfo_cb cb) override {
Yifan Hongfee209d2017-09-14 18:23:38 -0700301 cb({ mId /* pid */, getPtr(mId), DebugInfo::Architecture::IS_64BIT });
302 return hardware::Void();
303 }
304 hardware::Return<void> interfaceChain(interfaceChain_cb cb) override {
305 cb({getInterfaceName(mId), IBase::descriptor});
306 return hardware::Void();
307 }
308 hardware::Return<void> getHashChain(getHashChain_cb cb) override {
309 cb({getHashFromId(mId), getHashFromId(0xff)});
Yifan Hong8bf73162017-09-07 18:06:13 -0700310 return hardware::Void();
311 }
312private:
Yifan Hongfee209d2017-09-14 18:23:38 -0700313 pid_t mId;
Yifan Hong8bf73162017-09-07 18:06:13 -0700314};
315
316class ListTest : public ::testing::Test {
317public:
318 void SetUp() override {
319 initMockServiceManager();
320 lshal = std::make_unique<Lshal>(out, err, serviceManager, passthruManager);
321 initMockList();
322 }
323
324 void initMockList() {
325 mockList = std::make_unique<NiceMock<MockListCommand>>(lshal.get());
326 ON_CALL(*mockList, getPidInfo(_,_)).WillByDefault(Invoke(
327 [](pid_t serverPid, PidInfo* info) {
328 *info = getPidInfoFromId(serverPid);
329 return true;
330 }));
331 ON_CALL(*mockList, parseCmdline(_)).WillByDefault(Invoke(&getCmdlineFromId));
Yifan Hong93b8bff2017-09-14 16:02:52 -0700332 ON_CALL(*mockList, postprocess()).WillByDefault(Invoke([&]() {
333 mockList->internalPostprocess();
334 size_t i = 0;
335 mockList->forEachTable([&](Table& table) {
336 table.setDescription("[fake description " + std::to_string(i++) + "]");
337 });
338 }));
Yifan Hongf31aa052018-02-02 15:17:51 -0800339 ON_CALL(*mockList, getPartition(_)).WillByDefault(Return(Partition::VENDOR));
Yifan Hong8bf73162017-09-07 18:06:13 -0700340 }
341
342 void initMockServiceManager() {
343 serviceManager = new testing::NiceMock<MockServiceManager>();
344 passthruManager = new testing::NiceMock<MockServiceManager>();
345 using A = DebugInfo::Architecture;
346 ON_CALL(*serviceManager, list(_)).WillByDefault(Invoke(
347 [] (IServiceManager::list_cb cb) {
348 cb({ getFqInstanceName(1), getFqInstanceName(2) });
349 return hardware::Void();
350 }));
351
352 ON_CALL(*serviceManager, get(_, _)).WillByDefault(Invoke(
353 [&](const hidl_string&, const hidl_string& instance) {
354 int id = getIdFromInstanceName(instance);
Yifan Hongfee209d2017-09-14 18:23:38 -0700355 return sp<IBase>(new TestService(id));
Yifan Hong8bf73162017-09-07 18:06:13 -0700356 }));
357
358 ON_CALL(*serviceManager, debugDump(_)).WillByDefault(Invoke(
359 [] (IServiceManager::debugDump_cb cb) {
360 cb({InstanceDebugInfo{getInterfaceName(3), getInstanceName(3), 3,
361 getClients(3), A::IS_32BIT},
362 InstanceDebugInfo{getInterfaceName(4), getInstanceName(4), 4,
363 getClients(4), A::IS_32BIT}});
364 return hardware::Void();
365 }));
366
367 ON_CALL(*passthruManager, debugDump(_)).WillByDefault(Invoke(
368 [] (IServiceManager::debugDump_cb cb) {
369 cb({InstanceDebugInfo{getInterfaceName(5), getInstanceName(5), 5,
370 getClients(5), A::IS_32BIT},
371 InstanceDebugInfo{getInterfaceName(6), getInstanceName(6), 6,
372 getClients(6), A::IS_32BIT}});
373 return hardware::Void();
374 }));
375 }
376
377 std::stringstream err;
378 std::stringstream out;
379 std::unique_ptr<Lshal> lshal;
380 std::unique_ptr<MockListCommand> mockList;
381 sp<MockServiceManager> serviceManager;
382 sp<MockServiceManager> passthruManager;
383};
384
Yifan Hong1243dde2017-09-14 17:49:30 -0700385TEST_F(ListTest, GetPidInfoCached) {
386 EXPECT_CALL(*mockList, getPidInfo(5, _)).Times(1);
387
388 EXPECT_NE(nullptr, mockList->getPidInfoCached(5));
389 EXPECT_NE(nullptr, mockList->getPidInfoCached(5));
390}
391
Yifan Hong8bf73162017-09-07 18:06:13 -0700392TEST_F(ListTest, Fetch) {
393 EXPECT_EQ(0u, mockList->fetch());
Yifan Hong0ad64f52018-05-25 15:29:17 -0700394 vintf::TransportArch hwbinder{Transport::HWBINDER, Arch::ARCH_64};
395 vintf::TransportArch passthrough{Transport::PASSTHROUGH, Arch::ARCH_32};
396 std::array<vintf::TransportArch, 6> transportArchs{{hwbinder, hwbinder, passthrough,
397 passthrough, passthrough, passthrough}};
Yifan Hong8bf73162017-09-07 18:06:13 -0700398 int id = 1;
399 mockList->forEachTable([&](const Table& table) {
400 ASSERT_EQ(2u, table.size());
401 for (const auto& entry : table) {
Yifan Hong0ad64f52018-05-25 15:29:17 -0700402 auto transport = transportArchs.at(id - 1).transport;
Yifan Hong8bf73162017-09-07 18:06:13 -0700403 TableEntry expected{
404 .interfaceName = getFqInstanceName(id),
405 .transport = transport,
Yifan Hong8304e412018-05-25 15:05:36 -0700406 .serverPid = transport == Transport::HWBINDER ? id : NO_PID,
407 .threadUsage =
408 transport == Transport::HWBINDER ? getPidInfoFromId(id).threadUsage : 0,
409 .threadCount =
410 transport == Transport::HWBINDER ? getPidInfoFromId(id).threadCount : 0,
Yifan Hong8bf73162017-09-07 18:06:13 -0700411 .serverCmdline = {},
Yifan Hong8304e412018-05-25 15:05:36 -0700412 .serverObjectAddress = transport == Transport::HWBINDER ? getPtr(id) : NO_PTR,
Yifan Hong8bf73162017-09-07 18:06:13 -0700413 .clientPids = getClients(id),
414 .clientCmdlines = {},
Yifan Hong0ad64f52018-05-25 15:29:17 -0700415 .arch = transportArchs.at(id - 1).arch,
Yifan Hong8bf73162017-09-07 18:06:13 -0700416 };
417 EXPECT_EQ(expected, entry) << expected.to_string() << " vs. " << entry.to_string();
418
419 ++id;
420 }
421 });
422
423}
424
425TEST_F(ListTest, DumpVintf) {
Yifan Hongb2d096a2018-05-01 15:25:23 -0700426 const std::string expected = "<manifest version=\"1.0\" type=\"device\">\n"
427 " <hal format=\"hidl\">\n"
428 " <name>a.h.foo1</name>\n"
429 " <transport>hwbinder</transport>\n"
430 " <fqname>@1.0::IFoo/1</fqname>\n"
431 " </hal>\n"
432 " <hal format=\"hidl\">\n"
433 " <name>a.h.foo2</name>\n"
434 " <transport>hwbinder</transport>\n"
435 " <fqname>@2.0::IFoo/2</fqname>\n"
436 " </hal>\n"
437 " <hal format=\"hidl\">\n"
438 " <name>a.h.foo3</name>\n"
439 " <transport arch=\"32\">passthrough</transport>\n"
440 " <fqname>@3.0::IFoo/3</fqname>\n"
441 " </hal>\n"
442 " <hal format=\"hidl\">\n"
443 " <name>a.h.foo4</name>\n"
444 " <transport arch=\"32\">passthrough</transport>\n"
445 " <fqname>@4.0::IFoo/4</fqname>\n"
446 " </hal>\n"
447 "</manifest>";
Yifan Hong8bf73162017-09-07 18:06:13 -0700448
449 optind = 1; // mimic Lshal::parseArg()
450 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "--init-vintf"})));
Yifan Hongb2d096a2018-05-01 15:25:23 -0700451 auto output = out.str();
452 EXPECT_THAT(output, HasSubstr(expected));
453 EXPECT_THAT(output, HasSubstr("a.h.foo5@5.0::IFoo/5"));
454 EXPECT_THAT(output, HasSubstr("a.h.foo6@6.0::IFoo/6"));
Yifan Hong8bf73162017-09-07 18:06:13 -0700455 EXPECT_EQ("", err.str());
456
457 vintf::HalManifest m;
458 EXPECT_EQ(true, vintf::gHalManifestConverter(&m, out.str()))
459 << "--init-vintf does not emit valid HAL manifest: "
460 << vintf::gHalManifestConverter.lastError();
461}
462
Yifan Hongfee209d2017-09-14 18:23:38 -0700463// test default columns
464TEST_F(ListTest, DumpDefault) {
465 const std::string expected =
466 "[fake description 0]\n"
467 "R Interface Thread Use Server Clients\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700468 "N a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700469 "Y a.h.foo2@2.0::IFoo/2 12/22 2 3 5\n"
470 "\n"
471 "[fake description 1]\n"
472 "R Interface Thread Use Server Clients\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700473 "? a.h.foo3@3.0::IFoo/3 N/A N/A 4 6\n"
474 "? a.h.foo4@4.0::IFoo/4 N/A N/A 5 7\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700475 "\n"
476 "[fake description 2]\n"
477 "R Interface Thread Use Server Clients\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700478 "? a.h.foo5@5.0::IFoo/5 N/A N/A 6 8\n"
479 "? a.h.foo6@6.0::IFoo/6 N/A N/A 7 9\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700480 "\n";
481
482 optind = 1; // mimic Lshal::parseArg()
483 EXPECT_EQ(0u, mockList->main(createArg({"lshal"})));
484 EXPECT_EQ(expected, out.str());
485 EXPECT_EQ("", err.str());
486}
487
488TEST_F(ListTest, DumpHash) {
489 const std::string expected =
490 "[fake description 0]\n"
491 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700492 "a.h.foo1@1.0::IFoo/1 N 0000000000000000000000000000000000000000000000000000000000000000\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700493 "a.h.foo2@2.0::IFoo/2 Y 0202020202020202020202020202020202020202020202020202020202020202\n"
494 "\n"
495 "[fake description 1]\n"
496 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700497 "a.h.foo3@3.0::IFoo/3 ? \n"
498 "a.h.foo4@4.0::IFoo/4 ? \n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700499 "\n"
500 "[fake description 2]\n"
501 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700502 "a.h.foo5@5.0::IFoo/5 ? \n"
503 "a.h.foo6@6.0::IFoo/6 ? \n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700504 "\n";
505
506 optind = 1; // mimic Lshal::parseArg()
507 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-ils"})));
508 EXPECT_EQ(expected, out.str());
509 EXPECT_EQ("", err.str());
510}
511
Yifan Hong8bf73162017-09-07 18:06:13 -0700512TEST_F(ListTest, Dump) {
513 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700514 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700515 "Interface Transport Arch Thread Use Server PTR Clients\n"
516 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
517 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
518 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700519 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700520 "Interface Transport Arch Thread Use Server PTR Clients\n"
521 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
522 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
523 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700524 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700525 "Interface Transport Arch Thread Use Server PTR Clients\n"
526 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
527 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
528 "\n";
529
530 optind = 1; // mimic Lshal::parseArg()
531 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac"})));
532 EXPECT_EQ(expected, out.str());
533 EXPECT_EQ("", err.str());
534}
535
536TEST_F(ListTest, DumpCmdline) {
537 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700538 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700539 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
540 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 command_line_1 0000000000002711 command_line_2;command_line_4\n"
541 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 command_line_2 0000000000002712 command_line_3;command_line_5\n"
542 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700543 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700544 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
545 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A command_line_4;command_line_6\n"
546 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A command_line_5;command_line_7\n"
547 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700548 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700549 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
550 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A command_line_6;command_line_8\n"
551 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A command_line_7;command_line_9\n"
552 "\n";
553
554 optind = 1; // mimic Lshal::parseArg()
555 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepacm"})));
556 EXPECT_EQ(expected, out.str());
557 EXPECT_EQ("", err.str());
558}
559
560TEST_F(ListTest, DumpNeat) {
561 const std::string expected =
562 "a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n"
563 "a.h.foo2@2.0::IFoo/2 12/22 2 3 5\n"
564 "a.h.foo3@3.0::IFoo/3 N/A N/A 4 6\n"
565 "a.h.foo4@4.0::IFoo/4 N/A N/A 5 7\n"
566 "a.h.foo5@5.0::IFoo/5 N/A N/A 6 8\n"
567 "a.h.foo6@6.0::IFoo/6 N/A N/A 7 9\n";
568
569 optind = 1; // mimic Lshal::parseArg()
Yifan Hong7a3b46c2017-09-14 18:18:53 -0700570 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-iepc", "--neat"})));
Yifan Hong8bf73162017-09-07 18:06:13 -0700571 EXPECT_EQ(expected, out.str());
572 EXPECT_EQ("", err.str());
573}
Yifan Honga8bedc62017-09-08 18:00:31 -0700574
Nirav Atrecce988d2018-05-16 11:14:46 -0700575TEST_F(ListTest, DumpSingleHalType) {
576 const std::string expected =
577 "[fake description 0]\n"
578 "Interface Transport Arch Thread Use Server PTR Clients\n"
579 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
580 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
581 "\n";
582
583 optind = 1; // mimic Lshal::parseArg()
584 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=binderized"})));
585 EXPECT_EQ(expected, out.str());
586 EXPECT_EQ("", err.str());
587}
588
589TEST_F(ListTest, DumpReorderedHalTypes) {
590 const std::string expected =
591 "[fake description 0]\n"
592 "Interface Transport Arch Thread Use Server PTR Clients\n"
593 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
594 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
595 "\n"
596 "[fake description 1]\n"
597 "Interface Transport Arch Thread Use Server PTR Clients\n"
598 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
599 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
600 "\n"
601 "[fake description 2]\n"
602 "Interface Transport Arch Thread Use Server PTR Clients\n"
603 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
604 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
605 "\n";
606
607 optind = 1; // mimic Lshal::parseArg()
608 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=passthrough_clients",
609 "--types=passthrough_libs", "--types=binderized"})));
610 EXPECT_EQ(expected, out.str());
611 EXPECT_EQ("", err.str());
612}
613
614TEST_F(ListTest, DumpAbbreviatedHalTypes) {
615 const std::string expected =
616 "[fake description 0]\n"
617 "Interface Transport Arch Thread Use Server PTR Clients\n"
618 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
619 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
620 "\n"
621 "[fake description 1]\n"
622 "Interface Transport Arch Thread Use Server PTR Clients\n"
623 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
624 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
625 "\n";
626
627 optind = 1; // mimic Lshal::parseArg()
628 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,l"})));
629 EXPECT_EQ(expected, out.str());
630 EXPECT_EQ("", err.str());
631}
632
633TEST_F(ListTest, DumpEmptyAndDuplicateHalTypes) {
634 const std::string expected =
635 "[fake description 0]\n"
636 "Interface Transport Arch Thread Use Server PTR Clients\n"
637 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
638 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
639 "\n"
640 "[fake description 1]\n"
641 "Interface Transport Arch Thread Use Server PTR Clients\n"
642 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
643 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
644 "\n";
645
646 optind = 1; // mimic Lshal::parseArg()
647 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,l,,,l,l,c,",
648 "--types=passthrough_libs,passthrough_clients"})));
649 EXPECT_EQ(expected, out.str());
650 EXPECT_EQ("", err.str());
651}
652
653TEST_F(ListTest, UnknownHalType) {
654 optind = 1; // mimic Lshal::parseArg()
655 EXPECT_EQ(1u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,a"})));
656 EXPECT_THAT(err.str(), HasSubstr("Unrecognized HAL type: a"));
657}
658
Yifan Honga8bedc62017-09-08 18:00:31 -0700659class HelpTest : public ::testing::Test {
660public:
661 void SetUp() override {
662 lshal = std::make_unique<Lshal>(out, err, new MockServiceManager() /* serviceManager */,
663 new MockServiceManager() /* passthruManager */);
664 }
665
666 std::stringstream err;
667 std::stringstream out;
668 std::unique_ptr<Lshal> lshal;
669};
670
671TEST_F(HelpTest, GlobalUsage) {
672 (void)callMain(lshal, {"lshal", "--help"}); // ignore return
673 std::string errStr = err.str();
674 EXPECT_THAT(errStr, ContainsRegex("(^|\n)commands:($|\n)"))
675 << "`lshal --help` does not contain global usage";
676 EXPECT_THAT(errStr, ContainsRegex("(^|\n)list:($|\n)"))
677 << "`lshal --help` does not contain usage for 'list' command";
678 EXPECT_THAT(errStr, ContainsRegex("(^|\n)debug:($|\n)"))
679 << "`lshal --help` does not contain usage for 'debug' command";
680 EXPECT_THAT(errStr, ContainsRegex("(^|\n)help:($|\n)"))
681 << "`lshal --help` does not contain usage for 'help' command";
682
683 err.str("");
684 (void)callMain(lshal, {"lshal", "help"}); // ignore return
685 EXPECT_EQ(errStr, err.str()) << "`lshal help` should have the same output as `lshal --help`";
686
687 err.str("");
688 EXPECT_NE(0u, callMain(lshal, {"lshal", "--unknown-option"}));
689 EXPECT_THAT(err.str(), ContainsRegex("unrecognized option"));
690 EXPECT_THAT(err.str(), EndsWith(errStr))
691 << "`lshal --unknown-option` should have the same output as `lshal --help`";
692 EXPECT_EQ("", out.str());
693}
694
695TEST_F(HelpTest, UnknownOptionList1) {
696 (void)callMain(lshal, {"lshal", "help", "list"});
697 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)"))
698 << "`lshal help list` does not contain usage for 'list' command";
699}
700
701TEST_F(HelpTest, UnknownOptionList2) {
702 EXPECT_NE(0u, callMain(lshal, {"lshal", "list", "--unknown-option"}));
703 EXPECT_THAT(err.str(), ContainsRegex("unrecognized option"));
704 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)"))
705 << "`lshal list --unknown-option` does not contain usage for 'list' command";
706 EXPECT_EQ("", out.str());
707}
708
709TEST_F(HelpTest, UnknownOptionHelp1) {
710 (void)callMain(lshal, {"lshal", "help", "help"});
711 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)"))
712 << "`lshal help help` does not contain usage for 'help' command";
713}
714
715TEST_F(HelpTest, UnknownOptionHelp2) {
716 (void)callMain(lshal, {"lshal", "help", "--unknown-option"});
717 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)"))
718 << "`lshal help --unknown-option` does not contain usage for 'help' command";
719 EXPECT_EQ("", out.str());
720}
721
Yifan Hong9881df92017-05-10 14:33:05 -0700722} // namespace lshal
723} // namespace android
724
725int main(int argc, char **argv) {
726 ::testing::InitGoogleMock(&argc, argv);
727 return RUN_ALL_TESTS();
728}