blob: b13f59d34452dea120fe5c1f82803616a84a008b [file] [log] [blame]
Felipe Leme343175a2016-08-02 18:57:37 -07001/*
2 * Copyright (C) 2016 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#include "../dumpsys.h"
18
19#include <vector>
20
21#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23
24#include <android-base/file.h>
Vishnu Nair6a408532017-10-24 09:11:27 -070025#include <serviceutils/PriorityDumper.h>
Felipe Leme343175a2016-08-02 18:57:37 -070026#include <utils/String16.h>
Steven Morelanda0f7f2d2017-03-09 22:59:32 -080027#include <utils/String8.h>
Felipe Leme343175a2016-08-02 18:57:37 -070028#include <utils/Vector.h>
29
30using namespace android;
31
32using ::testing::_;
33using ::testing::Action;
34using ::testing::ActionInterface;
35using ::testing::DoAll;
36using ::testing::Eq;
37using ::testing::HasSubstr;
38using ::testing::MakeAction;
Felipe Leme5c8a98f2017-08-25 13:39:04 -070039using ::testing::Mock;
Felipe Leme343175a2016-08-02 18:57:37 -070040using ::testing::Not;
41using ::testing::Return;
42using ::testing::StrEq;
43using ::testing::Test;
44using ::testing::WithArg;
45using ::testing::internal::CaptureStderr;
46using ::testing::internal::CaptureStdout;
47using ::testing::internal::GetCapturedStderr;
48using ::testing::internal::GetCapturedStdout;
49
50class ServiceManagerMock : public IServiceManager {
51 public:
52 MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&));
53 MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&));
Vishnu Nairf56042d2017-09-19 15:25:10 -070054 MOCK_METHOD4(addService, status_t(const String16&, const sp<IBinder>&, bool, int));
55 MOCK_METHOD1(listServices, Vector<String16>(int));
Felipe Leme343175a2016-08-02 18:57:37 -070056
57 protected:
58 MOCK_METHOD0(onAsBinder, IBinder*());
59};
60
61class BinderMock : public BBinder {
62 public:
63 BinderMock() {
64 }
65
66 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
67};
68
69// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
70typedef void WriteOnFdFunction(int);
71
72class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
73 public:
74 explicit WriteOnFdAction(const std::string& output) : output_(output) {
75 }
76 virtual Result Perform(const ArgumentTuple& args) {
77 int fd = ::std::tr1::get<0>(args);
78 android::base::WriteStringToFd(output_, fd);
79 }
80
81 private:
82 std::string output_;
83};
84
85// Matcher used to emulate dump() by writing on its file descriptor.
86Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
87 return MakeAction(new WriteOnFdAction(output));
88}
89
90// Matcher for args using Android's Vector<String16> format
91// TODO: move it to some common testing library
92MATCHER_P(AndroidElementsAre, expected, "") {
93 std::ostringstream errors;
94 if (arg.size() != expected.size()) {
95 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
96 << ")\n";
97 }
98 int i = 0;
99 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700100 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800101 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700102 std::string expected_str = expected[i];
103 actual_stream << "'" << actual_str << "' ";
104 expected_stream << "'" << expected_str << "' ";
105 if (actual_str != expected_str) {
106 errors << " element mismatch at index " << i << "\n";
107 }
108 i++;
109 }
110
111 if (!errors.str().empty()) {
112 errors << "\nExpected args: " << expected_stream.str()
113 << "\nActual args: " << actual_stream.str();
114 *result_listener << errors.str();
115 return false;
116 }
117 return true;
118}
119
120// Custom action to sleep for timeout seconds
121ACTION_P(Sleep, timeout) {
122 sleep(timeout);
123}
124
125class DumpsysTest : public Test {
126 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000127 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700128 }
129
130 void ExpectListServices(std::vector<std::string> services) {
131 Vector<String16> services16;
132 for (auto& service : services) {
133 services16.add(String16(service.c_str()));
134 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700135 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
Vishnu Nairf56042d2017-09-19 15:25:10 -0700136 .WillRepeatedly(Return(services16));
137 }
138
Vishnu Nair6a408532017-10-24 09:11:27 -0700139 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700140 Vector<String16> services16;
141 for (auto& service : services) {
142 services16.add(String16(service.c_str()));
143 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700144 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700145 }
146
147 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
148 sp<BinderMock> binder_mock;
149 if (running) {
150 binder_mock = new BinderMock;
151 }
152 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
153 return binder_mock;
154 }
155
156 void ExpectDump(const char* name, const std::string& output) {
157 sp<BinderMock> binder_mock = ExpectCheckService(name);
158 EXPECT_CALL(*binder_mock, dump(_, _))
159 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
160 }
161
162 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
163 const std::string& output) {
164 sp<BinderMock> binder_mock = ExpectCheckService(name);
165 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
166 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
167 }
168
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700169 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700170 sp<BinderMock> binder_mock = ExpectCheckService(name);
171 EXPECT_CALL(*binder_mock, dump(_, _))
172 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700173 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700174 }
175
176 void CallMain(const std::vector<std::string>& args) {
177 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
178 int argc = (int)args.size() + 1;
179 int i = 1;
180 for (const std::string& arg : args) {
181 argv[i++] = arg.c_str();
182 }
183 CaptureStdout();
184 CaptureStderr();
185 int status = dump_.main(argc, const_cast<char**>(argv));
186 stdout_ = GetCapturedStdout();
187 stderr_ = GetCapturedStderr();
188 EXPECT_THAT(status, Eq(0));
189 }
190
Vishnu Naire4f61742017-12-21 08:30:28 -0800191 void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags,
192 bool supportsProto, std::chrono::duration<double>& elapsedDuration,
193 size_t& bytesWritten) {
194 CaptureStdout();
195 CaptureStderr();
196 dump_.setServiceArgs(args, supportsProto, priorityFlags);
197 status_t status = dump_.startDumpThread(serviceName, args);
198 EXPECT_THAT(status, Eq(0));
199 status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false,
200 elapsedDuration, bytesWritten);
201 EXPECT_THAT(status, Eq(0));
202 dump_.stopDumpThread(/* dumpCompleted = */ true);
203 stdout_ = GetCapturedStdout();
204 stderr_ = GetCapturedStderr();
205 }
206
Steven Moreland2c3cd832017-02-13 23:44:17 +0000207 void AssertRunningServices(const std::vector<std::string>& services) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700208 std::string expected;
209 if (services.size() > 1) {
210 expected.append("Currently running services:\n");
211 }
Felipe Leme343175a2016-08-02 18:57:37 -0700212 for (const std::string& service : services) {
213 expected.append(" ").append(service).append("\n");
214 }
215 EXPECT_THAT(stdout_, HasSubstr(expected));
216 }
217
218 void AssertOutput(const std::string& expected) {
219 EXPECT_THAT(stdout_, StrEq(expected));
220 }
221
222 void AssertOutputContains(const std::string& expected) {
223 EXPECT_THAT(stdout_, HasSubstr(expected));
224 }
225
226 void AssertDumped(const std::string& service, const std::string& dump) {
227 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800228 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Felipe Leme343175a2016-08-02 18:57:37 -0700229 }
230
Vishnu Nair6a408532017-10-24 09:11:27 -0700231 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
232 const char16_t* priorityType) {
233 std::string priority = String8(priorityType).c_str();
234 EXPECT_THAT(stdout_,
235 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800236 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Vishnu Nair6a408532017-10-24 09:11:27 -0700237 }
238
Felipe Leme343175a2016-08-02 18:57:37 -0700239 void AssertNotDumped(const std::string& dump) {
240 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
241 }
242
243 void AssertStopped(const std::string& service) {
244 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
245 }
246
247 ServiceManagerMock sm_;
248 Dumpsys dump_;
249
250 private:
251 std::string stdout_, stderr_;
252};
253
Felipe Leme343175a2016-08-02 18:57:37 -0700254// Tests 'dumpsys -l' when all services are running
255TEST_F(DumpsysTest, ListAllServices) {
256 ExpectListServices({"Locksmith", "Valet"});
257 ExpectCheckService("Locksmith");
258 ExpectCheckService("Valet");
259
260 CallMain({"-l"});
261
262 AssertRunningServices({"Locksmith", "Valet"});
263}
264
265// Tests 'dumpsys -l' when a service is not running
266TEST_F(DumpsysTest, ListRunningServices) {
267 ExpectListServices({"Locksmith", "Valet"});
268 ExpectCheckService("Locksmith");
269 ExpectCheckService("Valet", false);
270
271 CallMain({"-l"});
272
273 AssertRunningServices({"Locksmith"});
274 AssertNotDumped({"Valet"});
275}
276
Vishnu Nairf56042d2017-09-19 15:25:10 -0700277// Tests 'dumpsys -l --priority HIGH'
278TEST_F(DumpsysTest, ListAllServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700279 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700280 ExpectCheckService("Locksmith");
281 ExpectCheckService("Valet");
282
283 CallMain({"-l", "--priority", "HIGH"});
284
285 AssertRunningServices({"Locksmith", "Valet"});
286}
287
288// Tests 'dumpsys -l --priority HIGH' with and empty list
289TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700290 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700291
292 CallMain({"-l", "--priority", "HIGH"});
293
294 AssertRunningServices({});
295}
296
Vishnu Nair6a408532017-10-24 09:11:27 -0700297// Tests 'dumpsys -l --proto'
298TEST_F(DumpsysTest, ListAllServicesWithProto) {
299 ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
300 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
301 ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
302 ExpectCheckService("Car");
303 ExpectCheckService("Valet");
304
305 CallMain({"-l", "--proto"});
306
307 AssertRunningServices({"Car", "Valet"});
308}
309
Felipe Leme343175a2016-08-02 18:57:37 -0700310// Tests 'dumpsys service_name' on a service is running
311TEST_F(DumpsysTest, DumpRunningService) {
312 ExpectDump("Valet", "Here's your car");
313
314 CallMain({"Valet"});
315
316 AssertOutput("Here's your car");
317}
318
319// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800320TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700321 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700322
323 CallMain({"-t", "1", "Valet"});
324
Vishnu Nair6921f802017-11-22 09:17:23 -0800325 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
326 AssertNotDumped("Here's your car");
327
328 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
329 Mock::AllowLeak(binder_mock.get());
330}
331
332// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
333TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
334 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
335
336 CallMain({"-T", "500", "Valet"});
337
338 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700339 AssertNotDumped("Here's your car");
340
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700341 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
342 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700343}
344
345// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
346TEST_F(DumpsysTest, DumpWithArgsRunningService) {
347 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
348
349 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
350
351 AssertOutput("I DO!");
352}
353
354// Tests 'dumpsys' with no arguments
355TEST_F(DumpsysTest, DumpMultipleServices) {
356 ExpectListServices({"running1", "stopped2", "running3"});
357 ExpectDump("running1", "dump1");
358 ExpectCheckService("stopped2", false);
359 ExpectDump("running3", "dump3");
360
361 CallMain({});
362
363 AssertRunningServices({"running1", "running3"});
364 AssertDumped("running1", "dump1");
365 AssertStopped("stopped2");
366 AssertDumped("running3", "dump3");
367}
368
369// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
370TEST_F(DumpsysTest, DumpWithSkip) {
371 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
372 ExpectDump("running1", "dump1");
373 ExpectCheckService("stopped2", false);
374 ExpectDump("skipped3", "dump3");
375 ExpectDump("running4", "dump4");
376 ExpectDump("skipped5", "dump5");
377
378 CallMain({"--skip", "skipped3", "skipped5"});
379
380 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
381 AssertDumped("running1", "dump1");
382 AssertDumped("running4", "dump4");
383 AssertStopped("stopped2");
384 AssertNotDumped("dump3");
385 AssertNotDumped("dump5");
386}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700387
388// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
389TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
390 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700391 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700392 ExpectDump("running1", "dump1");
393 ExpectCheckService("stopped2", false);
394 ExpectDump("skipped3", "dump3");
395 ExpectDump("running4", "dump4");
396 ExpectDump("skipped5", "dump5");
397
398 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
399
400 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700401 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
402 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700403 AssertStopped("stopped2");
404 AssertNotDumped("dump3");
405 AssertNotDumped("dump5");
406}
407
408// Tests 'dumpsys --priority CRITICAL'
409TEST_F(DumpsysTest, DumpWithPriorityCritical) {
410 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700411 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700412 ExpectDump("runningcritical1", "dump1");
413 ExpectDump("runningcritical2", "dump2");
414
415 CallMain({"--priority", "CRITICAL"});
416
417 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700418 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
419 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700420}
421
422// Tests 'dumpsys --priority HIGH'
423TEST_F(DumpsysTest, DumpWithPriorityHigh) {
424 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700425 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700426 ExpectDump("runninghigh1", "dump1");
427 ExpectDump("runninghigh2", "dump2");
428
429 CallMain({"--priority", "HIGH"});
430
431 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700432 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
433 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700434}
435
436// Tests 'dumpsys --priority NORMAL'
437TEST_F(DumpsysTest, DumpWithPriorityNormal) {
438 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700439 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700440 ExpectDump("runningnormal1", "dump1");
441 ExpectDump("runningnormal2", "dump2");
442
443 CallMain({"--priority", "NORMAL"});
444
445 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Naire4f61742017-12-21 08:30:28 -0800446 AssertDumped("runningnormal1", "dump1");
447 AssertDumped("runningnormal2", "dump2");
Vishnu Nair6a408532017-10-24 09:11:27 -0700448}
449
450// Tests 'dumpsys --proto'
451TEST_F(DumpsysTest, DumpWithProto) {
452 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
453 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
454 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
455 IServiceManager::DUMP_FLAG_PROTO);
456 ExpectDump("run2", "dump1");
457 ExpectDump("run8", "dump2");
458
459 CallMain({"--proto"});
460
461 AssertRunningServices({"run2", "run8"});
462 AssertDumped("run2", "dump1");
463 AssertDumped("run8", "dump2");
464}
465
466// Tests 'dumpsys --priority HIGH --proto'
467TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
468 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
469 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
470 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
471 IServiceManager::DUMP_FLAG_PROTO);
472
473 ExpectDump("runninghigh1", "dump1");
474 ExpectDump("runninghigh2", "dump2");
475
476 CallMain({"--priority", "HIGH", "--proto"});
477
478 AssertRunningServices({"runninghigh1", "runninghigh2"});
479 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
480 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700481}
Vishnu Naire4f61742017-12-21 08:30:28 -0800482
483TEST_F(DumpsysTest, GetBytesWritten) {
484 const char* serviceName = "service2";
485 const char* dumpContents = "dump1";
486 ExpectDump(serviceName, dumpContents);
487
488 String16 service(serviceName);
489 Vector<String16> args;
490 std::chrono::duration<double> elapsedDuration;
491 size_t bytesWritten;
492
493 CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL,
494 /* as_proto = */ false, elapsedDuration, bytesWritten);
495
496 AssertOutput(dumpContents);
497 EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents)));
498}
499
500TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
501 std::chrono::duration<double> elapsedDuration;
502 size_t bytesWritten;
503 status_t status =
504 dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
505 /* as_proto = */ false, elapsedDuration, bytesWritten);
506 EXPECT_THAT(status, Eq(INVALID_OPERATION));
507}