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); |
Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 298 | |
| 299 | // Other options retain default values |
| 300 | EXPECT_TRUE(options_.do_vibrate); |
| 301 | EXPECT_FALSE(options_.use_control_socket); |
| 302 | EXPECT_FALSE(options_.show_header_only); |
| 303 | EXPECT_FALSE(options_.do_progress_updates); |
| 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 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 510 | std::string GetProgressMessage(const std::string& listener_name, int progress, int max, |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 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) { |
Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 524 | message += android::base::StringPrintf("Setting progress (%s): %d/%d (%d%%)\n", |
| 525 | listener_name.c_str(), progress, max, |
| 526 | (100 * progress / max)); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | return message; |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 532 | // `stdout` and `stderr` from the last command ran. |
| 533 | std::string out, err; |
| 534 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 535 | Dumpstate& ds = Dumpstate::GetInstance(); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | TEST_F(DumpstateTest, RunCommandNoArgs) { |
| 539 | EXPECT_EQ(-1, RunCommand("", {})); |
| 540 | } |
| 541 | |
| 542 | TEST_F(DumpstateTest, RunCommandNoTitle) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 543 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 544 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 545 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 546 | } |
| 547 | |
| 548 | TEST_F(DumpstateTest, RunCommandWithTitle) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 549 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 550 | EXPECT_THAT(err, StrEq("stderr\n")); |
Greg Kaiser | 3a811c1 | 2019-05-21 12:48:59 -0700 | [diff] [blame] | 551 | // The duration may not get output, depending on how long it takes, |
| 552 | // so we just check the prefix. |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 553 | EXPECT_THAT(out, |
Nandana Dutt | 47527b5 | 2019-03-29 15:34:36 +0000 | [diff] [blame] | 554 | StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 557 | TEST_F(DumpstateTest, RunCommandWithLoggingMessage) { |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 558 | EXPECT_EQ( |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 559 | 0, RunCommand("", {kSimpleCommand}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 560 | CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build())); |
| 561 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 562 | EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n")); |
| 563 | } |
| 564 | |
| 565 | TEST_F(DumpstateTest, RunCommandRedirectStderr) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 566 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 567 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 568 | EXPECT_THAT(out, IsEmpty()); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 569 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | TEST_F(DumpstateTest, RunCommandWithOneArg) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 573 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 574 | EXPECT_THAT(err, IsEmpty()); |
| 575 | EXPECT_THAT(out, StrEq("one\n")); |
| 576 | } |
| 577 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 578 | TEST_F(DumpstateTest, RunCommandWithMultipleArgs) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 579 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 580 | EXPECT_THAT(err, IsEmpty()); |
| 581 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); |
| 582 | } |
| 583 | |
| 584 | TEST_F(DumpstateTest, RunCommandDryRun) { |
| 585 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 586 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
Greg Kaiser | 3a811c1 | 2019-05-21 12:48:59 -0700 | [diff] [blame] | 587 | // The duration may not get output, depending on how long it takes, |
| 588 | // so we just check the prefix. |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 589 | EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand + |
Nandana Dutt | 47527b5 | 2019-03-29 15:34:36 +0000 | [diff] [blame] | 590 | ") ------\n\t(skipped on dry run)\n")); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 591 | EXPECT_THAT(err, IsEmpty()); |
| 592 | } |
| 593 | |
| 594 | TEST_F(DumpstateTest, RunCommandDryRunNoTitle) { |
| 595 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 596 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 597 | EXPECT_THAT(out, IsEmpty()); |
| 598 | EXPECT_THAT(err, IsEmpty()); |
| 599 | } |
| 600 | |
| 601 | TEST_F(DumpstateTest, RunCommandDryRunAlways) { |
| 602 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 603 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build())); |
Felipe Leme | 4c2d663 | 2016-09-28 14:32:00 -0700 | [diff] [blame] | 604 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 605 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 606 | } |
| 607 | |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 608 | TEST_F(DumpstateTest, RunCommandNotFound) { |
| 609 | EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"})); |
| 610 | EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code")); |
| 611 | EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed")); |
| 612 | } |
| 613 | |
| 614 | TEST_F(DumpstateTest, RunCommandFails) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 615 | EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"})); |
| 616 | EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand + |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 617 | " --exit 42' failed: exit code 42\n")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 618 | EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand + |
Felipe Leme | 9a523ae | 2016-10-20 15:10:33 -0700 | [diff] [blame] | 619 | " --exit 42' failed: exit code 42\n")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | TEST_F(DumpstateTest, RunCommandCrashes) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 623 | EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"})); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 624 | // We don't know the exit code, so check just the prefix. |
| 625 | EXPECT_THAT( |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 626 | out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 627 | EXPECT_THAT( |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 628 | err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | TEST_F(DumpstateTest, RunCommandTimesout) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 632 | EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 633 | CommandOptions::WithTimeout(1).Build())); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 634 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 635 | " --sleep 2' timed out after 1")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 636 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 637 | " --sleep 2' timed out after 1")); |
| 638 | } |
| 639 | |
| 640 | TEST_F(DumpstateTest, RunCommandIsKilled) { |
| 641 | CaptureStdout(); |
| 642 | CaptureStderr(); |
| 643 | |
| 644 | std::thread t([=]() { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 645 | EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"}, |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 646 | CommandOptions::WithTimeout(100).Always().Build())); |
| 647 | }); |
| 648 | |
| 649 | // Capture pid and pre-sleep output. |
| 650 | sleep(1); // Wait a little bit to make sure pid and 1st line were printed. |
| 651 | std::string err = GetCapturedStderr(); |
| 652 | EXPECT_THAT(err, StrEq("sleeping for 20s\n")); |
| 653 | |
| 654 | std::string out = GetCapturedStdout(); |
| 655 | std::vector<std::string> lines = android::base::Split(out, "\n"); |
| 656 | ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out; |
| 657 | |
| 658 | int pid = atoi(lines[0].c_str()); |
| 659 | EXPECT_THAT(lines[1], StrEq("stdout line1")); |
| 660 | EXPECT_THAT(lines[2], IsEmpty()); // \n |
| 661 | |
| 662 | // Then kill the process. |
| 663 | CaptureStdout(); |
| 664 | CaptureStderr(); |
| 665 | ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid; |
| 666 | t.join(); |
| 667 | |
| 668 | // Finally, check output after murder. |
| 669 | out = GetCapturedStdout(); |
| 670 | err = GetCapturedStderr(); |
| 671 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 672 | EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 673 | " --pid --sleep 20' failed: killed by signal 15\n")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 674 | EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand + |
Felipe Leme | fd8affa | 2016-09-30 17:38:57 -0700 | [diff] [blame] | 675 | " --pid --sleep 20' failed: killed by signal 15\n")); |
| 676 | } |
| 677 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 678 | TEST_F(DumpstateTest, RunCommandProgress) { |
| 679 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 680 | ds.listener_ = listener; |
| 681 | ds.listener_name_ = "FoxMulder"; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 682 | SetProgress(0, 30); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 683 | |
Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 684 | EXPECT_CALL(*listener, onProgress(66)); // 20/30 % |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 685 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build())); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 686 | std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30); |
| 687 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 688 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 689 | |
Nandana Dutt | 402a839 | 2019-06-14 14:25:13 +0100 | [diff] [blame] | 690 | EXPECT_CALL(*listener, onProgress(80)); // 24/30 % |
| 691 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build())); |
| 692 | progress_message = GetProgressMessage(ds.listener_name_, 24, 30); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 693 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 694 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 695 | |
| 696 | // Make sure command ran while in dry_run is counted. |
| 697 | SetDryRun(true); |
Nandana Dutt | 402a839 | 2019-06-14 14:25:13 +0100 | [diff] [blame] | 698 | EXPECT_CALL(*listener, onProgress(90)); // 27/30 % |
| 699 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build())); |
| 700 | progress_message = GetProgressMessage(ds.listener_name_, 27, 30); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 701 | EXPECT_THAT(out, IsEmpty()); |
| 702 | EXPECT_THAT(err, StrEq(progress_message)); |
| 703 | |
Nandana Dutt | 402a839 | 2019-06-14 14:25:13 +0100 | [diff] [blame] | 704 | SetDryRun(false); |
| 705 | EXPECT_CALL(*listener, onProgress(96)); // 29/30 % |
| 706 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(2).Build())); |
| 707 | progress_message = GetProgressMessage(ds.listener_name_, 29, 30); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 708 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 709 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 710 | |
Nandana Dutt | 402a839 | 2019-06-14 14:25:13 +0100 | [diff] [blame] | 711 | EXPECT_CALL(*listener, onProgress(100)); // 30/30 % |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 712 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build())); |
Nandana Dutt | 402a839 | 2019-06-14 14:25:13 +0100 | [diff] [blame] | 713 | progress_message = GetProgressMessage(ds.listener_name_, 30, 30); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 714 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 715 | EXPECT_THAT(err, StrEq("stderr\n" + progress_message)); |
| 716 | |
| 717 | ds.listener_.clear(); |
| 718 | } |
| 719 | |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 720 | TEST_F(DumpstateTest, RunCommandDropRoot) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 721 | if (!IsStandalone()) { |
| 722 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 723 | // to Shell - need to refactor tests to avoid this problem) |
| 724 | MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n") |
| 725 | return; |
| 726 | } |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 727 | // First check root case - only available when running with 'adb root'. |
| 728 | uid_t uid = getuid(); |
| 729 | if (uid == 0) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 730 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"})); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 731 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 732 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 733 | return; |
| 734 | } |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 735 | // Then run dropping root. |
| 736 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 737 | CommandOptions::WithTimeout(1).DropRoot().Build())); |
| 738 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
Felipe Leme | 26c4157 | 2016-10-06 14:34:43 -0700 | [diff] [blame] | 739 | 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] | 740 | } |
| 741 | |
| 742 | TEST_F(DumpstateTest, RunCommandAsRootUserBuild) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 743 | if (!IsStandalone()) { |
| 744 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 745 | // to Shell - need to refactor tests to avoid this problem) |
| 746 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n") |
| 747 | return; |
| 748 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 749 | if (!PropertiesHelper::IsUserBuild()) { |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 750 | // Emulates user build if necessarily. |
| 751 | SetBuildType("user"); |
| 752 | } |
| 753 | |
| 754 | DropRoot(); |
| 755 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 756 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build())); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 757 | |
| 758 | // We don't know the exact path of su, so we just check for the 'root ...' commands |
| 759 | EXPECT_THAT(out, StartsWith("Skipping")); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 760 | EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n")); |
Felipe Leme | d80e6b6 | 2016-10-03 13:08:14 -0700 | [diff] [blame] | 761 | EXPECT_THAT(err, IsEmpty()); |
| 762 | } |
| 763 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 764 | TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) { |
| 765 | if (!IsStandalone()) { |
| 766 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 767 | // to Shell - need to refactor tests to avoid this problem) |
| 768 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n") |
| 769 | return; |
| 770 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 771 | if (PropertiesHelper::IsUserBuild()) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 772 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | DropRoot(); |
| 777 | |
| 778 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 779 | CommandOptions::WithTimeout(1).AsRoot().Build())); |
| 780 | |
| 781 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 782 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 783 | } |
| 784 | |
Nandana Dutt | 4b392be | 2018-11-02 16:17:05 +0000 | [diff] [blame] | 785 | TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild_withUnroot) { |
| 786 | if (!IsStandalone()) { |
| 787 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 788 | // to Shell - need to refactor tests to avoid this problem) |
| 789 | MYLOGE( |
| 790 | "Skipping DumpstateTest.RunCommandAsRootNonUserBuild_withUnroot() " |
| 791 | "on test suite\n") |
| 792 | return; |
| 793 | } |
| 794 | if (PropertiesHelper::IsUserBuild()) { |
| 795 | ALOGI("Skipping RunCommandAsRootNonUserBuild_withUnroot on user builds\n"); |
| 796 | return; |
| 797 | } |
| 798 | |
| 799 | // Same test as above, but with unroot property set, which will override su availability. |
| 800 | SetUnroot(true); |
| 801 | DropRoot(); |
| 802 | |
| 803 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 804 | CommandOptions::WithTimeout(1).AsRoot().Build())); |
| 805 | |
| 806 | // AsRoot is ineffective. |
| 807 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
| 808 | EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n")); |
| 809 | } |
| 810 | |
Yifan Hong | 48e83a1 | 2017-10-03 14:10:07 -0700 | [diff] [blame] | 811 | TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) { |
| 812 | if (!IsStandalone()) { |
| 813 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 814 | // to Shell - need to refactor tests to avoid this problem) |
| 815 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n") |
| 816 | return; |
| 817 | } |
| 818 | if (!PropertiesHelper::IsUserBuild()) { |
| 819 | // Emulates user build if necessarily. |
| 820 | SetBuildType("user"); |
| 821 | } |
| 822 | |
| 823 | DropRoot(); |
| 824 | |
| 825 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 826 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); |
| 827 | |
| 828 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
| 829 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 830 | } |
| 831 | |
| 832 | TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) { |
| 833 | if (!IsStandalone()) { |
| 834 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 835 | // to Shell - need to refactor tests to avoid this problem) |
| 836 | MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n") |
| 837 | return; |
| 838 | } |
| 839 | if (PropertiesHelper::IsUserBuild()) { |
| 840 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | DropRoot(); |
| 845 | |
| 846 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 847 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); |
| 848 | |
| 849 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 850 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 851 | } |
| 852 | |
Nandana Dutt | 4b392be | 2018-11-02 16:17:05 +0000 | [diff] [blame] | 853 | TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild_withUnroot) { |
| 854 | if (!IsStandalone()) { |
| 855 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 856 | // to Shell - need to refactor tests to avoid this problem) |
| 857 | MYLOGE( |
| 858 | "Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild_withUnroot() " |
| 859 | "on test suite\n") |
| 860 | return; |
| 861 | } |
| 862 | if (PropertiesHelper::IsUserBuild()) { |
| 863 | ALOGI("Skipping RunCommandAsRootIfAvailableOnDebugBuild_withUnroot on user builds\n"); |
| 864 | return; |
| 865 | } |
| 866 | // Same test as above, but with unroot property set, which will override su availability. |
| 867 | SetUnroot(true); |
| 868 | |
| 869 | DropRoot(); |
| 870 | |
| 871 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 872 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); |
| 873 | |
| 874 | // It's a userdebug build, so "su root" should be available, but unroot=true overrides it. |
| 875 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
| 876 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 877 | } |
| 878 | |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 879 | TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) { |
| 880 | EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist")); |
| 881 | EXPECT_THAT(out, |
| 882 | StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n")); |
| 883 | EXPECT_THAT(err, IsEmpty()); |
| 884 | } |
| 885 | |
| 886 | TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) { |
| 887 | EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist")); |
| 888 | EXPECT_THAT(err, IsEmpty()); |
Greg Kaiser | 3a811c1 | 2019-05-21 12:48:59 -0700 | [diff] [blame] | 889 | // The duration may not get output, depending on how long it takes, |
| 890 | // so we just check the prefix. |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 891 | EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No " |
| 892 | "such file or directory\n")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | TEST_F(DumpstateTest, DumpFileSingleLine) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 896 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 897 | EXPECT_THAT(err, IsEmpty()); |
| 898 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 899 | } |
| 900 | |
| 901 | TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 902 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 903 | EXPECT_THAT(err, IsEmpty()); |
| 904 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); |
| 905 | } |
| 906 | |
| 907 | TEST_F(DumpstateTest, DumpFileMultipleLines) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 908 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 909 | EXPECT_THAT(err, IsEmpty()); |
| 910 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 911 | } |
| 912 | |
| 913 | TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 914 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 915 | EXPECT_THAT(err, IsEmpty()); |
| 916 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 917 | } |
| 918 | |
| 919 | TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) { |
| 920 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 921 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 922 | EXPECT_THAT(err, IsEmpty()); |
| 923 | EXPECT_THAT(out, IsEmpty()); |
| 924 | } |
| 925 | |
| 926 | TEST_F(DumpstateTest, DumpFileOnDryRun) { |
| 927 | SetDryRun(true); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 928 | 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] | 929 | EXPECT_THAT(err, IsEmpty()); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 930 | EXPECT_THAT( |
| 931 | out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:")); |
Nandana Dutt | 47527b5 | 2019-03-29 15:34:36 +0000 | [diff] [blame] | 932 | EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n")); |
Felipe Leme | cef0298 | 2016-10-03 17:22:22 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 935 | TEST_F(DumpstateTest, DumpFileUpdateProgress) { |
| 936 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 937 | ds.listener_ = listener; |
| 938 | ds.listener_name_ = "FoxMulder"; |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 939 | SetProgress(0, 30); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 940 | |
Nandana Dutt | babf6c7 | 2019-01-15 14:11:12 +0000 | [diff] [blame] | 941 | EXPECT_CALL(*listener, onProgress(16)); // 5/30 % |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 942 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 943 | |
| 944 | std::string progress_message = |
| 945 | GetProgressMessage(ds.listener_name_, 5, 30); // TODO: unhardcode WEIGHT_FILE (5)? |
| 946 | EXPECT_THAT(err, StrEq(progress_message)); |
| 947 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 948 | |
| 949 | ds.listener_.clear(); |
| 950 | } |
| 951 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 952 | class DumpstateServiceTest : public DumpstateBaseTest { |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 953 | public: |
| 954 | DumpstateService dss; |
| 955 | }; |
| 956 | |
| 957 | TEST_F(DumpstateServiceTest, SetListenerNoName) { |
| 958 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 959 | sp<IDumpstateToken> token; |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 960 | EXPECT_TRUE(dss.setListener("", listener, /* getSectionDetails = */ false, &token).isOk()); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 961 | ASSERT_THAT(token, IsNull()); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | TEST_F(DumpstateServiceTest, SetListenerNoPointer) { |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 965 | sp<IDumpstateToken> token; |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 966 | EXPECT_TRUE( |
| 967 | dss.setListener("whatever", nullptr, /* getSectionDetails = */ false, &token).isOk()); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 968 | ASSERT_THAT(token, IsNull()); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | TEST_F(DumpstateServiceTest, SetListenerTwice) { |
| 972 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 973 | sp<IDumpstateToken> token; |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 974 | EXPECT_TRUE( |
| 975 | dss.setListener("whatever", listener, /* getSectionDetails = */ false, &token).isOk()); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 976 | ASSERT_THAT(token, NotNull()); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 977 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 978 | EXPECT_FALSE(Dumpstate::GetInstance().report_section_); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 979 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 980 | token.clear(); |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 981 | EXPECT_TRUE( |
| 982 | dss.setListener("whatsoever", listener, /* getSectionDetails = */ false, &token).isOk()); |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 983 | ASSERT_THAT(token, IsNull()); |
| 984 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 985 | EXPECT_FALSE(Dumpstate::GetInstance().report_section_); |
| 986 | } |
| 987 | |
| 988 | TEST_F(DumpstateServiceTest, SetListenerWithSectionDetails) { |
| 989 | sp<DumpstateListenerMock> listener(new DumpstateListenerMock()); |
| 990 | sp<IDumpstateToken> token; |
| 991 | Dumpstate::GetInstance().listener_ = nullptr; |
| 992 | EXPECT_TRUE( |
| 993 | dss.setListener("whatever", listener, /* getSectionDetails = */ true, &token).isOk()); |
| 994 | ASSERT_THAT(token, NotNull()); |
| 995 | EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever")); |
| 996 | EXPECT_TRUE(Dumpstate::GetInstance().report_section_); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 997 | } |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 998 | |
| 999 | class ProgressTest : public DumpstateBaseTest { |
| 1000 | public: |
| 1001 | Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") { |
| 1002 | return Progress(max, growth_factor, path); |
| 1003 | } |
| 1004 | |
| 1005 | void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) { |
| 1006 | std::string expected_content = |
| 1007 | android::base::StringPrintf("%d %d\n", expected_runs, expected_average); |
| 1008 | std::string actual_content; |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1009 | ReadFileToString(path, &actual_content); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1010 | ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path; |
| 1011 | } |
| 1012 | }; |
| 1013 | |
| 1014 | TEST_F(ProgressTest, SimpleTest) { |
| 1015 | Progress progress; |
| 1016 | EXPECT_EQ(0, progress.Get()); |
| 1017 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); |
| 1018 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1019 | |
| 1020 | bool max_increased = progress.Inc(1); |
| 1021 | EXPECT_EQ(1, progress.Get()); |
| 1022 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); |
| 1023 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1024 | EXPECT_FALSE(max_increased); |
| 1025 | |
| 1026 | // Ignore negative increase. |
| 1027 | max_increased = progress.Inc(-1); |
| 1028 | EXPECT_EQ(1, progress.Get()); |
| 1029 | EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax()); |
| 1030 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1031 | EXPECT_FALSE(max_increased); |
| 1032 | } |
| 1033 | |
| 1034 | TEST_F(ProgressTest, MaxGrowsInsideNewRange) { |
| 1035 | Progress progress = GetInstance(10, 1.2); // 20% growth factor |
| 1036 | EXPECT_EQ(0, progress.Get()); |
| 1037 | EXPECT_EQ(10, progress.GetInitialMax()); |
| 1038 | EXPECT_EQ(10, progress.GetMax()); |
| 1039 | |
| 1040 | // No increase |
| 1041 | bool max_increased = progress.Inc(10); |
| 1042 | EXPECT_EQ(10, progress.Get()); |
| 1043 | EXPECT_EQ(10, progress.GetMax()); |
| 1044 | EXPECT_FALSE(max_increased); |
| 1045 | |
| 1046 | // Increase, with new value < max*20% |
| 1047 | max_increased = progress.Inc(1); |
| 1048 | EXPECT_EQ(11, progress.Get()); |
| 1049 | EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13 |
| 1050 | EXPECT_TRUE(max_increased); |
| 1051 | } |
| 1052 | |
| 1053 | TEST_F(ProgressTest, MaxGrowsOutsideNewRange) { |
| 1054 | Progress progress = GetInstance(10, 1.2); // 20% growth factor |
| 1055 | EXPECT_EQ(0, progress.Get()); |
| 1056 | EXPECT_EQ(10, progress.GetInitialMax()); |
| 1057 | EXPECT_EQ(10, progress.GetMax()); |
| 1058 | |
| 1059 | // No increase |
| 1060 | bool max_increased = progress.Inc(10); |
| 1061 | EXPECT_EQ(10, progress.Get()); |
| 1062 | EXPECT_EQ(10, progress.GetMax()); |
| 1063 | EXPECT_FALSE(max_increased); |
| 1064 | |
| 1065 | // Increase, with new value > max*20% |
| 1066 | max_increased = progress.Inc(5); |
| 1067 | EXPECT_EQ(15, progress.Get()); |
| 1068 | EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18 |
| 1069 | EXPECT_TRUE(max_increased); |
| 1070 | } |
| 1071 | |
| 1072 | TEST_F(ProgressTest, InvalidPath) { |
| 1073 | Progress progress("/devil/null"); |
| 1074 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1075 | } |
| 1076 | |
| 1077 | TEST_F(ProgressTest, EmptyFile) { |
| 1078 | Progress progress(CopyTextFileFixture("empty-file.txt")); |
| 1079 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1080 | } |
| 1081 | |
| 1082 | TEST_F(ProgressTest, InvalidLine1stEntryNAN) { |
| 1083 | Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt")); |
| 1084 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1085 | } |
| 1086 | |
| 1087 | TEST_F(ProgressTest, InvalidLine2ndEntryNAN) { |
| 1088 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt")); |
| 1089 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1090 | } |
| 1091 | |
| 1092 | TEST_F(ProgressTest, InvalidLineBothNAN) { |
| 1093 | Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt")); |
| 1094 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1095 | } |
| 1096 | |
| 1097 | TEST_F(ProgressTest, InvalidLine1stEntryNegative) { |
| 1098 | Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt")); |
| 1099 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1100 | } |
| 1101 | |
| 1102 | TEST_F(ProgressTest, InvalidLine2ndEntryNegative) { |
| 1103 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt")); |
| 1104 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1105 | } |
| 1106 | |
| 1107 | TEST_F(ProgressTest, InvalidLine1stEntryTooBig) { |
| 1108 | Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt")); |
| 1109 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1110 | } |
| 1111 | |
| 1112 | TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) { |
| 1113 | Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt")); |
| 1114 | EXPECT_EQ(Progress::kDefaultMax, progress.GetMax()); |
| 1115 | } |
| 1116 | |
| 1117 | // Tests stats are properly saved when the file does not exists. |
| 1118 | TEST_F(ProgressTest, FirstTime) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1119 | if (!IsStandalone()) { |
| 1120 | // TODO: temporarily disabled because it's failing when running as suite |
| 1121 | MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n") |
| 1122 | return; |
| 1123 | } |
| 1124 | |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1125 | std::string path = kTestDataPath + "FirstTime.txt"; |
| 1126 | android::base::RemoveFileIfExists(path); |
| 1127 | |
| 1128 | Progress run1(path); |
| 1129 | EXPECT_EQ(0, run1.Get()); |
| 1130 | EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax()); |
| 1131 | EXPECT_EQ(Progress::kDefaultMax, run1.GetMax()); |
| 1132 | |
| 1133 | bool max_increased = run1.Inc(20); |
| 1134 | EXPECT_EQ(20, run1.Get()); |
| 1135 | EXPECT_EQ(Progress::kDefaultMax, run1.GetMax()); |
| 1136 | EXPECT_FALSE(max_increased); |
| 1137 | |
| 1138 | run1.Save(); |
| 1139 | AssertStats(path, 1, 20); |
| 1140 | } |
| 1141 | |
| 1142 | // Tests what happens when the persistent settings contains the average duration of 1 run. |
| 1143 | // Data on file is 1 run and 109 average. |
| 1144 | TEST_F(ProgressTest, SecondTime) { |
| 1145 | std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt"); |
| 1146 | |
| 1147 | Progress run1 = GetInstance(-42, 1.2, path); |
| 1148 | EXPECT_EQ(0, run1.Get()); |
| 1149 | EXPECT_EQ(10, run1.GetInitialMax()); |
| 1150 | EXPECT_EQ(10, run1.GetMax()); |
| 1151 | |
| 1152 | bool max_increased = run1.Inc(20); |
| 1153 | EXPECT_EQ(20, run1.Get()); |
| 1154 | EXPECT_EQ(24, run1.GetMax()); |
| 1155 | EXPECT_TRUE(max_increased); |
| 1156 | |
| 1157 | // Average now is 2 runs and (10 + 20)/ 2 = 15 |
| 1158 | run1.Save(); |
| 1159 | AssertStats(path, 2, 15); |
| 1160 | |
| 1161 | Progress run2 = GetInstance(-42, 1.2, path); |
| 1162 | EXPECT_EQ(0, run2.Get()); |
| 1163 | EXPECT_EQ(15, run2.GetInitialMax()); |
| 1164 | EXPECT_EQ(15, run2.GetMax()); |
| 1165 | |
| 1166 | max_increased = run2.Inc(25); |
| 1167 | EXPECT_EQ(25, run2.Get()); |
| 1168 | EXPECT_EQ(30, run2.GetMax()); |
| 1169 | EXPECT_TRUE(max_increased); |
| 1170 | |
| 1171 | // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18 |
| 1172 | run2.Save(); |
| 1173 | AssertStats(path, 3, 18); |
| 1174 | |
| 1175 | Progress run3 = GetInstance(-42, 1.2, path); |
| 1176 | EXPECT_EQ(0, run3.Get()); |
| 1177 | EXPECT_EQ(18, run3.GetInitialMax()); |
| 1178 | EXPECT_EQ(18, run3.GetMax()); |
| 1179 | |
| 1180 | // Make sure average decreases as well |
| 1181 | max_increased = run3.Inc(5); |
| 1182 | EXPECT_EQ(5, run3.Get()); |
| 1183 | EXPECT_EQ(18, run3.GetMax()); |
| 1184 | EXPECT_FALSE(max_increased); |
| 1185 | |
| 1186 | // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14 |
| 1187 | run3.Save(); |
| 1188 | AssertStats(path, 4, 14); |
| 1189 | } |
| 1190 | |
| 1191 | // Tests what happens when the persistent settings contains the average duration of 2 runs. |
| 1192 | // Data on file is 2 runs and 15 average. |
| 1193 | TEST_F(ProgressTest, ThirdTime) { |
| 1194 | std::string path = CopyTextFileFixture("stats-two-runs.txt"); |
| 1195 | AssertStats(path, 2, 15); // Sanity check |
| 1196 | |
| 1197 | Progress run1 = GetInstance(-42, 1.2, path); |
| 1198 | EXPECT_EQ(0, run1.Get()); |
| 1199 | EXPECT_EQ(15, run1.GetInitialMax()); |
| 1200 | EXPECT_EQ(15, run1.GetMax()); |
| 1201 | |
| 1202 | bool max_increased = run1.Inc(20); |
| 1203 | EXPECT_EQ(20, run1.Get()); |
| 1204 | EXPECT_EQ(24, run1.GetMax()); |
| 1205 | EXPECT_TRUE(max_increased); |
| 1206 | |
| 1207 | // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16 |
| 1208 | run1.Save(); |
| 1209 | AssertStats(path, 3, 16); |
| 1210 | } |
| 1211 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1212 | class DumpstateUtilTest : public DumpstateBaseTest { |
| 1213 | public: |
| 1214 | void SetUp() { |
| 1215 | DumpstateBaseTest::SetUp(); |
| 1216 | SetDryRun(false); |
| 1217 | } |
| 1218 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1219 | void CaptureFdOut() { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1220 | ReadFileToString(path_, &out); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | void CreateFd(const std::string& name) { |
| 1224 | path_ = kTestDataPath + name; |
| 1225 | MYLOGD("Creating fd for file %s\n", path_.c_str()); |
| 1226 | |
| 1227 | fd = TEMP_FAILURE_RETRY(open(path_.c_str(), |
| 1228 | O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, |
| 1229 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
| 1230 | ASSERT_GE(fd, 0) << "could not create FD for path " << path_; |
| 1231 | } |
| 1232 | |
| 1233 | // Runs a command into the `fd` and capture `stderr`. |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1234 | 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] | 1235 | const CommandOptions& options = CommandOptions::DEFAULT) { |
| 1236 | CaptureStderr(); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1237 | int status = RunCommandToFd(fd, title, full_command, options); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1238 | close(fd); |
| 1239 | |
| 1240 | CaptureFdOut(); |
| 1241 | err = GetCapturedStderr(); |
| 1242 | return status; |
| 1243 | } |
| 1244 | |
| 1245 | // Dumps a file and into the `fd` and `stderr`. |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1246 | int DumpFile(const std::string& title, const std::string& path) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1247 | CaptureStderr(); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1248 | int status = DumpFileToFd(fd, title, path); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1249 | close(fd); |
| 1250 | |
| 1251 | CaptureFdOut(); |
| 1252 | err = GetCapturedStderr(); |
| 1253 | return status; |
| 1254 | } |
| 1255 | |
| 1256 | int fd; |
| 1257 | |
| 1258 | // 'fd` output and `stderr` from the last command ran. |
| 1259 | std::string out, err; |
| 1260 | |
| 1261 | private: |
| 1262 | std::string path_; |
| 1263 | }; |
| 1264 | |
| 1265 | TEST_F(DumpstateUtilTest, RunCommandNoArgs) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1266 | CreateFd("RunCommandNoArgs.txt"); |
| 1267 | EXPECT_EQ(-1, RunCommand("", {})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1270 | TEST_F(DumpstateUtilTest, RunCommandNoTitle) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1271 | CreateFd("RunCommandWithNoArgs.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1272 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1273 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 1274 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 1275 | } |
| 1276 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1277 | TEST_F(DumpstateUtilTest, RunCommandWithTitle) { |
| 1278 | CreateFd("RunCommandWithNoArgs.txt"); |
| 1279 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
| 1280 | EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n")); |
| 1281 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 1282 | } |
| 1283 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1284 | TEST_F(DumpstateUtilTest, RunCommandWithOneArg) { |
| 1285 | CreateFd("RunCommandWithOneArg.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1286 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1287 | EXPECT_THAT(err, IsEmpty()); |
| 1288 | EXPECT_THAT(out, StrEq("one\n")); |
| 1289 | } |
| 1290 | |
| 1291 | TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) { |
| 1292 | CreateFd("RunCommandWithMultipleArgs.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1293 | EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1294 | EXPECT_THAT(err, IsEmpty()); |
| 1295 | EXPECT_THAT(out, StrEq("one is the loniest number\n")); |
| 1296 | } |
| 1297 | |
| 1298 | TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) { |
| 1299 | CreateFd("RunCommandWithLoggingMessage.txt"); |
| 1300 | EXPECT_EQ( |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1301 | 0, RunCommand("", {kSimpleCommand}, |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1302 | CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build())); |
| 1303 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 1304 | EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n")); |
| 1305 | } |
| 1306 | |
| 1307 | TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) { |
| 1308 | CreateFd("RunCommandRedirectStderr.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1309 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, |
| 1310 | CommandOptions::WithTimeout(10).RedirectStderr().Build())); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1311 | EXPECT_THAT(out, IsEmpty()); |
| 1312 | EXPECT_THAT(err, StrEq("stdout\nstderr\n")); |
| 1313 | } |
| 1314 | |
| 1315 | TEST_F(DumpstateUtilTest, RunCommandDryRun) { |
| 1316 | CreateFd("RunCommandDryRun.txt"); |
| 1317 | SetDryRun(true); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1318 | EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand})); |
| 1319 | EXPECT_THAT(out, StrEq(android::base::StringPrintf( |
| 1320 | "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n", |
| 1321 | kSimpleCommand.c_str()))); |
| 1322 | EXPECT_THAT(err, IsEmpty()); |
| 1323 | } |
| 1324 | |
| 1325 | TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) { |
| 1326 | CreateFd("RunCommandDryRun.txt"); |
| 1327 | SetDryRun(true); |
| 1328 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1329 | EXPECT_THAT( |
| 1330 | out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str()))); |
| 1331 | EXPECT_THAT(err, IsEmpty()); |
| 1332 | } |
| 1333 | |
| 1334 | TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) { |
| 1335 | CreateFd("RunCommandDryRunAlways.txt"); |
| 1336 | SetDryRun(true); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1337 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build())); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1338 | EXPECT_THAT(out, StrEq("stdout\n")); |
| 1339 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 1340 | } |
| 1341 | |
| 1342 | TEST_F(DumpstateUtilTest, RunCommandNotFound) { |
| 1343 | CreateFd("RunCommandNotFound.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1344 | EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1345 | EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code")); |
| 1346 | EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed")); |
| 1347 | } |
| 1348 | |
| 1349 | TEST_F(DumpstateUtilTest, RunCommandFails) { |
| 1350 | CreateFd("RunCommandFails.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1351 | EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1352 | EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand + |
| 1353 | " --exit 42' failed: exit code 42\n")); |
| 1354 | EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand + |
| 1355 | " --exit 42' failed: exit code 42\n")); |
| 1356 | } |
| 1357 | |
| 1358 | TEST_F(DumpstateUtilTest, RunCommandCrashes) { |
| 1359 | CreateFd("RunCommandCrashes.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1360 | EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1361 | // We don't know the exit code, so check just the prefix. |
| 1362 | EXPECT_THAT( |
| 1363 | out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
| 1364 | EXPECT_THAT( |
| 1365 | err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code")); |
| 1366 | } |
| 1367 | |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 1368 | TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1369 | CreateFd("RunCommandTimesout.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1370 | EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"}, |
| 1371 | CommandOptions::WithTimeout(1).Build())); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1372 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand + |
| 1373 | " --sleep 2' timed out after 1")); |
| 1374 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand + |
| 1375 | " --sleep 2' timed out after 1")); |
| 1376 | } |
| 1377 | |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 1378 | TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) { |
| 1379 | CreateFd("RunCommandTimesout.txt"); |
| 1380 | EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"}, |
| 1381 | CommandOptions::WithTimeoutInMs(1000).Build())); |
| 1382 | EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand + |
| 1383 | " --sleep 2' timed out after 1")); |
| 1384 | EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand + |
| 1385 | " --sleep 2' timed out after 1")); |
| 1386 | } |
| 1387 | |
| 1388 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1389 | TEST_F(DumpstateUtilTest, RunCommandIsKilled) { |
| 1390 | CreateFd("RunCommandIsKilled.txt"); |
| 1391 | CaptureStderr(); |
| 1392 | |
| 1393 | std::thread t([=]() { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1394 | EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"}, |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1395 | CommandOptions::WithTimeout(100).Always().Build())); |
| 1396 | }); |
| 1397 | |
| 1398 | // Capture pid and pre-sleep output. |
| 1399 | sleep(1); // Wait a little bit to make sure pid and 1st line were printed. |
| 1400 | std::string err = GetCapturedStderr(); |
| 1401 | EXPECT_THAT(err, StrEq("sleeping for 20s\n")); |
| 1402 | |
| 1403 | CaptureFdOut(); |
| 1404 | std::vector<std::string> lines = android::base::Split(out, "\n"); |
| 1405 | ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out; |
| 1406 | |
| 1407 | int pid = atoi(lines[0].c_str()); |
| 1408 | EXPECT_THAT(lines[1], StrEq("stdout line1")); |
| 1409 | EXPECT_THAT(lines[2], IsEmpty()); // \n |
| 1410 | |
| 1411 | // Then kill the process. |
| 1412 | CaptureFdOut(); |
| 1413 | CaptureStderr(); |
| 1414 | ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid; |
| 1415 | t.join(); |
| 1416 | |
| 1417 | // Finally, check output after murder. |
| 1418 | CaptureFdOut(); |
| 1419 | err = GetCapturedStderr(); |
| 1420 | |
| 1421 | // out starts with the pid, which is an unknown |
| 1422 | EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand + |
| 1423 | " --pid --sleep 20' failed: killed by signal 15\n")); |
| 1424 | EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand + |
| 1425 | " --pid --sleep 20' failed: killed by signal 15\n")); |
| 1426 | } |
| 1427 | |
| 1428 | TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) { |
| 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.RunCommandAsRootUserBuild() on test suite\n") |
| 1433 | return; |
| 1434 | } |
| 1435 | CreateFd("RunCommandAsRootUserBuild.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1436 | if (!PropertiesHelper::IsUserBuild()) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1437 | // Emulates user build if necessarily. |
| 1438 | SetBuildType("user"); |
| 1439 | } |
| 1440 | |
| 1441 | DropRoot(); |
| 1442 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1443 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build())); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1444 | |
| 1445 | // We don't know the exact path of su, so we just check for the 'root ...' commands |
| 1446 | EXPECT_THAT(out, StartsWith("Skipping")); |
| 1447 | EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n")); |
| 1448 | EXPECT_THAT(err, IsEmpty()); |
| 1449 | } |
| 1450 | |
| 1451 | TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) { |
| 1452 | if (!IsStandalone()) { |
| 1453 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 1454 | // to Shell - need to refactor tests to avoid this problem) |
| 1455 | MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n") |
| 1456 | return; |
| 1457 | } |
| 1458 | CreateFd("RunCommandAsRootNonUserBuild.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1459 | if (PropertiesHelper::IsUserBuild()) { |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1460 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); |
| 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | DropRoot(); |
| 1465 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1466 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 1467 | CommandOptions::WithTimeout(1).AsRoot().Build())); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 1468 | |
| 1469 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 1470 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 1471 | } |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1472 | |
Yifan Hong | 48e83a1 | 2017-10-03 14:10:07 -0700 | [diff] [blame] | 1473 | |
| 1474 | TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) { |
| 1475 | if (!IsStandalone()) { |
| 1476 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 1477 | // to Shell - need to refactor tests to avoid this problem) |
| 1478 | MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n") |
| 1479 | return; |
| 1480 | } |
| 1481 | CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt"); |
| 1482 | if (!PropertiesHelper::IsUserBuild()) { |
| 1483 | // Emulates user build if necessarily. |
| 1484 | SetBuildType("user"); |
| 1485 | } |
| 1486 | |
| 1487 | DropRoot(); |
| 1488 | |
| 1489 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 1490 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); |
| 1491 | |
| 1492 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
| 1493 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 1494 | } |
| 1495 | |
| 1496 | TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) { |
| 1497 | if (!IsStandalone()) { |
| 1498 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 1499 | // to Shell - need to refactor tests to avoid this problem) |
| 1500 | MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n") |
| 1501 | return; |
| 1502 | } |
| 1503 | CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt"); |
| 1504 | if (PropertiesHelper::IsUserBuild()) { |
| 1505 | ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n"); |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | DropRoot(); |
| 1510 | |
| 1511 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
| 1512 | CommandOptions::WithTimeout(1).AsRootIfAvailable().Build())); |
| 1513 | |
| 1514 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 1515 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 1516 | } |
| 1517 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1518 | TEST_F(DumpstateUtilTest, RunCommandDropRoot) { |
| 1519 | if (!IsStandalone()) { |
| 1520 | // TODO: temporarily disabled because it might cause other tests to fail after dropping |
| 1521 | // to Shell - need to refactor tests to avoid this problem) |
| 1522 | MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n") |
| 1523 | return; |
| 1524 | } |
| 1525 | CreateFd("RunCommandDropRoot.txt"); |
| 1526 | // First check root case - only available when running with 'adb root'. |
| 1527 | uid_t uid = getuid(); |
| 1528 | if (uid == 0) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1529 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"})); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1530 | EXPECT_THAT(out, StrEq("0\nstdout\n")); |
| 1531 | EXPECT_THAT(err, StrEq("stderr\n")); |
| 1532 | return; |
| 1533 | } |
| 1534 | // Then run dropping root. |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1535 | EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}, |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1536 | CommandOptions::WithTimeout(1).DropRoot().Build())); |
| 1537 | EXPECT_THAT(out, StrEq("2000\nstdout\n")); |
| 1538 | EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n")); |
| 1539 | } |
| 1540 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1541 | TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) { |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1542 | CreateFd("DumpFileNotFound.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1543 | EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1544 | EXPECT_THAT(out, |
| 1545 | StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n")); |
| 1546 | EXPECT_THAT(err, IsEmpty()); |
| 1547 | } |
| 1548 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1549 | TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) { |
| 1550 | CreateFd("DumpFileNotFound.txt"); |
| 1551 | EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist")); |
| 1552 | EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such " |
| 1553 | "file or directory\n")); |
| 1554 | EXPECT_THAT(err, IsEmpty()); |
| 1555 | } |
| 1556 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1557 | TEST_F(DumpstateUtilTest, DumpFileSingleLine) { |
| 1558 | CreateFd("DumpFileSingleLine.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1559 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1560 | EXPECT_THAT(err, IsEmpty()); |
| 1561 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline |
| 1562 | } |
| 1563 | |
| 1564 | TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) { |
| 1565 | CreateFd("DumpFileSingleLineWithNewLine.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1566 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1567 | EXPECT_THAT(err, IsEmpty()); |
| 1568 | EXPECT_THAT(out, StrEq("I AM LINE1\n")); |
| 1569 | } |
| 1570 | |
| 1571 | TEST_F(DumpstateUtilTest, DumpFileMultipleLines) { |
| 1572 | CreateFd("DumpFileMultipleLines.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1573 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1574 | EXPECT_THAT(err, IsEmpty()); |
| 1575 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 1576 | } |
| 1577 | |
| 1578 | TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) { |
| 1579 | CreateFd("DumpFileMultipleLinesWithNewLine.txt"); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1580 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1581 | EXPECT_THAT(err, IsEmpty()); |
| 1582 | EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n")); |
| 1583 | } |
| 1584 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1585 | TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) { |
| 1586 | CreateFd("DumpFileOnDryRun.txt"); |
| 1587 | SetDryRun(true); |
| 1588 | std::string path = kTestDataPath + "single-line.txt"; |
| 1589 | EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt")); |
| 1590 | EXPECT_THAT(err, IsEmpty()); |
| 1591 | EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n")); |
| 1592 | } |
| 1593 | |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1594 | TEST_F(DumpstateUtilTest, DumpFileOnDryRun) { |
| 1595 | CreateFd("DumpFileOnDryRun.txt"); |
| 1596 | SetDryRun(true); |
| 1597 | std::string path = kTestDataPath + "single-line.txt"; |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1598 | 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] | 1599 | EXPECT_THAT(err, IsEmpty()); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1600 | EXPECT_THAT( |
| 1601 | out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:")); |
| 1602 | EXPECT_THAT(out, EndsWith("skipped on dry run\n")); |
Felipe Leme | 46b85da | 2016-11-21 17:40:45 -0800 | [diff] [blame] | 1603 | } |
Ecco Park | 61ffcf7 | 2016-10-27 15:46:26 -0700 | [diff] [blame] | 1604 | |
Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 1605 | } // namespace dumpstate |
| 1606 | } // namespace os |
| 1607 | } // namespace android |