blob: 718f4592a30851d5b420c1999aae05cff982c117 [file] [log] [blame]
Felipe Leme4c2d6632016-09-28 14:32:00 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Felipe Leme75876a22016-10-27 16:31:27 -070017#define LOG_TAG "dumpstate"
18#include <cutils/log.h>
19
Felipe Lemef0292972016-11-22 13:57:05 -080020#include "DumpstateInternal.h"
Felipe Leme75876a22016-10-27 16:31:27 -070021#include "DumpstateService.h"
22#include "android/os/BnDumpstate.h"
Felipe Leme4c2d6632016-09-28 14:32:00 -070023#include "dumpstate.h"
24
25#include <gmock/gmock.h>
26#include <gtest/gtest.h>
27
Felipe Leme46b85da2016-11-21 17:40:45 -080028#include <fcntl.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070029#include <libgen.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070030#include <signal.h>
31#include <sys/types.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070032#include <unistd.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070033#include <thread>
Felipe Leme4c2d6632016-09-28 14:32:00 -070034
35#include <android-base/file.h>
Felipe Lemed80e6b62016-10-03 13:08:14 -070036#include <android-base/properties.h>
37#include <android-base/stringprintf.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070038#include <android-base/strings.h>
Abhijeet Kaur904e0e02018-12-05 14:03:01 +000039#include <cutils/properties.h>
Abhijeet Kaure370d682019-10-01 16:49:30 +010040#include <android-base/unique_fd.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070041
Felipe Leme47e9be22016-12-21 15:37:07 -080042namespace android {
43namespace os {
44namespace dumpstate {
Felipe Lemed80e6b62016-10-03 13:08:14 -070045
Felipe Leme4c2d6632016-09-28 14:32:00 -070046using ::testing::EndsWith;
Felipe Leme46b85da2016-11-21 17:40:45 -080047using ::testing::HasSubstr;
Felipe Leme009ecbb2016-11-07 10:18:44 -080048using ::testing::IsNull;
Felipe Leme4c2d6632016-09-28 14:32:00 -070049using ::testing::IsEmpty;
Felipe Leme009ecbb2016-11-07 10:18:44 -080050using ::testing::NotNull;
Felipe Leme4c2d6632016-09-28 14:32:00 -070051using ::testing::StrEq;
52using ::testing::StartsWith;
53using ::testing::Test;
54using ::testing::internal::CaptureStderr;
55using ::testing::internal::CaptureStdout;
56using ::testing::internal::GetCapturedStderr;
57using ::testing::internal::GetCapturedStdout;
58
Nandana Dutt3f8c7172018-09-25 12:01:54 +010059#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
60
Felipe Leme75876a22016-10-27 16:31:27 -070061class DumpstateListenerMock : public IDumpstateListener {
62 public:
Nandana Dutta6a28bd2019-01-14 16:54:38 +000063 MOCK_METHOD1(onProgress, binder::Status(int32_t progress));
64 MOCK_METHOD1(onError, binder::Status(int32_t error_code));
Nandana Duttcc4ead82019-01-23 08:29:23 +000065 MOCK_METHOD0(onFinished, binder::Status());
Felipe Leme75876a22016-10-27 16:31:27 -070066
67 protected:
68 MOCK_METHOD0(onAsBinder, IBinder*());
69};
70
Felipe Leme46b85da2016-11-21 17:40:45 -080071static int calls_;
72
Felipe Leme7447d7c2016-11-03 18:12:22 -070073// Base class for all tests in this file
74class DumpstateBaseTest : public Test {
Felipe Leme46b85da2016-11-21 17:40:45 -080075 public:
76 virtual void SetUp() override {
77 calls_++;
Felipe Lemef0292972016-11-22 13:57:05 -080078 SetDryRun(false);
Felipe Leme46b85da2016-11-21 17:40:45 -080079 }
80
Felipe Lemef0292972016-11-22 13:57:05 -080081 void SetDryRun(bool dry_run) const {
82 PropertiesHelper::dry_run_ = dry_run;
83 }
84
85 void SetBuildType(const std::string& build_type) const {
86 PropertiesHelper::build_type_ = build_type;
87 }
88
Nandana Dutt4b392be2018-11-02 16:17:05 +000089 void SetUnroot(bool unroot) const {
90 PropertiesHelper::unroot_ = unroot;
91 }
92
Felipe Lemef0292972016-11-22 13:57:05 -080093 bool IsStandalone() const {
Felipe Leme46b85da2016-11-21 17:40:45 -080094 return calls_ == 1;
95 }
96
Felipe Lemef0292972016-11-22 13:57:05 -080097 void DropRoot() const {
98 DropRootUser();
Felipe Leme46b85da2016-11-21 17:40:45 -080099 uid_t uid = getuid();
100 ASSERT_EQ(2000, (int)uid);
101 }
102
Felipe Leme7447d7c2016-11-03 18:12:22 -0700103 protected:
104 const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str());
Dan Shie177e8e2019-06-20 11:08:14 -0700105 const std::string kTestDataPath = kTestPath + "/tests/testdata/";
106 const std::string kSimpleCommand = kTestPath + "/dumpstate_test_fixture";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700107 const std::string kEchoCommand = "/system/bin/echo";
108
109 /*
110 * Copies a text file fixture to a temporary file, returning it's path.
111 *
112 * Useful in cases where the test case changes the content of the tile.
113 */
114 std::string CopyTextFileFixture(const std::string& relative_name) {
115 std::string from = kTestDataPath + relative_name;
116 // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it
117 // around for poking when the test fails.
118 std::string to = kTestDataPath + relative_name + ".tmp";
119 ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str());
120 android::base::RemoveFileIfExists(to);
121 CopyTextFile(from, to);
122 return to.c_str();
123 }
124
Felipe Leme46b85da2016-11-21 17:40:45 -0800125 // Need functions that returns void to use assertions -
Felipe Leme7447d7c2016-11-03 18:12:22 -0700126 // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement
Felipe Leme46b85da2016-11-21 17:40:45 -0800127 void ReadFileToString(const std::string& path, std::string* content) {
128 ASSERT_TRUE(android::base::ReadFileToString(path, content))
129 << "could not read contents from " << path;
130 }
131 void WriteStringToFile(const std::string& content, const std::string& path) {
132 ASSERT_TRUE(android::base::WriteStringToFile(content, path))
133 << "could not write contents to " << path;
134 }
135
136 private:
Felipe Leme7447d7c2016-11-03 18:12:22 -0700137 void CopyTextFile(const std::string& from, const std::string& to) {
138 std::string content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800139 ReadFileToString(from, &content);
140 WriteStringToFile(content, to);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700141 }
142};
143
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100144class DumpOptionsTest : public Test {
145 public:
146 virtual ~DumpOptionsTest() {
147 }
148 virtual void SetUp() {
149 options_ = Dumpstate::DumpOptions();
150 }
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000151 void TearDown() {
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000152 }
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100153 Dumpstate::DumpOptions options_;
Abhijeet Kaure370d682019-10-01 16:49:30 +0100154 android::base::unique_fd fd;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100155};
156
157TEST_F(DumpOptionsTest, InitializeNone) {
158 // clang-format off
159 char* argv[] = {
160 const_cast<char*>("dumpstate")
161 };
162 // clang-format on
163
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100164 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
165
166 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000167
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100168 EXPECT_FALSE(options_.do_add_date);
169 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100170 EXPECT_FALSE(options_.use_socket);
171 EXPECT_FALSE(options_.use_control_socket);
172 EXPECT_FALSE(options_.show_header_only);
173 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000174 EXPECT_FALSE(options_.do_fb);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100175 EXPECT_FALSE(options_.do_progress_updates);
176 EXPECT_FALSE(options_.is_remote_mode);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000177}
178
179TEST_F(DumpOptionsTest, InitializeAdbBugreport) {
180 // clang-format off
181 char* argv[] = {
182 const_cast<char*>("dumpstatez"),
183 const_cast<char*>("-S"),
184 const_cast<char*>("-d"),
185 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000186 };
187 // clang-format on
188
189 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
190
191 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
192 EXPECT_TRUE(options_.do_add_date);
193 EXPECT_TRUE(options_.do_zip_file);
194 EXPECT_TRUE(options_.use_control_socket);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000195
196 // Other options retain default values
197 EXPECT_TRUE(options_.do_vibrate);
198 EXPECT_FALSE(options_.show_header_only);
199 EXPECT_FALSE(options_.do_fb);
200 EXPECT_FALSE(options_.do_progress_updates);
201 EXPECT_FALSE(options_.is_remote_mode);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000202 EXPECT_FALSE(options_.use_socket);
203}
204
205TEST_F(DumpOptionsTest, InitializeAdbShellBugreport) {
206 // clang-format off
207 char* argv[] = {
208 const_cast<char*>("dumpstate"),
209 const_cast<char*>("-s"),
210 };
211 // clang-format on
212
213 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
214
215 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
216 EXPECT_TRUE(options_.use_socket);
217
218 // Other options retain default values
219 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000220 EXPECT_FALSE(options_.do_add_date);
221 EXPECT_FALSE(options_.do_zip_file);
222 EXPECT_FALSE(options_.use_control_socket);
223 EXPECT_FALSE(options_.show_header_only);
224 EXPECT_FALSE(options_.do_fb);
225 EXPECT_FALSE(options_.do_progress_updates);
226 EXPECT_FALSE(options_.is_remote_mode);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000227}
228
229TEST_F(DumpOptionsTest, InitializeFullBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100230 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_FULL, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000231 EXPECT_TRUE(options_.do_add_date);
232 EXPECT_TRUE(options_.do_fb);
233 EXPECT_TRUE(options_.do_zip_file);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000234
235 // Other options retain default values
236 EXPECT_TRUE(options_.do_vibrate);
237 EXPECT_FALSE(options_.use_control_socket);
238 EXPECT_FALSE(options_.show_header_only);
239 EXPECT_FALSE(options_.do_progress_updates);
240 EXPECT_FALSE(options_.is_remote_mode);
241 EXPECT_FALSE(options_.use_socket);
242 EXPECT_FALSE(options_.do_start_service);
243}
244
245TEST_F(DumpOptionsTest, InitializeInteractiveBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100246 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000247 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000248 EXPECT_TRUE(options_.do_zip_file);
249 EXPECT_TRUE(options_.do_progress_updates);
250 EXPECT_TRUE(options_.do_start_service);
251 EXPECT_FALSE(options_.do_fb);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000252
253 // Other options retain default values
254 EXPECT_TRUE(options_.do_vibrate);
255 EXPECT_FALSE(options_.use_control_socket);
256 EXPECT_FALSE(options_.show_header_only);
257 EXPECT_FALSE(options_.is_remote_mode);
258 EXPECT_FALSE(options_.use_socket);
259}
260
261TEST_F(DumpOptionsTest, InitializeRemoteBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100262 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_REMOTE, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000263 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000264 EXPECT_TRUE(options_.do_zip_file);
265 EXPECT_TRUE(options_.is_remote_mode);
266 EXPECT_FALSE(options_.do_vibrate);
267 EXPECT_FALSE(options_.do_fb);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000268
269 // Other options retain default values
270 EXPECT_FALSE(options_.use_control_socket);
271 EXPECT_FALSE(options_.show_header_only);
272 EXPECT_FALSE(options_.do_progress_updates);
273 EXPECT_FALSE(options_.use_socket);
274}
275
276TEST_F(DumpOptionsTest, InitializeWearBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100277 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WEAR, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000278 EXPECT_TRUE(options_.do_add_date);
279 EXPECT_TRUE(options_.do_fb);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000280 EXPECT_TRUE(options_.do_zip_file);
281 EXPECT_TRUE(options_.do_progress_updates);
282 EXPECT_TRUE(options_.do_start_service);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000283
284 // Other options retain default values
285 EXPECT_TRUE(options_.do_vibrate);
286 EXPECT_FALSE(options_.use_control_socket);
287 EXPECT_FALSE(options_.show_header_only);
288 EXPECT_FALSE(options_.is_remote_mode);
289 EXPECT_FALSE(options_.use_socket);
290}
291
292TEST_F(DumpOptionsTest, InitializeTelephonyBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100293 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_TELEPHONY, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000294 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaurd3cca0d2019-03-25 12:04:16 +0000295 EXPECT_FALSE(options_.do_fb);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000296 EXPECT_TRUE(options_.do_zip_file);
297 EXPECT_TRUE(options_.telephony_only);
Hunter Knepshield820f9bc2020-02-05 20:10:53 -0800298 EXPECT_TRUE(options_.do_progress_updates);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000299
300 // Other options retain default values
301 EXPECT_TRUE(options_.do_vibrate);
302 EXPECT_FALSE(options_.use_control_socket);
303 EXPECT_FALSE(options_.show_header_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000304 EXPECT_FALSE(options_.is_remote_mode);
305 EXPECT_FALSE(options_.use_socket);
306}
307
308TEST_F(DumpOptionsTest, InitializeWifiBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100309 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WIFI, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000310 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaurd3cca0d2019-03-25 12:04:16 +0000311 EXPECT_FALSE(options_.do_fb);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000312 EXPECT_TRUE(options_.do_zip_file);
313 EXPECT_TRUE(options_.wifi_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000314
315 // Other options retain default values
316 EXPECT_TRUE(options_.do_vibrate);
317 EXPECT_FALSE(options_.use_control_socket);
318 EXPECT_FALSE(options_.show_header_only);
319 EXPECT_FALSE(options_.do_progress_updates);
320 EXPECT_FALSE(options_.is_remote_mode);
321 EXPECT_FALSE(options_.use_socket);
322}
323
324TEST_F(DumpOptionsTest, InitializeDefaultBugReport) {
325 // default: commandline options are not overridden
326 // clang-format off
327 char* argv[] = {
328 const_cast<char*>("bugreport"),
329 const_cast<char*>("-d"),
330 const_cast<char*>("-p"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000331 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000332 };
333 // clang-format on
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000334 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
335
336 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
337 EXPECT_TRUE(options_.do_add_date);
338 EXPECT_TRUE(options_.do_fb);
339 EXPECT_TRUE(options_.do_zip_file);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000340
341 // Other options retain default values
342 EXPECT_TRUE(options_.do_vibrate);
343 EXPECT_FALSE(options_.use_control_socket);
344 EXPECT_FALSE(options_.show_header_only);
345 EXPECT_FALSE(options_.do_progress_updates);
346 EXPECT_FALSE(options_.is_remote_mode);
347 EXPECT_FALSE(options_.use_socket);
348 EXPECT_FALSE(options_.wifi_only);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100349}
350
351TEST_F(DumpOptionsTest, InitializePartial1) {
352 // clang-format off
353 char* argv[] = {
354 const_cast<char*>("dumpstate"),
355 const_cast<char*>("-d"),
356 const_cast<char*>("-z"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100357 const_cast<char*>("-s"),
358 const_cast<char*>("-S"),
359
360 };
361 // clang-format on
362
363 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
364
365 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
366 EXPECT_TRUE(options_.do_add_date);
367 EXPECT_TRUE(options_.do_zip_file);
368 // TODO: Maybe we should trim the filename
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100369 EXPECT_TRUE(options_.use_socket);
370 EXPECT_TRUE(options_.use_control_socket);
371
372 // Other options retain default values
373 EXPECT_FALSE(options_.show_header_only);
374 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000375 EXPECT_FALSE(options_.do_fb);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100376 EXPECT_FALSE(options_.do_progress_updates);
377 EXPECT_FALSE(options_.is_remote_mode);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100378}
379
380TEST_F(DumpOptionsTest, InitializePartial2) {
381 // clang-format off
382 char* argv[] = {
383 const_cast<char*>("dumpstate"),
384 const_cast<char*>("-v"),
385 const_cast<char*>("-q"),
386 const_cast<char*>("-p"),
387 const_cast<char*>("-P"),
388 const_cast<char*>("-R"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100389 };
390 // clang-format on
391
392 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
393
394 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
395 EXPECT_TRUE(options_.show_header_only);
396 EXPECT_FALSE(options_.do_vibrate);
397 EXPECT_TRUE(options_.do_fb);
398 EXPECT_TRUE(options_.do_progress_updates);
399 EXPECT_TRUE(options_.is_remote_mode);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100400
401 // Other options retain default values
402 EXPECT_FALSE(options_.do_add_date);
403 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100404 EXPECT_FALSE(options_.use_socket);
405 EXPECT_FALSE(options_.use_control_socket);
406}
407
408TEST_F(DumpOptionsTest, InitializeHelp) {
409 // clang-format off
410 char* argv[] = {
411 const_cast<char*>("dumpstate"),
412 const_cast<char*>("-h")
413 };
414 // clang-format on
415
416 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
417
418 // -h is for help.
419 EXPECT_EQ(status, Dumpstate::RunStatus::HELP);
420}
421
422TEST_F(DumpOptionsTest, InitializeUnknown) {
423 // clang-format off
424 char* argv[] = {
425 const_cast<char*>("dumpstate"),
426 const_cast<char*>("-u") // unknown flag
427 };
428 // clang-format on
429
430 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
431
432 // -u is unknown.
433 EXPECT_EQ(status, Dumpstate::RunStatus::INVALID_INPUT);
434}
435
436TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile1) {
437 options_.do_zip_file = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000438 // Writing to socket = !writing to file.
439 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100440 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000441
442 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100443 EXPECT_TRUE(options_.ValidateOptions());
444}
445
446TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile2) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100447 options_.do_progress_updates = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000448 // Writing to socket = !writing to file.
449 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100450 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000451
452 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100453 EXPECT_TRUE(options_.ValidateOptions());
454}
455
456TEST_F(DumpOptionsTest, ValidateOptionsNeedZipfile) {
457 options_.use_control_socket = true;
458 EXPECT_FALSE(options_.ValidateOptions());
459
460 options_.do_zip_file = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100461 EXPECT_TRUE(options_.ValidateOptions());
462}
463
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100464TEST_F(DumpOptionsTest, ValidateOptionsRemoteMode) {
465 options_.is_remote_mode = true;
466 EXPECT_FALSE(options_.ValidateOptions());
467
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100468 options_.do_zip_file = true;
469 options_.do_add_date = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100470 EXPECT_TRUE(options_.ValidateOptions());
471}
472
Felipe Leme7447d7c2016-11-03 18:12:22 -0700473class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700474 public:
475 void SetUp() {
Felipe Leme46b85da2016-11-21 17:40:45 -0800476 DumpstateBaseTest::SetUp();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700477 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700478 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700479 ds.progress_.reset(new Progress());
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100480 ds.options_.reset(new Dumpstate::DumpOptions());
Felipe Leme4c2d6632016-09-28 14:32:00 -0700481 }
482
483 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700484 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700485 const CommandOptions& options = CommandOptions::DEFAULT) {
486 CaptureStdout();
487 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700488 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700489 out = GetCapturedStdout();
490 err = GetCapturedStderr();
491 return status;
492 }
493
Felipe Lemecef02982016-10-03 17:22:22 -0700494 // Dumps a file and capture `stdout` and `stderr`.
495 int DumpFile(const std::string& title, const std::string& path) {
496 CaptureStdout();
497 CaptureStderr();
498 int status = ds.DumpFile(title, path);
499 out = GetCapturedStdout();
500 err = GetCapturedStderr();
501 return status;
502 }
503
Nandana Dutt402a8392019-06-14 14:25:13 +0100504 void SetProgress(long progress, long initial_max) {
505 ds.last_reported_percent_progress_ = 0;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100506 ds.options_->do_progress_updates = true;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700507 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
508 }
509
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100510 std::string GetProgressMessage(int progress, int max,
511 int old_max = 0, bool update_progress = true) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700512 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
513 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700514
Felipe Leme7447d7c2016-11-03 18:12:22 -0700515 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700516
Felipe Leme009ecbb2016-11-07 10:18:44 -0800517 std::string message = "";
Felipe Leme75876a22016-10-27 16:31:27 -0700518 if (max_increased) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800519 message =
Felipe Leme7447d7c2016-11-03 18:12:22 -0700520 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700521 }
522
Felipe Leme009ecbb2016-11-07 10:18:44 -0800523 if (update_progress) {
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100524 message += android::base::StringPrintf("Setting progress: %d/%d (%d%%)\n",
525 progress, max, (100 * progress / max));
Felipe Leme009ecbb2016-11-07 10:18:44 -0800526 }
527
528 return message;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700529 }
530
Felipe Leme4c2d6632016-09-28 14:32:00 -0700531 // `stdout` and `stderr` from the last command ran.
532 std::string out, err;
533
Felipe Lemefd8affa2016-09-30 17:38:57 -0700534 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700535};
536
537TEST_F(DumpstateTest, RunCommandNoArgs) {
538 EXPECT_EQ(-1, RunCommand("", {}));
539}
540
541TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700542 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700543 EXPECT_THAT(out, StrEq("stdout\n"));
544 EXPECT_THAT(err, StrEq("stderr\n"));
545}
546
547TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700548 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700549 EXPECT_THAT(err, StrEq("stderr\n"));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700550 // The duration may not get output, depending on how long it takes,
551 // so we just check the prefix.
Felipe Lemefd8affa2016-09-30 17:38:57 -0700552 EXPECT_THAT(out,
Nandana Dutt47527b52019-03-29 15:34:36 +0000553 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700554}
555
Felipe Lemefd8affa2016-09-30 17:38:57 -0700556TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700557 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700558 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700559 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
560 EXPECT_THAT(out, StrEq("stdout\n"));
561 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
562}
563
564TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700565 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700566 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700567 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700568 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700569}
570
571TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700572 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700573 EXPECT_THAT(err, IsEmpty());
574 EXPECT_THAT(out, StrEq("one\n"));
575}
576
Felipe Lemefd8affa2016-09-30 17:38:57 -0700577TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700578 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700579 EXPECT_THAT(err, IsEmpty());
580 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
581}
582
583TEST_F(DumpstateTest, RunCommandDryRun) {
584 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700585 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700586 // The duration may not get output, depending on how long it takes,
587 // so we just check the prefix.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700588 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Nandana Dutt47527b52019-03-29 15:34:36 +0000589 ") ------\n\t(skipped on dry run)\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700590 EXPECT_THAT(err, IsEmpty());
591}
592
593TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
594 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700595 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700596 EXPECT_THAT(out, IsEmpty());
597 EXPECT_THAT(err, IsEmpty());
598}
599
600TEST_F(DumpstateTest, RunCommandDryRunAlways) {
601 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700602 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700603 EXPECT_THAT(out, StrEq("stdout\n"));
604 EXPECT_THAT(err, StrEq("stderr\n"));
605}
606
Felipe Lemefd8affa2016-09-30 17:38:57 -0700607TEST_F(DumpstateTest, RunCommandNotFound) {
608 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
609 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
610 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
611}
612
613TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700614 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
615 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700616 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700617 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700618 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700619}
620
621TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700622 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700623 // We don't know the exit code, so check just the prefix.
624 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700625 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700626 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700627 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700628}
629
630TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700631 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700632 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700633 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700634 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700635 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700636 " --sleep 2' timed out after 1"));
637}
638
639TEST_F(DumpstateTest, RunCommandIsKilled) {
640 CaptureStdout();
641 CaptureStderr();
642
643 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700644 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700645 CommandOptions::WithTimeout(100).Always().Build()));
646 });
647
648 // Capture pid and pre-sleep output.
649 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
650 std::string err = GetCapturedStderr();
651 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
652
653 std::string out = GetCapturedStdout();
654 std::vector<std::string> lines = android::base::Split(out, "\n");
655 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
656
657 int pid = atoi(lines[0].c_str());
658 EXPECT_THAT(lines[1], StrEq("stdout line1"));
659 EXPECT_THAT(lines[2], IsEmpty()); // \n
660
661 // Then kill the process.
662 CaptureStdout();
663 CaptureStderr();
664 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
665 t.join();
666
667 // Finally, check output after murder.
668 out = GetCapturedStdout();
669 err = GetCapturedStderr();
670
Felipe Leme7447d7c2016-11-03 18:12:22 -0700671 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700672 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700673 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700674 " --pid --sleep 20' failed: killed by signal 15\n"));
675}
676
Felipe Leme75876a22016-10-27 16:31:27 -0700677TEST_F(DumpstateTest, RunCommandProgress) {
678 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
679 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700680 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700681
Nandana Duttbabf6c72019-01-15 14:11:12 +0000682 EXPECT_CALL(*listener, onProgress(66)); // 20/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700683 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100684 std::string progress_message = GetProgressMessage(20, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700685 EXPECT_THAT(out, StrEq("stdout\n"));
686 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
687
Nandana Dutt402a8392019-06-14 14:25:13 +0100688 EXPECT_CALL(*listener, onProgress(80)); // 24/30 %
689 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100690 progress_message = GetProgressMessage(24, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700691 EXPECT_THAT(out, StrEq("stdout\n"));
692 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
693
694 // Make sure command ran while in dry_run is counted.
695 SetDryRun(true);
Nandana Dutt402a8392019-06-14 14:25:13 +0100696 EXPECT_CALL(*listener, onProgress(90)); // 27/30 %
697 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100698 progress_message = GetProgressMessage(27, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700699 EXPECT_THAT(out, IsEmpty());
700 EXPECT_THAT(err, StrEq(progress_message));
701
Nandana Dutt402a8392019-06-14 14:25:13 +0100702 SetDryRun(false);
703 EXPECT_CALL(*listener, onProgress(96)); // 29/30 %
704 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(2).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100705 progress_message = GetProgressMessage(29, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800706 EXPECT_THAT(out, StrEq("stdout\n"));
707 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
708
Nandana Dutt402a8392019-06-14 14:25:13 +0100709 EXPECT_CALL(*listener, onProgress(100)); // 30/30 %
Felipe Leme009ecbb2016-11-07 10:18:44 -0800710 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100711 progress_message = GetProgressMessage(30, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800712 EXPECT_THAT(out, StrEq("stdout\n"));
713 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
714
715 ds.listener_.clear();
716}
717
Felipe Lemed80e6b62016-10-03 13:08:14 -0700718TEST_F(DumpstateTest, RunCommandDropRoot) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800719 if (!IsStandalone()) {
720 // TODO: temporarily disabled because it might cause other tests to fail after dropping
721 // to Shell - need to refactor tests to avoid this problem)
722 MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n")
723 return;
724 }
Felipe Lemed80e6b62016-10-03 13:08:14 -0700725 // First check root case - only available when running with 'adb root'.
726 uid_t uid = getuid();
727 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700728 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700729 EXPECT_THAT(out, StrEq("0\nstdout\n"));
730 EXPECT_THAT(err, StrEq("stderr\n"));
731 return;
732 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700733 // Then run dropping root.
734 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700735 CommandOptions::WithTimeout(1).DropRoot().Build()));
736 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700737 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700738}
739
740TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800741 if (!IsStandalone()) {
742 // TODO: temporarily disabled because it might cause other tests to fail after dropping
743 // to Shell - need to refactor tests to avoid this problem)
744 MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n")
745 return;
746 }
Felipe Lemef0292972016-11-22 13:57:05 -0800747 if (!PropertiesHelper::IsUserBuild()) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700748 // Emulates user build if necessarily.
749 SetBuildType("user");
750 }
751
752 DropRoot();
753
Felipe Leme7447d7c2016-11-03 18:12:22 -0700754 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700755
756 // We don't know the exact path of su, so we just check for the 'root ...' commands
757 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700758 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700759 EXPECT_THAT(err, IsEmpty());
760}
761
Felipe Leme46b85da2016-11-21 17:40:45 -0800762TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
763 if (!IsStandalone()) {
764 // TODO: temporarily disabled because it might cause other tests to fail after dropping
765 // to Shell - need to refactor tests to avoid this problem)
766 MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n")
767 return;
768 }
Felipe Lemef0292972016-11-22 13:57:05 -0800769 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800770 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
771 return;
772 }
773
774 DropRoot();
775
776 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
777 CommandOptions::WithTimeout(1).AsRoot().Build()));
778
779 EXPECT_THAT(out, StrEq("0\nstdout\n"));
780 EXPECT_THAT(err, StrEq("stderr\n"));
781}
782
Nandana Dutt4b392be2018-11-02 16:17:05 +0000783TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild_withUnroot) {
784 if (!IsStandalone()) {
785 // TODO: temporarily disabled because it might cause other tests to fail after dropping
786 // to Shell - need to refactor tests to avoid this problem)
787 MYLOGE(
788 "Skipping DumpstateTest.RunCommandAsRootNonUserBuild_withUnroot() "
789 "on test suite\n")
790 return;
791 }
792 if (PropertiesHelper::IsUserBuild()) {
793 ALOGI("Skipping RunCommandAsRootNonUserBuild_withUnroot on user builds\n");
794 return;
795 }
796
797 // Same test as above, but with unroot property set, which will override su availability.
798 SetUnroot(true);
799 DropRoot();
800
801 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
802 CommandOptions::WithTimeout(1).AsRoot().Build()));
803
804 // AsRoot is ineffective.
805 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
806 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
807}
808
Yifan Hong48e83a12017-10-03 14:10:07 -0700809TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) {
810 if (!IsStandalone()) {
811 // TODO: temporarily disabled because it might cause other tests to fail after dropping
812 // to Shell - need to refactor tests to avoid this problem)
813 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
814 return;
815 }
816 if (!PropertiesHelper::IsUserBuild()) {
817 // Emulates user build if necessarily.
818 SetBuildType("user");
819 }
820
821 DropRoot();
822
823 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
824 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
825
826 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
827 EXPECT_THAT(err, StrEq("stderr\n"));
828}
829
830TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) {
831 if (!IsStandalone()) {
832 // TODO: temporarily disabled because it might cause other tests to fail after dropping
833 // to Shell - need to refactor tests to avoid this problem)
834 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
835 return;
836 }
837 if (PropertiesHelper::IsUserBuild()) {
838 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
839 return;
840 }
841
842 DropRoot();
843
844 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
845 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
846
847 EXPECT_THAT(out, StrEq("0\nstdout\n"));
848 EXPECT_THAT(err, StrEq("stderr\n"));
849}
850
Nandana Dutt4b392be2018-11-02 16:17:05 +0000851TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild_withUnroot) {
852 if (!IsStandalone()) {
853 // TODO: temporarily disabled because it might cause other tests to fail after dropping
854 // to Shell - need to refactor tests to avoid this problem)
855 MYLOGE(
856 "Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild_withUnroot() "
857 "on test suite\n")
858 return;
859 }
860 if (PropertiesHelper::IsUserBuild()) {
861 ALOGI("Skipping RunCommandAsRootIfAvailableOnDebugBuild_withUnroot on user builds\n");
862 return;
863 }
864 // Same test as above, but with unroot property set, which will override su availability.
865 SetUnroot(true);
866
867 DropRoot();
868
869 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
870 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
871
872 // It's a userdebug build, so "su root" should be available, but unroot=true overrides it.
873 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
874 EXPECT_THAT(err, StrEq("stderr\n"));
875}
876
Felipe Lemecef02982016-10-03 17:22:22 -0700877TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
878 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
879 EXPECT_THAT(out,
880 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
881 EXPECT_THAT(err, IsEmpty());
882}
883
884TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
885 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
886 EXPECT_THAT(err, IsEmpty());
Greg Kaiser3a811c12019-05-21 12:48:59 -0700887 // The duration may not get output, depending on how long it takes,
888 // so we just check the prefix.
Felipe Lemecef02982016-10-03 17:22:22 -0700889 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
890 "such file or directory\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700891}
892
893TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700894 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700895 EXPECT_THAT(err, IsEmpty());
896 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
897}
898
899TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700900 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700901 EXPECT_THAT(err, IsEmpty());
902 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
903}
904
905TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700906 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700907 EXPECT_THAT(err, IsEmpty());
908 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
909}
910
911TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700912 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700913 EXPECT_THAT(err, IsEmpty());
914 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
915}
916
917TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
918 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700919 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700920 EXPECT_THAT(err, IsEmpty());
921 EXPECT_THAT(out, IsEmpty());
922}
923
924TEST_F(DumpstateTest, DumpFileOnDryRun) {
925 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700926 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700927 EXPECT_THAT(err, IsEmpty());
Felipe Leme46b85da2016-11-21 17:40:45 -0800928 EXPECT_THAT(
929 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
Nandana Dutt47527b52019-03-29 15:34:36 +0000930 EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700931}
932
Felipe Leme75876a22016-10-27 16:31:27 -0700933TEST_F(DumpstateTest, DumpFileUpdateProgress) {
934 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
935 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700936 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700937
Nandana Duttbabf6c72019-01-15 14:11:12 +0000938 EXPECT_CALL(*listener, onProgress(16)); // 5/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700939 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -0700940
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100941 std::string progress_message = GetProgressMessage(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
Felipe Leme75876a22016-10-27 16:31:27 -0700942 EXPECT_THAT(err, StrEq(progress_message));
943 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
944
945 ds.listener_.clear();
946}
947
Felipe Leme7447d7c2016-11-03 18:12:22 -0700948class DumpstateServiceTest : public DumpstateBaseTest {
Felipe Leme75876a22016-10-27 16:31:27 -0700949 public:
950 DumpstateService dss;
951};
952
Felipe Leme7447d7c2016-11-03 18:12:22 -0700953class ProgressTest : public DumpstateBaseTest {
954 public:
955 Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {
956 return Progress(max, growth_factor, path);
957 }
958
959 void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) {
960 std::string expected_content =
961 android::base::StringPrintf("%d %d\n", expected_runs, expected_average);
962 std::string actual_content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800963 ReadFileToString(path, &actual_content);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700964 ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path;
965 }
966};
967
968TEST_F(ProgressTest, SimpleTest) {
969 Progress progress;
970 EXPECT_EQ(0, progress.Get());
971 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
972 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
973
974 bool max_increased = progress.Inc(1);
975 EXPECT_EQ(1, progress.Get());
976 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
977 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
978 EXPECT_FALSE(max_increased);
979
980 // Ignore negative increase.
981 max_increased = progress.Inc(-1);
982 EXPECT_EQ(1, progress.Get());
983 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
984 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
985 EXPECT_FALSE(max_increased);
986}
987
988TEST_F(ProgressTest, MaxGrowsInsideNewRange) {
989 Progress progress = GetInstance(10, 1.2); // 20% growth factor
990 EXPECT_EQ(0, progress.Get());
991 EXPECT_EQ(10, progress.GetInitialMax());
992 EXPECT_EQ(10, progress.GetMax());
993
994 // No increase
995 bool max_increased = progress.Inc(10);
996 EXPECT_EQ(10, progress.Get());
997 EXPECT_EQ(10, progress.GetMax());
998 EXPECT_FALSE(max_increased);
999
1000 // Increase, with new value < max*20%
1001 max_increased = progress.Inc(1);
1002 EXPECT_EQ(11, progress.Get());
1003 EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13
1004 EXPECT_TRUE(max_increased);
1005}
1006
1007TEST_F(ProgressTest, MaxGrowsOutsideNewRange) {
1008 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1009 EXPECT_EQ(0, progress.Get());
1010 EXPECT_EQ(10, progress.GetInitialMax());
1011 EXPECT_EQ(10, progress.GetMax());
1012
1013 // No increase
1014 bool max_increased = progress.Inc(10);
1015 EXPECT_EQ(10, progress.Get());
1016 EXPECT_EQ(10, progress.GetMax());
1017 EXPECT_FALSE(max_increased);
1018
1019 // Increase, with new value > max*20%
1020 max_increased = progress.Inc(5);
1021 EXPECT_EQ(15, progress.Get());
1022 EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18
1023 EXPECT_TRUE(max_increased);
1024}
1025
1026TEST_F(ProgressTest, InvalidPath) {
1027 Progress progress("/devil/null");
1028 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1029}
1030
1031TEST_F(ProgressTest, EmptyFile) {
1032 Progress progress(CopyTextFileFixture("empty-file.txt"));
1033 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1034}
1035
1036TEST_F(ProgressTest, InvalidLine1stEntryNAN) {
1037 Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt"));
1038 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1039}
1040
1041TEST_F(ProgressTest, InvalidLine2ndEntryNAN) {
1042 Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt"));
1043 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1044}
1045
1046TEST_F(ProgressTest, InvalidLineBothNAN) {
1047 Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt"));
1048 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1049}
1050
1051TEST_F(ProgressTest, InvalidLine1stEntryNegative) {
1052 Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt"));
1053 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1054}
1055
1056TEST_F(ProgressTest, InvalidLine2ndEntryNegative) {
1057 Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt"));
1058 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1059}
1060
1061TEST_F(ProgressTest, InvalidLine1stEntryTooBig) {
1062 Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt"));
1063 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1064}
1065
1066TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) {
1067 Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt"));
1068 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1069}
1070
1071// Tests stats are properly saved when the file does not exists.
1072TEST_F(ProgressTest, FirstTime) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001073 if (!IsStandalone()) {
1074 // TODO: temporarily disabled because it's failing when running as suite
1075 MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n")
1076 return;
1077 }
1078
Felipe Leme7447d7c2016-11-03 18:12:22 -07001079 std::string path = kTestDataPath + "FirstTime.txt";
1080 android::base::RemoveFileIfExists(path);
1081
1082 Progress run1(path);
1083 EXPECT_EQ(0, run1.Get());
1084 EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax());
1085 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1086
1087 bool max_increased = run1.Inc(20);
1088 EXPECT_EQ(20, run1.Get());
1089 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1090 EXPECT_FALSE(max_increased);
1091
1092 run1.Save();
1093 AssertStats(path, 1, 20);
1094}
1095
1096// Tests what happens when the persistent settings contains the average duration of 1 run.
1097// Data on file is 1 run and 109 average.
1098TEST_F(ProgressTest, SecondTime) {
1099 std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt");
1100
1101 Progress run1 = GetInstance(-42, 1.2, path);
1102 EXPECT_EQ(0, run1.Get());
1103 EXPECT_EQ(10, run1.GetInitialMax());
1104 EXPECT_EQ(10, run1.GetMax());
1105
1106 bool max_increased = run1.Inc(20);
1107 EXPECT_EQ(20, run1.Get());
1108 EXPECT_EQ(24, run1.GetMax());
1109 EXPECT_TRUE(max_increased);
1110
1111 // Average now is 2 runs and (10 + 20)/ 2 = 15
1112 run1.Save();
1113 AssertStats(path, 2, 15);
1114
1115 Progress run2 = GetInstance(-42, 1.2, path);
1116 EXPECT_EQ(0, run2.Get());
1117 EXPECT_EQ(15, run2.GetInitialMax());
1118 EXPECT_EQ(15, run2.GetMax());
1119
1120 max_increased = run2.Inc(25);
1121 EXPECT_EQ(25, run2.Get());
1122 EXPECT_EQ(30, run2.GetMax());
1123 EXPECT_TRUE(max_increased);
1124
1125 // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18
1126 run2.Save();
1127 AssertStats(path, 3, 18);
1128
1129 Progress run3 = GetInstance(-42, 1.2, path);
1130 EXPECT_EQ(0, run3.Get());
1131 EXPECT_EQ(18, run3.GetInitialMax());
1132 EXPECT_EQ(18, run3.GetMax());
1133
1134 // Make sure average decreases as well
1135 max_increased = run3.Inc(5);
1136 EXPECT_EQ(5, run3.Get());
1137 EXPECT_EQ(18, run3.GetMax());
1138 EXPECT_FALSE(max_increased);
1139
1140 // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14
1141 run3.Save();
1142 AssertStats(path, 4, 14);
1143}
1144
1145// Tests what happens when the persistent settings contains the average duration of 2 runs.
1146// Data on file is 2 runs and 15 average.
1147TEST_F(ProgressTest, ThirdTime) {
1148 std::string path = CopyTextFileFixture("stats-two-runs.txt");
1149 AssertStats(path, 2, 15); // Sanity check
1150
1151 Progress run1 = GetInstance(-42, 1.2, path);
1152 EXPECT_EQ(0, run1.Get());
1153 EXPECT_EQ(15, run1.GetInitialMax());
1154 EXPECT_EQ(15, run1.GetMax());
1155
1156 bool max_increased = run1.Inc(20);
1157 EXPECT_EQ(20, run1.Get());
1158 EXPECT_EQ(24, run1.GetMax());
1159 EXPECT_TRUE(max_increased);
1160
1161 // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16
1162 run1.Save();
1163 AssertStats(path, 3, 16);
1164}
1165
Felipe Leme46b85da2016-11-21 17:40:45 -08001166class DumpstateUtilTest : public DumpstateBaseTest {
1167 public:
1168 void SetUp() {
1169 DumpstateBaseTest::SetUp();
1170 SetDryRun(false);
1171 }
1172
Felipe Leme46b85da2016-11-21 17:40:45 -08001173 void CaptureFdOut() {
Felipe Lemef0292972016-11-22 13:57:05 -08001174 ReadFileToString(path_, &out);
Felipe Leme46b85da2016-11-21 17:40:45 -08001175 }
1176
1177 void CreateFd(const std::string& name) {
1178 path_ = kTestDataPath + name;
1179 MYLOGD("Creating fd for file %s\n", path_.c_str());
1180
1181 fd = TEMP_FAILURE_RETRY(open(path_.c_str(),
1182 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
1183 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
1184 ASSERT_GE(fd, 0) << "could not create FD for path " << path_;
1185 }
1186
1187 // Runs a command into the `fd` and capture `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001188 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme46b85da2016-11-21 17:40:45 -08001189 const CommandOptions& options = CommandOptions::DEFAULT) {
1190 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001191 int status = RunCommandToFd(fd, title, full_command, options);
Felipe Leme46b85da2016-11-21 17:40:45 -08001192 close(fd);
1193
1194 CaptureFdOut();
1195 err = GetCapturedStderr();
1196 return status;
1197 }
1198
1199 // Dumps a file and into the `fd` and `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001200 int DumpFile(const std::string& title, const std::string& path) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001201 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001202 int status = DumpFileToFd(fd, title, path);
Felipe Leme46b85da2016-11-21 17:40:45 -08001203 close(fd);
1204
1205 CaptureFdOut();
1206 err = GetCapturedStderr();
1207 return status;
1208 }
1209
1210 int fd;
1211
1212 // 'fd` output and `stderr` from the last command ran.
1213 std::string out, err;
1214
1215 private:
1216 std::string path_;
1217};
1218
1219TEST_F(DumpstateUtilTest, RunCommandNoArgs) {
Felipe Lemef0292972016-11-22 13:57:05 -08001220 CreateFd("RunCommandNoArgs.txt");
1221 EXPECT_EQ(-1, RunCommand("", {}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001222}
1223
Felipe Lemef0292972016-11-22 13:57:05 -08001224TEST_F(DumpstateUtilTest, RunCommandNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001225 CreateFd("RunCommandWithNoArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001226 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001227 EXPECT_THAT(out, StrEq("stdout\n"));
1228 EXPECT_THAT(err, StrEq("stderr\n"));
1229}
1230
Felipe Lemef0292972016-11-22 13:57:05 -08001231TEST_F(DumpstateUtilTest, RunCommandWithTitle) {
1232 CreateFd("RunCommandWithNoArgs.txt");
1233 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1234 EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
1235 EXPECT_THAT(err, StrEq("stderr\n"));
1236}
1237
Felipe Leme46b85da2016-11-21 17:40:45 -08001238TEST_F(DumpstateUtilTest, RunCommandWithOneArg) {
1239 CreateFd("RunCommandWithOneArg.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001240 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001241 EXPECT_THAT(err, IsEmpty());
1242 EXPECT_THAT(out, StrEq("one\n"));
1243}
1244
1245TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) {
1246 CreateFd("RunCommandWithMultipleArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001247 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001248 EXPECT_THAT(err, IsEmpty());
1249 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
1250}
1251
1252TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) {
1253 CreateFd("RunCommandWithLoggingMessage.txt");
1254 EXPECT_EQ(
Felipe Lemef0292972016-11-22 13:57:05 -08001255 0, RunCommand("", {kSimpleCommand},
Felipe Leme46b85da2016-11-21 17:40:45 -08001256 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
1257 EXPECT_THAT(out, StrEq("stdout\n"));
1258 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
1259}
1260
1261TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) {
1262 CreateFd("RunCommandRedirectStderr.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001263 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
1264 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001265 EXPECT_THAT(out, IsEmpty());
1266 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
1267}
1268
1269TEST_F(DumpstateUtilTest, RunCommandDryRun) {
1270 CreateFd("RunCommandDryRun.txt");
1271 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001272 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1273 EXPECT_THAT(out, StrEq(android::base::StringPrintf(
1274 "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n",
1275 kSimpleCommand.c_str())));
1276 EXPECT_THAT(err, IsEmpty());
1277}
1278
1279TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) {
1280 CreateFd("RunCommandDryRun.txt");
1281 SetDryRun(true);
1282 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001283 EXPECT_THAT(
1284 out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str())));
1285 EXPECT_THAT(err, IsEmpty());
1286}
1287
1288TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) {
1289 CreateFd("RunCommandDryRunAlways.txt");
1290 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001291 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001292 EXPECT_THAT(out, StrEq("stdout\n"));
1293 EXPECT_THAT(err, StrEq("stderr\n"));
1294}
1295
1296TEST_F(DumpstateUtilTest, RunCommandNotFound) {
1297 CreateFd("RunCommandNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001298 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001299 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
1300 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
1301}
1302
1303TEST_F(DumpstateUtilTest, RunCommandFails) {
1304 CreateFd("RunCommandFails.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001305 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001306 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
1307 " --exit 42' failed: exit code 42\n"));
1308 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
1309 " --exit 42' failed: exit code 42\n"));
1310}
1311
1312TEST_F(DumpstateUtilTest, RunCommandCrashes) {
1313 CreateFd("RunCommandCrashes.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001314 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001315 // We don't know the exit code, so check just the prefix.
1316 EXPECT_THAT(
1317 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1318 EXPECT_THAT(
1319 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1320}
1321
Vishnu Nair6921f802017-11-22 09:17:23 -08001322TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001323 CreateFd("RunCommandTimesout.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001324 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1325 CommandOptions::WithTimeout(1).Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001326 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1327 " --sleep 2' timed out after 1"));
1328 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1329 " --sleep 2' timed out after 1"));
1330}
1331
Vishnu Nair6921f802017-11-22 09:17:23 -08001332TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) {
1333 CreateFd("RunCommandTimesout.txt");
1334 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1335 CommandOptions::WithTimeoutInMs(1000).Build()));
1336 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1337 " --sleep 2' timed out after 1"));
1338 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1339 " --sleep 2' timed out after 1"));
1340}
1341
1342
Felipe Leme46b85da2016-11-21 17:40:45 -08001343TEST_F(DumpstateUtilTest, RunCommandIsKilled) {
1344 CreateFd("RunCommandIsKilled.txt");
1345 CaptureStderr();
1346
1347 std::thread t([=]() {
Felipe Lemef0292972016-11-22 13:57:05 -08001348 EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001349 CommandOptions::WithTimeout(100).Always().Build()));
1350 });
1351
1352 // Capture pid and pre-sleep output.
1353 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
1354 std::string err = GetCapturedStderr();
1355 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
1356
1357 CaptureFdOut();
1358 std::vector<std::string> lines = android::base::Split(out, "\n");
1359 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
1360
1361 int pid = atoi(lines[0].c_str());
1362 EXPECT_THAT(lines[1], StrEq("stdout line1"));
1363 EXPECT_THAT(lines[2], IsEmpty()); // \n
1364
1365 // Then kill the process.
1366 CaptureFdOut();
1367 CaptureStderr();
1368 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
1369 t.join();
1370
1371 // Finally, check output after murder.
1372 CaptureFdOut();
1373 err = GetCapturedStderr();
1374
1375 // out starts with the pid, which is an unknown
1376 EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand +
1377 " --pid --sleep 20' failed: killed by signal 15\n"));
1378 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
1379 " --pid --sleep 20' failed: killed by signal 15\n"));
1380}
1381
1382TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) {
1383 if (!IsStandalone()) {
1384 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1385 // to Shell - need to refactor tests to avoid this problem)
1386 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n")
1387 return;
1388 }
1389 CreateFd("RunCommandAsRootUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001390 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001391 // Emulates user build if necessarily.
1392 SetBuildType("user");
1393 }
1394
1395 DropRoot();
1396
Felipe Lemef0292972016-11-22 13:57:05 -08001397 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001398
1399 // We don't know the exact path of su, so we just check for the 'root ...' commands
1400 EXPECT_THAT(out, StartsWith("Skipping"));
1401 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
1402 EXPECT_THAT(err, IsEmpty());
1403}
1404
1405TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) {
1406 if (!IsStandalone()) {
1407 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1408 // to Shell - need to refactor tests to avoid this problem)
1409 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n")
1410 return;
1411 }
1412 CreateFd("RunCommandAsRootNonUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001413 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001414 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1415 return;
1416 }
1417
1418 DropRoot();
1419
Felipe Lemef0292972016-11-22 13:57:05 -08001420 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1421 CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001422
1423 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1424 EXPECT_THAT(err, StrEq("stderr\n"));
1425}
Felipe Leme46b85da2016-11-21 17:40:45 -08001426
Yifan Hong48e83a12017-10-03 14:10:07 -07001427
1428TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) {
1429 if (!IsStandalone()) {
1430 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1431 // to Shell - need to refactor tests to avoid this problem)
1432 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
1433 return;
1434 }
1435 CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt");
1436 if (!PropertiesHelper::IsUserBuild()) {
1437 // Emulates user build if necessarily.
1438 SetBuildType("user");
1439 }
1440
1441 DropRoot();
1442
1443 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1444 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1445
1446 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1447 EXPECT_THAT(err, StrEq("stderr\n"));
1448}
1449
1450TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) {
1451 if (!IsStandalone()) {
1452 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1453 // to Shell - need to refactor tests to avoid this problem)
1454 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
1455 return;
1456 }
1457 CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt");
1458 if (PropertiesHelper::IsUserBuild()) {
1459 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1460 return;
1461 }
1462
1463 DropRoot();
1464
1465 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1466 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1467
1468 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1469 EXPECT_THAT(err, StrEq("stderr\n"));
1470}
1471
Felipe Leme46b85da2016-11-21 17:40:45 -08001472TEST_F(DumpstateUtilTest, RunCommandDropRoot) {
1473 if (!IsStandalone()) {
1474 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1475 // to Shell - need to refactor tests to avoid this problem)
1476 MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n")
1477 return;
1478 }
1479 CreateFd("RunCommandDropRoot.txt");
1480 // First check root case - only available when running with 'adb root'.
1481 uid_t uid = getuid();
1482 if (uid == 0) {
Felipe Lemef0292972016-11-22 13:57:05 -08001483 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001484 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1485 EXPECT_THAT(err, StrEq("stderr\n"));
1486 return;
1487 }
1488 // Then run dropping root.
Felipe Lemef0292972016-11-22 13:57:05 -08001489 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001490 CommandOptions::WithTimeout(1).DropRoot().Build()));
1491 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1492 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
1493}
1494
Felipe Lemef0292972016-11-22 13:57:05 -08001495TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001496 CreateFd("DumpFileNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001497 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001498 EXPECT_THAT(out,
1499 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
1500 EXPECT_THAT(err, IsEmpty());
1501}
1502
Felipe Lemef0292972016-11-22 13:57:05 -08001503TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) {
1504 CreateFd("DumpFileNotFound.txt");
1505 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
1506 EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such "
1507 "file or directory\n"));
1508 EXPECT_THAT(err, IsEmpty());
1509}
1510
Felipe Leme46b85da2016-11-21 17:40:45 -08001511TEST_F(DumpstateUtilTest, DumpFileSingleLine) {
1512 CreateFd("DumpFileSingleLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001513 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001514 EXPECT_THAT(err, IsEmpty());
1515 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1516}
1517
1518TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) {
1519 CreateFd("DumpFileSingleLineWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001520 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001521 EXPECT_THAT(err, IsEmpty());
1522 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
1523}
1524
1525TEST_F(DumpstateUtilTest, DumpFileMultipleLines) {
1526 CreateFd("DumpFileMultipleLines.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001527 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001528 EXPECT_THAT(err, IsEmpty());
1529 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1530}
1531
1532TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) {
1533 CreateFd("DumpFileMultipleLinesWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001534 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001535 EXPECT_THAT(err, IsEmpty());
1536 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1537}
1538
Felipe Lemef0292972016-11-22 13:57:05 -08001539TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) {
1540 CreateFd("DumpFileOnDryRun.txt");
1541 SetDryRun(true);
1542 std::string path = kTestDataPath + "single-line.txt";
1543 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
1544 EXPECT_THAT(err, IsEmpty());
1545 EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n"));
1546}
1547
Felipe Leme46b85da2016-11-21 17:40:45 -08001548TEST_F(DumpstateUtilTest, DumpFileOnDryRun) {
1549 CreateFd("DumpFileOnDryRun.txt");
1550 SetDryRun(true);
1551 std::string path = kTestDataPath + "single-line.txt";
Felipe Lemef0292972016-11-22 13:57:05 -08001552 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001553 EXPECT_THAT(err, IsEmpty());
Felipe Lemef0292972016-11-22 13:57:05 -08001554 EXPECT_THAT(
1555 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
1556 EXPECT_THAT(out, EndsWith("skipped on dry run\n"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001557}
Ecco Park61ffcf72016-10-27 15:46:26 -07001558
Felipe Leme47e9be22016-12-21 15:37:07 -08001559} // namespace dumpstate
1560} // namespace os
1561} // namespace android