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 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 20 | #include "DumpstateInternal.h" |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 21 | #include "DumpstateService.h" |
| 22 | #include "android/os/BnDumpstate.h" |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 23 | #include "dumpstate.h" |
| 24 | |
| 25 | #include <gmock/gmock.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 28 | #include <fcntl.h> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 29 | #include <libgen.h> |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include <sys/types.h> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 32 | #include <unistd.h> |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 33 | #include <thread> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 34 | |
| 35 | #include <android-base/file.h> |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 36 | #include <android-base/properties.h> |
| 37 | #include <android-base/stringprintf.h> |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 38 | #include <android-base/strings.h> |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 39 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 40 | using namespace android; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 41 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 42 | using ::testing::EndsWith; |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 43 | using ::testing::HasSubstr; |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 44 | using ::testing::IsNull; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 45 | using ::testing::IsEmpty; |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 46 | using ::testing::NotNull; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 47 | using ::testing::StrEq; |
| 48 | using ::testing::StartsWith; |
| 49 | using ::testing::Test; |
| 50 | using ::testing::internal::CaptureStderr; |
| 51 | using ::testing::internal::CaptureStdout; |
| 52 | using ::testing::internal::GetCapturedStderr; |
| 53 | using ::testing::internal::GetCapturedStdout; |
| 54 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 55 | using os::DumpstateService; |
| 56 | using os::IDumpstateListener; |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 57 | using os::IDumpstateToken; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 58 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 59 | class DumpstateListenerMock : public IDumpstateListener { |
| 60 | public: |
| 61 | MOCK_METHOD1(onProgressUpdated, binder::Status(int32_t progress)); |
| 62 | MOCK_METHOD1(onMaxProgressUpdated, binder::Status(int32_t max_progress)); |
| 63 | |
| 64 | protected: |
| 65 | MOCK_METHOD0(onAsBinder, IBinder*()); |
| 66 | }; |
| 67 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 68 | static int calls_; |
| 69 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 70 | // Base class for all tests in this file |
| 71 | class DumpstateBaseTest : public Test { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 72 | public: |
| 73 | virtual void SetUp() override { |
| 74 | calls_++; |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 75 | SetDryRun(false); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 78 | void SetDryRun(bool dry_run) const { |
| 79 | PropertiesHelper::dry_run_ = dry_run; |
| 80 | } |
| 81 | |
| 82 | void SetBuildType(const std::string& build_type) const { |
| 83 | PropertiesHelper::build_type_ = build_type; |
| 84 | } |
| 85 | |
| 86 | bool IsStandalone() const { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 87 | return calls_ == 1; |
| 88 | } |
| 89 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 90 | void DropRoot() const { |
| 91 | DropRootUser(); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 92 | uid_t uid = getuid(); |
| 93 | ASSERT_EQ(2000, (int)uid); |
| 94 | } |
| 95 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 96 | protected: |
| 97 | const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str()); |
| 98 | const std::string kFixturesPath = kTestPath + "/../dumpstate_test_fixture/"; |
| 99 | const std::string kTestDataPath = kFixturesPath + "/testdata/"; |
| 100 | const std::string kSimpleCommand = kFixturesPath + "dumpstate_test_fixture"; |
| 101 | const std::string kEchoCommand = "/system/bin/echo"; |
| 102 | |
| 103 | /* |
| 104 | * Copies a text file fixture to a temporary file, returning it's path. |
| 105 | * |
| 106 | * Useful in cases where the test case changes the content of the tile. |
| 107 | */ |
| 108 | std::string CopyTextFileFixture(const std::string& relative_name) { |
| 109 | std::string from = kTestDataPath + relative_name; |
| 110 | // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it |
| 111 | // around for poking when the test fails. |
| 112 | std::string to = kTestDataPath + relative_name + ".tmp"; |
| 113 | ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str()); |
| 114 | android::base::RemoveFileIfExists(to); |
| 115 | CopyTextFile(from, to); |
| 116 | return to.c_str(); |
| 117 | } |
| 118 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 119 | // Need functions that returns void to use assertions - |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 120 | // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 121 | void ReadFileToString(const std::string& path, std::string* content) { |
| 122 | ASSERT_TRUE(android::base::ReadFileToString(path, content)) |
| 123 | << "could not read contents from " << path; |
| 124 | } |
| 125 | void WriteStringToFile(const std::string& content, const std::string& path) { |
| 126 | ASSERT_TRUE(android::base::WriteStringToFile(content, path)) |
| 127 | << "could not write contents to " << path; |
| 128 | } |
| 129 | |
| 130 | private: |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 131 | void CopyTextFile(const std::string& from, const std::string& to) { |
| 132 | std::string content; |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 133 | ReadFileToString(from, &content); |
| 134 | WriteStringToFile(content, to); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 135 | } |
| 136 | }; |
| 137 | |
| 138 | class DumpstateTest : public DumpstateBaseTest { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 139 | public: |
| 140 | void SetUp() { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 141 | DumpstateBaseTest::SetUp(); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 142 | SetDryRun(false); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 143 | SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 144 | ds.progress_.reset(new Progress()); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 145 | ds.update_progress_ = false; |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 146 | ds.update_progress_threshold_ = 0; |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Runs a command and capture `stdout` and `stderr`. |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 150 | 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] | 151 | const CommandOptions& options = CommandOptions::DEFAULT) { |
| 152 | CaptureStdout(); |
| 153 | CaptureStderr(); |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 154 | int status = ds.RunCommand(title, full_command, options); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 155 | out = GetCapturedStdout(); |
| 156 | err = GetCapturedStderr(); |
| 157 | return status; |
| 158 | } |
| 159 | |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 160 | // Dumps a file and capture `stdout` and `stderr`. |
| 161 | int DumpFile(const std::string& title, const std::string& path) { |
| 162 | CaptureStdout(); |
| 163 | CaptureStderr(); |
| 164 | int status = ds.DumpFile(title, path); |
| 165 | out = GetCapturedStdout(); |
| 166 | err = GetCapturedStderr(); |
| 167 | return status; |
| 168 | } |
| 169 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 170 | void SetProgress(long progress, long initial_max, long threshold = 0) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 171 | ds.update_progress_ = true; |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 172 | ds.update_progress_threshold_ = threshold; |
| 173 | ds.last_updated_progress_ = 0; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 174 | ds.progress_.reset(new Progress(initial_max, progress, 1.2)); |
| 175 | } |
| 176 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 177 | std::string GetProgressMessage(const std::string& listener_name, int progress, int max, |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 178 | int old_max = 0, bool update_progress = true) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 179 | EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress"; |
| 180 | EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max"; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 181 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 182 | bool max_increased = old_max > 0; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 183 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 184 | std::string message = ""; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 185 | if (max_increased) { |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 186 | message = |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 187 | 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] | 188 | } |
| 189 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 190 | if (update_progress) { |
| 191 | message += android::base::StringPrintf("Setting progress (%s): %d/%d\n", |
| 192 | listener_name.c_str(), progress, max); |
| 193 | } |
| 194 | |
| 195 | return message; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 198 | // `stdout` and `stderr` from the last command ran. |
| 199 | std::string out, err; |
| 200 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 201 | Dumpstate& ds = Dumpstate::GetInstance(); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | TEST_F(DumpstateTest, RunCommandNoArgs) { |
| 205 | EXPECT_EQ(-1, RunCommand("", {})); |
| 206 | } |
| 207 | |
| 208 | TEST_F(DumpstateTest, RunCommandNoTitle) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 209 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 210 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 211 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 212 | } |
| 213 | |
| 214 | TEST_F(DumpstateTest, RunCommandWithTitle) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 215 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 216 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 217 | // 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] | 218 | EXPECT_THAT(out, |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 219 | StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n------")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 220 | EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n")); |
| 221 | } |
| 222 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 223 | TEST_F(DumpstateTest, RunCommandWithLoggingMessage) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 224 | EXPECT_EQ( |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 225 | 0, RunCommand("", {kSimpleCommand}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 226 | CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build())); |
| 227 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 228 | EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n")); |
| 229 | } |
| 230 | |
| 231 | TEST_F(DumpstateTest, RunCommandRedirectStderr) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 232 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 233 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 234 | EXPECT_THAT(out, IsEmpty()); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 235 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | TEST_F(DumpstateTest, RunCommandWithOneArg) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 239 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 240 | EXPECT_THAT(err, IsEmpty()); |
| 241 | EXPECT_THAT(out, StrEq("one\n")); |
| 242 | } |
| 243 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 244 | TEST_F(DumpstateTest, RunCommandWithMultipleArgs) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 245 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 246 | EXPECT_THAT(err, IsEmpty()); |
| 247 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); |
| 248 | } |
| 249 | |
| 250 | TEST_F(DumpstateTest, RunCommandDryRun) { |
| 251 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 252 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 253 | // 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] | 254 | EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand + |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 255 | ") ------\n\t(skipped on dry run)\n------")); |
| 256 | EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n")); |
| 257 | EXPECT_THAT(err, IsEmpty()); |
| 258 | } |
| 259 | |
| 260 | TEST_F(DumpstateTest, RunCommandDryRunNoTitle) { |
| 261 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 262 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 263 | EXPECT_THAT(out, IsEmpty()); |
| 264 | EXPECT_THAT(err, IsEmpty()); |
| 265 | } |
| 266 | |
| 267 | TEST_F(DumpstateTest, RunCommandDryRunAlways) { |
| 268 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 269 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 270 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 271 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 272 | } |
| 273 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 274 | TEST_F(DumpstateTest, RunCommandNotFound) { |
| 275 | EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"})); |
| 276 | EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code")); |
| 277 | EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed")); |
| 278 | } |
| 279 | |
| 280 | TEST_F(DumpstateTest, RunCommandFails) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 281 | EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"})); |
| 282 | EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand + |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 283 | " --exit 42' failed: exit code 42\n")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 284 | EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand + |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 285 | " --exit 42' failed: exit code 42\n")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | TEST_F(DumpstateTest, RunCommandCrashes) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 289 | EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"})); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 290 | // We don't know the exit code, so check just the prefix. |
| 291 | EXPECT_THAT( |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 292 | out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 293 | EXPECT_THAT( |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 294 | err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | TEST_F(DumpstateTest, RunCommandTimesout) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 298 | EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 299 | CommandOptions::WithTimeout(1).Build())); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 300 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 301 | " --sleep 2' timed out after 1")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 302 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 303 | " --sleep 2' timed out after 1")); |
| 304 | } |
| 305 | |
| 306 | TEST_F(DumpstateTest, RunCommandIsKilled) { |
| 307 | CaptureStdout(); |
| 308 | CaptureStderr(); |
| 309 | |
| 310 | std::thread t([=]() { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 311 | EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 312 | CommandOptions::WithTimeout(100).Always().Build())); |
| 313 | }); |
| 314 | |
| 315 | // Capture pid and pre-sleep output. |
| 316 | sleep(1); // Wait a little bit to make sure pid and 1st line were printed. |
| 317 | std::string err = GetCapturedStderr(); |
| 318 | EXPECT_THAT(err, StrEq("sleeping for 20s\n")); |
| 319 | |
| 320 | std::string out = GetCapturedStdout(); |
| 321 | std::vector<std::string> lines = android::base::Split(out, "\n"); |
| 322 | ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out; |
| 323 | |
| 324 | int pid = atoi(lines[0].c_str()); |
| 325 | EXPECT_THAT(lines[1], StrEq("stdout line1")); |
| 326 | EXPECT_THAT(lines[2], IsEmpty()); // \n |
| 327 | |
| 328 | // Then kill the process. |
| 329 | CaptureStdout(); |
| 330 | CaptureStderr(); |
| 331 | ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid; |
| 332 | t.join(); |
| 333 | |
| 334 | // Finally, check output after murder. |
| 335 | out = GetCapturedStdout(); |
| 336 | err = GetCapturedStderr(); |
| 337 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 338 | EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 339 | " --pid --sleep 20' failed: killed by signal 15\n")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 340 | EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 341 | " --pid --sleep 20' failed: killed by signal 15\n")); |
| 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"; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 348 | SetProgress(0, 30); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 349 | |
| 350 | EXPECT_CALL(*listener, onProgressUpdated(20)); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 351 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 352 | std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30); |
| 353 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 354 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 355 | |
| 356 | EXPECT_CALL(*listener, onProgressUpdated(30)); |
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 = GetProgressMessage(ds.listener_name_, 30, 30); |
| 359 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 360 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 361 | |
| 362 | // Run a command that will increase maximum timeout. |
| 363 | EXPECT_CALL(*listener, onProgressUpdated(31)); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 364 | EXPECT_CALL(*listener, onMaxProgressUpdated(37)); |
| 365 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build())); |
| 366 | progress_message = GetProgressMessage(ds.listener_name_, 31, 37, 30); // 20% increase |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 367 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 368 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 369 | |
| 370 | // Make sure command ran while in dry_run is counted. |
| 371 | SetDryRun(true); |
| 372 | EXPECT_CALL(*listener, onProgressUpdated(35)); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 373 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build())); |
| 374 | progress_message = GetProgressMessage(ds.listener_name_, 35, 37); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 375 | EXPECT_THAT(out, IsEmpty()); |
| 376 | EXPECT_THAT(err, StrEq(progress_message)); |
| 377 | |
| 378 | ds.listener_.clear(); |
| 379 | } |
| 380 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 381 | TEST_F(DumpstateTest, RunCommandProgressIgnoreThreshold) { |
| 382 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 383 | ds.listener_ = listener; |
| 384 | ds.listener_name_ = "FoxMulder"; |
| 385 | SetProgress(0, 8, 5); // 8 max, 5 threshold |
| 386 | |
| 387 | // First update should always be sent. |
| 388 | EXPECT_CALL(*listener, onProgressUpdated(1)); |
| 389 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build())); |
| 390 | std::string progress_message = GetProgressMessage(ds.listener_name_, 1, 8); |
| 391 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 392 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 393 | |
| 394 | // Fourth update should be ignored because it's between the threshold (5 -1 = 4 < 5). |
| 395 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build())); |
| 396 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 397 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 398 | |
| 399 | // Third update should be sent because it reaches threshold (6 - 1 = 5). |
| 400 | EXPECT_CALL(*listener, onProgressUpdated(6)); |
| 401 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build())); |
| 402 | progress_message = GetProgressMessage(ds.listener_name_, 6, 8); |
| 403 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 404 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 405 | |
| 406 | // Fourth update should be ignored because it's between the threshold (9 - 6 = 3 < 5). |
| 407 | // But max update should be sent. |
| 408 | EXPECT_CALL(*listener, onMaxProgressUpdated(10)); // 9 * 120% = 10.8 = 10 |
| 409 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build())); |
| 410 | progress_message = GetProgressMessage(ds.listener_name_, 9, 10, 8, false); |
| 411 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 412 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 413 | |
| 414 | ds.listener_.clear(); |
| 415 | } |
| 416 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 417 | TEST_F(DumpstateTest, RunCommandDropRoot) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 418 | if (!IsStandalone()) { |
| 419 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 420 | // to Shell - need to refactor tests to avoid this problem) |
| 421 | MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n") |
| 422 | return; |
| 423 | } |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 424 | // First check root case - only available when running with 'adb root'. |
| 425 | uid_t uid = getuid(); |
| 426 | if (uid == 0) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 427 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"})); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 428 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 429 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 430 | return; |
| 431 | } |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 432 | // Then run dropping root. |
| 433 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 434 | CommandOptions::WithTimeout(1).DropRoot().Build())); |
| 435 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
Felipe Leme | 26c4157 | 2016-10-06 14:34:43 -0700 | [diff] [blame] | 436 | 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] | 437 | } |
| 438 | |
| 439 | TEST_F(DumpstateTest, RunCommandAsRootUserBuild) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 440 | if (!IsStandalone()) { |
| 441 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 442 | // to Shell - need to refactor tests to avoid this problem) |
| 443 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n") |
| 444 | return; |
| 445 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 446 | if (!PropertiesHelper::IsUserBuild()) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 447 | // Emulates user build if necessarily. |
| 448 | SetBuildType("user"); |
| 449 | } |
| 450 | |
| 451 | DropRoot(); |
| 452 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 453 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build())); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 454 | |
| 455 | // We don't know the exact path of su, so we just check for the 'root ...' commands |
| 456 | EXPECT_THAT(out, StartsWith("Skipping")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 457 | EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n")); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 458 | EXPECT_THAT(err, IsEmpty()); |
| 459 | } |
| 460 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 461 | TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) { |
| 462 | if (!IsStandalone()) { |
| 463 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 464 | // to Shell - need to refactor tests to avoid this problem) |
| 465 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n") |
| 466 | return; |
| 467 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 468 | if (PropertiesHelper::IsUserBuild()) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 469 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | DropRoot(); |
| 474 | |
| 475 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 476 | CommandOptions::WithTimeout(1).AsRoot().Build())); |
| 477 | |
| 478 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 479 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 480 | } |
| 481 | |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 482 | TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) { |
| 483 | EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist")); |
| 484 | EXPECT_THAT(out, |
| 485 | StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n")); |
| 486 | EXPECT_THAT(err, IsEmpty()); |
| 487 | } |
| 488 | |
| 489 | TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) { |
| 490 | EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist")); |
| 491 | EXPECT_THAT(err, IsEmpty()); |
| 492 | // We don't know the exact duration, so we check the prefix and suffix |
| 493 | EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No " |
| 494 | "such file or directory\n")); |
| 495 | EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n")); |
| 496 | } |
| 497 | |
| 498 | TEST_F(DumpstateTest, DumpFileSingleLine) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 499 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 500 | EXPECT_THAT(err, IsEmpty()); |
| 501 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 502 | } |
| 503 | |
| 504 | TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 505 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 506 | EXPECT_THAT(err, IsEmpty()); |
| 507 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); |
| 508 | } |
| 509 | |
| 510 | TEST_F(DumpstateTest, DumpFileMultipleLines) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 511 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 512 | EXPECT_THAT(err, IsEmpty()); |
| 513 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 514 | } |
| 515 | |
| 516 | TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 517 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 518 | EXPECT_THAT(err, IsEmpty()); |
| 519 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 520 | } |
| 521 | |
| 522 | TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) { |
| 523 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 524 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 525 | EXPECT_THAT(err, IsEmpty()); |
| 526 | EXPECT_THAT(out, IsEmpty()); |
| 527 | } |
| 528 | |
| 529 | TEST_F(DumpstateTest, DumpFileOnDryRun) { |
| 530 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 531 | 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] | 532 | EXPECT_THAT(err, IsEmpty()); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 533 | EXPECT_THAT( |
| 534 | out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:")); |
| 535 | EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n------")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 536 | EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 539 | TEST_F(DumpstateTest, DumpFileUpdateProgress) { |
| 540 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 541 | ds.listener_ = listener; |
| 542 | ds.listener_name_ = "FoxMulder"; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 543 | SetProgress(0, 30); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 544 | |
| 545 | EXPECT_CALL(*listener, onProgressUpdated(5)); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 546 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 547 | |
| 548 | std::string progress_message = |
| 549 | GetProgressMessage(ds.listener_name_, 5, 30); // TODO: unhardcode WEIGHT_FILE (5)? |
| 550 | EXPECT_THAT(err, StrEq(progress_message)); |
| 551 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 552 | |
| 553 | ds.listener_.clear(); |
| 554 | } |
| 555 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 556 | class DumpstateServiceTest : public DumpstateBaseTest { |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 557 | public: |
| 558 | DumpstateService dss; |
| 559 | }; |
| 560 | |
| 561 | TEST_F(DumpstateServiceTest, SetListenerNoName) { |
| 562 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 563 | sp<IDumpstateToken> token; |
| 564 | EXPECT_TRUE(dss.setListener("", listener, &token).isOk()); |
| 565 | ASSERT_THAT(token, IsNull()); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | TEST_F(DumpstateServiceTest, SetListenerNoPointer) { |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 569 | sp<IDumpstateToken> token; |
| 570 | EXPECT_TRUE(dss.setListener("whatever", nullptr, &token).isOk()); |
| 571 | ASSERT_THAT(token, IsNull()); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | TEST_F(DumpstateServiceTest, SetListenerTwice) { |
| 575 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 576 | sp<IDumpstateToken> token; |
| 577 | EXPECT_TRUE(dss.setListener("whatever", listener, &token).isOk()); |
| 578 | ASSERT_THAT(token, NotNull()); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 579 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); |
| 580 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 581 | token.clear(); |
| 582 | EXPECT_TRUE(dss.setListener("whatsoever", listener, &token).isOk()); |
| 583 | ASSERT_THAT(token, IsNull()); |
| 584 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 585 | } |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 586 | |
| 587 | class ProgressTest : public DumpstateBaseTest { |
| 588 | public: |
| 589 | Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") { |
| 590 | return Progress(max, growth_factor, path); |
| 591 | } |
| 592 | |
| 593 | void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) { |
| 594 | std::string expected_content = |
| 595 | android::base::StringPrintf("%d %d\n", expected_runs, expected_average); |
| 596 | std::string actual_content; |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 597 | ReadFileToString(path, &actual_content); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 598 | ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path; |
| 599 | } |
| 600 | }; |
| 601 | |
| 602 | TEST_F(ProgressTest, SimpleTest) { |
| 603 | Progress progress; |
| 604 | EXPECT_EQ(0, progress.Get()); |
| 605 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); |
| 606 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 607 | |
| 608 | bool max_increased = progress.Inc(1); |
| 609 | EXPECT_EQ(1, progress.Get()); |
| 610 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); |
| 611 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 612 | EXPECT_FALSE(max_increased); |
| 613 | |
| 614 | // Ignore negative increase. |
| 615 | max_increased = progress.Inc(-1); |
| 616 | EXPECT_EQ(1, progress.Get()); |
| 617 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); |
| 618 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 619 | EXPECT_FALSE(max_increased); |
| 620 | } |
| 621 | |
| 622 | TEST_F(ProgressTest, MaxGrowsInsideNewRange) { |
| 623 | Progress progress = GetInstance(10, 1.2); // 20% growth factor |
| 624 | EXPECT_EQ(0, progress.Get()); |
| 625 | EXPECT_EQ(10, progress.GetInitialMax()); |
| 626 | EXPECT_EQ(10, progress.GetMax()); |
| 627 | |
| 628 | // No increase |
| 629 | bool max_increased = progress.Inc(10); |
| 630 | EXPECT_EQ(10, progress.Get()); |
| 631 | EXPECT_EQ(10, progress.GetMax()); |
| 632 | EXPECT_FALSE(max_increased); |
| 633 | |
| 634 | // Increase, with new value < max*20% |
| 635 | max_increased = progress.Inc(1); |
| 636 | EXPECT_EQ(11, progress.Get()); |
| 637 | EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13 |
| 638 | EXPECT_TRUE(max_increased); |
| 639 | } |
| 640 | |
| 641 | TEST_F(ProgressTest, MaxGrowsOutsideNewRange) { |
| 642 | Progress progress = GetInstance(10, 1.2); // 20% growth factor |
| 643 | EXPECT_EQ(0, progress.Get()); |
| 644 | EXPECT_EQ(10, progress.GetInitialMax()); |
| 645 | EXPECT_EQ(10, progress.GetMax()); |
| 646 | |
| 647 | // No increase |
| 648 | bool max_increased = progress.Inc(10); |
| 649 | EXPECT_EQ(10, progress.Get()); |
| 650 | EXPECT_EQ(10, progress.GetMax()); |
| 651 | EXPECT_FALSE(max_increased); |
| 652 | |
| 653 | // Increase, with new value > max*20% |
| 654 | max_increased = progress.Inc(5); |
| 655 | EXPECT_EQ(15, progress.Get()); |
| 656 | EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18 |
| 657 | EXPECT_TRUE(max_increased); |
| 658 | } |
| 659 | |
| 660 | TEST_F(ProgressTest, InvalidPath) { |
| 661 | Progress progress("/devil/null"); |
| 662 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 663 | } |
| 664 | |
| 665 | TEST_F(ProgressTest, EmptyFile) { |
| 666 | Progress progress(CopyTextFileFixture("empty-file.txt")); |
| 667 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 668 | } |
| 669 | |
| 670 | TEST_F(ProgressTest, InvalidLine1stEntryNAN) { |
| 671 | Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt")); |
| 672 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 673 | } |
| 674 | |
| 675 | TEST_F(ProgressTest, InvalidLine2ndEntryNAN) { |
| 676 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt")); |
| 677 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 678 | } |
| 679 | |
| 680 | TEST_F(ProgressTest, InvalidLineBothNAN) { |
| 681 | Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt")); |
| 682 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 683 | } |
| 684 | |
| 685 | TEST_F(ProgressTest, InvalidLine1stEntryNegative) { |
| 686 | Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt")); |
| 687 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 688 | } |
| 689 | |
| 690 | TEST_F(ProgressTest, InvalidLine2ndEntryNegative) { |
| 691 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt")); |
| 692 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 693 | } |
| 694 | |
| 695 | TEST_F(ProgressTest, InvalidLine1stEntryTooBig) { |
| 696 | Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt")); |
| 697 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 698 | } |
| 699 | |
| 700 | TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) { |
| 701 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt")); |
| 702 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 703 | } |
| 704 | |
| 705 | // Tests stats are properly saved when the file does not exists. |
| 706 | TEST_F(ProgressTest, FirstTime) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 707 | if (!IsStandalone()) { |
| 708 | // TODO: temporarily disabled because it's failing when running as suite |
| 709 | MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n") |
| 710 | return; |
| 711 | } |
| 712 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 713 | std::string path = kTestDataPath + "FirstTime.txt"; |
| 714 | android::base::RemoveFileIfExists(path); |
| 715 | |
| 716 | Progress run1(path); |
| 717 | EXPECT_EQ(0, run1.Get()); |
| 718 | EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax()); |
| 719 | EXPECT_EQ(Progress::kDefaultMax, run1.GetMax()); |
| 720 | |
| 721 | bool max_increased = run1.Inc(20); |
| 722 | EXPECT_EQ(20, run1.Get()); |
| 723 | EXPECT_EQ(Progress::kDefaultMax, run1.GetMax()); |
| 724 | EXPECT_FALSE(max_increased); |
| 725 | |
| 726 | run1.Save(); |
| 727 | AssertStats(path, 1, 20); |
| 728 | } |
| 729 | |
| 730 | // Tests what happens when the persistent settings contains the average duration of 1 run. |
| 731 | // Data on file is 1 run and 109 average. |
| 732 | TEST_F(ProgressTest, SecondTime) { |
| 733 | std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt"); |
| 734 | |
| 735 | Progress run1 = GetInstance(-42, 1.2, path); |
| 736 | EXPECT_EQ(0, run1.Get()); |
| 737 | EXPECT_EQ(10, run1.GetInitialMax()); |
| 738 | EXPECT_EQ(10, run1.GetMax()); |
| 739 | |
| 740 | bool max_increased = run1.Inc(20); |
| 741 | EXPECT_EQ(20, run1.Get()); |
| 742 | EXPECT_EQ(24, run1.GetMax()); |
| 743 | EXPECT_TRUE(max_increased); |
| 744 | |
| 745 | // Average now is 2 runs and (10 + 20)/ 2 = 15 |
| 746 | run1.Save(); |
| 747 | AssertStats(path, 2, 15); |
| 748 | |
| 749 | Progress run2 = GetInstance(-42, 1.2, path); |
| 750 | EXPECT_EQ(0, run2.Get()); |
| 751 | EXPECT_EQ(15, run2.GetInitialMax()); |
| 752 | EXPECT_EQ(15, run2.GetMax()); |
| 753 | |
| 754 | max_increased = run2.Inc(25); |
| 755 | EXPECT_EQ(25, run2.Get()); |
| 756 | EXPECT_EQ(30, run2.GetMax()); |
| 757 | EXPECT_TRUE(max_increased); |
| 758 | |
| 759 | // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18 |
| 760 | run2.Save(); |
| 761 | AssertStats(path, 3, 18); |
| 762 | |
| 763 | Progress run3 = GetInstance(-42, 1.2, path); |
| 764 | EXPECT_EQ(0, run3.Get()); |
| 765 | EXPECT_EQ(18, run3.GetInitialMax()); |
| 766 | EXPECT_EQ(18, run3.GetMax()); |
| 767 | |
| 768 | // Make sure average decreases as well |
| 769 | max_increased = run3.Inc(5); |
| 770 | EXPECT_EQ(5, run3.Get()); |
| 771 | EXPECT_EQ(18, run3.GetMax()); |
| 772 | EXPECT_FALSE(max_increased); |
| 773 | |
| 774 | // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14 |
| 775 | run3.Save(); |
| 776 | AssertStats(path, 4, 14); |
| 777 | } |
| 778 | |
| 779 | // Tests what happens when the persistent settings contains the average duration of 2 runs. |
| 780 | // Data on file is 2 runs and 15 average. |
| 781 | TEST_F(ProgressTest, ThirdTime) { |
| 782 | std::string path = CopyTextFileFixture("stats-two-runs.txt"); |
| 783 | AssertStats(path, 2, 15); // Sanity check |
| 784 | |
| 785 | Progress run1 = GetInstance(-42, 1.2, path); |
| 786 | EXPECT_EQ(0, run1.Get()); |
| 787 | EXPECT_EQ(15, run1.GetInitialMax()); |
| 788 | EXPECT_EQ(15, run1.GetMax()); |
| 789 | |
| 790 | bool max_increased = run1.Inc(20); |
| 791 | EXPECT_EQ(20, run1.Get()); |
| 792 | EXPECT_EQ(24, run1.GetMax()); |
| 793 | EXPECT_TRUE(max_increased); |
| 794 | |
| 795 | // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16 |
| 796 | run1.Save(); |
| 797 | AssertStats(path, 3, 16); |
| 798 | } |
| 799 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 800 | class DumpstateUtilTest : public DumpstateBaseTest { |
| 801 | public: |
| 802 | void SetUp() { |
| 803 | DumpstateBaseTest::SetUp(); |
| 804 | SetDryRun(false); |
| 805 | } |
| 806 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 807 | void CaptureFdOut() { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 808 | ReadFileToString(path_, &out); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | void CreateFd(const std::string& name) { |
| 812 | path_ = kTestDataPath + name; |
| 813 | MYLOGD("Creating fd for file %s\n", path_.c_str()); |
| 814 | |
| 815 | fd = TEMP_FAILURE_RETRY(open(path_.c_str(), |
| 816 | O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, |
| 817 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
| 818 | ASSERT_GE(fd, 0) << "could not create FD for path " << path_; |
| 819 | } |
| 820 | |
| 821 | // Runs a command into the `fd` and capture `stderr`. |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 822 | int RunCommand(const std::string& title, const std::vector<std::string>& full_command, |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 823 | const CommandOptions& options = CommandOptions::DEFAULT) { |
| 824 | CaptureStderr(); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 825 | int status = RunCommandToFd(fd, title, full_command, options); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 826 | close(fd); |
| 827 | |
| 828 | CaptureFdOut(); |
| 829 | err = GetCapturedStderr(); |
| 830 | return status; |
| 831 | } |
| 832 | |
| 833 | // Dumps a file and into the `fd` and `stderr`. |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 834 | int DumpFile(const std::string& title, const std::string& path) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 835 | CaptureStderr(); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 836 | int status = DumpFileToFd(fd, title, path); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 837 | close(fd); |
| 838 | |
| 839 | CaptureFdOut(); |
| 840 | err = GetCapturedStderr(); |
| 841 | return status; |
| 842 | } |
| 843 | |
Ecco Park | 61ffcf7 | 2016-10-27 15:46:26 -0700 | [diff] [blame] | 844 | // Find out the pid of the process_name |
| 845 | int FindPidOfProcess(const std::string& process_name) { |
| 846 | CaptureStderr(); |
| 847 | int status = GetPidByName(process_name); |
| 848 | err = GetCapturedStderr(); |
| 849 | return status; |
| 850 | } |
| 851 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 852 | int fd; |
| 853 | |
| 854 | // 'fd` output and `stderr` from the last command ran. |
| 855 | std::string out, err; |
| 856 | |
| 857 | private: |
| 858 | std::string path_; |
| 859 | }; |
| 860 | |
| 861 | TEST_F(DumpstateUtilTest, RunCommandNoArgs) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 862 | CreateFd("RunCommandNoArgs.txt"); |
| 863 | EXPECT_EQ(-1, RunCommand("", {})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 864 | } |
| 865 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 866 | TEST_F(DumpstateUtilTest, RunCommandNoTitle) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 867 | CreateFd("RunCommandWithNoArgs.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 868 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 869 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 870 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 871 | } |
| 872 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 873 | TEST_F(DumpstateUtilTest, RunCommandWithTitle) { |
| 874 | CreateFd("RunCommandWithNoArgs.txt"); |
| 875 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
| 876 | EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n")); |
| 877 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 878 | } |
| 879 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 880 | TEST_F(DumpstateUtilTest, RunCommandWithOneArg) { |
| 881 | CreateFd("RunCommandWithOneArg.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 882 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 883 | EXPECT_THAT(err, IsEmpty()); |
| 884 | EXPECT_THAT(out, StrEq("one\n")); |
| 885 | } |
| 886 | |
| 887 | TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) { |
| 888 | CreateFd("RunCommandWithMultipleArgs.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 889 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 890 | EXPECT_THAT(err, IsEmpty()); |
| 891 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); |
| 892 | } |
| 893 | |
| 894 | TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) { |
| 895 | CreateFd("RunCommandWithLoggingMessage.txt"); |
| 896 | EXPECT_EQ( |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 897 | 0, RunCommand("", {kSimpleCommand}, |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 898 | CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build())); |
| 899 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 900 | EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n")); |
| 901 | } |
| 902 | |
| 903 | TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) { |
| 904 | CreateFd("RunCommandRedirectStderr.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 905 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, |
| 906 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 907 | EXPECT_THAT(out, IsEmpty()); |
| 908 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); |
| 909 | } |
| 910 | |
| 911 | TEST_F(DumpstateUtilTest, RunCommandDryRun) { |
| 912 | CreateFd("RunCommandDryRun.txt"); |
| 913 | SetDryRun(true); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 914 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
| 915 | EXPECT_THAT(out, StrEq(android::base::StringPrintf( |
| 916 | "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n", |
| 917 | kSimpleCommand.c_str()))); |
| 918 | EXPECT_THAT(err, IsEmpty()); |
| 919 | } |
| 920 | |
| 921 | TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) { |
| 922 | CreateFd("RunCommandDryRun.txt"); |
| 923 | SetDryRun(true); |
| 924 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 925 | EXPECT_THAT( |
| 926 | out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str()))); |
| 927 | EXPECT_THAT(err, IsEmpty()); |
| 928 | } |
| 929 | |
| 930 | TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) { |
| 931 | CreateFd("RunCommandDryRunAlways.txt"); |
| 932 | SetDryRun(true); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 933 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build())); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 934 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 935 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 936 | } |
| 937 | |
| 938 | TEST_F(DumpstateUtilTest, RunCommandNotFound) { |
| 939 | CreateFd("RunCommandNotFound.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 940 | EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 941 | EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code")); |
| 942 | EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed")); |
| 943 | } |
| 944 | |
| 945 | TEST_F(DumpstateUtilTest, RunCommandFails) { |
| 946 | CreateFd("RunCommandFails.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 947 | EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 948 | EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand + |
| 949 | " --exit 42' failed: exit code 42\n")); |
| 950 | EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand + |
| 951 | " --exit 42' failed: exit code 42\n")); |
| 952 | } |
| 953 | |
| 954 | TEST_F(DumpstateUtilTest, RunCommandCrashes) { |
| 955 | CreateFd("RunCommandCrashes.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 956 | EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 957 | // We don't know the exit code, so check just the prefix. |
| 958 | EXPECT_THAT( |
| 959 | out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
| 960 | EXPECT_THAT( |
| 961 | err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
| 962 | } |
| 963 | |
| 964 | TEST_F(DumpstateUtilTest, RunCommandTimesout) { |
| 965 | CreateFd("RunCommandTimesout.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 966 | EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"}, |
| 967 | CommandOptions::WithTimeout(1).Build())); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 968 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand + |
| 969 | " --sleep 2' timed out after 1")); |
| 970 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand + |
| 971 | " --sleep 2' timed out after 1")); |
| 972 | } |
| 973 | |
| 974 | TEST_F(DumpstateUtilTest, RunCommandIsKilled) { |
| 975 | CreateFd("RunCommandIsKilled.txt"); |
| 976 | CaptureStderr(); |
| 977 | |
| 978 | std::thread t([=]() { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 979 | EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"}, |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 980 | CommandOptions::WithTimeout(100).Always().Build())); |
| 981 | }); |
| 982 | |
| 983 | // Capture pid and pre-sleep output. |
| 984 | sleep(1); // Wait a little bit to make sure pid and 1st line were printed. |
| 985 | std::string err = GetCapturedStderr(); |
| 986 | EXPECT_THAT(err, StrEq("sleeping for 20s\n")); |
| 987 | |
| 988 | CaptureFdOut(); |
| 989 | std::vector<std::string> lines = android::base::Split(out, "\n"); |
| 990 | ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out; |
| 991 | |
| 992 | int pid = atoi(lines[0].c_str()); |
| 993 | EXPECT_THAT(lines[1], StrEq("stdout line1")); |
| 994 | EXPECT_THAT(lines[2], IsEmpty()); // \n |
| 995 | |
| 996 | // Then kill the process. |
| 997 | CaptureFdOut(); |
| 998 | CaptureStderr(); |
| 999 | ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid; |
| 1000 | t.join(); |
| 1001 | |
| 1002 | // Finally, check output after murder. |
| 1003 | CaptureFdOut(); |
| 1004 | err = GetCapturedStderr(); |
| 1005 | |
| 1006 | // out starts with the pid, which is an unknown |
| 1007 | EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand + |
| 1008 | " --pid --sleep 20' failed: killed by signal 15\n")); |
| 1009 | EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand + |
| 1010 | " --pid --sleep 20' failed: killed by signal 15\n")); |
| 1011 | } |
| 1012 | |
| 1013 | TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) { |
| 1014 | if (!IsStandalone()) { |
| 1015 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 1016 | // to Shell - need to refactor tests to avoid this problem) |
| 1017 | MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n") |
| 1018 | return; |
| 1019 | } |
| 1020 | CreateFd("RunCommandAsRootUserBuild.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1021 | if (!PropertiesHelper::IsUserBuild()) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1022 | // Emulates user build if necessarily. |
| 1023 | SetBuildType("user"); |
| 1024 | } |
| 1025 | |
| 1026 | DropRoot(); |
| 1027 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1028 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build())); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1029 | |
| 1030 | // We don't know the exact path of su, so we just check for the 'root ...' commands |
| 1031 | EXPECT_THAT(out, StartsWith("Skipping")); |
| 1032 | EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n")); |
| 1033 | EXPECT_THAT(err, IsEmpty()); |
| 1034 | } |
| 1035 | |
| 1036 | TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) { |
| 1037 | if (!IsStandalone()) { |
| 1038 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 1039 | // to Shell - need to refactor tests to avoid this problem) |
| 1040 | MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n") |
| 1041 | return; |
| 1042 | } |
| 1043 | CreateFd("RunCommandAsRootNonUserBuild.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1044 | if (PropertiesHelper::IsUserBuild()) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1045 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); |
| 1046 | return; |
| 1047 | } |
| 1048 | |
| 1049 | DropRoot(); |
| 1050 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1051 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 1052 | CommandOptions::WithTimeout(1).AsRoot().Build())); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1053 | |
| 1054 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 1055 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 1056 | } |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1057 | |
| 1058 | TEST_F(DumpstateUtilTest, RunCommandDropRoot) { |
| 1059 | if (!IsStandalone()) { |
| 1060 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 1061 | // to Shell - need to refactor tests to avoid this problem) |
| 1062 | MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n") |
| 1063 | return; |
| 1064 | } |
| 1065 | CreateFd("RunCommandDropRoot.txt"); |
| 1066 | // First check root case - only available when running with 'adb root'. |
| 1067 | uid_t uid = getuid(); |
| 1068 | if (uid == 0) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1069 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1070 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 1071 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 1072 | return; |
| 1073 | } |
| 1074 | // Then run dropping root. |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1075 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1076 | CommandOptions::WithTimeout(1).DropRoot().Build())); |
| 1077 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
| 1078 | EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n")); |
| 1079 | } |
| 1080 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1081 | TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1082 | CreateFd("DumpFileNotFound.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1083 | EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1084 | EXPECT_THAT(out, |
| 1085 | StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n")); |
| 1086 | EXPECT_THAT(err, IsEmpty()); |
| 1087 | } |
| 1088 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1089 | TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) { |
| 1090 | CreateFd("DumpFileNotFound.txt"); |
| 1091 | EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist")); |
| 1092 | EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such " |
| 1093 | "file or directory\n")); |
| 1094 | EXPECT_THAT(err, IsEmpty()); |
| 1095 | } |
| 1096 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1097 | TEST_F(DumpstateUtilTest, DumpFileSingleLine) { |
| 1098 | CreateFd("DumpFileSingleLine.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1099 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1100 | EXPECT_THAT(err, IsEmpty()); |
| 1101 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 1102 | } |
| 1103 | |
| 1104 | TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) { |
| 1105 | CreateFd("DumpFileSingleLineWithNewLine.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1106 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1107 | EXPECT_THAT(err, IsEmpty()); |
| 1108 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); |
| 1109 | } |
| 1110 | |
| 1111 | TEST_F(DumpstateUtilTest, DumpFileMultipleLines) { |
| 1112 | CreateFd("DumpFileMultipleLines.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1113 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1114 | EXPECT_THAT(err, IsEmpty()); |
| 1115 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 1116 | } |
| 1117 | |
| 1118 | TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) { |
| 1119 | CreateFd("DumpFileMultipleLinesWithNewLine.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1120 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1121 | EXPECT_THAT(err, IsEmpty()); |
| 1122 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 1123 | } |
| 1124 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1125 | TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) { |
| 1126 | CreateFd("DumpFileOnDryRun.txt"); |
| 1127 | SetDryRun(true); |
| 1128 | std::string path = kTestDataPath + "single-line.txt"; |
| 1129 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
| 1130 | EXPECT_THAT(err, IsEmpty()); |
| 1131 | EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n")); |
| 1132 | } |
| 1133 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1134 | TEST_F(DumpstateUtilTest, DumpFileOnDryRun) { |
| 1135 | CreateFd("DumpFileOnDryRun.txt"); |
| 1136 | SetDryRun(true); |
| 1137 | std::string path = kTestDataPath + "single-line.txt"; |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1138 | EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1139 | EXPECT_THAT(err, IsEmpty()); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1140 | EXPECT_THAT( |
| 1141 | out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:")); |
| 1142 | EXPECT_THAT(out, EndsWith("skipped on dry run\n")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1143 | } |
Ecco Park | 61ffcf7 | 2016-10-27 15:46:26 -0700 | [diff] [blame] | 1144 | |
| 1145 | TEST_F(DumpstateUtilTest, FindingPidWithExistingProcess) { |
| 1146 | // init process always has pid 1. |
| 1147 | EXPECT_EQ(1, FindPidOfProcess("init")); |
| 1148 | EXPECT_THAT(err, IsEmpty()); |
| 1149 | } |
| 1150 | |
| 1151 | TEST_F(DumpstateUtilTest, FindingPidWithNotExistingProcess) { |
| 1152 | // find the process with abnormal name. |
| 1153 | EXPECT_EQ(-1, FindPidOfProcess("abcdef12345-543")); |
| 1154 | EXPECT_THAT(err, StrEq("can't find the pid\n")); |
| 1155 | } |