blob: 2b368d7be5f1fff9364212f557cd2cbfd49c28ca [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 Leme0aa2ce32017-05-02 10:06:33 -0700126 MOCK_METHOD2(UpdateProgress, void(const std::string&, 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 Leme0aa2ce32017-05-02 10:06:33 -0700145 void ExpectProgress(int progress_percentage, const std::string& file = "file.zip") {
146 EXPECT_CALL(br_, UpdateProgress(StrEq("generating " + file), progress_percentage));
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);
Felipe Leme0aa2ce32017-05-02 10:06:33 -0700203 ExpectProgress(50, "da_bugreport.zip");
Felipe Lemee4893a22016-07-29 17:57:00 -0700204 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 Leme0aa2ce32017-05-02 10:06:33 -0700250 ExpectProgress(1);
251 ExpectProgress(10);
252 ExpectProgress(50);
253 ExpectProgress(99);
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");
Felipe Leme0aa2ce32017-05-02 10:06:33 -0700286 ExpectProgress(1);
287 ExpectProgress(50);
288 ExpectProgress(75);
Felipe Leme5dab2b42017-03-20 11:00:11 -0700289 // 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")),
Felipe Leme0aa2ce32017-05-02 10:06:33 -0700294 WithArg<4>(WriteOnStdout("PROGRESS:1/100\n")), // 1%
295 WithArg<4>(WriteOnStdout("PROGRESS:50/100\n")), // 50%
296 // 25% should be ignored becaused it receded.
297 WithArg<4>(WriteOnStdout("PROGRESS:25/100\n")), // 25%
298 WithArg<4>(WriteOnStdout("PROGRESS:75/100\n")), // 75%
299 // 75% should be ignored becaused it didn't change.
300 WithArg<4>(WriteOnStdout("PROGRESS:75/100\n")), // 75%
301 // Try a receeding percentage with a different max progress
302 WithArg<4>(WriteOnStdout("PROGRESS:700/1000\n")), // 70%
Felipe Leme5dab2b42017-03-20 11:00:11 -0700303 WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")),
304 WithArg<4>(ReturnCallbackDone())));
305 // clang-format on
306 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
307 true, StrEq("pulling file.zip")))
308 .WillOnce(Return(true));
309
310 const char* args[] = {"bugreport", "file.zip"};
311 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
312}
313
Felipe Lemee4893a22016-07-29 17:57:00 -0700314// Tests 'adb bugreport dir' when it succeeds and destination is a directory.
315TEST_F(BugreportTest, OkDirectory) {
316 ExpectBugreportzVersion("1.1");
317 TemporaryDir td;
318 std::string dest_file =
319 android::base::StringPrintf("%s%cda_bugreport.zip", td.path, OS_PATH_SEPARATOR);
320
321 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
322 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("BEGIN:/device/da_bugreport.zip\n")),
323 WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip")),
324 WithArg<4>(ReturnCallbackDone())));
325 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file),
Felipe Lemee87fdfa2016-08-15 16:01:58 -0700326 true, StrEq("pulling da_bugreport.zip")))
Felipe Lemee4893a22016-07-29 17:57:00 -0700327 .WillOnce(Return(true));
328
Felipe Lemefa236de2016-08-04 12:03:06 -0700329 const char* args[] = {"bugreport", td.path};
Felipe Lemee4893a22016-07-29 17:57:00 -0700330 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
331}
332
Felipe Lemee53b12b2016-07-26 12:23:40 -0700333// Tests 'adb bugreport file' when it succeeds
334TEST_F(BugreportTest, OkNoExtension) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700335 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700336 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
337 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip\n")),
338 WithArg<4>(ReturnCallbackDone())));
339 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Lemee87fdfa2016-08-15 16:01:58 -0700340 true, StrEq("pulling file.zip")))
Felipe Lemee53b12b2016-07-26 12:23:40 -0700341 .WillOnce(Return(true));
342
Felipe Lemefa236de2016-08-04 12:03:06 -0700343 const char* args[] = {"bugreport", "file"};
Felipe Lemee53b12b2016-07-26 12:23:40 -0700344 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
345}
346
Felipe Lemee4893a22016-07-29 17:57:00 -0700347// Tests 'adb bugreport dir' when it succeeds and destination is a directory and device runs N.
348TEST_F(BugreportTest, OkNDeviceDirectory) {
349 ExpectBugreportzVersion("1.0");
350 TemporaryDir td;
351 std::string dest_file =
352 android::base::StringPrintf("%s%cda_bugreport.zip", td.path, OS_PATH_SEPARATOR);
353
354 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _))
355 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("BEGIN:/device/da_bugreport.zip\n")),
356 WithArg<4>(WriteOnStdout("OK:/device/da_bugreport.zip")),
357 WithArg<4>(ReturnCallbackDone())));
358 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/da_bugreport.zip")), StrEq(dest_file),
Felipe Lemee87fdfa2016-08-15 16:01:58 -0700359 true, StrEq("pulling da_bugreport.zip")))
Felipe Lemee4893a22016-07-29 17:57:00 -0700360 .WillOnce(Return(true));
361
Felipe Lemefa236de2016-08-04 12:03:06 -0700362 const char* args[] = {"bugreport", td.path};
Felipe Lemee4893a22016-07-29 17:57:00 -0700363 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
364}
365
Felipe Leme042c3512016-07-19 17:07:22 -0700366// Tests 'adb bugreport file.zip' when the bugreport itself failed
367TEST_F(BugreportTest, BugreportzReturnedFail) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700368 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700369 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
370 .WillOnce(
371 DoAll(WithArg<4>(WriteOnStdout("FAIL:D'OH!\n")), WithArg<4>(ReturnCallbackDone())));
Felipe Leme60192aa2016-07-26 12:14:39 -0700372
373 CaptureStderr();
Felipe Lemefa236de2016-08-04 12:03:06 -0700374 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme60192aa2016-07-26 12:14:39 -0700375 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
Felipe Lemee53b12b2016-07-26 12:23:40 -0700376 ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!"));
Felipe Leme60192aa2016-07-26 12:14:39 -0700377}
378
379// Tests 'adb bugreport file.zip' when the bugreport itself failed but response
380// was sent in
381// multiple buffer writes
382TEST_F(BugreportTest, BugreportzReturnedFailSplitBuffer) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700383 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700384 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
385 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("FAIL")), WithArg<4>(WriteOnStdout(":D'OH!\n")),
Felipe Leme60192aa2016-07-26 12:14:39 -0700386 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700387
388 CaptureStderr();
Felipe Lemefa236de2016-08-04 12:03:06 -0700389 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme042c3512016-07-19 17:07:22 -0700390 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
Felipe Lemee53b12b2016-07-26 12:23:40 -0700391 ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH!"));
Felipe Leme042c3512016-07-19 17:07:22 -0700392}
393
394// Tests 'adb bugreport file.zip' when the bugreportz returned an unsupported
395// response.
396TEST_F(BugreportTest, BugreportzReturnedUnsupported) {
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("bugreportz? What am I, a zombie?")),
400 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700401
402 CaptureStderr();
Felipe Lemefa236de2016-08-04 12:03:06 -0700403 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme042c3512016-07-19 17:07:22 -0700404 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
405 ASSERT_THAT(GetCapturedStderr(), HasSubstr("bugreportz? What am I, a zombie?"));
406}
407
Felipe Lemee53b12b2016-07-26 12:23:40 -0700408// Tests 'adb bugreport file.zip' when the bugreportz -v command failed
409TEST_F(BugreportTest, BugreportzVersionFailed) {
410 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -v", false, _))
411 .WillOnce(Return(666));
412
Felipe Lemefa236de2016-08-04 12:03:06 -0700413 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemee53b12b2016-07-26 12:23:40 -0700414 ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
415}
416
Felipe Lemeab62b552016-07-29 15:47:00 -0700417// Tests 'adb bugreport file.zip' when the bugreportz -v returns status 0 but with no output.
418TEST_F(BugreportTest, BugreportzVersionEmpty) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700419 ExpectBugreportzVersion("");
Felipe Lemeab62b552016-07-29 15:47:00 -0700420
Felipe Lemefa236de2016-08-04 12:03:06 -0700421 const char* args[] = {"bugreport", "file.zip"};
Felipe Lemeab62b552016-07-29 15:47:00 -0700422 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
423}
424
Felipe Lemee53b12b2016-07-26 12:23:40 -0700425// Tests 'adb bugreport file.zip' when the main bugreportz command failed
Felipe Leme042c3512016-07-19 17:07:22 -0700426TEST_F(BugreportTest, BugreportzFailed) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700427 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700428 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Leme042c3512016-07-19 17:07:22 -0700429 .WillOnce(Return(666));
430
Felipe Lemefa236de2016-08-04 12:03:06 -0700431 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme042c3512016-07-19 17:07:22 -0700432 ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
433}
434
435// Tests 'adb bugreport file.zip' when the bugreport could not be pulled
436TEST_F(BugreportTest, PullFails) {
Felipe Lemee4893a22016-07-29 17:57:00 -0700437 ExpectBugreportzVersion("1.1");
Felipe Lemee53b12b2016-07-26 12:23:40 -0700438 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz -p", false, _))
Felipe Leme60192aa2016-07-26 12:14:39 -0700439 .WillOnce(DoAll(WithArg<4>(WriteOnStdout("OK:/device/bugreport.zip")),
440 WithArg<4>(ReturnCallbackDone())));
Felipe Leme042c3512016-07-19 17:07:22 -0700441 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
Felipe Leme954af842016-07-27 19:23:09 -0700442 true, HasSubstr("file.zip")))
Felipe Leme042c3512016-07-19 17:07:22 -0700443 .WillOnce(Return(false));
444
Felipe Lemefa236de2016-08-04 12:03:06 -0700445 const char* args[] = {"bugreport", "file.zip"};
Felipe Leme042c3512016-07-19 17:07:22 -0700446 ASSERT_EQ(1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
447}