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