blob: f9a0a5e8dd3ea617441239cc56e1a5ef78af9494 [file] [log] [blame]
Felipe Leme042c3512016-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 Lemee4893a22016-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 Leme042c3512016-07-19 17:07:22 -070028using ::testing::_;
Felipe Leme60192aa2016-07-26 12:14:39 -070029using ::testing::Action;
30using ::testing::ActionInterface;
Felipe Leme042c3512016-07-19 17:07:22 -070031using ::testing::DoAll;
32using ::testing::ElementsAre;
33using ::testing::HasSubstr;
Felipe Leme60192aa2016-07-26 12:14:39 -070034using ::testing::MakeAction;
Felipe Leme042c3512016-07-19 17:07:22 -070035using ::testing::Return;
Felipe Leme042c3512016-07-19 17:07:22 -070036using ::testing::StrEq;
Felipe Leme60192aa2016-07-26 12:14:39 -070037using ::testing::WithArg;
Felipe Leme042c3512016-07-19 17:07:22 -070038using ::testing::internal::CaptureStderr;
39using ::testing::internal::GetCapturedStderr;
40
Felipe Leme60192aa2016-07-26 12:14:39 -070041// Empty function so tests don't need to be linked against file_sync_service.cpp, which requires
Felipe Leme042c3512016-07-19 17:07:22 -070042// SELinux and its transitive dependencies...
43bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
44 const char* name) {
45 ADD_FAILURE() << "do_sync_pull() should have been mocked";
46 return false;
47}
48
Felipe Leme60192aa2016-07-26 12:14:39 -070049// Empty functions so tests don't need to be linked against commandline.cpp
50DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK(nullptr, nullptr);
Felipe Leme042c3512016-07-19 17:07:22 -070051int usage() {
52 return -42;
53}
Felipe Leme042c3512016-07-19 17:07:22 -070054int send_shell_command(TransportType transport_type, const char* serial, const std::string& command,
Felipe Leme60192aa2016-07-26 12:14:39 -070055 bool disable_shell_protocol, StandardStreamsCallbackInterface* callback) {
Felipe Leme042c3512016-07-19 17:07:22 -070056 ADD_FAILURE() << "send_shell_command() should have been mocked";
57 return -42;
58}
59
Felipe Lemee53b12b2016-07-26 12:23:40 -070060enum StreamType {
61 kStreamStdout,
62 kStreamStderr,
63};
64
Felipe Leme60192aa2016-07-26 12:14:39 -070065// gmock black magic to provide a WithArg<4>(WriteOnStdout(output)) matcher
66typedef void OnStandardStreamsCallbackFunction(StandardStreamsCallbackInterface*);
67
68class OnStandardStreamsCallbackAction : public ActionInterface<OnStandardStreamsCallbackFunction> {
69 public:
Felipe Lemee53b12b2016-07-26 12:23:40 -070070 explicit OnStandardStreamsCallbackAction(StreamType type, const std::string& output)
71 : type_(type), output_(output) {
Felipe Leme60192aa2016-07-26 12:14:39 -070072 }
73 virtual Result Perform(const ArgumentTuple& args) {
Felipe Lemee53b12b2016-07-26 12:23:40 -070074 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 Leme60192aa2016-07-26 12:14:39 -070080 }
81
82 private:
Felipe Lemee53b12b2016-07-26 12:23:40 -070083 StreamType type_;
Felipe Leme60192aa2016-07-26 12:14:39 -070084 std::string output_;
85};
86
Felipe Lemee53b12b2016-07-26 12:23:40 -070087// Matcher used to emulated StandardStreamsCallbackInterface.OnStdout(buffer,
88// length)
Felipe Leme60192aa2016-07-26 12:14:39 -070089Action<OnStandardStreamsCallbackFunction> WriteOnStdout(const std::string& output) {
Felipe Lemee53b12b2016-07-26 12:23:40 -070090 return MakeAction(new OnStandardStreamsCallbackAction(kStreamStdout, output));
91}
92
93// Matcher used to emulated StandardStreamsCallbackInterface.OnStderr(buffer,
94// length)
95Action<OnStandardStreamsCallbackFunction> WriteOnStderr(const std::string& output) {
96 return MakeAction(new OnStandardStreamsCallbackAction(kStreamStderr, output));
Felipe Leme60192aa2016-07-26 12:14:39 -070097}
98
99typedef int CallbackDoneFunction(StandardStreamsCallbackInterface*);
100
101class CallbackDoneAction : public ActionInterface<CallbackDoneFunction> {
102 public:
Felipe Lemee53b12b2016-07-26 12:23:40 -0700103 explicit CallbackDoneAction(int status) : status_(status) {
104 }
Felipe Leme60192aa2016-07-26 12:14:39 -0700105 virtual Result Perform(const ArgumentTuple& args) {
Felipe Lemee53b12b2016-07-26 12:23:40 -0700106 int status = ::std::tr1::get<0>(args)->Done(status_);
Felipe Leme60192aa2016-07-26 12:14:39 -0700107 return status;
108 }
Felipe Lemee53b12b2016-07-26 12:23:40 -0700109
110 private:
111 int status_;
Felipe Leme60192aa2016-07-26 12:14:39 -0700112};
113
Felipe Lemee53b12b2016-07-26 12:23:40 -0700114// Matcher used to emulated StandardStreamsCallbackInterface.Done(status)
115Action<CallbackDoneFunction> ReturnCallbackDone(int status = -1337) {
116 return MakeAction(new CallbackDoneAction(status));
Felipe Leme60192aa2016-07-26 12:14:39 -0700117}
118
Felipe Leme042c3512016-07-19 17:07:22 -0700119class BugreportMock : public Bugreport {
120 public:
Felipe Leme60192aa2016-07-26 12:14:39 -0700121 MOCK_METHOD5(SendShellCommand,
Felipe Leme042c3512016-07-19 17:07:22 -0700122 int(TransportType transport_type, const char* serial, const std::string& command,
Felipe Leme60192aa2016-07-26 12:14:39 -0700123 bool disable_shell_protocol, StandardStreamsCallbackInterface* callback));
Felipe Leme042c3512016-07-19 17:07:22 -0700124 MOCK_METHOD4(DoSyncPull, bool(const std::vector<const char*>& srcs, const char* dst,
125 bool copy_attrs, const char* name));
Felipe Leme954af842016-07-27 19:23:09 -0700126 MOCK_METHOD3(UpdateProgress, void(const std::string&, int, int));
Felipe Leme042c3512016-07-19 17:07:22 -0700127};
128
129class BugreportTest : public ::testing::Test {
130 public:
Felipe Lemee4893a22016-07-29 17:57:00 -0700131 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 Lemee53b12b2016-07-26 12:23:40 -0700139 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 Lemee4893a22016-07-29 17:57:00 -0700145 void ExpectProgress(int progress, int total, const std::string& file = "file.zip") {
146 EXPECT_CALL(br_, UpdateProgress(StrEq("generating " + file), progress, total));
Felipe Lemee53b12b2016-07-26 12:23:40 -0700147 }
148
Felipe Leme042c3512016-07-19 17:07:22 -0700149 BugreportMock br_;
Felipe Lemee4893a22016-07-29 17:57:00 -0700150 std::string cwd_; // TODO: make it static
Felipe Leme042c3512016-07-19 17:07:22 -0700151};
152
Felipe Lemee4893a22016-07-29 17:57:00 -0700153// Tests when called with invalid number of arguments
Felipe Leme042c3512016-07-19 17:07:22 -0700154TEST_F(BugreportTest, InvalidNumberArgs) {
155 const char* args[1024] = {"bugreport", "to", "principal"};
156 ASSERT_EQ(-42, br_.DoIt(kTransportLocal, "HannibalLecter", 3, args));
157}
158
Felipe Lemee4893a22016-07-29 17:57:00 -0700159// 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)
161TEST_F(BugreportTest, NoArgumentsPreNDevice) {
162 ExpectBugreportzVersion("");
Felipe Leme60192aa2016-07-26 12:14:39 -0700163 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreport", false, _))
Felipe Leme042c3512016-07-19 17:07:22 -0700164 .WillOnce(Return(0));
165
166 const char* args[1024] = {"bugreport"};
167 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 1, args));
168}
169
Felipe Lemee4893a22016-07-29 17:57:00 -0700170// Tests the 'adb bugreport' option when the device supports 'bugreportz' version 1.0 - it will
171// save the bugreport in the current directory with the name provided by the device.
172TEST_F(BugreportTest, NoArgumentsNDevice) {
173 ExpectBugreportzVersion("1.0");
174
175 std::string dest_file =
176 android::base::StringPrintf("%s%cda_bugreport.zip", cwd_.c_str(), OS_PATH_SEPARATOR);
177 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _))
178 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip")),
179 WithArg<4>(ReturnCallbackDone())));
180 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file),
181 true, StrEq("generating da_bugreport.zip")))
182 .WillOnce(Return(true));
183
184 const char* args[1024] = {"bugreport"};
185 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 1, args));
186}
187
188// Tests the 'adb bugreport' option when the device supports 'bugreportz' version 1.1 - it will
189// save the bugreport in the current directory with the name provided by the device.
190TEST_F(BugreportTest, NoArgumentsPostNDevice) {
191 ExpectBugreportzVersion("1.1");
192 std::string dest_file =
193 android::base::StringPrintf("%s%cda_bugreport.zip", cwd_.c_str(), OS_PATH_SEPARATOR);
194 ExpectProgress(50, 100, "da_bugreport.zip");
195 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
196 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("BEGIN:/device/da_bugreport.zip\n")),
197 WithArg<4>(WriteOnStdout("PROGRESS:50/100\n")),
198 WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip\n")),
199 WithArg<4>(ReturnCallbackDone())));
200 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file),
201 true, StrEq("generating da_bugreport.zip")))
202 .WillOnce(Return(true));
203
204 const char* args[1024] = {"bugreport"};
205 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 1, args));
206}
207
208// Tests 'adb bugreport file.zip' when it succeeds and device does not support progress.
209TEST_F(BugreportTest, OkNDevice) {
210 ExpectBugreportzVersion("1.0");
Felipe Leme60192aa2016-07-26 12:14:39 -0700211 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _))
212 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")),
213 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700214 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Lemee4893a22016-07-29 17:57:00 -0700215 true, StrEq("generating file.zip")))
Felipe Leme042c3512016-07-19 17:07:22 -0700216 .WillOnce(Return(true));
217
218 const char* args[1024] = {"bugreport", "file.zip"};
219 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
220}
221
Felipe Leme60192aa2016-07-26 12:14:39 -0700222// Tests 'adb bugreport file.zip' when it succeeds but response was sent in
Felipe Lemee53b12b2016-07-26 12:23:40 -0700223// multiple buffer writers and without progress updates.
Felipe Lemee4893a22016-07-29 17:57:00 -0700224TEST_F(BugreportTest, OkNDeviceSplitBuffer) {
225 ExpectBugreportzVersion("1.0");
Felipe Leme60192aa2016-07-26 12:14:39 -0700226 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _))
227 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device")),
228 WithArg<4>(WriteOnStdout("/bugreport.zip")),
229 WithArg<4>(ReturnCallbackDone())));
230 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Lemee4893a22016-07-29 17:57:00 -0700231 true, StrEq("generating file.zip")))
Felipe Leme60192aa2016-07-26 12:14:39 -0700232 .WillOnce(Return(true));
233
234 const char* args[1024] = {"bugreport", "file.zip"};
235 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
236}
237
Felipe Lemee53b12b2016-07-26 12:23:40 -0700238// Tests 'adb bugreport file.zip' when it succeeds and displays progress.
Felipe Lemee4893a22016-07-29 17:57:00 -0700239TEST_F(BugreportTest, OkProgress) {
240 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700241 ExpectProgress(1, 100);
242 ExpectProgress(10, 100);
243 ExpectProgress(50, 100);
244 ExpectProgress(99, 100);
Felipe Lemee53b12b2016-07-26 12:23:40 -0700245 // clang-format off
246 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Lemee4893a22016-07-29 17:57:00 -0700247 // NOTE: DoAll accepts at most 10 arguments, and we're almost reached that limit...
Felipe Lemee53b12b2016-07-26 12:23:40 -0700248 .WillOnce(DoAll(
Felipe Lemee4893a22016-07-29 17:57:00 -0700249 // Name might change on OK, so make sure the right one is picked.
250 WithArg<4>(WriteOnStdout("BEGIN:/device/bugreport___NOT.zip\n")),
Felipe Lemee53b12b2016-07-26 12:23:40 -0700251 // Progress line in one write
252 WithArg<4>(WriteOnStdout("PROGRESS:1/100\n")),
253 // Add some bogus lines
Felipe Lemeab62b552016-07-29 15:47:00 -0700254 WithArg<4>(WriteOnStdout("\nDUDE:SWEET\n\nBLA\n\nBLA\nBLA\n\n")),
Felipe Lemee53b12b2016-07-26 12:23:40 -0700255 // Multiple progress lines in one write
256 WithArg<4>(WriteOnStdout("PROGRESS:10/100\nPROGRESS:50/100\n")),
257 // Progress line in multiple writes
258 WithArg<4>(WriteOnStdout("PROG")),
259 WithArg<4>(WriteOnStdout("RESS:99")),
260 WithArg<4>(WriteOnStdout("/100\n")),
261 // Split last message as well, just in case
262 WithArg<4>(WriteOnStdout("OK:/device/bugreport")),
263 WithArg<4>(WriteOnStdout(".zip")),
264 WithArg<4>(ReturnCallbackDone())));
Felipe Lemeab62b552016-07-29 15:47:00 -0700265 // clang-format on
Felipe Lemee53b12b2016-07-26 12:23:40 -0700266 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Lemee4893a22016-07-29 17:57:00 -0700267 true, StrEq("generating file.zip")))
Felipe Lemee53b12b2016-07-26 12:23:40 -0700268 .WillOnce(Return(true));
269
270 const char* args[1024] = {"bugreport", "file.zip"};
271 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
272}
273
Felipe Lemee4893a22016-07-29 17:57:00 -0700274// Tests 'adb bugreport dir' when it succeeds and destination is a directory.
275TEST_F(BugreportTest, OkDirectory) {
276 ExpectBugreportzVersion("1.1");
277 TemporaryDir td;
278 std::string dest_file =
279 android::base::StringPrintf("%s%cda_bugreport.zip", td.path, OS_PATH_SEPARATOR);
280
281 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
282 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("BEGIN:/device/da_bugreport.zip\n")),
283 WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip")),
284 WithArg<4>(ReturnCallbackDone())));
285 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file),
286 true, StrEq("generating da_bugreport.zip")))
287 .WillOnce(Return(true));
288
289 const char* args[1024] = {"bugreport", td.path};
290 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
291}
292
Felipe Lemee53b12b2016-07-26 12:23:40 -0700293// Tests 'adb bugreport file' when it succeeds
294TEST_F(BugreportTest, OkNoExtension) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700295 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700296 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
297 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip\n")),
298 WithArg<4>(ReturnCallbackDone())));
299 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Lemee4893a22016-07-29 17:57:00 -0700300 true, StrEq("generating file.zip")))
Felipe Lemee53b12b2016-07-26 12:23:40 -0700301 .WillOnce(Return(true));
302
303 const char* args[1024] = {"bugreport", "file"};
304 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
305}
306
Felipe Lemee4893a22016-07-29 17:57:00 -0700307// Tests 'adb bugreport dir' when it succeeds and destination is a directory and device runs N.
308TEST_F(BugreportTest, OkNDeviceDirectory) {
309 ExpectBugreportzVersion("1.0");
310 TemporaryDir td;
311 std::string dest_file =
312 android::base::StringPrintf("%s%cda_bugreport.zip", td.path, OS_PATH_SEPARATOR);
313
314 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _))
315 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("BEGIN:/device/da_bugreport.zip\n")),
316 WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip")),
317 WithArg<4>(ReturnCallbackDone())));
318 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file),
319 true, StrEq("generating da_bugreport.zip")))
320 .WillOnce(Return(true));
321
322 const char* args[1024] = {"bugreport", td.path};
323 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
324}
325
Felipe Leme042c3512016-07-19 17:07:22 -0700326// Tests 'adb bugreport file.zip' when the bugreport itself failed
327TEST_F(BugreportTest, BugreportzReturnedFail) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700328 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700329 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
330 .WillOnce(
331 DoAll(WithArg<4>(WriteOnStdout("FAIL:D'OH!\n")), WithArg<4>(ReturnCallbackDone())));
Felipe Leme60192aa2016-07-26 12:14:39 -0700332
333 CaptureStderr();
334 const char* args[1024] = {"bugreport", "file.zip"};
335 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
Felipe Lemee53b12b2016-07-26 12:23:40 -0700336 ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!"));
Felipe Leme60192aa2016-07-26 12:14:39 -0700337}
338
339// Tests 'adb bugreport file.zip' when the bugreport itself failed but response
340// was sent in
341// multiple buffer writes
342TEST_F(BugreportTest, BugreportzReturnedFailSplitBuffer) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700343 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700344 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
345 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("FAIL")), WithArg<4>(WriteOnStdout(":D'OH!\n")),
Felipe Leme60192aa2016-07-26 12:14:39 -0700346 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700347
348 CaptureStderr();
349 const char* args[1024] = {"bugreport", "file.zip"};
350 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
Felipe Lemee53b12b2016-07-26 12:23:40 -0700351 ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!"));
Felipe Leme042c3512016-07-19 17:07:22 -0700352}
353
354// Tests 'adb bugreport file.zip' when the bugreportz returned an unsupported
355// response.
356TEST_F(BugreportTest, BugreportzReturnedUnsupported) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700357 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700358 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Leme60192aa2016-07-26 12:14:39 -0700359 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("bugreportz? What am I, a zombie?")),
360 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700361
362 CaptureStderr();
363 const char* args[1024] = {"bugreport", "file.zip"};
364 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
365 ASSERT_THAT(GetCapturedStderr(), HasSubstr("bugreportz? What am I, a zombie?"));
366}
367
Felipe Lemee53b12b2016-07-26 12:23:40 -0700368// Tests 'adb bugreport file.zip' when the bugreportz -v command failed
369TEST_F(BugreportTest, BugreportzVersionFailed) {
370 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -v", false, _))
371 .WillOnce(Return(666));
372
373 const char* args[1024] = {"bugreport", "file.zip"};
374 ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
375}
376
Felipe Lemeab62b552016-07-29 15:47:00 -0700377// Tests 'adb bugreport file.zip' when the bugreportz -v returns status 0 but with no output.
378TEST_F(BugreportTest, BugreportzVersionEmpty) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700379 ExpectBugreportzVersion("");
Felipe Lemeab62b552016-07-29 15:47:00 -0700380
381 const char* args[1024] = {"bugreport", "file.zip"};
382 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
383}
384
Felipe Lemee53b12b2016-07-26 12:23:40 -0700385// Tests 'adb bugreport file.zip' when the main bugreportz command failed
Felipe Leme042c3512016-07-19 17:07:22 -0700386TEST_F(BugreportTest, BugreportzFailed) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700387 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700388 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Leme042c3512016-07-19 17:07:22 -0700389 .WillOnce(Return(666));
390
391 const char* args[1024] = {"bugreport", "file.zip"};
392 ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
393}
394
395// Tests 'adb bugreport file.zip' when the bugreport could not be pulled
396TEST_F(BugreportTest, PullFails) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700397 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700398 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Leme60192aa2016-07-26 12:14:39 -0700399 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")),
400 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700401 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Leme954af842016-07-27 19:23:09 -0700402 true, HasSubstr("file.zip")))
Felipe Leme042c3512016-07-19 17:07:22 -0700403 .WillOnce(Return(false));
404
405 const char* args[1024] = {"bugreport", "file.zip"};
406 ASSERT_EQ(1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
407}