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