blob: bb23b2cb55c265406dd6aaef85844c2a6482c477 [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;
42using ::android::hardware::hidl_death_recipient;
43using ::android::hardware::hidl_handle;
44using ::android::hardware::hidl_string;
45using ::android::hardware::hidl_vec;
46
Yifan Hong8bf73162017-09-07 18:06:13 -070047using InstanceDebugInfo = IServiceManager::InstanceDebugInfo;
48
Yifan Hong9881df92017-05-10 14:33:05 -070049namespace android {
50namespace hardware {
51namespace tests {
52namespace baz {
53namespace V1_0 {
54namespace implementation {
55struct Quux : android::hardware::tests::baz::V1_0::IQuux {
56 ::android::hardware::Return<void> debug(const hidl_handle& hh, const hidl_vec<hidl_string>& options) override {
57 const native_handle_t *handle = hh.getNativeHandle();
58 if (handle->numFds < 1) {
59 return Void();
60 }
61 int fd = handle->data[0];
62 std::string content{descriptor};
63 for (const auto &option : options) {
64 content += "\n";
65 content += option.c_str();
66 }
67 ssize_t written = write(fd, content.c_str(), content.size());
68 if (written != (ssize_t)content.size()) {
69 LOG(WARNING) << "SERVER(Quux) debug writes " << written << " bytes < "
70 << content.size() << " bytes, errno = " << errno;
71 }
72 return Void();
73 }
74};
75
76} // namespace implementation
77} // namespace V1_0
78} // namespace baz
79} // namespace tests
80} // namespace hardware
81
82namespace lshal {
83
Yifan Hong9881df92017-05-10 14:33:05 -070084class MockServiceManager : public IServiceManager {
85public:
86 template<typename T>
87 using R = ::android::hardware::Return<T>;
88 using String = const hidl_string&;
89 ~MockServiceManager() = default;
90
91#define MOCK_METHOD_CB(name) MOCK_METHOD1(name, R<void>(IServiceManager::name##_cb))
92
93 MOCK_METHOD2(get, R<sp<IBase>>(String, String));
94 MOCK_METHOD2(add, R<bool>(String, const sp<IBase>&));
95 MOCK_METHOD2(getTransport, R<IServiceManager::Transport>(String, String));
96 MOCK_METHOD_CB(list);
97 MOCK_METHOD2(listByInterface, R<void>(String, listByInterface_cb));
98 MOCK_METHOD3(registerForNotifications, R<bool>(String, String, const sp<IServiceNotification>&));
99 MOCK_METHOD_CB(debugDump);
100 MOCK_METHOD2(registerPassthroughClient, R<void>(String, String));
101 MOCK_METHOD_CB(interfaceChain);
102 MOCK_METHOD2(debug, R<void>(const hidl_handle&, const hidl_vec<hidl_string>&));
103 MOCK_METHOD_CB(interfaceDescriptor);
104 MOCK_METHOD_CB(getHashChain);
105 MOCK_METHOD0(setHalInstrumentation, R<void>());
106 MOCK_METHOD2(linkToDeath, R<bool>(const sp<hidl_death_recipient>&, uint64_t));
107 MOCK_METHOD0(ping, R<void>());
108 MOCK_METHOD_CB(getDebugInfo);
109 MOCK_METHOD0(notifySyspropsChanged, R<void>());
110 MOCK_METHOD1(unlinkToDeath, R<bool>(const sp<hidl_death_recipient>&));
111
112};
113
Yifan Hongbf20a262017-09-07 11:10:58 -0700114class DebugTest : public ::testing::Test {
Yifan Hong9881df92017-05-10 14:33:05 -0700115public:
116 void SetUp() override {
117 using ::android::hardware::tests::baz::V1_0::IQuux;
118 using ::android::hardware::tests::baz::V1_0::implementation::Quux;
119
120 err.str("");
121 out.str("");
122 serviceManager = new testing::NiceMock<MockServiceManager>();
123 ON_CALL(*serviceManager, get(_, _)).WillByDefault(Invoke(
124 [](const auto &iface, const auto &inst) -> ::android::hardware::Return<sp<IBase>> {
125 if (iface == IQuux::descriptor && inst == "default")
126 return new Quux();
127 return nullptr;
128 }));
Yifan Hongbf20a262017-09-07 11:10:58 -0700129
130 lshal = std::make_unique<Lshal>(out, err, serviceManager, serviceManager);
Yifan Hong9881df92017-05-10 14:33:05 -0700131 }
132 void TearDown() override {}
133
134 std::stringstream err;
135 std::stringstream out;
136 sp<MockServiceManager> serviceManager;
Yifan Hongbf20a262017-09-07 11:10:58 -0700137
138 std::unique_ptr<Lshal> lshal;
Yifan Hong9881df92017-05-10 14:33:05 -0700139};
140
Yifan Hongbf20a262017-09-07 11:10:58 -0700141static Arg createArg(const std::vector<const char*>& args) {
142 return Arg{static_cast<int>(args.size()), const_cast<char**>(args.data())};
143}
144
145template<typename T>
146static Status callMain(const std::unique_ptr<T>& lshal, const std::vector<const char*>& args) {
147 return lshal->main(createArg(args));
148}
149
150TEST_F(DebugTest, Debug) {
151 EXPECT_EQ(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700152 "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux/default", "foo", "bar"
Yifan Hongbf20a262017-09-07 11:10:58 -0700153 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700154 EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nfoo\nbar"));
155 EXPECT_THAT(err.str(), IsEmpty());
156}
157
Yifan Hongbf20a262017-09-07 11:10:58 -0700158TEST_F(DebugTest, Debug2) {
159 EXPECT_EQ(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700160 "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux", "baz", "quux"
Yifan Hongbf20a262017-09-07 11:10:58 -0700161 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700162 EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nbaz\nquux"));
163 EXPECT_THAT(err.str(), IsEmpty());
164}
165
Yifan Hongbf20a262017-09-07 11:10:58 -0700166TEST_F(DebugTest, Debug3) {
167 EXPECT_NE(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700168 "lshal", "debug", "android.hardware.tests.doesnotexist@1.0::IDoesNotExist",
Yifan Hongbf20a262017-09-07 11:10:58 -0700169 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700170 EXPECT_THAT(err.str(), HasSubstr("does not exist"));
171}
172
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700173class MockLshal : public Lshal {
174public:
175 MockLshal() {}
176 ~MockLshal() = default;
177 MOCK_CONST_METHOD0(out, NullableOStream<std::ostream>());
178 MOCK_CONST_METHOD0(err, NullableOStream<std::ostream>());
179};
180
181// expose protected fields and methods for ListCommand
182class MockListCommand : public ListCommand {
183public:
184 MockListCommand(Lshal* lshal) : ListCommand(*lshal) {}
185
Yifan Honga8bedc62017-09-08 18:00:31 -0700186 Status parseArgs(const Arg& arg) { return ListCommand::parseArgs(arg); }
187 Status main(const Arg& arg) { return ListCommand::main(arg); }
Yifan Hong93b8bff2017-09-14 16:02:52 -0700188 void forEachTable(const std::function<void(Table &)> &f) {
189 return ListCommand::forEachTable(f);
190 }
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700191 void forEachTable(const std::function<void(const Table &)> &f) const {
192 return ListCommand::forEachTable(f);
193 }
Yifan Hong8bf73162017-09-07 18:06:13 -0700194 Status fetch() { return ListCommand::fetch(); }
195 void dumpVintf(const NullableOStream<std::ostream>& out) {
196 return ListCommand::dumpVintf(out);
197 }
Yifan Hong93b8bff2017-09-14 16:02:52 -0700198 void internalPostprocess() { ListCommand::postprocess(); }
Yifan Hong8bf73162017-09-07 18:06:13 -0700199
Yifan Hong93b8bff2017-09-14 16:02:52 -0700200 MOCK_METHOD0(postprocess, void());
Yifan Hong8bf73162017-09-07 18:06:13 -0700201 MOCK_CONST_METHOD2(getPidInfo, bool(pid_t, PidInfo*));
202 MOCK_CONST_METHOD1(parseCmdline, std::string(pid_t));
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700203};
204
205class ListParseArgsTest : public ::testing::Test {
206public:
207 void SetUp() override {
208 mockLshal = std::make_unique<NiceMock<MockLshal>>();
209 mockList = std::make_unique<MockListCommand>(mockLshal.get());
210 // ListCommand::parseArgs should parse arguments from the second element
211 optind = 1;
212 }
213 std::unique_ptr<MockLshal> mockLshal;
214 std::unique_ptr<MockListCommand> mockList;
215 std::stringstream output;
216};
217
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700218TEST_F(ListParseArgsTest, Args) {
219 EXPECT_EQ(0u, mockList->parseArgs(createArg({"lshal", "-p", "-i", "-a", "-c"})));
220 mockList->forEachTable([](const Table& table) {
221 EXPECT_EQ(SelectedColumns({TableColumnType::SERVER_PID, TableColumnType::INTERFACE_NAME,
222 TableColumnType::SERVER_ADDR, TableColumnType::CLIENT_PIDS}),
223 table.getSelectedColumns());
224 });
225}
226
227TEST_F(ListParseArgsTest, Cmds) {
228 EXPECT_EQ(0u, mockList->parseArgs(createArg({"lshal", "-m"})));
229 mockList->forEachTable([](const Table& table) {
Yifan Hong7a3b46c2017-09-14 18:18:53 -0700230 EXPECT_THAT(table.getSelectedColumns(), Not(Contains(TableColumnType::SERVER_PID)))
231 << "should not print server PID with -m";
232 EXPECT_THAT(table.getSelectedColumns(), Not(Contains(TableColumnType::CLIENT_PIDS)))
233 << "should not print client PIDs with -m";
234 EXPECT_THAT(table.getSelectedColumns(), Contains(TableColumnType::SERVER_CMD))
235 << "should print server cmd with -m";
236 EXPECT_THAT(table.getSelectedColumns(), Contains(TableColumnType::CLIENT_CMDS))
237 << "should print client cmds with -m";
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700238 });
239}
240
241TEST_F(ListParseArgsTest, DebugAndNeat) {
242 ON_CALL(*mockLshal, err()).WillByDefault(Return(NullableOStream<std::ostream>(output)));
243 EXPECT_NE(0u, mockList->parseArgs(createArg({"lshal", "--neat", "-d"})));
244 EXPECT_THAT(output.str(), StrNe(""));
245}
246
Yifan Hong8bf73162017-09-07 18:06:13 -0700247/// Fetch Test
248
249// A set of deterministic functions to generate fake debug infos.
250static uint64_t getPtr(pid_t serverId) { return 10000 + serverId; }
251static std::vector<pid_t> getClients(pid_t serverId) {
252 return {serverId + 1, serverId + 3};
253}
254static PidInfo getPidInfoFromId(pid_t serverId) {
255 PidInfo info;
256 info.refPids[getPtr(serverId)] = getClients(serverId);
257 info.threadUsage = 10 + serverId;
258 info.threadCount = 20 + serverId;
259 return info;
260}
261static std::string getInterfaceName(pid_t serverId) {
262 return "a.h.foo" + std::to_string(serverId) + "@" + std::to_string(serverId) + ".0::IFoo";
263}
264static std::string getInstanceName(pid_t serverId) {
265 return std::to_string(serverId);
266}
267static pid_t getIdFromInstanceName(const hidl_string& instance) {
268 return atoi(instance.c_str());
269}
270static std::string getFqInstanceName(pid_t serverId) {
271 return getInterfaceName(serverId) + "/" + getInstanceName(serverId);
272}
273static std::string getCmdlineFromId(pid_t serverId) {
274 if (serverId == NO_PID) return "";
275 return "command_line_" + std::to_string(serverId);
276}
277
278// Fake service returned by mocked IServiceManager::get.
279class TestService : public IBase {
280public:
281 TestService(DebugInfo&& info) : mInfo(std::move(info)) {}
282 hardware::Return<void> getDebugInfo(getDebugInfo_cb cb) override {
283 cb(mInfo);
284 return hardware::Void();
285 }
286private:
287 DebugInfo mInfo;
288};
289
290class ListTest : public ::testing::Test {
291public:
292 void SetUp() override {
293 initMockServiceManager();
294 lshal = std::make_unique<Lshal>(out, err, serviceManager, passthruManager);
295 initMockList();
296 }
297
298 void initMockList() {
299 mockList = std::make_unique<NiceMock<MockListCommand>>(lshal.get());
300 ON_CALL(*mockList, getPidInfo(_,_)).WillByDefault(Invoke(
301 [](pid_t serverPid, PidInfo* info) {
302 *info = getPidInfoFromId(serverPid);
303 return true;
304 }));
305 ON_CALL(*mockList, parseCmdline(_)).WillByDefault(Invoke(&getCmdlineFromId));
Yifan Hong93b8bff2017-09-14 16:02:52 -0700306 ON_CALL(*mockList, postprocess()).WillByDefault(Invoke([&]() {
307 mockList->internalPostprocess();
308 size_t i = 0;
309 mockList->forEachTable([&](Table& table) {
310 table.setDescription("[fake description " + std::to_string(i++) + "]");
311 });
312 }));
Yifan Hong8bf73162017-09-07 18:06:13 -0700313 }
314
315 void initMockServiceManager() {
316 serviceManager = new testing::NiceMock<MockServiceManager>();
317 passthruManager = new testing::NiceMock<MockServiceManager>();
318 using A = DebugInfo::Architecture;
319 ON_CALL(*serviceManager, list(_)).WillByDefault(Invoke(
320 [] (IServiceManager::list_cb cb) {
321 cb({ getFqInstanceName(1), getFqInstanceName(2) });
322 return hardware::Void();
323 }));
324
325 ON_CALL(*serviceManager, get(_, _)).WillByDefault(Invoke(
326 [&](const hidl_string&, const hidl_string& instance) {
327 int id = getIdFromInstanceName(instance);
328 return sp<IBase>(new TestService({ id /* pid */, getPtr(id), A::IS_64BIT }));
329 }));
330
331 ON_CALL(*serviceManager, debugDump(_)).WillByDefault(Invoke(
332 [] (IServiceManager::debugDump_cb cb) {
333 cb({InstanceDebugInfo{getInterfaceName(3), getInstanceName(3), 3,
334 getClients(3), A::IS_32BIT},
335 InstanceDebugInfo{getInterfaceName(4), getInstanceName(4), 4,
336 getClients(4), A::IS_32BIT}});
337 return hardware::Void();
338 }));
339
340 ON_CALL(*passthruManager, debugDump(_)).WillByDefault(Invoke(
341 [] (IServiceManager::debugDump_cb cb) {
342 cb({InstanceDebugInfo{getInterfaceName(5), getInstanceName(5), 5,
343 getClients(5), A::IS_32BIT},
344 InstanceDebugInfo{getInterfaceName(6), getInstanceName(6), 6,
345 getClients(6), A::IS_32BIT}});
346 return hardware::Void();
347 }));
348 }
349
350 std::stringstream err;
351 std::stringstream out;
352 std::unique_ptr<Lshal> lshal;
353 std::unique_ptr<MockListCommand> mockList;
354 sp<MockServiceManager> serviceManager;
355 sp<MockServiceManager> passthruManager;
356};
357
358TEST_F(ListTest, Fetch) {
359 EXPECT_EQ(0u, mockList->fetch());
360 std::array<std::string, 6> transports{{"hwbinder", "hwbinder", "passthrough",
361 "passthrough", "passthrough", "passthrough"}};
362 std::array<Architecture, 6> archs{{ARCH64, ARCH64, ARCH32, ARCH32, ARCH32, ARCH32}};
363 int id = 1;
364 mockList->forEachTable([&](const Table& table) {
365 ASSERT_EQ(2u, table.size());
366 for (const auto& entry : table) {
367 const auto& transport = transports[id - 1];
368 TableEntry expected{
369 .interfaceName = getFqInstanceName(id),
370 .transport = transport,
371 .serverPid = transport == "hwbinder" ? id : NO_PID,
372 .threadUsage = transport == "hwbinder" ? getPidInfoFromId(id).threadUsage : 0,
373 .threadCount = transport == "hwbinder" ? getPidInfoFromId(id).threadCount : 0,
374 .serverCmdline = {},
375 .serverObjectAddress = transport == "hwbinder" ? getPtr(id) : NO_PTR,
376 .clientPids = getClients(id),
377 .clientCmdlines = {},
378 .arch = archs[id - 1],
379 };
380 EXPECT_EQ(expected, entry) << expected.to_string() << " vs. " << entry.to_string();
381
382 ++id;
383 }
384 });
385
386}
387
388TEST_F(ListTest, DumpVintf) {
389 const std::string expected =
390 "<!-- \n"
391 " This is a skeleton device manifest. Notes: \n"
392 " 1. android.hidl.*, android.frameworks.*, android.system.* are not included.\n"
393 " 2. If a HAL is supported in both hwbinder and passthrough transport, \n"
394 " only hwbinder is shown.\n"
395 " 3. It is likely that HALs in passthrough transport does not have\n"
396 " <interface> declared; users will have to write them by hand.\n"
397 " 4. A HAL with lower minor version can be overridden by a HAL with\n"
398 " higher minor version if they have the same name and major version.\n"
399 " 5. sepolicy version is set to 0.0. It is recommended that the entry\n"
400 " is removed from the manifest file and written by assemble_vintf\n"
401 " at build time.\n"
402 "-->\n"
403 "<manifest version=\"1.0\" type=\"device\">\n"
404 " <hal format=\"hidl\">\n"
405 " <name>a.h.foo1</name>\n"
406 " <transport>hwbinder</transport>\n"
407 " <version>1.0</version>\n"
408 " <interface>\n"
409 " <name>IFoo</name>\n"
410 " <instance>1</instance>\n"
411 " </interface>\n"
412 " </hal>\n"
413 " <hal format=\"hidl\">\n"
414 " <name>a.h.foo2</name>\n"
415 " <transport>hwbinder</transport>\n"
416 " <version>2.0</version>\n"
417 " <interface>\n"
418 " <name>IFoo</name>\n"
419 " <instance>2</instance>\n"
420 " </interface>\n"
421 " </hal>\n"
422 " <hal format=\"hidl\">\n"
423 " <name>a.h.foo3</name>\n"
424 " <transport arch=\"32\">passthrough</transport>\n"
425 " <version>3.0</version>\n"
426 " <interface>\n"
427 " <name>IFoo</name>\n"
428 " <instance>3</instance>\n"
429 " </interface>\n"
430 " </hal>\n"
431 " <hal format=\"hidl\">\n"
432 " <name>a.h.foo4</name>\n"
433 " <transport arch=\"32\">passthrough</transport>\n"
434 " <version>4.0</version>\n"
435 " <interface>\n"
436 " <name>IFoo</name>\n"
437 " <instance>4</instance>\n"
438 " </interface>\n"
439 " </hal>\n"
440 " <hal format=\"hidl\">\n"
441 " <name>a.h.foo5</name>\n"
442 " <transport arch=\"32\">passthrough</transport>\n"
443 " <version>5.0</version>\n"
444 " </hal>\n"
445 " <hal format=\"hidl\">\n"
446 " <name>a.h.foo6</name>\n"
447 " <transport arch=\"32\">passthrough</transport>\n"
448 " <version>6.0</version>\n"
449 " </hal>\n"
450 " <sepolicy>\n"
451 " <version>0.0</version>\n"
452 " </sepolicy>\n"
453 "</manifest>\n";
454
455 optind = 1; // mimic Lshal::parseArg()
456 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "--init-vintf"})));
457 EXPECT_EQ(expected, out.str());
458 EXPECT_EQ("", err.str());
459
460 vintf::HalManifest m;
461 EXPECT_EQ(true, vintf::gHalManifestConverter(&m, out.str()))
462 << "--init-vintf does not emit valid HAL manifest: "
463 << vintf::gHalManifestConverter.lastError();
464}
465
466TEST_F(ListTest, Dump) {
467 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700468 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700469 "Interface Transport Arch Thread Use Server PTR Clients\n"
470 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
471 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
472 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700473 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700474 "Interface Transport Arch Thread Use Server PTR Clients\n"
475 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
476 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
477 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700478 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700479 "Interface Transport Arch Thread Use Server PTR Clients\n"
480 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
481 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
482 "\n";
483
484 optind = 1; // mimic Lshal::parseArg()
485 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac"})));
486 EXPECT_EQ(expected, out.str());
487 EXPECT_EQ("", err.str());
488}
489
490TEST_F(ListTest, DumpCmdline) {
491 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700492 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700493 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
494 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 command_line_1 0000000000002711 command_line_2;command_line_4\n"
495 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 command_line_2 0000000000002712 command_line_3;command_line_5\n"
496 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700497 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700498 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
499 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A command_line_4;command_line_6\n"
500 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A command_line_5;command_line_7\n"
501 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700502 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700503 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
504 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A command_line_6;command_line_8\n"
505 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A command_line_7;command_line_9\n"
506 "\n";
507
508 optind = 1; // mimic Lshal::parseArg()
509 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepacm"})));
510 EXPECT_EQ(expected, out.str());
511 EXPECT_EQ("", err.str());
512}
513
514TEST_F(ListTest, DumpNeat) {
515 const std::string expected =
516 "a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n"
517 "a.h.foo2@2.0::IFoo/2 12/22 2 3 5\n"
518 "a.h.foo3@3.0::IFoo/3 N/A N/A 4 6\n"
519 "a.h.foo4@4.0::IFoo/4 N/A N/A 5 7\n"
520 "a.h.foo5@5.0::IFoo/5 N/A N/A 6 8\n"
521 "a.h.foo6@6.0::IFoo/6 N/A N/A 7 9\n";
522
523 optind = 1; // mimic Lshal::parseArg()
Yifan Hong7a3b46c2017-09-14 18:18:53 -0700524 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-iepc", "--neat"})));
Yifan Hong8bf73162017-09-07 18:06:13 -0700525 EXPECT_EQ(expected, out.str());
526 EXPECT_EQ("", err.str());
527}
Yifan Honga8bedc62017-09-08 18:00:31 -0700528
529class HelpTest : public ::testing::Test {
530public:
531 void SetUp() override {
532 lshal = std::make_unique<Lshal>(out, err, new MockServiceManager() /* serviceManager */,
533 new MockServiceManager() /* passthruManager */);
534 }
535
536 std::stringstream err;
537 std::stringstream out;
538 std::unique_ptr<Lshal> lshal;
539};
540
541TEST_F(HelpTest, GlobalUsage) {
542 (void)callMain(lshal, {"lshal", "--help"}); // ignore return
543 std::string errStr = err.str();
544 EXPECT_THAT(errStr, ContainsRegex("(^|\n)commands:($|\n)"))
545 << "`lshal --help` does not contain global usage";
546 EXPECT_THAT(errStr, ContainsRegex("(^|\n)list:($|\n)"))
547 << "`lshal --help` does not contain usage for 'list' command";
548 EXPECT_THAT(errStr, ContainsRegex("(^|\n)debug:($|\n)"))
549 << "`lshal --help` does not contain usage for 'debug' command";
550 EXPECT_THAT(errStr, ContainsRegex("(^|\n)help:($|\n)"))
551 << "`lshal --help` does not contain usage for 'help' command";
552
553 err.str("");
554 (void)callMain(lshal, {"lshal", "help"}); // ignore return
555 EXPECT_EQ(errStr, err.str()) << "`lshal help` should have the same output as `lshal --help`";
556
557 err.str("");
558 EXPECT_NE(0u, callMain(lshal, {"lshal", "--unknown-option"}));
559 EXPECT_THAT(err.str(), ContainsRegex("unrecognized option"));
560 EXPECT_THAT(err.str(), EndsWith(errStr))
561 << "`lshal --unknown-option` should have the same output as `lshal --help`";
562 EXPECT_EQ("", out.str());
563}
564
565TEST_F(HelpTest, UnknownOptionList1) {
566 (void)callMain(lshal, {"lshal", "help", "list"});
567 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)"))
568 << "`lshal help list` does not contain usage for 'list' command";
569}
570
571TEST_F(HelpTest, UnknownOptionList2) {
572 EXPECT_NE(0u, callMain(lshal, {"lshal", "list", "--unknown-option"}));
573 EXPECT_THAT(err.str(), ContainsRegex("unrecognized option"));
574 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)"))
575 << "`lshal list --unknown-option` does not contain usage for 'list' command";
576 EXPECT_EQ("", out.str());
577}
578
579TEST_F(HelpTest, UnknownOptionHelp1) {
580 (void)callMain(lshal, {"lshal", "help", "help"});
581 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)"))
582 << "`lshal help help` does not contain usage for 'help' command";
583}
584
585TEST_F(HelpTest, UnknownOptionHelp2) {
586 (void)callMain(lshal, {"lshal", "help", "--unknown-option"});
587 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)"))
588 << "`lshal help --unknown-option` does not contain usage for 'help' command";
589 EXPECT_EQ("", out.str());
590}
591
Yifan Hong9881df92017-05-10 14:33:05 -0700592} // namespace lshal
593} // namespace android
594
595int main(int argc, char **argv) {
596 ::testing::InitGoogleMock(&argc, argv);
597 return RUN_ALL_TESTS();
598}