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