blob: b500c49c1a05ab9fd6b9d2ca7edf08341ec363ff [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;
Felipe Lemefa236de2016-08-04 12:03:06 -070039using ::testing::internal::CaptureStdout;
Felipe Leme042c3512016-07-19 17:07:22 -070040using ::testing::internal::GetCapturedStderr;
Felipe Lemefa236de2016-08-04 12:03:06 -070041using ::testing::internal::GetCapturedStdout;
Felipe Leme042c3512016-07-19 17:07:22 -070042
Felipe Leme60192aa2016-07-26 12:14:39 -070043// Empty function so tests don't need to be linked against file_sync_service.cpp, which requires
Felipe Leme042c3512016-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 Leme60192aa2016-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 Leme5dab2b42017-03-20 11:00:11 -070053
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) {
Felipe Lemefa236de2016-08-04 12:03:06 -0700155 const char* args[] = {"bugreport", "to", "principal"};
Felipe Leme5dab2b42017-03-20 11:00:11 -0700156 ASSERT_EQ(1, br_.DoIt(kTransportLocal, "HannibalLecter", 3, args));
Felipe Leme042c3512016-07-19 17:07:22 -0700157}
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) {
Felipe Lemefa236de2016-08-04 12:03:06 -0700162 // 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 Leme60192aa2016-07-26 12:14:39 -0700171 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreport", false, _))
Felipe Lemefa236de2016-08-04 12:03:06 -0700172 .WillOnce(DoAll(WithArg<4>(WriteOnStdout(bugreport)), Return(0)));
Felipe Leme042c3512016-07-19 17:07:22 -0700173
Felipe Lemefa236de2016-08-04 12:03:06 -0700174 const char* args[] = {"bugreport"};
Felipe Leme042c3512016-07-19 17:07:22 -0700175 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 1, args));
Felipe Lemefa236de2016-08-04 12:03:06 -0700176 ASSERT_THAT(GetCapturedStdout(), StrEq(bugreport));
Felipe Leme042c3512016-07-19 17:07:22 -0700177}
178
Felipe Lemee4893a22016-07-29 17:57:00 -0700179// 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.
181TEST_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 Lemee87fdfa2016-08-15 16:01:58 -0700190 true, StrEq("pulling da_bugreport.zip")))
Felipe Lemee4893a22016-07-29 17:57:00 -0700191 .WillOnce(Return(true));
192
Felipe Lemefa236de2016-08-04 12:03:06 -0700193 const char* args[] = {"bugreport"};
Felipe Lemee4893a22016-07-29 17:57:00 -0700194 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.
199TEST_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 Lemee87fdfa2016-08-15 16:01:58 -0700210 true, StrEq("pulling da_bugreport.zip")))
Felipe Lemee4893a22016-07-29 17:57:00 -0700211 .WillOnce(Return(true));
212
Felipe Lemefa236de2016-08-04 12:03:06 -0700213 const char* args[] = {"bugreport"};
Felipe Lemee4893a22016-07-29 17:57:00 -0700214 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.
218TEST_F(BugreportTest, OkNDevice) {
219 ExpectBugreportzVersion("1.0");
Felipe Leme60192aa2016-07-26 12:14:39 -0700220 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _))
221 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")),
222 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700223 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Lemee87fdfa2016-08-15 16:01:58 -0700224 true, StrEq("pulling file.zip")))
Felipe Leme042c3512016-07-19 17:07:22 -0700225 .WillOnce(Return(true));
226
Felipe Lemefa236de2016-08-04 12:03:06 -0700227 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme042c3512016-07-19 17:07:22 -0700228 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
229}
230
Felipe Leme60192aa2016-07-26 12:14:39 -0700231// Tests 'adb bugreport file.zip' when it succeeds but response was sent in
Felipe Lemee53b12b2016-07-26 12:23:40 -0700232// multiple buffer writers and without progress updates.
Felipe Lemee4893a22016-07-29 17:57:00 -0700233TEST_F(BugreportTest, OkNDeviceSplitBuffer) {
234 ExpectBugreportzVersion("1.0");
Felipe Leme60192aa2016-07-26 12:14:39 -0700235 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 Lemee87fdfa2016-08-15 16:01:58 -0700240 true, StrEq("pulling file.zip")))
Felipe Leme60192aa2016-07-26 12:14:39 -0700241 .WillOnce(Return(true));
242
Felipe Lemefa236de2016-08-04 12:03:06 -0700243 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme60192aa2016-07-26 12:14:39 -0700244 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
245}
246
Felipe Lemee53b12b2016-07-26 12:23:40 -0700247// Tests 'adb bugreport file.zip' when it succeeds and displays progress.
Felipe Lemee4893a22016-07-29 17:57:00 -0700248TEST_F(BugreportTest, OkProgress) {
249 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700250 ExpectProgress(1, 100);
251 ExpectProgress(10, 100);
252 ExpectProgress(50, 100);
253 ExpectProgress(99, 100);
Felipe Lemee53b12b2016-07-26 12:23:40 -0700254 // clang-format off
255 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Lemee4893a22016-07-29 17:57:00 -0700256 // NOTE: DoAll accepts at most 10 arguments, and we're almost reached that limit...
Felipe Lemee53b12b2016-07-26 12:23:40 -0700257 .WillOnce(DoAll(
Felipe Lemee4893a22016-07-29 17:57:00 -0700258 // Name might change on OK, so make sure the right one is picked.
259 WithArg<4>(WriteOnStdout("BEGIN:/device/bugreport___NOT.zip\n")),
Felipe Lemee53b12b2016-07-26 12:23:40 -0700260 // Progress line in one write
261 WithArg<4>(WriteOnStdout("PROGRESS:1/100\n")),
262 // Add some bogus lines
Felipe Lemeab62b552016-07-29 15:47:00 -0700263 WithArg<4>(WriteOnStdout("\nDUDE:SWEET\n\nBLA\n\nBLA\nBLA\n\n")),
Felipe Lemee53b12b2016-07-26 12:23:40 -0700264 // 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 Lemeab62b552016-07-29 15:47:00 -0700274 // clang-format on
Felipe Lemee53b12b2016-07-26 12:23:40 -0700275 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Lemee87fdfa2016-08-15 16:01:58 -0700276 true, StrEq("pulling file.zip")))
Felipe Lemee53b12b2016-07-26 12:23:40 -0700277 .WillOnce(Return(true));
278
Felipe Lemefa236de2016-08-04 12:03:06 -0700279 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemee53b12b2016-07-26 12:23:40 -0700280 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
281}
282
Felipe Leme5dab2b42017-03-20 11:00:11 -0700283// Tests 'adb bugreport file.zip' when it succeeds and displays progress, even if progress recedes.
284TEST_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 Lemee4893a22016-07-29 17:57:00 -0700312// Tests 'adb bugreport dir' when it succeeds and destination is a directory.
313TEST_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 Lemee87fdfa2016-08-15 16:01:58 -0700324 true, StrEq("pulling da_bugreport.zip")))
Felipe Lemee4893a22016-07-29 17:57:00 -0700325 .WillOnce(Return(true));
326
Felipe Lemefa236de2016-08-04 12:03:06 -0700327 const char* args[] = {"bugreport", td.path};
Felipe Lemee4893a22016-07-29 17:57:00 -0700328 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
329}
330
Felipe Lemee53b12b2016-07-26 12:23:40 -0700331// Tests 'adb bugreport file' when it succeeds
332TEST_F(BugreportTest, OkNoExtension) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700333 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700334 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 Lemee87fdfa2016-08-15 16:01:58 -0700338 true, StrEq("pulling file.zip")))
Felipe Lemee53b12b2016-07-26 12:23:40 -0700339 .WillOnce(Return(true));
340
Felipe Lemefa236de2016-08-04 12:03:06 -0700341 const char* args[] = {"bugreport", "file"};
Felipe Lemee53b12b2016-07-26 12:23:40 -0700342 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
343}
344
Felipe Lemee4893a22016-07-29 17:57:00 -0700345// Tests 'adb bugreport dir' when it succeeds and destination is a directory and device runs N.
346TEST_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 Lemee87fdfa2016-08-15 16:01:58 -0700357 true, StrEq("pulling da_bugreport.zip")))
Felipe Lemee4893a22016-07-29 17:57:00 -0700358 .WillOnce(Return(true));
359
Felipe Lemefa236de2016-08-04 12:03:06 -0700360 const char* args[] = {"bugreport", td.path};
Felipe Lemee4893a22016-07-29 17:57:00 -0700361 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
362}
363
Felipe Leme042c3512016-07-19 17:07:22 -0700364// Tests 'adb bugreport file.zip' when the bugreport itself failed
365TEST_F(BugreportTest, BugreportzReturnedFail) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700366 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700367 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
368 .WillOnce(
369 DoAll(WithArg<4>(WriteOnStdout("FAIL:D'OH!\n")), WithArg<4>(ReturnCallbackDone())));
Felipe Leme60192aa2016-07-26 12:14:39 -0700370
371 CaptureStderr();
Felipe Lemefa236de2016-08-04 12:03:06 -0700372 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme60192aa2016-07-26 12:14:39 -0700373 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
Felipe Lemee53b12b2016-07-26 12:23:40 -0700374 ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!"));
Felipe Leme60192aa2016-07-26 12:14:39 -0700375}
376
377// Tests 'adb bugreport file.zip' when the bugreport itself failed but response
378// was sent in
379// multiple buffer writes
380TEST_F(BugreportTest, BugreportzReturnedFailSplitBuffer) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700381 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700382 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
383 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("FAIL")), WithArg<4>(WriteOnStdout(":D'OH!\n")),
Felipe Leme60192aa2016-07-26 12:14:39 -0700384 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700385
386 CaptureStderr();
Felipe Lemefa236de2016-08-04 12:03:06 -0700387 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme042c3512016-07-19 17:07:22 -0700388 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
Felipe Lemee53b12b2016-07-26 12:23:40 -0700389 ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!"));
Felipe Leme042c3512016-07-19 17:07:22 -0700390}
391
392// Tests 'adb bugreport file.zip' when the bugreportz returned an unsupported
393// response.
394TEST_F(BugreportTest, BugreportzReturnedUnsupported) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700395 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700396 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Leme60192aa2016-07-26 12:14:39 -0700397 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("bugreportz? What am I, a zombie?")),
398 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700399
400 CaptureStderr();
Felipe Lemefa236de2016-08-04 12:03:06 -0700401 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme042c3512016-07-19 17:07:22 -0700402 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
403 ASSERT_THAT(GetCapturedStderr(), HasSubstr("bugreportz? What am I, a zombie?"));
404}
405
Felipe Lemee53b12b2016-07-26 12:23:40 -0700406// Tests 'adb bugreport file.zip' when the bugreportz -v command failed
407TEST_F(BugreportTest, BugreportzVersionFailed) {
408 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -v", false, _))
409 .WillOnce(Return(666));
410
Felipe Lemefa236de2016-08-04 12:03:06 -0700411 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemee53b12b2016-07-26 12:23:40 -0700412 ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
413}
414
Felipe Lemeab62b552016-07-29 15:47:00 -0700415// Tests 'adb bugreport file.zip' when the bugreportz -v returns status 0 but with no output.
416TEST_F(BugreportTest, BugreportzVersionEmpty) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700417 ExpectBugreportzVersion("");
Felipe Lemeab62b552016-07-29 15:47:00 -0700418
Felipe Lemefa236de2016-08-04 12:03:06 -0700419 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemeab62b552016-07-29 15:47:00 -0700420 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
421}
422
Felipe Lemee53b12b2016-07-26 12:23:40 -0700423// Tests 'adb bugreport file.zip' when the main bugreportz command failed
Felipe Leme042c3512016-07-19 17:07:22 -0700424TEST_F(BugreportTest, BugreportzFailed) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700425 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700426 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Leme042c3512016-07-19 17:07:22 -0700427 .WillOnce(Return(666));
428
Felipe Lemefa236de2016-08-04 12:03:06 -0700429 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme042c3512016-07-19 17:07:22 -0700430 ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
431}
432
433// Tests 'adb bugreport file.zip' when the bugreport could not be pulled
434TEST_F(BugreportTest, PullFails) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700435 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700436 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Leme60192aa2016-07-26 12:14:39 -0700437 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")),
438 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700439 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Leme954af842016-07-27 19:23:09 -0700440 true, HasSubstr("file.zip")))
Felipe Leme042c3512016-07-19 17:07:22 -0700441 .WillOnce(Return(false));
442
Felipe Lemefa236de2016-08-04 12:03:06 -0700443 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme042c3512016-07-19 17:07:22 -0700444 ASSERT_EQ(1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
445}