blob: 112928523e09596ac11cfd669d14f66de30e2c72 [file] [log] [blame]
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -07001/*
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 Leme8582c5d2016-07-29 17:57:00 -070022#include <android-base/strings.h>
23#include <android-base/test_utils.h>
24
25#include "sysdeps.h"
26#include "adb_utils.h"
27
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070028using ::testing::_;
Felipe Lemeed602ed2016-07-26 12:14:39 -070029using ::testing::Action;
30using ::testing::ActionInterface;
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070031using ::testing::DoAll;
32using ::testing::ElementsAre;
33using ::testing::HasSubstr;
Felipe Lemeed602ed2016-07-26 12:14:39 -070034using ::testing::MakeAction;
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070035using ::testing::Return;
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070036using ::testing::StrEq;
Felipe Lemeed602ed2016-07-26 12:14:39 -070037using ::testing::WithArg;
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070038using ::testing::internal::CaptureStderr;
Felipe Leme1fc578c2016-08-04 12:03:06 -070039using ::testing::internal::CaptureStdout;
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070040using ::testing::internal::GetCapturedStderr;
Felipe Leme1fc578c2016-08-04 12:03:06 -070041using ::testing::internal::GetCapturedStdout;
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070042
Felipe Lemeed602ed2016-07-26 12:14:39 -070043// Empty function so tests don't need to be linked against file_sync_service.cpp, which requires
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070044// SELinux and its transitive dependencies...
45bool 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 Lemeed602ed2016-07-26 12:14:39 -070051// Empty functions so tests don't need to be linked against commandline.cpp
52DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK(nullptr, nullptr);
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070053int usage() {
54 return -42;
55}
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070056int send_shell_command(TransportType transport_type, const char* serial, const std::string& command,
Felipe Lemeed602ed2016-07-26 12:14:39 -070057 bool disable_shell_protocol, StandardStreamsCallbackInterface* callback) {
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070058 ADD_FAILURE() << "send_shell_command() should have been mocked";
59 return -42;
60}
61
Felipe Leme5b786992016-07-26 12:23:40 -070062enum StreamType {
63 kStreamStdout,
64 kStreamStderr,
65};
66
Felipe Lemeed602ed2016-07-26 12:14:39 -070067// gmock black magic to provide a WithArg<4>(WriteOnStdout(output)) matcher
68typedef void OnStandardStreamsCallbackFunction(StandardStreamsCallbackInterface*);
69
70class OnStandardStreamsCallbackAction : public ActionInterface<OnStandardStreamsCallbackFunction> {
71 public:
Felipe Leme5b786992016-07-26 12:23:40 -070072 explicit OnStandardStreamsCallbackAction(StreamType type, const std::string& output)
73 : type_(type), output_(output) {
Felipe Lemeed602ed2016-07-26 12:14:39 -070074 }
75 virtual Result Perform(const ArgumentTuple& args) {
Felipe Leme5b786992016-07-26 12:23:40 -070076 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 Lemeed602ed2016-07-26 12:14:39 -070082 }
83
84 private:
Felipe Leme5b786992016-07-26 12:23:40 -070085 StreamType type_;
Felipe Lemeed602ed2016-07-26 12:14:39 -070086 std::string output_;
87};
88
Felipe Leme5b786992016-07-26 12:23:40 -070089// Matcher used to emulated StandardStreamsCallbackInterface.OnStdout(buffer,
90// length)
Felipe Lemeed602ed2016-07-26 12:14:39 -070091Action<OnStandardStreamsCallbackFunction> WriteOnStdout(const std::string& output) {
Felipe Leme5b786992016-07-26 12:23:40 -070092 return MakeAction(new OnStandardStreamsCallbackAction(kStreamStdout, output));
93}
94
95// Matcher used to emulated StandardStreamsCallbackInterface.OnStderr(buffer,
96// length)
97Action<OnStandardStreamsCallbackFunction> WriteOnStderr(const std::string& output) {
98 return MakeAction(new OnStandardStreamsCallbackAction(kStreamStderr, output));
Felipe Lemeed602ed2016-07-26 12:14:39 -070099}
100
101typedef int CallbackDoneFunction(StandardStreamsCallbackInterface*);
102
103class CallbackDoneAction : public ActionInterface<CallbackDoneFunction> {
104 public:
Felipe Leme5b786992016-07-26 12:23:40 -0700105 explicit CallbackDoneAction(int status) : status_(status) {
106 }
Felipe Lemeed602ed2016-07-26 12:14:39 -0700107 virtual Result Perform(const ArgumentTuple& args) {
Felipe Leme5b786992016-07-26 12:23:40 -0700108 int status = ::std::tr1::get<0>(args)->Done(status_);
Felipe Lemeed602ed2016-07-26 12:14:39 -0700109 return status;
110 }
Felipe Leme5b786992016-07-26 12:23:40 -0700111
112 private:
113 int status_;
Felipe Lemeed602ed2016-07-26 12:14:39 -0700114};
115
Felipe Leme5b786992016-07-26 12:23:40 -0700116// Matcher used to emulated StandardStreamsCallbackInterface.Done(status)
117Action<CallbackDoneFunction> ReturnCallbackDone(int status = -1337) {
118 return MakeAction(new CallbackDoneAction(status));
Felipe Lemeed602ed2016-07-26 12:14:39 -0700119}
120
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700121class BugreportMock : public Bugreport {
122 public:
Felipe Lemeed602ed2016-07-26 12:14:39 -0700123 MOCK_METHOD5(SendShellCommand,
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700124 int(TransportType transport_type, const char* serial, const std::string& command,
Felipe Lemeed602ed2016-07-26 12:14:39 -0700125 bool disable_shell_protocol, StandardStreamsCallbackInterface* callback));
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700126 MOCK_METHOD4(DoSyncPull, bool(const std::vector<const char*>& srcs, const char* dst,
127 bool copy_attrs, const char* name));
Felipe Leme9143a802016-07-27 19:23:09 -0700128 MOCK_METHOD3(UpdateProgress, void(const std::string&, int, int));
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700129};
130
131class BugreportTest : public ::testing::Test {
132 public:
Felipe Leme8582c5d2016-07-29 17:57:00 -0700133 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 Leme5b786992016-07-26 12:23:40 -0700141 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 Leme8582c5d2016-07-29 17:57:00 -0700147 void ExpectProgress(int progress, int total, const std::string& file = "file.zip") {
148 EXPECT_CALL(br_, UpdateProgress(StrEq("generating " + file), progress, total));
Felipe Leme5b786992016-07-26 12:23:40 -0700149 }
150
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700151 BugreportMock br_;
Felipe Leme8582c5d2016-07-29 17:57:00 -0700152 std::string cwd_; // TODO: make it static
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700153};
154
Felipe Leme8582c5d2016-07-29 17:57:00 -0700155// Tests when called with invalid number of arguments
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700156TEST_F(BugreportTest, InvalidNumberArgs) {
Felipe Leme1fc578c2016-08-04 12:03:06 -0700157 const char* args[] = {"bugreport", "to", "principal"};
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700158 ASSERT_EQ(-42, br_.DoIt(kTransportLocal, "HannibalLecter", 3, args));
159}
160
Felipe Leme8582c5d2016-07-29 17:57:00 -0700161// 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)
163TEST_F(BugreportTest, NoArgumentsPreNDevice) {
Felipe Leme1fc578c2016-08-04 12:03:06 -0700164 // 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 Lemeed602ed2016-07-26 12:14:39 -0700173 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreport", false, _))
Felipe Leme1fc578c2016-08-04 12:03:06 -0700174 .WillOnce(DoAll(WithArg<4>(WriteOnStdout(bugreport)), Return(0)));
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700175
Felipe Leme1fc578c2016-08-04 12:03:06 -0700176 const char* args[] = {"bugreport"};
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700177 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 1, args));
Felipe Leme1fc578c2016-08-04 12:03:06 -0700178 ASSERT_THAT(GetCapturedStdout(), StrEq(bugreport));
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700179}
180
Felipe Leme8582c5d2016-07-29 17:57:00 -0700181// 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.
183TEST_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 Leme4fa67e12016-08-15 16:01:58 -0700192 true, StrEq("pulling da_bugreport.zip")))
Felipe Leme8582c5d2016-07-29 17:57:00 -0700193 .WillOnce(Return(true));
194
Felipe Leme1fc578c2016-08-04 12:03:06 -0700195 const char* args[] = {"bugreport"};
Felipe Leme8582c5d2016-07-29 17:57:00 -0700196 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.
201TEST_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 Leme4fa67e12016-08-15 16:01:58 -0700212 true, StrEq("pulling da_bugreport.zip")))
Felipe Leme8582c5d2016-07-29 17:57:00 -0700213 .WillOnce(Return(true));
214
Felipe Leme1fc578c2016-08-04 12:03:06 -0700215 const char* args[] = {"bugreport"};
Felipe Leme8582c5d2016-07-29 17:57:00 -0700216 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.
220TEST_F(BugreportTest, OkNDevice) {
221 ExpectBugreportzVersion("1.0");
Felipe Lemeed602ed2016-07-26 12:14:39 -0700222 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _))
223 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")),
224 WithArg<4>(ReturnCallbackDone())));
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700225 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Leme4fa67e12016-08-15 16:01:58 -0700226 true, StrEq("pulling file.zip")))
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700227 .WillOnce(Return(true));
228
Felipe Leme1fc578c2016-08-04 12:03:06 -0700229 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700230 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
231}
232
Felipe Lemeed602ed2016-07-26 12:14:39 -0700233// Tests 'adb bugreport file.zip' when it succeeds but response was sent in
Felipe Leme5b786992016-07-26 12:23:40 -0700234// multiple buffer writers and without progress updates.
Felipe Leme8582c5d2016-07-29 17:57:00 -0700235TEST_F(BugreportTest, OkNDeviceSplitBuffer) {
236 ExpectBugreportzVersion("1.0");
Felipe Lemeed602ed2016-07-26 12:14:39 -0700237 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 Leme4fa67e12016-08-15 16:01:58 -0700242 true, StrEq("pulling file.zip")))
Felipe Lemeed602ed2016-07-26 12:14:39 -0700243 .WillOnce(Return(true));
244
Felipe Leme1fc578c2016-08-04 12:03:06 -0700245 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemeed602ed2016-07-26 12:14:39 -0700246 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
247}
248
Felipe Leme5b786992016-07-26 12:23:40 -0700249// Tests 'adb bugreport file.zip' when it succeeds and displays progress.
Felipe Leme8582c5d2016-07-29 17:57:00 -0700250TEST_F(BugreportTest, OkProgress) {
251 ExpectBugreportzVersion("1.1");
Felipe Leme5b786992016-07-26 12:23:40 -0700252 ExpectProgress(1, 100);
253 ExpectProgress(10, 100);
254 ExpectProgress(50, 100);
255 ExpectProgress(99, 100);
Felipe Leme5b786992016-07-26 12:23:40 -0700256 // clang-format off
257 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Leme8582c5d2016-07-29 17:57:00 -0700258 // NOTE: DoAll accepts at most 10 arguments, and we're almost reached that limit...
Felipe Leme5b786992016-07-26 12:23:40 -0700259 .WillOnce(DoAll(
Felipe Leme8582c5d2016-07-29 17:57:00 -0700260 // Name might change on OK, so make sure the right one is picked.
261 WithArg<4>(WriteOnStdout("BEGIN:/device/bugreport___NOT.zip\n")),
Felipe Leme5b786992016-07-26 12:23:40 -0700262 // Progress line in one write
263 WithArg<4>(WriteOnStdout("PROGRESS:1/100\n")),
264 // Add some bogus lines
Felipe Leme93fffb82016-07-29 15:47:00 -0700265 WithArg<4>(WriteOnStdout("\nDUDE:SWEET\n\nBLA\n\nBLA\nBLA\n\n")),
Felipe Leme5b786992016-07-26 12:23:40 -0700266 // 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 Leme93fffb82016-07-29 15:47:00 -0700276 // clang-format on
Felipe Leme5b786992016-07-26 12:23:40 -0700277 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Leme4fa67e12016-08-15 16:01:58 -0700278 true, StrEq("pulling file.zip")))
Felipe Leme5b786992016-07-26 12:23:40 -0700279 .WillOnce(Return(true));
280
Felipe Leme1fc578c2016-08-04 12:03:06 -0700281 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme5b786992016-07-26 12:23:40 -0700282 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
283}
284
Felipe Leme8582c5d2016-07-29 17:57:00 -0700285// Tests 'adb bugreport dir' when it succeeds and destination is a directory.
286TEST_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 Leme4fa67e12016-08-15 16:01:58 -0700297 true, StrEq("pulling da_bugreport.zip")))
Felipe Leme8582c5d2016-07-29 17:57:00 -0700298 .WillOnce(Return(true));
299
Felipe Leme1fc578c2016-08-04 12:03:06 -0700300 const char* args[] = {"bugreport", td.path};
Felipe Leme8582c5d2016-07-29 17:57:00 -0700301 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
302}
303
Felipe Leme5b786992016-07-26 12:23:40 -0700304// Tests 'adb bugreport file' when it succeeds
305TEST_F(BugreportTest, OkNoExtension) {
Felipe Leme8582c5d2016-07-29 17:57:00 -0700306 ExpectBugreportzVersion("1.1");
Felipe Leme5b786992016-07-26 12:23:40 -0700307 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 Leme4fa67e12016-08-15 16:01:58 -0700311 true, StrEq("pulling file.zip")))
Felipe Leme5b786992016-07-26 12:23:40 -0700312 .WillOnce(Return(true));
313
Felipe Leme1fc578c2016-08-04 12:03:06 -0700314 const char* args[] = {"bugreport", "file"};
Felipe Leme5b786992016-07-26 12:23:40 -0700315 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
316}
317
Felipe Leme8582c5d2016-07-29 17:57:00 -0700318// Tests 'adb bugreport dir' when it succeeds and destination is a directory and device runs N.
319TEST_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 Leme4fa67e12016-08-15 16:01:58 -0700330 true, StrEq("pulling da_bugreport.zip")))
Felipe Leme8582c5d2016-07-29 17:57:00 -0700331 .WillOnce(Return(true));
332
Felipe Leme1fc578c2016-08-04 12:03:06 -0700333 const char* args[] = {"bugreport", td.path};
Felipe Leme8582c5d2016-07-29 17:57:00 -0700334 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
335}
336
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700337// Tests 'adb bugreport file.zip' when the bugreport itself failed
338TEST_F(BugreportTest, BugreportzReturnedFail) {
Felipe Leme8582c5d2016-07-29 17:57:00 -0700339 ExpectBugreportzVersion("1.1");
Felipe Leme5b786992016-07-26 12:23:40 -0700340 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
341 .WillOnce(
342 DoAll(WithArg<4>(WriteOnStdout("FAIL:D'OH!\n")), WithArg<4>(ReturnCallbackDone())));
Felipe Lemeed602ed2016-07-26 12:14:39 -0700343
344 CaptureStderr();
Felipe Leme1fc578c2016-08-04 12:03:06 -0700345 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemeed602ed2016-07-26 12:14:39 -0700346 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
Felipe Leme5b786992016-07-26 12:23:40 -0700347 ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!"));
Felipe Lemeed602ed2016-07-26 12:14:39 -0700348}
349
350// Tests 'adb bugreport file.zip' when the bugreport itself failed but response
351// was sent in
352// multiple buffer writes
353TEST_F(BugreportTest, BugreportzReturnedFailSplitBuffer) {
Felipe Leme8582c5d2016-07-29 17:57:00 -0700354 ExpectBugreportzVersion("1.1");
Felipe Leme5b786992016-07-26 12:23:40 -0700355 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
356 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("FAIL")), WithArg<4>(WriteOnStdout(":D'OH!\n")),
Felipe Lemeed602ed2016-07-26 12:14:39 -0700357 WithArg<4>(ReturnCallbackDone())));
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700358
359 CaptureStderr();
Felipe Leme1fc578c2016-08-04 12:03:06 -0700360 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700361 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
Felipe Leme5b786992016-07-26 12:23:40 -0700362 ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!"));
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700363}
364
365// Tests 'adb bugreport file.zip' when the bugreportz returned an unsupported
366// response.
367TEST_F(BugreportTest, BugreportzReturnedUnsupported) {
Felipe Leme8582c5d2016-07-29 17:57:00 -0700368 ExpectBugreportzVersion("1.1");
Felipe Leme5b786992016-07-26 12:23:40 -0700369 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Lemeed602ed2016-07-26 12:14:39 -0700370 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("bugreportz? What am I, a zombie?")),
371 WithArg<4>(ReturnCallbackDone())));
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700372
373 CaptureStderr();
Felipe Leme1fc578c2016-08-04 12:03:06 -0700374 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700375 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
376 ASSERT_THAT(GetCapturedStderr(), HasSubstr("bugreportz? What am I, a zombie?"));
377}
378
Felipe Leme5b786992016-07-26 12:23:40 -0700379// Tests 'adb bugreport file.zip' when the bugreportz -v command failed
380TEST_F(BugreportTest, BugreportzVersionFailed) {
381 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -v", false, _))
382 .WillOnce(Return(666));
383
Felipe Leme1fc578c2016-08-04 12:03:06 -0700384 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme5b786992016-07-26 12:23:40 -0700385 ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
386}
387
Felipe Leme93fffb82016-07-29 15:47:00 -0700388// Tests 'adb bugreport file.zip' when the bugreportz -v returns status 0 but with no output.
389TEST_F(BugreportTest, BugreportzVersionEmpty) {
Felipe Leme8582c5d2016-07-29 17:57:00 -0700390 ExpectBugreportzVersion("");
Felipe Leme93fffb82016-07-29 15:47:00 -0700391
Felipe Leme1fc578c2016-08-04 12:03:06 -0700392 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme93fffb82016-07-29 15:47:00 -0700393 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
394}
395
Felipe Leme5b786992016-07-26 12:23:40 -0700396// Tests 'adb bugreport file.zip' when the main bugreportz command failed
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700397TEST_F(BugreportTest, BugreportzFailed) {
Felipe Leme8582c5d2016-07-29 17:57:00 -0700398 ExpectBugreportzVersion("1.1");
Felipe Leme5b786992016-07-26 12:23:40 -0700399 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700400 .WillOnce(Return(666));
401
Felipe Leme1fc578c2016-08-04 12:03:06 -0700402 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700403 ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
404}
405
406// Tests 'adb bugreport file.zip' when the bugreport could not be pulled
407TEST_F(BugreportTest, PullFails) {
Felipe Leme8582c5d2016-07-29 17:57:00 -0700408 ExpectBugreportzVersion("1.1");
Felipe Leme5b786992016-07-26 12:23:40 -0700409 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Lemeed602ed2016-07-26 12:14:39 -0700410 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")),
411 WithArg<4>(ReturnCallbackDone())));
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700412 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Leme9143a802016-07-27 19:23:09 -0700413 true, HasSubstr("file.zip")))
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700414 .WillOnce(Return(false));
415
Felipe Leme1fc578c2016-08-04 12:03:06 -0700416 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700417 ASSERT_EQ(1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
418}