blob: 4a9b061b1240df911d633ec27baf8923d3d2f648 [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;
47
Yifan Hong8bf73162017-09-07 18:06:13 -070048using InstanceDebugInfo = IServiceManager::InstanceDebugInfo;
49
Yifan Hongfee209d2017-09-14 18:23:38 -070050using hidl_hash = hidl_array<uint8_t, 32>;
51
Yifan Hong9881df92017-05-10 14:33:05 -070052namespace android {
53namespace hardware {
54namespace tests {
55namespace baz {
56namespace V1_0 {
57namespace implementation {
58struct Quux : android::hardware::tests::baz::V1_0::IQuux {
59 ::android::hardware::Return<void> debug(const hidl_handle& hh, const hidl_vec<hidl_string>& options) override {
60 const native_handle_t *handle = hh.getNativeHandle();
61 if (handle->numFds < 1) {
62 return Void();
63 }
64 int fd = handle->data[0];
65 std::string content{descriptor};
66 for (const auto &option : options) {
67 content += "\n";
68 content += option.c_str();
69 }
70 ssize_t written = write(fd, content.c_str(), content.size());
71 if (written != (ssize_t)content.size()) {
72 LOG(WARNING) << "SERVER(Quux) debug writes " << written << " bytes < "
73 << content.size() << " bytes, errno = " << errno;
74 }
75 return Void();
76 }
77};
78
79} // namespace implementation
80} // namespace V1_0
81} // namespace baz
82} // namespace tests
83} // namespace hardware
84
85namespace lshal {
86
Yifan Hong9881df92017-05-10 14:33:05 -070087class MockServiceManager : public IServiceManager {
88public:
89 template<typename T>
90 using R = ::android::hardware::Return<T>;
91 using String = const hidl_string&;
92 ~MockServiceManager() = default;
93
94#define MOCK_METHOD_CB(name) MOCK_METHOD1(name, R<void>(IServiceManager::name##_cb))
95
96 MOCK_METHOD2(get, R<sp<IBase>>(String, String));
97 MOCK_METHOD2(add, R<bool>(String, const sp<IBase>&));
98 MOCK_METHOD2(getTransport, R<IServiceManager::Transport>(String, String));
99 MOCK_METHOD_CB(list);
100 MOCK_METHOD2(listByInterface, R<void>(String, listByInterface_cb));
101 MOCK_METHOD3(registerForNotifications, R<bool>(String, String, const sp<IServiceNotification>&));
102 MOCK_METHOD_CB(debugDump);
103 MOCK_METHOD2(registerPassthroughClient, R<void>(String, String));
104 MOCK_METHOD_CB(interfaceChain);
105 MOCK_METHOD2(debug, R<void>(const hidl_handle&, const hidl_vec<hidl_string>&));
106 MOCK_METHOD_CB(interfaceDescriptor);
107 MOCK_METHOD_CB(getHashChain);
108 MOCK_METHOD0(setHalInstrumentation, R<void>());
109 MOCK_METHOD2(linkToDeath, R<bool>(const sp<hidl_death_recipient>&, uint64_t));
110 MOCK_METHOD0(ping, R<void>());
111 MOCK_METHOD_CB(getDebugInfo);
112 MOCK_METHOD0(notifySyspropsChanged, R<void>());
113 MOCK_METHOD1(unlinkToDeath, R<bool>(const sp<hidl_death_recipient>&));
114
115};
116
Yifan Hongbf20a262017-09-07 11:10:58 -0700117class DebugTest : public ::testing::Test {
Yifan Hong9881df92017-05-10 14:33:05 -0700118public:
119 void SetUp() override {
120 using ::android::hardware::tests::baz::V1_0::IQuux;
121 using ::android::hardware::tests::baz::V1_0::implementation::Quux;
122
123 err.str("");
124 out.str("");
125 serviceManager = new testing::NiceMock<MockServiceManager>();
126 ON_CALL(*serviceManager, get(_, _)).WillByDefault(Invoke(
127 [](const auto &iface, const auto &inst) -> ::android::hardware::Return<sp<IBase>> {
128 if (iface == IQuux::descriptor && inst == "default")
129 return new Quux();
130 return nullptr;
131 }));
Yifan Hongbf20a262017-09-07 11:10:58 -0700132
133 lshal = std::make_unique<Lshal>(out, err, serviceManager, serviceManager);
Yifan Hong9881df92017-05-10 14:33:05 -0700134 }
135 void TearDown() override {}
136
137 std::stringstream err;
138 std::stringstream out;
139 sp<MockServiceManager> serviceManager;
Yifan Hongbf20a262017-09-07 11:10:58 -0700140
141 std::unique_ptr<Lshal> lshal;
Yifan Hong9881df92017-05-10 14:33:05 -0700142};
143
Yifan Hongbf20a262017-09-07 11:10:58 -0700144static Arg createArg(const std::vector<const char*>& args) {
145 return Arg{static_cast<int>(args.size()), const_cast<char**>(args.data())};
146}
147
148template<typename T>
149static Status callMain(const std::unique_ptr<T>& lshal, const std::vector<const char*>& args) {
150 return lshal->main(createArg(args));
151}
152
153TEST_F(DebugTest, Debug) {
154 EXPECT_EQ(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700155 "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux/default", "foo", "bar"
Yifan Hongbf20a262017-09-07 11:10:58 -0700156 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700157 EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nfoo\nbar"));
158 EXPECT_THAT(err.str(), IsEmpty());
159}
160
Yifan Hongbf20a262017-09-07 11:10:58 -0700161TEST_F(DebugTest, Debug2) {
162 EXPECT_EQ(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700163 "lshal", "debug", "android.hardware.tests.baz@1.0::IQuux", "baz", "quux"
Yifan Hongbf20a262017-09-07 11:10:58 -0700164 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700165 EXPECT_THAT(out.str(), StrEq("android.hardware.tests.baz@1.0::IQuux\nbaz\nquux"));
166 EXPECT_THAT(err.str(), IsEmpty());
167}
168
Yifan Hongbf20a262017-09-07 11:10:58 -0700169TEST_F(DebugTest, Debug3) {
170 EXPECT_NE(0u, callMain(lshal, {
Yifan Hong9881df92017-05-10 14:33:05 -0700171 "lshal", "debug", "android.hardware.tests.doesnotexist@1.0::IDoesNotExist",
Yifan Hongbf20a262017-09-07 11:10:58 -0700172 }));
Yifan Hong9881df92017-05-10 14:33:05 -0700173 EXPECT_THAT(err.str(), HasSubstr("does not exist"));
174}
175
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700176class MockLshal : public Lshal {
177public:
178 MockLshal() {}
179 ~MockLshal() = default;
180 MOCK_CONST_METHOD0(out, NullableOStream<std::ostream>());
181 MOCK_CONST_METHOD0(err, NullableOStream<std::ostream>());
182};
183
184// expose protected fields and methods for ListCommand
185class MockListCommand : public ListCommand {
186public:
187 MockListCommand(Lshal* lshal) : ListCommand(*lshal) {}
188
Yifan Honga8bedc62017-09-08 18:00:31 -0700189 Status parseArgs(const Arg& arg) { return ListCommand::parseArgs(arg); }
190 Status main(const Arg& arg) { return ListCommand::main(arg); }
Yifan Hong93b8bff2017-09-14 16:02:52 -0700191 void forEachTable(const std::function<void(Table &)> &f) {
192 return ListCommand::forEachTable(f);
193 }
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700194 void forEachTable(const std::function<void(const Table &)> &f) const {
195 return ListCommand::forEachTable(f);
196 }
Yifan Hong8bf73162017-09-07 18:06:13 -0700197 Status fetch() { return ListCommand::fetch(); }
198 void dumpVintf(const NullableOStream<std::ostream>& out) {
199 return ListCommand::dumpVintf(out);
200 }
Yifan Hong93b8bff2017-09-14 16:02:52 -0700201 void internalPostprocess() { ListCommand::postprocess(); }
Yifan Hong1243dde2017-09-14 17:49:30 -0700202 const PidInfo* getPidInfoCached(pid_t serverPid) {
203 return ListCommand::getPidInfoCached(serverPid);
204 }
Yifan Hong8bf73162017-09-07 18:06:13 -0700205
Yifan Hong93b8bff2017-09-14 16:02:52 -0700206 MOCK_METHOD0(postprocess, void());
Yifan Hong8bf73162017-09-07 18:06:13 -0700207 MOCK_CONST_METHOD2(getPidInfo, bool(pid_t, PidInfo*));
208 MOCK_CONST_METHOD1(parseCmdline, std::string(pid_t));
Yifan Hongf31aa052018-02-02 15:17:51 -0800209 MOCK_METHOD1(getPartition, Partition(pid_t));
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700210};
211
212class ListParseArgsTest : public ::testing::Test {
213public:
214 void SetUp() override {
215 mockLshal = std::make_unique<NiceMock<MockLshal>>();
216 mockList = std::make_unique<MockListCommand>(mockLshal.get());
217 // ListCommand::parseArgs should parse arguments from the second element
218 optind = 1;
219 }
220 std::unique_ptr<MockLshal> mockLshal;
221 std::unique_ptr<MockListCommand> mockList;
222 std::stringstream output;
223};
224
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700225TEST_F(ListParseArgsTest, Args) {
226 EXPECT_EQ(0u, mockList->parseArgs(createArg({"lshal", "-p", "-i", "-a", "-c"})));
227 mockList->forEachTable([](const Table& table) {
228 EXPECT_EQ(SelectedColumns({TableColumnType::SERVER_PID, TableColumnType::INTERFACE_NAME,
229 TableColumnType::SERVER_ADDR, TableColumnType::CLIENT_PIDS}),
230 table.getSelectedColumns());
231 });
232}
233
234TEST_F(ListParseArgsTest, Cmds) {
235 EXPECT_EQ(0u, mockList->parseArgs(createArg({"lshal", "-m"})));
236 mockList->forEachTable([](const Table& table) {
Yifan Hong7a3b46c2017-09-14 18:18:53 -0700237 EXPECT_THAT(table.getSelectedColumns(), Not(Contains(TableColumnType::SERVER_PID)))
238 << "should not print server PID with -m";
239 EXPECT_THAT(table.getSelectedColumns(), Not(Contains(TableColumnType::CLIENT_PIDS)))
240 << "should not print client PIDs with -m";
241 EXPECT_THAT(table.getSelectedColumns(), Contains(TableColumnType::SERVER_CMD))
242 << "should print server cmd with -m";
243 EXPECT_THAT(table.getSelectedColumns(), Contains(TableColumnType::CLIENT_CMDS))
244 << "should print client cmds with -m";
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700245 });
246}
247
248TEST_F(ListParseArgsTest, DebugAndNeat) {
249 ON_CALL(*mockLshal, err()).WillByDefault(Return(NullableOStream<std::ostream>(output)));
250 EXPECT_NE(0u, mockList->parseArgs(createArg({"lshal", "--neat", "-d"})));
251 EXPECT_THAT(output.str(), StrNe(""));
252}
253
Yifan Hong8bf73162017-09-07 18:06:13 -0700254/// Fetch Test
255
256// A set of deterministic functions to generate fake debug infos.
257static uint64_t getPtr(pid_t serverId) { return 10000 + serverId; }
258static std::vector<pid_t> getClients(pid_t serverId) {
259 return {serverId + 1, serverId + 3};
260}
261static PidInfo getPidInfoFromId(pid_t serverId) {
262 PidInfo info;
263 info.refPids[getPtr(serverId)] = getClients(serverId);
264 info.threadUsage = 10 + serverId;
265 info.threadCount = 20 + serverId;
266 return info;
267}
268static std::string getInterfaceName(pid_t serverId) {
269 return "a.h.foo" + std::to_string(serverId) + "@" + std::to_string(serverId) + ".0::IFoo";
270}
271static std::string getInstanceName(pid_t serverId) {
272 return std::to_string(serverId);
273}
274static pid_t getIdFromInstanceName(const hidl_string& instance) {
275 return atoi(instance.c_str());
276}
277static std::string getFqInstanceName(pid_t serverId) {
278 return getInterfaceName(serverId) + "/" + getInstanceName(serverId);
279}
280static std::string getCmdlineFromId(pid_t serverId) {
281 if (serverId == NO_PID) return "";
282 return "command_line_" + std::to_string(serverId);
283}
Yifan Hongfee209d2017-09-14 18:23:38 -0700284static bool getIsReleasedFromId(pid_t p) { return p % 2 == 0; }
285static hidl_hash getHashFromId(pid_t serverId) {
286 hidl_hash hash;
287 bool isReleased = getIsReleasedFromId(serverId);
288 for (size_t i = 0; i < hash.size(); ++i) {
289 hash[i] = isReleased ? static_cast<uint8_t>(serverId) : 0u;
290 }
291 return hash;
292}
Yifan Hong8bf73162017-09-07 18:06:13 -0700293
294// Fake service returned by mocked IServiceManager::get.
295class TestService : public IBase {
296public:
Yifan Hongfee209d2017-09-14 18:23:38 -0700297 TestService(pid_t id) : mId(id) {}
Yifan Hong8bf73162017-09-07 18:06:13 -0700298 hardware::Return<void> getDebugInfo(getDebugInfo_cb cb) override {
Yifan Hongfee209d2017-09-14 18:23:38 -0700299 cb({ mId /* pid */, getPtr(mId), DebugInfo::Architecture::IS_64BIT });
300 return hardware::Void();
301 }
302 hardware::Return<void> interfaceChain(interfaceChain_cb cb) override {
303 cb({getInterfaceName(mId), IBase::descriptor});
304 return hardware::Void();
305 }
306 hardware::Return<void> getHashChain(getHashChain_cb cb) override {
307 cb({getHashFromId(mId), getHashFromId(0xff)});
Yifan Hong8bf73162017-09-07 18:06:13 -0700308 return hardware::Void();
309 }
310private:
Yifan Hongfee209d2017-09-14 18:23:38 -0700311 pid_t mId;
Yifan Hong8bf73162017-09-07 18:06:13 -0700312};
313
314class ListTest : public ::testing::Test {
315public:
316 void SetUp() override {
317 initMockServiceManager();
318 lshal = std::make_unique<Lshal>(out, err, serviceManager, passthruManager);
319 initMockList();
320 }
321
322 void initMockList() {
323 mockList = std::make_unique<NiceMock<MockListCommand>>(lshal.get());
324 ON_CALL(*mockList, getPidInfo(_,_)).WillByDefault(Invoke(
325 [](pid_t serverPid, PidInfo* info) {
326 *info = getPidInfoFromId(serverPid);
327 return true;
328 }));
329 ON_CALL(*mockList, parseCmdline(_)).WillByDefault(Invoke(&getCmdlineFromId));
Yifan Hong93b8bff2017-09-14 16:02:52 -0700330 ON_CALL(*mockList, postprocess()).WillByDefault(Invoke([&]() {
331 mockList->internalPostprocess();
332 size_t i = 0;
333 mockList->forEachTable([&](Table& table) {
334 table.setDescription("[fake description " + std::to_string(i++) + "]");
335 });
336 }));
Yifan Hongf31aa052018-02-02 15:17:51 -0800337 ON_CALL(*mockList, getPartition(_)).WillByDefault(Return(Partition::VENDOR));
Yifan Hong8bf73162017-09-07 18:06:13 -0700338 }
339
340 void initMockServiceManager() {
341 serviceManager = new testing::NiceMock<MockServiceManager>();
342 passthruManager = new testing::NiceMock<MockServiceManager>();
343 using A = DebugInfo::Architecture;
344 ON_CALL(*serviceManager, list(_)).WillByDefault(Invoke(
345 [] (IServiceManager::list_cb cb) {
346 cb({ getFqInstanceName(1), getFqInstanceName(2) });
347 return hardware::Void();
348 }));
349
350 ON_CALL(*serviceManager, get(_, _)).WillByDefault(Invoke(
351 [&](const hidl_string&, const hidl_string& instance) {
352 int id = getIdFromInstanceName(instance);
Yifan Hongfee209d2017-09-14 18:23:38 -0700353 return sp<IBase>(new TestService(id));
Yifan Hong8bf73162017-09-07 18:06:13 -0700354 }));
355
356 ON_CALL(*serviceManager, debugDump(_)).WillByDefault(Invoke(
357 [] (IServiceManager::debugDump_cb cb) {
358 cb({InstanceDebugInfo{getInterfaceName(3), getInstanceName(3), 3,
359 getClients(3), A::IS_32BIT},
360 InstanceDebugInfo{getInterfaceName(4), getInstanceName(4), 4,
361 getClients(4), A::IS_32BIT}});
362 return hardware::Void();
363 }));
364
365 ON_CALL(*passthruManager, debugDump(_)).WillByDefault(Invoke(
366 [] (IServiceManager::debugDump_cb cb) {
367 cb({InstanceDebugInfo{getInterfaceName(5), getInstanceName(5), 5,
368 getClients(5), A::IS_32BIT},
369 InstanceDebugInfo{getInterfaceName(6), getInstanceName(6), 6,
370 getClients(6), A::IS_32BIT}});
371 return hardware::Void();
372 }));
373 }
374
375 std::stringstream err;
376 std::stringstream out;
377 std::unique_ptr<Lshal> lshal;
378 std::unique_ptr<MockListCommand> mockList;
379 sp<MockServiceManager> serviceManager;
380 sp<MockServiceManager> passthruManager;
381};
382
Yifan Hong1243dde2017-09-14 17:49:30 -0700383TEST_F(ListTest, GetPidInfoCached) {
384 EXPECT_CALL(*mockList, getPidInfo(5, _)).Times(1);
385
386 EXPECT_NE(nullptr, mockList->getPidInfoCached(5));
387 EXPECT_NE(nullptr, mockList->getPidInfoCached(5));
388}
389
Yifan Hong8bf73162017-09-07 18:06:13 -0700390TEST_F(ListTest, Fetch) {
391 EXPECT_EQ(0u, mockList->fetch());
392 std::array<std::string, 6> transports{{"hwbinder", "hwbinder", "passthrough",
393 "passthrough", "passthrough", "passthrough"}};
394 std::array<Architecture, 6> archs{{ARCH64, ARCH64, ARCH32, ARCH32, ARCH32, ARCH32}};
395 int id = 1;
396 mockList->forEachTable([&](const Table& table) {
397 ASSERT_EQ(2u, table.size());
398 for (const auto& entry : table) {
399 const auto& transport = transports[id - 1];
400 TableEntry expected{
401 .interfaceName = getFqInstanceName(id),
402 .transport = transport,
403 .serverPid = transport == "hwbinder" ? id : NO_PID,
404 .threadUsage = transport == "hwbinder" ? getPidInfoFromId(id).threadUsage : 0,
405 .threadCount = transport == "hwbinder" ? getPidInfoFromId(id).threadCount : 0,
406 .serverCmdline = {},
407 .serverObjectAddress = transport == "hwbinder" ? getPtr(id) : NO_PTR,
408 .clientPids = getClients(id),
409 .clientCmdlines = {},
410 .arch = archs[id - 1],
411 };
412 EXPECT_EQ(expected, entry) << expected.to_string() << " vs. " << entry.to_string();
413
414 ++id;
415 }
416 });
417
418}
419
420TEST_F(ListTest, DumpVintf) {
Yifan Hongb2d096a2018-05-01 15:25:23 -0700421 const std::string expected = "<manifest version=\"1.0\" type=\"device\">\n"
422 " <hal format=\"hidl\">\n"
423 " <name>a.h.foo1</name>\n"
424 " <transport>hwbinder</transport>\n"
425 " <fqname>@1.0::IFoo/1</fqname>\n"
426 " </hal>\n"
427 " <hal format=\"hidl\">\n"
428 " <name>a.h.foo2</name>\n"
429 " <transport>hwbinder</transport>\n"
430 " <fqname>@2.0::IFoo/2</fqname>\n"
431 " </hal>\n"
432 " <hal format=\"hidl\">\n"
433 " <name>a.h.foo3</name>\n"
434 " <transport arch=\"32\">passthrough</transport>\n"
435 " <fqname>@3.0::IFoo/3</fqname>\n"
436 " </hal>\n"
437 " <hal format=\"hidl\">\n"
438 " <name>a.h.foo4</name>\n"
439 " <transport arch=\"32\">passthrough</transport>\n"
440 " <fqname>@4.0::IFoo/4</fqname>\n"
441 " </hal>\n"
442 "</manifest>";
Yifan Hong8bf73162017-09-07 18:06:13 -0700443
444 optind = 1; // mimic Lshal::parseArg()
445 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "--init-vintf"})));
Yifan Hongb2d096a2018-05-01 15:25:23 -0700446 auto output = out.str();
447 EXPECT_THAT(output, HasSubstr(expected));
448 EXPECT_THAT(output, HasSubstr("a.h.foo5@5.0::IFoo/5"));
449 EXPECT_THAT(output, HasSubstr("a.h.foo6@6.0::IFoo/6"));
Yifan Hong8bf73162017-09-07 18:06:13 -0700450 EXPECT_EQ("", err.str());
451
452 vintf::HalManifest m;
453 EXPECT_EQ(true, vintf::gHalManifestConverter(&m, out.str()))
454 << "--init-vintf does not emit valid HAL manifest: "
455 << vintf::gHalManifestConverter.lastError();
456}
457
Yifan Hongfee209d2017-09-14 18:23:38 -0700458// test default columns
459TEST_F(ListTest, DumpDefault) {
460 const std::string expected =
461 "[fake description 0]\n"
462 "R Interface Thread Use Server Clients\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700463 "N a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700464 "Y a.h.foo2@2.0::IFoo/2 12/22 2 3 5\n"
465 "\n"
466 "[fake description 1]\n"
467 "R Interface Thread Use Server Clients\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700468 "? a.h.foo3@3.0::IFoo/3 N/A N/A 4 6\n"
469 "? a.h.foo4@4.0::IFoo/4 N/A N/A 5 7\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700470 "\n"
471 "[fake description 2]\n"
472 "R Interface Thread Use Server Clients\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700473 "? a.h.foo5@5.0::IFoo/5 N/A N/A 6 8\n"
474 "? a.h.foo6@6.0::IFoo/6 N/A N/A 7 9\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700475 "\n";
476
477 optind = 1; // mimic Lshal::parseArg()
478 EXPECT_EQ(0u, mockList->main(createArg({"lshal"})));
479 EXPECT_EQ(expected, out.str());
480 EXPECT_EQ("", err.str());
481}
482
483TEST_F(ListTest, DumpHash) {
484 const std::string expected =
485 "[fake description 0]\n"
486 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700487 "a.h.foo1@1.0::IFoo/1 N 0000000000000000000000000000000000000000000000000000000000000000\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700488 "a.h.foo2@2.0::IFoo/2 Y 0202020202020202020202020202020202020202020202020202020202020202\n"
489 "\n"
490 "[fake description 1]\n"
491 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700492 "a.h.foo3@3.0::IFoo/3 ? \n"
493 "a.h.foo4@4.0::IFoo/4 ? \n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700494 "\n"
495 "[fake description 2]\n"
496 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700497 "a.h.foo5@5.0::IFoo/5 ? \n"
498 "a.h.foo6@6.0::IFoo/6 ? \n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700499 "\n";
500
501 optind = 1; // mimic Lshal::parseArg()
502 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-ils"})));
503 EXPECT_EQ(expected, out.str());
504 EXPECT_EQ("", err.str());
505}
506
Yifan Hong8bf73162017-09-07 18:06:13 -0700507TEST_F(ListTest, Dump) {
508 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700509 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700510 "Interface Transport Arch Thread Use Server PTR Clients\n"
511 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
512 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
513 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700514 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700515 "Interface Transport Arch Thread Use Server PTR Clients\n"
516 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
517 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
518 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700519 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700520 "Interface Transport Arch Thread Use Server PTR Clients\n"
521 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
522 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
523 "\n";
524
525 optind = 1; // mimic Lshal::parseArg()
526 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac"})));
527 EXPECT_EQ(expected, out.str());
528 EXPECT_EQ("", err.str());
529}
530
531TEST_F(ListTest, DumpCmdline) {
532 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700533 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700534 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
535 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 command_line_1 0000000000002711 command_line_2;command_line_4\n"
536 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 command_line_2 0000000000002712 command_line_3;command_line_5\n"
537 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700538 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700539 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
540 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A command_line_4;command_line_6\n"
541 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A command_line_5;command_line_7\n"
542 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700543 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700544 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
545 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A command_line_6;command_line_8\n"
546 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A command_line_7;command_line_9\n"
547 "\n";
548
549 optind = 1; // mimic Lshal::parseArg()
550 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepacm"})));
551 EXPECT_EQ(expected, out.str());
552 EXPECT_EQ("", err.str());
553}
554
555TEST_F(ListTest, DumpNeat) {
556 const std::string expected =
557 "a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n"
558 "a.h.foo2@2.0::IFoo/2 12/22 2 3 5\n"
559 "a.h.foo3@3.0::IFoo/3 N/A N/A 4 6\n"
560 "a.h.foo4@4.0::IFoo/4 N/A N/A 5 7\n"
561 "a.h.foo5@5.0::IFoo/5 N/A N/A 6 8\n"
562 "a.h.foo6@6.0::IFoo/6 N/A N/A 7 9\n";
563
564 optind = 1; // mimic Lshal::parseArg()
Yifan Hong7a3b46c2017-09-14 18:18:53 -0700565 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-iepc", "--neat"})));
Yifan Hong8bf73162017-09-07 18:06:13 -0700566 EXPECT_EQ(expected, out.str());
567 EXPECT_EQ("", err.str());
568}
Yifan Honga8bedc62017-09-08 18:00:31 -0700569
Nirav Atrecce988d2018-05-16 11:14:46 -0700570TEST_F(ListTest, DumpSingleHalType) {
571 const std::string expected =
572 "[fake description 0]\n"
573 "Interface Transport Arch Thread Use Server PTR Clients\n"
574 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
575 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
576 "\n";
577
578 optind = 1; // mimic Lshal::parseArg()
579 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=binderized"})));
580 EXPECT_EQ(expected, out.str());
581 EXPECT_EQ("", err.str());
582}
583
584TEST_F(ListTest, DumpReorderedHalTypes) {
585 const std::string expected =
586 "[fake description 0]\n"
587 "Interface Transport Arch Thread Use Server PTR Clients\n"
588 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
589 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
590 "\n"
591 "[fake description 1]\n"
592 "Interface Transport Arch Thread Use Server PTR Clients\n"
593 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
594 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
595 "\n"
596 "[fake description 2]\n"
597 "Interface Transport Arch Thread Use Server PTR Clients\n"
598 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
599 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
600 "\n";
601
602 optind = 1; // mimic Lshal::parseArg()
603 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=passthrough_clients",
604 "--types=passthrough_libs", "--types=binderized"})));
605 EXPECT_EQ(expected, out.str());
606 EXPECT_EQ("", err.str());
607}
608
609TEST_F(ListTest, DumpAbbreviatedHalTypes) {
610 const std::string expected =
611 "[fake description 0]\n"
612 "Interface Transport Arch Thread Use Server PTR Clients\n"
613 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
614 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
615 "\n"
616 "[fake description 1]\n"
617 "Interface Transport Arch Thread Use Server PTR Clients\n"
618 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
619 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
620 "\n";
621
622 optind = 1; // mimic Lshal::parseArg()
623 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,l"})));
624 EXPECT_EQ(expected, out.str());
625 EXPECT_EQ("", err.str());
626}
627
628TEST_F(ListTest, DumpEmptyAndDuplicateHalTypes) {
629 const std::string expected =
630 "[fake description 0]\n"
631 "Interface Transport Arch Thread Use Server PTR Clients\n"
632 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
633 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
634 "\n"
635 "[fake description 1]\n"
636 "Interface Transport Arch Thread Use Server PTR Clients\n"
637 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
638 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
639 "\n";
640
641 optind = 1; // mimic Lshal::parseArg()
642 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,l,,,l,l,c,",
643 "--types=passthrough_libs,passthrough_clients"})));
644 EXPECT_EQ(expected, out.str());
645 EXPECT_EQ("", err.str());
646}
647
648TEST_F(ListTest, UnknownHalType) {
649 optind = 1; // mimic Lshal::parseArg()
650 EXPECT_EQ(1u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,a"})));
651 EXPECT_THAT(err.str(), HasSubstr("Unrecognized HAL type: a"));
652}
653
Yifan Honga8bedc62017-09-08 18:00:31 -0700654class HelpTest : public ::testing::Test {
655public:
656 void SetUp() override {
657 lshal = std::make_unique<Lshal>(out, err, new MockServiceManager() /* serviceManager */,
658 new MockServiceManager() /* passthruManager */);
659 }
660
661 std::stringstream err;
662 std::stringstream out;
663 std::unique_ptr<Lshal> lshal;
664};
665
666TEST_F(HelpTest, GlobalUsage) {
667 (void)callMain(lshal, {"lshal", "--help"}); // ignore return
668 std::string errStr = err.str();
669 EXPECT_THAT(errStr, ContainsRegex("(^|\n)commands:($|\n)"))
670 << "`lshal --help` does not contain global usage";
671 EXPECT_THAT(errStr, ContainsRegex("(^|\n)list:($|\n)"))
672 << "`lshal --help` does not contain usage for 'list' command";
673 EXPECT_THAT(errStr, ContainsRegex("(^|\n)debug:($|\n)"))
674 << "`lshal --help` does not contain usage for 'debug' command";
675 EXPECT_THAT(errStr, ContainsRegex("(^|\n)help:($|\n)"))
676 << "`lshal --help` does not contain usage for 'help' command";
677
678 err.str("");
679 (void)callMain(lshal, {"lshal", "help"}); // ignore return
680 EXPECT_EQ(errStr, err.str()) << "`lshal help` should have the same output as `lshal --help`";
681
682 err.str("");
683 EXPECT_NE(0u, callMain(lshal, {"lshal", "--unknown-option"}));
684 EXPECT_THAT(err.str(), ContainsRegex("unrecognized option"));
685 EXPECT_THAT(err.str(), EndsWith(errStr))
686 << "`lshal --unknown-option` should have the same output as `lshal --help`";
687 EXPECT_EQ("", out.str());
688}
689
690TEST_F(HelpTest, UnknownOptionList1) {
691 (void)callMain(lshal, {"lshal", "help", "list"});
692 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)"))
693 << "`lshal help list` does not contain usage for 'list' command";
694}
695
696TEST_F(HelpTest, UnknownOptionList2) {
697 EXPECT_NE(0u, callMain(lshal, {"lshal", "list", "--unknown-option"}));
698 EXPECT_THAT(err.str(), ContainsRegex("unrecognized option"));
699 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)"))
700 << "`lshal list --unknown-option` does not contain usage for 'list' command";
701 EXPECT_EQ("", out.str());
702}
703
704TEST_F(HelpTest, UnknownOptionHelp1) {
705 (void)callMain(lshal, {"lshal", "help", "help"});
706 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)"))
707 << "`lshal help help` does not contain usage for 'help' command";
708}
709
710TEST_F(HelpTest, UnknownOptionHelp2) {
711 (void)callMain(lshal, {"lshal", "help", "--unknown-option"});
712 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)"))
713 << "`lshal help --unknown-option` does not contain usage for 'help' command";
714 EXPECT_EQ("", out.str());
715}
716
Yifan Hong9881df92017-05-10 14:33:05 -0700717} // namespace lshal
718} // namespace android
719
720int main(int argc, char **argv) {
721 ::testing::InitGoogleMock(&argc, argv);
722 return RUN_ALL_TESTS();
723}