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