blob: 60ac6d19a5fe15c427135164c44fcd9ebdbe986f [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 Lemed80e6b62016-10-03 13:08:14 -070069 void SetDryRun(bool dryRun) {
70 ALOGD("Setting dryRun_ to %s\n", dryRun ? "true" : "false");
71 ds.dryRun_ = dryRun;
72 }
73
74 void SetBuildType(const std::string& buildType) {
75 ALOGD("Setting buildType_ to '%s'\n", buildType.c_str());
76 ds.buildType_ = buildType;
77 }
78
79 bool IsUserBuild() {
80 return "user" == android::base::GetProperty("ro.build.type", "(unknown)");
81 }
82
83 void DropRoot() {
84 drop_root_user();
85 uid_t uid = getuid();
86 ASSERT_EQ(2000, (int)uid);
87 }
88
89 // TODO: remove when progress is set by Binder callbacks.
90 void AssertSystemProperty(const std::string& key, const std::string& expectedValue) {
91 std::string actualValue = android::base::GetProperty(key, "not set");
92 EXPECT_THAT(expectedValue, StrEq(actualValue)) << "invalid value for property " << key;
93 }
94
95 std::string GetProgressMessage(int progress, int weightTotal, int oldWeightTotal = 0) {
96 EXPECT_EQ(progress, ds.progress_) << "invalid progress";
97 EXPECT_EQ(weightTotal, ds.weightTotal_) << "invalid weightTotal";
98
99 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.progress", getpid()),
100 std::to_string(progress));
101
102 bool maxIncreased = oldWeightTotal > 0;
103
104 std::string adjustmentMessage = "";
105 if (maxIncreased) {
106 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.max", getpid()),
107 std::to_string(weightTotal));
108 adjustmentMessage = android::base::StringPrintf(
109 "Adjusting total weight from %d to %d\n", oldWeightTotal, weightTotal);
110 }
111
112 return android::base::StringPrintf("%sSetting progress (dumpstate.%d.progress): %d/%d\n",
113 adjustmentMessage.c_str(), getpid(), progress,
114 weightTotal);
115 }
116
Felipe Leme4c2d6632016-09-28 14:32:00 -0700117 // `stdout` and `stderr` from the last command ran.
118 std::string out, err;
119
120 std::string testPath = dirname(android::base::GetExecutablePath().c_str());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700121 std::string simpleCommand = testPath + "/../dumpstate_test_fixture/dumpstate_test_fixture";
122 std::string echoCommand = "/system/bin/echo";
123
124 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700125};
126
127TEST_F(DumpstateTest, RunCommandNoArgs) {
128 EXPECT_EQ(-1, RunCommand("", {}));
129}
130
131TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Lemefd8affa2016-09-30 17:38:57 -0700132 EXPECT_EQ(0, RunCommand("", {simpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700133 EXPECT_THAT(out, StrEq("stdout\n"));
134 EXPECT_THAT(err, StrEq("stderr\n"));
135}
136
137TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Lemefd8affa2016-09-30 17:38:57 -0700138 EXPECT_EQ(0, RunCommand("I AM GROOT", {simpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700139 EXPECT_THAT(err, StrEq("stderr\n"));
140 // We don't know the exact duration, so we check the prefix and suffix
Felipe Lemefd8affa2016-09-30 17:38:57 -0700141 EXPECT_THAT(out,
142 StartsWith("------ I AM GROOT (" + simpleCommand + ") ------\nstdout\n------"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700143 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
144}
145
Felipe Lemefd8affa2016-09-30 17:38:57 -0700146TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700147 EXPECT_EQ(
Felipe Lemefd8affa2016-09-30 17:38:57 -0700148 0, RunCommand("", {simpleCommand},
149 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
150 EXPECT_THAT(out, StrEq("stdout\n"));
151 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
152}
153
154TEST_F(DumpstateTest, RunCommandRedirectStderr) {
155 EXPECT_EQ(0, RunCommand("", {simpleCommand},
156 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700157 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700158 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700159}
160
161TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Lemefd8affa2016-09-30 17:38:57 -0700162 EXPECT_EQ(0, RunCommand("", {echoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700163 EXPECT_THAT(err, IsEmpty());
164 EXPECT_THAT(out, StrEq("one\n"));
165}
166
Felipe Lemefd8affa2016-09-30 17:38:57 -0700167TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
168 EXPECT_EQ(0, RunCommand("", {echoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700169 EXPECT_THAT(err, IsEmpty());
170 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
171}
172
173TEST_F(DumpstateTest, RunCommandDryRun) {
174 SetDryRun(true);
Felipe Lemefd8affa2016-09-30 17:38:57 -0700175 EXPECT_EQ(0, RunCommand("I AM GROOT", {simpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700176 // We don't know the exact duration, so we check the prefix and suffix
Felipe Lemefd8affa2016-09-30 17:38:57 -0700177 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + simpleCommand +
Felipe Leme4c2d6632016-09-28 14:32:00 -0700178 ") ------\n\t(skipped on dry run)\n------"));
179 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
180 EXPECT_THAT(err, IsEmpty());
181}
182
183TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
184 SetDryRun(true);
Felipe Lemefd8affa2016-09-30 17:38:57 -0700185 EXPECT_EQ(0, RunCommand("", {simpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700186 EXPECT_THAT(out, IsEmpty());
187 EXPECT_THAT(err, IsEmpty());
188}
189
190TEST_F(DumpstateTest, RunCommandDryRunAlways) {
191 SetDryRun(true);
Felipe Lemefd8affa2016-09-30 17:38:57 -0700192 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700193 EXPECT_THAT(out, StrEq("stdout\n"));
194 EXPECT_THAT(err, StrEq("stderr\n"));
195}
196
Felipe Lemefd8affa2016-09-30 17:38:57 -0700197TEST_F(DumpstateTest, RunCommandNotFound) {
198 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
199 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
200 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
201}
202
203TEST_F(DumpstateTest, RunCommandFails) {
204 EXPECT_EQ(42, RunCommand("", {simpleCommand, "--exit", "42"}));
205 EXPECT_THAT(
206 out, StrEq("stdout\n*** command '" + simpleCommand + " --exit 42' failed: exit code 42\n"));
207 EXPECT_THAT(
208 err, StrEq("stderr\n*** command '" + simpleCommand + " --exit 42' failed: exit code 42\n"));
209}
210
211TEST_F(DumpstateTest, RunCommandCrashes) {
212 EXPECT_NE(0, RunCommand("", {simpleCommand, "--crash"}));
213 // We don't know the exit code, so check just the prefix.
214 EXPECT_THAT(
215 out, StartsWith("stdout\n*** command '" + simpleCommand + " --crash' failed: exit code"));
216 EXPECT_THAT(
217 err, StartsWith("stderr\n*** command '" + simpleCommand + " --crash' failed: exit code"));
218}
219
220TEST_F(DumpstateTest, RunCommandTimesout) {
221 EXPECT_EQ(-1, RunCommand("", {simpleCommand, "--sleep", "2"},
222 CommandOptions::WithTimeout(1).Build()));
223 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + simpleCommand +
224 " --sleep 2' timed out after 1"));
225 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + simpleCommand +
226 " --sleep 2' timed out after 1"));
227}
228
229TEST_F(DumpstateTest, RunCommandIsKilled) {
230 CaptureStdout();
231 CaptureStderr();
232
233 std::thread t([=]() {
234 EXPECT_EQ(SIGTERM, ds.RunCommand("", {simpleCommand, "--pid", "--sleep", "20"},
235 CommandOptions::WithTimeout(100).Always().Build()));
236 });
237
238 // Capture pid and pre-sleep output.
239 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
240 std::string err = GetCapturedStderr();
241 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
242
243 std::string out = GetCapturedStdout();
244 std::vector<std::string> lines = android::base::Split(out, "\n");
245 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
246
247 int pid = atoi(lines[0].c_str());
248 EXPECT_THAT(lines[1], StrEq("stdout line1"));
249 EXPECT_THAT(lines[2], IsEmpty()); // \n
250
251 // Then kill the process.
252 CaptureStdout();
253 CaptureStderr();
254 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
255 t.join();
256
257 // Finally, check output after murder.
258 out = GetCapturedStdout();
259 err = GetCapturedStderr();
260
261 EXPECT_THAT(out, StrEq("*** command '" + simpleCommand +
262 " --pid --sleep 20' failed: killed by signal 15\n"));
263 EXPECT_THAT(err, StrEq("*** command '" + simpleCommand +
264 " --pid --sleep 20' failed: killed by signal 15\n"));
265}
266
Felipe Lemed80e6b62016-10-03 13:08:14 -0700267TEST_F(DumpstateTest, RunCommandProgress) {
268 ds.updateProgress_ = true;
269 ds.weightTotal_ = 30;
270
271 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(20).Build()));
272 std::string progressMessage = GetProgressMessage(20, 30);
273 EXPECT_THAT(out, StrEq("stdout\n"));
274 EXPECT_THAT(err, StrEq("stderr\n" + progressMessage));
275
276 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(10).Build()));
277 progressMessage = GetProgressMessage(30, 30);
278 EXPECT_THAT(out, StrEq("stdout\n"));
279 EXPECT_THAT(err, StrEq("stderr\n" + progressMessage));
280
281 // Run a command that will increase maximum timeout.
282 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(1).Build()));
283 progressMessage = GetProgressMessage(31, 36, 30); // 20% increase
284 EXPECT_THAT(out, StrEq("stdout\n"));
285 EXPECT_THAT(err, StrEq("stderr\n" + progressMessage));
286
287 // Make sure command ran while in dryRun is counted.
288 SetDryRun(true);
289 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(4).Build()));
290 progressMessage = GetProgressMessage(35, 36);
291 EXPECT_THAT(out, IsEmpty());
292 EXPECT_THAT(err, StrEq(progressMessage));
293}
294
295TEST_F(DumpstateTest, RunCommandDropRoot) {
296 // First check root case - only available when running with 'adb root'.
297 uid_t uid = getuid();
298 if (uid == 0) {
299 EXPECT_EQ(0, RunCommand("", {simpleCommand, "--uid"}));
300 EXPECT_THAT(out, StrEq("0\nstdout\n"));
301 EXPECT_THAT(err, StrEq("stderr\n"));
302 return;
303 }
304 // Then drop root.
305
306 EXPECT_EQ(0, RunCommand("", {simpleCommand, "--uid"},
307 CommandOptions::WithTimeout(1).DropRoot().Build()));
308 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700309 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700310}
311
312TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
313 if (!IsUserBuild()) {
314 // Emulates user build if necessarily.
315 SetBuildType("user");
316 }
317
318 DropRoot();
319
320 EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
321
322 // We don't know the exact path of su, so we just check for the 'root ...' commands
323 EXPECT_THAT(out, StartsWith("Skipping"));
324 EXPECT_THAT(out, EndsWith("root " + simpleCommand + "' on user build.\n"));
325 EXPECT_THAT(err, IsEmpty());
326}
327
328TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
329 if (IsUserBuild()) {
330 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
331 return;
332 }
333
334 DropRoot();
335
336 EXPECT_EQ(0, RunCommand("", {simpleCommand, "--uid"},
337 CommandOptions::WithTimeout(1).AsRoot().Build()));
338
339 EXPECT_THAT(out, StrEq("0\nstdout\n"));
340 EXPECT_THAT(err, StrEq("stderr\n"));
341}
Felipe Leme4c2d6632016-09-28 14:32:00 -0700342
343// TODO: test DumpFile()