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