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)")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 55 | ds.update_progress_ = false; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Runs a command and capture `stdout` and `stderr`. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 59 | int RunCommand(const std::string& title, const std::vector<std::string>& full_command, |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 60 | const CommandOptions& options = CommandOptions::DEFAULT) { |
| 61 | CaptureStdout(); |
| 62 | CaptureStderr(); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 63 | int status = ds.RunCommand(title, full_command, 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 | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 69 | // 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 Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 79 | void SetDryRun(bool dry_run) { |
| 80 | ALOGD("Setting dry_run_ to %s\n", dry_run ? "true" : "false"); |
| 81 | ds.dry_run_ = dry_run; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 84 | void SetBuildType(const std::string& build_type) { |
| 85 | ALOGD("Setting build_type_ to '%s'\n", build_type.c_str()); |
| 86 | ds.build_type_ = build_type; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 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. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 100 | void AssertSystemProperty(const std::string& key, const std::string& expected_value) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 101 | std::string actualValue = android::base::GetProperty(key, "not set"); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 102 | EXPECT_THAT(expected_value, StrEq(actualValue)) << "invalid value for property " << key; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 105 | std::string GetProgressMessage(int progress, int weigh_total, int old_weigh_total = 0) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 106 | EXPECT_EQ(progress, ds.progress_) << "invalid progress"; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 107 | EXPECT_EQ(weigh_total, ds.weight_total_) << "invalid weigh_total"; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 108 | |
| 109 | AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.progress", getpid()), |
| 110 | std::to_string(progress)); |
| 111 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 112 | bool max_increased = old_weigh_total > 0; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 113 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 114 | std::string adjustment_message = ""; |
| 115 | if (max_increased) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 116 | AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.max", getpid()), |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 117 | std::to_string(weigh_total)); |
| 118 | adjustment_message = android::base::StringPrintf( |
| 119 | "Adjusting total weight from %d to %d\n", old_weigh_total, weigh_total); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return android::base::StringPrintf("%sSetting progress (dumpstate.%d.progress): %d/%d\n", |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 123 | adjustment_message.c_str(), getpid(), progress, |
| 124 | weigh_total); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 127 | // `stdout` and `stderr` from the last command ran. |
| 128 | std::string out, err; |
| 129 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 130 | std::string test_path = dirname(android::base::GetExecutablePath().c_str()); |
| 131 | std::string fixtures_path = test_path + "/../dumpstate_test_fixture/"; |
| 132 | std::string test_data_path = fixtures_path + "/testdata/"; |
| 133 | std::string simple_command = fixtures_path + "dumpstate_test_fixture"; |
| 134 | std::string echo_command = "/system/bin/echo"; |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 135 | |
| 136 | Dumpstate& ds = Dumpstate::GetInstance(); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | TEST_F(DumpstateTest, RunCommandNoArgs) { |
| 140 | EXPECT_EQ(-1, RunCommand("", {})); |
| 141 | } |
| 142 | |
| 143 | TEST_F(DumpstateTest, RunCommandNoTitle) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 144 | EXPECT_EQ(0, RunCommand("", {simple_command})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 145 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 146 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 147 | } |
| 148 | |
| 149 | TEST_F(DumpstateTest, RunCommandWithTitle) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 150 | EXPECT_EQ(0, RunCommand("I AM GROOT", {simple_command})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 151 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 152 | // 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] | 153 | EXPECT_THAT(out, |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 154 | StartsWith("------ I AM GROOT (" + simple_command + ") ------\nstdout\n------")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 155 | EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n")); |
| 156 | } |
| 157 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 158 | TEST_F(DumpstateTest, RunCommandWithLoggingMessage) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 159 | EXPECT_EQ( |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 160 | 0, RunCommand("", {simple_command}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 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 | |
| 166 | TEST_F(DumpstateTest, RunCommandRedirectStderr) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 167 | EXPECT_EQ(0, RunCommand("", {simple_command}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 168 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 169 | EXPECT_THAT(out, IsEmpty()); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 170 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | TEST_F(DumpstateTest, RunCommandWithOneArg) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 174 | EXPECT_EQ(0, RunCommand("", {echo_command, "one"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 175 | EXPECT_THAT(err, IsEmpty()); |
| 176 | EXPECT_THAT(out, StrEq("one\n")); |
| 177 | } |
| 178 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 179 | TEST_F(DumpstateTest, RunCommandWithMultipleArgs) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 180 | EXPECT_EQ(0, RunCommand("", {echo_command, "one", "is", "the", "loniest", "number"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 181 | EXPECT_THAT(err, IsEmpty()); |
| 182 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); |
| 183 | } |
| 184 | |
| 185 | TEST_F(DumpstateTest, RunCommandDryRun) { |
| 186 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 187 | EXPECT_EQ(0, RunCommand("I AM GROOT", {simple_command})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 188 | // We don't know the exact duration, so we check the prefix and suffix |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 189 | EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + simple_command + |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 190 | ") ------\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 | |
| 195 | TEST_F(DumpstateTest, RunCommandDryRunNoTitle) { |
| 196 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 197 | EXPECT_EQ(0, RunCommand("", {simple_command})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 198 | EXPECT_THAT(out, IsEmpty()); |
| 199 | EXPECT_THAT(err, IsEmpty()); |
| 200 | } |
| 201 | |
| 202 | TEST_F(DumpstateTest, RunCommandDryRunAlways) { |
| 203 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 204 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Always().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 205 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 206 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 207 | } |
| 208 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 209 | TEST_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 | |
| 215 | TEST_F(DumpstateTest, RunCommandFails) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 216 | EXPECT_EQ(42, RunCommand("", {simple_command, "--exit", "42"})); |
| 217 | EXPECT_THAT(out, StrEq("stdout\n*** command '" + simple_command + |
| 218 | " --exit 42' failed: exit code 42\n")); |
| 219 | EXPECT_THAT(err, StrEq("stderr\n*** command '" + simple_command + |
| 220 | " --exit 42' failed: exit code 42\n")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | TEST_F(DumpstateTest, RunCommandCrashes) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 224 | EXPECT_NE(0, RunCommand("", {simple_command, "--crash"})); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 225 | // We don't know the exit code, so check just the prefix. |
| 226 | EXPECT_THAT( |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 227 | out, StartsWith("stdout\n*** command '" + simple_command + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 228 | EXPECT_THAT( |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 229 | err, StartsWith("stderr\n*** command '" + simple_command + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | TEST_F(DumpstateTest, RunCommandTimesout) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 233 | EXPECT_EQ(-1, RunCommand("", {simple_command, "--sleep", "2"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 234 | CommandOptions::WithTimeout(1).Build())); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 235 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + simple_command + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 236 | " --sleep 2' timed out after 1")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 237 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + simple_command + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 238 | " --sleep 2' timed out after 1")); |
| 239 | } |
| 240 | |
| 241 | TEST_F(DumpstateTest, RunCommandIsKilled) { |
| 242 | CaptureStdout(); |
| 243 | CaptureStderr(); |
| 244 | |
| 245 | std::thread t([=]() { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 246 | EXPECT_EQ(SIGTERM, ds.RunCommand("", {simple_command, "--pid", "--sleep", "20"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 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 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 273 | EXPECT_THAT(out, StrEq("*** command '" + simple_command + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 274 | " --pid --sleep 20' failed: killed by signal 15\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 275 | EXPECT_THAT(err, StrEq("*** command '" + simple_command + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 276 | " --pid --sleep 20' failed: killed by signal 15\n")); |
| 277 | } |
| 278 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 279 | TEST_F(DumpstateTest, RunCommandProgress) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 280 | ds.update_progress_ = true; |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 281 | ds.progress_ = 0; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 282 | ds.weight_total_ = 30; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 283 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 284 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(20).Build())); |
| 285 | std::string progress_message = GetProgressMessage(20, 30); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 286 | EXPECT_THAT(out, StrEq("stdout\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 287 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 288 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 289 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Build())); |
| 290 | progress_message = GetProgressMessage(30, 30); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 291 | EXPECT_THAT(out, StrEq("stdout\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 292 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 293 | |
| 294 | // Run a command that will increase maximum timeout. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 295 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).Build())); |
| 296 | progress_message = GetProgressMessage(31, 36, 30); // 20% increase |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 297 | EXPECT_THAT(out, StrEq("stdout\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 298 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 299 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 300 | // Make sure command ran while in dry_run is counted. |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 301 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 302 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(4).Build())); |
| 303 | progress_message = GetProgressMessage(35, 36); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 304 | EXPECT_THAT(out, IsEmpty()); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 305 | EXPECT_THAT(err, StrEq(progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | TEST_F(DumpstateTest, RunCommandDropRoot) { |
| 309 | // First check root case - only available when running with 'adb root'. |
| 310 | uid_t uid = getuid(); |
| 311 | if (uid == 0) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 312 | EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"})); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 313 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 314 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 315 | return; |
| 316 | } |
| 317 | // Then drop root. |
| 318 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 319 | EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"}, |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 320 | CommandOptions::WithTimeout(1).DropRoot().Build())); |
| 321 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
Felipe Leme | 26c4157 | 2016-10-06 14:34:43 -0700 | [diff] [blame] | 322 | EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n")); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | TEST_F(DumpstateTest, RunCommandAsRootUserBuild) { |
| 326 | if (!IsUserBuild()) { |
| 327 | // Emulates user build if necessarily. |
| 328 | SetBuildType("user"); |
| 329 | } |
| 330 | |
| 331 | DropRoot(); |
| 332 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 333 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).AsRoot().Build())); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 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")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 337 | EXPECT_THAT(out, EndsWith("root " + simple_command + "' on user build.\n")); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 338 | EXPECT_THAT(err, IsEmpty()); |
| 339 | } |
| 340 | |
| 341 | TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) { |
| 342 | if (IsUserBuild()) { |
| 343 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | DropRoot(); |
| 348 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 349 | EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"}, |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 350 | CommandOptions::WithTimeout(1).AsRoot().Build())); |
| 351 | |
| 352 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 353 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 354 | } |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 355 | |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 356 | TEST_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 | |
| 363 | TEST_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 | |
| 372 | TEST_F(DumpstateTest, DumpFileSingleLine) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 373 | EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 374 | EXPECT_THAT(err, IsEmpty()); |
| 375 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 376 | } |
| 377 | |
| 378 | TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 379 | EXPECT_EQ(0, DumpFile("", test_data_path + "single-line-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 380 | EXPECT_THAT(err, IsEmpty()); |
| 381 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); |
| 382 | } |
| 383 | |
| 384 | TEST_F(DumpstateTest, DumpFileMultipleLines) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 385 | EXPECT_EQ(0, DumpFile("", test_data_path + "multiple-lines.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 386 | EXPECT_THAT(err, IsEmpty()); |
| 387 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 388 | } |
| 389 | |
| 390 | TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 391 | EXPECT_EQ(0, DumpFile("", test_data_path + "multiple-lines-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 392 | EXPECT_THAT(err, IsEmpty()); |
| 393 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 394 | } |
| 395 | |
| 396 | TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) { |
| 397 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 398 | EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 399 | EXPECT_THAT(err, IsEmpty()); |
| 400 | EXPECT_THAT(out, IsEmpty()); |
| 401 | } |
| 402 | |
| 403 | TEST_F(DumpstateTest, DumpFileOnDryRun) { |
| 404 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 405 | EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", test_data_path + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 406 | EXPECT_THAT(err, IsEmpty()); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 407 | EXPECT_THAT(out, StartsWith("------ Might as well dump. Dump! (" + test_data_path + |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 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 | |
| 413 | TEST_F(DumpstateTest, DumpFileUpdateProgress) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 414 | ds.update_progress_ = true; |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 415 | ds.progress_ = 0; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 416 | ds.weight_total_ = 30; |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 417 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 418 | EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 419 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 420 | std::string progress_message = GetProgressMessage(5, 30); // TODO: unhardcode WEIGHT_FILE (5)? |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 421 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame^] | 422 | EXPECT_THAT(err, StrEq(progress_message)); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 423 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 424 | } |