blob: d692fda80f55bf71df9cf95301fac25b5bcf733b [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
20#include "DumpstateService.h"
21#include "android/os/BnDumpstate.h"
Felipe Leme4c2d6632016-09-28 14:32:00 -070022#include "dumpstate.h"
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26
27#include <libgen.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070028#include <signal.h>
29#include <sys/types.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070030#include <unistd.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070031#include <thread>
Felipe Leme4c2d6632016-09-28 14:32:00 -070032
33#include <android-base/file.h>
Felipe Lemed80e6b62016-10-03 13:08:14 -070034#include <android-base/properties.h>
35#include <android-base/stringprintf.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070036#include <android-base/strings.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070037
Felipe Leme75876a22016-10-27 16:31:27 -070038using namespace android;
Felipe Lemed80e6b62016-10-03 13:08:14 -070039
Felipe Leme4c2d6632016-09-28 14:32:00 -070040using ::testing::EndsWith;
41using ::testing::IsEmpty;
42using ::testing::StrEq;
43using ::testing::StartsWith;
44using ::testing::Test;
45using ::testing::internal::CaptureStderr;
46using ::testing::internal::CaptureStdout;
47using ::testing::internal::GetCapturedStderr;
48using ::testing::internal::GetCapturedStdout;
49
Felipe Leme75876a22016-10-27 16:31:27 -070050using os::DumpstateService;
51using os::IDumpstateListener;
52
Felipe Leme4c2d6632016-09-28 14:32:00 -070053// Not used on test cases yet...
54void dumpstate_board(void) {
55}
56
Felipe Leme75876a22016-10-27 16:31:27 -070057class DumpstateListenerMock : public IDumpstateListener {
58 public:
59 MOCK_METHOD1(onProgressUpdated, binder::Status(int32_t progress));
60 MOCK_METHOD1(onMaxProgressUpdated, binder::Status(int32_t max_progress));
61
62 protected:
63 MOCK_METHOD0(onAsBinder, IBinder*());
64};
65
Felipe Leme4c2d6632016-09-28 14:32:00 -070066class DumpstateTest : public Test {
67 public:
68 void SetUp() {
69 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -070070 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme9a523ae2016-10-20 15:10:33 -070071 ds.update_progress_ = false;
Felipe Leme4c2d6632016-09-28 14:32:00 -070072 }
73
74 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -070075 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -070076 const CommandOptions& options = CommandOptions::DEFAULT) {
77 CaptureStdout();
78 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -070079 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -070080 out = GetCapturedStdout();
81 err = GetCapturedStderr();
82 return status;
83 }
84
Felipe Lemecef02982016-10-03 17:22:22 -070085 // Dumps a file and capture `stdout` and `stderr`.
86 int DumpFile(const std::string& title, const std::string& path) {
87 CaptureStdout();
88 CaptureStderr();
89 int status = ds.DumpFile(title, path);
90 out = GetCapturedStdout();
91 err = GetCapturedStderr();
92 return status;
93 }
94
Felipe Leme9a523ae2016-10-20 15:10:33 -070095 void SetDryRun(bool dry_run) {
96 ALOGD("Setting dry_run_ to %s\n", dry_run ? "true" : "false");
97 ds.dry_run_ = dry_run;
Felipe Lemed80e6b62016-10-03 13:08:14 -070098 }
99
Felipe Leme9a523ae2016-10-20 15:10:33 -0700100 void SetBuildType(const std::string& build_type) {
101 ALOGD("Setting build_type_ to '%s'\n", build_type.c_str());
102 ds.build_type_ = build_type;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700103 }
104
105 bool IsUserBuild() {
106 return "user" == android::base::GetProperty("ro.build.type", "(unknown)");
107 }
108
109 void DropRoot() {
110 drop_root_user();
111 uid_t uid = getuid();
112 ASSERT_EQ(2000, (int)uid);
113 }
114
115 // TODO: remove when progress is set by Binder callbacks.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700116 void AssertSystemProperty(const std::string& key, const std::string& expected_value) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700117 std::string actualValue = android::base::GetProperty(key, "not set");
Felipe Leme9a523ae2016-10-20 15:10:33 -0700118 EXPECT_THAT(expected_value, StrEq(actualValue)) << "invalid value for property " << key;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700119 }
120
Felipe Leme75876a22016-10-27 16:31:27 -0700121 // TODO: remove when progress is set by Binder callbacks.
122 std::string GetProgressMessageAndAssertSystemProperties(int progress, int weight_total,
123 int old_weight_total = 0) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700124 EXPECT_EQ(progress, ds.progress_) << "invalid progress";
Felipe Leme75876a22016-10-27 16:31:27 -0700125 EXPECT_EQ(weight_total, ds.weight_total_) << "invalid weight_total";
Felipe Lemed80e6b62016-10-03 13:08:14 -0700126
127 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.progress", getpid()),
128 std::to_string(progress));
129
Felipe Leme75876a22016-10-27 16:31:27 -0700130 bool max_increased = old_weight_total > 0;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700131
Felipe Leme9a523ae2016-10-20 15:10:33 -0700132 std::string adjustment_message = "";
133 if (max_increased) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700134 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.max", getpid()),
Felipe Leme75876a22016-10-27 16:31:27 -0700135 std::to_string(weight_total));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700136 adjustment_message = android::base::StringPrintf(
Felipe Leme75876a22016-10-27 16:31:27 -0700137 "Adjusting total weight from %d to %d\n", old_weight_total, weight_total);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700138 }
139
140 return android::base::StringPrintf("%sSetting progress (dumpstate.%d.progress): %d/%d\n",
Felipe Leme9a523ae2016-10-20 15:10:33 -0700141 adjustment_message.c_str(), getpid(), progress,
Felipe Leme75876a22016-10-27 16:31:27 -0700142 weight_total);
143 }
144
145 std::string GetProgressMessage(const std::string& listener_name, int progress, int weight_total,
146 int old_weight_total = 0) {
147 EXPECT_EQ(progress, ds.progress_) << "invalid progress";
148 EXPECT_EQ(weight_total, ds.weight_total_) << "invalid weight_total";
149
150 bool max_increased = old_weight_total > 0;
151
152 std::string adjustment_message = "";
153 if (max_increased) {
154 adjustment_message = android::base::StringPrintf(
155 "Adjusting total weight from %d to %d\n", old_weight_total, weight_total);
156 }
157
158 return android::base::StringPrintf("%sSetting progress (%s): %d/%d\n",
159 adjustment_message.c_str(), listener_name.c_str(),
160 progress, weight_total);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700161 }
162
Felipe Leme4c2d6632016-09-28 14:32:00 -0700163 // `stdout` and `stderr` from the last command ran.
164 std::string out, err;
165
Felipe Leme9a523ae2016-10-20 15:10:33 -0700166 std::string test_path = dirname(android::base::GetExecutablePath().c_str());
167 std::string fixtures_path = test_path + "/../dumpstate_test_fixture/";
168 std::string test_data_path = fixtures_path + "/testdata/";
169 std::string simple_command = fixtures_path + "dumpstate_test_fixture";
170 std::string echo_command = "/system/bin/echo";
Felipe Lemefd8affa2016-09-30 17:38:57 -0700171
172 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700173};
174
175TEST_F(DumpstateTest, RunCommandNoArgs) {
176 EXPECT_EQ(-1, RunCommand("", {}));
177}
178
179TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700180 EXPECT_EQ(0, RunCommand("", {simple_command}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700181 EXPECT_THAT(out, StrEq("stdout\n"));
182 EXPECT_THAT(err, StrEq("stderr\n"));
183}
184
185TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700186 EXPECT_EQ(0, RunCommand("I AM GROOT", {simple_command}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700187 EXPECT_THAT(err, StrEq("stderr\n"));
188 // We don't know the exact duration, so we check the prefix and suffix
Felipe Lemefd8affa2016-09-30 17:38:57 -0700189 EXPECT_THAT(out,
Felipe Leme9a523ae2016-10-20 15:10:33 -0700190 StartsWith("------ I AM GROOT (" + simple_command + ") ------\nstdout\n------"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700191 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
192}
193
Felipe Lemefd8affa2016-09-30 17:38:57 -0700194TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700195 EXPECT_EQ(
Felipe Leme9a523ae2016-10-20 15:10:33 -0700196 0, RunCommand("", {simple_command},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700197 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
198 EXPECT_THAT(out, StrEq("stdout\n"));
199 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
200}
201
202TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700203 EXPECT_EQ(0, RunCommand("", {simple_command},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700204 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700205 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700206 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700207}
208
209TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700210 EXPECT_EQ(0, RunCommand("", {echo_command, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700211 EXPECT_THAT(err, IsEmpty());
212 EXPECT_THAT(out, StrEq("one\n"));
213}
214
Felipe Lemefd8affa2016-09-30 17:38:57 -0700215TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700216 EXPECT_EQ(0, RunCommand("", {echo_command, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700217 EXPECT_THAT(err, IsEmpty());
218 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
219}
220
221TEST_F(DumpstateTest, RunCommandDryRun) {
222 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700223 EXPECT_EQ(0, RunCommand("I AM GROOT", {simple_command}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700224 // We don't know the exact duration, so we check the prefix and suffix
Felipe Leme9a523ae2016-10-20 15:10:33 -0700225 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + simple_command +
Felipe Leme4c2d6632016-09-28 14:32:00 -0700226 ") ------\n\t(skipped on dry run)\n------"));
227 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
228 EXPECT_THAT(err, IsEmpty());
229}
230
231TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
232 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700233 EXPECT_EQ(0, RunCommand("", {simple_command}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700234 EXPECT_THAT(out, IsEmpty());
235 EXPECT_THAT(err, IsEmpty());
236}
237
238TEST_F(DumpstateTest, RunCommandDryRunAlways) {
239 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700240 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700241 EXPECT_THAT(out, StrEq("stdout\n"));
242 EXPECT_THAT(err, StrEq("stderr\n"));
243}
244
Felipe Lemefd8affa2016-09-30 17:38:57 -0700245TEST_F(DumpstateTest, RunCommandNotFound) {
246 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
247 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
248 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
249}
250
251TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700252 EXPECT_EQ(42, RunCommand("", {simple_command, "--exit", "42"}));
253 EXPECT_THAT(out, StrEq("stdout\n*** command '" + simple_command +
254 " --exit 42' failed: exit code 42\n"));
255 EXPECT_THAT(err, StrEq("stderr\n*** command '" + simple_command +
256 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700257}
258
259TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700260 EXPECT_NE(0, RunCommand("", {simple_command, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700261 // We don't know the exit code, so check just the prefix.
262 EXPECT_THAT(
Felipe Leme9a523ae2016-10-20 15:10:33 -0700263 out, StartsWith("stdout\n*** command '" + simple_command + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700264 EXPECT_THAT(
Felipe Leme9a523ae2016-10-20 15:10:33 -0700265 err, StartsWith("stderr\n*** command '" + simple_command + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700266}
267
268TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700269 EXPECT_EQ(-1, RunCommand("", {simple_command, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700270 CommandOptions::WithTimeout(1).Build()));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700271 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + simple_command +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700272 " --sleep 2' timed out after 1"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700273 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + simple_command +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700274 " --sleep 2' timed out after 1"));
275}
276
277TEST_F(DumpstateTest, RunCommandIsKilled) {
278 CaptureStdout();
279 CaptureStderr();
280
281 std::thread t([=]() {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700282 EXPECT_EQ(SIGTERM, ds.RunCommand("", {simple_command, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700283 CommandOptions::WithTimeout(100).Always().Build()));
284 });
285
286 // Capture pid and pre-sleep output.
287 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
288 std::string err = GetCapturedStderr();
289 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
290
291 std::string out = GetCapturedStdout();
292 std::vector<std::string> lines = android::base::Split(out, "\n");
293 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
294
295 int pid = atoi(lines[0].c_str());
296 EXPECT_THAT(lines[1], StrEq("stdout line1"));
297 EXPECT_THAT(lines[2], IsEmpty()); // \n
298
299 // Then kill the process.
300 CaptureStdout();
301 CaptureStderr();
302 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
303 t.join();
304
305 // Finally, check output after murder.
306 out = GetCapturedStdout();
307 err = GetCapturedStderr();
308
Felipe Leme9a523ae2016-10-20 15:10:33 -0700309 EXPECT_THAT(out, StrEq("*** command '" + simple_command +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700310 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700311 EXPECT_THAT(err, StrEq("*** command '" + simple_command +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700312 " --pid --sleep 20' failed: killed by signal 15\n"));
313}
314
Felipe Leme75876a22016-10-27 16:31:27 -0700315TEST_F(DumpstateTest, RunCommandProgressNoListener) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700316 ds.update_progress_ = true;
Felipe Lemecef02982016-10-03 17:22:22 -0700317 ds.progress_ = 0;
Felipe Leme9a523ae2016-10-20 15:10:33 -0700318 ds.weight_total_ = 30;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700319
Felipe Leme9a523ae2016-10-20 15:10:33 -0700320 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(20).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700321 std::string progress_message = GetProgressMessageAndAssertSystemProperties(20, 30);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700322 EXPECT_THAT(out, StrEq("stdout\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700323 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700324
Felipe Leme9a523ae2016-10-20 15:10:33 -0700325 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700326 progress_message = GetProgressMessageAndAssertSystemProperties(30, 30);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700327 EXPECT_THAT(out, StrEq("stdout\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700328 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700329
330 // Run a command that will increase maximum timeout.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700331 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700332 progress_message = GetProgressMessageAndAssertSystemProperties(31, 36, 30); // 20% increase
Felipe Lemed80e6b62016-10-03 13:08:14 -0700333 EXPECT_THAT(out, StrEq("stdout\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700334 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700335
Felipe Leme9a523ae2016-10-20 15:10:33 -0700336 // Make sure command ran while in dry_run is counted.
Felipe Lemed80e6b62016-10-03 13:08:14 -0700337 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700338 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(4).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700339 progress_message = GetProgressMessageAndAssertSystemProperties(35, 36);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700340 EXPECT_THAT(out, IsEmpty());
Felipe Leme9a523ae2016-10-20 15:10:33 -0700341 EXPECT_THAT(err, StrEq(progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700342}
343
Felipe Leme75876a22016-10-27 16:31:27 -0700344TEST_F(DumpstateTest, RunCommandProgress) {
345 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
346 ds.listener_ = listener;
347 ds.listener_name_ = "FoxMulder";
348
349 ds.update_progress_ = true;
350 ds.progress_ = 0;
351 ds.weight_total_ = 30;
352
353 EXPECT_CALL(*listener, onProgressUpdated(20));
354 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(20).Build()));
355 std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30);
356 EXPECT_THAT(out, StrEq("stdout\n"));
357 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
358
359 EXPECT_CALL(*listener, onProgressUpdated(30));
360 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Build()));
361 progress_message = GetProgressMessage(ds.listener_name_, 30, 30);
362 EXPECT_THAT(out, StrEq("stdout\n"));
363 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
364
365 // Run a command that will increase maximum timeout.
366 EXPECT_CALL(*listener, onProgressUpdated(31));
367 EXPECT_CALL(*listener, onMaxProgressUpdated(36));
368 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).Build()));
369 progress_message = GetProgressMessage(ds.listener_name_, 31, 36, 30); // 20% increase
370 EXPECT_THAT(out, StrEq("stdout\n"));
371 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
372
373 // Make sure command ran while in dry_run is counted.
374 SetDryRun(true);
375 EXPECT_CALL(*listener, onProgressUpdated(35));
376 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(4).Build()));
377 progress_message = GetProgressMessage(ds.listener_name_, 35, 36);
378 EXPECT_THAT(out, IsEmpty());
379 EXPECT_THAT(err, StrEq(progress_message));
380
381 ds.listener_.clear();
382}
383
Felipe Lemed80e6b62016-10-03 13:08:14 -0700384TEST_F(DumpstateTest, RunCommandDropRoot) {
385 // First check root case - only available when running with 'adb root'.
386 uid_t uid = getuid();
387 if (uid == 0) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700388 EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700389 EXPECT_THAT(out, StrEq("0\nstdout\n"));
390 EXPECT_THAT(err, StrEq("stderr\n"));
391 return;
392 }
393 // Then drop root.
394
Felipe Leme9a523ae2016-10-20 15:10:33 -0700395 EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700396 CommandOptions::WithTimeout(1).DropRoot().Build()));
397 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700398 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700399}
400
401TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
402 if (!IsUserBuild()) {
403 // Emulates user build if necessarily.
404 SetBuildType("user");
405 }
406
407 DropRoot();
408
Felipe Leme9a523ae2016-10-20 15:10:33 -0700409 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700410
411 // We don't know the exact path of su, so we just check for the 'root ...' commands
412 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700413 EXPECT_THAT(out, EndsWith("root " + simple_command + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700414 EXPECT_THAT(err, IsEmpty());
415}
416
417TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
418 if (IsUserBuild()) {
419 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
420 return;
421 }
422
423 DropRoot();
424
Felipe Leme9a523ae2016-10-20 15:10:33 -0700425 EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700426 CommandOptions::WithTimeout(1).AsRoot().Build()));
427
428 EXPECT_THAT(out, StrEq("0\nstdout\n"));
429 EXPECT_THAT(err, StrEq("stderr\n"));
430}
Felipe Leme4c2d6632016-09-28 14:32:00 -0700431
Felipe Lemecef02982016-10-03 17:22:22 -0700432TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
433 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
434 EXPECT_THAT(out,
435 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
436 EXPECT_THAT(err, IsEmpty());
437}
438
439TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
440 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
441 EXPECT_THAT(err, IsEmpty());
442 // We don't know the exact duration, so we check the prefix and suffix
443 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
444 "such file or directory\n"));
445 EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n"));
446}
447
448TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700449 EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700450 EXPECT_THAT(err, IsEmpty());
451 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
452}
453
454TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700455 EXPECT_EQ(0, DumpFile("", test_data_path + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700456 EXPECT_THAT(err, IsEmpty());
457 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
458}
459
460TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700461 EXPECT_EQ(0, DumpFile("", test_data_path + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700462 EXPECT_THAT(err, IsEmpty());
463 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
464}
465
466TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700467 EXPECT_EQ(0, DumpFile("", test_data_path + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700468 EXPECT_THAT(err, IsEmpty());
469 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
470}
471
472TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
473 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700474 EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700475 EXPECT_THAT(err, IsEmpty());
476 EXPECT_THAT(out, IsEmpty());
477}
478
479TEST_F(DumpstateTest, DumpFileOnDryRun) {
480 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700481 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", test_data_path + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700482 EXPECT_THAT(err, IsEmpty());
Felipe Leme9a523ae2016-10-20 15:10:33 -0700483 EXPECT_THAT(out, StartsWith("------ Might as well dump. Dump! (" + test_data_path +
Felipe Lemecef02982016-10-03 17:22:22 -0700484 "single-line.txt) ------\n\t(skipped on dry run)\n------"));
485 EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n"));
486 EXPECT_THAT(err, IsEmpty());
487}
488
Felipe Leme75876a22016-10-27 16:31:27 -0700489TEST_F(DumpstateTest, DumpFileUpdateProgressNoListener) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700490 ds.update_progress_ = true;
Felipe Lemecef02982016-10-03 17:22:22 -0700491 ds.progress_ = 0;
Felipe Leme9a523ae2016-10-20 15:10:33 -0700492 ds.weight_total_ = 30;
Felipe Lemecef02982016-10-03 17:22:22 -0700493
Felipe Leme9a523ae2016-10-20 15:10:33 -0700494 EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700495
Felipe Leme75876a22016-10-27 16:31:27 -0700496 std::string progress_message =
497 GetProgressMessageAndAssertSystemProperties(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
Felipe Lemecef02982016-10-03 17:22:22 -0700498
Felipe Leme9a523ae2016-10-20 15:10:33 -0700499 EXPECT_THAT(err, StrEq(progress_message));
Felipe Lemecef02982016-10-03 17:22:22 -0700500 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
501}
Felipe Leme75876a22016-10-27 16:31:27 -0700502
503TEST_F(DumpstateTest, DumpFileUpdateProgress) {
504 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
505 ds.listener_ = listener;
506 ds.listener_name_ = "FoxMulder";
507 ds.update_progress_ = true;
508 ds.progress_ = 0;
509 ds.weight_total_ = 30;
510
511 EXPECT_CALL(*listener, onProgressUpdated(5));
512 EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt"));
513
514 std::string progress_message =
515 GetProgressMessage(ds.listener_name_, 5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
516 EXPECT_THAT(err, StrEq(progress_message));
517 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
518
519 ds.listener_.clear();
520}
521
522class DumpstateServiceTest : public Test {
523 public:
524 DumpstateService dss;
525};
526
527TEST_F(DumpstateServiceTest, SetListenerNoName) {
528 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
529 bool result;
530 EXPECT_TRUE(dss.setListener("", listener, &result).isOk());
531 EXPECT_FALSE(result);
532}
533
534TEST_F(DumpstateServiceTest, SetListenerNoPointer) {
535 bool result;
536 EXPECT_TRUE(dss.setListener("whatever", nullptr, &result).isOk());
537 EXPECT_FALSE(result);
538}
539
540TEST_F(DumpstateServiceTest, SetListenerTwice) {
541 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
542 bool result;
543 EXPECT_TRUE(dss.setListener("whatever", listener, &result).isOk());
544 EXPECT_TRUE(result);
545
546 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
547
548 EXPECT_TRUE(dss.setListener("whatever", listener, &result).isOk());
549 EXPECT_FALSE(result);
550}