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