| 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> | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 39 | #include <cutils/properties.h> | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 40 |  | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 41 | namespace android { | 
|  | 42 | namespace os { | 
|  | 43 | namespace dumpstate { | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 44 |  | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 45 | using ::testing::EndsWith; | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 46 | using ::testing::HasSubstr; | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 47 | using ::testing::IsNull; | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 48 | using ::testing::IsEmpty; | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 49 | using ::testing::NotNull; | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 50 | using ::testing::StrEq; | 
|  | 51 | using ::testing::StartsWith; | 
|  | 52 | using ::testing::Test; | 
|  | 53 | using ::testing::internal::CaptureStderr; | 
|  | 54 | using ::testing::internal::CaptureStdout; | 
|  | 55 | using ::testing::internal::GetCapturedStderr; | 
|  | 56 | using ::testing::internal::GetCapturedStdout; | 
|  | 57 |  | 
| Nandana Dutt | 3f8c717 | 2018-09-25 12:01:54 +0100 | [diff] [blame] | 58 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) | 
|  | 59 |  | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 60 | class DumpstateListenerMock : public IDumpstateListener { | 
|  | 61 | public: | 
| Nandana Dutt | a6a28bd | 2019-01-14 16:54:38 +0000 | [diff] [blame] | 62 | MOCK_METHOD1(onProgress, binder::Status(int32_t progress)); | 
|  | 63 | MOCK_METHOD1(onError, binder::Status(int32_t error_code)); | 
| Nandana Dutt | cc4ead8 | 2019-01-23 08:29:23 +0000 | [diff] [blame] | 64 | MOCK_METHOD0(onFinished, binder::Status()); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 65 | MOCK_METHOD1(onProgressUpdated, binder::Status(int32_t progress)); | 
|  | 66 | MOCK_METHOD1(onMaxProgressUpdated, binder::Status(int32_t max_progress)); | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 67 | MOCK_METHOD4(onSectionComplete, binder::Status(const ::std::string& name, int32_t status, | 
|  | 68 | int32_t size, int32_t durationMs)); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 69 |  | 
|  | 70 | protected: | 
|  | 71 | MOCK_METHOD0(onAsBinder, IBinder*()); | 
|  | 72 | }; | 
|  | 73 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 74 | static int calls_; | 
|  | 75 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 76 | // Base class for all tests in this file | 
|  | 77 | class DumpstateBaseTest : public Test { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 78 | public: | 
|  | 79 | virtual void SetUp() override { | 
|  | 80 | calls_++; | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 81 | SetDryRun(false); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 84 | void SetDryRun(bool dry_run) const { | 
|  | 85 | PropertiesHelper::dry_run_ = dry_run; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | void SetBuildType(const std::string& build_type) const { | 
|  | 89 | PropertiesHelper::build_type_ = build_type; | 
|  | 90 | } | 
|  | 91 |  | 
| Nandana Dutt | 4b392be | 2018-11-02 16:17:05 +0000 | [diff] [blame] | 92 | void SetUnroot(bool unroot) const { | 
|  | 93 | PropertiesHelper::unroot_ = unroot; | 
|  | 94 | } | 
|  | 95 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 96 | bool IsStandalone() const { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 97 | return calls_ == 1; | 
|  | 98 | } | 
|  | 99 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 100 | void DropRoot() const { | 
|  | 101 | DropRootUser(); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 102 | uid_t uid = getuid(); | 
|  | 103 | ASSERT_EQ(2000, (int)uid); | 
|  | 104 | } | 
|  | 105 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 106 | protected: | 
|  | 107 | const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str()); | 
| Dan Shi | e177e8e | 2019-06-20 11:08:14 -0700 | [diff] [blame] | 108 | const std::string kTestDataPath = kTestPath + "/tests/testdata/"; | 
|  | 109 | const std::string kSimpleCommand = kTestPath + "/dumpstate_test_fixture"; | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 110 | const std::string kEchoCommand = "/system/bin/echo"; | 
|  | 111 |  | 
|  | 112 | /* | 
|  | 113 | * Copies a text file fixture to a temporary file, returning it's path. | 
|  | 114 | * | 
|  | 115 | * Useful in cases where the test case changes the content of the tile. | 
|  | 116 | */ | 
|  | 117 | std::string CopyTextFileFixture(const std::string& relative_name) { | 
|  | 118 | std::string from = kTestDataPath + relative_name; | 
|  | 119 | // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it | 
|  | 120 | // around for poking when the test fails. | 
|  | 121 | std::string to = kTestDataPath + relative_name + ".tmp"; | 
|  | 122 | ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str()); | 
|  | 123 | android::base::RemoveFileIfExists(to); | 
|  | 124 | CopyTextFile(from, to); | 
|  | 125 | return to.c_str(); | 
|  | 126 | } | 
|  | 127 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 128 | // Need functions that returns void to use assertions - | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 129 | // 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] | 130 | void ReadFileToString(const std::string& path, std::string* content) { | 
|  | 131 | ASSERT_TRUE(android::base::ReadFileToString(path, content)) | 
|  | 132 | << "could not read contents from " << path; | 
|  | 133 | } | 
|  | 134 | void WriteStringToFile(const std::string& content, const std::string& path) { | 
|  | 135 | ASSERT_TRUE(android::base::WriteStringToFile(content, path)) | 
|  | 136 | << "could not write contents to " << path; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | private: | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 140 | void CopyTextFile(const std::string& from, const std::string& to) { | 
|  | 141 | std::string content; | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 142 | ReadFileToString(from, &content); | 
|  | 143 | WriteStringToFile(content, to); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 144 | } | 
|  | 145 | }; | 
|  | 146 |  | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 147 | class DumpOptionsTest : public Test { | 
|  | 148 | public: | 
|  | 149 | virtual ~DumpOptionsTest() { | 
|  | 150 | } | 
|  | 151 | virtual void SetUp() { | 
|  | 152 | options_ = Dumpstate::DumpOptions(); | 
|  | 153 | } | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 154 | void TearDown() { | 
|  | 155 | // Reset the property | 
|  | 156 | property_set("dumpstate.options", ""); | 
|  | 157 | } | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 158 | Dumpstate::DumpOptions options_; | 
|  | 159 | }; | 
|  | 160 |  | 
|  | 161 | TEST_F(DumpOptionsTest, InitializeNone) { | 
|  | 162 | // clang-format off | 
|  | 163 | char* argv[] = { | 
|  | 164 | const_cast<char*>("dumpstate") | 
|  | 165 | }; | 
|  | 166 | // clang-format on | 
|  | 167 |  | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 168 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 169 |  | 
|  | 170 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
| Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 171 |  | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 172 | EXPECT_FALSE(options_.do_add_date); | 
|  | 173 | EXPECT_FALSE(options_.do_zip_file); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 174 | EXPECT_FALSE(options_.use_socket); | 
|  | 175 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 176 | EXPECT_FALSE(options_.show_header_only); | 
|  | 177 | EXPECT_TRUE(options_.do_vibrate); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 178 | EXPECT_FALSE(options_.do_fb); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 179 | EXPECT_FALSE(options_.do_progress_updates); | 
|  | 180 | EXPECT_FALSE(options_.is_remote_mode); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 181 | EXPECT_FALSE(options_.do_broadcast); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | TEST_F(DumpOptionsTest, InitializeAdbBugreport) { | 
|  | 185 | // clang-format off | 
|  | 186 | char* argv[] = { | 
|  | 187 | const_cast<char*>("dumpstatez"), | 
|  | 188 | const_cast<char*>("-S"), | 
|  | 189 | const_cast<char*>("-d"), | 
|  | 190 | const_cast<char*>("-z"), | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 191 | }; | 
|  | 192 | // clang-format on | 
|  | 193 |  | 
|  | 194 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 195 |  | 
|  | 196 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 197 | EXPECT_TRUE(options_.do_add_date); | 
|  | 198 | EXPECT_TRUE(options_.do_zip_file); | 
|  | 199 | EXPECT_TRUE(options_.use_control_socket); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 200 |  | 
|  | 201 | // Other options retain default values | 
|  | 202 | EXPECT_TRUE(options_.do_vibrate); | 
|  | 203 | EXPECT_FALSE(options_.show_header_only); | 
|  | 204 | EXPECT_FALSE(options_.do_fb); | 
|  | 205 | EXPECT_FALSE(options_.do_progress_updates); | 
|  | 206 | EXPECT_FALSE(options_.is_remote_mode); | 
|  | 207 | EXPECT_FALSE(options_.do_broadcast); | 
|  | 208 | EXPECT_FALSE(options_.use_socket); | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | TEST_F(DumpOptionsTest, InitializeAdbShellBugreport) { | 
|  | 212 | // clang-format off | 
|  | 213 | char* argv[] = { | 
|  | 214 | const_cast<char*>("dumpstate"), | 
|  | 215 | const_cast<char*>("-s"), | 
|  | 216 | }; | 
|  | 217 | // clang-format on | 
|  | 218 |  | 
|  | 219 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 220 |  | 
|  | 221 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 222 | EXPECT_TRUE(options_.use_socket); | 
|  | 223 |  | 
|  | 224 | // Other options retain default values | 
|  | 225 | EXPECT_TRUE(options_.do_vibrate); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 226 | EXPECT_FALSE(options_.do_add_date); | 
|  | 227 | EXPECT_FALSE(options_.do_zip_file); | 
|  | 228 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 229 | EXPECT_FALSE(options_.show_header_only); | 
|  | 230 | EXPECT_FALSE(options_.do_fb); | 
|  | 231 | EXPECT_FALSE(options_.do_progress_updates); | 
|  | 232 | EXPECT_FALSE(options_.is_remote_mode); | 
|  | 233 | EXPECT_FALSE(options_.do_broadcast); | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | TEST_F(DumpOptionsTest, InitializeFullBugReport) { | 
|  | 237 | // clang-format off | 
|  | 238 | char* argv[] = { | 
|  | 239 | const_cast<char*>("bugreport"), | 
|  | 240 | const_cast<char*>("-d"), | 
|  | 241 | const_cast<char*>("-p"), | 
|  | 242 | const_cast<char*>("-B"), | 
|  | 243 | const_cast<char*>("-z"), | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 244 | }; | 
|  | 245 | // clang-format on | 
|  | 246 | property_set("dumpstate.options", "bugreportfull"); | 
|  | 247 |  | 
|  | 248 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 249 |  | 
|  | 250 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 251 | EXPECT_TRUE(options_.do_add_date); | 
|  | 252 | EXPECT_TRUE(options_.do_fb); | 
|  | 253 | EXPECT_TRUE(options_.do_zip_file); | 
| Nandana Dutt | 58d72e2 | 2018-11-16 10:30:48 +0000 | [diff] [blame] | 254 | EXPECT_TRUE(options_.do_broadcast); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 255 |  | 
|  | 256 | // Other options retain default values | 
|  | 257 | EXPECT_TRUE(options_.do_vibrate); | 
|  | 258 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 259 | EXPECT_FALSE(options_.show_header_only); | 
|  | 260 | EXPECT_FALSE(options_.do_progress_updates); | 
|  | 261 | EXPECT_FALSE(options_.is_remote_mode); | 
|  | 262 | EXPECT_FALSE(options_.use_socket); | 
|  | 263 | EXPECT_FALSE(options_.do_start_service); | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | TEST_F(DumpOptionsTest, InitializeInteractiveBugReport) { | 
|  | 267 | // clang-format off | 
|  | 268 | char* argv[] = { | 
|  | 269 | const_cast<char*>("bugreport"), | 
|  | 270 | const_cast<char*>("-d"), | 
|  | 271 | const_cast<char*>("-p"), | 
|  | 272 | const_cast<char*>("-B"), | 
|  | 273 | const_cast<char*>("-z"), | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 274 | }; | 
|  | 275 | // clang-format on | 
|  | 276 |  | 
|  | 277 | property_set("dumpstate.options", "bugreportplus"); | 
|  | 278 |  | 
|  | 279 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 280 |  | 
|  | 281 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 282 | EXPECT_TRUE(options_.do_add_date); | 
|  | 283 | EXPECT_TRUE(options_.do_broadcast); | 
|  | 284 | EXPECT_TRUE(options_.do_zip_file); | 
|  | 285 | EXPECT_TRUE(options_.do_progress_updates); | 
|  | 286 | EXPECT_TRUE(options_.do_start_service); | 
|  | 287 | EXPECT_FALSE(options_.do_fb); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 288 |  | 
|  | 289 | // Other options retain default values | 
|  | 290 | EXPECT_TRUE(options_.do_vibrate); | 
|  | 291 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 292 | EXPECT_FALSE(options_.show_header_only); | 
|  | 293 | EXPECT_FALSE(options_.is_remote_mode); | 
|  | 294 | EXPECT_FALSE(options_.use_socket); | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | TEST_F(DumpOptionsTest, InitializeRemoteBugReport) { | 
|  | 298 | // clang-format off | 
|  | 299 | char* argv[] = { | 
|  | 300 | const_cast<char*>("bugreport"), | 
|  | 301 | const_cast<char*>("-d"), | 
|  | 302 | const_cast<char*>("-p"), | 
|  | 303 | const_cast<char*>("-B"), | 
|  | 304 | const_cast<char*>("-z"), | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 305 | }; | 
|  | 306 | // clang-format on | 
|  | 307 |  | 
|  | 308 | property_set("dumpstate.options", "bugreportremote"); | 
|  | 309 |  | 
|  | 310 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 311 |  | 
|  | 312 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 313 | EXPECT_TRUE(options_.do_add_date); | 
|  | 314 | EXPECT_TRUE(options_.do_broadcast); | 
|  | 315 | EXPECT_TRUE(options_.do_zip_file); | 
|  | 316 | EXPECT_TRUE(options_.is_remote_mode); | 
|  | 317 | EXPECT_FALSE(options_.do_vibrate); | 
|  | 318 | EXPECT_FALSE(options_.do_fb); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 319 |  | 
|  | 320 | // Other options retain default values | 
|  | 321 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 322 | EXPECT_FALSE(options_.show_header_only); | 
|  | 323 | EXPECT_FALSE(options_.do_progress_updates); | 
|  | 324 | EXPECT_FALSE(options_.use_socket); | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | TEST_F(DumpOptionsTest, InitializeWearBugReport) { | 
|  | 328 | // clang-format off | 
|  | 329 | char* argv[] = { | 
|  | 330 | const_cast<char*>("bugreport"), | 
|  | 331 | const_cast<char*>("-d"), | 
|  | 332 | const_cast<char*>("-p"), | 
|  | 333 | const_cast<char*>("-B"), | 
|  | 334 | const_cast<char*>("-z"), | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 335 | }; | 
|  | 336 | // clang-format on | 
|  | 337 |  | 
|  | 338 | property_set("dumpstate.options", "bugreportwear"); | 
|  | 339 |  | 
|  | 340 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 341 |  | 
|  | 342 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 343 | EXPECT_TRUE(options_.do_add_date); | 
|  | 344 | EXPECT_TRUE(options_.do_fb); | 
|  | 345 | EXPECT_TRUE(options_.do_broadcast); | 
|  | 346 | EXPECT_TRUE(options_.do_zip_file); | 
|  | 347 | EXPECT_TRUE(options_.do_progress_updates); | 
|  | 348 | EXPECT_TRUE(options_.do_start_service); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 349 |  | 
|  | 350 | // Other options retain default values | 
|  | 351 | EXPECT_TRUE(options_.do_vibrate); | 
|  | 352 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 353 | EXPECT_FALSE(options_.show_header_only); | 
|  | 354 | EXPECT_FALSE(options_.is_remote_mode); | 
|  | 355 | EXPECT_FALSE(options_.use_socket); | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | TEST_F(DumpOptionsTest, InitializeTelephonyBugReport) { | 
|  | 359 | // clang-format off | 
|  | 360 | char* argv[] = { | 
|  | 361 | const_cast<char*>("bugreport"), | 
|  | 362 | const_cast<char*>("-d"), | 
|  | 363 | const_cast<char*>("-p"), | 
|  | 364 | const_cast<char*>("-B"), | 
|  | 365 | const_cast<char*>("-z"), | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 366 | }; | 
|  | 367 | // clang-format on | 
|  | 368 |  | 
|  | 369 | property_set("dumpstate.options", "bugreporttelephony"); | 
|  | 370 |  | 
|  | 371 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 372 |  | 
|  | 373 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 374 | EXPECT_TRUE(options_.do_add_date); | 
| Abhijeet Kaur | d3cca0d | 2019-03-25 12:04:16 +0000 | [diff] [blame] | 375 | EXPECT_FALSE(options_.do_fb); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 376 | EXPECT_TRUE(options_.do_broadcast); | 
|  | 377 | EXPECT_TRUE(options_.do_zip_file); | 
|  | 378 | EXPECT_TRUE(options_.telephony_only); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 379 |  | 
|  | 380 | // Other options retain default values | 
|  | 381 | EXPECT_TRUE(options_.do_vibrate); | 
|  | 382 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 383 | EXPECT_FALSE(options_.show_header_only); | 
|  | 384 | EXPECT_FALSE(options_.do_progress_updates); | 
|  | 385 | EXPECT_FALSE(options_.is_remote_mode); | 
|  | 386 | EXPECT_FALSE(options_.use_socket); | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | TEST_F(DumpOptionsTest, InitializeWifiBugReport) { | 
|  | 390 | // clang-format off | 
|  | 391 | char* argv[] = { | 
|  | 392 | const_cast<char*>("bugreport"), | 
|  | 393 | const_cast<char*>("-d"), | 
|  | 394 | const_cast<char*>("-p"), | 
|  | 395 | const_cast<char*>("-B"), | 
|  | 396 | const_cast<char*>("-z"), | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 397 | }; | 
|  | 398 | // clang-format on | 
|  | 399 |  | 
|  | 400 | property_set("dumpstate.options", "bugreportwifi"); | 
|  | 401 |  | 
|  | 402 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 403 |  | 
|  | 404 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 405 | EXPECT_TRUE(options_.do_add_date); | 
| Abhijeet Kaur | d3cca0d | 2019-03-25 12:04:16 +0000 | [diff] [blame] | 406 | EXPECT_FALSE(options_.do_fb); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 407 | EXPECT_TRUE(options_.do_broadcast); | 
|  | 408 | EXPECT_TRUE(options_.do_zip_file); | 
|  | 409 | EXPECT_TRUE(options_.wifi_only); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 410 |  | 
|  | 411 | // Other options retain default values | 
|  | 412 | EXPECT_TRUE(options_.do_vibrate); | 
|  | 413 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 414 | EXPECT_FALSE(options_.show_header_only); | 
|  | 415 | EXPECT_FALSE(options_.do_progress_updates); | 
|  | 416 | EXPECT_FALSE(options_.is_remote_mode); | 
|  | 417 | EXPECT_FALSE(options_.use_socket); | 
|  | 418 | } | 
|  | 419 |  | 
|  | 420 | TEST_F(DumpOptionsTest, InitializeDefaultBugReport) { | 
|  | 421 | // default: commandline options are not overridden | 
|  | 422 | // clang-format off | 
|  | 423 | char* argv[] = { | 
|  | 424 | const_cast<char*>("bugreport"), | 
|  | 425 | const_cast<char*>("-d"), | 
|  | 426 | const_cast<char*>("-p"), | 
|  | 427 | const_cast<char*>("-B"), | 
|  | 428 | const_cast<char*>("-z"), | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 429 | }; | 
|  | 430 | // clang-format on | 
|  | 431 |  | 
|  | 432 | property_set("dumpstate.options", ""); | 
|  | 433 |  | 
|  | 434 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 435 |  | 
|  | 436 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 437 | EXPECT_TRUE(options_.do_add_date); | 
|  | 438 | EXPECT_TRUE(options_.do_fb); | 
|  | 439 | EXPECT_TRUE(options_.do_zip_file); | 
|  | 440 | EXPECT_TRUE(options_.do_broadcast); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 441 |  | 
|  | 442 | // Other options retain default values | 
|  | 443 | EXPECT_TRUE(options_.do_vibrate); | 
|  | 444 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 445 | EXPECT_FALSE(options_.show_header_only); | 
|  | 446 | EXPECT_FALSE(options_.do_progress_updates); | 
|  | 447 | EXPECT_FALSE(options_.is_remote_mode); | 
|  | 448 | EXPECT_FALSE(options_.use_socket); | 
|  | 449 | EXPECT_FALSE(options_.wifi_only); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 450 | } | 
|  | 451 |  | 
|  | 452 | TEST_F(DumpOptionsTest, InitializePartial1) { | 
|  | 453 | // clang-format off | 
|  | 454 | char* argv[] = { | 
|  | 455 | const_cast<char*>("dumpstate"), | 
|  | 456 | const_cast<char*>("-d"), | 
|  | 457 | const_cast<char*>("-z"), | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 458 | const_cast<char*>("-s"), | 
|  | 459 | const_cast<char*>("-S"), | 
|  | 460 |  | 
|  | 461 | }; | 
|  | 462 | // clang-format on | 
|  | 463 |  | 
|  | 464 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 465 |  | 
|  | 466 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 467 | EXPECT_TRUE(options_.do_add_date); | 
|  | 468 | EXPECT_TRUE(options_.do_zip_file); | 
|  | 469 | // TODO: Maybe we should trim the filename | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 470 | EXPECT_TRUE(options_.use_socket); | 
|  | 471 | EXPECT_TRUE(options_.use_control_socket); | 
|  | 472 |  | 
|  | 473 | // Other options retain default values | 
|  | 474 | EXPECT_FALSE(options_.show_header_only); | 
|  | 475 | EXPECT_TRUE(options_.do_vibrate); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 476 | EXPECT_FALSE(options_.do_fb); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 477 | EXPECT_FALSE(options_.do_progress_updates); | 
|  | 478 | EXPECT_FALSE(options_.is_remote_mode); | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 479 | EXPECT_FALSE(options_.do_broadcast); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 480 | } | 
|  | 481 |  | 
|  | 482 | TEST_F(DumpOptionsTest, InitializePartial2) { | 
|  | 483 | // clang-format off | 
|  | 484 | char* argv[] = { | 
|  | 485 | const_cast<char*>("dumpstate"), | 
|  | 486 | const_cast<char*>("-v"), | 
|  | 487 | const_cast<char*>("-q"), | 
|  | 488 | const_cast<char*>("-p"), | 
|  | 489 | const_cast<char*>("-P"), | 
|  | 490 | const_cast<char*>("-R"), | 
|  | 491 | const_cast<char*>("-B"), | 
|  | 492 | }; | 
|  | 493 | // clang-format on | 
|  | 494 |  | 
|  | 495 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 496 |  | 
|  | 497 | EXPECT_EQ(status, Dumpstate::RunStatus::OK); | 
|  | 498 | EXPECT_TRUE(options_.show_header_only); | 
|  | 499 | EXPECT_FALSE(options_.do_vibrate); | 
|  | 500 | EXPECT_TRUE(options_.do_fb); | 
|  | 501 | EXPECT_TRUE(options_.do_progress_updates); | 
|  | 502 | EXPECT_TRUE(options_.is_remote_mode); | 
|  | 503 | EXPECT_TRUE(options_.do_broadcast); | 
|  | 504 |  | 
|  | 505 | // Other options retain default values | 
|  | 506 | EXPECT_FALSE(options_.do_add_date); | 
|  | 507 | EXPECT_FALSE(options_.do_zip_file); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 508 | EXPECT_FALSE(options_.use_socket); | 
|  | 509 | EXPECT_FALSE(options_.use_control_socket); | 
|  | 510 | } | 
|  | 511 |  | 
|  | 512 | TEST_F(DumpOptionsTest, InitializeHelp) { | 
|  | 513 | // clang-format off | 
|  | 514 | char* argv[] = { | 
|  | 515 | const_cast<char*>("dumpstate"), | 
|  | 516 | const_cast<char*>("-h") | 
|  | 517 | }; | 
|  | 518 | // clang-format on | 
|  | 519 |  | 
|  | 520 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 521 |  | 
|  | 522 | // -h is for help. | 
|  | 523 | EXPECT_EQ(status, Dumpstate::RunStatus::HELP); | 
|  | 524 | } | 
|  | 525 |  | 
|  | 526 | TEST_F(DumpOptionsTest, InitializeUnknown) { | 
|  | 527 | // clang-format off | 
|  | 528 | char* argv[] = { | 
|  | 529 | const_cast<char*>("dumpstate"), | 
|  | 530 | const_cast<char*>("-u")  // unknown flag | 
|  | 531 | }; | 
|  | 532 | // clang-format on | 
|  | 533 |  | 
|  | 534 | Dumpstate::RunStatus status = options_.Initialize(ARRAY_SIZE(argv), argv); | 
|  | 535 |  | 
|  | 536 | // -u is unknown. | 
|  | 537 | EXPECT_EQ(status, Dumpstate::RunStatus::INVALID_INPUT); | 
|  | 538 | } | 
|  | 539 |  | 
|  | 540 | TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile1) { | 
|  | 541 | options_.do_zip_file = true; | 
| Nandana Dutt | 9a76d20 | 2019-01-21 15:56:48 +0000 | [diff] [blame] | 542 | // Writing to socket = !writing to file. | 
|  | 543 | options_.use_socket = true; | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 544 | EXPECT_FALSE(options_.ValidateOptions()); | 
| Nandana Dutt | 9a76d20 | 2019-01-21 15:56:48 +0000 | [diff] [blame] | 545 |  | 
|  | 546 | options_.use_socket = false; | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 547 | EXPECT_TRUE(options_.ValidateOptions()); | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | TEST_F(DumpOptionsTest, ValidateOptionsNeedOutfile2) { | 
|  | 551 | options_.do_broadcast = true; | 
| Nandana Dutt | 9a76d20 | 2019-01-21 15:56:48 +0000 | [diff] [blame] | 552 | // Writing to socket = !writing to file. | 
|  | 553 | options_.use_socket = true; | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 554 | EXPECT_FALSE(options_.ValidateOptions()); | 
| Nandana Dutt | 9a76d20 | 2019-01-21 15:56:48 +0000 | [diff] [blame] | 555 |  | 
|  | 556 | options_.use_socket = false; | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 557 | EXPECT_TRUE(options_.ValidateOptions()); | 
|  | 558 | } | 
|  | 559 |  | 
|  | 560 | TEST_F(DumpOptionsTest, ValidateOptionsNeedZipfile) { | 
|  | 561 | options_.use_control_socket = true; | 
|  | 562 | EXPECT_FALSE(options_.ValidateOptions()); | 
|  | 563 |  | 
|  | 564 | options_.do_zip_file = true; | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 565 | EXPECT_TRUE(options_.ValidateOptions()); | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | TEST_F(DumpOptionsTest, ValidateOptionsUpdateProgressNeedsBroadcast) { | 
|  | 569 | options_.do_progress_updates = true; | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 570 | EXPECT_FALSE(options_.ValidateOptions()); | 
|  | 571 |  | 
|  | 572 | options_.do_broadcast = true; | 
|  | 573 | EXPECT_TRUE(options_.ValidateOptions()); | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | TEST_F(DumpOptionsTest, ValidateOptionsRemoteMode) { | 
|  | 577 | options_.is_remote_mode = true; | 
|  | 578 | EXPECT_FALSE(options_.ValidateOptions()); | 
|  | 579 |  | 
|  | 580 | options_.do_broadcast = true; | 
|  | 581 | options_.do_zip_file = true; | 
|  | 582 | options_.do_add_date = true; | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 583 | EXPECT_TRUE(options_.ValidateOptions()); | 
|  | 584 | } | 
|  | 585 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 586 | class DumpstateTest : public DumpstateBaseTest { | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 587 | public: | 
|  | 588 | void SetUp() { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 589 | DumpstateBaseTest::SetUp(); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 590 | SetDryRun(false); | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 591 | SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)")); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 592 | ds.progress_.reset(new Progress()); | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 593 | ds.update_progress_threshold_ = 0; | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 594 | ds.options_.reset(new Dumpstate::DumpOptions()); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 595 | } | 
|  | 596 |  | 
|  | 597 | // Runs a command and capture `stdout` and `stderr`. | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 598 | 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] | 599 | const CommandOptions& options = CommandOptions::DEFAULT) { | 
|  | 600 | CaptureStdout(); | 
|  | 601 | CaptureStderr(); | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 602 | int status = ds.RunCommand(title, full_command, options); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 603 | out = GetCapturedStdout(); | 
|  | 604 | err = GetCapturedStderr(); | 
|  | 605 | return status; | 
|  | 606 | } | 
|  | 607 |  | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 608 | // Dumps a file and capture `stdout` and `stderr`. | 
|  | 609 | int DumpFile(const std::string& title, const std::string& path) { | 
|  | 610 | CaptureStdout(); | 
|  | 611 | CaptureStderr(); | 
|  | 612 | int status = ds.DumpFile(title, path); | 
|  | 613 | out = GetCapturedStdout(); | 
|  | 614 | err = GetCapturedStderr(); | 
|  | 615 | return status; | 
|  | 616 | } | 
|  | 617 |  | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 618 | void SetProgress(long progress, long initial_max, long threshold = 0) { | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 619 | ds.options_->do_progress_updates = true; | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 620 | ds.update_progress_threshold_ = threshold; | 
|  | 621 | ds.last_updated_progress_ = 0; | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 622 | ds.progress_.reset(new Progress(initial_max, progress, 1.2)); | 
|  | 623 | } | 
|  | 624 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 625 | std::string GetProgressMessage(const std::string& listener_name, int progress, int max, | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 626 | int old_max = 0, bool update_progress = true) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 627 | EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress"; | 
|  | 628 | EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max"; | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 629 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 630 | bool max_increased = old_max > 0; | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 631 |  | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 632 | std::string message = ""; | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 633 | if (max_increased) { | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 634 | message = | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 635 | 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] | 636 | } | 
|  | 637 |  | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 638 | if (update_progress) { | 
| Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 639 | message += android::base::StringPrintf("Setting progress (%s): %d/%d (%d%%)\n", | 
|  | 640 | listener_name.c_str(), progress, max, | 
|  | 641 | (100 * progress / max)); | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 642 | } | 
|  | 643 |  | 
|  | 644 | return message; | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 645 | } | 
|  | 646 |  | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 647 | // `stdout` and `stderr` from the last command ran. | 
|  | 648 | std::string out, err; | 
|  | 649 |  | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 650 | Dumpstate& ds = Dumpstate::GetInstance(); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 651 | }; | 
|  | 652 |  | 
|  | 653 | TEST_F(DumpstateTest, RunCommandNoArgs) { | 
|  | 654 | EXPECT_EQ(-1, RunCommand("", {})); | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | TEST_F(DumpstateTest, RunCommandNoTitle) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 658 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 659 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 660 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 661 | } | 
|  | 662 |  | 
|  | 663 | TEST_F(DumpstateTest, RunCommandWithTitle) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 664 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 665 | EXPECT_THAT(err, StrEq("stderr\n")); | 
| Greg Kaiser | 3a811c1 | 2019-05-21 12:48:59 -0700 | [diff] [blame] | 666 | // The duration may not get output, depending on how long it takes, | 
|  | 667 | // so we just check the prefix. | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 668 | EXPECT_THAT(out, | 
| Nandana Dutt | 47527b5 | 2019-03-29 15:34:36 +0000 | [diff] [blame] | 669 | StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n")); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 670 | } | 
|  | 671 |  | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 672 | TEST_F(DumpstateTest, RunCommandWithLoggingMessage) { | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 673 | EXPECT_EQ( | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 674 | 0, RunCommand("", {kSimpleCommand}, | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 675 | CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build())); | 
|  | 676 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 677 | EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n")); | 
|  | 678 | } | 
|  | 679 |  | 
|  | 680 | TEST_F(DumpstateTest, RunCommandRedirectStderr) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 681 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 682 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 683 | EXPECT_THAT(out, IsEmpty()); | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 684 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 685 | } | 
|  | 686 |  | 
|  | 687 | TEST_F(DumpstateTest, RunCommandWithOneArg) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 688 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"})); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 689 | EXPECT_THAT(err, IsEmpty()); | 
|  | 690 | EXPECT_THAT(out, StrEq("one\n")); | 
|  | 691 | } | 
|  | 692 |  | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 693 | TEST_F(DumpstateTest, RunCommandWithMultipleArgs) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 694 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"})); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 695 | EXPECT_THAT(err, IsEmpty()); | 
|  | 696 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); | 
|  | 697 | } | 
|  | 698 |  | 
|  | 699 | TEST_F(DumpstateTest, RunCommandDryRun) { | 
|  | 700 | SetDryRun(true); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 701 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); | 
| Greg Kaiser | 3a811c1 | 2019-05-21 12:48:59 -0700 | [diff] [blame] | 702 | // The duration may not get output, depending on how long it takes, | 
|  | 703 | // so we just check the prefix. | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 704 | EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand + | 
| Nandana Dutt | 47527b5 | 2019-03-29 15:34:36 +0000 | [diff] [blame] | 705 | ") ------\n\t(skipped on dry run)\n")); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 706 | EXPECT_THAT(err, IsEmpty()); | 
|  | 707 | } | 
|  | 708 |  | 
|  | 709 | TEST_F(DumpstateTest, RunCommandDryRunNoTitle) { | 
|  | 710 | SetDryRun(true); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 711 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 712 | EXPECT_THAT(out, IsEmpty()); | 
|  | 713 | EXPECT_THAT(err, IsEmpty()); | 
|  | 714 | } | 
|  | 715 |  | 
|  | 716 | TEST_F(DumpstateTest, RunCommandDryRunAlways) { | 
|  | 717 | SetDryRun(true); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 718 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build())); | 
| Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 719 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 720 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 721 | } | 
|  | 722 |  | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 723 | TEST_F(DumpstateTest, RunCommandNotFound) { | 
|  | 724 | EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"})); | 
|  | 725 | EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code")); | 
|  | 726 | EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed")); | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | TEST_F(DumpstateTest, RunCommandFails) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 730 | EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"})); | 
|  | 731 | EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand + | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 732 | " --exit 42' failed: exit code 42\n")); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 733 | EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand + | 
| Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 734 | " --exit 42' failed: exit code 42\n")); | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 735 | } | 
|  | 736 |  | 
|  | 737 | TEST_F(DumpstateTest, RunCommandCrashes) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 738 | EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"})); | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 739 | // We don't know the exit code, so check just the prefix. | 
|  | 740 | EXPECT_THAT( | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 741 | out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 742 | EXPECT_THAT( | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 743 | err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 744 | } | 
|  | 745 |  | 
|  | 746 | TEST_F(DumpstateTest, RunCommandTimesout) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 747 | EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"}, | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 748 | CommandOptions::WithTimeout(1).Build())); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 749 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand + | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 750 | " --sleep 2' timed out after 1")); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 751 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand + | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 752 | " --sleep 2' timed out after 1")); | 
|  | 753 | } | 
|  | 754 |  | 
|  | 755 | TEST_F(DumpstateTest, RunCommandIsKilled) { | 
|  | 756 | CaptureStdout(); | 
|  | 757 | CaptureStderr(); | 
|  | 758 |  | 
|  | 759 | std::thread t([=]() { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 760 | EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"}, | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 761 | CommandOptions::WithTimeout(100).Always().Build())); | 
|  | 762 | }); | 
|  | 763 |  | 
|  | 764 | // Capture pid and pre-sleep output. | 
|  | 765 | sleep(1);  // Wait a little bit to make sure pid and 1st line were printed. | 
|  | 766 | std::string err = GetCapturedStderr(); | 
|  | 767 | EXPECT_THAT(err, StrEq("sleeping for 20s\n")); | 
|  | 768 |  | 
|  | 769 | std::string out = GetCapturedStdout(); | 
|  | 770 | std::vector<std::string> lines = android::base::Split(out, "\n"); | 
|  | 771 | ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out; | 
|  | 772 |  | 
|  | 773 | int pid = atoi(lines[0].c_str()); | 
|  | 774 | EXPECT_THAT(lines[1], StrEq("stdout line1")); | 
|  | 775 | EXPECT_THAT(lines[2], IsEmpty());  // \n | 
|  | 776 |  | 
|  | 777 | // Then kill the process. | 
|  | 778 | CaptureStdout(); | 
|  | 779 | CaptureStderr(); | 
|  | 780 | ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid; | 
|  | 781 | t.join(); | 
|  | 782 |  | 
|  | 783 | // Finally, check output after murder. | 
|  | 784 | out = GetCapturedStdout(); | 
|  | 785 | err = GetCapturedStderr(); | 
|  | 786 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 787 | EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand + | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 788 | " --pid --sleep 20' failed: killed by signal 15\n")); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 789 | EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand + | 
| Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 790 | " --pid --sleep 20' failed: killed by signal 15\n")); | 
|  | 791 | } | 
|  | 792 |  | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 793 | TEST_F(DumpstateTest, RunCommandProgress) { | 
|  | 794 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); | 
|  | 795 | ds.listener_ = listener; | 
|  | 796 | ds.listener_name_ = "FoxMulder"; | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 797 | SetProgress(0, 30); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 798 |  | 
|  | 799 | EXPECT_CALL(*listener, onProgressUpdated(20)); | 
| Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 800 | EXPECT_CALL(*listener, onProgress(66));  // 20/30 % | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 801 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build())); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 802 | std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30); | 
|  | 803 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 804 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); | 
|  | 805 |  | 
|  | 806 | EXPECT_CALL(*listener, onProgressUpdated(30)); | 
| Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 807 | EXPECT_CALL(*listener, onProgress(100));  // 35/35 % | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 808 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Build())); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 809 | progress_message = GetProgressMessage(ds.listener_name_, 30, 30); | 
|  | 810 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 811 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); | 
|  | 812 |  | 
|  | 813 | // Run a command that will increase maximum timeout. | 
|  | 814 | EXPECT_CALL(*listener, onProgressUpdated(31)); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 815 | EXPECT_CALL(*listener, onMaxProgressUpdated(37)); | 
| Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 816 | EXPECT_CALL(*listener, onProgress(83));  // 31/37 % | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 817 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build())); | 
|  | 818 | progress_message = GetProgressMessage(ds.listener_name_, 31, 37, 30);  // 20% increase | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 819 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 820 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); | 
|  | 821 |  | 
|  | 822 | // Make sure command ran while in dry_run is counted. | 
|  | 823 | SetDryRun(true); | 
|  | 824 | EXPECT_CALL(*listener, onProgressUpdated(35)); | 
| Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 825 | EXPECT_CALL(*listener, onProgress(94));  // 35/37 % | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 826 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build())); | 
|  | 827 | progress_message = GetProgressMessage(ds.listener_name_, 35, 37); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 828 | EXPECT_THAT(out, IsEmpty()); | 
|  | 829 | EXPECT_THAT(err, StrEq(progress_message)); | 
|  | 830 |  | 
|  | 831 | ds.listener_.clear(); | 
|  | 832 | } | 
|  | 833 |  | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 834 | TEST_F(DumpstateTest, RunCommandProgressIgnoreThreshold) { | 
|  | 835 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); | 
|  | 836 | ds.listener_ = listener; | 
|  | 837 | ds.listener_name_ = "FoxMulder"; | 
|  | 838 | SetProgress(0, 8, 5);  // 8 max, 5 threshold | 
|  | 839 |  | 
|  | 840 | // First update should always be sent. | 
|  | 841 | EXPECT_CALL(*listener, onProgressUpdated(1)); | 
| Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 842 | EXPECT_CALL(*listener, onProgress(12));  // 1/12 % | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 843 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build())); | 
|  | 844 | std::string progress_message = GetProgressMessage(ds.listener_name_, 1, 8); | 
|  | 845 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 846 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); | 
|  | 847 |  | 
|  | 848 | // Fourth update should be ignored because it's between the threshold (5 -1 = 4 < 5). | 
|  | 849 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build())); | 
|  | 850 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 851 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 852 |  | 
|  | 853 | // Third update should be sent because it reaches threshold (6 - 1 = 5). | 
|  | 854 | EXPECT_CALL(*listener, onProgressUpdated(6)); | 
| Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 855 | EXPECT_CALL(*listener, onProgress(75));  // 6/8 % | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 856 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build())); | 
|  | 857 | progress_message = GetProgressMessage(ds.listener_name_, 6, 8); | 
|  | 858 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 859 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); | 
|  | 860 |  | 
|  | 861 | // Fourth update should be ignored because it's between the threshold (9 - 6 = 3 < 5). | 
|  | 862 | // But max update should be sent. | 
|  | 863 | EXPECT_CALL(*listener, onMaxProgressUpdated(10));  // 9 * 120% = 10.8 = 10 | 
|  | 864 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build())); | 
|  | 865 | progress_message = GetProgressMessage(ds.listener_name_, 9, 10, 8, false); | 
|  | 866 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 867 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); | 
|  | 868 |  | 
|  | 869 | ds.listener_.clear(); | 
|  | 870 | } | 
|  | 871 |  | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 872 | TEST_F(DumpstateTest, RunCommandDropRoot) { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 873 | if (!IsStandalone()) { | 
|  | 874 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 875 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 876 | MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n") | 
|  | 877 | return; | 
|  | 878 | } | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 879 | // First check root case - only available when running with 'adb root'. | 
|  | 880 | uid_t uid = getuid(); | 
|  | 881 | if (uid == 0) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 882 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"})); | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 883 | EXPECT_THAT(out, StrEq("0\nstdout\n")); | 
|  | 884 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 885 | return; | 
|  | 886 | } | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 887 | // Then run dropping root. | 
|  | 888 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 889 | CommandOptions::WithTimeout(1).DropRoot().Build())); | 
|  | 890 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); | 
| Felipe Leme | 26c4157 | 2016-10-06 14:34:43 -0700 | [diff] [blame] | 891 | 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] | 892 | } | 
|  | 893 |  | 
|  | 894 | TEST_F(DumpstateTest, RunCommandAsRootUserBuild) { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 895 | if (!IsStandalone()) { | 
|  | 896 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 897 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 898 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n") | 
|  | 899 | return; | 
|  | 900 | } | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 901 | if (!PropertiesHelper::IsUserBuild()) { | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 902 | // Emulates user build if necessarily. | 
|  | 903 | SetBuildType("user"); | 
|  | 904 | } | 
|  | 905 |  | 
|  | 906 | DropRoot(); | 
|  | 907 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 908 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build())); | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 909 |  | 
|  | 910 | // We don't know the exact path of su, so we just check for the 'root ...' commands | 
|  | 911 | EXPECT_THAT(out, StartsWith("Skipping")); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 912 | EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n")); | 
| Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 913 | EXPECT_THAT(err, IsEmpty()); | 
|  | 914 | } | 
|  | 915 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 916 | TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) { | 
|  | 917 | if (!IsStandalone()) { | 
|  | 918 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 919 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 920 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n") | 
|  | 921 | return; | 
|  | 922 | } | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 923 | if (PropertiesHelper::IsUserBuild()) { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 924 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); | 
|  | 925 | return; | 
|  | 926 | } | 
|  | 927 |  | 
|  | 928 | DropRoot(); | 
|  | 929 |  | 
|  | 930 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
|  | 931 | CommandOptions::WithTimeout(1).AsRoot().Build())); | 
|  | 932 |  | 
|  | 933 | EXPECT_THAT(out, StrEq("0\nstdout\n")); | 
|  | 934 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 935 | } | 
|  | 936 |  | 
| Nandana Dutt | 4b392be | 2018-11-02 16:17:05 +0000 | [diff] [blame] | 937 | TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild_withUnroot) { | 
|  | 938 | if (!IsStandalone()) { | 
|  | 939 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 940 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 941 | MYLOGE( | 
|  | 942 | "Skipping DumpstateTest.RunCommandAsRootNonUserBuild_withUnroot() " | 
|  | 943 | "on test suite\n") | 
|  | 944 | return; | 
|  | 945 | } | 
|  | 946 | if (PropertiesHelper::IsUserBuild()) { | 
|  | 947 | ALOGI("Skipping RunCommandAsRootNonUserBuild_withUnroot on user builds\n"); | 
|  | 948 | return; | 
|  | 949 | } | 
|  | 950 |  | 
|  | 951 | // Same test as above, but with unroot property set, which will override su availability. | 
|  | 952 | SetUnroot(true); | 
|  | 953 | DropRoot(); | 
|  | 954 |  | 
|  | 955 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
|  | 956 | CommandOptions::WithTimeout(1).AsRoot().Build())); | 
|  | 957 |  | 
|  | 958 | // AsRoot is ineffective. | 
|  | 959 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); | 
|  | 960 | EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n")); | 
|  | 961 | } | 
|  | 962 |  | 
| Yifan Hong | 48e83a1 | 2017-10-03 14:10:07 -0700 | [diff] [blame] | 963 | TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) { | 
|  | 964 | if (!IsStandalone()) { | 
|  | 965 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 966 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 967 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n") | 
|  | 968 | return; | 
|  | 969 | } | 
|  | 970 | if (!PropertiesHelper::IsUserBuild()) { | 
|  | 971 | // Emulates user build if necessarily. | 
|  | 972 | SetBuildType("user"); | 
|  | 973 | } | 
|  | 974 |  | 
|  | 975 | DropRoot(); | 
|  | 976 |  | 
|  | 977 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
|  | 978 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); | 
|  | 979 |  | 
|  | 980 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); | 
|  | 981 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 982 | } | 
|  | 983 |  | 
|  | 984 | TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) { | 
|  | 985 | if (!IsStandalone()) { | 
|  | 986 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 987 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 988 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n") | 
|  | 989 | return; | 
|  | 990 | } | 
|  | 991 | if (PropertiesHelper::IsUserBuild()) { | 
|  | 992 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); | 
|  | 993 | return; | 
|  | 994 | } | 
|  | 995 |  | 
|  | 996 | DropRoot(); | 
|  | 997 |  | 
|  | 998 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
|  | 999 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); | 
|  | 1000 |  | 
|  | 1001 | EXPECT_THAT(out, StrEq("0\nstdout\n")); | 
|  | 1002 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 1003 | } | 
|  | 1004 |  | 
| Nandana Dutt | 4b392be | 2018-11-02 16:17:05 +0000 | [diff] [blame] | 1005 | TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild_withUnroot) { | 
|  | 1006 | if (!IsStandalone()) { | 
|  | 1007 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 1008 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 1009 | MYLOGE( | 
|  | 1010 | "Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild_withUnroot() " | 
|  | 1011 | "on test suite\n") | 
|  | 1012 | return; | 
|  | 1013 | } | 
|  | 1014 | if (PropertiesHelper::IsUserBuild()) { | 
|  | 1015 | ALOGI("Skipping RunCommandAsRootIfAvailableOnDebugBuild_withUnroot on user builds\n"); | 
|  | 1016 | return; | 
|  | 1017 | } | 
|  | 1018 | // Same test as above, but with unroot property set, which will override su availability. | 
|  | 1019 | SetUnroot(true); | 
|  | 1020 |  | 
|  | 1021 | DropRoot(); | 
|  | 1022 |  | 
|  | 1023 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
|  | 1024 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); | 
|  | 1025 |  | 
|  | 1026 | // It's a userdebug build, so "su root" should be available, but unroot=true overrides it. | 
|  | 1027 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); | 
|  | 1028 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 1029 | } | 
|  | 1030 |  | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 1031 | TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) { | 
|  | 1032 | EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist")); | 
|  | 1033 | EXPECT_THAT(out, | 
|  | 1034 | StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n")); | 
|  | 1035 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1036 | } | 
|  | 1037 |  | 
|  | 1038 | TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) { | 
|  | 1039 | EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist")); | 
|  | 1040 | EXPECT_THAT(err, IsEmpty()); | 
| Greg Kaiser | 3a811c1 | 2019-05-21 12:48:59 -0700 | [diff] [blame] | 1041 | // The duration may not get output, depending on how long it takes, | 
|  | 1042 | // so we just check the prefix. | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 1043 | EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No " | 
|  | 1044 | "such file or directory\n")); | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 1045 | } | 
|  | 1046 |  | 
|  | 1047 | TEST_F(DumpstateTest, DumpFileSingleLine) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1048 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 1049 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1050 | EXPECT_THAT(out, StrEq("I AM LINE1\n"));  // dumpstate adds missing newline | 
|  | 1051 | } | 
|  | 1052 |  | 
|  | 1053 | TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1054 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt")); | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 1055 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1056 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); | 
|  | 1057 | } | 
|  | 1058 |  | 
|  | 1059 | TEST_F(DumpstateTest, DumpFileMultipleLines) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1060 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt")); | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 1061 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1062 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); | 
|  | 1063 | } | 
|  | 1064 |  | 
|  | 1065 | TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1066 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt")); | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 1067 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1068 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); | 
|  | 1069 | } | 
|  | 1070 |  | 
|  | 1071 | TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) { | 
|  | 1072 | SetDryRun(true); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1073 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 1074 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1075 | EXPECT_THAT(out, IsEmpty()); | 
|  | 1076 | } | 
|  | 1077 |  | 
|  | 1078 | TEST_F(DumpstateTest, DumpFileOnDryRun) { | 
|  | 1079 | SetDryRun(true); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1080 | 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] | 1081 | EXPECT_THAT(err, IsEmpty()); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1082 | EXPECT_THAT( | 
|  | 1083 | out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:")); | 
| Nandana Dutt | 47527b5 | 2019-03-29 15:34:36 +0000 | [diff] [blame] | 1084 | EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n")); | 
| Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 1085 | } | 
|  | 1086 |  | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1087 | TEST_F(DumpstateTest, DumpFileUpdateProgress) { | 
|  | 1088 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); | 
|  | 1089 | ds.listener_ = listener; | 
|  | 1090 | ds.listener_name_ = "FoxMulder"; | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1091 | SetProgress(0, 30); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1092 |  | 
|  | 1093 | EXPECT_CALL(*listener, onProgressUpdated(5)); | 
| Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 1094 | EXPECT_CALL(*listener, onProgress(16));  // 5/30 % | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1095 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1096 |  | 
|  | 1097 | std::string progress_message = | 
|  | 1098 | GetProgressMessage(ds.listener_name_, 5, 30);  // TODO: unhardcode WEIGHT_FILE (5)? | 
|  | 1099 | EXPECT_THAT(err, StrEq(progress_message)); | 
|  | 1100 | EXPECT_THAT(out, StrEq("I AM LINE1\n"));  // dumpstate adds missing newline | 
|  | 1101 |  | 
|  | 1102 | ds.listener_.clear(); | 
|  | 1103 | } | 
|  | 1104 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1105 | class DumpstateServiceTest : public DumpstateBaseTest { | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1106 | public: | 
|  | 1107 | DumpstateService dss; | 
|  | 1108 | }; | 
|  | 1109 |  | 
|  | 1110 | TEST_F(DumpstateServiceTest, SetListenerNoName) { | 
|  | 1111 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1112 | sp<IDumpstateToken> token; | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 1113 | EXPECT_TRUE(dss.setListener("", listener, /* getSectionDetails = */ false, &token).isOk()); | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1114 | ASSERT_THAT(token, IsNull()); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1115 | } | 
|  | 1116 |  | 
|  | 1117 | TEST_F(DumpstateServiceTest, SetListenerNoPointer) { | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1118 | sp<IDumpstateToken> token; | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 1119 | EXPECT_TRUE( | 
|  | 1120 | dss.setListener("whatever", nullptr, /* getSectionDetails = */ false, &token).isOk()); | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1121 | ASSERT_THAT(token, IsNull()); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1122 | } | 
|  | 1123 |  | 
|  | 1124 | TEST_F(DumpstateServiceTest, SetListenerTwice) { | 
|  | 1125 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1126 | sp<IDumpstateToken> token; | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 1127 | EXPECT_TRUE( | 
|  | 1128 | dss.setListener("whatever", listener, /* getSectionDetails = */ false, &token).isOk()); | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1129 | ASSERT_THAT(token, NotNull()); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1130 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 1131 | EXPECT_FALSE(Dumpstate::GetInstance().report_section_); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1132 |  | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1133 | token.clear(); | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 1134 | EXPECT_TRUE( | 
|  | 1135 | dss.setListener("whatsoever", listener, /* getSectionDetails = */ false, &token).isOk()); | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 1136 | ASSERT_THAT(token, IsNull()); | 
|  | 1137 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 1138 | EXPECT_FALSE(Dumpstate::GetInstance().report_section_); | 
|  | 1139 | } | 
|  | 1140 |  | 
|  | 1141 | TEST_F(DumpstateServiceTest, SetListenerWithSectionDetails) { | 
|  | 1142 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); | 
|  | 1143 | sp<IDumpstateToken> token; | 
|  | 1144 | Dumpstate::GetInstance().listener_ = nullptr; | 
|  | 1145 | EXPECT_TRUE( | 
|  | 1146 | dss.setListener("whatever", listener, /* getSectionDetails = */ true, &token).isOk()); | 
|  | 1147 | ASSERT_THAT(token, NotNull()); | 
|  | 1148 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); | 
|  | 1149 | EXPECT_TRUE(Dumpstate::GetInstance().report_section_); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 1150 | } | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1151 |  | 
|  | 1152 | class ProgressTest : public DumpstateBaseTest { | 
|  | 1153 | public: | 
|  | 1154 | Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") { | 
|  | 1155 | return Progress(max, growth_factor, path); | 
|  | 1156 | } | 
|  | 1157 |  | 
|  | 1158 | void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) { | 
|  | 1159 | std::string expected_content = | 
|  | 1160 | android::base::StringPrintf("%d %d\n", expected_runs, expected_average); | 
|  | 1161 | std::string actual_content; | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1162 | ReadFileToString(path, &actual_content); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1163 | ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path; | 
|  | 1164 | } | 
|  | 1165 | }; | 
|  | 1166 |  | 
|  | 1167 | TEST_F(ProgressTest, SimpleTest) { | 
|  | 1168 | Progress progress; | 
|  | 1169 | EXPECT_EQ(0, progress.Get()); | 
|  | 1170 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); | 
|  | 1171 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1172 |  | 
|  | 1173 | bool max_increased = progress.Inc(1); | 
|  | 1174 | EXPECT_EQ(1, progress.Get()); | 
|  | 1175 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); | 
|  | 1176 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1177 | EXPECT_FALSE(max_increased); | 
|  | 1178 |  | 
|  | 1179 | // Ignore negative increase. | 
|  | 1180 | max_increased = progress.Inc(-1); | 
|  | 1181 | EXPECT_EQ(1, progress.Get()); | 
|  | 1182 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); | 
|  | 1183 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1184 | EXPECT_FALSE(max_increased); | 
|  | 1185 | } | 
|  | 1186 |  | 
|  | 1187 | TEST_F(ProgressTest, MaxGrowsInsideNewRange) { | 
|  | 1188 | Progress progress = GetInstance(10, 1.2);  // 20% growth factor | 
|  | 1189 | EXPECT_EQ(0, progress.Get()); | 
|  | 1190 | EXPECT_EQ(10, progress.GetInitialMax()); | 
|  | 1191 | EXPECT_EQ(10, progress.GetMax()); | 
|  | 1192 |  | 
|  | 1193 | // No increase | 
|  | 1194 | bool max_increased = progress.Inc(10); | 
|  | 1195 | EXPECT_EQ(10, progress.Get()); | 
|  | 1196 | EXPECT_EQ(10, progress.GetMax()); | 
|  | 1197 | EXPECT_FALSE(max_increased); | 
|  | 1198 |  | 
|  | 1199 | // Increase, with new value < max*20% | 
|  | 1200 | max_increased = progress.Inc(1); | 
|  | 1201 | EXPECT_EQ(11, progress.Get()); | 
|  | 1202 | EXPECT_EQ(13, progress.GetMax());  // 11 average * 20% growth = 13.2 = 13 | 
|  | 1203 | EXPECT_TRUE(max_increased); | 
|  | 1204 | } | 
|  | 1205 |  | 
|  | 1206 | TEST_F(ProgressTest, MaxGrowsOutsideNewRange) { | 
|  | 1207 | Progress progress = GetInstance(10, 1.2);  // 20% growth factor | 
|  | 1208 | EXPECT_EQ(0, progress.Get()); | 
|  | 1209 | EXPECT_EQ(10, progress.GetInitialMax()); | 
|  | 1210 | EXPECT_EQ(10, progress.GetMax()); | 
|  | 1211 |  | 
|  | 1212 | // No increase | 
|  | 1213 | bool max_increased = progress.Inc(10); | 
|  | 1214 | EXPECT_EQ(10, progress.Get()); | 
|  | 1215 | EXPECT_EQ(10, progress.GetMax()); | 
|  | 1216 | EXPECT_FALSE(max_increased); | 
|  | 1217 |  | 
|  | 1218 | // Increase, with new value > max*20% | 
|  | 1219 | max_increased = progress.Inc(5); | 
|  | 1220 | EXPECT_EQ(15, progress.Get()); | 
|  | 1221 | EXPECT_EQ(18, progress.GetMax());  // 15 average * 20% growth = 18 | 
|  | 1222 | EXPECT_TRUE(max_increased); | 
|  | 1223 | } | 
|  | 1224 |  | 
|  | 1225 | TEST_F(ProgressTest, InvalidPath) { | 
|  | 1226 | Progress progress("/devil/null"); | 
|  | 1227 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1228 | } | 
|  | 1229 |  | 
|  | 1230 | TEST_F(ProgressTest, EmptyFile) { | 
|  | 1231 | Progress progress(CopyTextFileFixture("empty-file.txt")); | 
|  | 1232 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1233 | } | 
|  | 1234 |  | 
|  | 1235 | TEST_F(ProgressTest, InvalidLine1stEntryNAN) { | 
|  | 1236 | Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt")); | 
|  | 1237 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1238 | } | 
|  | 1239 |  | 
|  | 1240 | TEST_F(ProgressTest, InvalidLine2ndEntryNAN) { | 
|  | 1241 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt")); | 
|  | 1242 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1243 | } | 
|  | 1244 |  | 
|  | 1245 | TEST_F(ProgressTest, InvalidLineBothNAN) { | 
|  | 1246 | Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt")); | 
|  | 1247 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1248 | } | 
|  | 1249 |  | 
|  | 1250 | TEST_F(ProgressTest, InvalidLine1stEntryNegative) { | 
|  | 1251 | Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt")); | 
|  | 1252 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1253 | } | 
|  | 1254 |  | 
|  | 1255 | TEST_F(ProgressTest, InvalidLine2ndEntryNegative) { | 
|  | 1256 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt")); | 
|  | 1257 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1258 | } | 
|  | 1259 |  | 
|  | 1260 | TEST_F(ProgressTest, InvalidLine1stEntryTooBig) { | 
|  | 1261 | Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt")); | 
|  | 1262 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1263 | } | 
|  | 1264 |  | 
|  | 1265 | TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) { | 
|  | 1266 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt")); | 
|  | 1267 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); | 
|  | 1268 | } | 
|  | 1269 |  | 
|  | 1270 | // Tests stats are properly saved when the file does not exists. | 
|  | 1271 | TEST_F(ProgressTest, FirstTime) { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1272 | if (!IsStandalone()) { | 
|  | 1273 | // TODO: temporarily disabled because it's failing when running as suite | 
|  | 1274 | MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n") | 
|  | 1275 | return; | 
|  | 1276 | } | 
|  | 1277 |  | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1278 | std::string path = kTestDataPath + "FirstTime.txt"; | 
|  | 1279 | android::base::RemoveFileIfExists(path); | 
|  | 1280 |  | 
|  | 1281 | Progress run1(path); | 
|  | 1282 | EXPECT_EQ(0, run1.Get()); | 
|  | 1283 | EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax()); | 
|  | 1284 | EXPECT_EQ(Progress::kDefaultMax, run1.GetMax()); | 
|  | 1285 |  | 
|  | 1286 | bool max_increased = run1.Inc(20); | 
|  | 1287 | EXPECT_EQ(20, run1.Get()); | 
|  | 1288 | EXPECT_EQ(Progress::kDefaultMax, run1.GetMax()); | 
|  | 1289 | EXPECT_FALSE(max_increased); | 
|  | 1290 |  | 
|  | 1291 | run1.Save(); | 
|  | 1292 | AssertStats(path, 1, 20); | 
|  | 1293 | } | 
|  | 1294 |  | 
|  | 1295 | // Tests what happens when the persistent settings contains the average duration of 1 run. | 
|  | 1296 | // Data on file is 1 run and 109 average. | 
|  | 1297 | TEST_F(ProgressTest, SecondTime) { | 
|  | 1298 | std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt"); | 
|  | 1299 |  | 
|  | 1300 | Progress run1 = GetInstance(-42, 1.2, path); | 
|  | 1301 | EXPECT_EQ(0, run1.Get()); | 
|  | 1302 | EXPECT_EQ(10, run1.GetInitialMax()); | 
|  | 1303 | EXPECT_EQ(10, run1.GetMax()); | 
|  | 1304 |  | 
|  | 1305 | bool max_increased = run1.Inc(20); | 
|  | 1306 | EXPECT_EQ(20, run1.Get()); | 
|  | 1307 | EXPECT_EQ(24, run1.GetMax()); | 
|  | 1308 | EXPECT_TRUE(max_increased); | 
|  | 1309 |  | 
|  | 1310 | // Average now is 2 runs and (10 + 20)/ 2 = 15 | 
|  | 1311 | run1.Save(); | 
|  | 1312 | AssertStats(path, 2, 15); | 
|  | 1313 |  | 
|  | 1314 | Progress run2 = GetInstance(-42, 1.2, path); | 
|  | 1315 | EXPECT_EQ(0, run2.Get()); | 
|  | 1316 | EXPECT_EQ(15, run2.GetInitialMax()); | 
|  | 1317 | EXPECT_EQ(15, run2.GetMax()); | 
|  | 1318 |  | 
|  | 1319 | max_increased = run2.Inc(25); | 
|  | 1320 | EXPECT_EQ(25, run2.Get()); | 
|  | 1321 | EXPECT_EQ(30, run2.GetMax()); | 
|  | 1322 | EXPECT_TRUE(max_increased); | 
|  | 1323 |  | 
|  | 1324 | // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18 | 
|  | 1325 | run2.Save(); | 
|  | 1326 | AssertStats(path, 3, 18); | 
|  | 1327 |  | 
|  | 1328 | Progress run3 = GetInstance(-42, 1.2, path); | 
|  | 1329 | EXPECT_EQ(0, run3.Get()); | 
|  | 1330 | EXPECT_EQ(18, run3.GetInitialMax()); | 
|  | 1331 | EXPECT_EQ(18, run3.GetMax()); | 
|  | 1332 |  | 
|  | 1333 | // Make sure average decreases as well | 
|  | 1334 | max_increased = run3.Inc(5); | 
|  | 1335 | EXPECT_EQ(5, run3.Get()); | 
|  | 1336 | EXPECT_EQ(18, run3.GetMax()); | 
|  | 1337 | EXPECT_FALSE(max_increased); | 
|  | 1338 |  | 
|  | 1339 | // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14 | 
|  | 1340 | run3.Save(); | 
|  | 1341 | AssertStats(path, 4, 14); | 
|  | 1342 | } | 
|  | 1343 |  | 
|  | 1344 | // Tests what happens when the persistent settings contains the average duration of 2 runs. | 
|  | 1345 | // Data on file is 2 runs and 15 average. | 
|  | 1346 | TEST_F(ProgressTest, ThirdTime) { | 
|  | 1347 | std::string path = CopyTextFileFixture("stats-two-runs.txt"); | 
|  | 1348 | AssertStats(path, 2, 15);  // Sanity check | 
|  | 1349 |  | 
|  | 1350 | Progress run1 = GetInstance(-42, 1.2, path); | 
|  | 1351 | EXPECT_EQ(0, run1.Get()); | 
|  | 1352 | EXPECT_EQ(15, run1.GetInitialMax()); | 
|  | 1353 | EXPECT_EQ(15, run1.GetMax()); | 
|  | 1354 |  | 
|  | 1355 | bool max_increased = run1.Inc(20); | 
|  | 1356 | EXPECT_EQ(20, run1.Get()); | 
|  | 1357 | EXPECT_EQ(24, run1.GetMax()); | 
|  | 1358 | EXPECT_TRUE(max_increased); | 
|  | 1359 |  | 
|  | 1360 | // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16 | 
|  | 1361 | run1.Save(); | 
|  | 1362 | AssertStats(path, 3, 16); | 
|  | 1363 | } | 
|  | 1364 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1365 | class DumpstateUtilTest : public DumpstateBaseTest { | 
|  | 1366 | public: | 
|  | 1367 | void SetUp() { | 
|  | 1368 | DumpstateBaseTest::SetUp(); | 
|  | 1369 | SetDryRun(false); | 
|  | 1370 | } | 
|  | 1371 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1372 | void CaptureFdOut() { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1373 | ReadFileToString(path_, &out); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1374 | } | 
|  | 1375 |  | 
|  | 1376 | void CreateFd(const std::string& name) { | 
|  | 1377 | path_ = kTestDataPath + name; | 
|  | 1378 | MYLOGD("Creating fd for file %s\n", path_.c_str()); | 
|  | 1379 |  | 
|  | 1380 | fd = TEMP_FAILURE_RETRY(open(path_.c_str(), | 
|  | 1381 | O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, | 
|  | 1382 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); | 
|  | 1383 | ASSERT_GE(fd, 0) << "could not create FD for path " << path_; | 
|  | 1384 | } | 
|  | 1385 |  | 
|  | 1386 | // Runs a command into the `fd` and capture `stderr`. | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1387 | 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] | 1388 | const CommandOptions& options = CommandOptions::DEFAULT) { | 
|  | 1389 | CaptureStderr(); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1390 | int status = RunCommandToFd(fd, title, full_command, options); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1391 | close(fd); | 
|  | 1392 |  | 
|  | 1393 | CaptureFdOut(); | 
|  | 1394 | err = GetCapturedStderr(); | 
|  | 1395 | return status; | 
|  | 1396 | } | 
|  | 1397 |  | 
|  | 1398 | // Dumps a file and into the `fd` and `stderr`. | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1399 | int DumpFile(const std::string& title, const std::string& path) { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1400 | CaptureStderr(); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1401 | int status = DumpFileToFd(fd, title, path); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1402 | close(fd); | 
|  | 1403 |  | 
|  | 1404 | CaptureFdOut(); | 
|  | 1405 | err = GetCapturedStderr(); | 
|  | 1406 | return status; | 
|  | 1407 | } | 
|  | 1408 |  | 
| Ecco Park | 61ffcf7 | 2016-10-27 15:46:26 -0700 | [diff] [blame] | 1409 | // Find out the pid of the process_name | 
|  | 1410 | int FindPidOfProcess(const std::string& process_name) { | 
|  | 1411 | CaptureStderr(); | 
|  | 1412 | int status = GetPidByName(process_name); | 
|  | 1413 | err = GetCapturedStderr(); | 
|  | 1414 | return status; | 
|  | 1415 | } | 
|  | 1416 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1417 | int fd; | 
|  | 1418 |  | 
|  | 1419 | // 'fd` output and `stderr` from the last command ran. | 
|  | 1420 | std::string out, err; | 
|  | 1421 |  | 
|  | 1422 | private: | 
|  | 1423 | std::string path_; | 
|  | 1424 | }; | 
|  | 1425 |  | 
|  | 1426 | TEST_F(DumpstateUtilTest, RunCommandNoArgs) { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1427 | CreateFd("RunCommandNoArgs.txt"); | 
|  | 1428 | EXPECT_EQ(-1, RunCommand("", {})); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1429 | } | 
|  | 1430 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1431 | TEST_F(DumpstateUtilTest, RunCommandNoTitle) { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1432 | CreateFd("RunCommandWithNoArgs.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1433 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1434 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 1435 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 1436 | } | 
|  | 1437 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1438 | TEST_F(DumpstateUtilTest, RunCommandWithTitle) { | 
|  | 1439 | CreateFd("RunCommandWithNoArgs.txt"); | 
|  | 1440 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); | 
|  | 1441 | EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n")); | 
|  | 1442 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 1443 | } | 
|  | 1444 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1445 | TEST_F(DumpstateUtilTest, RunCommandWithOneArg) { | 
|  | 1446 | CreateFd("RunCommandWithOneArg.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1447 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"})); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1448 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1449 | EXPECT_THAT(out, StrEq("one\n")); | 
|  | 1450 | } | 
|  | 1451 |  | 
|  | 1452 | TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) { | 
|  | 1453 | CreateFd("RunCommandWithMultipleArgs.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1454 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"})); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1455 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1456 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); | 
|  | 1457 | } | 
|  | 1458 |  | 
|  | 1459 | TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) { | 
|  | 1460 | CreateFd("RunCommandWithLoggingMessage.txt"); | 
|  | 1461 | EXPECT_EQ( | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1462 | 0, RunCommand("", {kSimpleCommand}, | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1463 | CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build())); | 
|  | 1464 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 1465 | EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n")); | 
|  | 1466 | } | 
|  | 1467 |  | 
|  | 1468 | TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) { | 
|  | 1469 | CreateFd("RunCommandRedirectStderr.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1470 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, | 
|  | 1471 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1472 | EXPECT_THAT(out, IsEmpty()); | 
|  | 1473 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); | 
|  | 1474 | } | 
|  | 1475 |  | 
|  | 1476 | TEST_F(DumpstateUtilTest, RunCommandDryRun) { | 
|  | 1477 | CreateFd("RunCommandDryRun.txt"); | 
|  | 1478 | SetDryRun(true); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1479 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); | 
|  | 1480 | EXPECT_THAT(out, StrEq(android::base::StringPrintf( | 
|  | 1481 | "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n", | 
|  | 1482 | kSimpleCommand.c_str()))); | 
|  | 1483 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1484 | } | 
|  | 1485 |  | 
|  | 1486 | TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) { | 
|  | 1487 | CreateFd("RunCommandDryRun.txt"); | 
|  | 1488 | SetDryRun(true); | 
|  | 1489 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1490 | EXPECT_THAT( | 
|  | 1491 | out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str()))); | 
|  | 1492 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1493 | } | 
|  | 1494 |  | 
|  | 1495 | TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) { | 
|  | 1496 | CreateFd("RunCommandDryRunAlways.txt"); | 
|  | 1497 | SetDryRun(true); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1498 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build())); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1499 | EXPECT_THAT(out, StrEq("stdout\n")); | 
|  | 1500 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 1501 | } | 
|  | 1502 |  | 
|  | 1503 | TEST_F(DumpstateUtilTest, RunCommandNotFound) { | 
|  | 1504 | CreateFd("RunCommandNotFound.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1505 | EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"})); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1506 | EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code")); | 
|  | 1507 | EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed")); | 
|  | 1508 | } | 
|  | 1509 |  | 
|  | 1510 | TEST_F(DumpstateUtilTest, RunCommandFails) { | 
|  | 1511 | CreateFd("RunCommandFails.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1512 | EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"})); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1513 | EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand + | 
|  | 1514 | " --exit 42' failed: exit code 42\n")); | 
|  | 1515 | EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand + | 
|  | 1516 | " --exit 42' failed: exit code 42\n")); | 
|  | 1517 | } | 
|  | 1518 |  | 
|  | 1519 | TEST_F(DumpstateUtilTest, RunCommandCrashes) { | 
|  | 1520 | CreateFd("RunCommandCrashes.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1521 | EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"})); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1522 | // We don't know the exit code, so check just the prefix. | 
|  | 1523 | EXPECT_THAT( | 
|  | 1524 | out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); | 
|  | 1525 | EXPECT_THAT( | 
|  | 1526 | err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); | 
|  | 1527 | } | 
|  | 1528 |  | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 1529 | TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1530 | CreateFd("RunCommandTimesout.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1531 | EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"}, | 
|  | 1532 | CommandOptions::WithTimeout(1).Build())); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1533 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand + | 
|  | 1534 | " --sleep 2' timed out after 1")); | 
|  | 1535 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand + | 
|  | 1536 | " --sleep 2' timed out after 1")); | 
|  | 1537 | } | 
|  | 1538 |  | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 1539 | TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) { | 
|  | 1540 | CreateFd("RunCommandTimesout.txt"); | 
|  | 1541 | EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"}, | 
|  | 1542 | CommandOptions::WithTimeoutInMs(1000).Build())); | 
|  | 1543 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand + | 
|  | 1544 | " --sleep 2' timed out after 1")); | 
|  | 1545 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand + | 
|  | 1546 | " --sleep 2' timed out after 1")); | 
|  | 1547 | } | 
|  | 1548 |  | 
|  | 1549 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1550 | TEST_F(DumpstateUtilTest, RunCommandIsKilled) { | 
|  | 1551 | CreateFd("RunCommandIsKilled.txt"); | 
|  | 1552 | CaptureStderr(); | 
|  | 1553 |  | 
|  | 1554 | std::thread t([=]() { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1555 | EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"}, | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1556 | CommandOptions::WithTimeout(100).Always().Build())); | 
|  | 1557 | }); | 
|  | 1558 |  | 
|  | 1559 | // Capture pid and pre-sleep output. | 
|  | 1560 | sleep(1);  // Wait a little bit to make sure pid and 1st line were printed. | 
|  | 1561 | std::string err = GetCapturedStderr(); | 
|  | 1562 | EXPECT_THAT(err, StrEq("sleeping for 20s\n")); | 
|  | 1563 |  | 
|  | 1564 | CaptureFdOut(); | 
|  | 1565 | std::vector<std::string> lines = android::base::Split(out, "\n"); | 
|  | 1566 | ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out; | 
|  | 1567 |  | 
|  | 1568 | int pid = atoi(lines[0].c_str()); | 
|  | 1569 | EXPECT_THAT(lines[1], StrEq("stdout line1")); | 
|  | 1570 | EXPECT_THAT(lines[2], IsEmpty());  // \n | 
|  | 1571 |  | 
|  | 1572 | // Then kill the process. | 
|  | 1573 | CaptureFdOut(); | 
|  | 1574 | CaptureStderr(); | 
|  | 1575 | ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid; | 
|  | 1576 | t.join(); | 
|  | 1577 |  | 
|  | 1578 | // Finally, check output after murder. | 
|  | 1579 | CaptureFdOut(); | 
|  | 1580 | err = GetCapturedStderr(); | 
|  | 1581 |  | 
|  | 1582 | // out starts with the pid, which is an unknown | 
|  | 1583 | EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand + | 
|  | 1584 | " --pid --sleep 20' failed: killed by signal 15\n")); | 
|  | 1585 | EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand + | 
|  | 1586 | " --pid --sleep 20' failed: killed by signal 15\n")); | 
|  | 1587 | } | 
|  | 1588 |  | 
|  | 1589 | TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) { | 
|  | 1590 | if (!IsStandalone()) { | 
|  | 1591 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 1592 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 1593 | MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n") | 
|  | 1594 | return; | 
|  | 1595 | } | 
|  | 1596 | CreateFd("RunCommandAsRootUserBuild.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1597 | if (!PropertiesHelper::IsUserBuild()) { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1598 | // Emulates user build if necessarily. | 
|  | 1599 | SetBuildType("user"); | 
|  | 1600 | } | 
|  | 1601 |  | 
|  | 1602 | DropRoot(); | 
|  | 1603 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1604 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build())); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1605 |  | 
|  | 1606 | // We don't know the exact path of su, so we just check for the 'root ...' commands | 
|  | 1607 | EXPECT_THAT(out, StartsWith("Skipping")); | 
|  | 1608 | EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n")); | 
|  | 1609 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1610 | } | 
|  | 1611 |  | 
|  | 1612 | TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) { | 
|  | 1613 | if (!IsStandalone()) { | 
|  | 1614 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 1615 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 1616 | MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n") | 
|  | 1617 | return; | 
|  | 1618 | } | 
|  | 1619 | CreateFd("RunCommandAsRootNonUserBuild.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1620 | if (PropertiesHelper::IsUserBuild()) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1621 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); | 
|  | 1622 | return; | 
|  | 1623 | } | 
|  | 1624 |  | 
|  | 1625 | DropRoot(); | 
|  | 1626 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1627 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
|  | 1628 | CommandOptions::WithTimeout(1).AsRoot().Build())); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1629 |  | 
|  | 1630 | EXPECT_THAT(out, StrEq("0\nstdout\n")); | 
|  | 1631 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 1632 | } | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1633 |  | 
| Yifan Hong | 48e83a1 | 2017-10-03 14:10:07 -0700 | [diff] [blame] | 1634 |  | 
|  | 1635 | TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) { | 
|  | 1636 | if (!IsStandalone()) { | 
|  | 1637 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 1638 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 1639 | MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n") | 
|  | 1640 | return; | 
|  | 1641 | } | 
|  | 1642 | CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt"); | 
|  | 1643 | if (!PropertiesHelper::IsUserBuild()) { | 
|  | 1644 | // Emulates user build if necessarily. | 
|  | 1645 | SetBuildType("user"); | 
|  | 1646 | } | 
|  | 1647 |  | 
|  | 1648 | DropRoot(); | 
|  | 1649 |  | 
|  | 1650 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
|  | 1651 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); | 
|  | 1652 |  | 
|  | 1653 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); | 
|  | 1654 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 1655 | } | 
|  | 1656 |  | 
|  | 1657 | TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) { | 
|  | 1658 | if (!IsStandalone()) { | 
|  | 1659 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 1660 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 1661 | MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n") | 
|  | 1662 | return; | 
|  | 1663 | } | 
|  | 1664 | CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt"); | 
|  | 1665 | if (PropertiesHelper::IsUserBuild()) { | 
|  | 1666 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); | 
|  | 1667 | return; | 
|  | 1668 | } | 
|  | 1669 |  | 
|  | 1670 | DropRoot(); | 
|  | 1671 |  | 
|  | 1672 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
|  | 1673 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); | 
|  | 1674 |  | 
|  | 1675 | EXPECT_THAT(out, StrEq("0\nstdout\n")); | 
|  | 1676 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 1677 | } | 
|  | 1678 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1679 | TEST_F(DumpstateUtilTest, RunCommandDropRoot) { | 
|  | 1680 | if (!IsStandalone()) { | 
|  | 1681 | // TODO: temporarily disabled because it might cause other tests to fail after dropping | 
|  | 1682 | // to Shell - need to refactor tests to avoid this problem) | 
|  | 1683 | MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n") | 
|  | 1684 | return; | 
|  | 1685 | } | 
|  | 1686 | CreateFd("RunCommandDropRoot.txt"); | 
|  | 1687 | // First check root case - only available when running with 'adb root'. | 
|  | 1688 | uid_t uid = getuid(); | 
|  | 1689 | if (uid == 0) { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1690 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"})); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1691 | EXPECT_THAT(out, StrEq("0\nstdout\n")); | 
|  | 1692 | EXPECT_THAT(err, StrEq("stderr\n")); | 
|  | 1693 | return; | 
|  | 1694 | } | 
|  | 1695 | // Then run dropping root. | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1696 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1697 | CommandOptions::WithTimeout(1).DropRoot().Build())); | 
|  | 1698 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); | 
|  | 1699 | EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n")); | 
|  | 1700 | } | 
|  | 1701 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1702 | TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) { | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1703 | CreateFd("DumpFileNotFound.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1704 | EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist")); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1705 | EXPECT_THAT(out, | 
|  | 1706 | StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n")); | 
|  | 1707 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1708 | } | 
|  | 1709 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1710 | TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) { | 
|  | 1711 | CreateFd("DumpFileNotFound.txt"); | 
|  | 1712 | EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist")); | 
|  | 1713 | EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such " | 
|  | 1714 | "file or directory\n")); | 
|  | 1715 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1716 | } | 
|  | 1717 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1718 | TEST_F(DumpstateUtilTest, DumpFileSingleLine) { | 
|  | 1719 | CreateFd("DumpFileSingleLine.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1720 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1721 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1722 | EXPECT_THAT(out, StrEq("I AM LINE1\n"));  // dumpstate adds missing newline | 
|  | 1723 | } | 
|  | 1724 |  | 
|  | 1725 | TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) { | 
|  | 1726 | CreateFd("DumpFileSingleLineWithNewLine.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1727 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt")); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1728 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1729 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); | 
|  | 1730 | } | 
|  | 1731 |  | 
|  | 1732 | TEST_F(DumpstateUtilTest, DumpFileMultipleLines) { | 
|  | 1733 | CreateFd("DumpFileMultipleLines.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1734 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt")); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1735 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1736 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); | 
|  | 1737 | } | 
|  | 1738 |  | 
|  | 1739 | TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) { | 
|  | 1740 | CreateFd("DumpFileMultipleLinesWithNewLine.txt"); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1741 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt")); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1742 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1743 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); | 
|  | 1744 | } | 
|  | 1745 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1746 | TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) { | 
|  | 1747 | CreateFd("DumpFileOnDryRun.txt"); | 
|  | 1748 | SetDryRun(true); | 
|  | 1749 | std::string path = kTestDataPath + "single-line.txt"; | 
|  | 1750 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); | 
|  | 1751 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1752 | EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n")); | 
|  | 1753 | } | 
|  | 1754 |  | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1755 | TEST_F(DumpstateUtilTest, DumpFileOnDryRun) { | 
|  | 1756 | CreateFd("DumpFileOnDryRun.txt"); | 
|  | 1757 | SetDryRun(true); | 
|  | 1758 | std::string path = kTestDataPath + "single-line.txt"; | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1759 | 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] | 1760 | EXPECT_THAT(err, IsEmpty()); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1761 | EXPECT_THAT( | 
|  | 1762 | out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:")); | 
|  | 1763 | EXPECT_THAT(out, EndsWith("skipped on dry run\n")); | 
| Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1764 | } | 
| Ecco Park | 61ffcf7 | 2016-10-27 15:46:26 -0700 | [diff] [blame] | 1765 |  | 
|  | 1766 | TEST_F(DumpstateUtilTest, FindingPidWithExistingProcess) { | 
|  | 1767 | // init process always has pid 1. | 
|  | 1768 | EXPECT_EQ(1, FindPidOfProcess("init")); | 
|  | 1769 | EXPECT_THAT(err, IsEmpty()); | 
|  | 1770 | } | 
|  | 1771 |  | 
|  | 1772 | TEST_F(DumpstateUtilTest, FindingPidWithNotExistingProcess) { | 
|  | 1773 | // find the process with abnormal name. | 
|  | 1774 | EXPECT_EQ(-1, FindPidOfProcess("abcdef12345-543")); | 
|  | 1775 | EXPECT_THAT(err, StrEq("can't find the pid\n")); | 
|  | 1776 | } | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 1777 |  | 
|  | 1778 | }  // namespace dumpstate | 
|  | 1779 | }  // namespace os | 
|  | 1780 | }  // namespace android |