-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell_service_protocol_test.cpp
198 lines (159 loc) · 5.92 KB
/
shell_service_protocol_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "shell_protocol.h"
#include <gtest/gtest.h>
#include <signal.h>
#include <string.h>
#include "sysdeps.h"
class ShellProtocolTest : public ::testing::Test {
public:
static void SetUpTestCase() {
#if !defined(_WIN32)
// This is normally done in main.cpp.
saved_sigpipe_handler_ = signal(SIGPIPE, SIG_IGN);
#endif
}
static void TearDownTestCase() {
#if !defined(_WIN32)
signal(SIGPIPE, saved_sigpipe_handler_);
#endif
}
// Initializes the socketpair and ShellProtocols needed for testing.
void SetUp() {
int fds[2];
ASSERT_EQ(0, adb_socketpair(fds));
read_fd_ = fds[0];
write_fd_ = fds[1];
write_protocol_ = new ShellProtocol(write_fd_);
ASSERT_TRUE(write_protocol_ != nullptr);
read_protocol_ = new ShellProtocol(read_fd_);
ASSERT_TRUE(read_protocol_ != nullptr);
}
// Cleans up FDs and ShellProtocols. If an FD is closed manually during a
// test, set it to -1 to prevent TearDown() trying to close it again.
void TearDown() {
for (int fd : {read_fd_, write_fd_}) {
if (fd >= 0) {
adb_close(fd);
}
}
for (ShellProtocol* protocol : {read_protocol_, write_protocol_}) {
if (protocol) {
delete protocol;
}
}
}
// Fakes the buffer size so we can test filling buffers.
void SetReadDataCapacity(size_t size) {
read_protocol_->buffer_end_ = read_protocol_->data() + size;
}
#if !defined(_WIN32)
static sig_t saved_sigpipe_handler_;
#endif
int read_fd_ = -1, write_fd_ = -1;
ShellProtocol *read_protocol_ = nullptr, *write_protocol_ = nullptr;
};
#if !defined(_WIN32)
sig_t ShellProtocolTest::saved_sigpipe_handler_ = nullptr;
#endif
namespace {
// Returns true if the packet contains the given values. `data` can't be null.
bool PacketEquals(const ShellProtocol* protocol, ShellProtocol::Id id,
const void* data, size_t data_length) {
// Note that passing memcmp null is bad, even if data_length is 0.
return (protocol->id() == id &&
protocol->data_length() == data_length &&
!memcmp(data, protocol->data(), data_length));
}
} // namespace
// Tests data that can fit in a single packet.
TEST_F(ShellProtocolTest, FullPacket) {
ShellProtocol::Id id = ShellProtocol::kIdStdout;
char data[] = "abc 123 \0\r\n";
memcpy(write_protocol_->data(), data, sizeof(data));
ASSERT_TRUE(write_protocol_->Write(id, sizeof(data)));
ASSERT_TRUE(read_protocol_->Read());
ASSERT_TRUE(PacketEquals(read_protocol_, id, data, sizeof(data)));
}
// Tests data that has to be read multiple times due to smaller read buffer.
TEST_F(ShellProtocolTest, ReadBufferOverflow) {
ShellProtocol::Id id = ShellProtocol::kIdStdin;
memcpy(write_protocol_->data(), "1234567890", 10);
ASSERT_TRUE(write_protocol_->Write(id, 10));
SetReadDataCapacity(4);
ASSERT_TRUE(read_protocol_->Read());
ASSERT_TRUE(PacketEquals(read_protocol_, id, "1234", 4));
ASSERT_TRUE(read_protocol_->Read());
ASSERT_TRUE(PacketEquals(read_protocol_, id, "5678", 4));
ASSERT_TRUE(read_protocol_->Read());
ASSERT_TRUE(PacketEquals(read_protocol_, id, "90", 2));
}
// Tests a zero length packet.
TEST_F(ShellProtocolTest, ZeroLengthPacket) {
ShellProtocol::Id id = ShellProtocol::kIdStderr;
ASSERT_TRUE(write_protocol_->Write(id, 0));
ASSERT_TRUE(read_protocol_->Read());
char buf[1];
ASSERT_TRUE(PacketEquals(read_protocol_, id, buf, 0));
}
// Tests exit code packets.
TEST_F(ShellProtocolTest, ExitCodePacket) {
write_protocol_->data()[0] = 20;
ASSERT_TRUE(write_protocol_->Write(ShellProtocol::kIdExit, 1));
ASSERT_TRUE(read_protocol_->Read());
ASSERT_EQ(ShellProtocol::kIdExit, read_protocol_->id());
ASSERT_EQ(20, read_protocol_->data()[0]);
}
// Tests writing to a closed pipe.
TEST_F(ShellProtocolTest, WriteToClosedPipeFail) {
adb_close(read_fd_);
read_fd_ = -1;
ASSERT_FALSE(write_protocol_->Write(ShellProtocol::kIdStdout, 0));
}
// Tests writing to a closed FD.
TEST_F(ShellProtocolTest, WriteToClosedFdFail) {
adb_close(write_fd_);
write_fd_ = -1;
ASSERT_FALSE(write_protocol_->Write(ShellProtocol::kIdStdout, 0));
}
// Tests reading from a closed pipe.
TEST_F(ShellProtocolTest, ReadFromClosedPipeFail) {
adb_close(write_fd_);
write_fd_ = -1;
ASSERT_FALSE(read_protocol_->Read());
}
// Tests reading from a closed FD.
TEST_F(ShellProtocolTest, ReadFromClosedFdFail) {
adb_close(read_fd_);
read_fd_ = -1;
ASSERT_FALSE(read_protocol_->Read());
}
// Tests reading from a closed pipe that has a packet waiting. This checks that
// even if the pipe closes before we can fully read its contents we will still
// be able to access the last packets.
TEST_F(ShellProtocolTest, ReadPacketFromClosedPipe) {
ShellProtocol::Id id = ShellProtocol::kIdStdout;
char data[] = "foo bar";
memcpy(write_protocol_->data(), data, sizeof(data));
ASSERT_TRUE(write_protocol_->Write(id, sizeof(data)));
adb_close(write_fd_);
write_fd_ = -1;
// First read should grab the packet.
ASSERT_TRUE(read_protocol_->Read());
ASSERT_TRUE(PacketEquals(read_protocol_, id, data, sizeof(data)));
// Second read should fail.
ASSERT_FALSE(read_protocol_->Read());
}