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