blob: 3d550babf4ee6b5583d3a674fe4689da22c6c2f5 [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:
Chih-Hung Hsieh45e31c72018-12-20 15:47:01 -0800194 explicit MockListCommand(Lshal* lshal) : ListCommand(*lshal) {}
Yifan Hongb2a2ecb2017-09-07 15:08:22 -0700195
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:
Chih-Hung Hsieh45e31c72018-12-20 15:47:01 -0800311 explicit 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:
Yifan Hong13ba0a92018-06-25 16:15:56 -0700330 virtual void SetUp() override {
Yifan Hong8bf73162017-09-07 18:06:13 -0700331 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())
Yifan Hong13ba0a92018-06-25 16:15:56 -0700354 .WillByDefault(Return(std::make_shared<HalManifest>()));
Yifan Hongbdf44f82018-05-25 14:20:00 -0700355 ON_CALL(*mockList, getDeviceMatrix())
Yifan Hong13ba0a92018-06-25 16:15:56 -0700356 .WillByDefault(Return(std::make_shared<CompatibilityMatrix>()));
Yifan Hongbdf44f82018-05-25 14:20:00 -0700357 ON_CALL(*mockList, getFrameworkManifest())
Yifan Hong13ba0a92018-06-25 16:15:56 -0700358 .WillByDefault(Return(std::make_shared<HalManifest>()));
Yifan Hongbdf44f82018-05-25 14:20:00 -0700359 ON_CALL(*mockList, getFrameworkMatrix())
Yifan Hong13ba0a92018-06-25 16:15:56 -0700360 .WillByDefault(Return(std::make_shared<CompatibilityMatrix>()));
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) {
Yifan Hong13ba0a92018-06-25 16:15:56 -0700414 optind = 1; // mimic Lshal::parseArg()
415 ASSERT_EQ(0u, mockList->parseArgs(createArg({"lshal"})));
416 ASSERT_EQ(0u, mockList->fetch());
Yifan Hong0ad64f52018-05-25 15:29:17 -0700417 vintf::TransportArch hwbinder{Transport::HWBINDER, Arch::ARCH_64};
418 vintf::TransportArch passthrough{Transport::PASSTHROUGH, Arch::ARCH_32};
419 std::array<vintf::TransportArch, 6> transportArchs{{hwbinder, hwbinder, passthrough,
420 passthrough, passthrough, passthrough}};
Yifan Hong13ba0a92018-06-25 16:15:56 -0700421 int i = 0;
Yifan Hong8bf73162017-09-07 18:06:13 -0700422 mockList->forEachTable([&](const Table& table) {
Yifan Hong8bf73162017-09-07 18:06:13 -0700423 for (const auto& entry : table) {
Yifan Hong13ba0a92018-06-25 16:15:56 -0700424 if (i >= transportArchs.size()) {
425 break;
426 }
427
428 int id = i + 1;
429 auto transport = transportArchs.at(i).transport;
Yifan Hong8bf73162017-09-07 18:06:13 -0700430 TableEntry expected{
431 .interfaceName = getFqInstanceName(id),
432 .transport = transport,
Yifan Hong8304e412018-05-25 15:05:36 -0700433 .serverPid = transport == Transport::HWBINDER ? id : NO_PID,
434 .threadUsage =
435 transport == Transport::HWBINDER ? getPidInfoFromId(id).threadUsage : 0,
436 .threadCount =
437 transport == Transport::HWBINDER ? getPidInfoFromId(id).threadCount : 0,
Yifan Hong8bf73162017-09-07 18:06:13 -0700438 .serverCmdline = {},
Yifan Hong8304e412018-05-25 15:05:36 -0700439 .serverObjectAddress = transport == Transport::HWBINDER ? getPtr(id) : NO_PTR,
Yifan Hong8bf73162017-09-07 18:06:13 -0700440 .clientPids = getClients(id),
441 .clientCmdlines = {},
Yifan Hong13ba0a92018-06-25 16:15:56 -0700442 .arch = transportArchs.at(i).arch,
Yifan Hong8bf73162017-09-07 18:06:13 -0700443 };
444 EXPECT_EQ(expected, entry) << expected.to_string() << " vs. " << entry.to_string();
445
Yifan Hong13ba0a92018-06-25 16:15:56 -0700446 ++i;
Yifan Hong8bf73162017-09-07 18:06:13 -0700447 }
448 });
449
Yifan Hong13ba0a92018-06-25 16:15:56 -0700450 EXPECT_EQ(transportArchs.size(), i) << "Not all entries are tested.";
451
Yifan Hong8bf73162017-09-07 18:06:13 -0700452}
453
454TEST_F(ListTest, DumpVintf) {
Steven Morelandc3ba3a02019-09-09 10:41:49 -0700455 const std::string expected = "<manifest version=\"2.0\" type=\"device\">\n"
Yifan Hongb2d096a2018-05-01 15:25:23 -0700456 " <hal format=\"hidl\">\n"
457 " <name>a.h.foo1</name>\n"
458 " <transport>hwbinder</transport>\n"
459 " <fqname>@1.0::IFoo/1</fqname>\n"
460 " </hal>\n"
461 " <hal format=\"hidl\">\n"
462 " <name>a.h.foo2</name>\n"
463 " <transport>hwbinder</transport>\n"
464 " <fqname>@2.0::IFoo/2</fqname>\n"
465 " </hal>\n"
466 " <hal format=\"hidl\">\n"
467 " <name>a.h.foo3</name>\n"
468 " <transport arch=\"32\">passthrough</transport>\n"
469 " <fqname>@3.0::IFoo/3</fqname>\n"
470 " </hal>\n"
471 " <hal format=\"hidl\">\n"
472 " <name>a.h.foo4</name>\n"
473 " <transport arch=\"32\">passthrough</transport>\n"
474 " <fqname>@4.0::IFoo/4</fqname>\n"
475 " </hal>\n"
476 "</manifest>";
Yifan Hong8bf73162017-09-07 18:06:13 -0700477
478 optind = 1; // mimic Lshal::parseArg()
479 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "--init-vintf"})));
Yifan Hongb2d096a2018-05-01 15:25:23 -0700480 auto output = out.str();
481 EXPECT_THAT(output, HasSubstr(expected));
482 EXPECT_THAT(output, HasSubstr("a.h.foo5@5.0::IFoo/5"));
483 EXPECT_THAT(output, HasSubstr("a.h.foo6@6.0::IFoo/6"));
Yifan Hong8bf73162017-09-07 18:06:13 -0700484 EXPECT_EQ("", err.str());
485
486 vintf::HalManifest m;
487 EXPECT_EQ(true, vintf::gHalManifestConverter(&m, out.str()))
488 << "--init-vintf does not emit valid HAL manifest: "
489 << vintf::gHalManifestConverter.lastError();
490}
491
Yifan Hongfee209d2017-09-14 18:23:38 -0700492// test default columns
493TEST_F(ListTest, DumpDefault) {
494 const std::string expected =
495 "[fake description 0]\n"
Steven Morelandeae1a0b2019-06-25 13:01:49 -0700496 "VINTF R Interface Thread Use Server Clients\n"
497 "X N a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n"
498 "X Y a.h.foo2@2.0::IFoo/2 12/22 2 3 5\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700499 "\n"
500 "[fake description 1]\n"
Steven Morelandeae1a0b2019-06-25 13:01:49 -0700501 "VINTF R Interface Thread Use Server Clients\n"
502 "X ? a.h.foo3@3.0::IFoo/3 N/A N/A 4 6\n"
503 "X ? a.h.foo4@4.0::IFoo/4 N/A N/A 5 7\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700504 "\n"
505 "[fake description 2]\n"
Steven Morelandeae1a0b2019-06-25 13:01:49 -0700506 "VINTF R Interface Thread Use Server Clients\n"
507 "X ? a.h.foo5@5.0::IFoo/5 N/A N/A 6 8\n"
508 "X ? a.h.foo6@6.0::IFoo/6 N/A N/A 7 9\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700509 "\n";
510
511 optind = 1; // mimic Lshal::parseArg()
512 EXPECT_EQ(0u, mockList->main(createArg({"lshal"})));
513 EXPECT_EQ(expected, out.str());
514 EXPECT_EQ("", err.str());
515}
516
517TEST_F(ListTest, DumpHash) {
518 const std::string expected =
519 "[fake description 0]\n"
520 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700521 "a.h.foo1@1.0::IFoo/1 N 0000000000000000000000000000000000000000000000000000000000000000\n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700522 "a.h.foo2@2.0::IFoo/2 Y 0202020202020202020202020202020202020202020202020202020202020202\n"
523 "\n"
524 "[fake description 1]\n"
525 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700526 "a.h.foo3@3.0::IFoo/3 ? \n"
527 "a.h.foo4@4.0::IFoo/4 ? \n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700528 "\n"
529 "[fake description 2]\n"
530 "Interface R Hash\n"
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700531 "a.h.foo5@5.0::IFoo/5 ? \n"
532 "a.h.foo6@6.0::IFoo/6 ? \n"
Yifan Hongfee209d2017-09-14 18:23:38 -0700533 "\n";
534
535 optind = 1; // mimic Lshal::parseArg()
536 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-ils"})));
537 EXPECT_EQ(expected, out.str());
538 EXPECT_EQ("", err.str());
539}
540
Yifan Hong8bf73162017-09-07 18:06:13 -0700541TEST_F(ListTest, Dump) {
542 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700543 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700544 "Interface Transport Arch Thread Use Server PTR Clients\n"
545 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
546 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
547 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700548 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700549 "Interface Transport Arch Thread Use Server PTR Clients\n"
550 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
551 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
552 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700553 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700554 "Interface Transport Arch Thread Use Server PTR Clients\n"
555 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
556 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
557 "\n";
558
559 optind = 1; // mimic Lshal::parseArg()
560 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac"})));
561 EXPECT_EQ(expected, out.str());
562 EXPECT_EQ("", err.str());
563}
564
565TEST_F(ListTest, DumpCmdline) {
566 const std::string expected =
Yifan Hong93b8bff2017-09-14 16:02:52 -0700567 "[fake description 0]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700568 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
569 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 command_line_1 0000000000002711 command_line_2;command_line_4\n"
570 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 command_line_2 0000000000002712 command_line_3;command_line_5\n"
571 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700572 "[fake description 1]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700573 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
574 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A command_line_4;command_line_6\n"
575 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A command_line_5;command_line_7\n"
576 "\n"
Yifan Hong93b8bff2017-09-14 16:02:52 -0700577 "[fake description 2]\n"
Yifan Hong8bf73162017-09-07 18:06:13 -0700578 "Interface Transport Arch Thread Use Server CMD PTR Clients CMD\n"
579 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A command_line_6;command_line_8\n"
580 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A command_line_7;command_line_9\n"
581 "\n";
582
583 optind = 1; // mimic Lshal::parseArg()
584 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepacm"})));
585 EXPECT_EQ(expected, out.str());
586 EXPECT_EQ("", err.str());
587}
588
589TEST_F(ListTest, DumpNeat) {
590 const std::string expected =
591 "a.h.foo1@1.0::IFoo/1 11/21 1 2 4\n"
592 "a.h.foo2@2.0::IFoo/2 12/22 2 3 5\n"
593 "a.h.foo3@3.0::IFoo/3 N/A N/A 4 6\n"
594 "a.h.foo4@4.0::IFoo/4 N/A N/A 5 7\n"
595 "a.h.foo5@5.0::IFoo/5 N/A N/A 6 8\n"
596 "a.h.foo6@6.0::IFoo/6 N/A N/A 7 9\n";
597
598 optind = 1; // mimic Lshal::parseArg()
Yifan Hong7a3b46c2017-09-14 18:18:53 -0700599 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-iepc", "--neat"})));
Yifan Hong8bf73162017-09-07 18:06:13 -0700600 EXPECT_EQ(expected, out.str());
601 EXPECT_EQ("", err.str());
602}
Yifan Honga8bedc62017-09-08 18:00:31 -0700603
Nirav Atrecce988d2018-05-16 11:14:46 -0700604TEST_F(ListTest, DumpSingleHalType) {
605 const std::string expected =
606 "[fake description 0]\n"
607 "Interface Transport Arch Thread Use Server PTR Clients\n"
608 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
609 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
610 "\n";
611
612 optind = 1; // mimic Lshal::parseArg()
613 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=binderized"})));
614 EXPECT_EQ(expected, out.str());
615 EXPECT_EQ("", err.str());
616}
617
618TEST_F(ListTest, DumpReorderedHalTypes) {
619 const std::string expected =
620 "[fake description 0]\n"
621 "Interface Transport Arch Thread Use Server PTR Clients\n"
622 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
623 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
624 "\n"
625 "[fake description 1]\n"
626 "Interface Transport Arch Thread Use Server PTR Clients\n"
627 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
628 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
629 "\n"
630 "[fake description 2]\n"
631 "Interface Transport Arch Thread Use Server PTR Clients\n"
632 "a.h.foo1@1.0::IFoo/1 hwbinder 64 11/21 1 0000000000002711 2 4\n"
633 "a.h.foo2@2.0::IFoo/2 hwbinder 64 12/22 2 0000000000002712 3 5\n"
634 "\n";
635
636 optind = 1; // mimic Lshal::parseArg()
637 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=passthrough_clients",
638 "--types=passthrough_libs", "--types=binderized"})));
639 EXPECT_EQ(expected, out.str());
640 EXPECT_EQ("", err.str());
641}
642
643TEST_F(ListTest, DumpAbbreviatedHalTypes) {
644 const std::string expected =
645 "[fake description 0]\n"
646 "Interface Transport Arch Thread Use Server PTR Clients\n"
647 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
648 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
649 "\n"
650 "[fake description 1]\n"
651 "Interface Transport Arch Thread Use Server PTR Clients\n"
652 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
653 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
654 "\n";
655
656 optind = 1; // mimic Lshal::parseArg()
657 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,l"})));
658 EXPECT_EQ(expected, out.str());
659 EXPECT_EQ("", err.str());
660}
661
662TEST_F(ListTest, DumpEmptyAndDuplicateHalTypes) {
663 const std::string expected =
664 "[fake description 0]\n"
665 "Interface Transport Arch Thread Use Server PTR Clients\n"
666 "a.h.foo3@3.0::IFoo/3 passthrough 32 N/A N/A N/A 4 6\n"
667 "a.h.foo4@4.0::IFoo/4 passthrough 32 N/A N/A N/A 5 7\n"
668 "\n"
669 "[fake description 1]\n"
670 "Interface Transport Arch Thread Use Server PTR Clients\n"
671 "a.h.foo5@5.0::IFoo/5 passthrough 32 N/A N/A N/A 6 8\n"
672 "a.h.foo6@6.0::IFoo/6 passthrough 32 N/A N/A N/A 7 9\n"
673 "\n";
674
675 optind = 1; // mimic Lshal::parseArg()
676 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,l,,,l,l,c,",
677 "--types=passthrough_libs,passthrough_clients"})));
678 EXPECT_EQ(expected, out.str());
679 EXPECT_EQ("", err.str());
680}
681
682TEST_F(ListTest, UnknownHalType) {
683 optind = 1; // mimic Lshal::parseArg()
684 EXPECT_EQ(1u, mockList->main(createArg({"lshal", "-itrepac", "--types=c,a"})));
685 EXPECT_THAT(err.str(), HasSubstr("Unrecognized HAL type: a"));
686}
687
Yifan Hongbdf44f82018-05-25 14:20:00 -0700688TEST_F(ListTest, Vintf) {
689 std::string deviceManifestXml =
690 "<manifest version=\"1.0\" type=\"device\">\n"
691 " <hal>\n"
692 " <name>a.h.foo1</name>\n"
693 " <transport>hwbinder</transport>\n"
694 " <fqname>@1.0::IFoo/1</fqname>\n"
695 " </hal>\n"
696 " <hal>\n"
697 " <name>a.h.foo3</name>\n"
698 " <transport arch=\"32+64\">passthrough</transport>\n"
699 " <fqname>@3.0::IFoo/3</fqname>\n"
700 " </hal>\n"
701 "</manifest>\n";
702 std::string frameworkManifestXml =
703 "<manifest version=\"1.0\" type=\"framework\">\n"
704 " <hal>\n"
705 " <name>a.h.foo5</name>\n"
706 " <transport arch=\"32\">passthrough</transport>\n"
707 " <fqname>@5.0::IFoo/5</fqname>\n"
708 " </hal>\n"
709 "</manifest>\n";
710 std::string deviceMatrixXml =
711 "<compatibility-matrix version=\"1.0\" type=\"device\">\n"
712 " <hal>\n"
713 " <name>a.h.foo5</name>\n"
714 " <version>5.0</version>\n"
715 " <interface>\n"
716 " <name>IFoo</name>\n"
717 " <instance>5</instance>\n"
718 " </interface>\n"
719 " </hal>\n"
720 "</compatibility-matrix>\n";
721 std::string frameworkMatrixXml =
722 "<compatibility-matrix version=\"1.0\" type=\"framework\">\n"
723 " <hal>\n"
724 " <name>a.h.foo1</name>\n"
725 " <version>1.0</version>\n"
726 " <interface>\n"
727 " <name>IFoo</name>\n"
728 " <instance>1</instance>\n"
729 " </interface>\n"
730 " </hal>\n"
731 " <hal>\n"
732 " <name>a.h.foo3</name>\n"
733 " <version>3.0</version>\n"
734 " <interface>\n"
735 " <name>IFoo</name>\n"
736 " <instance>3</instance>\n"
737 " </interface>\n"
738 " </hal>\n"
739 "</compatibility-matrix>\n";
740
741 std::string expected = "DM,FC a.h.foo1@1.0::IFoo/1\n"
742 "X a.h.foo2@2.0::IFoo/2\n"
743 "DM,FC a.h.foo3@3.0::IFoo/3\n"
744 "X a.h.foo4@4.0::IFoo/4\n"
745 "DC,FM a.h.foo5@5.0::IFoo/5\n"
746 "X a.h.foo6@6.0::IFoo/6\n";
747
748 auto deviceManifest = std::make_shared<HalManifest>();
749 auto frameworkManifest = std::make_shared<HalManifest>();
750 auto deviceMatrix = std::make_shared<CompatibilityMatrix>();
751 auto frameworkMatrix = std::make_shared<CompatibilityMatrix>();
752
753 ASSERT_TRUE(gHalManifestConverter(deviceManifest.get(), deviceManifestXml));
754 ASSERT_TRUE(gHalManifestConverter(frameworkManifest.get(), frameworkManifestXml));
755 ASSERT_TRUE(gCompatibilityMatrixConverter(deviceMatrix.get(), deviceMatrixXml));
756 ASSERT_TRUE(gCompatibilityMatrixConverter(frameworkMatrix.get(), frameworkMatrixXml));
757
758 ON_CALL(*mockList, getDeviceManifest()).WillByDefault(Return(deviceManifest));
759 ON_CALL(*mockList, getDeviceMatrix()).WillByDefault(Return(deviceMatrix));
760 ON_CALL(*mockList, getFrameworkManifest()).WillByDefault(Return(frameworkManifest));
761 ON_CALL(*mockList, getFrameworkMatrix()).WillByDefault(Return(frameworkMatrix));
762
763 optind = 1; // mimic Lshal::parseArg()
764 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-Vi", "--neat"})));
765 EXPECT_THAT(out.str(), HasSubstr(expected));
766 EXPECT_EQ("", err.str());
767}
768
Yifan Hong13ba0a92018-06-25 16:15:56 -0700769class ListVintfTest : public ListTest {
770public:
771 virtual void SetUp() override {
772 ListTest::SetUp();
773 const std::string mockManifestXml =
774 "<manifest version=\"1.0\" type=\"device\">\n"
775 " <hal format=\"hidl\">\n"
776 " <name>a.h.foo1</name>\n"
777 " <transport>hwbinder</transport>\n"
778 " <fqname>@1.0::IFoo/1</fqname>\n"
779 " </hal>\n"
780 " <hal format=\"hidl\">\n"
781 " <name>a.h.bar1</name>\n"
782 " <transport>hwbinder</transport>\n"
783 " <fqname>@1.0::IBar/1</fqname>\n"
784 " </hal>\n"
785 " <hal format=\"hidl\">\n"
786 " <name>a.h.bar2</name>\n"
787 " <transport arch=\"32+64\">passthrough</transport>\n"
788 " <fqname>@2.0::IBar/2</fqname>\n"
789 " </hal>\n"
790 "</manifest>";
791 auto manifest = std::make_shared<HalManifest>();
792 EXPECT_TRUE(gHalManifestConverter(manifest.get(), mockManifestXml));
793 EXPECT_CALL(*mockList, getDeviceManifest())
794 .Times(AnyNumber())
795 .WillRepeatedly(Return(manifest));
796 }
797};
798
799TEST_F(ListVintfTest, ManifestHals) {
800 optind = 1; // mimic Lshal::parseArg()
801 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-iStr", "--types=v", "--neat"})));
802 EXPECT_THAT(out.str(), HasSubstr("a.h.bar1@1.0::IBar/1 declared hwbinder ?"));
803 EXPECT_THAT(out.str(), HasSubstr("a.h.bar2@2.0::IBar/2 declared passthrough 32+64"));
804 EXPECT_THAT(out.str(), HasSubstr("a.h.foo1@1.0::IFoo/1 declared hwbinder ?"));
805 EXPECT_EQ("", err.str());
806}
807
Yifan Hong3212f172018-06-28 12:39:50 -0700808TEST_F(ListVintfTest, Lazy) {
809 optind = 1; // mimic Lshal::parseArg()
810 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-iStr", "--types=z", "--neat"})));
811 EXPECT_THAT(out.str(), HasSubstr("a.h.bar1@1.0::IBar/1 declared hwbinder ?"));
812 EXPECT_THAT(out.str(), HasSubstr("a.h.bar2@2.0::IBar/2 declared passthrough 32+64"));
813 EXPECT_EQ("", err.str());
814}
815
816TEST_F(ListVintfTest, NoLazy) {
817 optind = 1; // mimic Lshal::parseArg()
818 EXPECT_EQ(0u, mockList->main(createArg({"lshal", "-iStr", "--types=b,c,l", "--neat"})));
819 EXPECT_THAT(out.str(), Not(HasSubstr("IBar")));
820 EXPECT_EQ("", err.str());
821}
822
Yifan Honga8bedc62017-09-08 18:00:31 -0700823class HelpTest : public ::testing::Test {
824public:
825 void SetUp() override {
826 lshal = std::make_unique<Lshal>(out, err, new MockServiceManager() /* serviceManager */,
827 new MockServiceManager() /* passthruManager */);
828 }
829
830 std::stringstream err;
831 std::stringstream out;
832 std::unique_ptr<Lshal> lshal;
833};
834
835TEST_F(HelpTest, GlobalUsage) {
836 (void)callMain(lshal, {"lshal", "--help"}); // ignore return
837 std::string errStr = err.str();
838 EXPECT_THAT(errStr, ContainsRegex("(^|\n)commands:($|\n)"))
839 << "`lshal --help` does not contain global usage";
840 EXPECT_THAT(errStr, ContainsRegex("(^|\n)list:($|\n)"))
841 << "`lshal --help` does not contain usage for 'list' command";
842 EXPECT_THAT(errStr, ContainsRegex("(^|\n)debug:($|\n)"))
843 << "`lshal --help` does not contain usage for 'debug' command";
844 EXPECT_THAT(errStr, ContainsRegex("(^|\n)help:($|\n)"))
845 << "`lshal --help` does not contain usage for 'help' command";
846
847 err.str("");
848 (void)callMain(lshal, {"lshal", "help"}); // ignore return
849 EXPECT_EQ(errStr, err.str()) << "`lshal help` should have the same output as `lshal --help`";
850
851 err.str("");
852 EXPECT_NE(0u, callMain(lshal, {"lshal", "--unknown-option"}));
853 EXPECT_THAT(err.str(), ContainsRegex("unrecognized option"));
854 EXPECT_THAT(err.str(), EndsWith(errStr))
855 << "`lshal --unknown-option` should have the same output as `lshal --help`";
856 EXPECT_EQ("", out.str());
857}
858
859TEST_F(HelpTest, UnknownOptionList1) {
860 (void)callMain(lshal, {"lshal", "help", "list"});
861 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)"))
862 << "`lshal help list` does not contain usage for 'list' command";
863}
864
865TEST_F(HelpTest, UnknownOptionList2) {
866 EXPECT_NE(0u, callMain(lshal, {"lshal", "list", "--unknown-option"}));
867 EXPECT_THAT(err.str(), ContainsRegex("unrecognized option"));
868 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)list:($|\n)"))
869 << "`lshal list --unknown-option` does not contain usage for 'list' command";
870 EXPECT_EQ("", out.str());
871}
872
873TEST_F(HelpTest, UnknownOptionHelp1) {
874 (void)callMain(lshal, {"lshal", "help", "help"});
875 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)"))
876 << "`lshal help help` does not contain usage for 'help' command";
877}
878
879TEST_F(HelpTest, UnknownOptionHelp2) {
880 (void)callMain(lshal, {"lshal", "help", "--unknown-option"});
881 EXPECT_THAT(err.str(), ContainsRegex("(^|\n)help:($|\n)"))
882 << "`lshal help --unknown-option` does not contain usage for 'help' command";
883 EXPECT_EQ("", out.str());
884}
885
Yifan Hong9881df92017-05-10 14:33:05 -0700886} // namespace lshal
887} // namespace android
888
889int main(int argc, char **argv) {
890 ::testing::InitGoogleMock(&argc, argv);
891 return RUN_ALL_TESTS();
892}