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 | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 66 | // Base class for all tests in this file |
| 67 | class DumpstateBaseTest : public Test { |
| 68 | protected: |
| 69 | const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str()); |
| 70 | const std::string kFixturesPath = kTestPath + "/../dumpstate_test_fixture/"; |
| 71 | const std::string kTestDataPath = kFixturesPath + "/testdata/"; |
| 72 | const std::string kSimpleCommand = kFixturesPath + "dumpstate_test_fixture"; |
| 73 | const std::string kEchoCommand = "/system/bin/echo"; |
| 74 | |
| 75 | /* |
| 76 | * Copies a text file fixture to a temporary file, returning it's path. |
| 77 | * |
| 78 | * Useful in cases where the test case changes the content of the tile. |
| 79 | */ |
| 80 | std::string CopyTextFileFixture(const std::string& relative_name) { |
| 81 | std::string from = kTestDataPath + relative_name; |
| 82 | // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it |
| 83 | // around for poking when the test fails. |
| 84 | std::string to = kTestDataPath + relative_name + ".tmp"; |
| 85 | ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str()); |
| 86 | android::base::RemoveFileIfExists(to); |
| 87 | CopyTextFile(from, to); |
| 88 | return to.c_str(); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | // Need a function that returns void to use assertions - |
| 93 | // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement |
| 94 | void CopyTextFile(const std::string& from, const std::string& to) { |
| 95 | std::string content; |
| 96 | ASSERT_TRUE(android::base::ReadFileToString(from, &content)) << "could not read from " |
| 97 | << from; |
| 98 | ASSERT_TRUE(android::base::WriteStringToFile(content, to)) << "could not write to " << to; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | class DumpstateTest : public DumpstateBaseTest { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 103 | public: |
| 104 | void SetUp() { |
| 105 | SetDryRun(false); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 106 | SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 107 | ds.progress_.reset(new Progress()); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 108 | ds.update_progress_ = false; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Runs a command and capture `stdout` and `stderr`. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 112 | 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] | 113 | const CommandOptions& options = CommandOptions::DEFAULT) { |
| 114 | CaptureStdout(); |
| 115 | CaptureStderr(); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 116 | int status = ds.RunCommand(title, full_command, options); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 117 | out = GetCapturedStdout(); |
| 118 | err = GetCapturedStderr(); |
| 119 | return status; |
| 120 | } |
| 121 | |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 122 | // Dumps a file and capture `stdout` and `stderr`. |
| 123 | int DumpFile(const std::string& title, const std::string& path) { |
| 124 | CaptureStdout(); |
| 125 | CaptureStderr(); |
| 126 | int status = ds.DumpFile(title, path); |
| 127 | out = GetCapturedStdout(); |
| 128 | err = GetCapturedStderr(); |
| 129 | return status; |
| 130 | } |
| 131 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 132 | void SetDryRun(bool dry_run) { |
| 133 | ALOGD("Setting dry_run_ to %s\n", dry_run ? "true" : "false"); |
| 134 | ds.dry_run_ = dry_run; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 137 | void SetBuildType(const std::string& build_type) { |
| 138 | ALOGD("Setting build_type_ to '%s'\n", build_type.c_str()); |
| 139 | ds.build_type_ = build_type; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | bool IsUserBuild() { |
| 143 | return "user" == android::base::GetProperty("ro.build.type", "(unknown)"); |
| 144 | } |
| 145 | |
| 146 | void DropRoot() { |
| 147 | drop_root_user(); |
| 148 | uid_t uid = getuid(); |
| 149 | ASSERT_EQ(2000, (int)uid); |
| 150 | } |
| 151 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 152 | void SetProgress(long progress, long initial_max) { |
| 153 | ds.update_progress_ = true; |
| 154 | ds.progress_.reset(new Progress(initial_max, progress, 1.2)); |
| 155 | } |
| 156 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 157 | // TODO: remove when progress is set by Binder callbacks. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 158 | void AssertSystemProperty(const std::string& key, const std::string& expected_value) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 159 | std::string actualValue = android::base::GetProperty(key, "not set"); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 160 | EXPECT_THAT(expected_value, StrEq(actualValue)) << "invalid value for property " << key; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 163 | // TODO: remove when progress is set by Binder callbacks. |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 164 | std::string GetProgressMessageAndAssertSystemProperties(int progress, int max, int old_max = 0) { |
| 165 | EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress"; |
| 166 | EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max"; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 167 | |
| 168 | AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.progress", getpid()), |
| 169 | std::to_string(progress)); |
| 170 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 171 | bool max_increased = old_max > 0; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 172 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 173 | std::string adjustment_message = ""; |
| 174 | if (max_increased) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 175 | AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.max", getpid()), |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 176 | std::to_string(max)); |
| 177 | adjustment_message = |
| 178 | android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | return android::base::StringPrintf("%sSetting progress (dumpstate.%d.progress): %d/%d\n", |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 182 | adjustment_message.c_str(), getpid(), progress, max); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 185 | std::string GetProgressMessage(const std::string& listener_name, int progress, int max, |
| 186 | int old_max = 0) { |
| 187 | EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress"; |
| 188 | EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max"; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 189 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 190 | bool max_increased = old_max > 0; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 191 | |
| 192 | std::string adjustment_message = ""; |
| 193 | if (max_increased) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 194 | adjustment_message = |
| 195 | android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return android::base::StringPrintf("%sSetting progress (%s): %d/%d\n", |
| 199 | adjustment_message.c_str(), listener_name.c_str(), |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 200 | progress, max); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 203 | // `stdout` and `stderr` from the last command ran. |
| 204 | std::string out, err; |
| 205 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 206 | Dumpstate& ds = Dumpstate::GetInstance(); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | TEST_F(DumpstateTest, RunCommandNoArgs) { |
| 210 | EXPECT_EQ(-1, RunCommand("", {})); |
| 211 | } |
| 212 | |
| 213 | TEST_F(DumpstateTest, RunCommandNoTitle) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 214 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 215 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 216 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 217 | } |
| 218 | |
| 219 | TEST_F(DumpstateTest, RunCommandWithTitle) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 220 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 221 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 222 | // 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] | 223 | EXPECT_THAT(out, |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 224 | StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n------")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 225 | EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n")); |
| 226 | } |
| 227 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 228 | TEST_F(DumpstateTest, RunCommandWithLoggingMessage) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 229 | EXPECT_EQ( |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 230 | 0, RunCommand("", {kSimpleCommand}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 231 | CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build())); |
| 232 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 233 | EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n")); |
| 234 | } |
| 235 | |
| 236 | TEST_F(DumpstateTest, RunCommandRedirectStderr) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 237 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 238 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 239 | EXPECT_THAT(out, IsEmpty()); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 240 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | TEST_F(DumpstateTest, RunCommandWithOneArg) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 244 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 245 | EXPECT_THAT(err, IsEmpty()); |
| 246 | EXPECT_THAT(out, StrEq("one\n")); |
| 247 | } |
| 248 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 249 | TEST_F(DumpstateTest, RunCommandWithMultipleArgs) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 250 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 251 | EXPECT_THAT(err, IsEmpty()); |
| 252 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); |
| 253 | } |
| 254 | |
| 255 | TEST_F(DumpstateTest, RunCommandDryRun) { |
| 256 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 257 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 258 | // We don't know the exact duration, so we check the prefix and suffix |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 259 | EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand + |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 260 | ") ------\n\t(skipped on dry run)\n------")); |
| 261 | EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n")); |
| 262 | EXPECT_THAT(err, IsEmpty()); |
| 263 | } |
| 264 | |
| 265 | TEST_F(DumpstateTest, RunCommandDryRunNoTitle) { |
| 266 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 267 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 268 | EXPECT_THAT(out, IsEmpty()); |
| 269 | EXPECT_THAT(err, IsEmpty()); |
| 270 | } |
| 271 | |
| 272 | TEST_F(DumpstateTest, RunCommandDryRunAlways) { |
| 273 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 274 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 275 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 276 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 277 | } |
| 278 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 279 | TEST_F(DumpstateTest, RunCommandNotFound) { |
| 280 | EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"})); |
| 281 | EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code")); |
| 282 | EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed")); |
| 283 | } |
| 284 | |
| 285 | TEST_F(DumpstateTest, RunCommandFails) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 286 | EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"})); |
| 287 | EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand + |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 288 | " --exit 42' failed: exit code 42\n")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 289 | EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand + |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 290 | " --exit 42' failed: exit code 42\n")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | TEST_F(DumpstateTest, RunCommandCrashes) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 294 | EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"})); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 295 | // We don't know the exit code, so check just the prefix. |
| 296 | EXPECT_THAT( |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 297 | out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 298 | EXPECT_THAT( |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 299 | err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | TEST_F(DumpstateTest, RunCommandTimesout) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 303 | EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 304 | CommandOptions::WithTimeout(1).Build())); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 305 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 306 | " --sleep 2' timed out after 1")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 307 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 308 | " --sleep 2' timed out after 1")); |
| 309 | } |
| 310 | |
| 311 | TEST_F(DumpstateTest, RunCommandIsKilled) { |
| 312 | CaptureStdout(); |
| 313 | CaptureStderr(); |
| 314 | |
| 315 | std::thread t([=]() { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 316 | EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 317 | CommandOptions::WithTimeout(100).Always().Build())); |
| 318 | }); |
| 319 | |
| 320 | // Capture pid and pre-sleep output. |
| 321 | sleep(1); // Wait a little bit to make sure pid and 1st line were printed. |
| 322 | std::string err = GetCapturedStderr(); |
| 323 | EXPECT_THAT(err, StrEq("sleeping for 20s\n")); |
| 324 | |
| 325 | std::string out = GetCapturedStdout(); |
| 326 | std::vector<std::string> lines = android::base::Split(out, "\n"); |
| 327 | ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out; |
| 328 | |
| 329 | int pid = atoi(lines[0].c_str()); |
| 330 | EXPECT_THAT(lines[1], StrEq("stdout line1")); |
| 331 | EXPECT_THAT(lines[2], IsEmpty()); // \n |
| 332 | |
| 333 | // Then kill the process. |
| 334 | CaptureStdout(); |
| 335 | CaptureStderr(); |
| 336 | ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid; |
| 337 | t.join(); |
| 338 | |
| 339 | // Finally, check output after murder. |
| 340 | out = GetCapturedStdout(); |
| 341 | err = GetCapturedStderr(); |
| 342 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 343 | EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 344 | " --pid --sleep 20' failed: killed by signal 15\n")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 345 | EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 346 | " --pid --sleep 20' failed: killed by signal 15\n")); |
| 347 | } |
| 348 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 349 | TEST_F(DumpstateTest, RunCommandProgressNoListener) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 350 | SetProgress(0, 30); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 351 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 352 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 353 | std::string progress_message = GetProgressMessageAndAssertSystemProperties(20, 30); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 354 | EXPECT_THAT(out, StrEq("stdout\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 355 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 356 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 357 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 358 | progress_message = GetProgressMessageAndAssertSystemProperties(30, 30); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 359 | EXPECT_THAT(out, StrEq("stdout\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 360 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 361 | |
| 362 | // Run a command that will increase maximum timeout. |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 363 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build())); |
| 364 | progress_message = GetProgressMessageAndAssertSystemProperties(31, 37, 30); // 20% increase |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 365 | EXPECT_THAT(out, StrEq("stdout\n")); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 366 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 367 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 368 | // Make sure command ran while in dry_run is counted. |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 369 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 370 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build())); |
| 371 | progress_message = GetProgressMessageAndAssertSystemProperties(35, 37); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 372 | EXPECT_THAT(out, IsEmpty()); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 373 | EXPECT_THAT(err, StrEq(progress_message)); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 376 | TEST_F(DumpstateTest, RunCommandProgress) { |
| 377 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 378 | ds.listener_ = listener; |
| 379 | ds.listener_name_ = "FoxMulder"; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 380 | SetProgress(0, 30); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 381 | |
| 382 | EXPECT_CALL(*listener, onProgressUpdated(20)); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 383 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 384 | std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30); |
| 385 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 386 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 387 | |
| 388 | EXPECT_CALL(*listener, onProgressUpdated(30)); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 389 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 390 | progress_message = GetProgressMessage(ds.listener_name_, 30, 30); |
| 391 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 392 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 393 | |
| 394 | // Run a command that will increase maximum timeout. |
| 395 | EXPECT_CALL(*listener, onProgressUpdated(31)); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 396 | EXPECT_CALL(*listener, onMaxProgressUpdated(37)); |
| 397 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build())); |
| 398 | progress_message = GetProgressMessage(ds.listener_name_, 31, 37, 30); // 20% increase |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 399 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 400 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 401 | |
| 402 | // Make sure command ran while in dry_run is counted. |
| 403 | SetDryRun(true); |
| 404 | EXPECT_CALL(*listener, onProgressUpdated(35)); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 405 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build())); |
| 406 | progress_message = GetProgressMessage(ds.listener_name_, 35, 37); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 407 | EXPECT_THAT(out, IsEmpty()); |
| 408 | EXPECT_THAT(err, StrEq(progress_message)); |
| 409 | |
| 410 | ds.listener_.clear(); |
| 411 | } |
| 412 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 413 | TEST_F(DumpstateTest, RunCommandDropRoot) { |
| 414 | // First check root case - only available when running with 'adb root'. |
| 415 | uid_t uid = getuid(); |
| 416 | if (uid == 0) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 417 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"})); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 418 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 419 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 420 | return; |
| 421 | } |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 422 | // Then run dropping root. |
| 423 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 424 | CommandOptions::WithTimeout(1).DropRoot().Build())); |
| 425 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
Felipe Leme | 26c4157 | 2016-10-06 14:34:43 -0700 | [diff] [blame] | 426 | 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] | 427 | } |
| 428 | |
| 429 | TEST_F(DumpstateTest, RunCommandAsRootUserBuild) { |
| 430 | if (!IsUserBuild()) { |
| 431 | // Emulates user build if necessarily. |
| 432 | SetBuildType("user"); |
| 433 | } |
| 434 | |
| 435 | DropRoot(); |
| 436 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 437 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build())); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 438 | |
| 439 | // We don't know the exact path of su, so we just check for the 'root ...' commands |
| 440 | EXPECT_THAT(out, StartsWith("Skipping")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 441 | EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n")); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 442 | EXPECT_THAT(err, IsEmpty()); |
| 443 | } |
| 444 | |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 445 | TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) { |
| 446 | EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist")); |
| 447 | EXPECT_THAT(out, |
| 448 | StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n")); |
| 449 | EXPECT_THAT(err, IsEmpty()); |
| 450 | } |
| 451 | |
| 452 | TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) { |
| 453 | EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist")); |
| 454 | EXPECT_THAT(err, IsEmpty()); |
| 455 | // We don't know the exact duration, so we check the prefix and suffix |
| 456 | EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No " |
| 457 | "such file or directory\n")); |
| 458 | EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n")); |
| 459 | } |
| 460 | |
| 461 | TEST_F(DumpstateTest, DumpFileSingleLine) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 462 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 463 | EXPECT_THAT(err, IsEmpty()); |
| 464 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 465 | } |
| 466 | |
| 467 | TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 468 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 469 | EXPECT_THAT(err, IsEmpty()); |
| 470 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); |
| 471 | } |
| 472 | |
| 473 | TEST_F(DumpstateTest, DumpFileMultipleLines) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 474 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 475 | EXPECT_THAT(err, IsEmpty()); |
| 476 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 477 | } |
| 478 | |
| 479 | TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 480 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 481 | EXPECT_THAT(err, IsEmpty()); |
| 482 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 483 | } |
| 484 | |
| 485 | TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) { |
| 486 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 487 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 488 | EXPECT_THAT(err, IsEmpty()); |
| 489 | EXPECT_THAT(out, IsEmpty()); |
| 490 | } |
| 491 | |
| 492 | TEST_F(DumpstateTest, DumpFileOnDryRun) { |
| 493 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 494 | EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 495 | EXPECT_THAT(err, IsEmpty()); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 496 | EXPECT_THAT(out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 497 | "single-line.txt) ------\n\t(skipped on dry run)\n------")); |
| 498 | EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n")); |
| 499 | EXPECT_THAT(err, IsEmpty()); |
| 500 | } |
| 501 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 502 | TEST_F(DumpstateTest, DumpFileUpdateProgressNoListener) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 503 | SetProgress(0, 30); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 504 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 505 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 506 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 507 | std::string progress_message = |
| 508 | GetProgressMessageAndAssertSystemProperties(5, 30); // TODO: unhardcode WEIGHT_FILE (5)? |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 509 | |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 510 | EXPECT_THAT(err, StrEq(progress_message)); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 511 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 512 | } |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 513 | |
| 514 | TEST_F(DumpstateTest, DumpFileUpdateProgress) { |
| 515 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 516 | ds.listener_ = listener; |
| 517 | ds.listener_name_ = "FoxMulder"; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 518 | SetProgress(0, 30); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 519 | |
| 520 | EXPECT_CALL(*listener, onProgressUpdated(5)); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 521 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 522 | |
| 523 | std::string progress_message = |
| 524 | GetProgressMessage(ds.listener_name_, 5, 30); // TODO: unhardcode WEIGHT_FILE (5)? |
| 525 | EXPECT_THAT(err, StrEq(progress_message)); |
| 526 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 527 | |
| 528 | ds.listener_.clear(); |
| 529 | } |
| 530 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 531 | class DumpstateServiceTest : public DumpstateBaseTest { |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 532 | public: |
| 533 | DumpstateService dss; |
| 534 | }; |
| 535 | |
| 536 | TEST_F(DumpstateServiceTest, SetListenerNoName) { |
| 537 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 538 | bool result; |
| 539 | EXPECT_TRUE(dss.setListener("", listener, &result).isOk()); |
| 540 | EXPECT_FALSE(result); |
| 541 | } |
| 542 | |
| 543 | TEST_F(DumpstateServiceTest, SetListenerNoPointer) { |
| 544 | bool result; |
| 545 | EXPECT_TRUE(dss.setListener("whatever", nullptr, &result).isOk()); |
| 546 | EXPECT_FALSE(result); |
| 547 | } |
| 548 | |
| 549 | TEST_F(DumpstateServiceTest, SetListenerTwice) { |
| 550 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 551 | bool result; |
| 552 | EXPECT_TRUE(dss.setListener("whatever", listener, &result).isOk()); |
| 553 | EXPECT_TRUE(result); |
| 554 | |
| 555 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); |
| 556 | |
| 557 | EXPECT_TRUE(dss.setListener("whatever", listener, &result).isOk()); |
| 558 | EXPECT_FALSE(result); |
| 559 | } |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame^] | 560 | |
| 561 | class ProgressTest : public DumpstateBaseTest { |
| 562 | public: |
| 563 | Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") { |
| 564 | return Progress(max, growth_factor, path); |
| 565 | } |
| 566 | |
| 567 | void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) { |
| 568 | std::string expected_content = |
| 569 | android::base::StringPrintf("%d %d\n", expected_runs, expected_average); |
| 570 | std::string actual_content; |
| 571 | ASSERT_TRUE(android::base::ReadFileToString(path, &actual_content)) |
| 572 | << "could not read statsfrom" << path; |
| 573 | ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path; |
| 574 | } |
| 575 | }; |
| 576 | |
| 577 | TEST_F(ProgressTest, SimpleTest) { |
| 578 | Progress progress; |
| 579 | EXPECT_EQ(0, progress.Get()); |
| 580 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); |
| 581 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 582 | |
| 583 | bool max_increased = progress.Inc(1); |
| 584 | EXPECT_EQ(1, progress.Get()); |
| 585 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); |
| 586 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 587 | EXPECT_FALSE(max_increased); |
| 588 | |
| 589 | // Ignore negative increase. |
| 590 | max_increased = progress.Inc(-1); |
| 591 | EXPECT_EQ(1, progress.Get()); |
| 592 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); |
| 593 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 594 | EXPECT_FALSE(max_increased); |
| 595 | } |
| 596 | |
| 597 | TEST_F(ProgressTest, MaxGrowsInsideNewRange) { |
| 598 | Progress progress = GetInstance(10, 1.2); // 20% growth factor |
| 599 | EXPECT_EQ(0, progress.Get()); |
| 600 | EXPECT_EQ(10, progress.GetInitialMax()); |
| 601 | EXPECT_EQ(10, progress.GetMax()); |
| 602 | |
| 603 | // No increase |
| 604 | bool max_increased = progress.Inc(10); |
| 605 | EXPECT_EQ(10, progress.Get()); |
| 606 | EXPECT_EQ(10, progress.GetMax()); |
| 607 | EXPECT_FALSE(max_increased); |
| 608 | |
| 609 | // Increase, with new value < max*20% |
| 610 | max_increased = progress.Inc(1); |
| 611 | EXPECT_EQ(11, progress.Get()); |
| 612 | EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13 |
| 613 | EXPECT_TRUE(max_increased); |
| 614 | } |
| 615 | |
| 616 | TEST_F(ProgressTest, MaxGrowsOutsideNewRange) { |
| 617 | Progress progress = GetInstance(10, 1.2); // 20% growth factor |
| 618 | EXPECT_EQ(0, progress.Get()); |
| 619 | EXPECT_EQ(10, progress.GetInitialMax()); |
| 620 | EXPECT_EQ(10, progress.GetMax()); |
| 621 | |
| 622 | // No increase |
| 623 | bool max_increased = progress.Inc(10); |
| 624 | EXPECT_EQ(10, progress.Get()); |
| 625 | EXPECT_EQ(10, progress.GetMax()); |
| 626 | EXPECT_FALSE(max_increased); |
| 627 | |
| 628 | // Increase, with new value > max*20% |
| 629 | max_increased = progress.Inc(5); |
| 630 | EXPECT_EQ(15, progress.Get()); |
| 631 | EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18 |
| 632 | EXPECT_TRUE(max_increased); |
| 633 | } |
| 634 | |
| 635 | TEST_F(ProgressTest, InvalidPath) { |
| 636 | Progress progress("/devil/null"); |
| 637 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 638 | } |
| 639 | |
| 640 | TEST_F(ProgressTest, EmptyFile) { |
| 641 | Progress progress(CopyTextFileFixture("empty-file.txt")); |
| 642 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 643 | } |
| 644 | |
| 645 | TEST_F(ProgressTest, InvalidLine1stEntryNAN) { |
| 646 | Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt")); |
| 647 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 648 | } |
| 649 | |
| 650 | TEST_F(ProgressTest, InvalidLine2ndEntryNAN) { |
| 651 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt")); |
| 652 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 653 | } |
| 654 | |
| 655 | TEST_F(ProgressTest, InvalidLineBothNAN) { |
| 656 | Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt")); |
| 657 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 658 | } |
| 659 | |
| 660 | TEST_F(ProgressTest, InvalidLine1stEntryNegative) { |
| 661 | Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt")); |
| 662 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 663 | } |
| 664 | |
| 665 | TEST_F(ProgressTest, InvalidLine2ndEntryNegative) { |
| 666 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt")); |
| 667 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 668 | } |
| 669 | |
| 670 | TEST_F(ProgressTest, InvalidLine1stEntryTooBig) { |
| 671 | Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt")); |
| 672 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 673 | } |
| 674 | |
| 675 | TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) { |
| 676 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt")); |
| 677 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 678 | } |
| 679 | |
| 680 | // Tests stats are properly saved when the file does not exists. |
| 681 | TEST_F(ProgressTest, FirstTime) { |
| 682 | std::string path = kTestDataPath + "FirstTime.txt"; |
| 683 | android::base::RemoveFileIfExists(path); |
| 684 | |
| 685 | Progress run1(path); |
| 686 | EXPECT_EQ(0, run1.Get()); |
| 687 | EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax()); |
| 688 | EXPECT_EQ(Progress::kDefaultMax, run1.GetMax()); |
| 689 | |
| 690 | bool max_increased = run1.Inc(20); |
| 691 | EXPECT_EQ(20, run1.Get()); |
| 692 | EXPECT_EQ(Progress::kDefaultMax, run1.GetMax()); |
| 693 | EXPECT_FALSE(max_increased); |
| 694 | |
| 695 | run1.Save(); |
| 696 | AssertStats(path, 1, 20); |
| 697 | } |
| 698 | |
| 699 | // Tests what happens when the persistent settings contains the average duration of 1 run. |
| 700 | // Data on file is 1 run and 109 average. |
| 701 | TEST_F(ProgressTest, SecondTime) { |
| 702 | std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt"); |
| 703 | |
| 704 | Progress run1 = GetInstance(-42, 1.2, path); |
| 705 | EXPECT_EQ(0, run1.Get()); |
| 706 | EXPECT_EQ(10, run1.GetInitialMax()); |
| 707 | EXPECT_EQ(10, run1.GetMax()); |
| 708 | |
| 709 | bool max_increased = run1.Inc(20); |
| 710 | EXPECT_EQ(20, run1.Get()); |
| 711 | EXPECT_EQ(24, run1.GetMax()); |
| 712 | EXPECT_TRUE(max_increased); |
| 713 | |
| 714 | // Average now is 2 runs and (10 + 20)/ 2 = 15 |
| 715 | run1.Save(); |
| 716 | AssertStats(path, 2, 15); |
| 717 | |
| 718 | Progress run2 = GetInstance(-42, 1.2, path); |
| 719 | EXPECT_EQ(0, run2.Get()); |
| 720 | EXPECT_EQ(15, run2.GetInitialMax()); |
| 721 | EXPECT_EQ(15, run2.GetMax()); |
| 722 | |
| 723 | max_increased = run2.Inc(25); |
| 724 | EXPECT_EQ(25, run2.Get()); |
| 725 | EXPECT_EQ(30, run2.GetMax()); |
| 726 | EXPECT_TRUE(max_increased); |
| 727 | |
| 728 | // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18 |
| 729 | run2.Save(); |
| 730 | AssertStats(path, 3, 18); |
| 731 | |
| 732 | Progress run3 = GetInstance(-42, 1.2, path); |
| 733 | EXPECT_EQ(0, run3.Get()); |
| 734 | EXPECT_EQ(18, run3.GetInitialMax()); |
| 735 | EXPECT_EQ(18, run3.GetMax()); |
| 736 | |
| 737 | // Make sure average decreases as well |
| 738 | max_increased = run3.Inc(5); |
| 739 | EXPECT_EQ(5, run3.Get()); |
| 740 | EXPECT_EQ(18, run3.GetMax()); |
| 741 | EXPECT_FALSE(max_increased); |
| 742 | |
| 743 | // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14 |
| 744 | run3.Save(); |
| 745 | AssertStats(path, 4, 14); |
| 746 | } |
| 747 | |
| 748 | // Tests what happens when the persistent settings contains the average duration of 2 runs. |
| 749 | // Data on file is 2 runs and 15 average. |
| 750 | TEST_F(ProgressTest, ThirdTime) { |
| 751 | std::string path = CopyTextFileFixture("stats-two-runs.txt"); |
| 752 | AssertStats(path, 2, 15); // Sanity check |
| 753 | |
| 754 | Progress run1 = GetInstance(-42, 1.2, path); |
| 755 | EXPECT_EQ(0, run1.Get()); |
| 756 | EXPECT_EQ(15, run1.GetInitialMax()); |
| 757 | EXPECT_EQ(15, run1.GetMax()); |
| 758 | |
| 759 | bool max_increased = run1.Inc(20); |
| 760 | EXPECT_EQ(20, run1.Get()); |
| 761 | EXPECT_EQ(24, run1.GetMax()); |
| 762 | EXPECT_TRUE(max_increased); |
| 763 | |
| 764 | // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16 |
| 765 | run1.Save(); |
| 766 | AssertStats(path, 3, 16); |
| 767 | } |
| 768 | |
| 769 | // TODO: RunCommandAsRootNonUserBuild must be the last test because it drops root, which could cause |
| 770 | // other tests to fail if they're relyin on the process running as root. |
| 771 | // For now this is fine, but eventually it might need to be moved to its own test case / process. |
| 772 | TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) { |
| 773 | if (IsUserBuild()) { |
| 774 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); |
| 775 | return; |
| 776 | } |
| 777 | |
| 778 | DropRoot(); |
| 779 | |
| 780 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 781 | CommandOptions::WithTimeout(1).AsRoot().Build())); |
| 782 | |
| 783 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 784 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 785 | } |