blob: dd2ff37fcf829e09fc4ba61fe444946e15673996 [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
22using ::testing::_;
23using ::testing::DoAll;
24using ::testing::ElementsAre;
25using ::testing::HasSubstr;
26using ::testing::Return;
27using ::testing::SetArgPointee;
28using ::testing::StrEq;
29using ::testing::internal::CaptureStderr;
30using ::testing::internal::GetCapturedStderr;
31
32// Empty function so tests don't need to be linked against
33// file_sync_service.cpp, which requires
34// SELinux and its transitive dependencies...
35bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
36 const char* name) {
37 ADD_FAILURE() << "do_sync_pull() should have been mocked";
38 return false;
39}
40
41// Implemented in commandline.cpp
42int usage() {
43 return -42;
44}
45
46// Implemented in commandline.cpp
47int send_shell_command(TransportType transport_type, const char* serial, const std::string& command,
48 bool disable_shell_protocol, std::string* output, std::string* err) {
49 ADD_FAILURE() << "send_shell_command() should have been mocked";
50 return -42;
51}
52
53class BugreportMock : public Bugreport {
54 public:
55 MOCK_METHOD6(SendShellCommand,
56 int(TransportType transport_type, const char* serial, const std::string& command,
57 bool disable_shell_protocol, std::string* output, std::string* err));
58 MOCK_METHOD4(DoSyncPull, bool(const std::vector<const char*>& srcs, const char* dst,
59 bool copy_attrs, const char* name));
60};
61
62class BugreportTest : public ::testing::Test {
63 public:
64 BugreportMock br_;
65};
66
67// Tests when called with invalid number of argumnts
68TEST_F(BugreportTest, InvalidNumberArgs) {
69 const char* args[1024] = {"bugreport", "to", "principal"};
70 ASSERT_EQ(-42, br_.DoIt(kTransportLocal, "HannibalLecter", 3, args));
71}
72
73// Tests the legacy 'adb bugreport' option
74TEST_F(BugreportTest, FlatFileFormat) {
75 EXPECT_CALL(br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreport", false,
76 nullptr, nullptr))
77 .WillOnce(Return(0));
78
79 const char* args[1024] = {"bugreport"};
80 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 1, args));
81}
82
83// Tests 'adb bugreport file.zip' when it succeeds
84TEST_F(BugreportTest, Ok) {
85 EXPECT_CALL(
86 br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _, nullptr))
87 .WillOnce(DoAll(SetArgPointee<4>("OK:/device/bugreport.zip"), Return(0)));
88 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
89 true, StrEq("file.zip")))
90 .WillOnce(Return(true));
91
92 const char* args[1024] = {"bugreport", "file.zip"};
93 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
94}
95
96// Tests 'adb bugreport file' when it succeeds
97TEST_F(BugreportTest, OkNoExtension) {
98 EXPECT_CALL(
99 br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _, nullptr))
100 .WillOnce(DoAll(SetArgPointee<4>("OK:/device/bugreport.zip"), Return(0)));
101 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
102 true, StrEq("file.zip")))
103 .WillOnce(Return(true));
104
105 const char* args[1024] = {"bugreport", "file"};
106 ASSERT_EQ(0, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
107}
108
109// Tests 'adb bugreport file.zip' when the bugreport itself failed
110TEST_F(BugreportTest, BugreportzReturnedFail) {
111 EXPECT_CALL(
112 br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _, nullptr))
113 .WillOnce(DoAll(SetArgPointee<4>("FAIL:D'OH!"), Return(0)));
114
115 CaptureStderr();
116 const char* args[1024] = {"bugreport", "file.zip"};
117 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
118 ASSERT_THAT(GetCapturedStderr(), HasSubstr("D'OH"));
119}
120
121// Tests 'adb bugreport file.zip' when the bugreportz returned an unsupported
122// response.
123TEST_F(BugreportTest, BugreportzReturnedUnsupported) {
124 EXPECT_CALL(
125 br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _, nullptr))
126 .WillOnce(DoAll(SetArgPointee<4>("bugreportz? What am I, a zombie?"), Return(0)));
127
128 CaptureStderr();
129 const char* args[1024] = {"bugreport", "file.zip"};
130 ASSERT_EQ(-1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
131 ASSERT_THAT(GetCapturedStderr(), HasSubstr("bugreportz? What am I, a zombie?"));
132}
133
134// Tests 'adb bugreport file.zip' when the bugreportz command fails
135TEST_F(BugreportTest, BugreportzFailed) {
136 EXPECT_CALL(
137 br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _, nullptr))
138 .WillOnce(Return(666));
139
140 const char* args[1024] = {"bugreport", "file.zip"};
141 ASSERT_EQ(666, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
142}
143
144// Tests 'adb bugreport file.zip' when the bugreport could not be pulled
145TEST_F(BugreportTest, PullFails) {
146 EXPECT_CALL(
147 br_, SendShellCommand(kTransportLocal, "HannibalLecter", "bugreportz", false, _, nullptr))
148 .WillOnce(DoAll(SetArgPointee<4>("OK:/device/bugreport.zip"), Return(0)));
149 EXPECT_CALL(br_, DoSyncPull(ElementsAre(StrEq("/device/bugreport.zip")), StrEq("file.zip"),
150 true, StrEq("file.zip")))
151 .WillOnce(Return(false));
152
153 const char* args[1024] = {"bugreport", "file.zip"};
154 ASSERT_EQ(1, br_.DoIt(kTransportLocal, "HannibalLecter", 2, args));
155}