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