blob: b3cb4349f315e022136d2b501fc2e6477ce8eb1b [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"
Rhed Jao27077b12020-07-14 18:38:08 +080024#include "DumpPool.h"
Felipe Leme4c2d6632016-09-28 14:32:00 -070025
26#include <gmock/gmock.h>
27#include <gtest/gtest.h>
28
Felipe Leme46b85da2016-11-21 17:40:45 -080029#include <fcntl.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070030#include <libgen.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070031#include <signal.h>
32#include <sys/types.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070033#include <unistd.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070034#include <thread>
Felipe Leme4c2d6632016-09-28 14:32:00 -070035
36#include <android-base/file.h>
Felipe Lemed80e6b62016-10-03 13:08:14 -070037#include <android-base/properties.h>
38#include <android-base/stringprintf.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070039#include <android-base/strings.h>
Abhijeet Kaure370d682019-10-01 16:49:30 +010040#include <android-base/unique_fd.h>
Hunter Knepshield8540faf2020-02-04 19:47:20 -080041#include <android/hardware/dumpstate/1.1/types.h>
42#include <cutils/properties.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070043
Felipe Leme47e9be22016-12-21 15:37:07 -080044namespace android {
45namespace os {
46namespace dumpstate {
Felipe Lemed80e6b62016-10-03 13:08:14 -070047
Hunter Knepshield8540faf2020-02-04 19:47:20 -080048using ::android::hardware::dumpstate::V1_1::DumpstateMode;
Felipe Leme4c2d6632016-09-28 14:32:00 -070049using ::testing::EndsWith;
Rhed Jao27077b12020-07-14 18:38:08 +080050using ::testing::Eq;
Felipe Leme46b85da2016-11-21 17:40:45 -080051using ::testing::HasSubstr;
Felipe Leme4c2d6632016-09-28 14:32:00 -070052using ::testing::IsEmpty;
Hunter Knepshield8540faf2020-02-04 19:47:20 -080053using ::testing::IsNull;
Felipe Leme009ecbb2016-11-07 10:18:44 -080054using ::testing::NotNull;
Felipe Leme4c2d6632016-09-28 14:32:00 -070055using ::testing::StartsWith;
Hunter Knepshield8540faf2020-02-04 19:47:20 -080056using ::testing::StrEq;
Felipe Leme4c2d6632016-09-28 14:32:00 -070057using ::testing::Test;
58using ::testing::internal::CaptureStderr;
59using ::testing::internal::CaptureStdout;
60using ::testing::internal::GetCapturedStderr;
61using ::testing::internal::GetCapturedStdout;
62
Nandana Dutt3f8c7172018-09-25 12:01:54 +010063#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
64
Felipe Leme75876a22016-10-27 16:31:27 -070065class DumpstateListenerMock : public IDumpstateListener {
66 public:
Nandana Dutta6a28bd2019-01-14 16:54:38 +000067 MOCK_METHOD1(onProgress, binder::Status(int32_t progress));
68 MOCK_METHOD1(onError, binder::Status(int32_t error_code));
Nandana Duttcc4ead82019-01-23 08:29:23 +000069 MOCK_METHOD0(onFinished, binder::Status());
Paul Chang0d2aad72020-02-13 20:04:03 +080070 MOCK_METHOD1(onScreenshotTaken, binder::Status(bool success));
Paul Changc490e662020-04-11 18:14:09 +080071 MOCK_METHOD1(onUiIntensiveBugreportDumpsFinished,
72 binder::Status(const android::String16& callingpackage));
Felipe Leme75876a22016-10-27 16:31:27 -070073
74 protected:
75 MOCK_METHOD0(onAsBinder, IBinder*());
76};
77
Felipe Leme46b85da2016-11-21 17:40:45 -080078static int calls_;
79
Felipe Leme7447d7c2016-11-03 18:12:22 -070080// Base class for all tests in this file
81class DumpstateBaseTest : public Test {
Felipe Leme46b85da2016-11-21 17:40:45 -080082 public:
83 virtual void SetUp() override {
84 calls_++;
Felipe Lemef0292972016-11-22 13:57:05 -080085 SetDryRun(false);
Felipe Leme46b85da2016-11-21 17:40:45 -080086 }
87
Felipe Lemef0292972016-11-22 13:57:05 -080088 void SetDryRun(bool dry_run) const {
89 PropertiesHelper::dry_run_ = dry_run;
90 }
91
92 void SetBuildType(const std::string& build_type) const {
93 PropertiesHelper::build_type_ = build_type;
94 }
95
Nandana Dutt4b392be2018-11-02 16:17:05 +000096 void SetUnroot(bool unroot) const {
97 PropertiesHelper::unroot_ = unroot;
98 }
99
Rhed Jao1c855122020-07-16 17:37:39 +0800100 void SetParallelRun(bool parallel_run) const {
101 PropertiesHelper::parallel_run_ = parallel_run;
102 }
103
Felipe Lemef0292972016-11-22 13:57:05 -0800104 bool IsStandalone() const {
Felipe Leme46b85da2016-11-21 17:40:45 -0800105 return calls_ == 1;
106 }
107
Felipe Lemef0292972016-11-22 13:57:05 -0800108 void DropRoot() const {
109 DropRootUser();
Felipe Leme46b85da2016-11-21 17:40:45 -0800110 uid_t uid = getuid();
111 ASSERT_EQ(2000, (int)uid);
112 }
113
Felipe Leme7447d7c2016-11-03 18:12:22 -0700114 protected:
115 const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str());
Dan Shie177e8e2019-06-20 11:08:14 -0700116 const std::string kTestDataPath = kTestPath + "/tests/testdata/";
117 const std::string kSimpleCommand = kTestPath + "/dumpstate_test_fixture";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700118 const std::string kEchoCommand = "/system/bin/echo";
119
120 /*
121 * Copies a text file fixture to a temporary file, returning it's path.
122 *
123 * Useful in cases where the test case changes the content of the tile.
124 */
125 std::string CopyTextFileFixture(const std::string& relative_name) {
126 std::string from = kTestDataPath + relative_name;
127 // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it
128 // around for poking when the test fails.
129 std::string to = kTestDataPath + relative_name + ".tmp";
130 ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str());
131 android::base::RemoveFileIfExists(to);
132 CopyTextFile(from, to);
133 return to.c_str();
134 }
135
Felipe Leme46b85da2016-11-21 17:40:45 -0800136 // Need functions that returns void to use assertions -
Felipe Leme7447d7c2016-11-03 18:12:22 -0700137 // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement
Felipe Leme46b85da2016-11-21 17:40:45 -0800138 void ReadFileToString(const std::string& path, std::string* content) {
139 ASSERT_TRUE(android::base::ReadFileToString(path, content))
140 << "could not read contents from " << path;
141 }
142 void WriteStringToFile(const std::string& content, const std::string& path) {
143 ASSERT_TRUE(android::base::WriteStringToFile(content, path))
144 << "could not write contents to " << path;
145 }
146
147 private:
Felipe Leme7447d7c2016-11-03 18:12:22 -0700148 void CopyTextFile(const std::string& from, const std::string& to) {
149 std::string content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800150 ReadFileToString(from, &content);
151 WriteStringToFile(content, to);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700152 }
153};
154
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100155class DumpOptionsTest : public Test {
156 public:
157 virtual ~DumpOptionsTest() {
158 }
159 virtual void SetUp() {
160 options_ = Dumpstate::DumpOptions();
161 }
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000162 void TearDown() {
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000163 }
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100164 Dumpstate::DumpOptions options_;
Abhijeet Kaure370d682019-10-01 16:49:30 +0100165 android::base::unique_fd fd;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100166};
167
168TEST_F(DumpOptionsTest, InitializeNone) {
169 // clang-format off
170 char* argv[] = {
171 const_cast<char*>("dumpstate")
172 };
173 // clang-format on
174
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100175 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
176
177 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
Nandana Dutt58d72e22018-11-16 10:30:48 +0000178
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100179 EXPECT_FALSE(options_.do_add_date);
180 EXPECT_FALSE(options_.do_zip_file);
mhasank3a4cfb42020-06-15 18:06:43 -0700181 EXPECT_EQ("", options_.out_dir);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100182 EXPECT_FALSE(options_.use_socket);
183 EXPECT_FALSE(options_.use_control_socket);
184 EXPECT_FALSE(options_.show_header_only);
185 EXPECT_TRUE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800186 EXPECT_FALSE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100187 EXPECT_FALSE(options_.do_progress_updates);
188 EXPECT_FALSE(options_.is_remote_mode);
mhasankd451a472020-05-26 18:02:39 -0700189 EXPECT_FALSE(options_.limited_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800190 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000191}
192
193TEST_F(DumpOptionsTest, InitializeAdbBugreport) {
194 // clang-format off
195 char* argv[] = {
196 const_cast<char*>("dumpstatez"),
197 const_cast<char*>("-S"),
198 const_cast<char*>("-d"),
199 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000200 };
201 // clang-format on
202
203 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
204
205 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
206 EXPECT_TRUE(options_.do_add_date);
207 EXPECT_TRUE(options_.do_zip_file);
208 EXPECT_TRUE(options_.use_control_socket);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000209
210 // Other options retain default values
211 EXPECT_TRUE(options_.do_vibrate);
212 EXPECT_FALSE(options_.show_header_only);
Paul Chang0d2aad72020-02-13 20:04:03 +0800213 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000214 EXPECT_FALSE(options_.do_progress_updates);
215 EXPECT_FALSE(options_.is_remote_mode);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000216 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700217 EXPECT_FALSE(options_.limited_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800218 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000219}
220
221TEST_F(DumpOptionsTest, InitializeAdbShellBugreport) {
222 // clang-format off
223 char* argv[] = {
224 const_cast<char*>("dumpstate"),
225 const_cast<char*>("-s"),
226 };
227 // clang-format on
228
229 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
230
231 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
232 EXPECT_TRUE(options_.use_socket);
233
234 // Other options retain default values
235 EXPECT_TRUE(options_.do_vibrate);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000236 EXPECT_FALSE(options_.do_add_date);
237 EXPECT_FALSE(options_.do_zip_file);
238 EXPECT_FALSE(options_.use_control_socket);
239 EXPECT_FALSE(options_.show_header_only);
Paul Chang0d2aad72020-02-13 20:04:03 +0800240 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000241 EXPECT_FALSE(options_.do_progress_updates);
242 EXPECT_FALSE(options_.is_remote_mode);
mhasankd451a472020-05-26 18:02:39 -0700243 EXPECT_FALSE(options_.limited_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800244 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000245}
246
247TEST_F(DumpOptionsTest, InitializeFullBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800248 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_FULL, fd, fd, true);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000249 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800250 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000251 EXPECT_TRUE(options_.do_zip_file);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800252 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::FULL);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000253
254 // Other options retain default values
255 EXPECT_TRUE(options_.do_vibrate);
256 EXPECT_FALSE(options_.use_control_socket);
257 EXPECT_FALSE(options_.show_header_only);
258 EXPECT_FALSE(options_.do_progress_updates);
259 EXPECT_FALSE(options_.is_remote_mode);
260 EXPECT_FALSE(options_.use_socket);
261 EXPECT_FALSE(options_.do_start_service);
mhasankd451a472020-05-26 18:02:39 -0700262 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000263}
264
265TEST_F(DumpOptionsTest, InitializeInteractiveBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800266 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE, fd, fd, true);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000267 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000268 EXPECT_TRUE(options_.do_zip_file);
269 EXPECT_TRUE(options_.do_progress_updates);
270 EXPECT_TRUE(options_.do_start_service);
Paul Chang0d2aad72020-02-13 20:04:03 +0800271 EXPECT_TRUE(options_.do_screenshot);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800272 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::INTERACTIVE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000273
274 // Other options retain default values
275 EXPECT_TRUE(options_.do_vibrate);
276 EXPECT_FALSE(options_.use_control_socket);
277 EXPECT_FALSE(options_.show_header_only);
278 EXPECT_FALSE(options_.is_remote_mode);
279 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700280 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000281}
282
283TEST_F(DumpOptionsTest, InitializeRemoteBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800284 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_REMOTE, fd, fd, false);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000285 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000286 EXPECT_TRUE(options_.do_zip_file);
287 EXPECT_TRUE(options_.is_remote_mode);
288 EXPECT_FALSE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800289 EXPECT_FALSE(options_.do_screenshot);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800290 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::REMOTE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000291
292 // Other options retain default values
293 EXPECT_FALSE(options_.use_control_socket);
294 EXPECT_FALSE(options_.show_header_only);
295 EXPECT_FALSE(options_.do_progress_updates);
296 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700297 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000298}
299
300TEST_F(DumpOptionsTest, InitializeWearBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800301 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WEAR, fd, fd, true);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000302 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800303 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000304 EXPECT_TRUE(options_.do_zip_file);
305 EXPECT_TRUE(options_.do_progress_updates);
306 EXPECT_TRUE(options_.do_start_service);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800307 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WEAR);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000308
309 // Other options retain default values
310 EXPECT_TRUE(options_.do_vibrate);
311 EXPECT_FALSE(options_.use_control_socket);
312 EXPECT_FALSE(options_.show_header_only);
313 EXPECT_FALSE(options_.is_remote_mode);
314 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700315 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000316}
317
318TEST_F(DumpOptionsTest, InitializeTelephonyBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800319 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_TELEPHONY, fd, fd, false);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000320 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800321 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000322 EXPECT_TRUE(options_.do_zip_file);
323 EXPECT_TRUE(options_.telephony_only);
Hunter Knepshield820f9bc2020-02-05 20:10:53 -0800324 EXPECT_TRUE(options_.do_progress_updates);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800325 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::CONNECTIVITY);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000326
327 // Other options retain default values
328 EXPECT_TRUE(options_.do_vibrate);
329 EXPECT_FALSE(options_.use_control_socket);
330 EXPECT_FALSE(options_.show_header_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000331 EXPECT_FALSE(options_.is_remote_mode);
332 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700333 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000334}
335
336TEST_F(DumpOptionsTest, InitializeWifiBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800337 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WIFI, fd, fd, false);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000338 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800339 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000340 EXPECT_TRUE(options_.do_zip_file);
341 EXPECT_TRUE(options_.wifi_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800342 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WIFI);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000343
344 // Other options retain default values
345 EXPECT_TRUE(options_.do_vibrate);
346 EXPECT_FALSE(options_.use_control_socket);
347 EXPECT_FALSE(options_.show_header_only);
348 EXPECT_FALSE(options_.do_progress_updates);
349 EXPECT_FALSE(options_.is_remote_mode);
350 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700351 EXPECT_FALSE(options_.limited_only);
352}
353
354TEST_F(DumpOptionsTest, InitializeLimitedOnlyBugreport) {
355 // clang-format off
356 char* argv[] = {
357 const_cast<char*>("dumpstatez"),
358 const_cast<char*>("-S"),
359 const_cast<char*>("-d"),
360 const_cast<char*>("-z"),
361 const_cast<char*>("-q"),
mhasank2d75c442020-06-11 15:05:25 -0700362 const_cast<char*>("-L"),
363 const_cast<char*>("-o abc")
mhasankd451a472020-05-26 18:02:39 -0700364 };
365 // clang-format on
366
367 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
368
369 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
370 EXPECT_TRUE(options_.do_add_date);
371 EXPECT_TRUE(options_.do_zip_file);
372 EXPECT_TRUE(options_.use_control_socket);
373 EXPECT_FALSE(options_.do_vibrate);
374 EXPECT_TRUE(options_.limited_only);
mhasank3a4cfb42020-06-15 18:06:43 -0700375 EXPECT_EQ(" abc", std::string(options_.out_dir));
mhasankd451a472020-05-26 18:02:39 -0700376
377 // Other options retain default values
378 EXPECT_FALSE(options_.show_header_only);
379 EXPECT_FALSE(options_.do_screenshot);
380 EXPECT_FALSE(options_.do_progress_updates);
381 EXPECT_FALSE(options_.is_remote_mode);
382 EXPECT_FALSE(options_.use_socket);
383 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000384}
385
386TEST_F(DumpOptionsTest, InitializeDefaultBugReport) {
387 // default: commandline options are not overridden
388 // clang-format off
389 char* argv[] = {
390 const_cast<char*>("bugreport"),
391 const_cast<char*>("-d"),
392 const_cast<char*>("-p"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000393 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000394 };
395 // clang-format on
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000396 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
397
398 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
399 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800400 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000401 EXPECT_TRUE(options_.do_zip_file);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800402 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000403
404 // Other options retain default values
405 EXPECT_TRUE(options_.do_vibrate);
406 EXPECT_FALSE(options_.use_control_socket);
407 EXPECT_FALSE(options_.show_header_only);
408 EXPECT_FALSE(options_.do_progress_updates);
409 EXPECT_FALSE(options_.is_remote_mode);
410 EXPECT_FALSE(options_.use_socket);
411 EXPECT_FALSE(options_.wifi_only);
mhasankd451a472020-05-26 18:02:39 -0700412 EXPECT_FALSE(options_.limited_only);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100413}
414
415TEST_F(DumpOptionsTest, InitializePartial1) {
416 // clang-format off
417 char* argv[] = {
418 const_cast<char*>("dumpstate"),
419 const_cast<char*>("-d"),
420 const_cast<char*>("-z"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100421 const_cast<char*>("-s"),
422 const_cast<char*>("-S"),
423
424 };
425 // clang-format on
426
427 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
428
429 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
430 EXPECT_TRUE(options_.do_add_date);
431 EXPECT_TRUE(options_.do_zip_file);
432 // TODO: Maybe we should trim the filename
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100433 EXPECT_TRUE(options_.use_socket);
434 EXPECT_TRUE(options_.use_control_socket);
435
436 // Other options retain default values
437 EXPECT_FALSE(options_.show_header_only);
438 EXPECT_TRUE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800439 EXPECT_FALSE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100440 EXPECT_FALSE(options_.do_progress_updates);
441 EXPECT_FALSE(options_.is_remote_mode);
mhasankd451a472020-05-26 18:02:39 -0700442 EXPECT_FALSE(options_.limited_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800443 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100444}
445
446TEST_F(DumpOptionsTest, InitializePartial2) {
447 // clang-format off
448 char* argv[] = {
449 const_cast<char*>("dumpstate"),
450 const_cast<char*>("-v"),
451 const_cast<char*>("-q"),
452 const_cast<char*>("-p"),
453 const_cast<char*>("-P"),
454 const_cast<char*>("-R"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100455 };
456 // clang-format on
457
458 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
459
460 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
461 EXPECT_TRUE(options_.show_header_only);
462 EXPECT_FALSE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800463 EXPECT_TRUE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100464 EXPECT_TRUE(options_.do_progress_updates);
465 EXPECT_TRUE(options_.is_remote_mode);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100466
467 // Other options retain default values
468 EXPECT_FALSE(options_.do_add_date);
469 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100470 EXPECT_FALSE(options_.use_socket);
471 EXPECT_FALSE(options_.use_control_socket);
mhasankd451a472020-05-26 18:02:39 -0700472 EXPECT_FALSE(options_.limited_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800473 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100474}
475
476TEST_F(DumpOptionsTest, InitializeHelp) {
477 // clang-format off
478 char* argv[] = {
479 const_cast<char*>("dumpstate"),
480 const_cast<char*>("-h")
481 };
482 // clang-format on
483
484 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
485
486 // -h is for help.
487 EXPECT_EQ(status, Dumpstate::RunStatus::HELP);
488}
489
490TEST_F(DumpOptionsTest, InitializeUnknown) {
491 // clang-format off
492 char* argv[] = {
493 const_cast<char*>("dumpstate"),
494 const_cast<char*>("-u") // unknown flag
495 };
496 // clang-format on
497
498 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
499
500 // -u is unknown.
501 EXPECT_EQ(status, Dumpstate::RunStatus::INVALID_INPUT);
502}
503
504TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile1) {
505 options_.do_zip_file = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000506 // Writing to socket = !writing to file.
507 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100508 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000509
510 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100511 EXPECT_TRUE(options_.ValidateOptions());
512}
513
514TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile2) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100515 options_.do_progress_updates = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000516 // Writing to socket = !writing to file.
517 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100518 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000519
520 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100521 EXPECT_TRUE(options_.ValidateOptions());
522}
523
524TEST_F(DumpOptionsTest, ValidateOptionsNeedZipfile) {
525 options_.use_control_socket = true;
526 EXPECT_FALSE(options_.ValidateOptions());
527
528 options_.do_zip_file = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100529 EXPECT_TRUE(options_.ValidateOptions());
530}
531
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100532TEST_F(DumpOptionsTest, ValidateOptionsRemoteMode) {
533 options_.is_remote_mode = true;
534 EXPECT_FALSE(options_.ValidateOptions());
535
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100536 options_.do_zip_file = true;
537 options_.do_add_date = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100538 EXPECT_TRUE(options_.ValidateOptions());
539}
540
Felipe Leme7447d7c2016-11-03 18:12:22 -0700541class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700542 public:
543 void SetUp() {
Felipe Leme46b85da2016-11-21 17:40:45 -0800544 DumpstateBaseTest::SetUp();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700545 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700546 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700547 ds.progress_.reset(new Progress());
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100548 ds.options_.reset(new Dumpstate::DumpOptions());
Felipe Leme4c2d6632016-09-28 14:32:00 -0700549 }
550
Rhed Jao1c855122020-07-16 17:37:39 +0800551 void TearDown() {
552 ds.ShutdownDumpPool();
553 }
554
Felipe Leme4c2d6632016-09-28 14:32:00 -0700555 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700556 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700557 const CommandOptions& options = CommandOptions::DEFAULT) {
558 CaptureStdout();
559 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700560 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700561 out = GetCapturedStdout();
562 err = GetCapturedStderr();
563 return status;
564 }
565
Felipe Lemecef02982016-10-03 17:22:22 -0700566 // Dumps a file and capture `stdout` and `stderr`.
567 int DumpFile(const std::string& title, const std::string& path) {
568 CaptureStdout();
569 CaptureStderr();
570 int status = ds.DumpFile(title, path);
571 out = GetCapturedStdout();
572 err = GetCapturedStderr();
573 return status;
574 }
575
Nandana Dutt402a8392019-06-14 14:25:13 +0100576 void SetProgress(long progress, long initial_max) {
577 ds.last_reported_percent_progress_ = 0;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100578 ds.options_->do_progress_updates = true;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700579 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
580 }
581
Rhed Jao1c855122020-07-16 17:37:39 +0800582 void EnableParallelRunIfNeeded() {
583 ds.EnableParallelRunIfNeeded();
584 }
585
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100586 std::string GetProgressMessage(int progress, int max,
587 int old_max = 0, bool update_progress = true) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700588 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
589 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700590
Felipe Leme7447d7c2016-11-03 18:12:22 -0700591 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700592
Felipe Leme009ecbb2016-11-07 10:18:44 -0800593 std::string message = "";
Felipe Leme75876a22016-10-27 16:31:27 -0700594 if (max_increased) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800595 message =
Felipe Leme7447d7c2016-11-03 18:12:22 -0700596 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700597 }
598
Felipe Leme009ecbb2016-11-07 10:18:44 -0800599 if (update_progress) {
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100600 message += android::base::StringPrintf("Setting progress: %d/%d (%d%%)\n",
601 progress, max, (100 * progress / max));
Felipe Leme009ecbb2016-11-07 10:18:44 -0800602 }
603
604 return message;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700605 }
606
Felipe Leme4c2d6632016-09-28 14:32:00 -0700607 // `stdout` and `stderr` from the last command ran.
608 std::string out, err;
609
Felipe Lemefd8affa2016-09-30 17:38:57 -0700610 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700611};
612
613TEST_F(DumpstateTest, RunCommandNoArgs) {
614 EXPECT_EQ(-1, RunCommand("", {}));
615}
616
617TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700618 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700619 EXPECT_THAT(out, StrEq("stdout\n"));
620 EXPECT_THAT(err, StrEq("stderr\n"));
621}
622
623TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700624 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700625 EXPECT_THAT(err, StrEq("stderr\n"));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700626 // The duration may not get output, depending on how long it takes,
627 // so we just check the prefix.
Felipe Lemefd8affa2016-09-30 17:38:57 -0700628 EXPECT_THAT(out,
Nandana Dutt47527b52019-03-29 15:34:36 +0000629 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700630}
631
Felipe Lemefd8affa2016-09-30 17:38:57 -0700632TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700633 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700634 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700635 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
636 EXPECT_THAT(out, StrEq("stdout\n"));
637 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
638}
639
640TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700641 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700642 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700643 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700644 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700645}
646
647TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700648 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700649 EXPECT_THAT(err, IsEmpty());
650 EXPECT_THAT(out, StrEq("one\n"));
651}
652
Felipe Lemefd8affa2016-09-30 17:38:57 -0700653TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700654 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700655 EXPECT_THAT(err, IsEmpty());
656 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
657}
658
659TEST_F(DumpstateTest, RunCommandDryRun) {
660 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700661 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700662 // The duration may not get output, depending on how long it takes,
663 // so we just check the prefix.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700664 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Nandana Dutt47527b52019-03-29 15:34:36 +0000665 ") ------\n\t(skipped on dry run)\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700666 EXPECT_THAT(err, IsEmpty());
667}
668
669TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
670 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700671 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700672 EXPECT_THAT(out, IsEmpty());
673 EXPECT_THAT(err, IsEmpty());
674}
675
676TEST_F(DumpstateTest, RunCommandDryRunAlways) {
677 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700678 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700679 EXPECT_THAT(out, StrEq("stdout\n"));
680 EXPECT_THAT(err, StrEq("stderr\n"));
681}
682
Felipe Lemefd8affa2016-09-30 17:38:57 -0700683TEST_F(DumpstateTest, RunCommandNotFound) {
684 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
685 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
686 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
687}
688
689TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700690 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
691 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700692 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700693 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700694 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700695}
696
697TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700698 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700699 // We don't know the exit code, so check just the prefix.
700 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700701 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700702 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700703 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700704}
705
706TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700707 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700708 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700709 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700710 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700711 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700712 " --sleep 2' timed out after 1"));
713}
714
715TEST_F(DumpstateTest, RunCommandIsKilled) {
716 CaptureStdout();
717 CaptureStderr();
718
719 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700720 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700721 CommandOptions::WithTimeout(100).Always().Build()));
722 });
723
724 // Capture pid and pre-sleep output.
725 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
726 std::string err = GetCapturedStderr();
727 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
728
729 std::string out = GetCapturedStdout();
730 std::vector<std::string> lines = android::base::Split(out, "\n");
731 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
732
733 int pid = atoi(lines[0].c_str());
734 EXPECT_THAT(lines[1], StrEq("stdout line1"));
735 EXPECT_THAT(lines[2], IsEmpty()); // \n
736
737 // Then kill the process.
738 CaptureStdout();
739 CaptureStderr();
740 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
741 t.join();
742
743 // Finally, check output after murder.
744 out = GetCapturedStdout();
745 err = GetCapturedStderr();
746
Felipe Leme7447d7c2016-11-03 18:12:22 -0700747 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700748 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700749 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700750 " --pid --sleep 20' failed: killed by signal 15\n"));
751}
752
Felipe Leme75876a22016-10-27 16:31:27 -0700753TEST_F(DumpstateTest, RunCommandProgress) {
754 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
755 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700756 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700757
Nandana Duttbabf6c72019-01-15 14:11:12 +0000758 EXPECT_CALL(*listener, onProgress(66)); // 20/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700759 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100760 std::string progress_message = GetProgressMessage(20, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700761 EXPECT_THAT(out, StrEq("stdout\n"));
762 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
763
Nandana Dutt402a8392019-06-14 14:25:13 +0100764 EXPECT_CALL(*listener, onProgress(80)); // 24/30 %
765 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100766 progress_message = GetProgressMessage(24, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700767 EXPECT_THAT(out, StrEq("stdout\n"));
768 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
769
770 // Make sure command ran while in dry_run is counted.
771 SetDryRun(true);
Nandana Dutt402a8392019-06-14 14:25:13 +0100772 EXPECT_CALL(*listener, onProgress(90)); // 27/30 %
773 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100774 progress_message = GetProgressMessage(27, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700775 EXPECT_THAT(out, IsEmpty());
776 EXPECT_THAT(err, StrEq(progress_message));
777
Nandana Dutt402a8392019-06-14 14:25:13 +0100778 SetDryRun(false);
779 EXPECT_CALL(*listener, onProgress(96)); // 29/30 %
780 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(2).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100781 progress_message = GetProgressMessage(29, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800782 EXPECT_THAT(out, StrEq("stdout\n"));
783 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
784
Nandana Dutt402a8392019-06-14 14:25:13 +0100785 EXPECT_CALL(*listener, onProgress(100)); // 30/30 %
Felipe Leme009ecbb2016-11-07 10:18:44 -0800786 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100787 progress_message = GetProgressMessage(30, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800788 EXPECT_THAT(out, StrEq("stdout\n"));
789 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
790
791 ds.listener_.clear();
792}
793
Felipe Lemed80e6b62016-10-03 13:08:14 -0700794TEST_F(DumpstateTest, RunCommandDropRoot) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800795 if (!IsStandalone()) {
796 // TODO: temporarily disabled because it might cause other tests to fail after dropping
797 // to Shell - need to refactor tests to avoid this problem)
798 MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n")
799 return;
800 }
Felipe Lemed80e6b62016-10-03 13:08:14 -0700801 // First check root case - only available when running with 'adb root'.
802 uid_t uid = getuid();
803 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700804 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700805 EXPECT_THAT(out, StrEq("0\nstdout\n"));
806 EXPECT_THAT(err, StrEq("stderr\n"));
807 return;
808 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700809 // Then run dropping root.
810 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700811 CommandOptions::WithTimeout(1).DropRoot().Build()));
812 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700813 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700814}
815
816TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800817 if (!IsStandalone()) {
818 // TODO: temporarily disabled because it might cause other tests to fail after dropping
819 // to Shell - need to refactor tests to avoid this problem)
820 MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n")
821 return;
822 }
Felipe Lemef0292972016-11-22 13:57:05 -0800823 if (!PropertiesHelper::IsUserBuild()) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700824 // Emulates user build if necessarily.
825 SetBuildType("user");
826 }
827
828 DropRoot();
829
Felipe Leme7447d7c2016-11-03 18:12:22 -0700830 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700831
832 // We don't know the exact path of su, so we just check for the 'root ...' commands
833 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700834 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700835 EXPECT_THAT(err, IsEmpty());
836}
837
Felipe Leme46b85da2016-11-21 17:40:45 -0800838TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
839 if (!IsStandalone()) {
840 // TODO: temporarily disabled because it might cause other tests to fail after dropping
841 // to Shell - need to refactor tests to avoid this problem)
842 MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n")
843 return;
844 }
Felipe Lemef0292972016-11-22 13:57:05 -0800845 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800846 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
847 return;
848 }
849
850 DropRoot();
851
852 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
853 CommandOptions::WithTimeout(1).AsRoot().Build()));
854
855 EXPECT_THAT(out, StrEq("0\nstdout\n"));
856 EXPECT_THAT(err, StrEq("stderr\n"));
857}
858
Nandana Dutt4b392be2018-11-02 16:17:05 +0000859TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild_withUnroot) {
860 if (!IsStandalone()) {
861 // TODO: temporarily disabled because it might cause other tests to fail after dropping
862 // to Shell - need to refactor tests to avoid this problem)
863 MYLOGE(
864 "Skipping DumpstateTest.RunCommandAsRootNonUserBuild_withUnroot() "
865 "on test suite\n")
866 return;
867 }
868 if (PropertiesHelper::IsUserBuild()) {
869 ALOGI("Skipping RunCommandAsRootNonUserBuild_withUnroot on user builds\n");
870 return;
871 }
872
873 // Same test as above, but with unroot property set, which will override su availability.
874 SetUnroot(true);
875 DropRoot();
876
877 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
878 CommandOptions::WithTimeout(1).AsRoot().Build()));
879
880 // AsRoot is ineffective.
881 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
882 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
883}
884
Yifan Hong48e83a12017-10-03 14:10:07 -0700885TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) {
886 if (!IsStandalone()) {
887 // TODO: temporarily disabled because it might cause other tests to fail after dropping
888 // to Shell - need to refactor tests to avoid this problem)
889 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
890 return;
891 }
892 if (!PropertiesHelper::IsUserBuild()) {
893 // Emulates user build if necessarily.
894 SetBuildType("user");
895 }
896
897 DropRoot();
898
899 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
900 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
901
902 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
903 EXPECT_THAT(err, StrEq("stderr\n"));
904}
905
906TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) {
907 if (!IsStandalone()) {
908 // TODO: temporarily disabled because it might cause other tests to fail after dropping
909 // to Shell - need to refactor tests to avoid this problem)
910 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
911 return;
912 }
913 if (PropertiesHelper::IsUserBuild()) {
914 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
915 return;
916 }
917
918 DropRoot();
919
920 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
921 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
922
923 EXPECT_THAT(out, StrEq("0\nstdout\n"));
924 EXPECT_THAT(err, StrEq("stderr\n"));
925}
926
Nandana Dutt4b392be2018-11-02 16:17:05 +0000927TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild_withUnroot) {
928 if (!IsStandalone()) {
929 // TODO: temporarily disabled because it might cause other tests to fail after dropping
930 // to Shell - need to refactor tests to avoid this problem)
931 MYLOGE(
932 "Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild_withUnroot() "
933 "on test suite\n")
934 return;
935 }
936 if (PropertiesHelper::IsUserBuild()) {
937 ALOGI("Skipping RunCommandAsRootIfAvailableOnDebugBuild_withUnroot on user builds\n");
938 return;
939 }
940 // Same test as above, but with unroot property set, which will override su availability.
941 SetUnroot(true);
942
943 DropRoot();
944
945 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
946 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
947
948 // It's a userdebug build, so "su root" should be available, but unroot=true overrides it.
949 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
950 EXPECT_THAT(err, StrEq("stderr\n"));
951}
952
Felipe Lemecef02982016-10-03 17:22:22 -0700953TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
954 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
955 EXPECT_THAT(out,
956 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
957 EXPECT_THAT(err, IsEmpty());
958}
959
960TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
961 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
962 EXPECT_THAT(err, IsEmpty());
Greg Kaiser3a811c12019-05-21 12:48:59 -0700963 // The duration may not get output, depending on how long it takes,
964 // so we just check the prefix.
Felipe Lemecef02982016-10-03 17:22:22 -0700965 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
966 "such file or directory\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700967}
968
969TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700970 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700971 EXPECT_THAT(err, IsEmpty());
972 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
973}
974
975TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700976 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700977 EXPECT_THAT(err, IsEmpty());
978 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
979}
980
981TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700982 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700983 EXPECT_THAT(err, IsEmpty());
984 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
985}
986
987TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700988 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700989 EXPECT_THAT(err, IsEmpty());
990 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
991}
992
993TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
994 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700995 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700996 EXPECT_THAT(err, IsEmpty());
997 EXPECT_THAT(out, IsEmpty());
998}
999
1000TEST_F(DumpstateTest, DumpFileOnDryRun) {
1001 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -07001002 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -07001003 EXPECT_THAT(err, IsEmpty());
Felipe Leme46b85da2016-11-21 17:40:45 -08001004 EXPECT_THAT(
1005 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
Nandana Dutt47527b52019-03-29 15:34:36 +00001006 EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n"));
Felipe Lemecef02982016-10-03 17:22:22 -07001007}
1008
Felipe Leme75876a22016-10-27 16:31:27 -07001009TEST_F(DumpstateTest, DumpFileUpdateProgress) {
1010 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
1011 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -07001012 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -07001013
Nandana Duttbabf6c72019-01-15 14:11:12 +00001014 EXPECT_CALL(*listener, onProgress(16)); // 5/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -07001015 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -07001016
Abhijeet Kaured5d6a62019-10-07 15:02:05 +01001017 std::string progress_message = GetProgressMessage(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
Felipe Leme75876a22016-10-27 16:31:27 -07001018 EXPECT_THAT(err, StrEq(progress_message));
1019 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1020
1021 ds.listener_.clear();
1022}
1023
Rhed Jao1c855122020-07-16 17:37:39 +08001024TEST_F(DumpstateTest, DumpPool_withOutputToFileAndParallelRunEnabled_notNull) {
1025 ds.options_->use_socket = false;
1026 SetParallelRun(true);
1027 EnableParallelRunIfNeeded();
1028 EXPECT_TRUE(ds.options_->OutputToFile());
1029 EXPECT_TRUE(ds.dump_pool_);
1030}
1031
1032TEST_F(DumpstateTest, DumpPool_withNotOutputToFile_isNull) {
1033 ds.options_->use_socket = true;
1034 EnableParallelRunIfNeeded();
1035 EXPECT_FALSE(ds.options_->OutputToFile());
1036 EXPECT_FALSE(ds.dump_pool_);
1037}
1038
1039TEST_F(DumpstateTest, DumpPool_withParallelRunDisabled_isNull) {
1040 SetParallelRun(false);
1041 EnableParallelRunIfNeeded();
1042 EXPECT_FALSE(ds.dump_pool_);
1043}
1044
Felipe Leme7447d7c2016-11-03 18:12:22 -07001045class DumpstateServiceTest : public DumpstateBaseTest {
Felipe Leme75876a22016-10-27 16:31:27 -07001046 public:
1047 DumpstateService dss;
1048};
1049
Felipe Leme7447d7c2016-11-03 18:12:22 -07001050class ProgressTest : public DumpstateBaseTest {
1051 public:
1052 Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {
1053 return Progress(max, growth_factor, path);
1054 }
1055
1056 void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) {
1057 std::string expected_content =
1058 android::base::StringPrintf("%d %d\n", expected_runs, expected_average);
1059 std::string actual_content;
Felipe Leme46b85da2016-11-21 17:40:45 -08001060 ReadFileToString(path, &actual_content);
Felipe Leme7447d7c2016-11-03 18:12:22 -07001061 ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path;
1062 }
1063};
1064
1065TEST_F(ProgressTest, SimpleTest) {
1066 Progress progress;
1067 EXPECT_EQ(0, progress.Get());
1068 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
1069 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1070
1071 bool max_increased = progress.Inc(1);
1072 EXPECT_EQ(1, progress.Get());
1073 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
1074 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1075 EXPECT_FALSE(max_increased);
1076
1077 // Ignore negative increase.
1078 max_increased = progress.Inc(-1);
1079 EXPECT_EQ(1, progress.Get());
1080 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
1081 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1082 EXPECT_FALSE(max_increased);
1083}
1084
1085TEST_F(ProgressTest, MaxGrowsInsideNewRange) {
1086 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1087 EXPECT_EQ(0, progress.Get());
1088 EXPECT_EQ(10, progress.GetInitialMax());
1089 EXPECT_EQ(10, progress.GetMax());
1090
1091 // No increase
1092 bool max_increased = progress.Inc(10);
1093 EXPECT_EQ(10, progress.Get());
1094 EXPECT_EQ(10, progress.GetMax());
1095 EXPECT_FALSE(max_increased);
1096
1097 // Increase, with new value < max*20%
1098 max_increased = progress.Inc(1);
1099 EXPECT_EQ(11, progress.Get());
1100 EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13
1101 EXPECT_TRUE(max_increased);
1102}
1103
1104TEST_F(ProgressTest, MaxGrowsOutsideNewRange) {
1105 Progress progress = GetInstance(10, 1.2); // 20% growth factor
1106 EXPECT_EQ(0, progress.Get());
1107 EXPECT_EQ(10, progress.GetInitialMax());
1108 EXPECT_EQ(10, progress.GetMax());
1109
1110 // No increase
1111 bool max_increased = progress.Inc(10);
1112 EXPECT_EQ(10, progress.Get());
1113 EXPECT_EQ(10, progress.GetMax());
1114 EXPECT_FALSE(max_increased);
1115
1116 // Increase, with new value > max*20%
1117 max_increased = progress.Inc(5);
1118 EXPECT_EQ(15, progress.Get());
1119 EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18
1120 EXPECT_TRUE(max_increased);
1121}
1122
1123TEST_F(ProgressTest, InvalidPath) {
1124 Progress progress("/devil/null");
1125 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1126}
1127
1128TEST_F(ProgressTest, EmptyFile) {
1129 Progress progress(CopyTextFileFixture("empty-file.txt"));
1130 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1131}
1132
1133TEST_F(ProgressTest, InvalidLine1stEntryNAN) {
1134 Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt"));
1135 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1136}
1137
1138TEST_F(ProgressTest, InvalidLine2ndEntryNAN) {
1139 Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt"));
1140 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1141}
1142
1143TEST_F(ProgressTest, InvalidLineBothNAN) {
1144 Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt"));
1145 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1146}
1147
1148TEST_F(ProgressTest, InvalidLine1stEntryNegative) {
1149 Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt"));
1150 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1151}
1152
1153TEST_F(ProgressTest, InvalidLine2ndEntryNegative) {
1154 Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt"));
1155 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1156}
1157
1158TEST_F(ProgressTest, InvalidLine1stEntryTooBig) {
1159 Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt"));
1160 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1161}
1162
1163TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) {
1164 Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt"));
1165 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
1166}
1167
1168// Tests stats are properly saved when the file does not exists.
1169TEST_F(ProgressTest, FirstTime) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001170 if (!IsStandalone()) {
1171 // TODO: temporarily disabled because it's failing when running as suite
1172 MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n")
1173 return;
1174 }
1175
Felipe Leme7447d7c2016-11-03 18:12:22 -07001176 std::string path = kTestDataPath + "FirstTime.txt";
1177 android::base::RemoveFileIfExists(path);
1178
1179 Progress run1(path);
1180 EXPECT_EQ(0, run1.Get());
1181 EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax());
1182 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1183
1184 bool max_increased = run1.Inc(20);
1185 EXPECT_EQ(20, run1.Get());
1186 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
1187 EXPECT_FALSE(max_increased);
1188
1189 run1.Save();
1190 AssertStats(path, 1, 20);
1191}
1192
1193// Tests what happens when the persistent settings contains the average duration of 1 run.
1194// Data on file is 1 run and 109 average.
1195TEST_F(ProgressTest, SecondTime) {
1196 std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt");
1197
1198 Progress run1 = GetInstance(-42, 1.2, path);
1199 EXPECT_EQ(0, run1.Get());
1200 EXPECT_EQ(10, run1.GetInitialMax());
1201 EXPECT_EQ(10, run1.GetMax());
1202
1203 bool max_increased = run1.Inc(20);
1204 EXPECT_EQ(20, run1.Get());
1205 EXPECT_EQ(24, run1.GetMax());
1206 EXPECT_TRUE(max_increased);
1207
1208 // Average now is 2 runs and (10 + 20)/ 2 = 15
1209 run1.Save();
1210 AssertStats(path, 2, 15);
1211
1212 Progress run2 = GetInstance(-42, 1.2, path);
1213 EXPECT_EQ(0, run2.Get());
1214 EXPECT_EQ(15, run2.GetInitialMax());
1215 EXPECT_EQ(15, run2.GetMax());
1216
1217 max_increased = run2.Inc(25);
1218 EXPECT_EQ(25, run2.Get());
1219 EXPECT_EQ(30, run2.GetMax());
1220 EXPECT_TRUE(max_increased);
1221
1222 // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18
1223 run2.Save();
1224 AssertStats(path, 3, 18);
1225
1226 Progress run3 = GetInstance(-42, 1.2, path);
1227 EXPECT_EQ(0, run3.Get());
1228 EXPECT_EQ(18, run3.GetInitialMax());
1229 EXPECT_EQ(18, run3.GetMax());
1230
1231 // Make sure average decreases as well
1232 max_increased = run3.Inc(5);
1233 EXPECT_EQ(5, run3.Get());
1234 EXPECT_EQ(18, run3.GetMax());
1235 EXPECT_FALSE(max_increased);
1236
1237 // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14
1238 run3.Save();
1239 AssertStats(path, 4, 14);
1240}
1241
1242// Tests what happens when the persistent settings contains the average duration of 2 runs.
1243// Data on file is 2 runs and 15 average.
1244TEST_F(ProgressTest, ThirdTime) {
1245 std::string path = CopyTextFileFixture("stats-two-runs.txt");
1246 AssertStats(path, 2, 15); // Sanity check
1247
1248 Progress run1 = GetInstance(-42, 1.2, path);
1249 EXPECT_EQ(0, run1.Get());
1250 EXPECT_EQ(15, run1.GetInitialMax());
1251 EXPECT_EQ(15, run1.GetMax());
1252
1253 bool max_increased = run1.Inc(20);
1254 EXPECT_EQ(20, run1.Get());
1255 EXPECT_EQ(24, run1.GetMax());
1256 EXPECT_TRUE(max_increased);
1257
1258 // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16
1259 run1.Save();
1260 AssertStats(path, 3, 16);
1261}
1262
Felipe Leme46b85da2016-11-21 17:40:45 -08001263class DumpstateUtilTest : public DumpstateBaseTest {
1264 public:
1265 void SetUp() {
1266 DumpstateBaseTest::SetUp();
1267 SetDryRun(false);
1268 }
1269
Felipe Leme46b85da2016-11-21 17:40:45 -08001270 void CaptureFdOut() {
Felipe Lemef0292972016-11-22 13:57:05 -08001271 ReadFileToString(path_, &out);
Felipe Leme46b85da2016-11-21 17:40:45 -08001272 }
1273
1274 void CreateFd(const std::string& name) {
1275 path_ = kTestDataPath + name;
1276 MYLOGD("Creating fd for file %s\n", path_.c_str());
1277
1278 fd = TEMP_FAILURE_RETRY(open(path_.c_str(),
1279 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
1280 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
1281 ASSERT_GE(fd, 0) << "could not create FD for path " << path_;
1282 }
1283
1284 // Runs a command into the `fd` and capture `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001285 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme46b85da2016-11-21 17:40:45 -08001286 const CommandOptions& options = CommandOptions::DEFAULT) {
1287 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001288 int status = RunCommandToFd(fd, title, full_command, options);
Felipe Leme46b85da2016-11-21 17:40:45 -08001289 close(fd);
1290
1291 CaptureFdOut();
1292 err = GetCapturedStderr();
1293 return status;
1294 }
1295
1296 // Dumps a file and into the `fd` and `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -08001297 int DumpFile(const std::string& title, const std::string& path) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001298 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -08001299 int status = DumpFileToFd(fd, title, path);
Felipe Leme46b85da2016-11-21 17:40:45 -08001300 close(fd);
1301
1302 CaptureFdOut();
1303 err = GetCapturedStderr();
1304 return status;
1305 }
1306
1307 int fd;
1308
1309 // 'fd` output and `stderr` from the last command ran.
1310 std::string out, err;
1311
1312 private:
1313 std::string path_;
1314};
1315
1316TEST_F(DumpstateUtilTest, RunCommandNoArgs) {
Felipe Lemef0292972016-11-22 13:57:05 -08001317 CreateFd("RunCommandNoArgs.txt");
1318 EXPECT_EQ(-1, RunCommand("", {}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001319}
1320
Felipe Lemef0292972016-11-22 13:57:05 -08001321TEST_F(DumpstateUtilTest, RunCommandNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001322 CreateFd("RunCommandWithNoArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001323 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001324 EXPECT_THAT(out, StrEq("stdout\n"));
1325 EXPECT_THAT(err, StrEq("stderr\n"));
1326}
1327
Felipe Lemef0292972016-11-22 13:57:05 -08001328TEST_F(DumpstateUtilTest, RunCommandWithTitle) {
1329 CreateFd("RunCommandWithNoArgs.txt");
1330 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1331 EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
1332 EXPECT_THAT(err, StrEq("stderr\n"));
1333}
1334
Felipe Leme46b85da2016-11-21 17:40:45 -08001335TEST_F(DumpstateUtilTest, RunCommandWithOneArg) {
1336 CreateFd("RunCommandWithOneArg.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001337 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001338 EXPECT_THAT(err, IsEmpty());
1339 EXPECT_THAT(out, StrEq("one\n"));
1340}
1341
1342TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) {
1343 CreateFd("RunCommandWithMultipleArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001344 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001345 EXPECT_THAT(err, IsEmpty());
1346 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
1347}
1348
1349TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) {
1350 CreateFd("RunCommandWithLoggingMessage.txt");
1351 EXPECT_EQ(
Felipe Lemef0292972016-11-22 13:57:05 -08001352 0, RunCommand("", {kSimpleCommand},
Felipe Leme46b85da2016-11-21 17:40:45 -08001353 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
1354 EXPECT_THAT(out, StrEq("stdout\n"));
1355 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
1356}
1357
1358TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) {
1359 CreateFd("RunCommandRedirectStderr.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001360 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
1361 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001362 EXPECT_THAT(out, IsEmpty());
1363 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
1364}
1365
1366TEST_F(DumpstateUtilTest, RunCommandDryRun) {
1367 CreateFd("RunCommandDryRun.txt");
1368 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001369 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
1370 EXPECT_THAT(out, StrEq(android::base::StringPrintf(
1371 "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n",
1372 kSimpleCommand.c_str())));
1373 EXPECT_THAT(err, IsEmpty());
1374}
1375
1376TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) {
1377 CreateFd("RunCommandDryRun.txt");
1378 SetDryRun(true);
1379 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001380 EXPECT_THAT(
1381 out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str())));
1382 EXPECT_THAT(err, IsEmpty());
1383}
1384
1385TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) {
1386 CreateFd("RunCommandDryRunAlways.txt");
1387 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -08001388 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001389 EXPECT_THAT(out, StrEq("stdout\n"));
1390 EXPECT_THAT(err, StrEq("stderr\n"));
1391}
1392
1393TEST_F(DumpstateUtilTest, RunCommandNotFound) {
1394 CreateFd("RunCommandNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001395 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001396 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
1397 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
1398}
1399
1400TEST_F(DumpstateUtilTest, RunCommandFails) {
1401 CreateFd("RunCommandFails.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001402 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001403 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
1404 " --exit 42' failed: exit code 42\n"));
1405 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
1406 " --exit 42' failed: exit code 42\n"));
1407}
1408
1409TEST_F(DumpstateUtilTest, RunCommandCrashes) {
1410 CreateFd("RunCommandCrashes.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001411 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001412 // We don't know the exit code, so check just the prefix.
1413 EXPECT_THAT(
1414 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1415 EXPECT_THAT(
1416 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1417}
1418
Vishnu Nair6921f802017-11-22 09:17:23 -08001419TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001420 CreateFd("RunCommandTimesout.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001421 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1422 CommandOptions::WithTimeout(1).Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001423 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1424 " --sleep 2' timed out after 1"));
1425 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1426 " --sleep 2' timed out after 1"));
1427}
1428
Vishnu Nair6921f802017-11-22 09:17:23 -08001429TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) {
1430 CreateFd("RunCommandTimesout.txt");
1431 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1432 CommandOptions::WithTimeoutInMs(1000).Build()));
1433 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1434 " --sleep 2' timed out after 1"));
1435 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1436 " --sleep 2' timed out after 1"));
1437}
1438
1439
Felipe Leme46b85da2016-11-21 17:40:45 -08001440TEST_F(DumpstateUtilTest, RunCommandIsKilled) {
1441 CreateFd("RunCommandIsKilled.txt");
1442 CaptureStderr();
1443
1444 std::thread t([=]() {
Felipe Lemef0292972016-11-22 13:57:05 -08001445 EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001446 CommandOptions::WithTimeout(100).Always().Build()));
1447 });
1448
1449 // Capture pid and pre-sleep output.
1450 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
1451 std::string err = GetCapturedStderr();
1452 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
1453
1454 CaptureFdOut();
1455 std::vector<std::string> lines = android::base::Split(out, "\n");
1456 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
1457
1458 int pid = atoi(lines[0].c_str());
1459 EXPECT_THAT(lines[1], StrEq("stdout line1"));
1460 EXPECT_THAT(lines[2], IsEmpty()); // \n
1461
1462 // Then kill the process.
1463 CaptureFdOut();
1464 CaptureStderr();
1465 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
1466 t.join();
1467
1468 // Finally, check output after murder.
1469 CaptureFdOut();
1470 err = GetCapturedStderr();
1471
1472 // out starts with the pid, which is an unknown
1473 EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand +
1474 " --pid --sleep 20' failed: killed by signal 15\n"));
1475 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
1476 " --pid --sleep 20' failed: killed by signal 15\n"));
1477}
1478
1479TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) {
1480 if (!IsStandalone()) {
1481 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1482 // to Shell - need to refactor tests to avoid this problem)
1483 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n")
1484 return;
1485 }
1486 CreateFd("RunCommandAsRootUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001487 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001488 // Emulates user build if necessarily.
1489 SetBuildType("user");
1490 }
1491
1492 DropRoot();
1493
Felipe Lemef0292972016-11-22 13:57:05 -08001494 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001495
1496 // We don't know the exact path of su, so we just check for the 'root ...' commands
1497 EXPECT_THAT(out, StartsWith("Skipping"));
1498 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
1499 EXPECT_THAT(err, IsEmpty());
1500}
1501
1502TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) {
1503 if (!IsStandalone()) {
1504 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1505 // to Shell - need to refactor tests to avoid this problem)
1506 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n")
1507 return;
1508 }
1509 CreateFd("RunCommandAsRootNonUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001510 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001511 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1512 return;
1513 }
1514
1515 DropRoot();
1516
Felipe Lemef0292972016-11-22 13:57:05 -08001517 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1518 CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001519
1520 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1521 EXPECT_THAT(err, StrEq("stderr\n"));
1522}
Felipe Leme46b85da2016-11-21 17:40:45 -08001523
Yifan Hong48e83a12017-10-03 14:10:07 -07001524
1525TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) {
1526 if (!IsStandalone()) {
1527 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1528 // to Shell - need to refactor tests to avoid this problem)
1529 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
1530 return;
1531 }
1532 CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt");
1533 if (!PropertiesHelper::IsUserBuild()) {
1534 // Emulates user build if necessarily.
1535 SetBuildType("user");
1536 }
1537
1538 DropRoot();
1539
1540 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1541 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1542
1543 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1544 EXPECT_THAT(err, StrEq("stderr\n"));
1545}
1546
1547TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) {
1548 if (!IsStandalone()) {
1549 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1550 // to Shell - need to refactor tests to avoid this problem)
1551 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
1552 return;
1553 }
1554 CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt");
1555 if (PropertiesHelper::IsUserBuild()) {
1556 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1557 return;
1558 }
1559
1560 DropRoot();
1561
1562 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1563 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1564
1565 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1566 EXPECT_THAT(err, StrEq("stderr\n"));
1567}
1568
Felipe Leme46b85da2016-11-21 17:40:45 -08001569TEST_F(DumpstateUtilTest, RunCommandDropRoot) {
1570 if (!IsStandalone()) {
1571 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1572 // to Shell - need to refactor tests to avoid this problem)
1573 MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n")
1574 return;
1575 }
1576 CreateFd("RunCommandDropRoot.txt");
1577 // First check root case - only available when running with 'adb root'.
1578 uid_t uid = getuid();
1579 if (uid == 0) {
Felipe Lemef0292972016-11-22 13:57:05 -08001580 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001581 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1582 EXPECT_THAT(err, StrEq("stderr\n"));
1583 return;
1584 }
1585 // Then run dropping root.
Felipe Lemef0292972016-11-22 13:57:05 -08001586 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001587 CommandOptions::WithTimeout(1).DropRoot().Build()));
1588 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1589 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
1590}
1591
Felipe Lemef0292972016-11-22 13:57:05 -08001592TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001593 CreateFd("DumpFileNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001594 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001595 EXPECT_THAT(out,
1596 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
1597 EXPECT_THAT(err, IsEmpty());
1598}
1599
Felipe Lemef0292972016-11-22 13:57:05 -08001600TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) {
1601 CreateFd("DumpFileNotFound.txt");
1602 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
1603 EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such "
1604 "file or directory\n"));
1605 EXPECT_THAT(err, IsEmpty());
1606}
1607
Felipe Leme46b85da2016-11-21 17:40:45 -08001608TEST_F(DumpstateUtilTest, DumpFileSingleLine) {
1609 CreateFd("DumpFileSingleLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001610 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001611 EXPECT_THAT(err, IsEmpty());
1612 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1613}
1614
1615TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) {
1616 CreateFd("DumpFileSingleLineWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001617 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001618 EXPECT_THAT(err, IsEmpty());
1619 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
1620}
1621
1622TEST_F(DumpstateUtilTest, DumpFileMultipleLines) {
1623 CreateFd("DumpFileMultipleLines.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001624 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001625 EXPECT_THAT(err, IsEmpty());
1626 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1627}
1628
1629TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) {
1630 CreateFd("DumpFileMultipleLinesWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001631 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001632 EXPECT_THAT(err, IsEmpty());
1633 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1634}
1635
Felipe Lemef0292972016-11-22 13:57:05 -08001636TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) {
1637 CreateFd("DumpFileOnDryRun.txt");
1638 SetDryRun(true);
1639 std::string path = kTestDataPath + "single-line.txt";
1640 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
1641 EXPECT_THAT(err, IsEmpty());
1642 EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n"));
1643}
1644
Felipe Leme46b85da2016-11-21 17:40:45 -08001645TEST_F(DumpstateUtilTest, DumpFileOnDryRun) {
1646 CreateFd("DumpFileOnDryRun.txt");
1647 SetDryRun(true);
1648 std::string path = kTestDataPath + "single-line.txt";
Felipe Lemef0292972016-11-22 13:57:05 -08001649 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001650 EXPECT_THAT(err, IsEmpty());
Felipe Lemef0292972016-11-22 13:57:05 -08001651 EXPECT_THAT(
1652 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
1653 EXPECT_THAT(out, EndsWith("skipped on dry run\n"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001654}
Ecco Park61ffcf72016-10-27 15:46:26 -07001655
Rhed Jao27077b12020-07-14 18:38:08 +08001656class DumpPoolTest : public DumpstateBaseTest {
1657 public:
1658 void SetUp() {
Rhed Jao1c855122020-07-16 17:37:39 +08001659 dump_pool_ = std::make_unique<DumpPool>(kTestDataPath);
Rhed Jao27077b12020-07-14 18:38:08 +08001660 DumpstateBaseTest::SetUp();
1661 CreateOutputFile();
1662 }
1663
1664 void CreateOutputFile() {
1665 out_path_ = kTestDataPath + "out.txt";
1666 out_fd_.reset(TEMP_FAILURE_RETRY(open(out_path_.c_str(),
1667 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
1668 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)));
1669 ASSERT_GE(out_fd_.get(), 0) << "could not create FD for path "
1670 << out_path_;
1671 }
1672
1673 int getTempFileCounts(const std::string& folder) {
1674 int count = 0;
1675 std::unique_ptr<DIR, decltype(&closedir)> dir_ptr(opendir(folder.c_str()),
1676 &closedir);
1677 if (!dir_ptr) {
1678 return -1;
1679 }
1680 int dir_fd = dirfd(dir_ptr.get());
1681 if (dir_fd < 0) {
1682 return -1;
1683 }
1684
1685 struct dirent* de;
1686 while ((de = readdir(dir_ptr.get()))) {
1687 if (de->d_type != DT_REG) {
1688 continue;
1689 }
1690 std::string file_name(de->d_name);
1691 if (file_name.find(DumpPool::PREFIX_TMPFILE_NAME) != 0) {
1692 continue;
1693 }
1694 count++;
1695 }
1696 return count;
1697 }
1698
Rhed Jao1c855122020-07-16 17:37:39 +08001699 void setLogDuration(bool log_duration) {
1700 dump_pool_->setLogDuration(log_duration);
1701 }
1702
1703 std::unique_ptr<DumpPool> dump_pool_;
Rhed Jao27077b12020-07-14 18:38:08 +08001704 android::base::unique_fd out_fd_;
1705 std::string out_path_;
1706};
1707
Rhed Jao1c855122020-07-16 17:37:39 +08001708TEST_F(DumpPoolTest, EnqueueTaskWithFd) {
Rhed Jao27077b12020-07-14 18:38:08 +08001709 auto dump_func_1 = [](int out_fd) {
1710 dprintf(out_fd, "A");
1711 };
1712 auto dump_func_2 = [](int out_fd) {
1713 dprintf(out_fd, "B");
1714 sleep(1);
1715 };
1716 auto dump_func_3 = [](int out_fd) {
1717 dprintf(out_fd, "C");
1718 };
Rhed Jao1c855122020-07-16 17:37:39 +08001719 setLogDuration(/* log_duration = */false);
1720 dump_pool_->enqueueTaskWithFd(/* task_name = */"1", dump_func_1, std::placeholders::_1);
1721 dump_pool_->enqueueTaskWithFd(/* task_name = */"2", dump_func_2, std::placeholders::_1);
1722 dump_pool_->enqueueTaskWithFd(/* task_name = */"3", dump_func_3, std::placeholders::_1);
Rhed Jao27077b12020-07-14 18:38:08 +08001723
Rhed Jao1c855122020-07-16 17:37:39 +08001724 dump_pool_->waitForTask("1", "", out_fd_.get());
1725 dump_pool_->waitForTask("2", "", out_fd_.get());
1726 dump_pool_->waitForTask("3", "", out_fd_.get());
1727 dump_pool_->shutdown();
Rhed Jao27077b12020-07-14 18:38:08 +08001728
1729 std::string result;
1730 ReadFileToString(out_path_, &result);
1731 EXPECT_THAT(result, StrEq("A\nB\nC\n"));
1732 EXPECT_THAT(getTempFileCounts(kTestDataPath), Eq(0));
Rhed Jao1c855122020-07-16 17:37:39 +08001733}
1734
1735TEST_F(DumpPoolTest, EnqueueTask_withDurationLog) {
1736 bool run_1 = false;
1737 auto dump_func_1 = [&]() {
1738 run_1 = true;
1739 };
1740
1741 dump_pool_->enqueueTask(/* task_name = */"1", dump_func_1);
1742 dump_pool_->waitForTask("1", "", out_fd_.get());
1743 dump_pool_->shutdown();
1744
1745 std::string result;
1746 ReadFileToString(out_path_, &result);
1747 EXPECT_TRUE(run_1);
1748 EXPECT_THAT(result, StrEq("------ 0.000s was the duration of '1' ------\n"));
1749 EXPECT_THAT(getTempFileCounts(kTestDataPath), Eq(0));
Rhed Jao27077b12020-07-14 18:38:08 +08001750}
1751
1752
Felipe Leme47e9be22016-12-21 15:37:07 -08001753} // namespace dumpstate
1754} // namespace os
1755} // namespace android