blob: a826035abbca038a1d2e2612f43d861195ed9d0e [file] [log] [blame]
David Pursellb9e2e842015-08-31 15:36:18 -07001/*
2 * Copyright (C) 2015 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 "shell_service.h"
18
19#include <gtest/gtest.h>
20
21#include <signal.h>
22#include <string.h>
23
24#include "sysdeps.h"
25
26class ShellProtocolTest : public ::testing::Test {
27 public:
28 static void SetUpTestCase() {
29#if !defined(_WIN32)
30 // This is normally done in main.cpp.
31 saved_sigpipe_handler_ = signal(SIGPIPE, SIG_IGN);
32#endif
33 }
34
35 static void TearDownTestCase() {
36#if !defined(_WIN32)
37 signal(SIGPIPE, saved_sigpipe_handler_);
38#endif
39 }
40
41 // Initializes the socketpair and ShellProtocols needed for testing.
42 void SetUp() {
43 int fds[2];
44 ASSERT_EQ(0, adb_socketpair(fds));
45 read_fd_ = fds[0];
46 write_fd_ = fds[1];
47
48 write_protocol_ = new ShellProtocol(write_fd_);
49 ASSERT_TRUE(write_protocol_ != nullptr);
50
51 read_protocol_ = new ShellProtocol(read_fd_);
52 ASSERT_TRUE(read_protocol_ != nullptr);
53 }
54
55 // Cleans up FDs and ShellProtocols. If an FD is closed manually during a
56 // test, set it to -1 to prevent TearDown() trying to close it again.
57 void TearDown() {
58 for (int fd : {read_fd_, write_fd_}) {
59 if (fd >= 0) {
60 adb_close(fd);
61 }
62 }
63 for (ShellProtocol* protocol : {read_protocol_, write_protocol_}) {
64 if (protocol) {
65 delete protocol;
66 }
67 }
68 }
69
70 // Fakes the buffer size so we can test filling buffers.
71 void SetReadDataCapacity(size_t size) {
72 read_protocol_->buffer_end_ = read_protocol_->data() + size;
73 }
74
David Pursellb404dec2015-09-11 16:06:59 -070075#if !defined(_WIN32)
David Pursellb9e2e842015-08-31 15:36:18 -070076 static sig_t saved_sigpipe_handler_;
David Pursellb404dec2015-09-11 16:06:59 -070077#endif
David Pursellb9e2e842015-08-31 15:36:18 -070078
79 int read_fd_ = -1, write_fd_ = -1;
80 ShellProtocol *read_protocol_ = nullptr, *write_protocol_ = nullptr;
81};
82
David Pursellb404dec2015-09-11 16:06:59 -070083#if !defined(_WIN32)
David Pursellb9e2e842015-08-31 15:36:18 -070084sig_t ShellProtocolTest::saved_sigpipe_handler_ = nullptr;
David Pursellb404dec2015-09-11 16:06:59 -070085#endif
David Pursellb9e2e842015-08-31 15:36:18 -070086
87namespace {
88
89// Returns true if the packet contains the given values.
90bool PacketEquals(const ShellProtocol* protocol, ShellProtocol::Id id,
91 const void* data, size_t data_length) {
92 return (protocol->id() == id &&
93 protocol->data_length() == data_length &&
94 !memcmp(data, protocol->data(), data_length));
95}
96
97} // namespace
98
99// Tests data that can fit in a single packet.
100TEST_F(ShellProtocolTest, FullPacket) {
101 ShellProtocol::Id id = ShellProtocol::kIdStdout;
102 char data[] = "abc 123 \0\r\n";
103
104 memcpy(write_protocol_->data(), data, sizeof(data));
105 ASSERT_TRUE(write_protocol_->Write(id, sizeof(data)));
106
107 ASSERT_TRUE(read_protocol_->Read());
108 ASSERT_TRUE(PacketEquals(read_protocol_, id, data, sizeof(data)));
109}
110
111// Tests data that has to be read multiple times due to smaller read buffer.
112TEST_F(ShellProtocolTest, ReadBufferOverflow) {
113 ShellProtocol::Id id = ShellProtocol::kIdStdin;
114
115 memcpy(write_protocol_->data(), "1234567890", 10);
116 ASSERT_TRUE(write_protocol_->Write(id, 10));
117
118 SetReadDataCapacity(4);
119 ASSERT_TRUE(read_protocol_->Read());
120 ASSERT_TRUE(PacketEquals(read_protocol_, id, "1234", 4));
121 ASSERT_TRUE(read_protocol_->Read());
122 ASSERT_TRUE(PacketEquals(read_protocol_, id, "5678", 4));
123 ASSERT_TRUE(read_protocol_->Read());
124 ASSERT_TRUE(PacketEquals(read_protocol_, id, "90", 2));
125}
126
127// Tests a zero length packet.
128TEST_F(ShellProtocolTest, ZeroLengthPacket) {
129 ShellProtocol::Id id = ShellProtocol::kIdStderr;
130
131 ASSERT_TRUE(write_protocol_->Write(id, 0));
132 ASSERT_TRUE(read_protocol_->Read());
133 ASSERT_TRUE(PacketEquals(read_protocol_, id, nullptr, 0));
134}
135
136// Tests exit code packets.
137TEST_F(ShellProtocolTest, ExitCodePacket) {
138 write_protocol_->data()[0] = 20;
139 ASSERT_TRUE(write_protocol_->Write(ShellProtocol::kIdExit, 1));
140
141 ASSERT_TRUE(read_protocol_->Read());
142 ASSERT_EQ(ShellProtocol::kIdExit, read_protocol_->id());
143 ASSERT_EQ(20, read_protocol_->data()[0]);
144}
145
146// Tests writing to a closed pipe.
147TEST_F(ShellProtocolTest, WriteToClosedPipeFail) {
148 adb_close(read_fd_);
149 read_fd_ = -1;
150
151 ASSERT_FALSE(write_protocol_->Write(ShellProtocol::kIdStdout, 0));
152}
153
154// Tests writing to a closed FD.
155TEST_F(ShellProtocolTest, WriteToClosedFdFail) {
156 adb_close(write_fd_);
157 write_fd_ = -1;
158
159 ASSERT_FALSE(write_protocol_->Write(ShellProtocol::kIdStdout, 0));
160}
161
162// Tests reading from a closed pipe.
163TEST_F(ShellProtocolTest, ReadFromClosedPipeFail) {
164 adb_close(write_fd_);
165 write_fd_ = -1;
166
167 ASSERT_FALSE(read_protocol_->Read());
168}
169
170// Tests reading from a closed FD.
171TEST_F(ShellProtocolTest, ReadFromClosedFdFail) {
172 adb_close(read_fd_);
173 read_fd_ = -1;
174
175 ASSERT_FALSE(read_protocol_->Read());
176}
177
178// Tests reading from a closed pipe that has a packet waiting. This checks that
179// even if the pipe closes before we can fully read its contents we will still
180// be able to access the last packets.
181TEST_F(ShellProtocolTest, ReadPacketFromClosedPipe) {
182 ShellProtocol::Id id = ShellProtocol::kIdStdout;
183 char data[] = "foo bar";
184
185 memcpy(write_protocol_->data(), data, sizeof(data));
186 ASSERT_TRUE(write_protocol_->Write(id, sizeof(data)));
187 adb_close(write_fd_);
188 write_fd_ = -1;
189
190 // First read should grab the packet.
191 ASSERT_TRUE(read_protocol_->Read());
192 ASSERT_TRUE(PacketEquals(read_protocol_, id, data, sizeof(data)));
193
194 // Second read should fail.
195 ASSERT_FALSE(read_protocol_->Read());
196}