blob: 98ee1b0e01d15b35a765df05877c1ba05f3d4ca8 [file] [log] [blame]
Felipe Leme4c2d6632016-09-28 14:32:00 -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
Felipe Leme75876a22016-10-27 16:31:27 -070017#define LOG_TAG "dumpstate"
18#include <cutils/log.h>
19
Felipe Lemef0292972016-11-22 13:57:05 -080020#include "DumpstateInternal.h"
Felipe Leme75876a22016-10-27 16:31:27 -070021#include "DumpstateService.h"
22#include "android/os/BnDumpstate.h"
Felipe Leme4c2d6632016-09-28 14:32:00 -070023#include "dumpstate.h"
24
25#include <gmock/gmock.h>
26#include <gtest/gtest.h>
27
Felipe Leme46b85da2016-11-21 17:40:45 -080028#include <fcntl.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070029#include <libgen.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070030#include <signal.h>
31#include <sys/types.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070032#include <unistd.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070033#include <thread>
Felipe Leme4c2d6632016-09-28 14:32:00 -070034
35#include <android-base/file.h>
Felipe Lemed80e6b62016-10-03 13:08:14 -070036#include <android-base/properties.h>
37#include <android-base/stringprintf.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070038#include <android-base/strings.h>
Abhijeet Kaur904e0e02018-12-05 14:03:01 +000039#include <cutils/properties.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070040
Felipe Leme47e9be22016-12-21 15:37:07 -080041namespace android {
42namespace os {
43namespace dumpstate {
Felipe Lemed80e6b62016-10-03 13:08:14 -070044
Felipe Leme4c2d6632016-09-28 14:32:00 -070045using ::testing::EndsWith;
Felipe Leme46b85da2016-11-21 17:40:45 -080046using ::testing::HasSubstr;
Felipe Leme009ecbb2016-11-07 10:18:44 -080047using ::testing::IsNull;
Felipe Leme4c2d6632016-09-28 14:32:00 -070048using ::testing::IsEmpty;
Felipe Leme009ecbb2016-11-07 10:18:44 -080049using ::testing::NotNull;
Felipe Leme4c2d6632016-09-28 14:32:00 -070050using ::testing::StrEq;
51using ::testing::StartsWith;
52using ::testing::Test;
53using ::testing::internal::CaptureStderr;
54using ::testing::internal::CaptureStdout;
55using ::testing::internal::GetCapturedStderr;
56using ::testing::internal::GetCapturedStdout;
57
Nandana Dutt3f8c7172018-09-25 12:01:54 +010058#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
59
Felipe Leme75876a22016-10-27 16:31:27 -070060class DumpstateListenerMock : public IDumpstateListener {
61 public:
Nandana Dutta6a28bd2019-01-14 16:54:38 +000062 MOCK_METHOD1(onProgress, binder::Status(int32_t progress));
63 MOCK_METHOD1(onError, binder::Status(int32_t error_code));
64 MOCK_METHOD3(onFinished, binder::Status(int64_t duration_ms, const ::std::string& title,
65 const ::std::string& description));
Felipe Leme75876a22016-10-27 16:31:27 -070066 MOCK_METHOD1(onProgressUpdated, binder::Status(int32_t progress));
67 MOCK_METHOD1(onMaxProgressUpdated, binder::Status(int32_t max_progress));
Vishnu Nair20cf5032018-01-05 13:15:49 -080068 MOCK_METHOD4(onSectionComplete, binder::Status(const ::std::string& name, int32_t status,
69 int32_t size, int32_t durationMs));
Felipe Leme75876a22016-10-27 16:31:27 -070070
71 protected:
72 MOCK_METHOD0(onAsBinder, IBinder*());
73};
74
Felipe Leme46b85da2016-11-21 17:40:45 -080075static int calls_;
76
Felipe Leme7447d7c2016-11-03 18:12:22 -070077// Base class for all tests in this file
78class DumpstateBaseTest : public Test {
Felipe Leme46b85da2016-11-21 17:40:45 -080079 public:
80 virtual void SetUp() override {
81 calls_++;
Felipe Lemef0292972016-11-22 13:57:05 -080082 SetDryRun(false);
Felipe Leme46b85da2016-11-21 17:40:45 -080083 }
84
Felipe Lemef0292972016-11-22 13:57:05 -080085 void SetDryRun(bool dry_run) const {
86 PropertiesHelper::dry_run_ = dry_run;
87 }
88
89 void SetBuildType(const std::string& build_type) const {
90 PropertiesHelper::build_type_ = build_type;
91 }
92
Nandana Dutt4b392be2018-11-02 16:17:05 +000093 void SetUnroot(bool unroot) const {
94 PropertiesHelper::unroot_ = unroot;
95 }
96
Felipe Lemef0292972016-11-22 13:57:05 -080097 bool IsStandalone() const {
Felipe Leme46b85da2016-11-21 17:40:45 -080098 return calls_ == 1;
99 }
100
Felipe Lemef0292972016-11-22 13:57:05 -0800101 void DropRoot() const {
102 DropRootUser();
Felipe Leme46b85da2016-11-21 17:40:45 -0800103 uid_t uid = getuid();
104 ASSERT_EQ(2000, (int)uid);
105 }
106
Felipe Leme7447d7c2016-11-03 18:12:22 -0700107 protected:
108 const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str());
109 const std::string kFixturesPath = kTestPath + "/../dumpstate_test_fixture/";
Felipe Leme7fb8dee2017-08-25 10:15:01 -0700110 const std::string kTestDataPath = kFixturesPath + "tests/testdata/";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700111 const std::string kSimpleCommand = kFixturesPath + "dumpstate_test_fixture";
112 const std::string kEchoCommand = "/system/bin/echo";
113
114 /*
115 * Copies a text file fixture to a temporary file, returning it's path.
116 *
117 * Useful in cases where the test case changes the content of the tile.
118 */
119 std::string CopyTextFileFixture(const std::string& relative_name) {
120 std::string from = kTestDataPath + relative_name;
121 // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it
122 // around for poking when the test fails.
123 std::string to = kTestDataPath + relative_name + ".tmp";
124 ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str());
125 android::base::RemoveFileIfExists(to);
126 CopyTextFile(from, to);
127 return to.c_str();
128 }
129
Felipe Leme46b85da2016-11-21 17:40:45 -0800130 // Need functions that returns void to use assertions -
Felipe Leme7447d7c2016-11-03 18:12:22 -0700131 // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement
Felipe Leme46b85da2016-11-21 17:40:45 -0800132 void ReadFileToString(const std::string& path, std::string* content) {
133 ASSERT_TRUE(android::base::ReadFileToString(path, content))
134 << "could not read contents from " << path;
135 }
136 void WriteStringToFile(const std::string& content, const std::string& path) {
137 ASSERT_TRUE(android::base::WriteStringToFile(content, path))
138 << "could not write contents to " << path;
139 }
140
141 private:
Felipe Leme7447d7c2016-11-03 18:12:22 -0700142 void CopyTextFile(const std::string& from, const std::string& to) {
143 std::string content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800144 ReadFileToString(from, &content);
145 WriteStringToFile(content, to);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700146 }
147};
148
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100149class DumpOptionsTest : public Test {
150 public:
151 virtual ~DumpOptionsTest() {
152 }
153 virtual void SetUp() {
154 options_ = Dumpstate::DumpOptions();
155 }
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000156 void TearDown() {
157 // Reset the property
158 property_set("dumpstate.options", "");
159 }
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100160 Dumpstate::DumpOptions options_;
161};
162
163TEST_F(DumpOptionsTest, InitializeNone) {
164 // clang-format off
165 char* argv[] = {
166 const_cast<char*>("dumpstate")
167 };
168 // clang-format on
169
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100170 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
171
172 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000173
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100174 EXPECT_FALSE(options_.do_add_date);
175 EXPECT_FALSE(options_.do_zip_file);
176 EXPECT_EQ("", options_.use_outfile);
177 EXPECT_FALSE(options_.use_socket);
178 EXPECT_FALSE(options_.use_control_socket);
179 EXPECT_FALSE(options_.show_header_only);
180 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000181 EXPECT_FALSE(options_.do_fb);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100182 EXPECT_FALSE(options_.do_progress_updates);
183 EXPECT_FALSE(options_.is_remote_mode);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000184 EXPECT_FALSE(options_.do_broadcast);
185}
186
187TEST_F(DumpOptionsTest, InitializeAdbBugreport) {
188 // clang-format off
189 char* argv[] = {
190 const_cast<char*>("dumpstatez"),
191 const_cast<char*>("-S"),
192 const_cast<char*>("-d"),
193 const_cast<char*>("-z"),
194 const_cast<char*>("-o abc"),
195 };
196 // clang-format on
197
198 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
199
200 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
201 EXPECT_TRUE(options_.do_add_date);
202 EXPECT_TRUE(options_.do_zip_file);
203 EXPECT_TRUE(options_.use_control_socket);
204 EXPECT_EQ(" abc", std::string(options_.use_outfile));
205
206 // Other options retain default values
207 EXPECT_TRUE(options_.do_vibrate);
208 EXPECT_FALSE(options_.show_header_only);
209 EXPECT_FALSE(options_.do_fb);
210 EXPECT_FALSE(options_.do_progress_updates);
211 EXPECT_FALSE(options_.is_remote_mode);
212 EXPECT_FALSE(options_.do_broadcast);
213 EXPECT_FALSE(options_.use_socket);
214}
215
216TEST_F(DumpOptionsTest, InitializeAdbShellBugreport) {
217 // clang-format off
218 char* argv[] = {
219 const_cast<char*>("dumpstate"),
220 const_cast<char*>("-s"),
221 };
222 // clang-format on
223
224 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
225
226 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
227 EXPECT_TRUE(options_.use_socket);
228
229 // Other options retain default values
230 EXPECT_TRUE(options_.do_vibrate);
231 EXPECT_EQ("", options_.use_outfile);
232 EXPECT_FALSE(options_.do_add_date);
233 EXPECT_FALSE(options_.do_zip_file);
234 EXPECT_FALSE(options_.use_control_socket);
235 EXPECT_FALSE(options_.show_header_only);
236 EXPECT_FALSE(options_.do_fb);
237 EXPECT_FALSE(options_.do_progress_updates);
238 EXPECT_FALSE(options_.is_remote_mode);
239 EXPECT_FALSE(options_.do_broadcast);
240}
241
242TEST_F(DumpOptionsTest, InitializeFullBugReport) {
243 // clang-format off
244 char* argv[] = {
245 const_cast<char*>("bugreport"),
246 const_cast<char*>("-d"),
247 const_cast<char*>("-p"),
248 const_cast<char*>("-B"),
249 const_cast<char*>("-z"),
250 const_cast<char*>("-o abc"),
251 };
252 // clang-format on
253 property_set("dumpstate.options", "bugreportfull");
254
255 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
256
257 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
258 EXPECT_TRUE(options_.do_add_date);
259 EXPECT_TRUE(options_.do_fb);
260 EXPECT_TRUE(options_.do_zip_file);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000261 EXPECT_TRUE(options_.do_broadcast);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000262 EXPECT_EQ(" abc", std::string(options_.use_outfile));
263
264 // Other options retain default values
265 EXPECT_TRUE(options_.do_vibrate);
266 EXPECT_FALSE(options_.use_control_socket);
267 EXPECT_FALSE(options_.show_header_only);
268 EXPECT_FALSE(options_.do_progress_updates);
269 EXPECT_FALSE(options_.is_remote_mode);
270 EXPECT_FALSE(options_.use_socket);
271 EXPECT_FALSE(options_.do_start_service);
272}
273
274TEST_F(DumpOptionsTest, InitializeInteractiveBugReport) {
275 // clang-format off
276 char* argv[] = {
277 const_cast<char*>("bugreport"),
278 const_cast<char*>("-d"),
279 const_cast<char*>("-p"),
280 const_cast<char*>("-B"),
281 const_cast<char*>("-z"),
282 const_cast<char*>("-o abc"),
283 };
284 // clang-format on
285
286 property_set("dumpstate.options", "bugreportplus");
287
288 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
289
290 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
291 EXPECT_TRUE(options_.do_add_date);
292 EXPECT_TRUE(options_.do_broadcast);
293 EXPECT_TRUE(options_.do_zip_file);
294 EXPECT_TRUE(options_.do_progress_updates);
295 EXPECT_TRUE(options_.do_start_service);
296 EXPECT_FALSE(options_.do_fb);
297 EXPECT_EQ(" abc", std::string(options_.use_outfile));
298
299 // Other options retain default values
300 EXPECT_TRUE(options_.do_vibrate);
301 EXPECT_FALSE(options_.use_control_socket);
302 EXPECT_FALSE(options_.show_header_only);
303 EXPECT_FALSE(options_.is_remote_mode);
304 EXPECT_FALSE(options_.use_socket);
305}
306
307TEST_F(DumpOptionsTest, InitializeRemoteBugReport) {
308 // clang-format off
309 char* argv[] = {
310 const_cast<char*>("bugreport"),
311 const_cast<char*>("-d"),
312 const_cast<char*>("-p"),
313 const_cast<char*>("-B"),
314 const_cast<char*>("-z"),
315 const_cast<char*>("-o abc"),
316 };
317 // clang-format on
318
319 property_set("dumpstate.options", "bugreportremote");
320
321 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
322
323 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
324 EXPECT_TRUE(options_.do_add_date);
325 EXPECT_TRUE(options_.do_broadcast);
326 EXPECT_TRUE(options_.do_zip_file);
327 EXPECT_TRUE(options_.is_remote_mode);
328 EXPECT_FALSE(options_.do_vibrate);
329 EXPECT_FALSE(options_.do_fb);
330 EXPECT_EQ(" abc", std::string(options_.use_outfile));
331
332 // Other options retain default values
333 EXPECT_FALSE(options_.use_control_socket);
334 EXPECT_FALSE(options_.show_header_only);
335 EXPECT_FALSE(options_.do_progress_updates);
336 EXPECT_FALSE(options_.use_socket);
337}
338
339TEST_F(DumpOptionsTest, InitializeWearBugReport) {
340 // clang-format off
341 char* argv[] = {
342 const_cast<char*>("bugreport"),
343 const_cast<char*>("-d"),
344 const_cast<char*>("-p"),
345 const_cast<char*>("-B"),
346 const_cast<char*>("-z"),
347 const_cast<char*>("-o abc"),
348 };
349 // clang-format on
350
351 property_set("dumpstate.options", "bugreportwear");
352
353 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
354
355 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
356 EXPECT_TRUE(options_.do_add_date);
357 EXPECT_TRUE(options_.do_fb);
358 EXPECT_TRUE(options_.do_broadcast);
359 EXPECT_TRUE(options_.do_zip_file);
360 EXPECT_TRUE(options_.do_progress_updates);
361 EXPECT_TRUE(options_.do_start_service);
362 EXPECT_EQ(" abc", std::string(options_.use_outfile));
363
364 // Other options retain default values
365 EXPECT_TRUE(options_.do_vibrate);
366 EXPECT_FALSE(options_.use_control_socket);
367 EXPECT_FALSE(options_.show_header_only);
368 EXPECT_FALSE(options_.is_remote_mode);
369 EXPECT_FALSE(options_.use_socket);
370}
371
372TEST_F(DumpOptionsTest, InitializeTelephonyBugReport) {
373 // clang-format off
374 char* argv[] = {
375 const_cast<char*>("bugreport"),
376 const_cast<char*>("-d"),
377 const_cast<char*>("-p"),
378 const_cast<char*>("-B"),
379 const_cast<char*>("-z"),
380 const_cast<char*>("-o abc"),
381 };
382 // clang-format on
383
384 property_set("dumpstate.options", "bugreporttelephony");
385
386 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
387
388 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
389 EXPECT_TRUE(options_.do_add_date);
390 EXPECT_TRUE(options_.do_fb);
391 EXPECT_TRUE(options_.do_broadcast);
392 EXPECT_TRUE(options_.do_zip_file);
393 EXPECT_TRUE(options_.telephony_only);
394 EXPECT_EQ(" abc", std::string(options_.use_outfile));
395
396 // Other options retain default values
397 EXPECT_TRUE(options_.do_vibrate);
398 EXPECT_FALSE(options_.use_control_socket);
399 EXPECT_FALSE(options_.show_header_only);
400 EXPECT_FALSE(options_.do_progress_updates);
401 EXPECT_FALSE(options_.is_remote_mode);
402 EXPECT_FALSE(options_.use_socket);
403}
404
405TEST_F(DumpOptionsTest, InitializeWifiBugReport) {
406 // clang-format off
407 char* argv[] = {
408 const_cast<char*>("bugreport"),
409 const_cast<char*>("-d"),
410 const_cast<char*>("-p"),
411 const_cast<char*>("-B"),
412 const_cast<char*>("-z"),
413 const_cast<char*>("-o abc"),
414 };
415 // clang-format on
416
417 property_set("dumpstate.options", "bugreportwifi");
418
419 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
420
421 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
422 EXPECT_TRUE(options_.do_add_date);
423 EXPECT_TRUE(options_.do_fb);
424 EXPECT_TRUE(options_.do_broadcast);
425 EXPECT_TRUE(options_.do_zip_file);
426 EXPECT_TRUE(options_.wifi_only);
427 EXPECT_EQ(" abc", std::string(options_.use_outfile));
428
429 // Other options retain default values
430 EXPECT_TRUE(options_.do_vibrate);
431 EXPECT_FALSE(options_.use_control_socket);
432 EXPECT_FALSE(options_.show_header_only);
433 EXPECT_FALSE(options_.do_progress_updates);
434 EXPECT_FALSE(options_.is_remote_mode);
435 EXPECT_FALSE(options_.use_socket);
436}
437
438TEST_F(DumpOptionsTest, InitializeDefaultBugReport) {
439 // default: commandline options are not overridden
440 // clang-format off
441 char* argv[] = {
442 const_cast<char*>("bugreport"),
443 const_cast<char*>("-d"),
444 const_cast<char*>("-p"),
445 const_cast<char*>("-B"),
446 const_cast<char*>("-z"),
447 const_cast<char*>("-o abc"),
448 };
449 // clang-format on
450
451 property_set("dumpstate.options", "");
452
453 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
454
455 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
456 EXPECT_TRUE(options_.do_add_date);
457 EXPECT_TRUE(options_.do_fb);
458 EXPECT_TRUE(options_.do_zip_file);
459 EXPECT_TRUE(options_.do_broadcast);
460 EXPECT_EQ(" abc", std::string(options_.use_outfile));
461
462 // Other options retain default values
463 EXPECT_TRUE(options_.do_vibrate);
464 EXPECT_FALSE(options_.use_control_socket);
465 EXPECT_FALSE(options_.show_header_only);
466 EXPECT_FALSE(options_.do_progress_updates);
467 EXPECT_FALSE(options_.is_remote_mode);
468 EXPECT_FALSE(options_.use_socket);
469 EXPECT_FALSE(options_.wifi_only);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100470}
471
472TEST_F(DumpOptionsTest, InitializePartial1) {
473 // clang-format off
474 char* argv[] = {
475 const_cast<char*>("dumpstate"),
476 const_cast<char*>("-d"),
477 const_cast<char*>("-z"),
478 const_cast<char*>("-o abc"),
479 const_cast<char*>("-s"),
480 const_cast<char*>("-S"),
481
482 };
483 // clang-format on
484
485 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
486
487 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
488 EXPECT_TRUE(options_.do_add_date);
489 EXPECT_TRUE(options_.do_zip_file);
490 // TODO: Maybe we should trim the filename
491 EXPECT_EQ(" abc", std::string(options_.use_outfile));
492 EXPECT_TRUE(options_.use_socket);
493 EXPECT_TRUE(options_.use_control_socket);
494
495 // Other options retain default values
496 EXPECT_FALSE(options_.show_header_only);
497 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000498 EXPECT_FALSE(options_.do_fb);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100499 EXPECT_FALSE(options_.do_progress_updates);
500 EXPECT_FALSE(options_.is_remote_mode);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000501 EXPECT_FALSE(options_.do_broadcast);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100502}
503
504TEST_F(DumpOptionsTest, InitializePartial2) {
505 // clang-format off
506 char* argv[] = {
507 const_cast<char*>("dumpstate"),
508 const_cast<char*>("-v"),
509 const_cast<char*>("-q"),
510 const_cast<char*>("-p"),
511 const_cast<char*>("-P"),
512 const_cast<char*>("-R"),
513 const_cast<char*>("-B"),
514 };
515 // clang-format on
516
517 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
518
519 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
520 EXPECT_TRUE(options_.show_header_only);
521 EXPECT_FALSE(options_.do_vibrate);
522 EXPECT_TRUE(options_.do_fb);
523 EXPECT_TRUE(options_.do_progress_updates);
524 EXPECT_TRUE(options_.is_remote_mode);
525 EXPECT_TRUE(options_.do_broadcast);
526
527 // Other options retain default values
528 EXPECT_FALSE(options_.do_add_date);
529 EXPECT_FALSE(options_.do_zip_file);
530 EXPECT_EQ("", options_.use_outfile);
531 EXPECT_FALSE(options_.use_socket);
532 EXPECT_FALSE(options_.use_control_socket);
533}
534
535TEST_F(DumpOptionsTest, InitializeHelp) {
536 // clang-format off
537 char* argv[] = {
538 const_cast<char*>("dumpstate"),
539 const_cast<char*>("-h")
540 };
541 // clang-format on
542
543 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
544
545 // -h is for help.
546 EXPECT_EQ(status, Dumpstate::RunStatus::HELP);
547}
548
549TEST_F(DumpOptionsTest, InitializeUnknown) {
550 // clang-format off
551 char* argv[] = {
552 const_cast<char*>("dumpstate"),
553 const_cast<char*>("-u") // unknown flag
554 };
555 // clang-format on
556
557 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
558
559 // -u is unknown.
560 EXPECT_EQ(status, Dumpstate::RunStatus::INVALID_INPUT);
561}
562
563TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile1) {
564 options_.do_zip_file = true;
565 EXPECT_FALSE(options_.ValidateOptions());
566 options_.use_outfile = "a/b/c";
567 EXPECT_TRUE(options_.ValidateOptions());
568}
569
570TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile2) {
571 options_.do_broadcast = true;
572 EXPECT_FALSE(options_.ValidateOptions());
573 options_.use_outfile = "a/b/c";
574 EXPECT_TRUE(options_.ValidateOptions());
575}
576
577TEST_F(DumpOptionsTest, ValidateOptionsNeedZipfile) {
578 options_.use_control_socket = true;
579 EXPECT_FALSE(options_.ValidateOptions());
580
581 options_.do_zip_file = true;
582 options_.use_outfile = "a/b/c"; // do_zip_file needs outfile
583 EXPECT_TRUE(options_.ValidateOptions());
584}
585
586TEST_F(DumpOptionsTest, ValidateOptionsUpdateProgressNeedsBroadcast) {
587 options_.do_progress_updates = true;
588 options_.use_outfile = "a/b/c"; // do_progress_updates needs outfile
589 EXPECT_FALSE(options_.ValidateOptions());
590
591 options_.do_broadcast = true;
592 EXPECT_TRUE(options_.ValidateOptions());
593}
594
595TEST_F(DumpOptionsTest, ValidateOptionsRemoteMode) {
596 options_.is_remote_mode = true;
597 EXPECT_FALSE(options_.ValidateOptions());
598
599 options_.do_broadcast = true;
600 options_.do_zip_file = true;
601 options_.do_add_date = true;
602 options_.use_outfile = "a/b/c"; // do_broadcast needs outfile
603 EXPECT_TRUE(options_.ValidateOptions());
604}
605
Felipe Leme7447d7c2016-11-03 18:12:22 -0700606class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700607 public:
608 void SetUp() {
Felipe Leme46b85da2016-11-21 17:40:45 -0800609 DumpstateBaseTest::SetUp();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700610 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700611 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700612 ds.progress_.reset(new Progress());
Felipe Leme009ecbb2016-11-07 10:18:44 -0800613 ds.update_progress_threshold_ = 0;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100614 ds.options_.reset(new Dumpstate::DumpOptions());
Felipe Leme4c2d6632016-09-28 14:32:00 -0700615 }
616
617 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700618 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700619 const CommandOptions& options = CommandOptions::DEFAULT) {
620 CaptureStdout();
621 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700622 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700623 out = GetCapturedStdout();
624 err = GetCapturedStderr();
625 return status;
626 }
627
Felipe Lemecef02982016-10-03 17:22:22 -0700628 // Dumps a file and capture `stdout` and `stderr`.
629 int DumpFile(const std::string& title, const std::string& path) {
630 CaptureStdout();
631 CaptureStderr();
632 int status = ds.DumpFile(title, path);
633 out = GetCapturedStdout();
634 err = GetCapturedStderr();
635 return status;
636 }
637
Felipe Leme009ecbb2016-11-07 10:18:44 -0800638 void SetProgress(long progress, long initial_max, long threshold = 0) {
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100639 ds.options_->do_progress_updates = true;
Felipe Leme009ecbb2016-11-07 10:18:44 -0800640 ds.update_progress_threshold_ = threshold;
641 ds.last_updated_progress_ = 0;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700642 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
643 }
644
Felipe Leme7447d7c2016-11-03 18:12:22 -0700645 std::string GetProgressMessage(const std::string& listener_name, int progress, int max,
Felipe Leme009ecbb2016-11-07 10:18:44 -0800646 int old_max = 0, bool update_progress = true) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700647 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
648 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700649
Felipe Leme7447d7c2016-11-03 18:12:22 -0700650 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700651
Felipe Leme009ecbb2016-11-07 10:18:44 -0800652 std::string message = "";
Felipe Leme75876a22016-10-27 16:31:27 -0700653 if (max_increased) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800654 message =
Felipe Leme7447d7c2016-11-03 18:12:22 -0700655 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700656 }
657
Felipe Leme009ecbb2016-11-07 10:18:44 -0800658 if (update_progress) {
659 message += android::base::StringPrintf("Setting progress (%s): %d/%d\n",
660 listener_name.c_str(), progress, max);
661 }
662
663 return message;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700664 }
665
Felipe Leme4c2d6632016-09-28 14:32:00 -0700666 // `stdout` and `stderr` from the last command ran.
667 std::string out, err;
668
Felipe Lemefd8affa2016-09-30 17:38:57 -0700669 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700670};
671
672TEST_F(DumpstateTest, RunCommandNoArgs) {
673 EXPECT_EQ(-1, RunCommand("", {}));
674}
675
676TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700677 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700678 EXPECT_THAT(out, StrEq("stdout\n"));
679 EXPECT_THAT(err, StrEq("stderr\n"));
680}
681
682TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700683 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700684 EXPECT_THAT(err, StrEq("stderr\n"));
685 // We don't know the exact duration, so we check the prefix and suffix
Felipe Lemefd8affa2016-09-30 17:38:57 -0700686 EXPECT_THAT(out,
Felipe Leme7447d7c2016-11-03 18:12:22 -0700687 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n------"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700688 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
689}
690
Felipe Lemefd8affa2016-09-30 17:38:57 -0700691TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700692 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700693 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700694 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
695 EXPECT_THAT(out, StrEq("stdout\n"));
696 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
697}
698
699TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700700 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700701 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700702 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700703 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700704}
705
706TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700707 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700708 EXPECT_THAT(err, IsEmpty());
709 EXPECT_THAT(out, StrEq("one\n"));
710}
711
Felipe Lemefd8affa2016-09-30 17:38:57 -0700712TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700713 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700714 EXPECT_THAT(err, IsEmpty());
715 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
716}
717
718TEST_F(DumpstateTest, RunCommandDryRun) {
719 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700720 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700721 // We don't know the exact duration, so we check the prefix and suffix
Felipe Leme7447d7c2016-11-03 18:12:22 -0700722 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Felipe Leme4c2d6632016-09-28 14:32:00 -0700723 ") ------\n\t(skipped on dry run)\n------"));
724 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
725 EXPECT_THAT(err, IsEmpty());
726}
727
728TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
729 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700730 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700731 EXPECT_THAT(out, IsEmpty());
732 EXPECT_THAT(err, IsEmpty());
733}
734
735TEST_F(DumpstateTest, RunCommandDryRunAlways) {
736 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700737 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700738 EXPECT_THAT(out, StrEq("stdout\n"));
739 EXPECT_THAT(err, StrEq("stderr\n"));
740}
741
Felipe Lemefd8affa2016-09-30 17:38:57 -0700742TEST_F(DumpstateTest, RunCommandNotFound) {
743 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
744 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
745 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
746}
747
748TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700749 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
750 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700751 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700752 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700753 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700754}
755
756TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700757 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700758 // We don't know the exit code, so check just the prefix.
759 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700760 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700761 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700762 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700763}
764
765TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700766 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700767 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700768 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700769 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700770 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700771 " --sleep 2' timed out after 1"));
772}
773
774TEST_F(DumpstateTest, RunCommandIsKilled) {
775 CaptureStdout();
776 CaptureStderr();
777
778 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700779 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700780 CommandOptions::WithTimeout(100).Always().Build()));
781 });
782
783 // Capture pid and pre-sleep output.
784 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
785 std::string err = GetCapturedStderr();
786 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
787
788 std::string out = GetCapturedStdout();
789 std::vector<std::string> lines = android::base::Split(out, "\n");
790 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
791
792 int pid = atoi(lines[0].c_str());
793 EXPECT_THAT(lines[1], StrEq("stdout line1"));
794 EXPECT_THAT(lines[2], IsEmpty()); // \n
795
796 // Then kill the process.
797 CaptureStdout();
798 CaptureStderr();
799 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
800 t.join();
801
802 // Finally, check output after murder.
803 out = GetCapturedStdout();
804 err = GetCapturedStderr();
805
Felipe Leme7447d7c2016-11-03 18:12:22 -0700806 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700807 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700808 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700809 " --pid --sleep 20' failed: killed by signal 15\n"));
810}
811
Felipe Leme75876a22016-10-27 16:31:27 -0700812TEST_F(DumpstateTest, RunCommandProgress) {
813 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
814 ds.listener_ = listener;
815 ds.listener_name_ = "FoxMulder";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700816 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700817
818 EXPECT_CALL(*listener, onProgressUpdated(20));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700819 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700820 std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30);
821 EXPECT_THAT(out, StrEq("stdout\n"));
822 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
823
824 EXPECT_CALL(*listener, onProgressUpdated(30));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700825 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700826 progress_message = GetProgressMessage(ds.listener_name_, 30, 30);
827 EXPECT_THAT(out, StrEq("stdout\n"));
828 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
829
830 // Run a command that will increase maximum timeout.
831 EXPECT_CALL(*listener, onProgressUpdated(31));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700832 EXPECT_CALL(*listener, onMaxProgressUpdated(37));
833 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
834 progress_message = GetProgressMessage(ds.listener_name_, 31, 37, 30); // 20% increase
Felipe Leme75876a22016-10-27 16:31:27 -0700835 EXPECT_THAT(out, StrEq("stdout\n"));
836 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
837
838 // Make sure command ran while in dry_run is counted.
839 SetDryRun(true);
840 EXPECT_CALL(*listener, onProgressUpdated(35));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700841 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
842 progress_message = GetProgressMessage(ds.listener_name_, 35, 37);
Felipe Leme75876a22016-10-27 16:31:27 -0700843 EXPECT_THAT(out, IsEmpty());
844 EXPECT_THAT(err, StrEq(progress_message));
845
846 ds.listener_.clear();
847}
848
Felipe Leme009ecbb2016-11-07 10:18:44 -0800849TEST_F(DumpstateTest, RunCommandProgressIgnoreThreshold) {
850 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
851 ds.listener_ = listener;
852 ds.listener_name_ = "FoxMulder";
853 SetProgress(0, 8, 5); // 8 max, 5 threshold
854
855 // First update should always be sent.
856 EXPECT_CALL(*listener, onProgressUpdated(1));
857 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
858 std::string progress_message = GetProgressMessage(ds.listener_name_, 1, 8);
859 EXPECT_THAT(out, StrEq("stdout\n"));
860 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
861
862 // Fourth update should be ignored because it's between the threshold (5 -1 = 4 < 5).
863 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
864 EXPECT_THAT(out, StrEq("stdout\n"));
865 EXPECT_THAT(err, StrEq("stderr\n"));
866
867 // Third update should be sent because it reaches threshold (6 - 1 = 5).
868 EXPECT_CALL(*listener, onProgressUpdated(6));
869 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
870 progress_message = GetProgressMessage(ds.listener_name_, 6, 8);
871 EXPECT_THAT(out, StrEq("stdout\n"));
872 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
873
874 // Fourth update should be ignored because it's between the threshold (9 - 6 = 3 < 5).
875 // But max update should be sent.
876 EXPECT_CALL(*listener, onMaxProgressUpdated(10)); // 9 * 120% = 10.8 = 10
877 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build()));
878 progress_message = GetProgressMessage(ds.listener_name_, 9, 10, 8, false);
879 EXPECT_THAT(out, StrEq("stdout\n"));
880 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
881
882 ds.listener_.clear();
883}
884
Felipe Lemed80e6b62016-10-03 13:08:14 -0700885TEST_F(DumpstateTest, RunCommandDropRoot) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800886 if (!IsStandalone()) {
887 // TODO: temporarily disabled because it might cause other tests to fail after dropping
888 // to Shell - need to refactor tests to avoid this problem)
889 MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n")
890 return;
891 }
Felipe Lemed80e6b62016-10-03 13:08:14 -0700892 // First check root case - only available when running with 'adb root'.
893 uid_t uid = getuid();
894 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700895 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700896 EXPECT_THAT(out, StrEq("0\nstdout\n"));
897 EXPECT_THAT(err, StrEq("stderr\n"));
898 return;
899 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700900 // Then run dropping root.
901 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700902 CommandOptions::WithTimeout(1).DropRoot().Build()));
903 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700904 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700905}
906
907TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800908 if (!IsStandalone()) {
909 // TODO: temporarily disabled because it might cause other tests to fail after dropping
910 // to Shell - need to refactor tests to avoid this problem)
911 MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n")
912 return;
913 }
Felipe Lemef0292972016-11-22 13:57:05 -0800914 if (!PropertiesHelper::IsUserBuild()) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700915 // Emulates user build if necessarily.
916 SetBuildType("user");
917 }
918
919 DropRoot();
920
Felipe Leme7447d7c2016-11-03 18:12:22 -0700921 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700922
923 // We don't know the exact path of su, so we just check for the 'root ...' commands
924 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700925 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700926 EXPECT_THAT(err, IsEmpty());
927}
928
Felipe Leme46b85da2016-11-21 17:40:45 -0800929TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
930 if (!IsStandalone()) {
931 // TODO: temporarily disabled because it might cause other tests to fail after dropping
932 // to Shell - need to refactor tests to avoid this problem)
933 MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n")
934 return;
935 }
Felipe Lemef0292972016-11-22 13:57:05 -0800936 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800937 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
938 return;
939 }
940
941 DropRoot();
942
943 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
944 CommandOptions::WithTimeout(1).AsRoot().Build()));
945
946 EXPECT_THAT(out, StrEq("0\nstdout\n"));
947 EXPECT_THAT(err, StrEq("stderr\n"));
948}
949
Nandana Dutt4b392be2018-11-02 16:17:05 +0000950TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild_withUnroot) {
951 if (!IsStandalone()) {
952 // TODO: temporarily disabled because it might cause other tests to fail after dropping
953 // to Shell - need to refactor tests to avoid this problem)
954 MYLOGE(
955 "Skipping DumpstateTest.RunCommandAsRootNonUserBuild_withUnroot() "
956 "on test suite\n")
957 return;
958 }
959 if (PropertiesHelper::IsUserBuild()) {
960 ALOGI("Skipping RunCommandAsRootNonUserBuild_withUnroot on user builds\n");
961 return;
962 }
963
964 // Same test as above, but with unroot property set, which will override su availability.
965 SetUnroot(true);
966 DropRoot();
967
968 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
969 CommandOptions::WithTimeout(1).AsRoot().Build()));
970
971 // AsRoot is ineffective.
972 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
973 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
974}
975
Yifan Hong48e83a12017-10-03 14:10:07 -0700976TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) {
977 if (!IsStandalone()) {
978 // TODO: temporarily disabled because it might cause other tests to fail after dropping
979 // to Shell - need to refactor tests to avoid this problem)
980 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
981 return;
982 }
983 if (!PropertiesHelper::IsUserBuild()) {
984 // Emulates user build if necessarily.
985 SetBuildType("user");
986 }
987
988 DropRoot();
989
990 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
991 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
992
993 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
994 EXPECT_THAT(err, StrEq("stderr\n"));
995}
996
997TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) {
998 if (!IsStandalone()) {
999 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1000 // to Shell - need to refactor tests to avoid this problem)
1001 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
1002 return;
1003 }
1004 if (PropertiesHelper::IsUserBuild()) {
1005 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1006 return;
1007 }
1008
1009 DropRoot();
1010
1011 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1012 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1013
1014 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1015 EXPECT_THAT(err, StrEq("stderr\n"));
1016}
1017
Nandana Dutt4b392be2018-11-02 16:17:05 +00001018TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild_withUnroot) {
1019 if (!IsStandalone()) {
1020 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1021 // to Shell - need to refactor tests to avoid this problem)
1022 MYLOGE(
1023 "Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild_withUnroot() "
1024 "on test suite\n")
1025 return;
1026 }
1027 if (PropertiesHelper::IsUserBuild()) {
1028 ALOGI("Skipping RunCommandAsRootIfAvailableOnDebugBuild_withUnroot on user builds\n");
1029 return;
1030 }
1031 // Same test as above, but with unroot property set, which will override su availability.
1032 SetUnroot(true);
1033
1034 DropRoot();
1035
1036 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1037 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1038
1039 // It's a userdebug build, so "su root" should be available, but unroot=true overrides it.
1040 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1041 EXPECT_THAT(err, StrEq("stderr\n"));
1042}
1043
Felipe Lemecef02982016-10-03 17:22:22 -07001044TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
1045 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
1046 EXPECT_THAT(out,
1047 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
1048 EXPECT_THAT(err, IsEmpty());
1049}
1050
1051TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
1052 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
1053 EXPECT_THAT(err, IsEmpty());
1054 // We don't know the exact duration, so we check the prefix and suffix
1055 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
1056 "such file or directory\n"));
1057 EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n"));
1058}
1059
1060TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001061 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -07001062 EXPECT_THAT(err, IsEmpty());
1063 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1064}
1065
1066TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001067 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -07001068 EXPECT_THAT(err, IsEmpty());
1069 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
1070}
1071
1072TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001073 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -07001074 EXPECT_THAT(err, IsEmpty());
1075 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1076}
1077
1078TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001079 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -07001080 EXPECT_THAT(err, IsEmpty());
1081 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1082}
1083
1084TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
1085 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -07001086 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -07001087 EXPECT_THAT(err, IsEmpty());
1088 EXPECT_THAT(out, IsEmpty());
1089}
1090
1091TEST_F(DumpstateTest, DumpFileOnDryRun) {
1092 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -07001093 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -07001094 EXPECT_THAT(err, IsEmpty());
Felipe Leme46b85da2016-11-21 17:40:45 -08001095 EXPECT_THAT(
1096 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
1097 EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n------"));
Felipe Lemecef02982016-10-03 17:22:22 -07001098 EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n"));
Felipe Lemecef02982016-10-03 17:22:22 -07001099}
1100
Felipe Leme75876a22016-10-27 16:31:27 -07001101TEST_F(DumpstateTest, DumpFileUpdateProgress) {
1102 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
1103 ds.listener_ = listener;
1104 ds.listener_name_ = "FoxMulder";
Felipe Leme7447d7c2016-11-03 18:12:22 -07001105 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -07001106
1107 EXPECT_CALL(*listener, onProgressUpdated(5));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001108 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -07001109
1110 std::string progress_message =
1111 GetProgressMessage(ds.listener_name_, 5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
1112 EXPECT_THAT(err, StrEq(progress_message));
1113 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1114
1115 ds.listener_.clear();
1116}
1117
Felipe Leme7447d7c2016-11-03 18:12:22 -07001118class DumpstateServiceTest : public DumpstateBaseTest {
Felipe Leme75876a22016-10-27 16:31:27 -07001119 public:
1120 DumpstateService dss;
1121};
1122
1123TEST_F(DumpstateServiceTest, SetListenerNoName) {
1124 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
Felipe Leme009ecbb2016-11-07 10:18:44 -08001125 sp<IDumpstateToken> token;
Vishnu Nair20cf5032018-01-05 13:15:49 -08001126 EXPECT_TRUE(dss.setListener("", listener, /* getSectionDetails = */ false, &token).isOk());
Felipe Leme009ecbb2016-11-07 10:18:44 -08001127 ASSERT_THAT(token, IsNull());
Felipe Leme75876a22016-10-27 16:31:27 -07001128}
1129
1130TEST_F(DumpstateServiceTest, SetListenerNoPointer) {
Felipe Leme009ecbb2016-11-07 10:18:44 -08001131 sp<IDumpstateToken> token;
Vishnu Nair20cf5032018-01-05 13:15:49 -08001132 EXPECT_TRUE(
1133 dss.setListener("whatever", nullptr, /* getSectionDetails = */ false, &token).isOk());
Felipe Leme009ecbb2016-11-07 10:18:44 -08001134 ASSERT_THAT(token, IsNull());
Felipe Leme75876a22016-10-27 16:31:27 -07001135}
1136
1137TEST_F(DumpstateServiceTest, SetListenerTwice) {
1138 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
Felipe Leme009ecbb2016-11-07 10:18:44 -08001139 sp<IDumpstateToken> token;
Vishnu Nair20cf5032018-01-05 13:15:49 -08001140 EXPECT_TRUE(
1141 dss.setListener("whatever", listener, /* getSectionDetails = */ false, &token).isOk());
Felipe Leme009ecbb2016-11-07 10:18:44 -08001142 ASSERT_THAT(token, NotNull());
Felipe Leme75876a22016-10-27 16:31:27 -07001143 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
Vishnu Nair20cf5032018-01-05 13:15:49 -08001144 EXPECT_FALSE(Dumpstate::GetInstance().report_section_);
Felipe Leme75876a22016-10-27 16:31:27 -07001145
Felipe Leme009ecbb2016-11-07 10:18:44 -08001146 token.clear();
Vishnu Nair20cf5032018-01-05 13:15:49 -08001147 EXPECT_TRUE(
1148 dss.setListener("whatsoever", listener, /* getSectionDetails = */ false, &token).isOk());
Felipe Leme009ecbb2016-11-07 10:18:44 -08001149 ASSERT_THAT(token, IsNull());
1150 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
Vishnu Nair20cf5032018-01-05 13:15:49 -08001151 EXPECT_FALSE(Dumpstate::GetInstance().report_section_);
1152}
1153
1154TEST_F(DumpstateServiceTest, SetListenerWithSectionDetails) {
1155 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
1156 sp<IDumpstateToken> token;
1157 Dumpstate::GetInstance().listener_ = nullptr;
1158 EXPECT_TRUE(
1159 dss.setListener("whatever", listener, /* getSectionDetails = */ true, &token).isOk());
1160 ASSERT_THAT(token, NotNull());
1161 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
1162 EXPECT_TRUE(Dumpstate::GetInstance().report_section_);
Felipe Leme75876a22016-10-27 16:31:27 -07001163}
Felipe Leme7447d7c2016-11-03 18:12:22 -07001164
1165class ProgressTest : public DumpstateBaseTest {
1166 public:
1167 Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {
1168 return Progress(max, growth_factor, path);
1169 }
1170
1171 void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) {
1172 std::string expected_content =
1173 android::base::StringPrintf("%d %d\n", expected_runs, expected_average);
1174 std::string actual_content;
Felipe Leme46b85da2016-11-21 17:40:45 -08001175 ReadFileToString(path, &actual_content);
Felipe Leme7447d7c2016-11-03 18:12:22 -07001176 ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path;
1177 }
1178};
1179
1180TEST_F(ProgressTest, SimpleTest) {
1181 Progress progress;
1182 EXPECT_EQ(0, progress.Get());
1183 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
1184 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1185
1186 bool max_increased = progress.Inc(1);
1187 EXPECT_EQ(1, progress.Get());
1188 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
1189 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1190 EXPECT_FALSE(max_increased);
1191
1192 // Ignore negative increase.
1193 max_increased = progress.Inc(-1);
1194 EXPECT_EQ(1, progress.Get());
1195 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
1196 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1197 EXPECT_FALSE(max_increased);
1198}
1199
1200TEST_F(ProgressTest, MaxGrowsInsideNewRange) {
1201 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1202 EXPECT_EQ(0, progress.Get());
1203 EXPECT_EQ(10, progress.GetInitialMax());
1204 EXPECT_EQ(10, progress.GetMax());
1205
1206 // No increase
1207 bool max_increased = progress.Inc(10);
1208 EXPECT_EQ(10, progress.Get());
1209 EXPECT_EQ(10, progress.GetMax());
1210 EXPECT_FALSE(max_increased);
1211
1212 // Increase, with new value < max*20%
1213 max_increased = progress.Inc(1);
1214 EXPECT_EQ(11, progress.Get());
1215 EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13
1216 EXPECT_TRUE(max_increased);
1217}
1218
1219TEST_F(ProgressTest, MaxGrowsOutsideNewRange) {
1220 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1221 EXPECT_EQ(0, progress.Get());
1222 EXPECT_EQ(10, progress.GetInitialMax());
1223 EXPECT_EQ(10, progress.GetMax());
1224
1225 // No increase
1226 bool max_increased = progress.Inc(10);
1227 EXPECT_EQ(10, progress.Get());
1228 EXPECT_EQ(10, progress.GetMax());
1229 EXPECT_FALSE(max_increased);
1230
1231 // Increase, with new value > max*20%
1232 max_increased = progress.Inc(5);
1233 EXPECT_EQ(15, progress.Get());
1234 EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18
1235 EXPECT_TRUE(max_increased);
1236}
1237
1238TEST_F(ProgressTest, InvalidPath) {
1239 Progress progress("/devil/null");
1240 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1241}
1242
1243TEST_F(ProgressTest, EmptyFile) {
1244 Progress progress(CopyTextFileFixture("empty-file.txt"));
1245 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1246}
1247
1248TEST_F(ProgressTest, InvalidLine1stEntryNAN) {
1249 Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt"));
1250 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1251}
1252
1253TEST_F(ProgressTest, InvalidLine2ndEntryNAN) {
1254 Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt"));
1255 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1256}
1257
1258TEST_F(ProgressTest, InvalidLineBothNAN) {
1259 Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt"));
1260 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1261}
1262
1263TEST_F(ProgressTest, InvalidLine1stEntryNegative) {
1264 Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt"));
1265 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1266}
1267
1268TEST_F(ProgressTest, InvalidLine2ndEntryNegative) {
1269 Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt"));
1270 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1271}
1272
1273TEST_F(ProgressTest, InvalidLine1stEntryTooBig) {
1274 Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt"));
1275 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1276}
1277
1278TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) {
1279 Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt"));
1280 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1281}
1282
1283// Tests stats are properly saved when the file does not exists.
1284TEST_F(ProgressTest, FirstTime) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001285 if (!IsStandalone()) {
1286 // TODO: temporarily disabled because it's failing when running as suite
1287 MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n")
1288 return;
1289 }
1290
Felipe Leme7447d7c2016-11-03 18:12:22 -07001291 std::string path = kTestDataPath + "FirstTime.txt";
1292 android::base::RemoveFileIfExists(path);
1293
1294 Progress run1(path);
1295 EXPECT_EQ(0, run1.Get());
1296 EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax());
1297 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1298
1299 bool max_increased = run1.Inc(20);
1300 EXPECT_EQ(20, run1.Get());
1301 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1302 EXPECT_FALSE(max_increased);
1303
1304 run1.Save();
1305 AssertStats(path, 1, 20);
1306}
1307
1308// Tests what happens when the persistent settings contains the average duration of 1 run.
1309// Data on file is 1 run and 109 average.
1310TEST_F(ProgressTest, SecondTime) {
1311 std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt");
1312
1313 Progress run1 = GetInstance(-42, 1.2, path);
1314 EXPECT_EQ(0, run1.Get());
1315 EXPECT_EQ(10, run1.GetInitialMax());
1316 EXPECT_EQ(10, run1.GetMax());
1317
1318 bool max_increased = run1.Inc(20);
1319 EXPECT_EQ(20, run1.Get());
1320 EXPECT_EQ(24, run1.GetMax());
1321 EXPECT_TRUE(max_increased);
1322
1323 // Average now is 2 runs and (10 + 20)/ 2 = 15
1324 run1.Save();
1325 AssertStats(path, 2, 15);
1326
1327 Progress run2 = GetInstance(-42, 1.2, path);
1328 EXPECT_EQ(0, run2.Get());
1329 EXPECT_EQ(15, run2.GetInitialMax());
1330 EXPECT_EQ(15, run2.GetMax());
1331
1332 max_increased = run2.Inc(25);
1333 EXPECT_EQ(25, run2.Get());
1334 EXPECT_EQ(30, run2.GetMax());
1335 EXPECT_TRUE(max_increased);
1336
1337 // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18
1338 run2.Save();
1339 AssertStats(path, 3, 18);
1340
1341 Progress run3 = GetInstance(-42, 1.2, path);
1342 EXPECT_EQ(0, run3.Get());
1343 EXPECT_EQ(18, run3.GetInitialMax());
1344 EXPECT_EQ(18, run3.GetMax());
1345
1346 // Make sure average decreases as well
1347 max_increased = run3.Inc(5);
1348 EXPECT_EQ(5, run3.Get());
1349 EXPECT_EQ(18, run3.GetMax());
1350 EXPECT_FALSE(max_increased);
1351
1352 // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14
1353 run3.Save();
1354 AssertStats(path, 4, 14);
1355}
1356
1357// Tests what happens when the persistent settings contains the average duration of 2 runs.
1358// Data on file is 2 runs and 15 average.
1359TEST_F(ProgressTest, ThirdTime) {
1360 std::string path = CopyTextFileFixture("stats-two-runs.txt");
1361 AssertStats(path, 2, 15); // Sanity check
1362
1363 Progress run1 = GetInstance(-42, 1.2, path);
1364 EXPECT_EQ(0, run1.Get());
1365 EXPECT_EQ(15, run1.GetInitialMax());
1366 EXPECT_EQ(15, run1.GetMax());
1367
1368 bool max_increased = run1.Inc(20);
1369 EXPECT_EQ(20, run1.Get());
1370 EXPECT_EQ(24, run1.GetMax());
1371 EXPECT_TRUE(max_increased);
1372
1373 // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16
1374 run1.Save();
1375 AssertStats(path, 3, 16);
1376}
1377
Felipe Leme46b85da2016-11-21 17:40:45 -08001378class DumpstateUtilTest : public DumpstateBaseTest {
1379 public:
1380 void SetUp() {
1381 DumpstateBaseTest::SetUp();
1382 SetDryRun(false);
1383 }
1384
Felipe Leme46b85da2016-11-21 17:40:45 -08001385 void CaptureFdOut() {
Felipe Lemef0292972016-11-22 13:57:05 -08001386 ReadFileToString(path_, &out);
Felipe Leme46b85da2016-11-21 17:40:45 -08001387 }
1388
1389 void CreateFd(const std::string& name) {
1390 path_ = kTestDataPath + name;
1391 MYLOGD("Creating fd for file %s\n", path_.c_str());
1392
1393 fd = TEMP_FAILURE_RETRY(open(path_.c_str(),
1394 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
1395 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
1396 ASSERT_GE(fd, 0) << "could not create FD for path " << path_;
1397 }
1398
1399 // Runs a command into the `fd` and capture `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001400 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme46b85da2016-11-21 17:40:45 -08001401 const CommandOptions& options = CommandOptions::DEFAULT) {
1402 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001403 int status = RunCommandToFd(fd, title, full_command, options);
Felipe Leme46b85da2016-11-21 17:40:45 -08001404 close(fd);
1405
1406 CaptureFdOut();
1407 err = GetCapturedStderr();
1408 return status;
1409 }
1410
1411 // Dumps a file and into the `fd` and `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001412 int DumpFile(const std::string& title, const std::string& path) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001413 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001414 int status = DumpFileToFd(fd, title, path);
Felipe Leme46b85da2016-11-21 17:40:45 -08001415 close(fd);
1416
1417 CaptureFdOut();
1418 err = GetCapturedStderr();
1419 return status;
1420 }
1421
Ecco Park61ffcf72016-10-27 15:46:26 -07001422 // Find out the pid of the process_name
1423 int FindPidOfProcess(const std::string& process_name) {
1424 CaptureStderr();
1425 int status = GetPidByName(process_name);
1426 err = GetCapturedStderr();
1427 return status;
1428 }
1429
Felipe Leme46b85da2016-11-21 17:40:45 -08001430 int fd;
1431
1432 // 'fd` output and `stderr` from the last command ran.
1433 std::string out, err;
1434
1435 private:
1436 std::string path_;
1437};
1438
1439TEST_F(DumpstateUtilTest, RunCommandNoArgs) {
Felipe Lemef0292972016-11-22 13:57:05 -08001440 CreateFd("RunCommandNoArgs.txt");
1441 EXPECT_EQ(-1, RunCommand("", {}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001442}
1443
Felipe Lemef0292972016-11-22 13:57:05 -08001444TEST_F(DumpstateUtilTest, RunCommandNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001445 CreateFd("RunCommandWithNoArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001446 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001447 EXPECT_THAT(out, StrEq("stdout\n"));
1448 EXPECT_THAT(err, StrEq("stderr\n"));
1449}
1450
Felipe Lemef0292972016-11-22 13:57:05 -08001451TEST_F(DumpstateUtilTest, RunCommandWithTitle) {
1452 CreateFd("RunCommandWithNoArgs.txt");
1453 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1454 EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
1455 EXPECT_THAT(err, StrEq("stderr\n"));
1456}
1457
Felipe Leme46b85da2016-11-21 17:40:45 -08001458TEST_F(DumpstateUtilTest, RunCommandWithOneArg) {
1459 CreateFd("RunCommandWithOneArg.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001460 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001461 EXPECT_THAT(err, IsEmpty());
1462 EXPECT_THAT(out, StrEq("one\n"));
1463}
1464
1465TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) {
1466 CreateFd("RunCommandWithMultipleArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001467 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001468 EXPECT_THAT(err, IsEmpty());
1469 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
1470}
1471
1472TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) {
1473 CreateFd("RunCommandWithLoggingMessage.txt");
1474 EXPECT_EQ(
Felipe Lemef0292972016-11-22 13:57:05 -08001475 0, RunCommand("", {kSimpleCommand},
Felipe Leme46b85da2016-11-21 17:40:45 -08001476 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
1477 EXPECT_THAT(out, StrEq("stdout\n"));
1478 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
1479}
1480
1481TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) {
1482 CreateFd("RunCommandRedirectStderr.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001483 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
1484 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001485 EXPECT_THAT(out, IsEmpty());
1486 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
1487}
1488
1489TEST_F(DumpstateUtilTest, RunCommandDryRun) {
1490 CreateFd("RunCommandDryRun.txt");
1491 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001492 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1493 EXPECT_THAT(out, StrEq(android::base::StringPrintf(
1494 "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n",
1495 kSimpleCommand.c_str())));
1496 EXPECT_THAT(err, IsEmpty());
1497}
1498
1499TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) {
1500 CreateFd("RunCommandDryRun.txt");
1501 SetDryRun(true);
1502 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001503 EXPECT_THAT(
1504 out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str())));
1505 EXPECT_THAT(err, IsEmpty());
1506}
1507
1508TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) {
1509 CreateFd("RunCommandDryRunAlways.txt");
1510 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001511 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001512 EXPECT_THAT(out, StrEq("stdout\n"));
1513 EXPECT_THAT(err, StrEq("stderr\n"));
1514}
1515
1516TEST_F(DumpstateUtilTest, RunCommandNotFound) {
1517 CreateFd("RunCommandNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001518 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001519 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
1520 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
1521}
1522
1523TEST_F(DumpstateUtilTest, RunCommandFails) {
1524 CreateFd("RunCommandFails.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001525 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001526 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
1527 " --exit 42' failed: exit code 42\n"));
1528 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
1529 " --exit 42' failed: exit code 42\n"));
1530}
1531
1532TEST_F(DumpstateUtilTest, RunCommandCrashes) {
1533 CreateFd("RunCommandCrashes.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001534 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001535 // We don't know the exit code, so check just the prefix.
1536 EXPECT_THAT(
1537 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1538 EXPECT_THAT(
1539 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1540}
1541
Vishnu Nair6921f802017-11-22 09:17:23 -08001542TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001543 CreateFd("RunCommandTimesout.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001544 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1545 CommandOptions::WithTimeout(1).Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001546 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1547 " --sleep 2' timed out after 1"));
1548 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1549 " --sleep 2' timed out after 1"));
1550}
1551
Vishnu Nair6921f802017-11-22 09:17:23 -08001552TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) {
1553 CreateFd("RunCommandTimesout.txt");
1554 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1555 CommandOptions::WithTimeoutInMs(1000).Build()));
1556 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1557 " --sleep 2' timed out after 1"));
1558 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1559 " --sleep 2' timed out after 1"));
1560}
1561
1562
Felipe Leme46b85da2016-11-21 17:40:45 -08001563TEST_F(DumpstateUtilTest, RunCommandIsKilled) {
1564 CreateFd("RunCommandIsKilled.txt");
1565 CaptureStderr();
1566
1567 std::thread t([=]() {
Felipe Lemef0292972016-11-22 13:57:05 -08001568 EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001569 CommandOptions::WithTimeout(100).Always().Build()));
1570 });
1571
1572 // Capture pid and pre-sleep output.
1573 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
1574 std::string err = GetCapturedStderr();
1575 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
1576
1577 CaptureFdOut();
1578 std::vector<std::string> lines = android::base::Split(out, "\n");
1579 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
1580
1581 int pid = atoi(lines[0].c_str());
1582 EXPECT_THAT(lines[1], StrEq("stdout line1"));
1583 EXPECT_THAT(lines[2], IsEmpty()); // \n
1584
1585 // Then kill the process.
1586 CaptureFdOut();
1587 CaptureStderr();
1588 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
1589 t.join();
1590
1591 // Finally, check output after murder.
1592 CaptureFdOut();
1593 err = GetCapturedStderr();
1594
1595 // out starts with the pid, which is an unknown
1596 EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand +
1597 " --pid --sleep 20' failed: killed by signal 15\n"));
1598 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
1599 " --pid --sleep 20' failed: killed by signal 15\n"));
1600}
1601
1602TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) {
1603 if (!IsStandalone()) {
1604 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1605 // to Shell - need to refactor tests to avoid this problem)
1606 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n")
1607 return;
1608 }
1609 CreateFd("RunCommandAsRootUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001610 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001611 // Emulates user build if necessarily.
1612 SetBuildType("user");
1613 }
1614
1615 DropRoot();
1616
Felipe Lemef0292972016-11-22 13:57:05 -08001617 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001618
1619 // We don't know the exact path of su, so we just check for the 'root ...' commands
1620 EXPECT_THAT(out, StartsWith("Skipping"));
1621 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
1622 EXPECT_THAT(err, IsEmpty());
1623}
1624
1625TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) {
1626 if (!IsStandalone()) {
1627 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1628 // to Shell - need to refactor tests to avoid this problem)
1629 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n")
1630 return;
1631 }
1632 CreateFd("RunCommandAsRootNonUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001633 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001634 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1635 return;
1636 }
1637
1638 DropRoot();
1639
Felipe Lemef0292972016-11-22 13:57:05 -08001640 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1641 CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001642
1643 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1644 EXPECT_THAT(err, StrEq("stderr\n"));
1645}
Felipe Leme46b85da2016-11-21 17:40:45 -08001646
Yifan Hong48e83a12017-10-03 14:10:07 -07001647
1648TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) {
1649 if (!IsStandalone()) {
1650 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1651 // to Shell - need to refactor tests to avoid this problem)
1652 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
1653 return;
1654 }
1655 CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt");
1656 if (!PropertiesHelper::IsUserBuild()) {
1657 // Emulates user build if necessarily.
1658 SetBuildType("user");
1659 }
1660
1661 DropRoot();
1662
1663 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1664 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1665
1666 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1667 EXPECT_THAT(err, StrEq("stderr\n"));
1668}
1669
1670TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) {
1671 if (!IsStandalone()) {
1672 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1673 // to Shell - need to refactor tests to avoid this problem)
1674 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
1675 return;
1676 }
1677 CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt");
1678 if (PropertiesHelper::IsUserBuild()) {
1679 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1680 return;
1681 }
1682
1683 DropRoot();
1684
1685 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1686 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1687
1688 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1689 EXPECT_THAT(err, StrEq("stderr\n"));
1690}
1691
Felipe Leme46b85da2016-11-21 17:40:45 -08001692TEST_F(DumpstateUtilTest, RunCommandDropRoot) {
1693 if (!IsStandalone()) {
1694 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1695 // to Shell - need to refactor tests to avoid this problem)
1696 MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n")
1697 return;
1698 }
1699 CreateFd("RunCommandDropRoot.txt");
1700 // First check root case - only available when running with 'adb root'.
1701 uid_t uid = getuid();
1702 if (uid == 0) {
Felipe Lemef0292972016-11-22 13:57:05 -08001703 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001704 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1705 EXPECT_THAT(err, StrEq("stderr\n"));
1706 return;
1707 }
1708 // Then run dropping root.
Felipe Lemef0292972016-11-22 13:57:05 -08001709 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001710 CommandOptions::WithTimeout(1).DropRoot().Build()));
1711 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1712 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
1713}
1714
Felipe Lemef0292972016-11-22 13:57:05 -08001715TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001716 CreateFd("DumpFileNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001717 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001718 EXPECT_THAT(out,
1719 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
1720 EXPECT_THAT(err, IsEmpty());
1721}
1722
Felipe Lemef0292972016-11-22 13:57:05 -08001723TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) {
1724 CreateFd("DumpFileNotFound.txt");
1725 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
1726 EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such "
1727 "file or directory\n"));
1728 EXPECT_THAT(err, IsEmpty());
1729}
1730
Felipe Leme46b85da2016-11-21 17:40:45 -08001731TEST_F(DumpstateUtilTest, DumpFileSingleLine) {
1732 CreateFd("DumpFileSingleLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001733 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001734 EXPECT_THAT(err, IsEmpty());
1735 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1736}
1737
1738TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) {
1739 CreateFd("DumpFileSingleLineWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001740 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001741 EXPECT_THAT(err, IsEmpty());
1742 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
1743}
1744
1745TEST_F(DumpstateUtilTest, DumpFileMultipleLines) {
1746 CreateFd("DumpFileMultipleLines.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001747 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001748 EXPECT_THAT(err, IsEmpty());
1749 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1750}
1751
1752TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) {
1753 CreateFd("DumpFileMultipleLinesWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001754 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001755 EXPECT_THAT(err, IsEmpty());
1756 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1757}
1758
Felipe Lemef0292972016-11-22 13:57:05 -08001759TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) {
1760 CreateFd("DumpFileOnDryRun.txt");
1761 SetDryRun(true);
1762 std::string path = kTestDataPath + "single-line.txt";
1763 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
1764 EXPECT_THAT(err, IsEmpty());
1765 EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n"));
1766}
1767
Felipe Leme46b85da2016-11-21 17:40:45 -08001768TEST_F(DumpstateUtilTest, DumpFileOnDryRun) {
1769 CreateFd("DumpFileOnDryRun.txt");
1770 SetDryRun(true);
1771 std::string path = kTestDataPath + "single-line.txt";
Felipe Lemef0292972016-11-22 13:57:05 -08001772 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001773 EXPECT_THAT(err, IsEmpty());
Felipe Lemef0292972016-11-22 13:57:05 -08001774 EXPECT_THAT(
1775 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
1776 EXPECT_THAT(out, EndsWith("skipped on dry run\n"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001777}
Ecco Park61ffcf72016-10-27 15:46:26 -07001778
1779TEST_F(DumpstateUtilTest, FindingPidWithExistingProcess) {
1780 // init process always has pid 1.
1781 EXPECT_EQ(1, FindPidOfProcess("init"));
1782 EXPECT_THAT(err, IsEmpty());
1783}
1784
1785TEST_F(DumpstateUtilTest, FindingPidWithNotExistingProcess) {
1786 // find the process with abnormal name.
1787 EXPECT_EQ(-1, FindPidOfProcess("abcdef12345-543"));
1788 EXPECT_THAT(err, StrEq("can't find the pid\n"));
1789}
Felipe Leme47e9be22016-12-21 15:37:07 -08001790
1791} // namespace dumpstate
1792} // namespace os
1793} // namespace android