blob: 2fcf321ad301e07b67c7850e19855f3f70741c7f [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 Leme7447d7c2016-11-03 18:12:22 -070066// Base class for all tests in this file
67class DumpstateBaseTest : public Test {
68 protected:
69 const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str());
70 const std::string kFixturesPath = kTestPath + "/../dumpstate_test_fixture/";
71 const std::string kTestDataPath = kFixturesPath + "/testdata/";
72 const std::string kSimpleCommand = kFixturesPath + "dumpstate_test_fixture";
73 const std::string kEchoCommand = "/system/bin/echo";
74
75 /*
76 * Copies a text file fixture to a temporary file, returning it's path.
77 *
78 * Useful in cases where the test case changes the content of the tile.
79 */
80 std::string CopyTextFileFixture(const std::string& relative_name) {
81 std::string from = kTestDataPath + relative_name;
82 // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it
83 // around for poking when the test fails.
84 std::string to = kTestDataPath + relative_name + ".tmp";
85 ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str());
86 android::base::RemoveFileIfExists(to);
87 CopyTextFile(from, to);
88 return to.c_str();
89 }
90
91 private:
92 // Need a function that returns void to use assertions -
93 // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement
94 void CopyTextFile(const std::string& from, const std::string& to) {
95 std::string content;
96 ASSERT_TRUE(android::base::ReadFileToString(from, &content)) << "could not read from "
97 << from;
98 ASSERT_TRUE(android::base::WriteStringToFile(content, to)) << "could not write to " << to;
99 }
100};
101
102class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700103 public:
104 void SetUp() {
105 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700106 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700107 ds.progress_.reset(new Progress());
Felipe Leme9a523ae2016-10-20 15:10:33 -0700108 ds.update_progress_ = false;
Felipe Leme4c2d6632016-09-28 14:32:00 -0700109 }
110
111 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700112 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700113 const CommandOptions& options = CommandOptions::DEFAULT) {
114 CaptureStdout();
115 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700116 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700117 out = GetCapturedStdout();
118 err = GetCapturedStderr();
119 return status;
120 }
121
Felipe Lemecef02982016-10-03 17:22:22 -0700122 // Dumps a file and capture `stdout` and `stderr`.
123 int DumpFile(const std::string& title, const std::string& path) {
124 CaptureStdout();
125 CaptureStderr();
126 int status = ds.DumpFile(title, path);
127 out = GetCapturedStdout();
128 err = GetCapturedStderr();
129 return status;
130 }
131
Felipe Leme9a523ae2016-10-20 15:10:33 -0700132 void SetDryRun(bool dry_run) {
133 ALOGD("Setting dry_run_ to %s\n", dry_run ? "true" : "false");
134 ds.dry_run_ = dry_run;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700135 }
136
Felipe Leme9a523ae2016-10-20 15:10:33 -0700137 void SetBuildType(const std::string& build_type) {
138 ALOGD("Setting build_type_ to '%s'\n", build_type.c_str());
139 ds.build_type_ = build_type;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700140 }
141
142 bool IsUserBuild() {
143 return "user" == android::base::GetProperty("ro.build.type", "(unknown)");
144 }
145
146 void DropRoot() {
147 drop_root_user();
148 uid_t uid = getuid();
149 ASSERT_EQ(2000, (int)uid);
150 }
151
Felipe Leme7447d7c2016-11-03 18:12:22 -0700152 void SetProgress(long progress, long initial_max) {
153 ds.update_progress_ = true;
154 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
155 }
156
Felipe Lemed80e6b62016-10-03 13:08:14 -0700157 // TODO: remove when progress is set by Binder callbacks.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700158 void AssertSystemProperty(const std::string& key, const std::string& expected_value) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700159 std::string actualValue = android::base::GetProperty(key, "not set");
Felipe Leme9a523ae2016-10-20 15:10:33 -0700160 EXPECT_THAT(expected_value, StrEq(actualValue)) << "invalid value for property " << key;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700161 }
162
Felipe Leme75876a22016-10-27 16:31:27 -0700163 // TODO: remove when progress is set by Binder callbacks.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700164 std::string GetProgressMessageAndAssertSystemProperties(int progress, int max, int old_max = 0) {
165 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
166 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Lemed80e6b62016-10-03 13:08:14 -0700167
168 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.progress", getpid()),
169 std::to_string(progress));
170
Felipe Leme7447d7c2016-11-03 18:12:22 -0700171 bool max_increased = old_max > 0;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700172
Felipe Leme9a523ae2016-10-20 15:10:33 -0700173 std::string adjustment_message = "";
174 if (max_increased) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700175 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.max", getpid()),
Felipe Leme7447d7c2016-11-03 18:12:22 -0700176 std::to_string(max));
177 adjustment_message =
178 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700179 }
180
181 return android::base::StringPrintf("%sSetting progress (dumpstate.%d.progress): %d/%d\n",
Felipe Leme7447d7c2016-11-03 18:12:22 -0700182 adjustment_message.c_str(), getpid(), progress, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700183 }
184
Felipe Leme7447d7c2016-11-03 18:12:22 -0700185 std::string GetProgressMessage(const std::string& listener_name, int progress, int max,
186 int old_max = 0) {
187 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
188 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700189
Felipe Leme7447d7c2016-11-03 18:12:22 -0700190 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700191
192 std::string adjustment_message = "";
193 if (max_increased) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700194 adjustment_message =
195 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700196 }
197
198 return android::base::StringPrintf("%sSetting progress (%s): %d/%d\n",
199 adjustment_message.c_str(), listener_name.c_str(),
Felipe Leme7447d7c2016-11-03 18:12:22 -0700200 progress, max);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700201 }
202
Felipe Leme4c2d6632016-09-28 14:32:00 -0700203 // `stdout` and `stderr` from the last command ran.
204 std::string out, err;
205
Felipe Lemefd8affa2016-09-30 17:38:57 -0700206 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700207};
208
209TEST_F(DumpstateTest, RunCommandNoArgs) {
210 EXPECT_EQ(-1, RunCommand("", {}));
211}
212
213TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700214 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700215 EXPECT_THAT(out, StrEq("stdout\n"));
216 EXPECT_THAT(err, StrEq("stderr\n"));
217}
218
219TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700220 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700221 EXPECT_THAT(err, StrEq("stderr\n"));
222 // We don't know the exact duration, so we check the prefix and suffix
Felipe Lemefd8affa2016-09-30 17:38:57 -0700223 EXPECT_THAT(out,
Felipe Leme7447d7c2016-11-03 18:12:22 -0700224 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n------"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700225 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
226}
227
Felipe Lemefd8affa2016-09-30 17:38:57 -0700228TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700229 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700230 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700231 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
232 EXPECT_THAT(out, StrEq("stdout\n"));
233 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
234}
235
236TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700237 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700238 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700239 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700240 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700241}
242
243TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700244 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700245 EXPECT_THAT(err, IsEmpty());
246 EXPECT_THAT(out, StrEq("one\n"));
247}
248
Felipe Lemefd8affa2016-09-30 17:38:57 -0700249TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700250 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700251 EXPECT_THAT(err, IsEmpty());
252 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
253}
254
255TEST_F(DumpstateTest, RunCommandDryRun) {
256 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700257 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700258 // We don't know the exact duration, so we check the prefix and suffix
Felipe Leme7447d7c2016-11-03 18:12:22 -0700259 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Felipe Leme4c2d6632016-09-28 14:32:00 -0700260 ") ------\n\t(skipped on dry run)\n------"));
261 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
262 EXPECT_THAT(err, IsEmpty());
263}
264
265TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
266 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700267 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700268 EXPECT_THAT(out, IsEmpty());
269 EXPECT_THAT(err, IsEmpty());
270}
271
272TEST_F(DumpstateTest, RunCommandDryRunAlways) {
273 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700274 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700275 EXPECT_THAT(out, StrEq("stdout\n"));
276 EXPECT_THAT(err, StrEq("stderr\n"));
277}
278
Felipe Lemefd8affa2016-09-30 17:38:57 -0700279TEST_F(DumpstateTest, RunCommandNotFound) {
280 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
281 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
282 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
283}
284
285TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700286 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
287 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700288 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700289 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700290 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700291}
292
293TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700294 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700295 // We don't know the exit code, so check just the prefix.
296 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700297 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700298 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700299 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700300}
301
302TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700303 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700304 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700305 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700306 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700307 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700308 " --sleep 2' timed out after 1"));
309}
310
311TEST_F(DumpstateTest, RunCommandIsKilled) {
312 CaptureStdout();
313 CaptureStderr();
314
315 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700316 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700317 CommandOptions::WithTimeout(100).Always().Build()));
318 });
319
320 // Capture pid and pre-sleep output.
321 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
322 std::string err = GetCapturedStderr();
323 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
324
325 std::string out = GetCapturedStdout();
326 std::vector<std::string> lines = android::base::Split(out, "\n");
327 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
328
329 int pid = atoi(lines[0].c_str());
330 EXPECT_THAT(lines[1], StrEq("stdout line1"));
331 EXPECT_THAT(lines[2], IsEmpty()); // \n
332
333 // Then kill the process.
334 CaptureStdout();
335 CaptureStderr();
336 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
337 t.join();
338
339 // Finally, check output after murder.
340 out = GetCapturedStdout();
341 err = GetCapturedStderr();
342
Felipe Leme7447d7c2016-11-03 18:12:22 -0700343 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700344 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700345 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700346 " --pid --sleep 20' failed: killed by signal 15\n"));
347}
348
Felipe Leme75876a22016-10-27 16:31:27 -0700349TEST_F(DumpstateTest, RunCommandProgressNoListener) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700350 SetProgress(0, 30);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700351
Felipe Leme7447d7c2016-11-03 18:12:22 -0700352 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700353 std::string progress_message = GetProgressMessageAndAssertSystemProperties(20, 30);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700354 EXPECT_THAT(out, StrEq("stdout\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700355 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700356
Felipe Leme7447d7c2016-11-03 18:12:22 -0700357 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700358 progress_message = GetProgressMessageAndAssertSystemProperties(30, 30);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700359 EXPECT_THAT(out, StrEq("stdout\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700360 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700361
362 // Run a command that will increase maximum timeout.
Felipe Leme7447d7c2016-11-03 18:12:22 -0700363 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
364 progress_message = GetProgressMessageAndAssertSystemProperties(31, 37, 30); // 20% increase
Felipe Lemed80e6b62016-10-03 13:08:14 -0700365 EXPECT_THAT(out, StrEq("stdout\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700366 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700367
Felipe Leme9a523ae2016-10-20 15:10:33 -0700368 // Make sure command ran while in dry_run is counted.
Felipe Lemed80e6b62016-10-03 13:08:14 -0700369 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700370 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
371 progress_message = GetProgressMessageAndAssertSystemProperties(35, 37);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700372 EXPECT_THAT(out, IsEmpty());
Felipe Leme9a523ae2016-10-20 15:10:33 -0700373 EXPECT_THAT(err, StrEq(progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700374}
375
Felipe Leme75876a22016-10-27 16:31:27 -0700376TEST_F(DumpstateTest, RunCommandProgress) {
377 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
378 ds.listener_ = listener;
379 ds.listener_name_ = "FoxMulder";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700380 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700381
382 EXPECT_CALL(*listener, onProgressUpdated(20));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700383 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700384 std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30);
385 EXPECT_THAT(out, StrEq("stdout\n"));
386 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
387
388 EXPECT_CALL(*listener, onProgressUpdated(30));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700389 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700390 progress_message = GetProgressMessage(ds.listener_name_, 30, 30);
391 EXPECT_THAT(out, StrEq("stdout\n"));
392 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
393
394 // Run a command that will increase maximum timeout.
395 EXPECT_CALL(*listener, onProgressUpdated(31));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700396 EXPECT_CALL(*listener, onMaxProgressUpdated(37));
397 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
398 progress_message = GetProgressMessage(ds.listener_name_, 31, 37, 30); // 20% increase
Felipe Leme75876a22016-10-27 16:31:27 -0700399 EXPECT_THAT(out, StrEq("stdout\n"));
400 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
401
402 // Make sure command ran while in dry_run is counted.
403 SetDryRun(true);
404 EXPECT_CALL(*listener, onProgressUpdated(35));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700405 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
406 progress_message = GetProgressMessage(ds.listener_name_, 35, 37);
Felipe Leme75876a22016-10-27 16:31:27 -0700407 EXPECT_THAT(out, IsEmpty());
408 EXPECT_THAT(err, StrEq(progress_message));
409
410 ds.listener_.clear();
411}
412
Felipe Lemed80e6b62016-10-03 13:08:14 -0700413TEST_F(DumpstateTest, RunCommandDropRoot) {
414 // First check root case - only available when running with 'adb root'.
415 uid_t uid = getuid();
416 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700417 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700418 EXPECT_THAT(out, StrEq("0\nstdout\n"));
419 EXPECT_THAT(err, StrEq("stderr\n"));
420 return;
421 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700422 // Then run dropping root.
423 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700424 CommandOptions::WithTimeout(1).DropRoot().Build()));
425 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700426 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700427}
428
429TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
430 if (!IsUserBuild()) {
431 // Emulates user build if necessarily.
432 SetBuildType("user");
433 }
434
435 DropRoot();
436
Felipe Leme7447d7c2016-11-03 18:12:22 -0700437 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700438
439 // We don't know the exact path of su, so we just check for the 'root ...' commands
440 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700441 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700442 EXPECT_THAT(err, IsEmpty());
443}
444
Felipe Lemecef02982016-10-03 17:22:22 -0700445TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
446 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
447 EXPECT_THAT(out,
448 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
449 EXPECT_THAT(err, IsEmpty());
450}
451
452TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
453 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
454 EXPECT_THAT(err, IsEmpty());
455 // We don't know the exact duration, so we check the prefix and suffix
456 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
457 "such file or directory\n"));
458 EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n"));
459}
460
461TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700462 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700463 EXPECT_THAT(err, IsEmpty());
464 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
465}
466
467TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700468 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700469 EXPECT_THAT(err, IsEmpty());
470 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
471}
472
473TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700474 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700475 EXPECT_THAT(err, IsEmpty());
476 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
477}
478
479TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700480 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700481 EXPECT_THAT(err, IsEmpty());
482 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
483}
484
485TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
486 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700487 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700488 EXPECT_THAT(err, IsEmpty());
489 EXPECT_THAT(out, IsEmpty());
490}
491
492TEST_F(DumpstateTest, DumpFileOnDryRun) {
493 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700494 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700495 EXPECT_THAT(err, IsEmpty());
Felipe Leme7447d7c2016-11-03 18:12:22 -0700496 EXPECT_THAT(out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath +
Felipe Lemecef02982016-10-03 17:22:22 -0700497 "single-line.txt) ------\n\t(skipped on dry run)\n------"));
498 EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n"));
499 EXPECT_THAT(err, IsEmpty());
500}
501
Felipe Leme75876a22016-10-27 16:31:27 -0700502TEST_F(DumpstateTest, DumpFileUpdateProgressNoListener) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700503 SetProgress(0, 30);
Felipe Lemecef02982016-10-03 17:22:22 -0700504
Felipe Leme7447d7c2016-11-03 18:12:22 -0700505 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700506
Felipe Leme75876a22016-10-27 16:31:27 -0700507 std::string progress_message =
508 GetProgressMessageAndAssertSystemProperties(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
Felipe Lemecef02982016-10-03 17:22:22 -0700509
Felipe Leme9a523ae2016-10-20 15:10:33 -0700510 EXPECT_THAT(err, StrEq(progress_message));
Felipe Lemecef02982016-10-03 17:22:22 -0700511 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
512}
Felipe Leme75876a22016-10-27 16:31:27 -0700513
514TEST_F(DumpstateTest, DumpFileUpdateProgress) {
515 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
516 ds.listener_ = listener;
517 ds.listener_name_ = "FoxMulder";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700518 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700519
520 EXPECT_CALL(*listener, onProgressUpdated(5));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700521 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -0700522
523 std::string progress_message =
524 GetProgressMessage(ds.listener_name_, 5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
525 EXPECT_THAT(err, StrEq(progress_message));
526 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
527
528 ds.listener_.clear();
529}
530
Felipe Leme7447d7c2016-11-03 18:12:22 -0700531class DumpstateServiceTest : public DumpstateBaseTest {
Felipe Leme75876a22016-10-27 16:31:27 -0700532 public:
533 DumpstateService dss;
534};
535
536TEST_F(DumpstateServiceTest, SetListenerNoName) {
537 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
538 bool result;
539 EXPECT_TRUE(dss.setListener("", listener, &result).isOk());
540 EXPECT_FALSE(result);
541}
542
543TEST_F(DumpstateServiceTest, SetListenerNoPointer) {
544 bool result;
545 EXPECT_TRUE(dss.setListener("whatever", nullptr, &result).isOk());
546 EXPECT_FALSE(result);
547}
548
549TEST_F(DumpstateServiceTest, SetListenerTwice) {
550 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
551 bool result;
552 EXPECT_TRUE(dss.setListener("whatever", listener, &result).isOk());
553 EXPECT_TRUE(result);
554
555 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
556
557 EXPECT_TRUE(dss.setListener("whatever", listener, &result).isOk());
558 EXPECT_FALSE(result);
559}
Felipe Leme7447d7c2016-11-03 18:12:22 -0700560
561class ProgressTest : public DumpstateBaseTest {
562 public:
563 Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {
564 return Progress(max, growth_factor, path);
565 }
566
567 void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) {
568 std::string expected_content =
569 android::base::StringPrintf("%d %d\n", expected_runs, expected_average);
570 std::string actual_content;
571 ASSERT_TRUE(android::base::ReadFileToString(path, &actual_content))
572 << "could not read statsfrom" << path;
573 ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path;
574 }
575};
576
577TEST_F(ProgressTest, SimpleTest) {
578 Progress progress;
579 EXPECT_EQ(0, progress.Get());
580 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
581 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
582
583 bool max_increased = progress.Inc(1);
584 EXPECT_EQ(1, progress.Get());
585 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
586 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
587 EXPECT_FALSE(max_increased);
588
589 // Ignore negative increase.
590 max_increased = progress.Inc(-1);
591 EXPECT_EQ(1, progress.Get());
592 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
593 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
594 EXPECT_FALSE(max_increased);
595}
596
597TEST_F(ProgressTest, MaxGrowsInsideNewRange) {
598 Progress progress = GetInstance(10, 1.2); // 20% growth factor
599 EXPECT_EQ(0, progress.Get());
600 EXPECT_EQ(10, progress.GetInitialMax());
601 EXPECT_EQ(10, progress.GetMax());
602
603 // No increase
604 bool max_increased = progress.Inc(10);
605 EXPECT_EQ(10, progress.Get());
606 EXPECT_EQ(10, progress.GetMax());
607 EXPECT_FALSE(max_increased);
608
609 // Increase, with new value < max*20%
610 max_increased = progress.Inc(1);
611 EXPECT_EQ(11, progress.Get());
612 EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13
613 EXPECT_TRUE(max_increased);
614}
615
616TEST_F(ProgressTest, MaxGrowsOutsideNewRange) {
617 Progress progress = GetInstance(10, 1.2); // 20% growth factor
618 EXPECT_EQ(0, progress.Get());
619 EXPECT_EQ(10, progress.GetInitialMax());
620 EXPECT_EQ(10, progress.GetMax());
621
622 // No increase
623 bool max_increased = progress.Inc(10);
624 EXPECT_EQ(10, progress.Get());
625 EXPECT_EQ(10, progress.GetMax());
626 EXPECT_FALSE(max_increased);
627
628 // Increase, with new value > max*20%
629 max_increased = progress.Inc(5);
630 EXPECT_EQ(15, progress.Get());
631 EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18
632 EXPECT_TRUE(max_increased);
633}
634
635TEST_F(ProgressTest, InvalidPath) {
636 Progress progress("/devil/null");
637 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
638}
639
640TEST_F(ProgressTest, EmptyFile) {
641 Progress progress(CopyTextFileFixture("empty-file.txt"));
642 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
643}
644
645TEST_F(ProgressTest, InvalidLine1stEntryNAN) {
646 Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt"));
647 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
648}
649
650TEST_F(ProgressTest, InvalidLine2ndEntryNAN) {
651 Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt"));
652 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
653}
654
655TEST_F(ProgressTest, InvalidLineBothNAN) {
656 Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt"));
657 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
658}
659
660TEST_F(ProgressTest, InvalidLine1stEntryNegative) {
661 Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt"));
662 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
663}
664
665TEST_F(ProgressTest, InvalidLine2ndEntryNegative) {
666 Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt"));
667 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
668}
669
670TEST_F(ProgressTest, InvalidLine1stEntryTooBig) {
671 Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt"));
672 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
673}
674
675TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) {
676 Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt"));
677 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
678}
679
680// Tests stats are properly saved when the file does not exists.
681TEST_F(ProgressTest, FirstTime) {
682 std::string path = kTestDataPath + "FirstTime.txt";
683 android::base::RemoveFileIfExists(path);
684
685 Progress run1(path);
686 EXPECT_EQ(0, run1.Get());
687 EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax());
688 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
689
690 bool max_increased = run1.Inc(20);
691 EXPECT_EQ(20, run1.Get());
692 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
693 EXPECT_FALSE(max_increased);
694
695 run1.Save();
696 AssertStats(path, 1, 20);
697}
698
699// Tests what happens when the persistent settings contains the average duration of 1 run.
700// Data on file is 1 run and 109 average.
701TEST_F(ProgressTest, SecondTime) {
702 std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt");
703
704 Progress run1 = GetInstance(-42, 1.2, path);
705 EXPECT_EQ(0, run1.Get());
706 EXPECT_EQ(10, run1.GetInitialMax());
707 EXPECT_EQ(10, run1.GetMax());
708
709 bool max_increased = run1.Inc(20);
710 EXPECT_EQ(20, run1.Get());
711 EXPECT_EQ(24, run1.GetMax());
712 EXPECT_TRUE(max_increased);
713
714 // Average now is 2 runs and (10 + 20)/ 2 = 15
715 run1.Save();
716 AssertStats(path, 2, 15);
717
718 Progress run2 = GetInstance(-42, 1.2, path);
719 EXPECT_EQ(0, run2.Get());
720 EXPECT_EQ(15, run2.GetInitialMax());
721 EXPECT_EQ(15, run2.GetMax());
722
723 max_increased = run2.Inc(25);
724 EXPECT_EQ(25, run2.Get());
725 EXPECT_EQ(30, run2.GetMax());
726 EXPECT_TRUE(max_increased);
727
728 // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18
729 run2.Save();
730 AssertStats(path, 3, 18);
731
732 Progress run3 = GetInstance(-42, 1.2, path);
733 EXPECT_EQ(0, run3.Get());
734 EXPECT_EQ(18, run3.GetInitialMax());
735 EXPECT_EQ(18, run3.GetMax());
736
737 // Make sure average decreases as well
738 max_increased = run3.Inc(5);
739 EXPECT_EQ(5, run3.Get());
740 EXPECT_EQ(18, run3.GetMax());
741 EXPECT_FALSE(max_increased);
742
743 // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14
744 run3.Save();
745 AssertStats(path, 4, 14);
746}
747
748// Tests what happens when the persistent settings contains the average duration of 2 runs.
749// Data on file is 2 runs and 15 average.
750TEST_F(ProgressTest, ThirdTime) {
751 std::string path = CopyTextFileFixture("stats-two-runs.txt");
752 AssertStats(path, 2, 15); // Sanity check
753
754 Progress run1 = GetInstance(-42, 1.2, path);
755 EXPECT_EQ(0, run1.Get());
756 EXPECT_EQ(15, run1.GetInitialMax());
757 EXPECT_EQ(15, run1.GetMax());
758
759 bool max_increased = run1.Inc(20);
760 EXPECT_EQ(20, run1.Get());
761 EXPECT_EQ(24, run1.GetMax());
762 EXPECT_TRUE(max_increased);
763
764 // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16
765 run1.Save();
766 AssertStats(path, 3, 16);
767}
768
769// TODO: RunCommandAsRootNonUserBuild must be the last test because it drops root, which could cause
770// other tests to fail if they're relyin on the process running as root.
771// For now this is fine, but eventually it might need to be moved to its own test case / process.
772TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
773 if (IsUserBuild()) {
774 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
775 return;
776 }
777
778 DropRoot();
779
780 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
781 CommandOptions::WithTimeout(1).AsRoot().Build()));
782
783 EXPECT_THAT(out, StrEq("0\nstdout\n"));
784 EXPECT_THAT(err, StrEq("stderr\n"));
785}