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