blob: 76b996081d67c9e366f7d3aec86a8ec4ed2ef566 [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());
Felipe Leme75876a22016-10-27 16:31:27 -070068
69 protected:
70 MOCK_METHOD0(onAsBinder, IBinder*());
71};
72
Felipe Leme46b85da2016-11-21 17:40:45 -080073static int calls_;
74
Felipe Leme7447d7c2016-11-03 18:12:22 -070075// Base class for all tests in this file
76class DumpstateBaseTest : public Test {
Felipe Leme46b85da2016-11-21 17:40:45 -080077 public:
78 virtual void SetUp() override {
79 calls_++;
Felipe Lemef0292972016-11-22 13:57:05 -080080 SetDryRun(false);
Felipe Leme46b85da2016-11-21 17:40:45 -080081 }
82
Felipe Lemef0292972016-11-22 13:57:05 -080083 void SetDryRun(bool dry_run) const {
84 PropertiesHelper::dry_run_ = dry_run;
85 }
86
87 void SetBuildType(const std::string& build_type) const {
88 PropertiesHelper::build_type_ = build_type;
89 }
90
Nandana Dutt4b392be2018-11-02 16:17:05 +000091 void SetUnroot(bool unroot) const {
92 PropertiesHelper::unroot_ = unroot;
93 }
94
Felipe Lemef0292972016-11-22 13:57:05 -080095 bool IsStandalone() const {
Felipe Leme46b85da2016-11-21 17:40:45 -080096 return calls_ == 1;
97 }
98
Felipe Lemef0292972016-11-22 13:57:05 -080099 void DropRoot() const {
100 DropRootUser();
Felipe Leme46b85da2016-11-21 17:40:45 -0800101 uid_t uid = getuid();
102 ASSERT_EQ(2000, (int)uid);
103 }
104
Felipe Leme7447d7c2016-11-03 18:12:22 -0700105 protected:
106 const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str());
Dan Shie177e8e2019-06-20 11:08:14 -0700107 const std::string kTestDataPath = kTestPath + "/tests/testdata/";
108 const std::string kSimpleCommand = kTestPath + "/dumpstate_test_fixture";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700109 const std::string kEchoCommand = "/system/bin/echo";
110
111 /*
112 * Copies a text file fixture to a temporary file, returning it's path.
113 *
114 * Useful in cases where the test case changes the content of the tile.
115 */
116 std::string CopyTextFileFixture(const std::string& relative_name) {
117 std::string from = kTestDataPath + relative_name;
118 // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it
119 // around for poking when the test fails.
120 std::string to = kTestDataPath + relative_name + ".tmp";
121 ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str());
122 android::base::RemoveFileIfExists(to);
123 CopyTextFile(from, to);
124 return to.c_str();
125 }
126
Felipe Leme46b85da2016-11-21 17:40:45 -0800127 // Need functions that returns void to use assertions -
Felipe Leme7447d7c2016-11-03 18:12:22 -0700128 // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement
Felipe Leme46b85da2016-11-21 17:40:45 -0800129 void ReadFileToString(const std::string& path, std::string* content) {
130 ASSERT_TRUE(android::base::ReadFileToString(path, content))
131 << "could not read contents from " << path;
132 }
133 void WriteStringToFile(const std::string& content, const std::string& path) {
134 ASSERT_TRUE(android::base::WriteStringToFile(content, path))
135 << "could not write contents to " << path;
136 }
137
138 private:
Felipe Leme7447d7c2016-11-03 18:12:22 -0700139 void CopyTextFile(const std::string& from, const std::string& to) {
140 std::string content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800141 ReadFileToString(from, &content);
142 WriteStringToFile(content, to);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700143 }
144};
145
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100146class DumpOptionsTest : public Test {
147 public:
148 virtual ~DumpOptionsTest() {
149 }
150 virtual void SetUp() {
151 options_ = Dumpstate::DumpOptions();
152 }
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000153 void TearDown() {
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000154 }
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100155 Dumpstate::DumpOptions options_;
Abhijeet Kaure370d682019-10-01 16:49:30 +0100156 android::base::unique_fd fd;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100157};
158
159TEST_F(DumpOptionsTest, InitializeNone) {
160 // clang-format off
161 char* argv[] = {
162 const_cast<char*>("dumpstate")
163 };
164 // clang-format on
165
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100166 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
167
168 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000169
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100170 EXPECT_FALSE(options_.do_add_date);
171 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100172 EXPECT_FALSE(options_.use_socket);
173 EXPECT_FALSE(options_.use_control_socket);
174 EXPECT_FALSE(options_.show_header_only);
175 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000176 EXPECT_FALSE(options_.do_fb);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100177 EXPECT_FALSE(options_.do_progress_updates);
178 EXPECT_FALSE(options_.is_remote_mode);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800179 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000180}
181
182TEST_F(DumpOptionsTest, InitializeAdbBugreport) {
183 // clang-format off
184 char* argv[] = {
185 const_cast<char*>("dumpstatez"),
186 const_cast<char*>("-S"),
187 const_cast<char*>("-d"),
188 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000189 };
190 // clang-format on
191
192 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
193
194 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
195 EXPECT_TRUE(options_.do_add_date);
196 EXPECT_TRUE(options_.do_zip_file);
197 EXPECT_TRUE(options_.use_control_socket);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000198
199 // Other options retain default values
200 EXPECT_TRUE(options_.do_vibrate);
201 EXPECT_FALSE(options_.show_header_only);
202 EXPECT_FALSE(options_.do_fb);
203 EXPECT_FALSE(options_.do_progress_updates);
204 EXPECT_FALSE(options_.is_remote_mode);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000205 EXPECT_FALSE(options_.use_socket);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800206 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000207}
208
209TEST_F(DumpOptionsTest, InitializeAdbShellBugreport) {
210 // clang-format off
211 char* argv[] = {
212 const_cast<char*>("dumpstate"),
213 const_cast<char*>("-s"),
214 };
215 // clang-format on
216
217 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
218
219 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
220 EXPECT_TRUE(options_.use_socket);
221
222 // Other options retain default values
223 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000224 EXPECT_FALSE(options_.do_add_date);
225 EXPECT_FALSE(options_.do_zip_file);
226 EXPECT_FALSE(options_.use_control_socket);
227 EXPECT_FALSE(options_.show_header_only);
228 EXPECT_FALSE(options_.do_fb);
229 EXPECT_FALSE(options_.do_progress_updates);
230 EXPECT_FALSE(options_.is_remote_mode);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800231 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000232}
233
234TEST_F(DumpOptionsTest, InitializeFullBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100235 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_FULL, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000236 EXPECT_TRUE(options_.do_add_date);
237 EXPECT_TRUE(options_.do_fb);
238 EXPECT_TRUE(options_.do_zip_file);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800239 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::FULL);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000240
241 // Other options retain default values
242 EXPECT_TRUE(options_.do_vibrate);
243 EXPECT_FALSE(options_.use_control_socket);
244 EXPECT_FALSE(options_.show_header_only);
245 EXPECT_FALSE(options_.do_progress_updates);
246 EXPECT_FALSE(options_.is_remote_mode);
247 EXPECT_FALSE(options_.use_socket);
248 EXPECT_FALSE(options_.do_start_service);
249}
250
251TEST_F(DumpOptionsTest, InitializeInteractiveBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100252 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000253 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000254 EXPECT_TRUE(options_.do_zip_file);
255 EXPECT_TRUE(options_.do_progress_updates);
256 EXPECT_TRUE(options_.do_start_service);
257 EXPECT_FALSE(options_.do_fb);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800258 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::INTERACTIVE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000259
260 // Other options retain default values
261 EXPECT_TRUE(options_.do_vibrate);
262 EXPECT_FALSE(options_.use_control_socket);
263 EXPECT_FALSE(options_.show_header_only);
264 EXPECT_FALSE(options_.is_remote_mode);
265 EXPECT_FALSE(options_.use_socket);
266}
267
268TEST_F(DumpOptionsTest, InitializeRemoteBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100269 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_REMOTE, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000270 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000271 EXPECT_TRUE(options_.do_zip_file);
272 EXPECT_TRUE(options_.is_remote_mode);
273 EXPECT_FALSE(options_.do_vibrate);
274 EXPECT_FALSE(options_.do_fb);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800275 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::REMOTE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000276
277 // Other options retain default values
278 EXPECT_FALSE(options_.use_control_socket);
279 EXPECT_FALSE(options_.show_header_only);
280 EXPECT_FALSE(options_.do_progress_updates);
281 EXPECT_FALSE(options_.use_socket);
282}
283
284TEST_F(DumpOptionsTest, InitializeWearBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100285 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WEAR, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000286 EXPECT_TRUE(options_.do_add_date);
287 EXPECT_TRUE(options_.do_fb);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000288 EXPECT_TRUE(options_.do_zip_file);
289 EXPECT_TRUE(options_.do_progress_updates);
290 EXPECT_TRUE(options_.do_start_service);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800291 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WEAR);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000292
293 // Other options retain default values
294 EXPECT_TRUE(options_.do_vibrate);
295 EXPECT_FALSE(options_.use_control_socket);
296 EXPECT_FALSE(options_.show_header_only);
297 EXPECT_FALSE(options_.is_remote_mode);
298 EXPECT_FALSE(options_.use_socket);
299}
300
301TEST_F(DumpOptionsTest, InitializeTelephonyBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100302 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_TELEPHONY, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000303 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaurd3cca0d2019-03-25 12:04:16 +0000304 EXPECT_FALSE(options_.do_fb);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000305 EXPECT_TRUE(options_.do_zip_file);
306 EXPECT_TRUE(options_.telephony_only);
Hunter Knepshield820f9bc2020-02-05 20:10:53 -0800307 EXPECT_TRUE(options_.do_progress_updates);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800308 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::CONNECTIVITY);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000309
310 // Other options retain default values
311 EXPECT_TRUE(options_.do_vibrate);
312 EXPECT_FALSE(options_.use_control_socket);
313 EXPECT_FALSE(options_.show_header_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000314 EXPECT_FALSE(options_.is_remote_mode);
315 EXPECT_FALSE(options_.use_socket);
316}
317
318TEST_F(DumpOptionsTest, InitializeWifiBugReport) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100319 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WIFI, fd, fd);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000320 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaurd3cca0d2019-03-25 12:04:16 +0000321 EXPECT_FALSE(options_.do_fb);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000322 EXPECT_TRUE(options_.do_zip_file);
323 EXPECT_TRUE(options_.wifi_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800324 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WIFI);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000325
326 // Other options retain default values
327 EXPECT_TRUE(options_.do_vibrate);
328 EXPECT_FALSE(options_.use_control_socket);
329 EXPECT_FALSE(options_.show_header_only);
330 EXPECT_FALSE(options_.do_progress_updates);
331 EXPECT_FALSE(options_.is_remote_mode);
332 EXPECT_FALSE(options_.use_socket);
333}
334
335TEST_F(DumpOptionsTest, InitializeDefaultBugReport) {
336 // default: commandline options are not overridden
337 // clang-format off
338 char* argv[] = {
339 const_cast<char*>("bugreport"),
340 const_cast<char*>("-d"),
341 const_cast<char*>("-p"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000342 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000343 };
344 // clang-format on
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000345 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
346
347 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
348 EXPECT_TRUE(options_.do_add_date);
349 EXPECT_TRUE(options_.do_fb);
350 EXPECT_TRUE(options_.do_zip_file);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800351 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000352
353 // Other options retain default values
354 EXPECT_TRUE(options_.do_vibrate);
355 EXPECT_FALSE(options_.use_control_socket);
356 EXPECT_FALSE(options_.show_header_only);
357 EXPECT_FALSE(options_.do_progress_updates);
358 EXPECT_FALSE(options_.is_remote_mode);
359 EXPECT_FALSE(options_.use_socket);
360 EXPECT_FALSE(options_.wifi_only);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100361}
362
363TEST_F(DumpOptionsTest, InitializePartial1) {
364 // clang-format off
365 char* argv[] = {
366 const_cast<char*>("dumpstate"),
367 const_cast<char*>("-d"),
368 const_cast<char*>("-z"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100369 const_cast<char*>("-s"),
370 const_cast<char*>("-S"),
371
372 };
373 // clang-format on
374
375 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
376
377 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
378 EXPECT_TRUE(options_.do_add_date);
379 EXPECT_TRUE(options_.do_zip_file);
380 // TODO: Maybe we should trim the filename
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100381 EXPECT_TRUE(options_.use_socket);
382 EXPECT_TRUE(options_.use_control_socket);
383
384 // Other options retain default values
385 EXPECT_FALSE(options_.show_header_only);
386 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000387 EXPECT_FALSE(options_.do_fb);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100388 EXPECT_FALSE(options_.do_progress_updates);
389 EXPECT_FALSE(options_.is_remote_mode);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800390 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100391}
392
393TEST_F(DumpOptionsTest, InitializePartial2) {
394 // clang-format off
395 char* argv[] = {
396 const_cast<char*>("dumpstate"),
397 const_cast<char*>("-v"),
398 const_cast<char*>("-q"),
399 const_cast<char*>("-p"),
400 const_cast<char*>("-P"),
401 const_cast<char*>("-R"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100402 };
403 // clang-format on
404
405 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
406
407 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
408 EXPECT_TRUE(options_.show_header_only);
409 EXPECT_FALSE(options_.do_vibrate);
410 EXPECT_TRUE(options_.do_fb);
411 EXPECT_TRUE(options_.do_progress_updates);
412 EXPECT_TRUE(options_.is_remote_mode);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100413
414 // Other options retain default values
415 EXPECT_FALSE(options_.do_add_date);
416 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100417 EXPECT_FALSE(options_.use_socket);
418 EXPECT_FALSE(options_.use_control_socket);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800419 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100420}
421
422TEST_F(DumpOptionsTest, InitializeHelp) {
423 // clang-format off
424 char* argv[] = {
425 const_cast<char*>("dumpstate"),
426 const_cast<char*>("-h")
427 };
428 // clang-format on
429
430 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
431
432 // -h is for help.
433 EXPECT_EQ(status, Dumpstate::RunStatus::HELP);
434}
435
436TEST_F(DumpOptionsTest, InitializeUnknown) {
437 // clang-format off
438 char* argv[] = {
439 const_cast<char*>("dumpstate"),
440 const_cast<char*>("-u") // unknown flag
441 };
442 // clang-format on
443
444 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
445
446 // -u is unknown.
447 EXPECT_EQ(status, Dumpstate::RunStatus::INVALID_INPUT);
448}
449
450TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile1) {
451 options_.do_zip_file = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000452 // Writing to socket = !writing to file.
453 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100454 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000455
456 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100457 EXPECT_TRUE(options_.ValidateOptions());
458}
459
460TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile2) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100461 options_.do_progress_updates = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000462 // Writing to socket = !writing to file.
463 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100464 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000465
466 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100467 EXPECT_TRUE(options_.ValidateOptions());
468}
469
470TEST_F(DumpOptionsTest, ValidateOptionsNeedZipfile) {
471 options_.use_control_socket = true;
472 EXPECT_FALSE(options_.ValidateOptions());
473
474 options_.do_zip_file = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100475 EXPECT_TRUE(options_.ValidateOptions());
476}
477
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100478TEST_F(DumpOptionsTest, ValidateOptionsRemoteMode) {
479 options_.is_remote_mode = true;
480 EXPECT_FALSE(options_.ValidateOptions());
481
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100482 options_.do_zip_file = true;
483 options_.do_add_date = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100484 EXPECT_TRUE(options_.ValidateOptions());
485}
486
Felipe Leme7447d7c2016-11-03 18:12:22 -0700487class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700488 public:
489 void SetUp() {
Felipe Leme46b85da2016-11-21 17:40:45 -0800490 DumpstateBaseTest::SetUp();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700491 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700492 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700493 ds.progress_.reset(new Progress());
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100494 ds.options_.reset(new Dumpstate::DumpOptions());
Felipe Leme4c2d6632016-09-28 14:32:00 -0700495 }
496
497 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700498 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700499 const CommandOptions& options = CommandOptions::DEFAULT) {
500 CaptureStdout();
501 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700502 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700503 out = GetCapturedStdout();
504 err = GetCapturedStderr();
505 return status;
506 }
507
Felipe Lemecef02982016-10-03 17:22:22 -0700508 // Dumps a file and capture `stdout` and `stderr`.
509 int DumpFile(const std::string& title, const std::string& path) {
510 CaptureStdout();
511 CaptureStderr();
512 int status = ds.DumpFile(title, path);
513 out = GetCapturedStdout();
514 err = GetCapturedStderr();
515 return status;
516 }
517
Nandana Dutt402a8392019-06-14 14:25:13 +0100518 void SetProgress(long progress, long initial_max) {
519 ds.last_reported_percent_progress_ = 0;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100520 ds.options_->do_progress_updates = true;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700521 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
522 }
523
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100524 std::string GetProgressMessage(int progress, int max,
525 int old_max = 0, bool update_progress = true) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700526 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
527 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700528
Felipe Leme7447d7c2016-11-03 18:12:22 -0700529 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700530
Felipe Leme009ecbb2016-11-07 10:18:44 -0800531 std::string message = "";
Felipe Leme75876a22016-10-27 16:31:27 -0700532 if (max_increased) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800533 message =
Felipe Leme7447d7c2016-11-03 18:12:22 -0700534 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700535 }
536
Felipe Leme009ecbb2016-11-07 10:18:44 -0800537 if (update_progress) {
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100538 message += android::base::StringPrintf("Setting progress: %d/%d (%d%%)\n",
539 progress, max, (100 * progress / max));
Felipe Leme009ecbb2016-11-07 10:18:44 -0800540 }
541
542 return message;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700543 }
544
Felipe Leme4c2d6632016-09-28 14:32:00 -0700545 // `stdout` and `stderr` from the last command ran.
546 std::string out, err;
547
Felipe Lemefd8affa2016-09-30 17:38:57 -0700548 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700549};
550
551TEST_F(DumpstateTest, RunCommandNoArgs) {
552 EXPECT_EQ(-1, RunCommand("", {}));
553}
554
555TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700556 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700557 EXPECT_THAT(out, StrEq("stdout\n"));
558 EXPECT_THAT(err, StrEq("stderr\n"));
559}
560
561TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700562 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700563 EXPECT_THAT(err, StrEq("stderr\n"));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700564 // The duration may not get output, depending on how long it takes,
565 // so we just check the prefix.
Felipe Lemefd8affa2016-09-30 17:38:57 -0700566 EXPECT_THAT(out,
Nandana Dutt47527b52019-03-29 15:34:36 +0000567 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700568}
569
Felipe Lemefd8affa2016-09-30 17:38:57 -0700570TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700571 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700572 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700573 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
574 EXPECT_THAT(out, StrEq("stdout\n"));
575 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
576}
577
578TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700579 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700580 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700581 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700582 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700583}
584
585TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700586 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700587 EXPECT_THAT(err, IsEmpty());
588 EXPECT_THAT(out, StrEq("one\n"));
589}
590
Felipe Lemefd8affa2016-09-30 17:38:57 -0700591TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700592 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700593 EXPECT_THAT(err, IsEmpty());
594 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
595}
596
597TEST_F(DumpstateTest, RunCommandDryRun) {
598 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700599 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700600 // The duration may not get output, depending on how long it takes,
601 // so we just check the prefix.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700602 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Nandana Dutt47527b52019-03-29 15:34:36 +0000603 ") ------\n\t(skipped on dry run)\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700604 EXPECT_THAT(err, IsEmpty());
605}
606
607TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
608 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700609 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700610 EXPECT_THAT(out, IsEmpty());
611 EXPECT_THAT(err, IsEmpty());
612}
613
614TEST_F(DumpstateTest, RunCommandDryRunAlways) {
615 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700616 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700617 EXPECT_THAT(out, StrEq("stdout\n"));
618 EXPECT_THAT(err, StrEq("stderr\n"));
619}
620
Felipe Lemefd8affa2016-09-30 17:38:57 -0700621TEST_F(DumpstateTest, RunCommandNotFound) {
622 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
623 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
624 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
625}
626
627TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700628 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
629 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700630 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700631 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700632 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700633}
634
635TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700636 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700637 // We don't know the exit code, so check just the prefix.
638 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700639 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700640 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700641 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700642}
643
644TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700645 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700646 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700647 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700648 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700649 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700650 " --sleep 2' timed out after 1"));
651}
652
653TEST_F(DumpstateTest, RunCommandIsKilled) {
654 CaptureStdout();
655 CaptureStderr();
656
657 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700658 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700659 CommandOptions::WithTimeout(100).Always().Build()));
660 });
661
662 // Capture pid and pre-sleep output.
663 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
664 std::string err = GetCapturedStderr();
665 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
666
667 std::string out = GetCapturedStdout();
668 std::vector<std::string> lines = android::base::Split(out, "\n");
669 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
670
671 int pid = atoi(lines[0].c_str());
672 EXPECT_THAT(lines[1], StrEq("stdout line1"));
673 EXPECT_THAT(lines[2], IsEmpty()); // \n
674
675 // Then kill the process.
676 CaptureStdout();
677 CaptureStderr();
678 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
679 t.join();
680
681 // Finally, check output after murder.
682 out = GetCapturedStdout();
683 err = GetCapturedStderr();
684
Felipe Leme7447d7c2016-11-03 18:12:22 -0700685 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700686 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700687 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700688 " --pid --sleep 20' failed: killed by signal 15\n"));
689}
690
Felipe Leme75876a22016-10-27 16:31:27 -0700691TEST_F(DumpstateTest, RunCommandProgress) {
692 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
693 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700694 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700695
Nandana Duttbabf6c72019-01-15 14:11:12 +0000696 EXPECT_CALL(*listener, onProgress(66)); // 20/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700697 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100698 std::string progress_message = GetProgressMessage(20, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700699 EXPECT_THAT(out, StrEq("stdout\n"));
700 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
701
Nandana Dutt402a8392019-06-14 14:25:13 +0100702 EXPECT_CALL(*listener, onProgress(80)); // 24/30 %
703 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100704 progress_message = GetProgressMessage(24, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700705 EXPECT_THAT(out, StrEq("stdout\n"));
706 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
707
708 // Make sure command ran while in dry_run is counted.
709 SetDryRun(true);
Nandana Dutt402a8392019-06-14 14:25:13 +0100710 EXPECT_CALL(*listener, onProgress(90)); // 27/30 %
711 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100712 progress_message = GetProgressMessage(27, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700713 EXPECT_THAT(out, IsEmpty());
714 EXPECT_THAT(err, StrEq(progress_message));
715
Nandana Dutt402a8392019-06-14 14:25:13 +0100716 SetDryRun(false);
717 EXPECT_CALL(*listener, onProgress(96)); // 29/30 %
718 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(2).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100719 progress_message = GetProgressMessage(29, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800720 EXPECT_THAT(out, StrEq("stdout\n"));
721 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
722
Nandana Dutt402a8392019-06-14 14:25:13 +0100723 EXPECT_CALL(*listener, onProgress(100)); // 30/30 %
Felipe Leme009ecbb2016-11-07 10:18:44 -0800724 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100725 progress_message = GetProgressMessage(30, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800726 EXPECT_THAT(out, StrEq("stdout\n"));
727 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
728
729 ds.listener_.clear();
730}
731
Felipe Lemed80e6b62016-10-03 13:08:14 -0700732TEST_F(DumpstateTest, RunCommandDropRoot) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800733 if (!IsStandalone()) {
734 // TODO: temporarily disabled because it might cause other tests to fail after dropping
735 // to Shell - need to refactor tests to avoid this problem)
736 MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n")
737 return;
738 }
Felipe Lemed80e6b62016-10-03 13:08:14 -0700739 // First check root case - only available when running with 'adb root'.
740 uid_t uid = getuid();
741 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700742 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700743 EXPECT_THAT(out, StrEq("0\nstdout\n"));
744 EXPECT_THAT(err, StrEq("stderr\n"));
745 return;
746 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700747 // Then run dropping root.
748 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700749 CommandOptions::WithTimeout(1).DropRoot().Build()));
750 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700751 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700752}
753
754TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800755 if (!IsStandalone()) {
756 // TODO: temporarily disabled because it might cause other tests to fail after dropping
757 // to Shell - need to refactor tests to avoid this problem)
758 MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n")
759 return;
760 }
Felipe Lemef0292972016-11-22 13:57:05 -0800761 if (!PropertiesHelper::IsUserBuild()) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700762 // Emulates user build if necessarily.
763 SetBuildType("user");
764 }
765
766 DropRoot();
767
Felipe Leme7447d7c2016-11-03 18:12:22 -0700768 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700769
770 // We don't know the exact path of su, so we just check for the 'root ...' commands
771 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700772 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700773 EXPECT_THAT(err, IsEmpty());
774}
775
Felipe Leme46b85da2016-11-21 17:40:45 -0800776TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
777 if (!IsStandalone()) {
778 // TODO: temporarily disabled because it might cause other tests to fail after dropping
779 // to Shell - need to refactor tests to avoid this problem)
780 MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n")
781 return;
782 }
Felipe Lemef0292972016-11-22 13:57:05 -0800783 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800784 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
785 return;
786 }
787
788 DropRoot();
789
790 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
791 CommandOptions::WithTimeout(1).AsRoot().Build()));
792
793 EXPECT_THAT(out, StrEq("0\nstdout\n"));
794 EXPECT_THAT(err, StrEq("stderr\n"));
795}
796
Nandana Dutt4b392be2018-11-02 16:17:05 +0000797TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild_withUnroot) {
798 if (!IsStandalone()) {
799 // TODO: temporarily disabled because it might cause other tests to fail after dropping
800 // to Shell - need to refactor tests to avoid this problem)
801 MYLOGE(
802 "Skipping DumpstateTest.RunCommandAsRootNonUserBuild_withUnroot() "
803 "on test suite\n")
804 return;
805 }
806 if (PropertiesHelper::IsUserBuild()) {
807 ALOGI("Skipping RunCommandAsRootNonUserBuild_withUnroot on user builds\n");
808 return;
809 }
810
811 // Same test as above, but with unroot property set, which will override su availability.
812 SetUnroot(true);
813 DropRoot();
814
815 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
816 CommandOptions::WithTimeout(1).AsRoot().Build()));
817
818 // AsRoot is ineffective.
819 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
820 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
821}
822
Yifan Hong48e83a12017-10-03 14:10:07 -0700823TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) {
824 if (!IsStandalone()) {
825 // TODO: temporarily disabled because it might cause other tests to fail after dropping
826 // to Shell - need to refactor tests to avoid this problem)
827 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
828 return;
829 }
830 if (!PropertiesHelper::IsUserBuild()) {
831 // Emulates user build if necessarily.
832 SetBuildType("user");
833 }
834
835 DropRoot();
836
837 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
838 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
839
840 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
841 EXPECT_THAT(err, StrEq("stderr\n"));
842}
843
844TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) {
845 if (!IsStandalone()) {
846 // TODO: temporarily disabled because it might cause other tests to fail after dropping
847 // to Shell - need to refactor tests to avoid this problem)
848 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
849 return;
850 }
851 if (PropertiesHelper::IsUserBuild()) {
852 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
853 return;
854 }
855
856 DropRoot();
857
858 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
859 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
860
861 EXPECT_THAT(out, StrEq("0\nstdout\n"));
862 EXPECT_THAT(err, StrEq("stderr\n"));
863}
864
Nandana Dutt4b392be2018-11-02 16:17:05 +0000865TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild_withUnroot) {
866 if (!IsStandalone()) {
867 // TODO: temporarily disabled because it might cause other tests to fail after dropping
868 // to Shell - need to refactor tests to avoid this problem)
869 MYLOGE(
870 "Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild_withUnroot() "
871 "on test suite\n")
872 return;
873 }
874 if (PropertiesHelper::IsUserBuild()) {
875 ALOGI("Skipping RunCommandAsRootIfAvailableOnDebugBuild_withUnroot on user builds\n");
876 return;
877 }
878 // Same test as above, but with unroot property set, which will override su availability.
879 SetUnroot(true);
880
881 DropRoot();
882
883 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
884 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
885
886 // It's a userdebug build, so "su root" should be available, but unroot=true overrides it.
887 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
888 EXPECT_THAT(err, StrEq("stderr\n"));
889}
890
Felipe Lemecef02982016-10-03 17:22:22 -0700891TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
892 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
893 EXPECT_THAT(out,
894 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
895 EXPECT_THAT(err, IsEmpty());
896}
897
898TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
899 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
900 EXPECT_THAT(err, IsEmpty());
Greg Kaiser3a811c12019-05-21 12:48:59 -0700901 // The duration may not get output, depending on how long it takes,
902 // so we just check the prefix.
Felipe Lemecef02982016-10-03 17:22:22 -0700903 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
904 "such file or directory\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700905}
906
907TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700908 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700909 EXPECT_THAT(err, IsEmpty());
910 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
911}
912
913TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700914 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700915 EXPECT_THAT(err, IsEmpty());
916 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
917}
918
919TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700920 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700921 EXPECT_THAT(err, IsEmpty());
922 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
923}
924
925TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700926 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700927 EXPECT_THAT(err, IsEmpty());
928 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
929}
930
931TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
932 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700933 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700934 EXPECT_THAT(err, IsEmpty());
935 EXPECT_THAT(out, IsEmpty());
936}
937
938TEST_F(DumpstateTest, DumpFileOnDryRun) {
939 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700940 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700941 EXPECT_THAT(err, IsEmpty());
Felipe Leme46b85da2016-11-21 17:40:45 -0800942 EXPECT_THAT(
943 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
Nandana Dutt47527b52019-03-29 15:34:36 +0000944 EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700945}
946
Felipe Leme75876a22016-10-27 16:31:27 -0700947TEST_F(DumpstateTest, DumpFileUpdateProgress) {
948 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
949 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700950 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700951
Nandana Duttbabf6c72019-01-15 14:11:12 +0000952 EXPECT_CALL(*listener, onProgress(16)); // 5/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700953 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -0700954
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100955 std::string progress_message = GetProgressMessage(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
Felipe Leme75876a22016-10-27 16:31:27 -0700956 EXPECT_THAT(err, StrEq(progress_message));
957 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
958
959 ds.listener_.clear();
960}
961
Felipe Leme7447d7c2016-11-03 18:12:22 -0700962class DumpstateServiceTest : public DumpstateBaseTest {
Felipe Leme75876a22016-10-27 16:31:27 -0700963 public:
964 DumpstateService dss;
965};
966
Felipe Leme7447d7c2016-11-03 18:12:22 -0700967class ProgressTest : public DumpstateBaseTest {
968 public:
969 Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {
970 return Progress(max, growth_factor, path);
971 }
972
973 void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) {
974 std::string expected_content =
975 android::base::StringPrintf("%d %d\n", expected_runs, expected_average);
976 std::string actual_content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800977 ReadFileToString(path, &actual_content);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700978 ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path;
979 }
980};
981
982TEST_F(ProgressTest, SimpleTest) {
983 Progress progress;
984 EXPECT_EQ(0, progress.Get());
985 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
986 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
987
988 bool max_increased = progress.Inc(1);
989 EXPECT_EQ(1, progress.Get());
990 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
991 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
992 EXPECT_FALSE(max_increased);
993
994 // Ignore negative increase.
995 max_increased = progress.Inc(-1);
996 EXPECT_EQ(1, progress.Get());
997 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
998 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
999 EXPECT_FALSE(max_increased);
1000}
1001
1002TEST_F(ProgressTest, MaxGrowsInsideNewRange) {
1003 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1004 EXPECT_EQ(0, progress.Get());
1005 EXPECT_EQ(10, progress.GetInitialMax());
1006 EXPECT_EQ(10, progress.GetMax());
1007
1008 // No increase
1009 bool max_increased = progress.Inc(10);
1010 EXPECT_EQ(10, progress.Get());
1011 EXPECT_EQ(10, progress.GetMax());
1012 EXPECT_FALSE(max_increased);
1013
1014 // Increase, with new value < max*20%
1015 max_increased = progress.Inc(1);
1016 EXPECT_EQ(11, progress.Get());
1017 EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13
1018 EXPECT_TRUE(max_increased);
1019}
1020
1021TEST_F(ProgressTest, MaxGrowsOutsideNewRange) {
1022 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1023 EXPECT_EQ(0, progress.Get());
1024 EXPECT_EQ(10, progress.GetInitialMax());
1025 EXPECT_EQ(10, progress.GetMax());
1026
1027 // No increase
1028 bool max_increased = progress.Inc(10);
1029 EXPECT_EQ(10, progress.Get());
1030 EXPECT_EQ(10, progress.GetMax());
1031 EXPECT_FALSE(max_increased);
1032
1033 // Increase, with new value > max*20%
1034 max_increased = progress.Inc(5);
1035 EXPECT_EQ(15, progress.Get());
1036 EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18
1037 EXPECT_TRUE(max_increased);
1038}
1039
1040TEST_F(ProgressTest, InvalidPath) {
1041 Progress progress("/devil/null");
1042 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1043}
1044
1045TEST_F(ProgressTest, EmptyFile) {
1046 Progress progress(CopyTextFileFixture("empty-file.txt"));
1047 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1048}
1049
1050TEST_F(ProgressTest, InvalidLine1stEntryNAN) {
1051 Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt"));
1052 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1053}
1054
1055TEST_F(ProgressTest, InvalidLine2ndEntryNAN) {
1056 Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt"));
1057 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1058}
1059
1060TEST_F(ProgressTest, InvalidLineBothNAN) {
1061 Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt"));
1062 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1063}
1064
1065TEST_F(ProgressTest, InvalidLine1stEntryNegative) {
1066 Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt"));
1067 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1068}
1069
1070TEST_F(ProgressTest, InvalidLine2ndEntryNegative) {
1071 Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt"));
1072 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1073}
1074
1075TEST_F(ProgressTest, InvalidLine1stEntryTooBig) {
1076 Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt"));
1077 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1078}
1079
1080TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) {
1081 Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt"));
1082 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1083}
1084
1085// Tests stats are properly saved when the file does not exists.
1086TEST_F(ProgressTest, FirstTime) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001087 if (!IsStandalone()) {
1088 // TODO: temporarily disabled because it's failing when running as suite
1089 MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n")
1090 return;
1091 }
1092
Felipe Leme7447d7c2016-11-03 18:12:22 -07001093 std::string path = kTestDataPath + "FirstTime.txt";
1094 android::base::RemoveFileIfExists(path);
1095
1096 Progress run1(path);
1097 EXPECT_EQ(0, run1.Get());
1098 EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax());
1099 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1100
1101 bool max_increased = run1.Inc(20);
1102 EXPECT_EQ(20, run1.Get());
1103 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1104 EXPECT_FALSE(max_increased);
1105
1106 run1.Save();
1107 AssertStats(path, 1, 20);
1108}
1109
1110// Tests what happens when the persistent settings contains the average duration of 1 run.
1111// Data on file is 1 run and 109 average.
1112TEST_F(ProgressTest, SecondTime) {
1113 std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt");
1114
1115 Progress run1 = GetInstance(-42, 1.2, path);
1116 EXPECT_EQ(0, run1.Get());
1117 EXPECT_EQ(10, run1.GetInitialMax());
1118 EXPECT_EQ(10, run1.GetMax());
1119
1120 bool max_increased = run1.Inc(20);
1121 EXPECT_EQ(20, run1.Get());
1122 EXPECT_EQ(24, run1.GetMax());
1123 EXPECT_TRUE(max_increased);
1124
1125 // Average now is 2 runs and (10 + 20)/ 2 = 15
1126 run1.Save();
1127 AssertStats(path, 2, 15);
1128
1129 Progress run2 = GetInstance(-42, 1.2, path);
1130 EXPECT_EQ(0, run2.Get());
1131 EXPECT_EQ(15, run2.GetInitialMax());
1132 EXPECT_EQ(15, run2.GetMax());
1133
1134 max_increased = run2.Inc(25);
1135 EXPECT_EQ(25, run2.Get());
1136 EXPECT_EQ(30, run2.GetMax());
1137 EXPECT_TRUE(max_increased);
1138
1139 // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18
1140 run2.Save();
1141 AssertStats(path, 3, 18);
1142
1143 Progress run3 = GetInstance(-42, 1.2, path);
1144 EXPECT_EQ(0, run3.Get());
1145 EXPECT_EQ(18, run3.GetInitialMax());
1146 EXPECT_EQ(18, run3.GetMax());
1147
1148 // Make sure average decreases as well
1149 max_increased = run3.Inc(5);
1150 EXPECT_EQ(5, run3.Get());
1151 EXPECT_EQ(18, run3.GetMax());
1152 EXPECT_FALSE(max_increased);
1153
1154 // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14
1155 run3.Save();
1156 AssertStats(path, 4, 14);
1157}
1158
1159// Tests what happens when the persistent settings contains the average duration of 2 runs.
1160// Data on file is 2 runs and 15 average.
1161TEST_F(ProgressTest, ThirdTime) {
1162 std::string path = CopyTextFileFixture("stats-two-runs.txt");
1163 AssertStats(path, 2, 15); // Sanity check
1164
1165 Progress run1 = GetInstance(-42, 1.2, path);
1166 EXPECT_EQ(0, run1.Get());
1167 EXPECT_EQ(15, run1.GetInitialMax());
1168 EXPECT_EQ(15, run1.GetMax());
1169
1170 bool max_increased = run1.Inc(20);
1171 EXPECT_EQ(20, run1.Get());
1172 EXPECT_EQ(24, run1.GetMax());
1173 EXPECT_TRUE(max_increased);
1174
1175 // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16
1176 run1.Save();
1177 AssertStats(path, 3, 16);
1178}
1179
Felipe Leme46b85da2016-11-21 17:40:45 -08001180class DumpstateUtilTest : public DumpstateBaseTest {
1181 public:
1182 void SetUp() {
1183 DumpstateBaseTest::SetUp();
1184 SetDryRun(false);
1185 }
1186
Felipe Leme46b85da2016-11-21 17:40:45 -08001187 void CaptureFdOut() {
Felipe Lemef0292972016-11-22 13:57:05 -08001188 ReadFileToString(path_, &out);
Felipe Leme46b85da2016-11-21 17:40:45 -08001189 }
1190
1191 void CreateFd(const std::string& name) {
1192 path_ = kTestDataPath + name;
1193 MYLOGD("Creating fd for file %s\n", path_.c_str());
1194
1195 fd = TEMP_FAILURE_RETRY(open(path_.c_str(),
1196 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
1197 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
1198 ASSERT_GE(fd, 0) << "could not create FD for path " << path_;
1199 }
1200
1201 // Runs a command into the `fd` and capture `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001202 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme46b85da2016-11-21 17:40:45 -08001203 const CommandOptions& options = CommandOptions::DEFAULT) {
1204 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001205 int status = RunCommandToFd(fd, title, full_command, options);
Felipe Leme46b85da2016-11-21 17:40:45 -08001206 close(fd);
1207
1208 CaptureFdOut();
1209 err = GetCapturedStderr();
1210 return status;
1211 }
1212
1213 // Dumps a file and into the `fd` and `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001214 int DumpFile(const std::string& title, const std::string& path) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001215 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001216 int status = DumpFileToFd(fd, title, path);
Felipe Leme46b85da2016-11-21 17:40:45 -08001217 close(fd);
1218
1219 CaptureFdOut();
1220 err = GetCapturedStderr();
1221 return status;
1222 }
1223
1224 int fd;
1225
1226 // 'fd` output and `stderr` from the last command ran.
1227 std::string out, err;
1228
1229 private:
1230 std::string path_;
1231};
1232
1233TEST_F(DumpstateUtilTest, RunCommandNoArgs) {
Felipe Lemef0292972016-11-22 13:57:05 -08001234 CreateFd("RunCommandNoArgs.txt");
1235 EXPECT_EQ(-1, RunCommand("", {}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001236}
1237
Felipe Lemef0292972016-11-22 13:57:05 -08001238TEST_F(DumpstateUtilTest, RunCommandNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001239 CreateFd("RunCommandWithNoArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001240 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001241 EXPECT_THAT(out, StrEq("stdout\n"));
1242 EXPECT_THAT(err, StrEq("stderr\n"));
1243}
1244
Felipe Lemef0292972016-11-22 13:57:05 -08001245TEST_F(DumpstateUtilTest, RunCommandWithTitle) {
1246 CreateFd("RunCommandWithNoArgs.txt");
1247 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1248 EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
1249 EXPECT_THAT(err, StrEq("stderr\n"));
1250}
1251
Felipe Leme46b85da2016-11-21 17:40:45 -08001252TEST_F(DumpstateUtilTest, RunCommandWithOneArg) {
1253 CreateFd("RunCommandWithOneArg.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001254 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001255 EXPECT_THAT(err, IsEmpty());
1256 EXPECT_THAT(out, StrEq("one\n"));
1257}
1258
1259TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) {
1260 CreateFd("RunCommandWithMultipleArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001261 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001262 EXPECT_THAT(err, IsEmpty());
1263 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
1264}
1265
1266TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) {
1267 CreateFd("RunCommandWithLoggingMessage.txt");
1268 EXPECT_EQ(
Felipe Lemef0292972016-11-22 13:57:05 -08001269 0, RunCommand("", {kSimpleCommand},
Felipe Leme46b85da2016-11-21 17:40:45 -08001270 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
1271 EXPECT_THAT(out, StrEq("stdout\n"));
1272 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
1273}
1274
1275TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) {
1276 CreateFd("RunCommandRedirectStderr.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001277 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
1278 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001279 EXPECT_THAT(out, IsEmpty());
1280 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
1281}
1282
1283TEST_F(DumpstateUtilTest, RunCommandDryRun) {
1284 CreateFd("RunCommandDryRun.txt");
1285 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001286 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1287 EXPECT_THAT(out, StrEq(android::base::StringPrintf(
1288 "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n",
1289 kSimpleCommand.c_str())));
1290 EXPECT_THAT(err, IsEmpty());
1291}
1292
1293TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) {
1294 CreateFd("RunCommandDryRun.txt");
1295 SetDryRun(true);
1296 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001297 EXPECT_THAT(
1298 out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str())));
1299 EXPECT_THAT(err, IsEmpty());
1300}
1301
1302TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) {
1303 CreateFd("RunCommandDryRunAlways.txt");
1304 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001305 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001306 EXPECT_THAT(out, StrEq("stdout\n"));
1307 EXPECT_THAT(err, StrEq("stderr\n"));
1308}
1309
1310TEST_F(DumpstateUtilTest, RunCommandNotFound) {
1311 CreateFd("RunCommandNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001312 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001313 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
1314 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
1315}
1316
1317TEST_F(DumpstateUtilTest, RunCommandFails) {
1318 CreateFd("RunCommandFails.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001319 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001320 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
1321 " --exit 42' failed: exit code 42\n"));
1322 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
1323 " --exit 42' failed: exit code 42\n"));
1324}
1325
1326TEST_F(DumpstateUtilTest, RunCommandCrashes) {
1327 CreateFd("RunCommandCrashes.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001328 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001329 // We don't know the exit code, so check just the prefix.
1330 EXPECT_THAT(
1331 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1332 EXPECT_THAT(
1333 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1334}
1335
Vishnu Nair6921f802017-11-22 09:17:23 -08001336TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001337 CreateFd("RunCommandTimesout.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001338 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1339 CommandOptions::WithTimeout(1).Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001340 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1341 " --sleep 2' timed out after 1"));
1342 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1343 " --sleep 2' timed out after 1"));
1344}
1345
Vishnu Nair6921f802017-11-22 09:17:23 -08001346TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) {
1347 CreateFd("RunCommandTimesout.txt");
1348 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1349 CommandOptions::WithTimeoutInMs(1000).Build()));
1350 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1351 " --sleep 2' timed out after 1"));
1352 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1353 " --sleep 2' timed out after 1"));
1354}
1355
1356
Felipe Leme46b85da2016-11-21 17:40:45 -08001357TEST_F(DumpstateUtilTest, RunCommandIsKilled) {
1358 CreateFd("RunCommandIsKilled.txt");
1359 CaptureStderr();
1360
1361 std::thread t([=]() {
Felipe Lemef0292972016-11-22 13:57:05 -08001362 EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001363 CommandOptions::WithTimeout(100).Always().Build()));
1364 });
1365
1366 // Capture pid and pre-sleep output.
1367 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
1368 std::string err = GetCapturedStderr();
1369 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
1370
1371 CaptureFdOut();
1372 std::vector<std::string> lines = android::base::Split(out, "\n");
1373 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
1374
1375 int pid = atoi(lines[0].c_str());
1376 EXPECT_THAT(lines[1], StrEq("stdout line1"));
1377 EXPECT_THAT(lines[2], IsEmpty()); // \n
1378
1379 // Then kill the process.
1380 CaptureFdOut();
1381 CaptureStderr();
1382 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
1383 t.join();
1384
1385 // Finally, check output after murder.
1386 CaptureFdOut();
1387 err = GetCapturedStderr();
1388
1389 // out starts with the pid, which is an unknown
1390 EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand +
1391 " --pid --sleep 20' failed: killed by signal 15\n"));
1392 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
1393 " --pid --sleep 20' failed: killed by signal 15\n"));
1394}
1395
1396TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) {
1397 if (!IsStandalone()) {
1398 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1399 // to Shell - need to refactor tests to avoid this problem)
1400 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n")
1401 return;
1402 }
1403 CreateFd("RunCommandAsRootUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001404 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001405 // Emulates user build if necessarily.
1406 SetBuildType("user");
1407 }
1408
1409 DropRoot();
1410
Felipe Lemef0292972016-11-22 13:57:05 -08001411 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001412
1413 // We don't know the exact path of su, so we just check for the 'root ...' commands
1414 EXPECT_THAT(out, StartsWith("Skipping"));
1415 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
1416 EXPECT_THAT(err, IsEmpty());
1417}
1418
1419TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) {
1420 if (!IsStandalone()) {
1421 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1422 // to Shell - need to refactor tests to avoid this problem)
1423 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n")
1424 return;
1425 }
1426 CreateFd("RunCommandAsRootNonUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001427 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001428 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1429 return;
1430 }
1431
1432 DropRoot();
1433
Felipe Lemef0292972016-11-22 13:57:05 -08001434 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1435 CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001436
1437 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1438 EXPECT_THAT(err, StrEq("stderr\n"));
1439}
Felipe Leme46b85da2016-11-21 17:40:45 -08001440
Yifan Hong48e83a12017-10-03 14:10:07 -07001441
1442TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) {
1443 if (!IsStandalone()) {
1444 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1445 // to Shell - need to refactor tests to avoid this problem)
1446 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
1447 return;
1448 }
1449 CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt");
1450 if (!PropertiesHelper::IsUserBuild()) {
1451 // Emulates user build if necessarily.
1452 SetBuildType("user");
1453 }
1454
1455 DropRoot();
1456
1457 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1458 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1459
1460 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1461 EXPECT_THAT(err, StrEq("stderr\n"));
1462}
1463
1464TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) {
1465 if (!IsStandalone()) {
1466 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1467 // to Shell - need to refactor tests to avoid this problem)
1468 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
1469 return;
1470 }
1471 CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt");
1472 if (PropertiesHelper::IsUserBuild()) {
1473 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1474 return;
1475 }
1476
1477 DropRoot();
1478
1479 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1480 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1481
1482 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1483 EXPECT_THAT(err, StrEq("stderr\n"));
1484}
1485
Felipe Leme46b85da2016-11-21 17:40:45 -08001486TEST_F(DumpstateUtilTest, RunCommandDropRoot) {
1487 if (!IsStandalone()) {
1488 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1489 // to Shell - need to refactor tests to avoid this problem)
1490 MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n")
1491 return;
1492 }
1493 CreateFd("RunCommandDropRoot.txt");
1494 // First check root case - only available when running with 'adb root'.
1495 uid_t uid = getuid();
1496 if (uid == 0) {
Felipe Lemef0292972016-11-22 13:57:05 -08001497 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001498 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1499 EXPECT_THAT(err, StrEq("stderr\n"));
1500 return;
1501 }
1502 // Then run dropping root.
Felipe Lemef0292972016-11-22 13:57:05 -08001503 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001504 CommandOptions::WithTimeout(1).DropRoot().Build()));
1505 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1506 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
1507}
1508
Felipe Lemef0292972016-11-22 13:57:05 -08001509TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001510 CreateFd("DumpFileNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001511 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001512 EXPECT_THAT(out,
1513 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
1514 EXPECT_THAT(err, IsEmpty());
1515}
1516
Felipe Lemef0292972016-11-22 13:57:05 -08001517TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) {
1518 CreateFd("DumpFileNotFound.txt");
1519 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
1520 EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such "
1521 "file or directory\n"));
1522 EXPECT_THAT(err, IsEmpty());
1523}
1524
Felipe Leme46b85da2016-11-21 17:40:45 -08001525TEST_F(DumpstateUtilTest, DumpFileSingleLine) {
1526 CreateFd("DumpFileSingleLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001527 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001528 EXPECT_THAT(err, IsEmpty());
1529 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1530}
1531
1532TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) {
1533 CreateFd("DumpFileSingleLineWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001534 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001535 EXPECT_THAT(err, IsEmpty());
1536 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
1537}
1538
1539TEST_F(DumpstateUtilTest, DumpFileMultipleLines) {
1540 CreateFd("DumpFileMultipleLines.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001541 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001542 EXPECT_THAT(err, IsEmpty());
1543 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1544}
1545
1546TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) {
1547 CreateFd("DumpFileMultipleLinesWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001548 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001549 EXPECT_THAT(err, IsEmpty());
1550 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1551}
1552
Felipe Lemef0292972016-11-22 13:57:05 -08001553TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) {
1554 CreateFd("DumpFileOnDryRun.txt");
1555 SetDryRun(true);
1556 std::string path = kTestDataPath + "single-line.txt";
1557 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
1558 EXPECT_THAT(err, IsEmpty());
1559 EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n"));
1560}
1561
Felipe Leme46b85da2016-11-21 17:40:45 -08001562TEST_F(DumpstateUtilTest, DumpFileOnDryRun) {
1563 CreateFd("DumpFileOnDryRun.txt");
1564 SetDryRun(true);
1565 std::string path = kTestDataPath + "single-line.txt";
Felipe Lemef0292972016-11-22 13:57:05 -08001566 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001567 EXPECT_THAT(err, IsEmpty());
Felipe Lemef0292972016-11-22 13:57:05 -08001568 EXPECT_THAT(
1569 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
1570 EXPECT_THAT(out, EndsWith("skipped on dry run\n"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001571}
Ecco Park61ffcf72016-10-27 15:46:26 -07001572
Felipe Leme47e9be22016-12-21 15:37:07 -08001573} // namespace dumpstate
1574} // namespace os
1575} // namespace android