blob: 9871a3bd6c0e99b57a2ce229e951850396d99da8 [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));
Paul Changc490e662020-04-11 18:14:09 +080069 MOCK_METHOD1(onUiIntensiveBugreportDumpsFinished,
70 binder::Status(const android::String16& callingpackage));
Felipe Leme75876a22016-10-27 16:31:27 -070071
72 protected:
73 MOCK_METHOD0(onAsBinder, IBinder*());
74};
75
Felipe Leme46b85da2016-11-21 17:40:45 -080076static int calls_;
77
Felipe Leme7447d7c2016-11-03 18:12:22 -070078// Base class for all tests in this file
79class DumpstateBaseTest : public Test {
Felipe Leme46b85da2016-11-21 17:40:45 -080080 public:
81 virtual void SetUp() override {
82 calls_++;
Felipe Lemef0292972016-11-22 13:57:05 -080083 SetDryRun(false);
Felipe Leme46b85da2016-11-21 17:40:45 -080084 }
85
Felipe Lemef0292972016-11-22 13:57:05 -080086 void SetDryRun(bool dry_run) const {
87 PropertiesHelper::dry_run_ = dry_run;
88 }
89
90 void SetBuildType(const std::string& build_type) const {
91 PropertiesHelper::build_type_ = build_type;
92 }
93
Nandana Dutt4b392be2018-11-02 16:17:05 +000094 void SetUnroot(bool unroot) const {
95 PropertiesHelper::unroot_ = unroot;
96 }
97
Felipe Lemef0292972016-11-22 13:57:05 -080098 bool IsStandalone() const {
Felipe Leme46b85da2016-11-21 17:40:45 -080099 return calls_ == 1;
100 }
101
Felipe Lemef0292972016-11-22 13:57:05 -0800102 void DropRoot() const {
103 DropRootUser();
Felipe Leme46b85da2016-11-21 17:40:45 -0800104 uid_t uid = getuid();
105 ASSERT_EQ(2000, (int)uid);
106 }
107
Felipe Leme7447d7c2016-11-03 18:12:22 -0700108 protected:
109 const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str());
Dan Shie177e8e2019-06-20 11:08:14 -0700110 const std::string kTestDataPath = kTestPath + "/tests/testdata/";
111 const std::string kSimpleCommand = kTestPath + "/dumpstate_test_fixture";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700112 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() {
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000157 }
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100158 Dumpstate::DumpOptions options_;
Abhijeet Kaure370d682019-10-01 16:49:30 +0100159 android::base::unique_fd fd;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100160};
161
162TEST_F(DumpOptionsTest, InitializeNone) {
163 // clang-format off
164 char* argv[] = {
165 const_cast<char*>("dumpstate")
166 };
167 // clang-format on
168
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100169 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
170
171 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000172
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100173 EXPECT_FALSE(options_.do_add_date);
174 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100175 EXPECT_FALSE(options_.use_socket);
176 EXPECT_FALSE(options_.use_control_socket);
177 EXPECT_FALSE(options_.show_header_only);
178 EXPECT_TRUE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800179 EXPECT_FALSE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100180 EXPECT_FALSE(options_.do_progress_updates);
181 EXPECT_FALSE(options_.is_remote_mode);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800182 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000183}
184
185TEST_F(DumpOptionsTest, InitializeAdbBugreport) {
186 // clang-format off
187 char* argv[] = {
188 const_cast<char*>("dumpstatez"),
189 const_cast<char*>("-S"),
190 const_cast<char*>("-d"),
191 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000192 };
193 // clang-format on
194
195 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
196
197 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
198 EXPECT_TRUE(options_.do_add_date);
199 EXPECT_TRUE(options_.do_zip_file);
200 EXPECT_TRUE(options_.use_control_socket);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000201
202 // Other options retain default values
203 EXPECT_TRUE(options_.do_vibrate);
204 EXPECT_FALSE(options_.show_header_only);
Paul Chang0d2aad72020-02-13 20:04:03 +0800205 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000206 EXPECT_FALSE(options_.do_progress_updates);
207 EXPECT_FALSE(options_.is_remote_mode);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000208 EXPECT_FALSE(options_.use_socket);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800209 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000210}
211
212TEST_F(DumpOptionsTest, InitializeAdbShellBugreport) {
213 // clang-format off
214 char* argv[] = {
215 const_cast<char*>("dumpstate"),
216 const_cast<char*>("-s"),
217 };
218 // clang-format on
219
220 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
221
222 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
223 EXPECT_TRUE(options_.use_socket);
224
225 // Other options retain default values
226 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000227 EXPECT_FALSE(options_.do_add_date);
228 EXPECT_FALSE(options_.do_zip_file);
229 EXPECT_FALSE(options_.use_control_socket);
230 EXPECT_FALSE(options_.show_header_only);
Paul Chang0d2aad72020-02-13 20:04:03 +0800231 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000232 EXPECT_FALSE(options_.do_progress_updates);
233 EXPECT_FALSE(options_.is_remote_mode);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800234 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000235}
236
237TEST_F(DumpOptionsTest, InitializeFullBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800238 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_FULL, fd, fd, true);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000239 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800240 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000241 EXPECT_TRUE(options_.do_zip_file);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800242 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::FULL);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000243
244 // Other options retain default values
245 EXPECT_TRUE(options_.do_vibrate);
246 EXPECT_FALSE(options_.use_control_socket);
247 EXPECT_FALSE(options_.show_header_only);
248 EXPECT_FALSE(options_.do_progress_updates);
249 EXPECT_FALSE(options_.is_remote_mode);
250 EXPECT_FALSE(options_.use_socket);
251 EXPECT_FALSE(options_.do_start_service);
252}
253
254TEST_F(DumpOptionsTest, InitializeInteractiveBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800255 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE, fd, fd, true);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000256 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000257 EXPECT_TRUE(options_.do_zip_file);
258 EXPECT_TRUE(options_.do_progress_updates);
259 EXPECT_TRUE(options_.do_start_service);
Paul Chang0d2aad72020-02-13 20:04:03 +0800260 EXPECT_TRUE(options_.do_screenshot);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800261 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::INTERACTIVE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000262
263 // Other options retain default values
264 EXPECT_TRUE(options_.do_vibrate);
265 EXPECT_FALSE(options_.use_control_socket);
266 EXPECT_FALSE(options_.show_header_only);
267 EXPECT_FALSE(options_.is_remote_mode);
268 EXPECT_FALSE(options_.use_socket);
269}
270
271TEST_F(DumpOptionsTest, InitializeRemoteBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800272 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_REMOTE, fd, fd, false);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000273 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000274 EXPECT_TRUE(options_.do_zip_file);
275 EXPECT_TRUE(options_.is_remote_mode);
276 EXPECT_FALSE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800277 EXPECT_FALSE(options_.do_screenshot);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800278 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::REMOTE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000279
280 // Other options retain default values
281 EXPECT_FALSE(options_.use_control_socket);
282 EXPECT_FALSE(options_.show_header_only);
283 EXPECT_FALSE(options_.do_progress_updates);
284 EXPECT_FALSE(options_.use_socket);
285}
286
287TEST_F(DumpOptionsTest, InitializeWearBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800288 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WEAR, fd, fd, true);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000289 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800290 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000291 EXPECT_TRUE(options_.do_zip_file);
292 EXPECT_TRUE(options_.do_progress_updates);
293 EXPECT_TRUE(options_.do_start_service);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800294 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WEAR);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000295
296 // Other options retain default values
297 EXPECT_TRUE(options_.do_vibrate);
298 EXPECT_FALSE(options_.use_control_socket);
299 EXPECT_FALSE(options_.show_header_only);
300 EXPECT_FALSE(options_.is_remote_mode);
301 EXPECT_FALSE(options_.use_socket);
302}
303
304TEST_F(DumpOptionsTest, InitializeTelephonyBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800305 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_TELEPHONY, fd, fd, false);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000306 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800307 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000308 EXPECT_TRUE(options_.do_zip_file);
309 EXPECT_TRUE(options_.telephony_only);
Hunter Knepshield820f9bc2020-02-05 20:10:53 -0800310 EXPECT_TRUE(options_.do_progress_updates);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800311 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::CONNECTIVITY);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000312
313 // Other options retain default values
314 EXPECT_TRUE(options_.do_vibrate);
315 EXPECT_FALSE(options_.use_control_socket);
316 EXPECT_FALSE(options_.show_header_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000317 EXPECT_FALSE(options_.is_remote_mode);
318 EXPECT_FALSE(options_.use_socket);
319}
320
321TEST_F(DumpOptionsTest, InitializeWifiBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800322 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WIFI, fd, fd, false);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000323 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800324 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000325 EXPECT_TRUE(options_.do_zip_file);
326 EXPECT_TRUE(options_.wifi_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800327 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WIFI);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000328
329 // Other options retain default values
330 EXPECT_TRUE(options_.do_vibrate);
331 EXPECT_FALSE(options_.use_control_socket);
332 EXPECT_FALSE(options_.show_header_only);
333 EXPECT_FALSE(options_.do_progress_updates);
334 EXPECT_FALSE(options_.is_remote_mode);
335 EXPECT_FALSE(options_.use_socket);
336}
337
338TEST_F(DumpOptionsTest, InitializeDefaultBugReport) {
339 // default: commandline options are not overridden
340 // clang-format off
341 char* argv[] = {
342 const_cast<char*>("bugreport"),
343 const_cast<char*>("-d"),
344 const_cast<char*>("-p"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000345 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000346 };
347 // clang-format on
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000348 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
349
350 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
351 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800352 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000353 EXPECT_TRUE(options_.do_zip_file);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800354 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000355
356 // Other options retain default values
357 EXPECT_TRUE(options_.do_vibrate);
358 EXPECT_FALSE(options_.use_control_socket);
359 EXPECT_FALSE(options_.show_header_only);
360 EXPECT_FALSE(options_.do_progress_updates);
361 EXPECT_FALSE(options_.is_remote_mode);
362 EXPECT_FALSE(options_.use_socket);
363 EXPECT_FALSE(options_.wifi_only);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100364}
365
366TEST_F(DumpOptionsTest, InitializePartial1) {
367 // clang-format off
368 char* argv[] = {
369 const_cast<char*>("dumpstate"),
370 const_cast<char*>("-d"),
371 const_cast<char*>("-z"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100372 const_cast<char*>("-s"),
373 const_cast<char*>("-S"),
374
375 };
376 // clang-format on
377
378 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
379
380 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
381 EXPECT_TRUE(options_.do_add_date);
382 EXPECT_TRUE(options_.do_zip_file);
383 // TODO: Maybe we should trim the filename
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100384 EXPECT_TRUE(options_.use_socket);
385 EXPECT_TRUE(options_.use_control_socket);
386
387 // Other options retain default values
388 EXPECT_FALSE(options_.show_header_only);
389 EXPECT_TRUE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800390 EXPECT_FALSE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100391 EXPECT_FALSE(options_.do_progress_updates);
392 EXPECT_FALSE(options_.is_remote_mode);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800393 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100394}
395
396TEST_F(DumpOptionsTest, InitializePartial2) {
397 // clang-format off
398 char* argv[] = {
399 const_cast<char*>("dumpstate"),
400 const_cast<char*>("-v"),
401 const_cast<char*>("-q"),
402 const_cast<char*>("-p"),
403 const_cast<char*>("-P"),
404 const_cast<char*>("-R"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100405 };
406 // clang-format on
407
408 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
409
410 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
411 EXPECT_TRUE(options_.show_header_only);
412 EXPECT_FALSE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800413 EXPECT_TRUE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100414 EXPECT_TRUE(options_.do_progress_updates);
415 EXPECT_TRUE(options_.is_remote_mode);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100416
417 // Other options retain default values
418 EXPECT_FALSE(options_.do_add_date);
419 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100420 EXPECT_FALSE(options_.use_socket);
421 EXPECT_FALSE(options_.use_control_socket);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800422 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100423}
424
425TEST_F(DumpOptionsTest, InitializeHelp) {
426 // clang-format off
427 char* argv[] = {
428 const_cast<char*>("dumpstate"),
429 const_cast<char*>("-h")
430 };
431 // clang-format on
432
433 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
434
435 // -h is for help.
436 EXPECT_EQ(status, Dumpstate::RunStatus::HELP);
437}
438
439TEST_F(DumpOptionsTest, InitializeUnknown) {
440 // clang-format off
441 char* argv[] = {
442 const_cast<char*>("dumpstate"),
443 const_cast<char*>("-u") // unknown flag
444 };
445 // clang-format on
446
447 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
448
449 // -u is unknown.
450 EXPECT_EQ(status, Dumpstate::RunStatus::INVALID_INPUT);
451}
452
453TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile1) {
454 options_.do_zip_file = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000455 // Writing to socket = !writing to file.
456 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100457 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000458
459 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100460 EXPECT_TRUE(options_.ValidateOptions());
461}
462
463TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile2) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100464 options_.do_progress_updates = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000465 // Writing to socket = !writing to file.
466 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100467 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000468
469 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100470 EXPECT_TRUE(options_.ValidateOptions());
471}
472
473TEST_F(DumpOptionsTest, ValidateOptionsNeedZipfile) {
474 options_.use_control_socket = true;
475 EXPECT_FALSE(options_.ValidateOptions());
476
477 options_.do_zip_file = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100478 EXPECT_TRUE(options_.ValidateOptions());
479}
480
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100481TEST_F(DumpOptionsTest, ValidateOptionsRemoteMode) {
482 options_.is_remote_mode = true;
483 EXPECT_FALSE(options_.ValidateOptions());
484
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100485 options_.do_zip_file = true;
486 options_.do_add_date = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100487 EXPECT_TRUE(options_.ValidateOptions());
488}
489
Felipe Leme7447d7c2016-11-03 18:12:22 -0700490class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700491 public:
492 void SetUp() {
Felipe Leme46b85da2016-11-21 17:40:45 -0800493 DumpstateBaseTest::SetUp();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700494 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700495 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700496 ds.progress_.reset(new Progress());
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100497 ds.options_.reset(new Dumpstate::DumpOptions());
Felipe Leme4c2d6632016-09-28 14:32:00 -0700498 }
499
500 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700501 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700502 const CommandOptions& options = CommandOptions::DEFAULT) {
503 CaptureStdout();
504 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700505 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700506 out = GetCapturedStdout();
507 err = GetCapturedStderr();
508 return status;
509 }
510
Felipe Lemecef02982016-10-03 17:22:22 -0700511 // Dumps a file and capture `stdout` and `stderr`.
512 int DumpFile(const std::string& title, const std::string& path) {
513 CaptureStdout();
514 CaptureStderr();
515 int status = ds.DumpFile(title, path);
516 out = GetCapturedStdout();
517 err = GetCapturedStderr();
518 return status;
519 }
520
Nandana Dutt402a8392019-06-14 14:25:13 +0100521 void SetProgress(long progress, long initial_max) {
522 ds.last_reported_percent_progress_ = 0;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100523 ds.options_->do_progress_updates = true;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700524 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
525 }
526
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100527 std::string GetProgressMessage(int progress, int max,
528 int old_max = 0, bool update_progress = true) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700529 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
530 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700531
Felipe Leme7447d7c2016-11-03 18:12:22 -0700532 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700533
Felipe Leme009ecbb2016-11-07 10:18:44 -0800534 std::string message = "";
Felipe Leme75876a22016-10-27 16:31:27 -0700535 if (max_increased) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800536 message =
Felipe Leme7447d7c2016-11-03 18:12:22 -0700537 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700538 }
539
Felipe Leme009ecbb2016-11-07 10:18:44 -0800540 if (update_progress) {
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100541 message += android::base::StringPrintf("Setting progress: %d/%d (%d%%)\n",
542 progress, max, (100 * progress / max));
Felipe Leme009ecbb2016-11-07 10:18:44 -0800543 }
544
545 return message;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700546 }
547
Felipe Leme4c2d6632016-09-28 14:32:00 -0700548 // `stdout` and `stderr` from the last command ran.
549 std::string out, err;
550
Felipe Lemefd8affa2016-09-30 17:38:57 -0700551 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700552};
553
554TEST_F(DumpstateTest, RunCommandNoArgs) {
555 EXPECT_EQ(-1, RunCommand("", {}));
556}
557
558TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700559 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700560 EXPECT_THAT(out, StrEq("stdout\n"));
561 EXPECT_THAT(err, StrEq("stderr\n"));
562}
563
564TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700565 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700566 EXPECT_THAT(err, StrEq("stderr\n"));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700567 // The duration may not get output, depending on how long it takes,
568 // so we just check the prefix.
Felipe Lemefd8affa2016-09-30 17:38:57 -0700569 EXPECT_THAT(out,
Nandana Dutt47527b52019-03-29 15:34:36 +0000570 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700571}
572
Felipe Lemefd8affa2016-09-30 17:38:57 -0700573TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700574 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700575 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700576 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
577 EXPECT_THAT(out, StrEq("stdout\n"));
578 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
579}
580
581TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700582 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700583 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700584 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700585 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700586}
587
588TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700589 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700590 EXPECT_THAT(err, IsEmpty());
591 EXPECT_THAT(out, StrEq("one\n"));
592}
593
Felipe Lemefd8affa2016-09-30 17:38:57 -0700594TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700595 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700596 EXPECT_THAT(err, IsEmpty());
597 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
598}
599
600TEST_F(DumpstateTest, RunCommandDryRun) {
601 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700602 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700603 // The duration may not get output, depending on how long it takes,
604 // so we just check the prefix.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700605 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Nandana Dutt47527b52019-03-29 15:34:36 +0000606 ") ------\n\t(skipped on dry run)\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700607 EXPECT_THAT(err, IsEmpty());
608}
609
610TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
611 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700612 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700613 EXPECT_THAT(out, IsEmpty());
614 EXPECT_THAT(err, IsEmpty());
615}
616
617TEST_F(DumpstateTest, RunCommandDryRunAlways) {
618 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700619 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700620 EXPECT_THAT(out, StrEq("stdout\n"));
621 EXPECT_THAT(err, StrEq("stderr\n"));
622}
623
Felipe Lemefd8affa2016-09-30 17:38:57 -0700624TEST_F(DumpstateTest, RunCommandNotFound) {
625 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
626 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
627 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
628}
629
630TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700631 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
632 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700633 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700634 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700635 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700636}
637
638TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700639 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700640 // We don't know the exit code, so check just the prefix.
641 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700642 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700643 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700644 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700645}
646
647TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700648 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700649 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700650 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700651 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700652 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700653 " --sleep 2' timed out after 1"));
654}
655
656TEST_F(DumpstateTest, RunCommandIsKilled) {
657 CaptureStdout();
658 CaptureStderr();
659
660 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700661 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700662 CommandOptions::WithTimeout(100).Always().Build()));
663 });
664
665 // Capture pid and pre-sleep output.
666 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
667 std::string err = GetCapturedStderr();
668 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
669
670 std::string out = GetCapturedStdout();
671 std::vector<std::string> lines = android::base::Split(out, "\n");
672 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
673
674 int pid = atoi(lines[0].c_str());
675 EXPECT_THAT(lines[1], StrEq("stdout line1"));
676 EXPECT_THAT(lines[2], IsEmpty()); // \n
677
678 // Then kill the process.
679 CaptureStdout();
680 CaptureStderr();
681 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
682 t.join();
683
684 // Finally, check output after murder.
685 out = GetCapturedStdout();
686 err = GetCapturedStderr();
687
Felipe Leme7447d7c2016-11-03 18:12:22 -0700688 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700689 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700690 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700691 " --pid --sleep 20' failed: killed by signal 15\n"));
692}
693
Felipe Leme75876a22016-10-27 16:31:27 -0700694TEST_F(DumpstateTest, RunCommandProgress) {
695 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
696 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700697 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700698
Nandana Duttbabf6c72019-01-15 14:11:12 +0000699 EXPECT_CALL(*listener, onProgress(66)); // 20/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700700 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100701 std::string progress_message = GetProgressMessage(20, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700702 EXPECT_THAT(out, StrEq("stdout\n"));
703 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
704
Nandana Dutt402a8392019-06-14 14:25:13 +0100705 EXPECT_CALL(*listener, onProgress(80)); // 24/30 %
706 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100707 progress_message = GetProgressMessage(24, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700708 EXPECT_THAT(out, StrEq("stdout\n"));
709 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
710
711 // Make sure command ran while in dry_run is counted.
712 SetDryRun(true);
Nandana Dutt402a8392019-06-14 14:25:13 +0100713 EXPECT_CALL(*listener, onProgress(90)); // 27/30 %
714 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100715 progress_message = GetProgressMessage(27, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700716 EXPECT_THAT(out, IsEmpty());
717 EXPECT_THAT(err, StrEq(progress_message));
718
Nandana Dutt402a8392019-06-14 14:25:13 +0100719 SetDryRun(false);
720 EXPECT_CALL(*listener, onProgress(96)); // 29/30 %
721 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(2).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100722 progress_message = GetProgressMessage(29, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800723 EXPECT_THAT(out, StrEq("stdout\n"));
724 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
725
Nandana Dutt402a8392019-06-14 14:25:13 +0100726 EXPECT_CALL(*listener, onProgress(100)); // 30/30 %
Felipe Leme009ecbb2016-11-07 10:18:44 -0800727 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100728 progress_message = GetProgressMessage(30, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800729 EXPECT_THAT(out, StrEq("stdout\n"));
730 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
731
732 ds.listener_.clear();
733}
734
Felipe Lemed80e6b62016-10-03 13:08:14 -0700735TEST_F(DumpstateTest, RunCommandDropRoot) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800736 if (!IsStandalone()) {
737 // TODO: temporarily disabled because it might cause other tests to fail after dropping
738 // to Shell - need to refactor tests to avoid this problem)
739 MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n")
740 return;
741 }
Felipe Lemed80e6b62016-10-03 13:08:14 -0700742 // First check root case - only available when running with 'adb root'.
743 uid_t uid = getuid();
744 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700745 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700746 EXPECT_THAT(out, StrEq("0\nstdout\n"));
747 EXPECT_THAT(err, StrEq("stderr\n"));
748 return;
749 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700750 // Then run dropping root.
751 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700752 CommandOptions::WithTimeout(1).DropRoot().Build()));
753 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700754 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700755}
756
757TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800758 if (!IsStandalone()) {
759 // TODO: temporarily disabled because it might cause other tests to fail after dropping
760 // to Shell - need to refactor tests to avoid this problem)
761 MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n")
762 return;
763 }
Felipe Lemef0292972016-11-22 13:57:05 -0800764 if (!PropertiesHelper::IsUserBuild()) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700765 // Emulates user build if necessarily.
766 SetBuildType("user");
767 }
768
769 DropRoot();
770
Felipe Leme7447d7c2016-11-03 18:12:22 -0700771 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700772
773 // We don't know the exact path of su, so we just check for the 'root ...' commands
774 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700775 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700776 EXPECT_THAT(err, IsEmpty());
777}
778
Felipe Leme46b85da2016-11-21 17:40:45 -0800779TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
780 if (!IsStandalone()) {
781 // TODO: temporarily disabled because it might cause other tests to fail after dropping
782 // to Shell - need to refactor tests to avoid this problem)
783 MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n")
784 return;
785 }
Felipe Lemef0292972016-11-22 13:57:05 -0800786 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800787 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
788 return;
789 }
790
791 DropRoot();
792
793 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
794 CommandOptions::WithTimeout(1).AsRoot().Build()));
795
796 EXPECT_THAT(out, StrEq("0\nstdout\n"));
797 EXPECT_THAT(err, StrEq("stderr\n"));
798}
799
Nandana Dutt4b392be2018-11-02 16:17:05 +0000800TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild_withUnroot) {
801 if (!IsStandalone()) {
802 // TODO: temporarily disabled because it might cause other tests to fail after dropping
803 // to Shell - need to refactor tests to avoid this problem)
804 MYLOGE(
805 "Skipping DumpstateTest.RunCommandAsRootNonUserBuild_withUnroot() "
806 "on test suite\n")
807 return;
808 }
809 if (PropertiesHelper::IsUserBuild()) {
810 ALOGI("Skipping RunCommandAsRootNonUserBuild_withUnroot on user builds\n");
811 return;
812 }
813
814 // Same test as above, but with unroot property set, which will override su availability.
815 SetUnroot(true);
816 DropRoot();
817
818 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
819 CommandOptions::WithTimeout(1).AsRoot().Build()));
820
821 // AsRoot is ineffective.
822 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
823 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
824}
825
Yifan Hong48e83a12017-10-03 14:10:07 -0700826TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) {
827 if (!IsStandalone()) {
828 // TODO: temporarily disabled because it might cause other tests to fail after dropping
829 // to Shell - need to refactor tests to avoid this problem)
830 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
831 return;
832 }
833 if (!PropertiesHelper::IsUserBuild()) {
834 // Emulates user build if necessarily.
835 SetBuildType("user");
836 }
837
838 DropRoot();
839
840 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
841 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
842
843 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
844 EXPECT_THAT(err, StrEq("stderr\n"));
845}
846
847TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) {
848 if (!IsStandalone()) {
849 // TODO: temporarily disabled because it might cause other tests to fail after dropping
850 // to Shell - need to refactor tests to avoid this problem)
851 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
852 return;
853 }
854 if (PropertiesHelper::IsUserBuild()) {
855 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
856 return;
857 }
858
859 DropRoot();
860
861 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
862 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
863
864 EXPECT_THAT(out, StrEq("0\nstdout\n"));
865 EXPECT_THAT(err, StrEq("stderr\n"));
866}
867
Nandana Dutt4b392be2018-11-02 16:17:05 +0000868TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild_withUnroot) {
869 if (!IsStandalone()) {
870 // TODO: temporarily disabled because it might cause other tests to fail after dropping
871 // to Shell - need to refactor tests to avoid this problem)
872 MYLOGE(
873 "Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild_withUnroot() "
874 "on test suite\n")
875 return;
876 }
877 if (PropertiesHelper::IsUserBuild()) {
878 ALOGI("Skipping RunCommandAsRootIfAvailableOnDebugBuild_withUnroot on user builds\n");
879 return;
880 }
881 // Same test as above, but with unroot property set, which will override su availability.
882 SetUnroot(true);
883
884 DropRoot();
885
886 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
887 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
888
889 // It's a userdebug build, so "su root" should be available, but unroot=true overrides it.
890 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
891 EXPECT_THAT(err, StrEq("stderr\n"));
892}
893
Felipe Lemecef02982016-10-03 17:22:22 -0700894TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
895 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
896 EXPECT_THAT(out,
897 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
898 EXPECT_THAT(err, IsEmpty());
899}
900
901TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
902 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
903 EXPECT_THAT(err, IsEmpty());
Greg Kaiser3a811c12019-05-21 12:48:59 -0700904 // The duration may not get output, depending on how long it takes,
905 // so we just check the prefix.
Felipe Lemecef02982016-10-03 17:22:22 -0700906 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
907 "such file or directory\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700908}
909
910TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700911 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700912 EXPECT_THAT(err, IsEmpty());
913 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
914}
915
916TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700917 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700918 EXPECT_THAT(err, IsEmpty());
919 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
920}
921
922TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700923 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700924 EXPECT_THAT(err, IsEmpty());
925 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
926}
927
928TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700929 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700930 EXPECT_THAT(err, IsEmpty());
931 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
932}
933
934TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
935 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700936 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700937 EXPECT_THAT(err, IsEmpty());
938 EXPECT_THAT(out, IsEmpty());
939}
940
941TEST_F(DumpstateTest, DumpFileOnDryRun) {
942 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700943 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700944 EXPECT_THAT(err, IsEmpty());
Felipe Leme46b85da2016-11-21 17:40:45 -0800945 EXPECT_THAT(
946 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
Nandana Dutt47527b52019-03-29 15:34:36 +0000947 EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700948}
949
Felipe Leme75876a22016-10-27 16:31:27 -0700950TEST_F(DumpstateTest, DumpFileUpdateProgress) {
951 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
952 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700953 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700954
Nandana Duttbabf6c72019-01-15 14:11:12 +0000955 EXPECT_CALL(*listener, onProgress(16)); // 5/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700956 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -0700957
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100958 std::string progress_message = GetProgressMessage(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
Felipe Leme75876a22016-10-27 16:31:27 -0700959 EXPECT_THAT(err, StrEq(progress_message));
960 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
961
962 ds.listener_.clear();
963}
964
Felipe Leme7447d7c2016-11-03 18:12:22 -0700965class DumpstateServiceTest : public DumpstateBaseTest {
Felipe Leme75876a22016-10-27 16:31:27 -0700966 public:
967 DumpstateService dss;
968};
969
Felipe Leme7447d7c2016-11-03 18:12:22 -0700970class ProgressTest : public DumpstateBaseTest {
971 public:
972 Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {
973 return Progress(max, growth_factor, path);
974 }
975
976 void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) {
977 std::string expected_content =
978 android::base::StringPrintf("%d %d\n", expected_runs, expected_average);
979 std::string actual_content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800980 ReadFileToString(path, &actual_content);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700981 ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path;
982 }
983};
984
985TEST_F(ProgressTest, SimpleTest) {
986 Progress progress;
987 EXPECT_EQ(0, progress.Get());
988 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
989 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
990
991 bool max_increased = progress.Inc(1);
992 EXPECT_EQ(1, progress.Get());
993 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
994 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
995 EXPECT_FALSE(max_increased);
996
997 // Ignore negative increase.
998 max_increased = progress.Inc(-1);
999 EXPECT_EQ(1, progress.Get());
1000 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
1001 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1002 EXPECT_FALSE(max_increased);
1003}
1004
1005TEST_F(ProgressTest, MaxGrowsInsideNewRange) {
1006 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1007 EXPECT_EQ(0, progress.Get());
1008 EXPECT_EQ(10, progress.GetInitialMax());
1009 EXPECT_EQ(10, progress.GetMax());
1010
1011 // No increase
1012 bool max_increased = progress.Inc(10);
1013 EXPECT_EQ(10, progress.Get());
1014 EXPECT_EQ(10, progress.GetMax());
1015 EXPECT_FALSE(max_increased);
1016
1017 // Increase, with new value < max*20%
1018 max_increased = progress.Inc(1);
1019 EXPECT_EQ(11, progress.Get());
1020 EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13
1021 EXPECT_TRUE(max_increased);
1022}
1023
1024TEST_F(ProgressTest, MaxGrowsOutsideNewRange) {
1025 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1026 EXPECT_EQ(0, progress.Get());
1027 EXPECT_EQ(10, progress.GetInitialMax());
1028 EXPECT_EQ(10, progress.GetMax());
1029
1030 // No increase
1031 bool max_increased = progress.Inc(10);
1032 EXPECT_EQ(10, progress.Get());
1033 EXPECT_EQ(10, progress.GetMax());
1034 EXPECT_FALSE(max_increased);
1035
1036 // Increase, with new value > max*20%
1037 max_increased = progress.Inc(5);
1038 EXPECT_EQ(15, progress.Get());
1039 EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18
1040 EXPECT_TRUE(max_increased);
1041}
1042
1043TEST_F(ProgressTest, InvalidPath) {
1044 Progress progress("/devil/null");
1045 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1046}
1047
1048TEST_F(ProgressTest, EmptyFile) {
1049 Progress progress(CopyTextFileFixture("empty-file.txt"));
1050 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1051}
1052
1053TEST_F(ProgressTest, InvalidLine1stEntryNAN) {
1054 Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt"));
1055 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1056}
1057
1058TEST_F(ProgressTest, InvalidLine2ndEntryNAN) {
1059 Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt"));
1060 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1061}
1062
1063TEST_F(ProgressTest, InvalidLineBothNAN) {
1064 Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt"));
1065 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1066}
1067
1068TEST_F(ProgressTest, InvalidLine1stEntryNegative) {
1069 Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt"));
1070 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1071}
1072
1073TEST_F(ProgressTest, InvalidLine2ndEntryNegative) {
1074 Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt"));
1075 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1076}
1077
1078TEST_F(ProgressTest, InvalidLine1stEntryTooBig) {
1079 Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt"));
1080 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1081}
1082
1083TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) {
1084 Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt"));
1085 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1086}
1087
1088// Tests stats are properly saved when the file does not exists.
1089TEST_F(ProgressTest, FirstTime) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001090 if (!IsStandalone()) {
1091 // TODO: temporarily disabled because it's failing when running as suite
1092 MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n")
1093 return;
1094 }
1095
Felipe Leme7447d7c2016-11-03 18:12:22 -07001096 std::string path = kTestDataPath + "FirstTime.txt";
1097 android::base::RemoveFileIfExists(path);
1098
1099 Progress run1(path);
1100 EXPECT_EQ(0, run1.Get());
1101 EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax());
1102 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1103
1104 bool max_increased = run1.Inc(20);
1105 EXPECT_EQ(20, run1.Get());
1106 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1107 EXPECT_FALSE(max_increased);
1108
1109 run1.Save();
1110 AssertStats(path, 1, 20);
1111}
1112
1113// Tests what happens when the persistent settings contains the average duration of 1 run.
1114// Data on file is 1 run and 109 average.
1115TEST_F(ProgressTest, SecondTime) {
1116 std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt");
1117
1118 Progress run1 = GetInstance(-42, 1.2, path);
1119 EXPECT_EQ(0, run1.Get());
1120 EXPECT_EQ(10, run1.GetInitialMax());
1121 EXPECT_EQ(10, run1.GetMax());
1122
1123 bool max_increased = run1.Inc(20);
1124 EXPECT_EQ(20, run1.Get());
1125 EXPECT_EQ(24, run1.GetMax());
1126 EXPECT_TRUE(max_increased);
1127
1128 // Average now is 2 runs and (10 + 20)/ 2 = 15
1129 run1.Save();
1130 AssertStats(path, 2, 15);
1131
1132 Progress run2 = GetInstance(-42, 1.2, path);
1133 EXPECT_EQ(0, run2.Get());
1134 EXPECT_EQ(15, run2.GetInitialMax());
1135 EXPECT_EQ(15, run2.GetMax());
1136
1137 max_increased = run2.Inc(25);
1138 EXPECT_EQ(25, run2.Get());
1139 EXPECT_EQ(30, run2.GetMax());
1140 EXPECT_TRUE(max_increased);
1141
1142 // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18
1143 run2.Save();
1144 AssertStats(path, 3, 18);
1145
1146 Progress run3 = GetInstance(-42, 1.2, path);
1147 EXPECT_EQ(0, run3.Get());
1148 EXPECT_EQ(18, run3.GetInitialMax());
1149 EXPECT_EQ(18, run3.GetMax());
1150
1151 // Make sure average decreases as well
1152 max_increased = run3.Inc(5);
1153 EXPECT_EQ(5, run3.Get());
1154 EXPECT_EQ(18, run3.GetMax());
1155 EXPECT_FALSE(max_increased);
1156
1157 // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14
1158 run3.Save();
1159 AssertStats(path, 4, 14);
1160}
1161
1162// Tests what happens when the persistent settings contains the average duration of 2 runs.
1163// Data on file is 2 runs and 15 average.
1164TEST_F(ProgressTest, ThirdTime) {
1165 std::string path = CopyTextFileFixture("stats-two-runs.txt");
1166 AssertStats(path, 2, 15); // Sanity check
1167
1168 Progress run1 = GetInstance(-42, 1.2, path);
1169 EXPECT_EQ(0, run1.Get());
1170 EXPECT_EQ(15, run1.GetInitialMax());
1171 EXPECT_EQ(15, run1.GetMax());
1172
1173 bool max_increased = run1.Inc(20);
1174 EXPECT_EQ(20, run1.Get());
1175 EXPECT_EQ(24, run1.GetMax());
1176 EXPECT_TRUE(max_increased);
1177
1178 // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16
1179 run1.Save();
1180 AssertStats(path, 3, 16);
1181}
1182
Felipe Leme46b85da2016-11-21 17:40:45 -08001183class DumpstateUtilTest : public DumpstateBaseTest {
1184 public:
1185 void SetUp() {
1186 DumpstateBaseTest::SetUp();
1187 SetDryRun(false);
1188 }
1189
Felipe Leme46b85da2016-11-21 17:40:45 -08001190 void CaptureFdOut() {
Felipe Lemef0292972016-11-22 13:57:05 -08001191 ReadFileToString(path_, &out);
Felipe Leme46b85da2016-11-21 17:40:45 -08001192 }
1193
1194 void CreateFd(const std::string& name) {
1195 path_ = kTestDataPath + name;
1196 MYLOGD("Creating fd for file %s\n", path_.c_str());
1197
1198 fd = TEMP_FAILURE_RETRY(open(path_.c_str(),
1199 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
1200 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
1201 ASSERT_GE(fd, 0) << "could not create FD for path " << path_;
1202 }
1203
1204 // Runs a command into the `fd` and capture `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001205 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme46b85da2016-11-21 17:40:45 -08001206 const CommandOptions& options = CommandOptions::DEFAULT) {
1207 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001208 int status = RunCommandToFd(fd, title, full_command, options);
Felipe Leme46b85da2016-11-21 17:40:45 -08001209 close(fd);
1210
1211 CaptureFdOut();
1212 err = GetCapturedStderr();
1213 return status;
1214 }
1215
1216 // Dumps a file and into the `fd` and `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001217 int DumpFile(const std::string& title, const std::string& path) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001218 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001219 int status = DumpFileToFd(fd, title, path);
Felipe Leme46b85da2016-11-21 17:40:45 -08001220 close(fd);
1221
1222 CaptureFdOut();
1223 err = GetCapturedStderr();
1224 return status;
1225 }
1226
1227 int fd;
1228
1229 // 'fd` output and `stderr` from the last command ran.
1230 std::string out, err;
1231
1232 private:
1233 std::string path_;
1234};
1235
1236TEST_F(DumpstateUtilTest, RunCommandNoArgs) {
Felipe Lemef0292972016-11-22 13:57:05 -08001237 CreateFd("RunCommandNoArgs.txt");
1238 EXPECT_EQ(-1, RunCommand("", {}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001239}
1240
Felipe Lemef0292972016-11-22 13:57:05 -08001241TEST_F(DumpstateUtilTest, RunCommandNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001242 CreateFd("RunCommandWithNoArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001243 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001244 EXPECT_THAT(out, StrEq("stdout\n"));
1245 EXPECT_THAT(err, StrEq("stderr\n"));
1246}
1247
Felipe Lemef0292972016-11-22 13:57:05 -08001248TEST_F(DumpstateUtilTest, RunCommandWithTitle) {
1249 CreateFd("RunCommandWithNoArgs.txt");
1250 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1251 EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
1252 EXPECT_THAT(err, StrEq("stderr\n"));
1253}
1254
Felipe Leme46b85da2016-11-21 17:40:45 -08001255TEST_F(DumpstateUtilTest, RunCommandWithOneArg) {
1256 CreateFd("RunCommandWithOneArg.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001257 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001258 EXPECT_THAT(err, IsEmpty());
1259 EXPECT_THAT(out, StrEq("one\n"));
1260}
1261
1262TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) {
1263 CreateFd("RunCommandWithMultipleArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001264 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001265 EXPECT_THAT(err, IsEmpty());
1266 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
1267}
1268
1269TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) {
1270 CreateFd("RunCommandWithLoggingMessage.txt");
1271 EXPECT_EQ(
Felipe Lemef0292972016-11-22 13:57:05 -08001272 0, RunCommand("", {kSimpleCommand},
Felipe Leme46b85da2016-11-21 17:40:45 -08001273 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
1274 EXPECT_THAT(out, StrEq("stdout\n"));
1275 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
1276}
1277
1278TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) {
1279 CreateFd("RunCommandRedirectStderr.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001280 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
1281 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001282 EXPECT_THAT(out, IsEmpty());
1283 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
1284}
1285
1286TEST_F(DumpstateUtilTest, RunCommandDryRun) {
1287 CreateFd("RunCommandDryRun.txt");
1288 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001289 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1290 EXPECT_THAT(out, StrEq(android::base::StringPrintf(
1291 "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n",
1292 kSimpleCommand.c_str())));
1293 EXPECT_THAT(err, IsEmpty());
1294}
1295
1296TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) {
1297 CreateFd("RunCommandDryRun.txt");
1298 SetDryRun(true);
1299 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001300 EXPECT_THAT(
1301 out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str())));
1302 EXPECT_THAT(err, IsEmpty());
1303}
1304
1305TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) {
1306 CreateFd("RunCommandDryRunAlways.txt");
1307 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001308 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001309 EXPECT_THAT(out, StrEq("stdout\n"));
1310 EXPECT_THAT(err, StrEq("stderr\n"));
1311}
1312
1313TEST_F(DumpstateUtilTest, RunCommandNotFound) {
1314 CreateFd("RunCommandNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001315 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001316 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
1317 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
1318}
1319
1320TEST_F(DumpstateUtilTest, RunCommandFails) {
1321 CreateFd("RunCommandFails.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001322 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001323 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
1324 " --exit 42' failed: exit code 42\n"));
1325 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
1326 " --exit 42' failed: exit code 42\n"));
1327}
1328
1329TEST_F(DumpstateUtilTest, RunCommandCrashes) {
1330 CreateFd("RunCommandCrashes.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001331 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001332 // We don't know the exit code, so check just the prefix.
1333 EXPECT_THAT(
1334 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1335 EXPECT_THAT(
1336 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1337}
1338
Vishnu Nair6921f802017-11-22 09:17:23 -08001339TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001340 CreateFd("RunCommandTimesout.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001341 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1342 CommandOptions::WithTimeout(1).Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001343 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1344 " --sleep 2' timed out after 1"));
1345 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1346 " --sleep 2' timed out after 1"));
1347}
1348
Vishnu Nair6921f802017-11-22 09:17:23 -08001349TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) {
1350 CreateFd("RunCommandTimesout.txt");
1351 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1352 CommandOptions::WithTimeoutInMs(1000).Build()));
1353 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1354 " --sleep 2' timed out after 1"));
1355 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1356 " --sleep 2' timed out after 1"));
1357}
1358
1359
Felipe Leme46b85da2016-11-21 17:40:45 -08001360TEST_F(DumpstateUtilTest, RunCommandIsKilled) {
1361 CreateFd("RunCommandIsKilled.txt");
1362 CaptureStderr();
1363
1364 std::thread t([=]() {
Felipe Lemef0292972016-11-22 13:57:05 -08001365 EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001366 CommandOptions::WithTimeout(100).Always().Build()));
1367 });
1368
1369 // Capture pid and pre-sleep output.
1370 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
1371 std::string err = GetCapturedStderr();
1372 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
1373
1374 CaptureFdOut();
1375 std::vector<std::string> lines = android::base::Split(out, "\n");
1376 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
1377
1378 int pid = atoi(lines[0].c_str());
1379 EXPECT_THAT(lines[1], StrEq("stdout line1"));
1380 EXPECT_THAT(lines[2], IsEmpty()); // \n
1381
1382 // Then kill the process.
1383 CaptureFdOut();
1384 CaptureStderr();
1385 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
1386 t.join();
1387
1388 // Finally, check output after murder.
1389 CaptureFdOut();
1390 err = GetCapturedStderr();
1391
1392 // out starts with the pid, which is an unknown
1393 EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand +
1394 " --pid --sleep 20' failed: killed by signal 15\n"));
1395 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
1396 " --pid --sleep 20' failed: killed by signal 15\n"));
1397}
1398
1399TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) {
1400 if (!IsStandalone()) {
1401 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1402 // to Shell - need to refactor tests to avoid this problem)
1403 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n")
1404 return;
1405 }
1406 CreateFd("RunCommandAsRootUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001407 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001408 // Emulates user build if necessarily.
1409 SetBuildType("user");
1410 }
1411
1412 DropRoot();
1413
Felipe Lemef0292972016-11-22 13:57:05 -08001414 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001415
1416 // We don't know the exact path of su, so we just check for the 'root ...' commands
1417 EXPECT_THAT(out, StartsWith("Skipping"));
1418 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
1419 EXPECT_THAT(err, IsEmpty());
1420}
1421
1422TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) {
1423 if (!IsStandalone()) {
1424 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1425 // to Shell - need to refactor tests to avoid this problem)
1426 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n")
1427 return;
1428 }
1429 CreateFd("RunCommandAsRootNonUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001430 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001431 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1432 return;
1433 }
1434
1435 DropRoot();
1436
Felipe Lemef0292972016-11-22 13:57:05 -08001437 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1438 CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001439
1440 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1441 EXPECT_THAT(err, StrEq("stderr\n"));
1442}
Felipe Leme46b85da2016-11-21 17:40:45 -08001443
Yifan Hong48e83a12017-10-03 14:10:07 -07001444
1445TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) {
1446 if (!IsStandalone()) {
1447 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1448 // to Shell - need to refactor tests to avoid this problem)
1449 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
1450 return;
1451 }
1452 CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt");
1453 if (!PropertiesHelper::IsUserBuild()) {
1454 // Emulates user build if necessarily.
1455 SetBuildType("user");
1456 }
1457
1458 DropRoot();
1459
1460 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1461 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1462
1463 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1464 EXPECT_THAT(err, StrEq("stderr\n"));
1465}
1466
1467TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) {
1468 if (!IsStandalone()) {
1469 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1470 // to Shell - need to refactor tests to avoid this problem)
1471 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
1472 return;
1473 }
1474 CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt");
1475 if (PropertiesHelper::IsUserBuild()) {
1476 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1477 return;
1478 }
1479
1480 DropRoot();
1481
1482 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1483 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1484
1485 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1486 EXPECT_THAT(err, StrEq("stderr\n"));
1487}
1488
Felipe Leme46b85da2016-11-21 17:40:45 -08001489TEST_F(DumpstateUtilTest, RunCommandDropRoot) {
1490 if (!IsStandalone()) {
1491 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1492 // to Shell - need to refactor tests to avoid this problem)
1493 MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n")
1494 return;
1495 }
1496 CreateFd("RunCommandDropRoot.txt");
1497 // First check root case - only available when running with 'adb root'.
1498 uid_t uid = getuid();
1499 if (uid == 0) {
Felipe Lemef0292972016-11-22 13:57:05 -08001500 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001501 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1502 EXPECT_THAT(err, StrEq("stderr\n"));
1503 return;
1504 }
1505 // Then run dropping root.
Felipe Lemef0292972016-11-22 13:57:05 -08001506 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001507 CommandOptions::WithTimeout(1).DropRoot().Build()));
1508 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1509 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
1510}
1511
Felipe Lemef0292972016-11-22 13:57:05 -08001512TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001513 CreateFd("DumpFileNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001514 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001515 EXPECT_THAT(out,
1516 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
1517 EXPECT_THAT(err, IsEmpty());
1518}
1519
Felipe Lemef0292972016-11-22 13:57:05 -08001520TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) {
1521 CreateFd("DumpFileNotFound.txt");
1522 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
1523 EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such "
1524 "file or directory\n"));
1525 EXPECT_THAT(err, IsEmpty());
1526}
1527
Felipe Leme46b85da2016-11-21 17:40:45 -08001528TEST_F(DumpstateUtilTest, DumpFileSingleLine) {
1529 CreateFd("DumpFileSingleLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001530 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001531 EXPECT_THAT(err, IsEmpty());
1532 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1533}
1534
1535TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) {
1536 CreateFd("DumpFileSingleLineWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001537 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001538 EXPECT_THAT(err, IsEmpty());
1539 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
1540}
1541
1542TEST_F(DumpstateUtilTest, DumpFileMultipleLines) {
1543 CreateFd("DumpFileMultipleLines.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001544 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001545 EXPECT_THAT(err, IsEmpty());
1546 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1547}
1548
1549TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) {
1550 CreateFd("DumpFileMultipleLinesWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001551 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001552 EXPECT_THAT(err, IsEmpty());
1553 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1554}
1555
Felipe Lemef0292972016-11-22 13:57:05 -08001556TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) {
1557 CreateFd("DumpFileOnDryRun.txt");
1558 SetDryRun(true);
1559 std::string path = kTestDataPath + "single-line.txt";
1560 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
1561 EXPECT_THAT(err, IsEmpty());
1562 EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n"));
1563}
1564
Felipe Leme46b85da2016-11-21 17:40:45 -08001565TEST_F(DumpstateUtilTest, DumpFileOnDryRun) {
1566 CreateFd("DumpFileOnDryRun.txt");
1567 SetDryRun(true);
1568 std::string path = kTestDataPath + "single-line.txt";
Felipe Lemef0292972016-11-22 13:57:05 -08001569 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001570 EXPECT_THAT(err, IsEmpty());
Felipe Lemef0292972016-11-22 13:57:05 -08001571 EXPECT_THAT(
1572 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
1573 EXPECT_THAT(out, EndsWith("skipped on dry run\n"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001574}
Ecco Park61ffcf72016-10-27 15:46:26 -07001575
Felipe Leme47e9be22016-12-21 15:37:07 -08001576} // namespace dumpstate
1577} // namespace os
1578} // namespace android