blob: 782230ac01e5b42562ddd1e4ea455b751319bba4 [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
17#include "dumpstate.h"
18
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21
22#include <libgen.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070023#include <signal.h>
24#include <sys/types.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070025#include <unistd.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070026#include <thread>
Felipe Leme4c2d6632016-09-28 14:32:00 -070027
28#include <android-base/file.h>
Felipe Lemed80e6b62016-10-03 13:08:14 -070029#include <android-base/properties.h>
30#include <android-base/stringprintf.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070031#include <android-base/strings.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070032
Felipe Lemed80e6b62016-10-03 13:08:14 -070033#define LOG_TAG "dumpstate"
34#include <cutils/log.h>
35
Felipe Leme4c2d6632016-09-28 14:32:00 -070036using ::testing::EndsWith;
37using ::testing::IsEmpty;
38using ::testing::StrEq;
39using ::testing::StartsWith;
40using ::testing::Test;
41using ::testing::internal::CaptureStderr;
42using ::testing::internal::CaptureStdout;
43using ::testing::internal::GetCapturedStderr;
44using ::testing::internal::GetCapturedStdout;
45
46// Not used on test cases yet...
47void dumpstate_board(void) {
48}
49
50class DumpstateTest : public Test {
51 public:
52 void SetUp() {
53 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -070054 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
55 ds.updateProgress_ = false;
Felipe Leme4c2d6632016-09-28 14:32:00 -070056 }
57
58 // Runs a command and capture `stdout` and `stderr`.
59 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
60 const CommandOptions& options = CommandOptions::DEFAULT) {
61 CaptureStdout();
62 CaptureStderr();
Felipe Lemefd8affa2016-09-30 17:38:57 -070063 int status = ds.RunCommand(title, fullCommand, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -070064 out = GetCapturedStdout();
65 err = GetCapturedStderr();
66 return status;
67 }
68
Felipe Lemecef02982016-10-03 17:22:22 -070069 // Dumps a file and capture `stdout` and `stderr`.
70 int DumpFile(const std::string& title, const std::string& path) {
71 CaptureStdout();
72 CaptureStderr();
73 int status = ds.DumpFile(title, path);
74 out = GetCapturedStdout();
75 err = GetCapturedStderr();
76 return status;
77 }
78
Felipe Lemed80e6b62016-10-03 13:08:14 -070079 void SetDryRun(bool dryRun) {
80 ALOGD("Setting dryRun_ to %s\n", dryRun ? "true" : "false");
81 ds.dryRun_ = dryRun;
82 }
83
84 void SetBuildType(const std::string& buildType) {
85 ALOGD("Setting buildType_ to '%s'\n", buildType.c_str());
86 ds.buildType_ = buildType;
87 }
88
89 bool IsUserBuild() {
90 return "user" == android::base::GetProperty("ro.build.type", "(unknown)");
91 }
92
93 void DropRoot() {
94 drop_root_user();
95 uid_t uid = getuid();
96 ASSERT_EQ(2000, (int)uid);
97 }
98
99 // TODO: remove when progress is set by Binder callbacks.
100 void AssertSystemProperty(const std::string& key, const std::string& expectedValue) {
101 std::string actualValue = android::base::GetProperty(key, "not set");
102 EXPECT_THAT(expectedValue, StrEq(actualValue)) << "invalid value for property " << key;
103 }
104
105 std::string GetProgressMessage(int progress, int weightTotal, int oldWeightTotal = 0) {
106 EXPECT_EQ(progress, ds.progress_) << "invalid progress";
107 EXPECT_EQ(weightTotal, ds.weightTotal_) << "invalid weightTotal";
108
109 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.progress", getpid()),
110 std::to_string(progress));
111
112 bool maxIncreased = oldWeightTotal > 0;
113
114 std::string adjustmentMessage = "";
115 if (maxIncreased) {
116 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.max", getpid()),
117 std::to_string(weightTotal));
118 adjustmentMessage = android::base::StringPrintf(
119 "Adjusting total weight from %d to %d\n", oldWeightTotal, weightTotal);
120 }
121
122 return android::base::StringPrintf("%sSetting progress (dumpstate.%d.progress): %d/%d\n",
123 adjustmentMessage.c_str(), getpid(), progress,
124 weightTotal);
125 }
126
Felipe Leme4c2d6632016-09-28 14:32:00 -0700127 // `stdout` and `stderr` from the last command ran.
128 std::string out, err;
129
130 std::string testPath = dirname(android::base::GetExecutablePath().c_str());
Felipe Lemecef02982016-10-03 17:22:22 -0700131 std::string fixturesPath = testPath + "/../dumpstate_test_fixture/";
132 std::string testDataPath = fixturesPath + "/testdata/";
133 std::string simpleCommand = fixturesPath + "dumpstate_test_fixture";
Felipe Lemefd8affa2016-09-30 17:38:57 -0700134 std::string echoCommand = "/system/bin/echo";
135
136 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700137};
138
139TEST_F(DumpstateTest, RunCommandNoArgs) {
140 EXPECT_EQ(-1, RunCommand("", {}));
141}
142
143TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Lemefd8affa2016-09-30 17:38:57 -0700144 EXPECT_EQ(0, RunCommand("", {simpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700145 EXPECT_THAT(out, StrEq("stdout\n"));
146 EXPECT_THAT(err, StrEq("stderr\n"));
147}
148
149TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Lemefd8affa2016-09-30 17:38:57 -0700150 EXPECT_EQ(0, RunCommand("I AM GROOT", {simpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700151 EXPECT_THAT(err, StrEq("stderr\n"));
152 // We don't know the exact duration, so we check the prefix and suffix
Felipe Lemefd8affa2016-09-30 17:38:57 -0700153 EXPECT_THAT(out,
154 StartsWith("------ I AM GROOT (" + simpleCommand + ") ------\nstdout\n------"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700155 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
156}
157
Felipe Lemefd8affa2016-09-30 17:38:57 -0700158TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700159 EXPECT_EQ(
Felipe Lemefd8affa2016-09-30 17:38:57 -0700160 0, RunCommand("", {simpleCommand},
161 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
162 EXPECT_THAT(out, StrEq("stdout\n"));
163 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
164}
165
166TEST_F(DumpstateTest, RunCommandRedirectStderr) {
167 EXPECT_EQ(0, RunCommand("", {simpleCommand},
168 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700169 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700170 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700171}
172
173TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Lemefd8affa2016-09-30 17:38:57 -0700174 EXPECT_EQ(0, RunCommand("", {echoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700175 EXPECT_THAT(err, IsEmpty());
176 EXPECT_THAT(out, StrEq("one\n"));
177}
178
Felipe Lemefd8affa2016-09-30 17:38:57 -0700179TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
180 EXPECT_EQ(0, RunCommand("", {echoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700181 EXPECT_THAT(err, IsEmpty());
182 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
183}
184
185TEST_F(DumpstateTest, RunCommandDryRun) {
186 SetDryRun(true);
Felipe Lemefd8affa2016-09-30 17:38:57 -0700187 EXPECT_EQ(0, RunCommand("I AM GROOT", {simpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700188 // 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, StartsWith("------ I AM GROOT (" + simpleCommand +
Felipe Leme4c2d6632016-09-28 14:32:00 -0700190 ") ------\n\t(skipped on dry run)\n------"));
191 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
192 EXPECT_THAT(err, IsEmpty());
193}
194
195TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
196 SetDryRun(true);
Felipe Lemefd8affa2016-09-30 17:38:57 -0700197 EXPECT_EQ(0, RunCommand("", {simpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700198 EXPECT_THAT(out, IsEmpty());
199 EXPECT_THAT(err, IsEmpty());
200}
201
202TEST_F(DumpstateTest, RunCommandDryRunAlways) {
203 SetDryRun(true);
Felipe Lemefd8affa2016-09-30 17:38:57 -0700204 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700205 EXPECT_THAT(out, StrEq("stdout\n"));
206 EXPECT_THAT(err, StrEq("stderr\n"));
207}
208
Felipe Lemefd8affa2016-09-30 17:38:57 -0700209TEST_F(DumpstateTest, RunCommandNotFound) {
210 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
211 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
212 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
213}
214
215TEST_F(DumpstateTest, RunCommandFails) {
216 EXPECT_EQ(42, RunCommand("", {simpleCommand, "--exit", "42"}));
217 EXPECT_THAT(
218 out, StrEq("stdout\n*** command '" + simpleCommand + " --exit 42' failed: exit code 42\n"));
219 EXPECT_THAT(
220 err, StrEq("stderr\n*** command '" + simpleCommand + " --exit 42' failed: exit code 42\n"));
221}
222
223TEST_F(DumpstateTest, RunCommandCrashes) {
224 EXPECT_NE(0, RunCommand("", {simpleCommand, "--crash"}));
225 // We don't know the exit code, so check just the prefix.
226 EXPECT_THAT(
227 out, StartsWith("stdout\n*** command '" + simpleCommand + " --crash' failed: exit code"));
228 EXPECT_THAT(
229 err, StartsWith("stderr\n*** command '" + simpleCommand + " --crash' failed: exit code"));
230}
231
232TEST_F(DumpstateTest, RunCommandTimesout) {
233 EXPECT_EQ(-1, RunCommand("", {simpleCommand, "--sleep", "2"},
234 CommandOptions::WithTimeout(1).Build()));
235 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + simpleCommand +
236 " --sleep 2' timed out after 1"));
237 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + simpleCommand +
238 " --sleep 2' timed out after 1"));
239}
240
241TEST_F(DumpstateTest, RunCommandIsKilled) {
242 CaptureStdout();
243 CaptureStderr();
244
245 std::thread t([=]() {
246 EXPECT_EQ(SIGTERM, ds.RunCommand("", {simpleCommand, "--pid", "--sleep", "20"},
247 CommandOptions::WithTimeout(100).Always().Build()));
248 });
249
250 // Capture pid and pre-sleep output.
251 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
252 std::string err = GetCapturedStderr();
253 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
254
255 std::string out = GetCapturedStdout();
256 std::vector<std::string> lines = android::base::Split(out, "\n");
257 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
258
259 int pid = atoi(lines[0].c_str());
260 EXPECT_THAT(lines[1], StrEq("stdout line1"));
261 EXPECT_THAT(lines[2], IsEmpty()); // \n
262
263 // Then kill the process.
264 CaptureStdout();
265 CaptureStderr();
266 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
267 t.join();
268
269 // Finally, check output after murder.
270 out = GetCapturedStdout();
271 err = GetCapturedStderr();
272
273 EXPECT_THAT(out, StrEq("*** command '" + simpleCommand +
274 " --pid --sleep 20' failed: killed by signal 15\n"));
275 EXPECT_THAT(err, StrEq("*** command '" + simpleCommand +
276 " --pid --sleep 20' failed: killed by signal 15\n"));
277}
278
Felipe Lemed80e6b62016-10-03 13:08:14 -0700279TEST_F(DumpstateTest, RunCommandProgress) {
280 ds.updateProgress_ = true;
Felipe Lemecef02982016-10-03 17:22:22 -0700281 ds.progress_ = 0;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700282 ds.weightTotal_ = 30;
283
284 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(20).Build()));
285 std::string progressMessage = GetProgressMessage(20, 30);
286 EXPECT_THAT(out, StrEq("stdout\n"));
287 EXPECT_THAT(err, StrEq("stderr\n" + progressMessage));
288
289 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(10).Build()));
290 progressMessage = GetProgressMessage(30, 30);
291 EXPECT_THAT(out, StrEq("stdout\n"));
292 EXPECT_THAT(err, StrEq("stderr\n" + progressMessage));
293
294 // Run a command that will increase maximum timeout.
295 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(1).Build()));
296 progressMessage = GetProgressMessage(31, 36, 30); // 20% increase
297 EXPECT_THAT(out, StrEq("stdout\n"));
298 EXPECT_THAT(err, StrEq("stderr\n" + progressMessage));
299
300 // Make sure command ran while in dryRun is counted.
301 SetDryRun(true);
302 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(4).Build()));
303 progressMessage = GetProgressMessage(35, 36);
304 EXPECT_THAT(out, IsEmpty());
305 EXPECT_THAT(err, StrEq(progressMessage));
306}
307
308TEST_F(DumpstateTest, RunCommandDropRoot) {
309 // First check root case - only available when running with 'adb root'.
310 uid_t uid = getuid();
311 if (uid == 0) {
312 EXPECT_EQ(0, RunCommand("", {simpleCommand, "--uid"}));
313 EXPECT_THAT(out, StrEq("0\nstdout\n"));
314 EXPECT_THAT(err, StrEq("stderr\n"));
315 return;
316 }
317 // Then drop root.
318
319 EXPECT_EQ(0, RunCommand("", {simpleCommand, "--uid"},
320 CommandOptions::WithTimeout(1).DropRoot().Build()));
321 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
322 EXPECT_THAT(err, StrEq("stderr\n"));
323}
324
325TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
326 if (!IsUserBuild()) {
327 // Emulates user build if necessarily.
328 SetBuildType("user");
329 }
330
331 DropRoot();
332
333 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
334
335 // We don't know the exact path of su, so we just check for the 'root ...' commands
336 EXPECT_THAT(out, StartsWith("Skipping"));
337 EXPECT_THAT(out, EndsWith("root " + simpleCommand + "' on user build.\n"));
338 EXPECT_THAT(err, IsEmpty());
339}
340
341TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
342 if (IsUserBuild()) {
343 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
344 return;
345 }
346
347 DropRoot();
348
349 EXPECT_EQ(0, RunCommand("", {simpleCommand, "--uid"},
350 CommandOptions::WithTimeout(1).AsRoot().Build()));
351
352 EXPECT_THAT(out, StrEq("0\nstdout\n"));
353 EXPECT_THAT(err, StrEq("stderr\n"));
354}
Felipe Leme4c2d6632016-09-28 14:32:00 -0700355
Felipe Lemecef02982016-10-03 17:22:22 -0700356TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
357 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
358 EXPECT_THAT(out,
359 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
360 EXPECT_THAT(err, IsEmpty());
361}
362
363TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
364 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
365 EXPECT_THAT(err, IsEmpty());
366 // We don't know the exact duration, so we check the prefix and suffix
367 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
368 "such file or directory\n"));
369 EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n"));
370}
371
372TEST_F(DumpstateTest, DumpFileSingleLine) {
373 EXPECT_EQ(0, DumpFile("", testDataPath + "single-line.txt"));
374 EXPECT_THAT(err, IsEmpty());
375 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
376}
377
378TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
379 EXPECT_EQ(0, DumpFile("", testDataPath + "single-line-with-newline.txt"));
380 EXPECT_THAT(err, IsEmpty());
381 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
382}
383
384TEST_F(DumpstateTest, DumpFileMultipleLines) {
385 EXPECT_EQ(0, DumpFile("", testDataPath + "multiple-lines.txt"));
386 EXPECT_THAT(err, IsEmpty());
387 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
388}
389
390TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
391 EXPECT_EQ(0, DumpFile("", testDataPath + "multiple-lines-with-newline.txt"));
392 EXPECT_THAT(err, IsEmpty());
393 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
394}
395
396TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
397 SetDryRun(true);
398 EXPECT_EQ(0, DumpFile("", testDataPath + "single-line.txt"));
399 EXPECT_THAT(err, IsEmpty());
400 EXPECT_THAT(out, IsEmpty());
401}
402
403TEST_F(DumpstateTest, DumpFileOnDryRun) {
404 SetDryRun(true);
405 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", testDataPath + "single-line.txt"));
406 EXPECT_THAT(err, IsEmpty());
407 EXPECT_THAT(out, StartsWith("------ Might as well dump. Dump! (" + testDataPath +
408 "single-line.txt) ------\n\t(skipped on dry run)\n------"));
409 EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n"));
410 EXPECT_THAT(err, IsEmpty());
411}
412
413TEST_F(DumpstateTest, DumpFileUpdateProgress) {
414 ds.updateProgress_ = true;
415 ds.progress_ = 0;
416 ds.weightTotal_ = 30;
417
418 EXPECT_EQ(0, DumpFile("", testDataPath + "single-line.txt"));
419
420 std::string progressMessage = GetProgressMessage(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
421
422 EXPECT_THAT(err, StrEq(progressMessage));
423 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
424}