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