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 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 17 | #define LOG_TAG "dumpstate" |
| 18 | #include <cutils/log.h> |
| 19 | |
| 20 | #include "DumpstateService.h" |
| 21 | #include "android/os/BnDumpstate.h" |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 22 | #include "dumpstate.h" |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #include <libgen.h> |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 28 | #include <signal.h> |
| 29 | #include <sys/types.h> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 30 | #include <unistd.h> |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 31 | #include <thread> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 32 | |
| 33 | #include <android-base/file.h> |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 34 | #include <android-base/properties.h> |
| 35 | #include <android-base/stringprintf.h> |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 36 | #include <android-base/strings.h> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 37 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 38 | using namespace android; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 39 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 40 | using ::testing::EndsWith; |
| 41 | using ::testing::IsEmpty; |
| 42 | using ::testing::StrEq; |
| 43 | using ::testing::StartsWith; |
| 44 | using ::testing::Test; |
| 45 | using ::testing::internal::CaptureStderr; |
| 46 | using ::testing::internal::CaptureStdout; |
| 47 | using ::testing::internal::GetCapturedStderr; |
| 48 | using ::testing::internal::GetCapturedStdout; |
| 49 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 50 | using os::DumpstateService; |
| 51 | using os::IDumpstateListener; |
| 52 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 53 | // Not used on test cases yet... |
| 54 | void dumpstate_board(void) { |
| 55 | } |
| 56 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 57 | class 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 Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 66 | class DumpstateTest : public Test { |
| 67 | public: |
| 68 | void SetUp() { |
| 69 | SetDryRun(false); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 70 | SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 71 | ds.update_progress_ = false; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Runs a command and capture `stdout` and `stderr`. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 75 | 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] | 76 | const CommandOptions& options = CommandOptions::DEFAULT) { |
| 77 | CaptureStdout(); |
| 78 | CaptureStderr(); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 79 | int status = ds.RunCommand(title, full_command, options); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 80 | out = GetCapturedStdout(); |
| 81 | err = GetCapturedStderr(); |
| 82 | return status; |
| 83 | } |
| 84 | |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 85 | // Dumps a file and capture `stdout` and `stderr`. |
| 86 | int DumpFile(const std::string& title, const std::string& path) { |
| 87 | CaptureStdout(); |
| 88 | CaptureStderr(); |
| 89 | int status = ds.DumpFile(title, path); |
| 90 | out = GetCapturedStdout(); |
| 91 | err = GetCapturedStderr(); |
| 92 | return status; |
| 93 | } |
| 94 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 95 | void SetDryRun(bool dry_run) { |
| 96 | ALOGD("Setting dry_run_ to %s\n", dry_run ? "true" : "false"); |
| 97 | ds.dry_run_ = dry_run; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 100 | void SetBuildType(const std::string& build_type) { |
| 101 | ALOGD("Setting build_type_ to '%s'\n", build_type.c_str()); |
| 102 | ds.build_type_ = build_type; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool IsUserBuild() { |
| 106 | return "user" == android::base::GetProperty("ro.build.type", "(unknown)"); |
| 107 | } |
| 108 | |
| 109 | void DropRoot() { |
| 110 | drop_root_user(); |
| 111 | uid_t uid = getuid(); |
| 112 | ASSERT_EQ(2000, (int)uid); |
| 113 | } |
| 114 | |
| 115 | // TODO: remove when progress is set by Binder callbacks. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 116 | void AssertSystemProperty(const std::string& key, const std::string& expected_value) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 117 | std::string actualValue = android::base::GetProperty(key, "not set"); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 118 | EXPECT_THAT(expected_value, StrEq(actualValue)) << "invalid value for property " << key; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 121 | // TODO: remove when progress is set by Binder callbacks. |
| 122 | std::string GetProgressMessageAndAssertSystemProperties(int progress, int weight_total, |
| 123 | int old_weight_total = 0) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 124 | EXPECT_EQ(progress, ds.progress_) << "invalid progress"; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 125 | EXPECT_EQ(weight_total, ds.weight_total_) << "invalid weight_total"; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 126 | |
| 127 | AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.progress", getpid()), |
| 128 | std::to_string(progress)); |
| 129 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 130 | bool max_increased = old_weight_total > 0; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 131 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 132 | std::string adjustment_message = ""; |
| 133 | if (max_increased) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 134 | AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.max", getpid()), |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 135 | std::to_string(weight_total)); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 136 | adjustment_message = android::base::StringPrintf( |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 137 | "Adjusting total weight from %d to %d\n", old_weight_total, weight_total); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return android::base::StringPrintf("%sSetting progress (dumpstate.%d.progress): %d/%d\n", |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 141 | adjustment_message.c_str(), getpid(), progress, |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 142 | weight_total); |
| 143 | } |
| 144 | |
| 145 | std::string GetProgressMessage(const std::string& listener_name, int progress, int weight_total, |
| 146 | int old_weight_total = 0) { |
| 147 | EXPECT_EQ(progress, ds.progress_) << "invalid progress"; |
| 148 | EXPECT_EQ(weight_total, ds.weight_total_) << "invalid weight_total"; |
| 149 | |
| 150 | bool max_increased = old_weight_total > 0; |
| 151 | |
| 152 | std::string adjustment_message = ""; |
| 153 | if (max_increased) { |
| 154 | adjustment_message = android::base::StringPrintf( |
| 155 | "Adjusting total weight from %d to %d\n", old_weight_total, weight_total); |
| 156 | } |
| 157 | |
| 158 | return android::base::StringPrintf("%sSetting progress (%s): %d/%d\n", |
| 159 | adjustment_message.c_str(), listener_name.c_str(), |
| 160 | progress, weight_total); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 163 | // `stdout` and `stderr` from the last command ran. |
| 164 | std::string out, err; |
| 165 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 166 | std::string test_path = dirname(android::base::GetExecutablePath().c_str()); |
| 167 | std::string fixtures_path = test_path + "/../dumpstate_test_fixture/"; |
| 168 | std::string test_data_path = fixtures_path + "/testdata/"; |
| 169 | std::string simple_command = fixtures_path + "dumpstate_test_fixture"; |
| 170 | std::string echo_command = "/system/bin/echo"; |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 171 | |
| 172 | Dumpstate& ds = Dumpstate::GetInstance(); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | TEST_F(DumpstateTest, RunCommandNoArgs) { |
| 176 | EXPECT_EQ(-1, RunCommand("", {})); |
| 177 | } |
| 178 | |
| 179 | TEST_F(DumpstateTest, RunCommandNoTitle) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 180 | EXPECT_EQ(0, RunCommand("", {simple_command})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 181 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 182 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 183 | } |
| 184 | |
| 185 | TEST_F(DumpstateTest, RunCommandWithTitle) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 186 | EXPECT_EQ(0, RunCommand("I AM GROOT", {simple_command})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 187 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 188 | // 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] | 189 | EXPECT_THAT(out, |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 190 | StartsWith("------ I AM GROOT (" + simple_command + ") ------\nstdout\n------")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 191 | EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n")); |
| 192 | } |
| 193 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 194 | TEST_F(DumpstateTest, RunCommandWithLoggingMessage) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 195 | EXPECT_EQ( |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 196 | 0, RunCommand("", {simple_command}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 197 | CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build())); |
| 198 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 199 | EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n")); |
| 200 | } |
| 201 | |
| 202 | TEST_F(DumpstateTest, RunCommandRedirectStderr) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 203 | EXPECT_EQ(0, RunCommand("", {simple_command}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 204 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 205 | EXPECT_THAT(out, IsEmpty()); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 206 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_F(DumpstateTest, RunCommandWithOneArg) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 210 | EXPECT_EQ(0, RunCommand("", {echo_command, "one"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 211 | EXPECT_THAT(err, IsEmpty()); |
| 212 | EXPECT_THAT(out, StrEq("one\n")); |
| 213 | } |
| 214 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 215 | TEST_F(DumpstateTest, RunCommandWithMultipleArgs) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 216 | EXPECT_EQ(0, RunCommand("", {echo_command, "one", "is", "the", "loniest", "number"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 217 | EXPECT_THAT(err, IsEmpty()); |
| 218 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); |
| 219 | } |
| 220 | |
| 221 | TEST_F(DumpstateTest, RunCommandDryRun) { |
| 222 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 223 | EXPECT_EQ(0, RunCommand("I AM GROOT", {simple_command})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 224 | // 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] | 225 | EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + simple_command + |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 226 | ") ------\n\t(skipped on dry run)\n------")); |
| 227 | EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n")); |
| 228 | EXPECT_THAT(err, IsEmpty()); |
| 229 | } |
| 230 | |
| 231 | TEST_F(DumpstateTest, RunCommandDryRunNoTitle) { |
| 232 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 233 | EXPECT_EQ(0, RunCommand("", {simple_command})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 234 | EXPECT_THAT(out, IsEmpty()); |
| 235 | EXPECT_THAT(err, IsEmpty()); |
| 236 | } |
| 237 | |
| 238 | TEST_F(DumpstateTest, RunCommandDryRunAlways) { |
| 239 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 240 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Always().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 241 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 242 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 243 | } |
| 244 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 245 | TEST_F(DumpstateTest, RunCommandNotFound) { |
| 246 | EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"})); |
| 247 | EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code")); |
| 248 | EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed")); |
| 249 | } |
| 250 | |
| 251 | TEST_F(DumpstateTest, RunCommandFails) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 252 | EXPECT_EQ(42, RunCommand("", {simple_command, "--exit", "42"})); |
| 253 | EXPECT_THAT(out, StrEq("stdout\n*** command '" + simple_command + |
| 254 | " --exit 42' failed: exit code 42\n")); |
| 255 | EXPECT_THAT(err, StrEq("stderr\n*** command '" + simple_command + |
| 256 | " --exit 42' failed: exit code 42\n")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | TEST_F(DumpstateTest, RunCommandCrashes) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 260 | EXPECT_NE(0, RunCommand("", {simple_command, "--crash"})); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 261 | // We don't know the exit code, so check just the prefix. |
| 262 | EXPECT_THAT( |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 263 | out, StartsWith("stdout\n*** command '" + simple_command + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 264 | EXPECT_THAT( |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 265 | err, StartsWith("stderr\n*** command '" + simple_command + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | TEST_F(DumpstateTest, RunCommandTimesout) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 269 | EXPECT_EQ(-1, RunCommand("", {simple_command, "--sleep", "2"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 270 | CommandOptions::WithTimeout(1).Build())); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 271 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + simple_command + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 272 | " --sleep 2' timed out after 1")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 273 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + simple_command + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 274 | " --sleep 2' timed out after 1")); |
| 275 | } |
| 276 | |
| 277 | TEST_F(DumpstateTest, RunCommandIsKilled) { |
| 278 | CaptureStdout(); |
| 279 | CaptureStderr(); |
| 280 | |
| 281 | std::thread t([=]() { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 282 | EXPECT_EQ(SIGTERM, ds.RunCommand("", {simple_command, "--pid", "--sleep", "20"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 283 | CommandOptions::WithTimeout(100).Always().Build())); |
| 284 | }); |
| 285 | |
| 286 | // Capture pid and pre-sleep output. |
| 287 | sleep(1); // Wait a little bit to make sure pid and 1st line were printed. |
| 288 | std::string err = GetCapturedStderr(); |
| 289 | EXPECT_THAT(err, StrEq("sleeping for 20s\n")); |
| 290 | |
| 291 | std::string out = GetCapturedStdout(); |
| 292 | std::vector<std::string> lines = android::base::Split(out, "\n"); |
| 293 | ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out; |
| 294 | |
| 295 | int pid = atoi(lines[0].c_str()); |
| 296 | EXPECT_THAT(lines[1], StrEq("stdout line1")); |
| 297 | EXPECT_THAT(lines[2], IsEmpty()); // \n |
| 298 | |
| 299 | // Then kill the process. |
| 300 | CaptureStdout(); |
| 301 | CaptureStderr(); |
| 302 | ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid; |
| 303 | t.join(); |
| 304 | |
| 305 | // Finally, check output after murder. |
| 306 | out = GetCapturedStdout(); |
| 307 | err = GetCapturedStderr(); |
| 308 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 309 | EXPECT_THAT(out, StrEq("*** command '" + simple_command + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 310 | " --pid --sleep 20' failed: killed by signal 15\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 311 | EXPECT_THAT(err, StrEq("*** command '" + simple_command + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 312 | " --pid --sleep 20' failed: killed by signal 15\n")); |
| 313 | } |
| 314 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 315 | TEST_F(DumpstateTest, RunCommandProgressNoListener) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 316 | ds.update_progress_ = true; |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 317 | ds.progress_ = 0; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 318 | ds.weight_total_ = 30; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 319 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 320 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(20).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 321 | std::string progress_message = GetProgressMessageAndAssertSystemProperties(20, 30); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 322 | EXPECT_THAT(out, StrEq("stdout\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 323 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 324 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 325 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 326 | progress_message = GetProgressMessageAndAssertSystemProperties(30, 30); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 327 | EXPECT_THAT(out, StrEq("stdout\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 328 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 329 | |
| 330 | // Run a command that will increase maximum timeout. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 331 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 332 | progress_message = GetProgressMessageAndAssertSystemProperties(31, 36, 30); // 20% increase |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 333 | EXPECT_THAT(out, StrEq("stdout\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 334 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 335 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 336 | // Make sure command ran while in dry_run is counted. |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 337 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 338 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(4).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 339 | progress_message = GetProgressMessageAndAssertSystemProperties(35, 36); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 340 | EXPECT_THAT(out, IsEmpty()); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 341 | EXPECT_THAT(err, StrEq(progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 344 | TEST_F(DumpstateTest, RunCommandProgress) { |
| 345 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 346 | ds.listener_ = listener; |
| 347 | ds.listener_name_ = "FoxMulder"; |
| 348 | |
| 349 | ds.update_progress_ = true; |
| 350 | ds.progress_ = 0; |
| 351 | ds.weight_total_ = 30; |
| 352 | |
| 353 | EXPECT_CALL(*listener, onProgressUpdated(20)); |
| 354 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(20).Build())); |
| 355 | std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30); |
| 356 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 357 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 358 | |
| 359 | EXPECT_CALL(*listener, onProgressUpdated(30)); |
| 360 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Build())); |
| 361 | progress_message = GetProgressMessage(ds.listener_name_, 30, 30); |
| 362 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 363 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 364 | |
| 365 | // Run a command that will increase maximum timeout. |
| 366 | EXPECT_CALL(*listener, onProgressUpdated(31)); |
| 367 | EXPECT_CALL(*listener, onMaxProgressUpdated(36)); |
| 368 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).Build())); |
| 369 | progress_message = GetProgressMessage(ds.listener_name_, 31, 36, 30); // 20% increase |
| 370 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 371 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 372 | |
| 373 | // Make sure command ran while in dry_run is counted. |
| 374 | SetDryRun(true); |
| 375 | EXPECT_CALL(*listener, onProgressUpdated(35)); |
| 376 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(4).Build())); |
| 377 | progress_message = GetProgressMessage(ds.listener_name_, 35, 36); |
| 378 | EXPECT_THAT(out, IsEmpty()); |
| 379 | EXPECT_THAT(err, StrEq(progress_message)); |
| 380 | |
| 381 | ds.listener_.clear(); |
| 382 | } |
| 383 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 384 | TEST_F(DumpstateTest, RunCommandDropRoot) { |
| 385 | // First check root case - only available when running with 'adb root'. |
| 386 | uid_t uid = getuid(); |
| 387 | if (uid == 0) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 388 | EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"})); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 389 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 390 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 391 | return; |
| 392 | } |
| 393 | // Then drop root. |
| 394 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 395 | EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"}, |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 396 | CommandOptions::WithTimeout(1).DropRoot().Build())); |
| 397 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
Felipe Leme | 26c4157 | 2016-10-06 14:34:43 -0700 | [diff] [blame] | 398 | 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] | 399 | } |
| 400 | |
| 401 | TEST_F(DumpstateTest, RunCommandAsRootUserBuild) { |
| 402 | if (!IsUserBuild()) { |
| 403 | // Emulates user build if necessarily. |
| 404 | SetBuildType("user"); |
| 405 | } |
| 406 | |
| 407 | DropRoot(); |
| 408 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 409 | EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).AsRoot().Build())); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 410 | |
| 411 | // We don't know the exact path of su, so we just check for the 'root ...' commands |
| 412 | EXPECT_THAT(out, StartsWith("Skipping")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 413 | EXPECT_THAT(out, EndsWith("root " + simple_command + "' on user build.\n")); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 414 | EXPECT_THAT(err, IsEmpty()); |
| 415 | } |
| 416 | |
| 417 | TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) { |
| 418 | if (IsUserBuild()) { |
| 419 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | DropRoot(); |
| 424 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 425 | EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"}, |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 426 | CommandOptions::WithTimeout(1).AsRoot().Build())); |
| 427 | |
| 428 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 429 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 430 | } |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 431 | |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 432 | TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) { |
| 433 | EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist")); |
| 434 | EXPECT_THAT(out, |
| 435 | StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n")); |
| 436 | EXPECT_THAT(err, IsEmpty()); |
| 437 | } |
| 438 | |
| 439 | TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) { |
| 440 | EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist")); |
| 441 | EXPECT_THAT(err, IsEmpty()); |
| 442 | // We don't know the exact duration, so we check the prefix and suffix |
| 443 | EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No " |
| 444 | "such file or directory\n")); |
| 445 | EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n")); |
| 446 | } |
| 447 | |
| 448 | TEST_F(DumpstateTest, DumpFileSingleLine) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 449 | EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 450 | EXPECT_THAT(err, IsEmpty()); |
| 451 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 452 | } |
| 453 | |
| 454 | TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 455 | EXPECT_EQ(0, DumpFile("", test_data_path + "single-line-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 456 | EXPECT_THAT(err, IsEmpty()); |
| 457 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); |
| 458 | } |
| 459 | |
| 460 | TEST_F(DumpstateTest, DumpFileMultipleLines) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 461 | EXPECT_EQ(0, DumpFile("", test_data_path + "multiple-lines.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 462 | EXPECT_THAT(err, IsEmpty()); |
| 463 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 464 | } |
| 465 | |
| 466 | TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 467 | EXPECT_EQ(0, DumpFile("", test_data_path + "multiple-lines-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 468 | EXPECT_THAT(err, IsEmpty()); |
| 469 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 470 | } |
| 471 | |
| 472 | TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) { |
| 473 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 474 | EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 475 | EXPECT_THAT(err, IsEmpty()); |
| 476 | EXPECT_THAT(out, IsEmpty()); |
| 477 | } |
| 478 | |
| 479 | TEST_F(DumpstateTest, DumpFileOnDryRun) { |
| 480 | SetDryRun(true); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 481 | 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] | 482 | EXPECT_THAT(err, IsEmpty()); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 483 | EXPECT_THAT(out, StartsWith("------ Might as well dump. Dump! (" + test_data_path + |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 484 | "single-line.txt) ------\n\t(skipped on dry run)\n------")); |
| 485 | EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n")); |
| 486 | EXPECT_THAT(err, IsEmpty()); |
| 487 | } |
| 488 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 489 | TEST_F(DumpstateTest, DumpFileUpdateProgressNoListener) { |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 490 | ds.update_progress_ = true; |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 491 | ds.progress_ = 0; |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 492 | ds.weight_total_ = 30; |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 493 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 494 | EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 495 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 496 | std::string progress_message = |
| 497 | GetProgressMessageAndAssertSystemProperties(5, 30); // TODO: unhardcode WEIGHT_FILE (5)? |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 498 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 499 | EXPECT_THAT(err, StrEq(progress_message)); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 500 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 501 | } |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 502 | |
| 503 | TEST_F(DumpstateTest, DumpFileUpdateProgress) { |
| 504 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 505 | ds.listener_ = listener; |
| 506 | ds.listener_name_ = "FoxMulder"; |
| 507 | ds.update_progress_ = true; |
| 508 | ds.progress_ = 0; |
| 509 | ds.weight_total_ = 30; |
| 510 | |
| 511 | EXPECT_CALL(*listener, onProgressUpdated(5)); |
| 512 | EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt")); |
| 513 | |
| 514 | std::string progress_message = |
| 515 | GetProgressMessage(ds.listener_name_, 5, 30); // TODO: unhardcode WEIGHT_FILE (5)? |
| 516 | EXPECT_THAT(err, StrEq(progress_message)); |
| 517 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 518 | |
| 519 | ds.listener_.clear(); |
| 520 | } |
| 521 | |
| 522 | class DumpstateServiceTest : public Test { |
| 523 | public: |
| 524 | DumpstateService dss; |
| 525 | }; |
| 526 | |
| 527 | TEST_F(DumpstateServiceTest, SetListenerNoName) { |
| 528 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 529 | bool result; |
| 530 | EXPECT_TRUE(dss.setListener("", listener, &result).isOk()); |
| 531 | EXPECT_FALSE(result); |
| 532 | } |
| 533 | |
| 534 | TEST_F(DumpstateServiceTest, SetListenerNoPointer) { |
| 535 | bool result; |
| 536 | EXPECT_TRUE(dss.setListener("whatever", nullptr, &result).isOk()); |
| 537 | EXPECT_FALSE(result); |
| 538 | } |
| 539 | |
| 540 | TEST_F(DumpstateServiceTest, SetListenerTwice) { |
| 541 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 542 | bool result; |
| 543 | EXPECT_TRUE(dss.setListener("whatever", listener, &result).isOk()); |
| 544 | EXPECT_TRUE(result); |
| 545 | |
| 546 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); |
| 547 | |
| 548 | EXPECT_TRUE(dss.setListener("whatever", listener, &result).isOk()); |
| 549 | EXPECT_FALSE(result); |
| 550 | } |