blob: 6b9369239cee250ce6398b9c11e24b37fa603e60 [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);
mhasankd451a472020-05-26 18:02:39 -0700261 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000262}
263
264TEST_F(DumpOptionsTest, InitializeInteractiveBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800265 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE, fd, fd, true);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000266 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000267 EXPECT_TRUE(options_.do_zip_file);
268 EXPECT_TRUE(options_.do_progress_updates);
Paul Chang0d2aad72020-02-13 20:04:03 +0800269 EXPECT_TRUE(options_.do_screenshot);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800270 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::INTERACTIVE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000271
272 // Other options retain default values
273 EXPECT_TRUE(options_.do_vibrate);
274 EXPECT_FALSE(options_.use_control_socket);
275 EXPECT_FALSE(options_.show_header_only);
276 EXPECT_FALSE(options_.is_remote_mode);
277 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700278 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000279}
280
281TEST_F(DumpOptionsTest, InitializeRemoteBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800282 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_REMOTE, fd, fd, false);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000283 EXPECT_TRUE(options_.do_add_date);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000284 EXPECT_TRUE(options_.do_zip_file);
285 EXPECT_TRUE(options_.is_remote_mode);
286 EXPECT_FALSE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800287 EXPECT_FALSE(options_.do_screenshot);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800288 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::REMOTE);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000289
290 // Other options retain default values
291 EXPECT_FALSE(options_.use_control_socket);
292 EXPECT_FALSE(options_.show_header_only);
293 EXPECT_FALSE(options_.do_progress_updates);
294 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700295 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000296}
297
298TEST_F(DumpOptionsTest, InitializeWearBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800299 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WEAR, fd, fd, true);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000300 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800301 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000302 EXPECT_TRUE(options_.do_zip_file);
303 EXPECT_TRUE(options_.do_progress_updates);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800304 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WEAR);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000305
306 // Other options retain default values
307 EXPECT_TRUE(options_.do_vibrate);
308 EXPECT_FALSE(options_.use_control_socket);
309 EXPECT_FALSE(options_.show_header_only);
310 EXPECT_FALSE(options_.is_remote_mode);
311 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700312 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000313}
314
315TEST_F(DumpOptionsTest, InitializeTelephonyBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800316 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_TELEPHONY, fd, fd, false);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000317 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800318 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000319 EXPECT_TRUE(options_.do_zip_file);
320 EXPECT_TRUE(options_.telephony_only);
Hunter Knepshield820f9bc2020-02-05 20:10:53 -0800321 EXPECT_TRUE(options_.do_progress_updates);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800322 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::CONNECTIVITY);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000323
324 // Other options retain default values
325 EXPECT_TRUE(options_.do_vibrate);
326 EXPECT_FALSE(options_.use_control_socket);
327 EXPECT_FALSE(options_.show_header_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000328 EXPECT_FALSE(options_.is_remote_mode);
329 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700330 EXPECT_FALSE(options_.limited_only);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000331}
332
333TEST_F(DumpOptionsTest, InitializeWifiBugReport) {
Paul Changf59c2b72020-03-10 02:08:55 +0800334 options_.Initialize(Dumpstate::BugreportMode::BUGREPORT_WIFI, fd, fd, false);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000335 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800336 EXPECT_FALSE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000337 EXPECT_TRUE(options_.do_zip_file);
338 EXPECT_TRUE(options_.wifi_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800339 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::WIFI);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000340
341 // Other options retain default values
342 EXPECT_TRUE(options_.do_vibrate);
343 EXPECT_FALSE(options_.use_control_socket);
344 EXPECT_FALSE(options_.show_header_only);
345 EXPECT_FALSE(options_.do_progress_updates);
346 EXPECT_FALSE(options_.is_remote_mode);
347 EXPECT_FALSE(options_.use_socket);
mhasankd451a472020-05-26 18:02:39 -0700348 EXPECT_FALSE(options_.limited_only);
349}
350
351TEST_F(DumpOptionsTest, InitializeLimitedOnlyBugreport) {
352 // clang-format off
353 char* argv[] = {
354 const_cast<char*>("dumpstatez"),
355 const_cast<char*>("-S"),
356 const_cast<char*>("-d"),
357 const_cast<char*>("-z"),
358 const_cast<char*>("-q"),
mhasank2d75c442020-06-11 15:05:25 -0700359 const_cast<char*>("-L"),
360 const_cast<char*>("-o abc")
mhasankd451a472020-05-26 18:02:39 -0700361 };
362 // clang-format on
363
364 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
365
366 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
367 EXPECT_TRUE(options_.do_add_date);
368 EXPECT_TRUE(options_.do_zip_file);
369 EXPECT_TRUE(options_.use_control_socket);
370 EXPECT_FALSE(options_.do_vibrate);
371 EXPECT_TRUE(options_.limited_only);
mhasank3a4cfb42020-06-15 18:06:43 -0700372 EXPECT_EQ(" abc", std::string(options_.out_dir));
mhasankd451a472020-05-26 18:02:39 -0700373
374 // Other options retain default values
375 EXPECT_FALSE(options_.show_header_only);
376 EXPECT_FALSE(options_.do_screenshot);
377 EXPECT_FALSE(options_.do_progress_updates);
378 EXPECT_FALSE(options_.is_remote_mode);
379 EXPECT_FALSE(options_.use_socket);
380 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000381}
382
383TEST_F(DumpOptionsTest, InitializeDefaultBugReport) {
384 // default: commandline options are not overridden
385 // clang-format off
386 char* argv[] = {
387 const_cast<char*>("bugreport"),
388 const_cast<char*>("-d"),
389 const_cast<char*>("-p"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000390 const_cast<char*>("-z"),
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000391 };
392 // clang-format on
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000393 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
394
395 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
396 EXPECT_TRUE(options_.do_add_date);
Paul Chang0d2aad72020-02-13 20:04:03 +0800397 EXPECT_TRUE(options_.do_screenshot);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000398 EXPECT_TRUE(options_.do_zip_file);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800399 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000400
401 // Other options retain default values
402 EXPECT_TRUE(options_.do_vibrate);
403 EXPECT_FALSE(options_.use_control_socket);
404 EXPECT_FALSE(options_.show_header_only);
405 EXPECT_FALSE(options_.do_progress_updates);
406 EXPECT_FALSE(options_.is_remote_mode);
407 EXPECT_FALSE(options_.use_socket);
408 EXPECT_FALSE(options_.wifi_only);
mhasankd451a472020-05-26 18:02:39 -0700409 EXPECT_FALSE(options_.limited_only);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100410}
411
412TEST_F(DumpOptionsTest, InitializePartial1) {
413 // clang-format off
414 char* argv[] = {
415 const_cast<char*>("dumpstate"),
416 const_cast<char*>("-d"),
417 const_cast<char*>("-z"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100418 const_cast<char*>("-s"),
419 const_cast<char*>("-S"),
420
421 };
422 // clang-format on
423
424 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
425
426 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
427 EXPECT_TRUE(options_.do_add_date);
428 EXPECT_TRUE(options_.do_zip_file);
429 // TODO: Maybe we should trim the filename
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100430 EXPECT_TRUE(options_.use_socket);
431 EXPECT_TRUE(options_.use_control_socket);
432
433 // Other options retain default values
434 EXPECT_FALSE(options_.show_header_only);
435 EXPECT_TRUE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800436 EXPECT_FALSE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100437 EXPECT_FALSE(options_.do_progress_updates);
438 EXPECT_FALSE(options_.is_remote_mode);
mhasankd451a472020-05-26 18:02:39 -0700439 EXPECT_FALSE(options_.limited_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800440 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100441}
442
443TEST_F(DumpOptionsTest, InitializePartial2) {
444 // clang-format off
445 char* argv[] = {
446 const_cast<char*>("dumpstate"),
447 const_cast<char*>("-v"),
448 const_cast<char*>("-q"),
449 const_cast<char*>("-p"),
450 const_cast<char*>("-P"),
451 const_cast<char*>("-R"),
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100452 };
453 // clang-format on
454
455 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
456
457 EXPECT_EQ(status, Dumpstate::RunStatus::OK);
458 EXPECT_TRUE(options_.show_header_only);
459 EXPECT_FALSE(options_.do_vibrate);
Paul Chang0d2aad72020-02-13 20:04:03 +0800460 EXPECT_TRUE(options_.do_screenshot);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100461 EXPECT_TRUE(options_.do_progress_updates);
462 EXPECT_TRUE(options_.is_remote_mode);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100463
464 // Other options retain default values
465 EXPECT_FALSE(options_.do_add_date);
466 EXPECT_FALSE(options_.do_zip_file);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100467 EXPECT_FALSE(options_.use_socket);
468 EXPECT_FALSE(options_.use_control_socket);
mhasankd451a472020-05-26 18:02:39 -0700469 EXPECT_FALSE(options_.limited_only);
Hunter Knepshield8540faf2020-02-04 19:47:20 -0800470 EXPECT_EQ(options_.dumpstate_hal_mode, DumpstateMode::DEFAULT);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100471}
472
473TEST_F(DumpOptionsTest, InitializeHelp) {
474 // clang-format off
475 char* argv[] = {
476 const_cast<char*>("dumpstate"),
477 const_cast<char*>("-h")
478 };
479 // clang-format on
480
481 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
482
483 // -h is for help.
484 EXPECT_EQ(status, Dumpstate::RunStatus::HELP);
485}
486
487TEST_F(DumpOptionsTest, InitializeUnknown) {
488 // clang-format off
489 char* argv[] = {
490 const_cast<char*>("dumpstate"),
491 const_cast<char*>("-u") // unknown flag
492 };
493 // clang-format on
494
495 Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv);
496
497 // -u is unknown.
498 EXPECT_EQ(status, Dumpstate::RunStatus::INVALID_INPUT);
499}
500
501TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile1) {
502 options_.do_zip_file = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000503 // Writing to socket = !writing to file.
504 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100505 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000506
507 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100508 EXPECT_TRUE(options_.ValidateOptions());
509}
510
511TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile2) {
Abhijeet Kaure370d682019-10-01 16:49:30 +0100512 options_.do_progress_updates = true;
Nandana Dutt9a76d202019-01-21 15:56:48 +0000513 // Writing to socket = !writing to file.
514 options_.use_socket = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100515 EXPECT_FALSE(options_.ValidateOptions());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000516
517 options_.use_socket = false;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100518 EXPECT_TRUE(options_.ValidateOptions());
519}
520
521TEST_F(DumpOptionsTest, ValidateOptionsNeedZipfile) {
522 options_.use_control_socket = true;
523 EXPECT_FALSE(options_.ValidateOptions());
524
525 options_.do_zip_file = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100526 EXPECT_TRUE(options_.ValidateOptions());
527}
528
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100529TEST_F(DumpOptionsTest, ValidateOptionsRemoteMode) {
530 options_.is_remote_mode = true;
531 EXPECT_FALSE(options_.ValidateOptions());
532
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100533 options_.do_zip_file = true;
534 options_.do_add_date = true;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100535 EXPECT_TRUE(options_.ValidateOptions());
536}
537
Felipe Leme7447d7c2016-11-03 18:12:22 -0700538class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700539 public:
540 void SetUp() {
Felipe Leme46b85da2016-11-21 17:40:45 -0800541 DumpstateBaseTest::SetUp();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700542 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700543 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700544 ds.progress_.reset(new Progress());
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100545 ds.options_.reset(new Dumpstate::DumpOptions());
Felipe Leme4c2d6632016-09-28 14:32:00 -0700546 }
547
Rhed Jao1c855122020-07-16 17:37:39 +0800548 void TearDown() {
549 ds.ShutdownDumpPool();
550 }
551
Felipe Leme4c2d6632016-09-28 14:32:00 -0700552 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700553 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700554 const CommandOptions& options = CommandOptions::DEFAULT) {
555 CaptureStdout();
556 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700557 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700558 out = GetCapturedStdout();
559 err = GetCapturedStderr();
560 return status;
561 }
562
Felipe Lemecef02982016-10-03 17:22:22 -0700563 // Dumps a file and capture `stdout` and `stderr`.
564 int DumpFile(const std::string& title, const std::string& path) {
565 CaptureStdout();
566 CaptureStderr();
567 int status = ds.DumpFile(title, path);
568 out = GetCapturedStdout();
569 err = GetCapturedStderr();
570 return status;
571 }
572
Nandana Dutt402a8392019-06-14 14:25:13 +0100573 void SetProgress(long progress, long initial_max) {
574 ds.last_reported_percent_progress_ = 0;
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100575 ds.options_->do_progress_updates = true;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700576 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
577 }
578
Rhed Jao1c855122020-07-16 17:37:39 +0800579 void EnableParallelRunIfNeeded() {
580 ds.EnableParallelRunIfNeeded();
581 }
582
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100583 std::string GetProgressMessage(int progress, int max,
584 int old_max = 0, bool update_progress = true) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700585 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
586 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700587
Felipe Leme7447d7c2016-11-03 18:12:22 -0700588 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700589
Felipe Leme009ecbb2016-11-07 10:18:44 -0800590 std::string message = "";
Felipe Leme75876a22016-10-27 16:31:27 -0700591 if (max_increased) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800592 message =
Felipe Leme7447d7c2016-11-03 18:12:22 -0700593 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700594 }
595
Felipe Leme009ecbb2016-11-07 10:18:44 -0800596 if (update_progress) {
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100597 message += android::base::StringPrintf("Setting progress: %d/%d (%d%%)\n",
598 progress, max, (100 * progress / max));
Felipe Leme009ecbb2016-11-07 10:18:44 -0800599 }
600
601 return message;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700602 }
603
Felipe Leme4c2d6632016-09-28 14:32:00 -0700604 // `stdout` and `stderr` from the last command ran.
605 std::string out, err;
606
Felipe Lemefd8affa2016-09-30 17:38:57 -0700607 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700608};
609
610TEST_F(DumpstateTest, RunCommandNoArgs) {
611 EXPECT_EQ(-1, RunCommand("", {}));
612}
613
614TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700615 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700616 EXPECT_THAT(out, StrEq("stdout\n"));
617 EXPECT_THAT(err, StrEq("stderr\n"));
618}
619
620TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700621 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700622 EXPECT_THAT(err, StrEq("stderr\n"));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700623 // The duration may not get output, depending on how long it takes,
624 // so we just check the prefix.
Felipe Lemefd8affa2016-09-30 17:38:57 -0700625 EXPECT_THAT(out,
Nandana Dutt47527b52019-03-29 15:34:36 +0000626 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700627}
628
Felipe Lemefd8affa2016-09-30 17:38:57 -0700629TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700630 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700631 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700632 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
633 EXPECT_THAT(out, StrEq("stdout\n"));
634 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
635}
636
637TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700638 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700639 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700640 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700641 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700642}
643
644TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700645 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700646 EXPECT_THAT(err, IsEmpty());
647 EXPECT_THAT(out, StrEq("one\n"));
648}
649
Felipe Lemefd8affa2016-09-30 17:38:57 -0700650TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700651 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700652 EXPECT_THAT(err, IsEmpty());
653 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
654}
655
656TEST_F(DumpstateTest, RunCommandDryRun) {
657 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700658 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Greg Kaiser3a811c12019-05-21 12:48:59 -0700659 // The duration may not get output, depending on how long it takes,
660 // so we just check the prefix.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700661 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Nandana Dutt47527b52019-03-29 15:34:36 +0000662 ") ------\n\t(skipped on dry run)\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700663 EXPECT_THAT(err, IsEmpty());
664}
665
666TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
667 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700668 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700669 EXPECT_THAT(out, IsEmpty());
670 EXPECT_THAT(err, IsEmpty());
671}
672
673TEST_F(DumpstateTest, RunCommandDryRunAlways) {
674 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700675 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700676 EXPECT_THAT(out, StrEq("stdout\n"));
677 EXPECT_THAT(err, StrEq("stderr\n"));
678}
679
Felipe Lemefd8affa2016-09-30 17:38:57 -0700680TEST_F(DumpstateTest, RunCommandNotFound) {
681 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
682 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
683 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
684}
685
686TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700687 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
688 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700689 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700690 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700691 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700692}
693
694TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700695 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700696 // We don't know the exit code, so check just the prefix.
697 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700698 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700699 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700700 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700701}
702
703TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700704 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700705 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700706 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700707 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700708 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700709 " --sleep 2' timed out after 1"));
710}
711
712TEST_F(DumpstateTest, RunCommandIsKilled) {
713 CaptureStdout();
714 CaptureStderr();
715
716 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700717 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700718 CommandOptions::WithTimeout(100).Always().Build()));
719 });
720
721 // Capture pid and pre-sleep output.
722 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
723 std::string err = GetCapturedStderr();
724 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
725
726 std::string out = GetCapturedStdout();
727 std::vector<std::string> lines = android::base::Split(out, "\n");
728 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
729
730 int pid = atoi(lines[0].c_str());
731 EXPECT_THAT(lines[1], StrEq("stdout line1"));
732 EXPECT_THAT(lines[2], IsEmpty()); // \n
733
734 // Then kill the process.
735 CaptureStdout();
736 CaptureStderr();
737 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
738 t.join();
739
740 // Finally, check output after murder.
741 out = GetCapturedStdout();
742 err = GetCapturedStderr();
743
Felipe Leme7447d7c2016-11-03 18:12:22 -0700744 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700745 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700746 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700747 " --pid --sleep 20' failed: killed by signal 15\n"));
748}
749
Felipe Leme75876a22016-10-27 16:31:27 -0700750TEST_F(DumpstateTest, RunCommandProgress) {
751 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
752 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700753 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700754
Nandana Duttbabf6c72019-01-15 14:11:12 +0000755 EXPECT_CALL(*listener, onProgress(66)); // 20/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -0700756 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100757 std::string progress_message = GetProgressMessage(20, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700758 EXPECT_THAT(out, StrEq("stdout\n"));
759 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
760
Nandana Dutt402a8392019-06-14 14:25:13 +0100761 EXPECT_CALL(*listener, onProgress(80)); // 24/30 %
762 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100763 progress_message = GetProgressMessage(24, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700764 EXPECT_THAT(out, StrEq("stdout\n"));
765 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
766
767 // Make sure command ran while in dry_run is counted.
768 SetDryRun(true);
Nandana Dutt402a8392019-06-14 14:25:13 +0100769 EXPECT_CALL(*listener, onProgress(90)); // 27/30 %
770 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100771 progress_message = GetProgressMessage(27, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700772 EXPECT_THAT(out, IsEmpty());
773 EXPECT_THAT(err, StrEq(progress_message));
774
Nandana Dutt402a8392019-06-14 14:25:13 +0100775 SetDryRun(false);
776 EXPECT_CALL(*listener, onProgress(96)); // 29/30 %
777 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(2).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100778 progress_message = GetProgressMessage(29, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800779 EXPECT_THAT(out, StrEq("stdout\n"));
780 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
781
Nandana Dutt402a8392019-06-14 14:25:13 +0100782 EXPECT_CALL(*listener, onProgress(100)); // 30/30 %
Felipe Leme009ecbb2016-11-07 10:18:44 -0800783 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100784 progress_message = GetProgressMessage(30, 30);
Felipe Leme009ecbb2016-11-07 10:18:44 -0800785 EXPECT_THAT(out, StrEq("stdout\n"));
786 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
787
788 ds.listener_.clear();
789}
790
Felipe Lemed80e6b62016-10-03 13:08:14 -0700791TEST_F(DumpstateTest, RunCommandDropRoot) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800792 if (!IsStandalone()) {
793 // TODO: temporarily disabled because it might cause other tests to fail after dropping
794 // to Shell - need to refactor tests to avoid this problem)
795 MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n")
796 return;
797 }
Felipe Lemed80e6b62016-10-03 13:08:14 -0700798 // First check root case - only available when running with 'adb root'.
799 uid_t uid = getuid();
800 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700801 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700802 EXPECT_THAT(out, StrEq("0\nstdout\n"));
803 EXPECT_THAT(err, StrEq("stderr\n"));
804 return;
805 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700806 // Then run dropping root.
807 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700808 CommandOptions::WithTimeout(1).DropRoot().Build()));
809 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700810 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700811}
812
813TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800814 if (!IsStandalone()) {
815 // TODO: temporarily disabled because it might cause other tests to fail after dropping
816 // to Shell - need to refactor tests to avoid this problem)
817 MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n")
818 return;
819 }
Felipe Lemef0292972016-11-22 13:57:05 -0800820 if (!PropertiesHelper::IsUserBuild()) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700821 // Emulates user build if necessarily.
822 SetBuildType("user");
823 }
824
825 DropRoot();
826
Felipe Leme7447d7c2016-11-03 18:12:22 -0700827 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700828
829 // We don't know the exact path of su, so we just check for the 'root ...' commands
830 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700831 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700832 EXPECT_THAT(err, IsEmpty());
833}
834
Felipe Leme46b85da2016-11-21 17:40:45 -0800835TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
836 if (!IsStandalone()) {
837 // TODO: temporarily disabled because it might cause other tests to fail after dropping
838 // to Shell - need to refactor tests to avoid this problem)
839 MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n")
840 return;
841 }
Felipe Lemef0292972016-11-22 13:57:05 -0800842 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800843 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
844 return;
845 }
846
847 DropRoot();
848
849 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
850 CommandOptions::WithTimeout(1).AsRoot().Build()));
851
852 EXPECT_THAT(out, StrEq("0\nstdout\n"));
853 EXPECT_THAT(err, StrEq("stderr\n"));
854}
855
Nandana Dutt4b392be2018-11-02 16:17:05 +0000856TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild_withUnroot) {
857 if (!IsStandalone()) {
858 // TODO: temporarily disabled because it might cause other tests to fail after dropping
859 // to Shell - need to refactor tests to avoid this problem)
860 MYLOGE(
861 "Skipping DumpstateTest.RunCommandAsRootNonUserBuild_withUnroot() "
862 "on test suite\n")
863 return;
864 }
865 if (PropertiesHelper::IsUserBuild()) {
866 ALOGI("Skipping RunCommandAsRootNonUserBuild_withUnroot on user builds\n");
867 return;
868 }
869
870 // Same test as above, but with unroot property set, which will override su availability.
871 SetUnroot(true);
872 DropRoot();
873
874 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
875 CommandOptions::WithTimeout(1).AsRoot().Build()));
876
877 // AsRoot is ineffective.
878 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
879 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
880}
881
Yifan Hong48e83a12017-10-03 14:10:07 -0700882TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) {
883 if (!IsStandalone()) {
884 // TODO: temporarily disabled because it might cause other tests to fail after dropping
885 // to Shell - need to refactor tests to avoid this problem)
886 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
887 return;
888 }
889 if (!PropertiesHelper::IsUserBuild()) {
890 // Emulates user build if necessarily.
891 SetBuildType("user");
892 }
893
894 DropRoot();
895
896 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
897 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
898
899 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
900 EXPECT_THAT(err, StrEq("stderr\n"));
901}
902
903TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) {
904 if (!IsStandalone()) {
905 // TODO: temporarily disabled because it might cause other tests to fail after dropping
906 // to Shell - need to refactor tests to avoid this problem)
907 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
908 return;
909 }
910 if (PropertiesHelper::IsUserBuild()) {
911 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
912 return;
913 }
914
915 DropRoot();
916
917 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
918 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
919
920 EXPECT_THAT(out, StrEq("0\nstdout\n"));
921 EXPECT_THAT(err, StrEq("stderr\n"));
922}
923
Nandana Dutt4b392be2018-11-02 16:17:05 +0000924TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild_withUnroot) {
925 if (!IsStandalone()) {
926 // TODO: temporarily disabled because it might cause other tests to fail after dropping
927 // to Shell - need to refactor tests to avoid this problem)
928 MYLOGE(
929 "Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild_withUnroot() "
930 "on test suite\n")
931 return;
932 }
933 if (PropertiesHelper::IsUserBuild()) {
934 ALOGI("Skipping RunCommandAsRootIfAvailableOnDebugBuild_withUnroot on user builds\n");
935 return;
936 }
937 // Same test as above, but with unroot property set, which will override su availability.
938 SetUnroot(true);
939
940 DropRoot();
941
942 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
943 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
944
945 // It's a userdebug build, so "su root" should be available, but unroot=true overrides it.
946 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
947 EXPECT_THAT(err, StrEq("stderr\n"));
948}
949
Felipe Lemecef02982016-10-03 17:22:22 -0700950TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
951 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
952 EXPECT_THAT(out,
953 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
954 EXPECT_THAT(err, IsEmpty());
955}
956
957TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
958 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
959 EXPECT_THAT(err, IsEmpty());
Greg Kaiser3a811c12019-05-21 12:48:59 -0700960 // The duration may not get output, depending on how long it takes,
961 // so we just check the prefix.
Felipe Lemecef02982016-10-03 17:22:22 -0700962 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
963 "such file or directory\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700964}
965
966TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700967 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700968 EXPECT_THAT(err, IsEmpty());
969 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
970}
971
972TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700973 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700974 EXPECT_THAT(err, IsEmpty());
975 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
976}
977
978TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700979 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700980 EXPECT_THAT(err, IsEmpty());
981 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
982}
983
984TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700985 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700986 EXPECT_THAT(err, IsEmpty());
987 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
988}
989
990TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
991 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700992 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700993 EXPECT_THAT(err, IsEmpty());
994 EXPECT_THAT(out, IsEmpty());
995}
996
997TEST_F(DumpstateTest, DumpFileOnDryRun) {
998 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700999 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -07001000 EXPECT_THAT(err, IsEmpty());
Felipe Leme46b85da2016-11-21 17:40:45 -08001001 EXPECT_THAT(
1002 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
Nandana Dutt47527b52019-03-29 15:34:36 +00001003 EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n"));
Felipe Lemecef02982016-10-03 17:22:22 -07001004}
1005
Felipe Leme75876a22016-10-27 16:31:27 -07001006TEST_F(DumpstateTest, DumpFileUpdateProgress) {
1007 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
1008 ds.listener_ = listener;
Felipe Leme7447d7c2016-11-03 18:12:22 -07001009 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -07001010
Nandana Duttbabf6c72019-01-15 14:11:12 +00001011 EXPECT_CALL(*listener, onProgress(16)); // 5/30 %
Felipe Leme7447d7c2016-11-03 18:12:22 -07001012 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -07001013
Abhijeet Kaured5d6a62019-10-07 15:02:05 +01001014 std::string progress_message = GetProgressMessage(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
Felipe Leme75876a22016-10-27 16:31:27 -07001015 EXPECT_THAT(err, StrEq(progress_message));
1016 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1017
1018 ds.listener_.clear();
1019}
1020
Rhed Jao1c855122020-07-16 17:37:39 +08001021TEST_F(DumpstateTest, DumpPool_withOutputToFileAndParallelRunEnabled_notNull) {
1022 ds.options_->use_socket = false;
1023 SetParallelRun(true);
1024 EnableParallelRunIfNeeded();
1025 EXPECT_TRUE(ds.options_->OutputToFile());
Rhed Jao4875aa62020-07-20 17:46:29 +08001026 EXPECT_TRUE(ds.zip_entry_tasks_);
Rhed Jao1c855122020-07-16 17:37:39 +08001027 EXPECT_TRUE(ds.dump_pool_);
1028}
1029
1030TEST_F(DumpstateTest, DumpPool_withNotOutputToFile_isNull) {
1031 ds.options_->use_socket = true;
1032 EnableParallelRunIfNeeded();
1033 EXPECT_FALSE(ds.options_->OutputToFile());
Rhed Jao4875aa62020-07-20 17:46:29 +08001034 EXPECT_FALSE(ds.zip_entry_tasks_);
Rhed Jao1c855122020-07-16 17:37:39 +08001035 EXPECT_FALSE(ds.dump_pool_);
1036}
1037
1038TEST_F(DumpstateTest, DumpPool_withParallelRunDisabled_isNull) {
1039 SetParallelRun(false);
1040 EnableParallelRunIfNeeded();
Rhed Jao4875aa62020-07-20 17:46:29 +08001041 EXPECT_FALSE(ds.zip_entry_tasks_);
Rhed Jao1c855122020-07-16 17:37:39 +08001042 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
Rhed Jao4875aa62020-07-20 17:46:29 +08001752class TaskQueueTest : public DumpstateBaseTest {
1753public:
1754 void SetUp() {
1755 DumpstateBaseTest::SetUp();
1756 }
1757
1758 TaskQueue task_queue_;
1759};
1760
1761TEST_F(TaskQueueTest, runTask) {
1762 bool is_task1_run = false;
1763 bool is_task2_run = false;
1764 auto task_1 = [&](bool task_cancelled) {
1765 if (task_cancelled) {
1766 return;
1767 }
1768 is_task1_run = true;
1769 };
1770 auto task_2 = [&](bool task_cancelled) {
1771 if (task_cancelled) {
1772 return;
1773 }
1774 is_task2_run = true;
1775 };
1776 task_queue_.add(task_1, std::placeholders::_1);
1777 task_queue_.add(task_2, std::placeholders::_1);
1778
1779 task_queue_.run(/* do_cancel = */false);
1780
1781 EXPECT_TRUE(is_task1_run);
1782 EXPECT_TRUE(is_task2_run);
1783}
1784
1785TEST_F(TaskQueueTest, runTask_withCancelled) {
1786 bool is_task1_cancelled = false;
1787 bool is_task2_cancelled = false;
1788 auto task_1 = [&](bool task_cancelled) {
1789 is_task1_cancelled = task_cancelled;
1790 };
1791 auto task_2 = [&](bool task_cancelled) {
1792 is_task2_cancelled = task_cancelled;
1793 };
1794 task_queue_.add(task_1, std::placeholders::_1);
1795 task_queue_.add(task_2, std::placeholders::_1);
1796
1797 task_queue_.run(/* do_cancel = */true);
1798
1799 EXPECT_TRUE(is_task1_cancelled);
1800 EXPECT_TRUE(is_task2_cancelled);
1801}
1802
Rhed Jao27077b12020-07-14 18:38:08 +08001803
Felipe Leme47e9be22016-12-21 15:37:07 -08001804} // namespace dumpstate
1805} // namespace os
1806} // namespace android