Felipe Leme | 042c351 | 2016-07-19 17:07:22 -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 | |
| 17 | #include "bugreport.h" |
| 18 | |
| 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 22 | #include <android-base/strings.h> |
| 23 | #include <android-base/test_utils.h> |
| 24 | |
| 25 | #include "sysdeps.h" |
| 26 | #include "adb_utils.h" |
| 27 | |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 28 | using ::testing::_; |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 29 | using ::testing::Action; |
| 30 | using ::testing::ActionInterface; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 31 | using ::testing::DoAll; |
| 32 | using ::testing::ElementsAre; |
| 33 | using ::testing::HasSubstr; |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 34 | using ::testing::MakeAction; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 35 | using ::testing::Return; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 36 | using ::testing::StrEq; |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 37 | using ::testing::WithArg; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 38 | using ::testing::internal::CaptureStderr; |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 39 | using ::testing::internal::CaptureStdout; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 40 | using ::testing::internal::GetCapturedStderr; |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 41 | using ::testing::internal::GetCapturedStdout; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 42 | |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 43 | // Empty function so tests don't need to be linked against file_sync_service.cpp, which requires |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 44 | // SELinux and its transitive dependencies... |
| 45 | bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs, |
| 46 | const char* name) { |
| 47 | ADD_FAILURE() << "do_sync_pull() should have been mocked"; |
| 48 | return false; |
| 49 | } |
| 50 | |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 51 | // Empty functions so tests don't need to be linked against commandline.cpp |
| 52 | DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK(nullptr, nullptr); |
Felipe Leme | 5dab2b4 | 2017-03-20 11:00:11 -0700 | [diff] [blame] | 53 | |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 54 | int send_shell_command(TransportType transport_type, const char* serial, const std::string& command, |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 55 | bool disable_shell_protocol, StandardStreamsCallbackInterface* callback) { |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 56 | ADD_FAILURE() << "send_shell_command() should have been mocked"; |
| 57 | return -42; |
| 58 | } |
| 59 | |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 60 | enum StreamType { |
| 61 | kStreamStdout, |
| 62 | kStreamStderr, |
| 63 | }; |
| 64 | |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 65 | // gmock black magic to provide a WithArg<4>(WriteOnStdout(output)) matcher |
| 66 | typedef void OnStandardStreamsCallbackFunction(StandardStreamsCallbackInterface*); |
| 67 | |
| 68 | class OnStandardStreamsCallbackAction : public ActionInterface<OnStandardStreamsCallbackFunction> { |
| 69 | public: |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 70 | explicit OnStandardStreamsCallbackAction(StreamType type, const std::string& output) |
| 71 | : type_(type), output_(output) { |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 72 | } |
| 73 | virtual Result Perform(const ArgumentTuple& args) { |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 74 | if (type_ == kStreamStdout) { |
| 75 | ::std::tr1::get<0>(args)->OnStdout(output_.c_str(), output_.size()); |
| 76 | } |
| 77 | if (type_ == kStreamStderr) { |
| 78 | ::std::tr1::get<0>(args)->OnStderr(output_.c_str(), output_.size()); |
| 79 | } |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | private: |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 83 | StreamType type_; |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 84 | std::string output_; |
| 85 | }; |
| 86 | |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 87 | // Matcher used to emulated StandardStreamsCallbackInterface.OnStdout(buffer, |
| 88 | // length) |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 89 | Action<OnStandardStreamsCallbackFunction> WriteOnStdout(const std::string& output) { |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 90 | return MakeAction(new OnStandardStreamsCallbackAction(kStreamStdout, output)); |
| 91 | } |
| 92 | |
| 93 | // Matcher used to emulated StandardStreamsCallbackInterface.OnStderr(buffer, |
| 94 | // length) |
| 95 | Action<OnStandardStreamsCallbackFunction> WriteOnStderr(const std::string& output) { |
| 96 | return MakeAction(new OnStandardStreamsCallbackAction(kStreamStderr, output)); |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | typedef int CallbackDoneFunction(StandardStreamsCallbackInterface*); |
| 100 | |
| 101 | class CallbackDoneAction : public ActionInterface<CallbackDoneFunction> { |
| 102 | public: |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 103 | explicit CallbackDoneAction(int status) : status_(status) { |
| 104 | } |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 105 | virtual Result Perform(const ArgumentTuple& args) { |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 106 | int status = ::std::tr1::get<0>(args)->Done(status_); |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 107 | return status; |
| 108 | } |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 109 | |
| 110 | private: |
| 111 | int status_; |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 114 | // Matcher used to emulated StandardStreamsCallbackInterface.Done(status) |
| 115 | Action<CallbackDoneFunction> ReturnCallbackDone(int status = -1337) { |
| 116 | return MakeAction(new CallbackDoneAction(status)); |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 119 | class BugreportMock : public Bugreport { |
| 120 | public: |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 121 | MOCK_METHOD5(SendShellCommand, |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 122 | int(TransportType transport_type, const char* serial, const std::string& command, |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 123 | bool disable_shell_protocol, StandardStreamsCallbackInterface* callback)); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 124 | MOCK_METHOD4(DoSyncPull, bool(const std::vector<const char*>& srcs, const char* dst, |
| 125 | bool copy_attrs, const char* name)); |
Felipe Leme | 954af84 | 2016-07-27 19:23:09 -0700 | [diff] [blame] | 126 | MOCK_METHOD3(UpdateProgress, void(const std::string&, int, int)); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | class BugreportTest : public ::testing::Test { |
| 130 | public: |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 131 | void SetUp() { |
| 132 | if (!getcwd(&cwd_)) { |
| 133 | ADD_FAILURE() << "getcwd failed: " << strerror(errno); |
| 134 | return; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void ExpectBugreportzVersion(const std::string& version) { |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 139 | EXPECT_CALL(br_, |
| 140 | SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -v", false, _)) |
| 141 | .WillOnce(DoAll(WithArg<4>(WriteOnStderr(version.c_str())), |
| 142 | WithArg<4>(ReturnCallbackDone(0)))); |
| 143 | } |
| 144 | |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 145 | void ExpectProgress(int progress, int total, const std::string& file = "file.zip") { |
| 146 | EXPECT_CALL(br_, UpdateProgress(StrEq("generating " + file), progress, total)); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 149 | BugreportMock br_; |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 150 | std::string cwd_; // TODO: make it static |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 153 | // Tests when called with invalid number of arguments |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 154 | TEST_F(BugreportTest, InvalidNumberArgs) { |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 155 | const char* args[] = {"bugreport", "to", "principal"}; |
Felipe Leme | 5dab2b4 | 2017-03-20 11:00:11 -0700 | [diff] [blame] | 156 | ASSERT_EQ(1, br_.DoIt(kTransportLocal, "HannibalLecter", 3, args)); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 159 | // Tests the 'adb bugreport' option when the device does not support 'bugreportz' - it falls back |
| 160 | // to the flat-file format ('bugreport' binary on device) |
| 161 | TEST_F(BugreportTest, NoArgumentsPreNDevice) { |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 162 | // clang-format off |
| 163 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -v", false, _)) |
| 164 | .WillOnce(DoAll(WithArg<4>(WriteOnStderr("")), |
| 165 | // Write some bogus output on stdout to make sure it's ignored |
| 166 | WithArg<4>(WriteOnStdout("Dude, where is my bugreportz?")), |
| 167 | WithArg<4>(ReturnCallbackDone(0)))); |
| 168 | // clang-format on |
| 169 | std::string bugreport = "Reported the bug was."; |
| 170 | CaptureStdout(); |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 171 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreport", false, _)) |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 172 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout(bugreport)), Return(0))); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 173 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 174 | const char* args[] = {"bugreport"}; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 175 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 1, args)); |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 176 | ASSERT_THAT(GetCapturedStdout(), StrEq(bugreport)); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 179 | // Tests the 'adb bugreport' option when the device supports 'bugreportz' version 1.0 - it will |
| 180 | // save the bugreport in the current directory with the name provided by the device. |
| 181 | TEST_F(BugreportTest, NoArgumentsNDevice) { |
| 182 | ExpectBugreportzVersion("1.0"); |
| 183 | |
| 184 | std::string dest_file = |
| 185 | android::base::StringPrintf("%s%cda_bugreport.zip", cwd_.c_str(), OS_PATH_SEPARATOR); |
| 186 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _)) |
| 187 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip")), |
| 188 | WithArg<4>(ReturnCallbackDone()))); |
| 189 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file), |
Felipe Leme | e87fdfa | 2016-08-15 16:01:58 -0700 | [diff] [blame] | 190 | true, StrEq("pulling da_bugreport.zip"))) |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 191 | .WillOnce(Return(true)); |
| 192 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 193 | const char* args[] = {"bugreport"}; |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 194 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 1, args)); |
| 195 | } |
| 196 | |
| 197 | // Tests the 'adb bugreport' option when the device supports 'bugreportz' version 1.1 - it will |
| 198 | // save the bugreport in the current directory with the name provided by the device. |
| 199 | TEST_F(BugreportTest, NoArgumentsPostNDevice) { |
| 200 | ExpectBugreportzVersion("1.1"); |
| 201 | std::string dest_file = |
| 202 | android::base::StringPrintf("%s%cda_bugreport.zip", cwd_.c_str(), OS_PATH_SEPARATOR); |
| 203 | ExpectProgress(50, 100, "da_bugreport.zip"); |
| 204 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
| 205 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("BEGIN:/device/da_bugreport.zip\n")), |
| 206 | WithArg<4>(WriteOnStdout("PROGRESS:50/100\n")), |
| 207 | WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip\n")), |
| 208 | WithArg<4>(ReturnCallbackDone()))); |
| 209 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file), |
Felipe Leme | e87fdfa | 2016-08-15 16:01:58 -0700 | [diff] [blame] | 210 | true, StrEq("pulling da_bugreport.zip"))) |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 211 | .WillOnce(Return(true)); |
| 212 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 213 | const char* args[] = {"bugreport"}; |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 214 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 1, args)); |
| 215 | } |
| 216 | |
| 217 | // Tests 'adb bugreport file.zip' when it succeeds and device does not support progress. |
| 218 | TEST_F(BugreportTest, OkNDevice) { |
| 219 | ExpectBugreportzVersion("1.0"); |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 220 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _)) |
| 221 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")), |
| 222 | WithArg<4>(ReturnCallbackDone()))); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 223 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"), |
Felipe Leme | e87fdfa | 2016-08-15 16:01:58 -0700 | [diff] [blame] | 224 | true, StrEq("pulling file.zip"))) |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 225 | .WillOnce(Return(true)); |
| 226 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 227 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 228 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 229 | } |
| 230 | |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 231 | // Tests 'adb bugreport file.zip' when it succeeds but response was sent in |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 232 | // multiple buffer writers and without progress updates. |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 233 | TEST_F(BugreportTest, OkNDeviceSplitBuffer) { |
| 234 | ExpectBugreportzVersion("1.0"); |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 235 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _)) |
| 236 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device")), |
| 237 | WithArg<4>(WriteOnStdout("/bugreport.zip")), |
| 238 | WithArg<4>(ReturnCallbackDone()))); |
| 239 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"), |
Felipe Leme | e87fdfa | 2016-08-15 16:01:58 -0700 | [diff] [blame] | 240 | true, StrEq("pulling file.zip"))) |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 241 | .WillOnce(Return(true)); |
| 242 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 243 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 244 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 245 | } |
| 246 | |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 247 | // Tests 'adb bugreport file.zip' when it succeeds and displays progress. |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 248 | TEST_F(BugreportTest, OkProgress) { |
| 249 | ExpectBugreportzVersion("1.1"); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 250 | ExpectProgress(1, 100); |
| 251 | ExpectProgress(10, 100); |
| 252 | ExpectProgress(50, 100); |
| 253 | ExpectProgress(99, 100); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 254 | // clang-format off |
| 255 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 256 | // NOTE: DoAll accepts at most 10 arguments, and we're almost reached that limit... |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 257 | .WillOnce(DoAll( |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 258 | // Name might change on OK, so make sure the right one is picked. |
| 259 | WithArg<4>(WriteOnStdout("BEGIN:/device/bugreport___NOT.zip\n")), |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 260 | // Progress line in one write |
| 261 | WithArg<4>(WriteOnStdout("PROGRESS:1/100\n")), |
| 262 | // Add some bogus lines |
Felipe Leme | ab62b55 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 263 | WithArg<4>(WriteOnStdout("\nDUDE:SWEET\n\nBLA\n\nBLA\nBLA\n\n")), |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 264 | // Multiple progress lines in one write |
| 265 | WithArg<4>(WriteOnStdout("PROGRESS:10/100\nPROGRESS:50/100\n")), |
| 266 | // Progress line in multiple writes |
| 267 | WithArg<4>(WriteOnStdout("PROG")), |
| 268 | WithArg<4>(WriteOnStdout("RESS:99")), |
| 269 | WithArg<4>(WriteOnStdout("/100\n")), |
| 270 | // Split last message as well, just in case |
| 271 | WithArg<4>(WriteOnStdout("OK:/device/bugreport")), |
| 272 | WithArg<4>(WriteOnStdout(".zip")), |
| 273 | WithArg<4>(ReturnCallbackDone()))); |
Felipe Leme | ab62b55 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 274 | // clang-format on |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 275 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"), |
Felipe Leme | e87fdfa | 2016-08-15 16:01:58 -0700 | [diff] [blame] | 276 | true, StrEq("pulling file.zip"))) |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 277 | .WillOnce(Return(true)); |
| 278 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 279 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 280 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 281 | } |
| 282 | |
Felipe Leme | 5dab2b4 | 2017-03-20 11:00:11 -0700 | [diff] [blame] | 283 | // Tests 'adb bugreport file.zip' when it succeeds and displays progress, even if progress recedes. |
| 284 | TEST_F(BugreportTest, OkProgressAlwaysForward) { |
| 285 | ExpectBugreportzVersion("1.1"); |
| 286 | ExpectProgress(1, 100); |
| 287 | ExpectProgress(50, 100); |
| 288 | ExpectProgress(75, 100); |
| 289 | // clang-format off |
| 290 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
| 291 | // NOTE: DoAll accepts at most 10 arguments, and we're almost reached that limit... |
| 292 | .WillOnce(DoAll( |
| 293 | WithArg<4>(WriteOnStdout("BEGIN:/device/bugreport.zip\n")), |
| 294 | WithArg<4>(WriteOnStdout("PROGRESS:1/100\n")), |
| 295 | WithArg<4>(WriteOnStdout("PROGRESS:50/100\n")), |
| 296 | // 25 should be ignored becaused it receded. |
| 297 | WithArg<4>(WriteOnStdout("PROGRESS:25/100\n")), |
| 298 | WithArg<4>(WriteOnStdout("PROGRESS:75/100\n")), |
| 299 | // 75 should be ignored becaused it didn't change. |
| 300 | WithArg<4>(WriteOnStdout("PROGRESS:75/100\n")), |
| 301 | WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")), |
| 302 | WithArg<4>(ReturnCallbackDone()))); |
| 303 | // clang-format on |
| 304 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"), |
| 305 | true, StrEq("pulling file.zip"))) |
| 306 | .WillOnce(Return(true)); |
| 307 | |
| 308 | const char* args[] = {"bugreport", "file.zip"}; |
| 309 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 310 | } |
| 311 | |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 312 | // Tests 'adb bugreport dir' when it succeeds and destination is a directory. |
| 313 | TEST_F(BugreportTest, OkDirectory) { |
| 314 | ExpectBugreportzVersion("1.1"); |
| 315 | TemporaryDir td; |
| 316 | std::string dest_file = |
| 317 | android::base::StringPrintf("%s%cda_bugreport.zip", td.path, OS_PATH_SEPARATOR); |
| 318 | |
| 319 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
| 320 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("BEGIN:/device/da_bugreport.zip\n")), |
| 321 | WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip")), |
| 322 | WithArg<4>(ReturnCallbackDone()))); |
| 323 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file), |
Felipe Leme | e87fdfa | 2016-08-15 16:01:58 -0700 | [diff] [blame] | 324 | true, StrEq("pulling da_bugreport.zip"))) |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 325 | .WillOnce(Return(true)); |
| 326 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 327 | const char* args[] = {"bugreport", td.path}; |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 328 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 329 | } |
| 330 | |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 331 | // Tests 'adb bugreport file' when it succeeds |
| 332 | TEST_F(BugreportTest, OkNoExtension) { |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 333 | ExpectBugreportzVersion("1.1"); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 334 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
| 335 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip\n")), |
| 336 | WithArg<4>(ReturnCallbackDone()))); |
| 337 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"), |
Felipe Leme | e87fdfa | 2016-08-15 16:01:58 -0700 | [diff] [blame] | 338 | true, StrEq("pulling file.zip"))) |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 339 | .WillOnce(Return(true)); |
| 340 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 341 | const char* args[] = {"bugreport", "file"}; |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 342 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 343 | } |
| 344 | |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 345 | // Tests 'adb bugreport dir' when it succeeds and destination is a directory and device runs N. |
| 346 | TEST_F(BugreportTest, OkNDeviceDirectory) { |
| 347 | ExpectBugreportzVersion("1.0"); |
| 348 | TemporaryDir td; |
| 349 | std::string dest_file = |
| 350 | android::base::StringPrintf("%s%cda_bugreport.zip", td.path, OS_PATH_SEPARATOR); |
| 351 | |
| 352 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _)) |
| 353 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("BEGIN:/device/da_bugreport.zip\n")), |
| 354 | WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip")), |
| 355 | WithArg<4>(ReturnCallbackDone()))); |
| 356 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file), |
Felipe Leme | e87fdfa | 2016-08-15 16:01:58 -0700 | [diff] [blame] | 357 | true, StrEq("pulling da_bugreport.zip"))) |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 358 | .WillOnce(Return(true)); |
| 359 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 360 | const char* args[] = {"bugreport", td.path}; |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 361 | ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 362 | } |
| 363 | |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 364 | // Tests 'adb bugreport file.zip' when the bugreport itself failed |
| 365 | TEST_F(BugreportTest, BugreportzReturnedFail) { |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 366 | ExpectBugreportzVersion("1.1"); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 367 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
| 368 | .WillOnce( |
| 369 | DoAll(WithArg<4>(WriteOnStdout("FAIL:D'OH!\n")), WithArg<4>(ReturnCallbackDone()))); |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 370 | |
| 371 | CaptureStderr(); |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 372 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 373 | ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 374 | ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!")); |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // Tests 'adb bugreport file.zip' when the bugreport itself failed but response |
| 378 | // was sent in |
| 379 | // multiple buffer writes |
| 380 | TEST_F(BugreportTest, BugreportzReturnedFailSplitBuffer) { |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 381 | ExpectBugreportzVersion("1.1"); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 382 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
| 383 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("FAIL")), WithArg<4>(WriteOnStdout(":D'OH!\n")), |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 384 | WithArg<4>(ReturnCallbackDone()))); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 385 | |
| 386 | CaptureStderr(); |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 387 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 388 | ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 389 | ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!")); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Tests 'adb bugreport file.zip' when the bugreportz returned an unsupported |
| 393 | // response. |
| 394 | TEST_F(BugreportTest, BugreportzReturnedUnsupported) { |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 395 | ExpectBugreportzVersion("1.1"); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 396 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 397 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("bugreportz? What am I, a zombie?")), |
| 398 | WithArg<4>(ReturnCallbackDone()))); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 399 | |
| 400 | CaptureStderr(); |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 401 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 402 | ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 403 | ASSERT_THAT(GetCapturedStderr(), HasSubstr("bugreportz? What am I, a zombie?")); |
| 404 | } |
| 405 | |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 406 | // Tests 'adb bugreport file.zip' when the bugreportz -v command failed |
| 407 | TEST_F(BugreportTest, BugreportzVersionFailed) { |
| 408 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -v", false, _)) |
| 409 | .WillOnce(Return(666)); |
| 410 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 411 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 412 | ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 413 | } |
| 414 | |
Felipe Leme | ab62b55 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 415 | // Tests 'adb bugreport file.zip' when the bugreportz -v returns status 0 but with no output. |
| 416 | TEST_F(BugreportTest, BugreportzVersionEmpty) { |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 417 | ExpectBugreportzVersion(""); |
Felipe Leme | ab62b55 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 418 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 419 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | ab62b55 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 420 | ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 421 | } |
| 422 | |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 423 | // Tests 'adb bugreport file.zip' when the main bugreportz command failed |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 424 | TEST_F(BugreportTest, BugreportzFailed) { |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 425 | ExpectBugreportzVersion("1.1"); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 426 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 427 | .WillOnce(Return(666)); |
| 428 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 429 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 430 | ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 431 | } |
| 432 | |
| 433 | // Tests 'adb bugreport file.zip' when the bugreport could not be pulled |
| 434 | TEST_F(BugreportTest, PullFails) { |
Felipe Leme | e4893a2 | 2016-07-29 17:57:00 -0700 | [diff] [blame] | 435 | ExpectBugreportzVersion("1.1"); |
Felipe Leme | e53b12b | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 436 | EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _)) |
Felipe Leme | 60192aa | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 437 | .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")), |
| 438 | WithArg<4>(ReturnCallbackDone()))); |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 439 | EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"), |
Felipe Leme | 954af84 | 2016-07-27 19:23:09 -0700 | [diff] [blame] | 440 | true, HasSubstr("file.zip"))) |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 441 | .WillOnce(Return(false)); |
| 442 | |
Felipe Leme | fa236de | 2016-08-04 12:03:06 -0700 | [diff] [blame] | 443 | const char* args[] = {"bugreport", "file.zip"}; |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 444 | ASSERT_EQ(1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args)); |
| 445 | } |