Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 23 | #include <signal.h> |
| 24 | #include <sys/types.h> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 26 | #include <thread> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 27 | |
| 28 | #include <android-base/file.h> |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame^] | 29 | #include <android-base/properties.h> |
| 30 | #include <android-base/stringprintf.h> |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 31 | #include <android-base/strings.h> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 32 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame^] | 33 | #define LOG_TAG "dumpstate" |
| 34 | #include <cutils/log.h> |
| 35 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 36 | using ::testing::EndsWith; |
| 37 | using ::testing::IsEmpty; |
| 38 | using ::testing::StrEq; |
| 39 | using ::testing::StartsWith; |
| 40 | using ::testing::Test; |
| 41 | using ::testing::internal::CaptureStderr; |
| 42 | using ::testing::internal::CaptureStdout; |
| 43 | using ::testing::internal::GetCapturedStderr; |
| 44 | using ::testing::internal::GetCapturedStdout; |
| 45 | |
| 46 | // Not used on test cases yet... |
| 47 | void dumpstate_board(void) { |
| 48 | } |
| 49 | |
| 50 | class DumpstateTest : public Test { |
| 51 | public: |
| 52 | void SetUp() { |
| 53 | SetDryRun(false); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame^] | 54 | SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)")); |
| 55 | ds.updateProgress_ = false; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 56 | } |
| 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 Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 63 | int status = ds.RunCommand(title, fullCommand, options); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 64 | out = GetCapturedStdout(); |
| 65 | err = GetCapturedStderr(); |
| 66 | return status; |
| 67 | } |
| 68 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame^] | 69 | 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 Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 117 | // `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 Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 121 | std::string simpleCommand = testPath + "/../dumpstate_test_fixture/dumpstate_test_fixture"; |
| 122 | std::string echoCommand = "/system/bin/echo"; |
| 123 | |
| 124 | Dumpstate& ds = Dumpstate::GetInstance(); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | TEST_F(DumpstateTest, RunCommandNoArgs) { |
| 128 | EXPECT_EQ(-1, RunCommand("", {})); |
| 129 | } |
| 130 | |
| 131 | TEST_F(DumpstateTest, RunCommandNoTitle) { |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 132 | EXPECT_EQ(0, RunCommand("", {simpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 133 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 134 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 135 | } |
| 136 | |
| 137 | TEST_F(DumpstateTest, RunCommandWithTitle) { |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 138 | EXPECT_EQ(0, RunCommand("I AM GROOT", {simpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 139 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 140 | // We don't know the exact duration, so we check the prefix and suffix |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 141 | EXPECT_THAT(out, |
| 142 | StartsWith("------ I AM GROOT (" + simpleCommand + ") ------\nstdout\n------")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 143 | EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n")); |
| 144 | } |
| 145 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 146 | TEST_F(DumpstateTest, RunCommandWithLoggingMessage) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 147 | EXPECT_EQ( |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 148 | 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 | |
| 154 | TEST_F(DumpstateTest, RunCommandRedirectStderr) { |
| 155 | EXPECT_EQ(0, RunCommand("", {simpleCommand}, |
| 156 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 157 | EXPECT_THAT(out, IsEmpty()); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 158 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | TEST_F(DumpstateTest, RunCommandWithOneArg) { |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 162 | EXPECT_EQ(0, RunCommand("", {echoCommand, "one"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 163 | EXPECT_THAT(err, IsEmpty()); |
| 164 | EXPECT_THAT(out, StrEq("one\n")); |
| 165 | } |
| 166 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 167 | TEST_F(DumpstateTest, RunCommandWithMultipleArgs) { |
| 168 | EXPECT_EQ(0, RunCommand("", {echoCommand, "one", "is", "the", "loniest", "number"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 169 | EXPECT_THAT(err, IsEmpty()); |
| 170 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); |
| 171 | } |
| 172 | |
| 173 | TEST_F(DumpstateTest, RunCommandDryRun) { |
| 174 | SetDryRun(true); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 175 | EXPECT_EQ(0, RunCommand("I AM GROOT", {simpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 176 | // We don't know the exact duration, so we check the prefix and suffix |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 177 | EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + simpleCommand + |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 178 | ") ------\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 | |
| 183 | TEST_F(DumpstateTest, RunCommandDryRunNoTitle) { |
| 184 | SetDryRun(true); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 185 | EXPECT_EQ(0, RunCommand("", {simpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 186 | EXPECT_THAT(out, IsEmpty()); |
| 187 | EXPECT_THAT(err, IsEmpty()); |
| 188 | } |
| 189 | |
| 190 | TEST_F(DumpstateTest, RunCommandDryRunAlways) { |
| 191 | SetDryRun(true); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 192 | EXPECT_EQ(0, RunCommand("", {simpleCommand}, CommandOptions::WithTimeout(10).Always().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 193 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 194 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 195 | } |
| 196 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 197 | TEST_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 | |
| 203 | TEST_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 | |
| 211 | TEST_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 | |
| 220 | TEST_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 | |
| 229 | TEST_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 Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame^] | 267 | TEST_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 | |
| 295 | TEST_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")); |
| 309 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 310 | } |
| 311 | |
| 312 | TEST_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 | |
| 328 | TEST_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 Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 342 | |
| 343 | // TODO: test DumpFile() |