blob: 0a0c40ee0d28650502b2a153f237b88e3ee7af8d [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 Kaure370d682019-10-01 16:49:30 +010039#include <android-base/unique_fd.h>
Hunter Knepshield8540faf2020-02-04 19:47:20 -080040#include <android/hardware/dumpstate/1.1/types.h>
41#include <cutils/properties.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070042
Felipe Leme47e9be22016-12-21 15:37:07 -080043namespace android {
44namespace os {
45namespace dumpstate {
Felipe Lemed80e6b62016-10-03 13:08:14 -070046
Hunter Knepshield8540faf2020-02-04 19:47:20 -080047using ::android::hardware::dumpstate::V1_1::DumpstateMode;
Felipe Leme4c2d6632016-09-28 14:32:00 -070048using ::testing::EndsWith;
Felipe Leme46b85da2016-11-21 17:40:45 -080049using ::testing::HasSubstr;
Felipe Leme4c2d6632016-09-28 14:32:00 -070050using ::testing::IsEmpty;
Hunter Knepshield8540faf2020-02-04 19:47:20 -080051using ::testing::IsNull;
Felipe Leme009ecbb2016-11-07 10:18:44 -080052using ::testing::NotNull;
Felipe Leme4c2d6632016-09-28 14:32:00 -070053using ::testing::StartsWith;
Hunter Knepshield8540faf2020-02-04 19:47:20 -080054using ::testing::StrEq;
Felipe Leme4c2d6632016-09-28 14:32:00 -070055using ::testing::Test;
56using ::testing::internal::CaptureStderr;
57using ::testing::internal::CaptureStdout;
58using ::testing::internal::GetCapturedStderr;
59using ::testing::internal::GetCapturedStdout;
60
Nandana Dutt3f8c7172018-09-25 12:01:54 +010061#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
62
Felipe Leme75876a22016-10-27 16:31:27 -070063class DumpstateListenerMock : public IDumpstateListener {
64 public:
Nandana Dutta6a28bd2019-01-14 16:54:38 +000065 MOCK_METHOD1(onProgress, binder::Status(int32_t progress));
66 MOCK_METHOD1(onError, binder::Status(int32_t error_code));
Nandana Duttcc4ead82019-01-23 08:29:23 +000067 MOCK_METHOD0(onFinished, binder::Status());
Paul Chang0d2aad72020-02-13 20:04:03 +080068 MOCK_METHOD1(onScreenshotTaken, binder::Status(bool success));
Felipe Leme75876a22016-10-27 16:31:27 -070069
70 protected:
71 MOCK_METHOD0(onAsBinder, IBinder*());
72};
73
Felipe Leme46b85da2016-11-21 17:40:45 -080074static int calls_;
75
Felipe Leme7447d7c2016-11-03 18:12:22 -070076// Base class for all tests in this file
77class DumpstateBaseTest : public Test {
Felipe Leme46b85da2016-11-21 17:40:45 -080078 public:
79 virtual void SetUp() override {
80 calls_++;
Felipe Lemef0292972016-11-22 13:57:05 -080081 SetDryRun(false);
Felipe Leme46b85da2016-11-21 17:40:45 -080082 }
83
Felipe Lemef0292972016-11-22 13:57:05 -080084 void SetDryRun(bool dry_run) const {
85 PropertiesHelper::dry_run_ = dry_run;
86 }
87
88 void SetBuildType(const std::string& build_type) const {
89 PropertiesHelper::build_type_ = build_type;
90 }
91
Nandana Dutt4b392be2018-11-02 16:17:05 +000092 void SetUnroot(bool unroot) const {
93 PropertiesHelper::unroot_ = unroot;
94 }
95
Felipe Lemef0292972016-11-22 13:57:05 -080096 bool IsStandalone() const {
Felipe Leme46b85da2016-11-21 17:40:45 -080097 return calls_ == 1;
98 }
99
Felipe Lemef0292972016-11-22 13:57:05 -0800100 void DropRoot() const {
101 DropRootUser();
Felipe Leme46b85da2016-11-21 17:40:45 -0800102 uid_t uid = getuid();
103 ASSERT_EQ(2000, (int)uid);
104 }
105
Felipe Leme7447d7c2016-11-03 18:12:22 -0700106 protected:
107 const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str());
Dan Shie177e8e2019-06-20 11:08:14 -0700108 const std::string kTestDataPath = kTestPath + "/tests/testdata/";
109 const std::string kSimpleCommand = kTestPath + "/dumpstate_test_fixture";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700110 const std::string kEchoCommand = "/system/bin/echo";
111
112 /*
113 * Copies a text file fixture to a temporary file, returning it's path.
114 *
115 * Useful in cases where the test case changes the content of the tile.
116 */
117 std::string CopyTextFileFixture(const std::string& relative_name) {
118 std::string from = kTestDataPath + relative_name;
119 // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it
120 // around for poking when the test fails.
121 std::string to = kTestDataPath + relative_name + ".tmp";
122 ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str());
123 android::base::RemoveFileIfExists(to);
124 CopyTextFile(from, to);
125 return to.c_str();
126 }
127
Felipe Leme46b85da2016-11-21 17:40:45 -0800128 // Need functions that returns void to use assertions -
Felipe Leme7447d7c2016-11-03 18:12:22 -0700129 // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement
Felipe Leme46b85da2016-11-21 17:40:45 -0800130 void ReadFileToString(const std::string& path, std::string* content) {
131 ASSERT_TRUE(android::base::ReadFileToString(path, content))
132 << "could not read contents from " << path;
133 }
134 void WriteStringToFile(const std::string& content, const std::string& path) {
135 ASSERT_TRUE(android::base::WriteStringToFile(content, path))
136 << "could not write contents to " << path;
137 }
138
139 private:
Felipe Leme7447d7c2016-11-03 18:12:22 -0700140 void CopyTextFile(const std::string& from, const std::string& to) {
141 std::string content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800142 ReadFileToString(from, &content);
143 WriteStringToFile(content, to);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700144 }
145};
146
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100147class DumpOptionsTest : public Test {
148 public:
149 virtual ~DumpOptionsTest() {
150 }
151 virtual void SetUp() {
152 options_ = Dumpstate::DumpOptions();
153 }
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000154 void TearDown() {
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000155 }
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100156 Dumpstate::DumpOptions options_;
Abhijeet Kaure370d682019-10-01 16:49:30 +0100157 android::base::unique_fd fd;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100158};
159
160TEST_F(DumpOptionsTest, InitializeNone) {
161 // clang-format off
162 char* argv[] = {
163 const_cast<char*>("dumpstate")
164 };
165 // clang-format on
166
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100167 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
168
169 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000170
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100171 EXPECT_FALSE(options_.do_add_date);
172 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100173 EXPECT_FALSE(options_.use_socket);
174 EXPECT_FALSE(options_.use_control_socket);
175 EXPECT_FALSE(options_.show_header_only);
176 EXPECT_TRUE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800177 EXPECT_FALSE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100178 EXPECT_FALSE(options_.do_progress_updates);
179 EXPECT_FALSE(options_.is_remote_mode);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800180 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000181}
182
183TEST_F(DumpOptionsTest, InitializeAdbBugreport) {
184 // clang-format off
185 char* argv[] = {
186 const_cast<char*>("dumpstatez"),
187 const_cast<char*>("-S"),
188 const_cast<char*>("-d"),
189 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000190 };
191 // clang-format on
192
193 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
194
195 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
196 EXPECT_TRUE(options_.do_add_date);
197 EXPECT_TRUE(options_.do_zip_file);
198 EXPECT_TRUE(options_.use_control_socket);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000199
200 // Other options retain default values
201 EXPECT_TRUE(options_.do_vibrate);
202 EXPECT_FALSE(options_.show_header_only);
Paul Chang0d2aad72020-02-13 20:04:03 +0800203 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000204 EXPECT_FALSE(options_.do_progress_updates);
205 EXPECT_FALSE(options_.is_remote_mode);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000206 EXPECT_FALSE(options_.use_socket);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800207 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000208}
209
210TEST_F(DumpOptionsTest, InitializeAdbShellBugreport) {
211 // clang-format off
212 char* argv[] = {
213 const_cast<char*>("dumpstate"),
214 const_cast<char*>("-s"),
215 };
216 // clang-format on
217
218 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
219
220 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
221 EXPECT_TRUE(options_.use_socket);
222
223 // Other options retain default values
224 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000225 EXPECT_FALSE(options_.do_add_date);
226 EXPECT_FALSE(options_.do_zip_file);
227 EXPECT_FALSE(options_.use_control_socket);
228 EXPECT_FALSE(options_.show_header_only);
Paul Chang0d2aad72020-02-13 20:04:03 +0800229 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000230 EXPECT_FALSE(options_.do_progress_updates);
231 EXPECT_FALSE(options_.is_remote_mode);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800232 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000233}
234
235TEST_F(DumpOptionsTest, InitializeFullBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100236 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_FULL, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000237 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800238 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000239 EXPECT_TRUE(options_.do_zip_file);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800240 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::FULL);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000241
242 // Other options retain default values
243 EXPECT_TRUE(options_.do_vibrate);
244 EXPECT_FALSE(options_.use_control_socket);
245 EXPECT_FALSE(options_.show_header_only);
246 EXPECT_FALSE(options_.do_progress_updates);
247 EXPECT_FALSE(options_.is_remote_mode);
248 EXPECT_FALSE(options_.use_socket);
249 EXPECT_FALSE(options_.do_start_service);
250}
251
252TEST_F(DumpOptionsTest, InitializeInteractiveBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100253 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000254 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000255 EXPECT_TRUE(options_.do_zip_file);
256 EXPECT_TRUE(options_.do_progress_updates);
257 EXPECT_TRUE(options_.do_start_service);
Paul Chang0d2aad72020-02-13 20:04:03 +0800258 EXPECT_TRUE(options_.do_screenshot);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800259 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::INTERACTIVE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000260
261 // Other options retain default values
262 EXPECT_TRUE(options_.do_vibrate);
263 EXPECT_FALSE(options_.use_control_socket);
264 EXPECT_FALSE(options_.show_header_only);
265 EXPECT_FALSE(options_.is_remote_mode);
266 EXPECT_FALSE(options_.use_socket);
267}
268
269TEST_F(DumpOptionsTest, InitializeRemoteBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100270 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_REMOTE, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000271 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000272 EXPECT_TRUE(options_.do_zip_file);
273 EXPECT_TRUE(options_.is_remote_mode);
274 EXPECT_FALSE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800275 EXPECT_FALSE(options_.do_screenshot);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800276 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::REMOTE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000277
278 // Other options retain default values
279 EXPECT_FALSE(options_.use_control_socket);
280 EXPECT_FALSE(options_.show_header_only);
281 EXPECT_FALSE(options_.do_progress_updates);
282 EXPECT_FALSE(options_.use_socket);
283}
284
285TEST_F(DumpOptionsTest, InitializeWearBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100286 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WEAR, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000287 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800288 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000289 EXPECT_TRUE(options_.do_zip_file);
290 EXPECT_TRUE(options_.do_progress_updates);
291 EXPECT_TRUE(options_.do_start_service);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800292 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WEAR);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000293
294 // Other options retain default values
295 EXPECT_TRUE(options_.do_vibrate);
296 EXPECT_FALSE(options_.use_control_socket);
297 EXPECT_FALSE(options_.show_header_only);
298 EXPECT_FALSE(options_.is_remote_mode);
299 EXPECT_FALSE(options_.use_socket);
300}
301
302TEST_F(DumpOptionsTest, InitializeTelephonyBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100303 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_TELEPHONY, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000304 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800305 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000306 EXPECT_TRUE(options_.do_zip_file);
307 EXPECT_TRUE(options_.telephony_only);
Hunter Knepshield820f9bc2020-02-05 20:10:53 -0800308 EXPECT_TRUE(options_.do_progress_updates);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800309 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::CONNECTIVITY);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000310
311 // Other options retain default values
312 EXPECT_TRUE(options_.do_vibrate);
313 EXPECT_FALSE(options_.use_control_socket);
314 EXPECT_FALSE(options_.show_header_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000315 EXPECT_FALSE(options_.is_remote_mode);
316 EXPECT_FALSE(options_.use_socket);
317}
318
319TEST_F(DumpOptionsTest, InitializeWifiBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100320 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WIFI, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000321 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800322 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000323 EXPECT_TRUE(options_.do_zip_file);
324 EXPECT_TRUE(options_.wifi_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800325 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WIFI);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000326
327 // Other options retain default values
328 EXPECT_TRUE(options_.do_vibrate);
329 EXPECT_FALSE(options_.use_control_socket);
330 EXPECT_FALSE(options_.show_header_only);
331 EXPECT_FALSE(options_.do_progress_updates);
332 EXPECT_FALSE(options_.is_remote_mode);
333 EXPECT_FALSE(options_.use_socket);
334}
335
336TEST_F(DumpOptionsTest, InitializeDefaultBugReport) {
337 // default: commandline options are not overridden
338 // clang-format off
339 char* argv[] = {
340 const_cast<char*>("bugreport"),
341 const_cast<char*>("-d"),
342 const_cast<char*>("-p"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000343 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000344 };
345 // clang-format on
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000346 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
347
348 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
349 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800350 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000351 EXPECT_TRUE(options_.do_zip_file);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800352 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000353
354 // Other options retain default values
355 EXPECT_TRUE(options_.do_vibrate);
356 EXPECT_FALSE(options_.use_control_socket);
357 EXPECT_FALSE(options_.show_header_only);
358 EXPECT_FALSE(options_.do_progress_updates);
359 EXPECT_FALSE(options_.is_remote_mode);
360 EXPECT_FALSE(options_.use_socket);
361 EXPECT_FALSE(options_.wifi_only);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100362}
363
364TEST_F(DumpOptionsTest, InitializePartial1) {
365 // clang-format off
366 char* argv[] = {
367 const_cast<char*>("dumpstate"),
368 const_cast<char*>("-d"),
369 const_cast<char*>("-z"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100370 const_cast<char*>("-s"),
371 const_cast<char*>("-S"),
372
373 };
374 // clang-format on
375
376 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
377
378 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
379 EXPECT_TRUE(options_.do_add_date);
380 EXPECT_TRUE(options_.do_zip_file);
381 // TODO: Maybe we should trim the filename
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100382 EXPECT_TRUE(options_.use_socket);
383 EXPECT_TRUE(options_.use_control_socket);
384
385 // Other options retain default values
386 EXPECT_FALSE(options_.show_header_only);
387 EXPECT_TRUE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800388 EXPECT_FALSE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100389 EXPECT_FALSE(options_.do_progress_updates);
390 EXPECT_FALSE(options_.is_remote_mode);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800391 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100392}
393
394TEST_F(DumpOptionsTest, InitializePartial2) {
395 // clang-format off
396 char* argv[] = {
397 const_cast<char*>("dumpstate"),
398 const_cast<char*>("-v"),
399 const_cast<char*>("-q"),
400 const_cast<char*>("-p"),
401 const_cast<char*>("-P"),
402 const_cast<char*>("-R"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100403 };
404 // clang-format on
405
406 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
407
408 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
409 EXPECT_TRUE(options_.show_header_only);
410 EXPECT_FALSE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800411 EXPECT_TRUE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100412 EXPECT_TRUE(options_.do_progress_updates);
413 EXPECT_TRUE(options_.is_remote_mode);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100414
415 // Other options retain default values
416 EXPECT_FALSE(options_.do_add_date);
417 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100418 EXPECT_FALSE(options_.use_socket);
419 EXPECT_FALSE(options_.use_control_socket);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800420 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100421}
422
423TEST_F(DumpOptionsTest, InitializeHelp) {
424 // clang-format off
425 char* argv[] = {
426 const_cast<char*>("dumpstate"),
427 const_cast<char*>("-h")
428 };
429 // clang-format on
430
431 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
432
433 // -h is for help.
434 EXPECT_EQ(status, Dumpstate::RunStatus::HELP);
435}
436
437TEST_F(DumpOptionsTest, InitializeUnknown) {
438 // clang-format off
439 char* argv[] = {
440 const_cast<char*>("dumpstate"),
441 const_cast<char*>("-u") // unknown flag
442 };
443 // clang-format on
444
445 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
446
447 // -u is unknown.
448 EXPECT_EQ(status, Dumpstate::RunStatus::INVALID_INPUT);
449}
450
451TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile1) {
452 options_.do_zip_file = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000453 // Writing to socket = !writing to file.
454 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100455 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000456
457 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100458 EXPECT_TRUE(options_.ValidateOptions());
459}
460
461TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile2) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100462 options_.do_progress_updates = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000463 // Writing to socket = !writing to file.
464 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100465 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000466
467 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100468 EXPECT_TRUE(options_.ValidateOptions());
469}
470
471TEST_F(DumpOptionsTest, ValidateOptionsNeedZipfile) {
472 options_.use_control_socket = true;
473 EXPECT_FALSE(options_.ValidateOptions());
474
475 options_.do_zip_file = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100476 EXPECT_TRUE(options_.ValidateOptions());
477}
478
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100479TEST_F(DumpOptionsTest, ValidateOptionsRemoteMode) {
480 options_.is_remote_mode = true;
481 EXPECT_FALSE(options_.ValidateOptions());
482
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100483 options_.do_zip_file = true;
484 options_.do_add_date = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100485 EXPECT_TRUE(options_.ValidateOptions());
486}
487
Felipe Leme7447d7c2016-11-03 18:12:22 -0700488class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700489 public:
490 void SetUp() {
Felipe Leme46b85da2016-11-21 17:40:45 -0800491 DumpstateBaseTest::SetUp();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700492 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700493 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700494 ds.progress_.reset(new Progress());
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100495 ds.options_.reset(new Dumpstate::DumpOptions());
Felipe Leme4c2d6632016-09-28 14:32:00 -0700496 }
497
498 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700499 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700500 const CommandOptions& options = CommandOptions::DEFAULT) {
501 CaptureStdout();
502 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700503 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700504 out = GetCapturedStdout();
505 err = GetCapturedStderr();
506 return status;
507 }
508
Felipe Lemecef02982016-10-03 17:22:22 -0700509 // Dumps a file and capture `stdout` and `stderr`.
510 int DumpFile(const std::string& title, const std::string& path) {
511 CaptureStdout();
512 CaptureStderr();
513 int status = ds.DumpFile(title, path);
514 out = GetCapturedStdout();
515 err = GetCapturedStderr();
516 return status;
517 }
518
Nandana Dutt402a8392019-06-14 14:25:13 +0100519 void SetProgress(long progress, long initial_max) {
520 ds.last_reported_percent_progress_ = 0;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100521 ds.options_->do_progress_updates = true;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700522 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
523 }
524
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100525 std::string GetProgressMessage(int progress, int max,
526 int old_max = 0, bool update_progress = true) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700527 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
528 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700529
Felipe Leme7447d7c2016-11-03 18:12:22 -0700530 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700531
Felipe Leme009ecbb2016-11-07 10:18:44 -0800532 std::string message = "";
Felipe Leme75876a22016-10-27 16:31:27 -0700533 if (max_increased) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800534 message =
Felipe Leme7447d7c2016-11-03 18:12:22 -0700535 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700536 }
537
Felipe Leme009ecbb2016-11-07 10:18:44 -0800538 if (update_progress) {
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100539 message += android::base::StringPrintf("Setting progress: %d/%d (%d%%)\n",
540 progress, max, (100 * progress / max));
Felipe Leme009ecbb2016-11-07 10:18:44 -0800541 }
542
543 return message;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700544 }
545
Felipe Leme4c2d6632016-09-28 14:32:00 -0700546 // `stdout` and `stderr` from the last command ran.
547 std::string out, err;
548
Felipe Lemefd8affa2016-09-30 17:38:57 -0700549 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700550};
551
552TEST_F(DumpstateTest, RunCommandNoArgs) {
553 EXPECT_EQ(-1, RunCommand("", {}));
554}
555
556TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700557 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700558 EXPECT_THAT(out, StrEq("stdout\n"));
559 EXPECT_THAT(err, StrEq("stderr\n"));
560}
561
562TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700563 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700564 EXPECT_THAT(err, StrEq("stderr\n"));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700565 // The duration may not get output, depending on how long it takes,
566 // so we just check the prefix.
Felipe Lemefd8affa2016-09-30 17:38:57 -0700567 EXPECT_THAT(out,
Nandana Dutt47527b52019-03-29 15:34:36 +0000568 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700569}
570
Felipe Lemefd8affa2016-09-30 17:38:57 -0700571TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700572 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700573 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700574 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
575 EXPECT_THAT(out, StrEq("stdout\n"));
576 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
577}
578
579TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700580 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700581 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700582 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700583 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700584}
585
586TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700587 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700588 EXPECT_THAT(err, IsEmpty());
589 EXPECT_THAT(out, StrEq("one\n"));
590}
591
Felipe Lemefd8affa2016-09-30 17:38:57 -0700592TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700593 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700594 EXPECT_THAT(err, IsEmpty());
595 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
596}
597
598TEST_F(DumpstateTest, RunCommandDryRun) {
599 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700600 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700601 // The duration may not get output, depending on how long it takes,
602 // so we just check the prefix.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700603 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Nandana Dutt47527b52019-03-29 15:34:36 +0000604 ") ------\n\t(skipped on dry run)\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700605 EXPECT_THAT(err, IsEmpty());
606}
607
608TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
609 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700610 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700611 EXPECT_THAT(out, IsEmpty());
612 EXPECT_THAT(err, IsEmpty());
613}
614
615TEST_F(DumpstateTest, RunCommandDryRunAlways) {
616 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700617 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700618 EXPECT_THAT(out, StrEq("stdout\n"));
619 EXPECT_THAT(err, StrEq("stderr\n"));
620}
621
Felipe Lemefd8affa2016-09-30 17:38:57 -0700622TEST_F(DumpstateTest, RunCommandNotFound) {
623 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
624 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
625 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
626}
627
628TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700629 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
630 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700631 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700632 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700633 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700634}
635
636TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700637 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700638 // We don't know the exit code, so check just the prefix.
639 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700640 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700641 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700642 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700643}
644
645TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700646 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700647 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700648 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700649 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700650 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700651 " --sleep 2' timed out after 1"));
652}
653
654TEST_F(DumpstateTest, RunCommandIsKilled) {
655 CaptureStdout();
656 CaptureStderr();
657
658 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700659 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700660 CommandOptions::WithTimeout(100).Always().Build()));
661 });
662
663 // Capture pid and pre-sleep output.
664 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
665 std::string err = GetCapturedStderr();
666 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
667
668 std::string out = GetCapturedStdout();
669 std::vector<std::string> lines = android::base::Split(out, "\n");
670 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
671
672 int pid = atoi(lines[0].c_str());
673 EXPECT_THAT(lines[1], StrEq("stdout line1"));
674 EXPECT_THAT(lines[2], IsEmpty()); // \n
675
676 // Then kill the process.
677 CaptureStdout();
678 CaptureStderr();
679 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
680 t.join();
681
682 // Finally, check output after murder.
683 out = GetCapturedStdout();
684 err = GetCapturedStderr();
685
Felipe Leme7447d7c2016-11-03 18:12:22 -0700686 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700687 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700688 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700689 " --pid --sleep 20' failed: killed by signal 15\n"));
690}
691
Felipe Leme75876a22016-10-27 16:31:27 -0700692TEST_F(DumpstateTest, RunCommandProgress) {
693 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
694 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700695 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700696
Nandana Duttbabf6c72019-01-15 14:11:12 +0000697 EXPECT_CALL(*listener, onProgress(66)); // 20/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700698 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100699 std::string progress_message = GetProgressMessage(20, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700700 EXPECT_THAT(out, StrEq("stdout\n"));
701 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
702
Nandana Dutt402a8392019-06-14 14:25:13 +0100703 EXPECT_CALL(*listener, onProgress(80)); // 24/30 %
704 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100705 progress_message = GetProgressMessage(24, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700706 EXPECT_THAT(out, StrEq("stdout\n"));
707 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
708
709 // Make sure command ran while in dry_run is counted.
710 SetDryRun(true);
Nandana Dutt402a8392019-06-14 14:25:13 +0100711 EXPECT_CALL(*listener, onProgress(90)); // 27/30 %
712 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100713 progress_message = GetProgressMessage(27, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700714 EXPECT_THAT(out, IsEmpty());
715 EXPECT_THAT(err, StrEq(progress_message));
716
Nandana Dutt402a8392019-06-14 14:25:13 +0100717 SetDryRun(false);
718 EXPECT_CALL(*listener, onProgress(96)); // 29/30 %
719 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(2).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100720 progress_message = GetProgressMessage(29, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800721 EXPECT_THAT(out, StrEq("stdout\n"));
722 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
723
Nandana Dutt402a8392019-06-14 14:25:13 +0100724 EXPECT_CALL(*listener, onProgress(100)); // 30/30 %
Felipe Leme009ecbb2016-11-07 10:18:44 -0800725 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100726 progress_message = GetProgressMessage(30, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800727 EXPECT_THAT(out, StrEq("stdout\n"));
728 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
729
730 ds.listener_.clear();
731}
732
Felipe Lemed80e6b62016-10-03 13:08:14 -0700733TEST_F(DumpstateTest, RunCommandDropRoot) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800734 if (!IsStandalone()) {
735 // TODO: temporarily disabled because it might cause other tests to fail after dropping
736 // to Shell - need to refactor tests to avoid this problem)
737 MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n")
738 return;
739 }
Felipe Lemed80e6b62016-10-03 13:08:14 -0700740 // First check root case - only available when running with 'adb root'.
741 uid_t uid = getuid();
742 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700743 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700744 EXPECT_THAT(out, StrEq("0\nstdout\n"));
745 EXPECT_THAT(err, StrEq("stderr\n"));
746 return;
747 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700748 // Then run dropping root.
749 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700750 CommandOptions::WithTimeout(1).DropRoot().Build()));
751 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700752 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700753}
754
755TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800756 if (!IsStandalone()) {
757 // TODO: temporarily disabled because it might cause other tests to fail after dropping
758 // to Shell - need to refactor tests to avoid this problem)
759 MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n")
760 return;
761 }
Felipe Lemef0292972016-11-22 13:57:05 -0800762 if (!PropertiesHelper::IsUserBuild()) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700763 // Emulates user build if necessarily.
764 SetBuildType("user");
765 }
766
767 DropRoot();
768
Felipe Leme7447d7c2016-11-03 18:12:22 -0700769 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700770
771 // We don't know the exact path of su, so we just check for the 'root ...' commands
772 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700773 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700774 EXPECT_THAT(err, IsEmpty());
775}
776
Felipe Leme46b85da2016-11-21 17:40:45 -0800777TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
778 if (!IsStandalone()) {
779 // TODO: temporarily disabled because it might cause other tests to fail after dropping
780 // to Shell - need to refactor tests to avoid this problem)
781 MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n")
782 return;
783 }
Felipe Lemef0292972016-11-22 13:57:05 -0800784 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800785 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
786 return;
787 }
788
789 DropRoot();
790
791 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
792 CommandOptions::WithTimeout(1).AsRoot().Build()));
793
794 EXPECT_THAT(out, StrEq("0\nstdout\n"));
795 EXPECT_THAT(err, StrEq("stderr\n"));
796}
797
Nandana Dutt4b392be2018-11-02 16:17:05 +0000798TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild_withUnroot) {
799 if (!IsStandalone()) {
800 // TODO: temporarily disabled because it might cause other tests to fail after dropping
801 // to Shell - need to refactor tests to avoid this problem)
802 MYLOGE(
803 "Skipping DumpstateTest.RunCommandAsRootNonUserBuild_withUnroot() "
804 "on test suite\n")
805 return;
806 }
807 if (PropertiesHelper::IsUserBuild()) {
808 ALOGI("Skipping RunCommandAsRootNonUserBuild_withUnroot on user builds\n");
809 return;
810 }
811
812 // Same test as above, but with unroot property set, which will override su availability.
813 SetUnroot(true);
814 DropRoot();
815
816 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
817 CommandOptions::WithTimeout(1).AsRoot().Build()));
818
819 // AsRoot is ineffective.
820 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
821 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
822}
823
Yifan Hong48e83a12017-10-03 14:10:07 -0700824TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) {
825 if (!IsStandalone()) {
826 // TODO: temporarily disabled because it might cause other tests to fail after dropping
827 // to Shell - need to refactor tests to avoid this problem)
828 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
829 return;
830 }
831 if (!PropertiesHelper::IsUserBuild()) {
832 // Emulates user build if necessarily.
833 SetBuildType("user");
834 }
835
836 DropRoot();
837
838 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
839 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
840
841 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
842 EXPECT_THAT(err, StrEq("stderr\n"));
843}
844
845TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) {
846 if (!IsStandalone()) {
847 // TODO: temporarily disabled because it might cause other tests to fail after dropping
848 // to Shell - need to refactor tests to avoid this problem)
849 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
850 return;
851 }
852 if (PropertiesHelper::IsUserBuild()) {
853 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
854 return;
855 }
856
857 DropRoot();
858
859 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
860 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
861
862 EXPECT_THAT(out, StrEq("0\nstdout\n"));
863 EXPECT_THAT(err, StrEq("stderr\n"));
864}
865
Nandana Dutt4b392be2018-11-02 16:17:05 +0000866TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild_withUnroot) {
867 if (!IsStandalone()) {
868 // TODO: temporarily disabled because it might cause other tests to fail after dropping
869 // to Shell - need to refactor tests to avoid this problem)
870 MYLOGE(
871 "Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild_withUnroot() "
872 "on test suite\n")
873 return;
874 }
875 if (PropertiesHelper::IsUserBuild()) {
876 ALOGI("Skipping RunCommandAsRootIfAvailableOnDebugBuild_withUnroot on user builds\n");
877 return;
878 }
879 // Same test as above, but with unroot property set, which will override su availability.
880 SetUnroot(true);
881
882 DropRoot();
883
884 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
885 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
886
887 // It's a userdebug build, so "su root" should be available, but unroot=true overrides it.
888 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
889 EXPECT_THAT(err, StrEq("stderr\n"));
890}
891
Felipe Lemecef02982016-10-03 17:22:22 -0700892TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
893 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
894 EXPECT_THAT(out,
895 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
896 EXPECT_THAT(err, IsEmpty());
897}
898
899TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
900 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
901 EXPECT_THAT(err, IsEmpty());
Greg Kaiser3a811c12019-05-21 12:48:59 -0700902 // The duration may not get output, depending on how long it takes,
903 // so we just check the prefix.
Felipe Lemecef02982016-10-03 17:22:22 -0700904 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
905 "such file or directory\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700906}
907
908TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700909 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700910 EXPECT_THAT(err, IsEmpty());
911 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
912}
913
914TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700915 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700916 EXPECT_THAT(err, IsEmpty());
917 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
918}
919
920TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700921 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700922 EXPECT_THAT(err, IsEmpty());
923 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
924}
925
926TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700927 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700928 EXPECT_THAT(err, IsEmpty());
929 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
930}
931
932TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
933 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700934 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700935 EXPECT_THAT(err, IsEmpty());
936 EXPECT_THAT(out, IsEmpty());
937}
938
939TEST_F(DumpstateTest, DumpFileOnDryRun) {
940 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700941 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700942 EXPECT_THAT(err, IsEmpty());
Felipe Leme46b85da2016-11-21 17:40:45 -0800943 EXPECT_THAT(
944 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
Nandana Dutt47527b52019-03-29 15:34:36 +0000945 EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700946}
947
Felipe Leme75876a22016-10-27 16:31:27 -0700948TEST_F(DumpstateTest, DumpFileUpdateProgress) {
949 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
950 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700951 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700952
Nandana Duttbabf6c72019-01-15 14:11:12 +0000953 EXPECT_CALL(*listener, onProgress(16)); // 5/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700954 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -0700955
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100956 std::string progress_message = GetProgressMessage(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
Felipe Leme75876a22016-10-27 16:31:27 -0700957 EXPECT_THAT(err, StrEq(progress_message));
958 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
959
960 ds.listener_.clear();
961}
962
Felipe Leme7447d7c2016-11-03 18:12:22 -0700963class DumpstateServiceTest : public DumpstateBaseTest {
Felipe Leme75876a22016-10-27 16:31:27 -0700964 public:
965 DumpstateService dss;
966};
967
Felipe Leme7447d7c2016-11-03 18:12:22 -0700968class ProgressTest : public DumpstateBaseTest {
969 public:
970 Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {
971 return Progress(max, growth_factor, path);
972 }
973
974 void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) {
975 std::string expected_content =
976 android::base::StringPrintf("%d %d\n", expected_runs, expected_average);
977 std::string actual_content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800978 ReadFileToString(path, &actual_content);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700979 ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path;
980 }
981};
982
983TEST_F(ProgressTest, SimpleTest) {
984 Progress progress;
985 EXPECT_EQ(0, progress.Get());
986 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
987 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
988
989 bool max_increased = progress.Inc(1);
990 EXPECT_EQ(1, progress.Get());
991 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
992 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
993 EXPECT_FALSE(max_increased);
994
995 // Ignore negative increase.
996 max_increased = progress.Inc(-1);
997 EXPECT_EQ(1, progress.Get());
998 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
999 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1000 EXPECT_FALSE(max_increased);
1001}
1002
1003TEST_F(ProgressTest, MaxGrowsInsideNewRange) {
1004 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1005 EXPECT_EQ(0, progress.Get());
1006 EXPECT_EQ(10, progress.GetInitialMax());
1007 EXPECT_EQ(10, progress.GetMax());
1008
1009 // No increase
1010 bool max_increased = progress.Inc(10);
1011 EXPECT_EQ(10, progress.Get());
1012 EXPECT_EQ(10, progress.GetMax());
1013 EXPECT_FALSE(max_increased);
1014
1015 // Increase, with new value < max*20%
1016 max_increased = progress.Inc(1);
1017 EXPECT_EQ(11, progress.Get());
1018 EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13
1019 EXPECT_TRUE(max_increased);
1020}
1021
1022TEST_F(ProgressTest, MaxGrowsOutsideNewRange) {
1023 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1024 EXPECT_EQ(0, progress.Get());
1025 EXPECT_EQ(10, progress.GetInitialMax());
1026 EXPECT_EQ(10, progress.GetMax());
1027
1028 // No increase
1029 bool max_increased = progress.Inc(10);
1030 EXPECT_EQ(10, progress.Get());
1031 EXPECT_EQ(10, progress.GetMax());
1032 EXPECT_FALSE(max_increased);
1033
1034 // Increase, with new value > max*20%
1035 max_increased = progress.Inc(5);
1036 EXPECT_EQ(15, progress.Get());
1037 EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18
1038 EXPECT_TRUE(max_increased);
1039}
1040
1041TEST_F(ProgressTest, InvalidPath) {
1042 Progress progress("/devil/null");
1043 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1044}
1045
1046TEST_F(ProgressTest, EmptyFile) {
1047 Progress progress(CopyTextFileFixture("empty-file.txt"));
1048 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1049}
1050
1051TEST_F(ProgressTest, InvalidLine1stEntryNAN) {
1052 Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt"));
1053 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1054}
1055
1056TEST_F(ProgressTest, InvalidLine2ndEntryNAN) {
1057 Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt"));
1058 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1059}
1060
1061TEST_F(ProgressTest, InvalidLineBothNAN) {
1062 Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt"));
1063 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1064}
1065
1066TEST_F(ProgressTest, InvalidLine1stEntryNegative) {
1067 Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt"));
1068 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1069}
1070
1071TEST_F(ProgressTest, InvalidLine2ndEntryNegative) {
1072 Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt"));
1073 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1074}
1075
1076TEST_F(ProgressTest, InvalidLine1stEntryTooBig) {
1077 Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt"));
1078 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1079}
1080
1081TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) {
1082 Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt"));
1083 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1084}
1085
1086// Tests stats are properly saved when the file does not exists.
1087TEST_F(ProgressTest, FirstTime) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001088 if (!IsStandalone()) {
1089 // TODO: temporarily disabled because it's failing when running as suite
1090 MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n")
1091 return;
1092 }
1093
Felipe Leme7447d7c2016-11-03 18:12:22 -07001094 std::string path = kTestDataPath + "FirstTime.txt";
1095 android::base::RemoveFileIfExists(path);
1096
1097 Progress run1(path);
1098 EXPECT_EQ(0, run1.Get());
1099 EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax());
1100 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1101
1102 bool max_increased = run1.Inc(20);
1103 EXPECT_EQ(20, run1.Get());
1104 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1105 EXPECT_FALSE(max_increased);
1106
1107 run1.Save();
1108 AssertStats(path, 1, 20);
1109}
1110
1111// Tests what happens when the persistent settings contains the average duration of 1 run.
1112// Data on file is 1 run and 109 average.
1113TEST_F(ProgressTest, SecondTime) {
1114 std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt");
1115
1116 Progress run1 = GetInstance(-42, 1.2, path);
1117 EXPECT_EQ(0, run1.Get());
1118 EXPECT_EQ(10, run1.GetInitialMax());
1119 EXPECT_EQ(10, run1.GetMax());
1120
1121 bool max_increased = run1.Inc(20);
1122 EXPECT_EQ(20, run1.Get());
1123 EXPECT_EQ(24, run1.GetMax());
1124 EXPECT_TRUE(max_increased);
1125
1126 // Average now is 2 runs and (10 + 20)/ 2 = 15
1127 run1.Save();
1128 AssertStats(path, 2, 15);
1129
1130 Progress run2 = GetInstance(-42, 1.2, path);
1131 EXPECT_EQ(0, run2.Get());
1132 EXPECT_EQ(15, run2.GetInitialMax());
1133 EXPECT_EQ(15, run2.GetMax());
1134
1135 max_increased = run2.Inc(25);
1136 EXPECT_EQ(25, run2.Get());
1137 EXPECT_EQ(30, run2.GetMax());
1138 EXPECT_TRUE(max_increased);
1139
1140 // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18
1141 run2.Save();
1142 AssertStats(path, 3, 18);
1143
1144 Progress run3 = GetInstance(-42, 1.2, path);
1145 EXPECT_EQ(0, run3.Get());
1146 EXPECT_EQ(18, run3.GetInitialMax());
1147 EXPECT_EQ(18, run3.GetMax());
1148
1149 // Make sure average decreases as well
1150 max_increased = run3.Inc(5);
1151 EXPECT_EQ(5, run3.Get());
1152 EXPECT_EQ(18, run3.GetMax());
1153 EXPECT_FALSE(max_increased);
1154
1155 // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14
1156 run3.Save();
1157 AssertStats(path, 4, 14);
1158}
1159
1160// Tests what happens when the persistent settings contains the average duration of 2 runs.
1161// Data on file is 2 runs and 15 average.
1162TEST_F(ProgressTest, ThirdTime) {
1163 std::string path = CopyTextFileFixture("stats-two-runs.txt");
1164 AssertStats(path, 2, 15); // Sanity check
1165
1166 Progress run1 = GetInstance(-42, 1.2, path);
1167 EXPECT_EQ(0, run1.Get());
1168 EXPECT_EQ(15, run1.GetInitialMax());
1169 EXPECT_EQ(15, run1.GetMax());
1170
1171 bool max_increased = run1.Inc(20);
1172 EXPECT_EQ(20, run1.Get());
1173 EXPECT_EQ(24, run1.GetMax());
1174 EXPECT_TRUE(max_increased);
1175
1176 // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16
1177 run1.Save();
1178 AssertStats(path, 3, 16);
1179}
1180
Felipe Leme46b85da2016-11-21 17:40:45 -08001181class DumpstateUtilTest : public DumpstateBaseTest {
1182 public:
1183 void SetUp() {
1184 DumpstateBaseTest::SetUp();
1185 SetDryRun(false);
1186 }
1187
Felipe Leme46b85da2016-11-21 17:40:45 -08001188 void CaptureFdOut() {
Felipe Lemef0292972016-11-22 13:57:05 -08001189 ReadFileToString(path_, &out);
Felipe Leme46b85da2016-11-21 17:40:45 -08001190 }
1191
1192 void CreateFd(const std::string& name) {
1193 path_ = kTestDataPath + name;
1194 MYLOGD("Creating fd for file %s\n", path_.c_str());
1195
1196 fd = TEMP_FAILURE_RETRY(open(path_.c_str(),
1197 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
1198 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
1199 ASSERT_GE(fd, 0) << "could not create FD for path " << path_;
1200 }
1201
1202 // Runs a command into the `fd` and capture `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001203 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme46b85da2016-11-21 17:40:45 -08001204 const CommandOptions& options = CommandOptions::DEFAULT) {
1205 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001206 int status = RunCommandToFd(fd, title, full_command, options);
Felipe Leme46b85da2016-11-21 17:40:45 -08001207 close(fd);
1208
1209 CaptureFdOut();
1210 err = GetCapturedStderr();
1211 return status;
1212 }
1213
1214 // Dumps a file and into the `fd` and `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001215 int DumpFile(const std::string& title, const std::string& path) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001216 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001217 int status = DumpFileToFd(fd, title, path);
Felipe Leme46b85da2016-11-21 17:40:45 -08001218 close(fd);
1219
1220 CaptureFdOut();
1221 err = GetCapturedStderr();
1222 return status;
1223 }
1224
1225 int fd;
1226
1227 // 'fd` output and `stderr` from the last command ran.
1228 std::string out, err;
1229
1230 private:
1231 std::string path_;
1232};
1233
1234TEST_F(DumpstateUtilTest, RunCommandNoArgs) {
Felipe Lemef0292972016-11-22 13:57:05 -08001235 CreateFd("RunCommandNoArgs.txt");
1236 EXPECT_EQ(-1, RunCommand("", {}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001237}
1238
Felipe Lemef0292972016-11-22 13:57:05 -08001239TEST_F(DumpstateUtilTest, RunCommandNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001240 CreateFd("RunCommandWithNoArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001241 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001242 EXPECT_THAT(out, StrEq("stdout\n"));
1243 EXPECT_THAT(err, StrEq("stderr\n"));
1244}
1245
Felipe Lemef0292972016-11-22 13:57:05 -08001246TEST_F(DumpstateUtilTest, RunCommandWithTitle) {
1247 CreateFd("RunCommandWithNoArgs.txt");
1248 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1249 EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
1250 EXPECT_THAT(err, StrEq("stderr\n"));
1251}
1252
Felipe Leme46b85da2016-11-21 17:40:45 -08001253TEST_F(DumpstateUtilTest, RunCommandWithOneArg) {
1254 CreateFd("RunCommandWithOneArg.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001255 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001256 EXPECT_THAT(err, IsEmpty());
1257 EXPECT_THAT(out, StrEq("one\n"));
1258}
1259
1260TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) {
1261 CreateFd("RunCommandWithMultipleArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001262 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001263 EXPECT_THAT(err, IsEmpty());
1264 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
1265}
1266
1267TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) {
1268 CreateFd("RunCommandWithLoggingMessage.txt");
1269 EXPECT_EQ(
Felipe Lemef0292972016-11-22 13:57:05 -08001270 0, RunCommand("", {kSimpleCommand},
Felipe Leme46b85da2016-11-21 17:40:45 -08001271 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
1272 EXPECT_THAT(out, StrEq("stdout\n"));
1273 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
1274}
1275
1276TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) {
1277 CreateFd("RunCommandRedirectStderr.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001278 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
1279 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001280 EXPECT_THAT(out, IsEmpty());
1281 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
1282}
1283
1284TEST_F(DumpstateUtilTest, RunCommandDryRun) {
1285 CreateFd("RunCommandDryRun.txt");
1286 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001287 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1288 EXPECT_THAT(out, StrEq(android::base::StringPrintf(
1289 "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n",
1290 kSimpleCommand.c_str())));
1291 EXPECT_THAT(err, IsEmpty());
1292}
1293
1294TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) {
1295 CreateFd("RunCommandDryRun.txt");
1296 SetDryRun(true);
1297 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001298 EXPECT_THAT(
1299 out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str())));
1300 EXPECT_THAT(err, IsEmpty());
1301}
1302
1303TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) {
1304 CreateFd("RunCommandDryRunAlways.txt");
1305 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001306 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001307 EXPECT_THAT(out, StrEq("stdout\n"));
1308 EXPECT_THAT(err, StrEq("stderr\n"));
1309}
1310
1311TEST_F(DumpstateUtilTest, RunCommandNotFound) {
1312 CreateFd("RunCommandNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001313 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001314 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
1315 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
1316}
1317
1318TEST_F(DumpstateUtilTest, RunCommandFails) {
1319 CreateFd("RunCommandFails.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001320 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001321 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
1322 " --exit 42' failed: exit code 42\n"));
1323 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
1324 " --exit 42' failed: exit code 42\n"));
1325}
1326
1327TEST_F(DumpstateUtilTest, RunCommandCrashes) {
1328 CreateFd("RunCommandCrashes.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001329 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001330 // We don't know the exit code, so check just the prefix.
1331 EXPECT_THAT(
1332 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1333 EXPECT_THAT(
1334 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1335}
1336
Vishnu Nair6921f802017-11-22 09:17:23 -08001337TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001338 CreateFd("RunCommandTimesout.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001339 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1340 CommandOptions::WithTimeout(1).Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001341 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1342 " --sleep 2' timed out after 1"));
1343 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1344 " --sleep 2' timed out after 1"));
1345}
1346
Vishnu Nair6921f802017-11-22 09:17:23 -08001347TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) {
1348 CreateFd("RunCommandTimesout.txt");
1349 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1350 CommandOptions::WithTimeoutInMs(1000).Build()));
1351 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1352 " --sleep 2' timed out after 1"));
1353 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1354 " --sleep 2' timed out after 1"));
1355}
1356
1357
Felipe Leme46b85da2016-11-21 17:40:45 -08001358TEST_F(DumpstateUtilTest, RunCommandIsKilled) {
1359 CreateFd("RunCommandIsKilled.txt");
1360 CaptureStderr();
1361
1362 std::thread t([=]() {
Felipe Lemef0292972016-11-22 13:57:05 -08001363 EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001364 CommandOptions::WithTimeout(100).Always().Build()));
1365 });
1366
1367 // Capture pid and pre-sleep output.
1368 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
1369 std::string err = GetCapturedStderr();
1370 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
1371
1372 CaptureFdOut();
1373 std::vector<std::string> lines = android::base::Split(out, "\n");
1374 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
1375
1376 int pid = atoi(lines[0].c_str());
1377 EXPECT_THAT(lines[1], StrEq("stdout line1"));
1378 EXPECT_THAT(lines[2], IsEmpty()); // \n
1379
1380 // Then kill the process.
1381 CaptureFdOut();
1382 CaptureStderr();
1383 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
1384 t.join();
1385
1386 // Finally, check output after murder.
1387 CaptureFdOut();
1388 err = GetCapturedStderr();
1389
1390 // out starts with the pid, which is an unknown
1391 EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand +
1392 " --pid --sleep 20' failed: killed by signal 15\n"));
1393 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
1394 " --pid --sleep 20' failed: killed by signal 15\n"));
1395}
1396
1397TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) {
1398 if (!IsStandalone()) {
1399 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1400 // to Shell - need to refactor tests to avoid this problem)
1401 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n")
1402 return;
1403 }
1404 CreateFd("RunCommandAsRootUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001405 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001406 // Emulates user build if necessarily.
1407 SetBuildType("user");
1408 }
1409
1410 DropRoot();
1411
Felipe Lemef0292972016-11-22 13:57:05 -08001412 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001413
1414 // We don't know the exact path of su, so we just check for the 'root ...' commands
1415 EXPECT_THAT(out, StartsWith("Skipping"));
1416 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
1417 EXPECT_THAT(err, IsEmpty());
1418}
1419
1420TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) {
1421 if (!IsStandalone()) {
1422 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1423 // to Shell - need to refactor tests to avoid this problem)
1424 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n")
1425 return;
1426 }
1427 CreateFd("RunCommandAsRootNonUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001428 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001429 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1430 return;
1431 }
1432
1433 DropRoot();
1434
Felipe Lemef0292972016-11-22 13:57:05 -08001435 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1436 CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001437
1438 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1439 EXPECT_THAT(err, StrEq("stderr\n"));
1440}
Felipe Leme46b85da2016-11-21 17:40:45 -08001441
Yifan Hong48e83a12017-10-03 14:10:07 -07001442
1443TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) {
1444 if (!IsStandalone()) {
1445 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1446 // to Shell - need to refactor tests to avoid this problem)
1447 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
1448 return;
1449 }
1450 CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt");
1451 if (!PropertiesHelper::IsUserBuild()) {
1452 // Emulates user build if necessarily.
1453 SetBuildType("user");
1454 }
1455
1456 DropRoot();
1457
1458 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1459 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1460
1461 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1462 EXPECT_THAT(err, StrEq("stderr\n"));
1463}
1464
1465TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) {
1466 if (!IsStandalone()) {
1467 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1468 // to Shell - need to refactor tests to avoid this problem)
1469 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
1470 return;
1471 }
1472 CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt");
1473 if (PropertiesHelper::IsUserBuild()) {
1474 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1475 return;
1476 }
1477
1478 DropRoot();
1479
1480 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1481 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1482
1483 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1484 EXPECT_THAT(err, StrEq("stderr\n"));
1485}
1486
Felipe Leme46b85da2016-11-21 17:40:45 -08001487TEST_F(DumpstateUtilTest, RunCommandDropRoot) {
1488 if (!IsStandalone()) {
1489 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1490 // to Shell - need to refactor tests to avoid this problem)
1491 MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n")
1492 return;
1493 }
1494 CreateFd("RunCommandDropRoot.txt");
1495 // First check root case - only available when running with 'adb root'.
1496 uid_t uid = getuid();
1497 if (uid == 0) {
Felipe Lemef0292972016-11-22 13:57:05 -08001498 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001499 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1500 EXPECT_THAT(err, StrEq("stderr\n"));
1501 return;
1502 }
1503 // Then run dropping root.
Felipe Lemef0292972016-11-22 13:57:05 -08001504 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001505 CommandOptions::WithTimeout(1).DropRoot().Build()));
1506 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1507 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
1508}
1509
Felipe Lemef0292972016-11-22 13:57:05 -08001510TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001511 CreateFd("DumpFileNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001512 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001513 EXPECT_THAT(out,
1514 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
1515 EXPECT_THAT(err, IsEmpty());
1516}
1517
Felipe Lemef0292972016-11-22 13:57:05 -08001518TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) {
1519 CreateFd("DumpFileNotFound.txt");
1520 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
1521 EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such "
1522 "file or directory\n"));
1523 EXPECT_THAT(err, IsEmpty());
1524}
1525
Felipe Leme46b85da2016-11-21 17:40:45 -08001526TEST_F(DumpstateUtilTest, DumpFileSingleLine) {
1527 CreateFd("DumpFileSingleLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001528 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001529 EXPECT_THAT(err, IsEmpty());
1530 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1531}
1532
1533TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) {
1534 CreateFd("DumpFileSingleLineWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001535 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001536 EXPECT_THAT(err, IsEmpty());
1537 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
1538}
1539
1540TEST_F(DumpstateUtilTest, DumpFileMultipleLines) {
1541 CreateFd("DumpFileMultipleLines.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001542 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001543 EXPECT_THAT(err, IsEmpty());
1544 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1545}
1546
1547TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) {
1548 CreateFd("DumpFileMultipleLinesWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001549 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001550 EXPECT_THAT(err, IsEmpty());
1551 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1552}
1553
Felipe Lemef0292972016-11-22 13:57:05 -08001554TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) {
1555 CreateFd("DumpFileOnDryRun.txt");
1556 SetDryRun(true);
1557 std::string path = kTestDataPath + "single-line.txt";
1558 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
1559 EXPECT_THAT(err, IsEmpty());
1560 EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n"));
1561}
1562
Felipe Leme46b85da2016-11-21 17:40:45 -08001563TEST_F(DumpstateUtilTest, DumpFileOnDryRun) {
1564 CreateFd("DumpFileOnDryRun.txt");
1565 SetDryRun(true);
1566 std::string path = kTestDataPath + "single-line.txt";
Felipe Lemef0292972016-11-22 13:57:05 -08001567 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001568 EXPECT_THAT(err, IsEmpty());
Felipe Lemef0292972016-11-22 13:57:05 -08001569 EXPECT_THAT(
1570 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
1571 EXPECT_THAT(out, EndsWith("skipped on dry run\n"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001572}
Ecco Park61ffcf72016-10-27 15:46:26 -07001573
Felipe Leme47e9be22016-12-21 15:37:07 -08001574} // namespace dumpstate
1575} // namespace os
1576} // namespace android