1/*
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
75#if !defined(_WIN32)
76    static sig_t saved_sigpipe_handler_;
77#endif
78
79    int read_fd_ = -1, write_fd_ = -1;
80    ShellProtocol *read_protocol_ = nullptr, *write_protocol_ = nullptr;
81};
82
83#if !defined(_WIN32)
84sig_t ShellProtocolTest::saved_sigpipe_handler_ = nullptr;
85#endif
86
87namespace {
88
89// Returns true if the packet contains the given values. `data` can't be null.
90bool PacketEquals(const ShellProtocol* protocol, ShellProtocol::Id id,
91                    const void* data, size_t data_length) {
92    // Note that passing memcmp null is bad, even if data_length is 0.
93    return (protocol->id() == id &&
94            protocol->data_length() == data_length &&
95            !memcmp(data, protocol->data(), data_length));
96}
97
98}  // namespace
99
100// Tests data that can fit in a single packet.
101TEST_F(ShellProtocolTest, FullPacket) {
102    ShellProtocol::Id id = ShellProtocol::kIdStdout;
103    char data[] = "abc 123 \0\r\n";
104
105    memcpy(write_protocol_->data(), data, sizeof(data));
106    ASSERT_TRUE(write_protocol_->Write(id, sizeof(data)));
107
108    ASSERT_TRUE(read_protocol_->Read());
109    ASSERT_TRUE(PacketEquals(read_protocol_, id, data, sizeof(data)));
110}
111
112// Tests data that has to be read multiple times due to smaller read buffer.
113TEST_F(ShellProtocolTest, ReadBufferOverflow) {
114    ShellProtocol::Id id = ShellProtocol::kIdStdin;
115
116    memcpy(write_protocol_->data(), "1234567890", 10);
117    ASSERT_TRUE(write_protocol_->Write(id, 10));
118
119    SetReadDataCapacity(4);
120    ASSERT_TRUE(read_protocol_->Read());
121    ASSERT_TRUE(PacketEquals(read_protocol_, id, "1234", 4));
122    ASSERT_TRUE(read_protocol_->Read());
123    ASSERT_TRUE(PacketEquals(read_protocol_, id, "5678", 4));
124    ASSERT_TRUE(read_protocol_->Read());
125    ASSERT_TRUE(PacketEquals(read_protocol_, id, "90", 2));
126}
127
128// Tests a zero length packet.
129TEST_F(ShellProtocolTest, ZeroLengthPacket) {
130    ShellProtocol::Id id = ShellProtocol::kIdStderr;
131
132    ASSERT_TRUE(write_protocol_->Write(id, 0));
133    ASSERT_TRUE(read_protocol_->Read());
134    char buf[1];
135    ASSERT_TRUE(PacketEquals(read_protocol_, id, buf, 0));
136}
137
138// Tests exit code packets.
139TEST_F(ShellProtocolTest, ExitCodePacket) {
140    write_protocol_->data()[0] = 20;
141    ASSERT_TRUE(write_protocol_->Write(ShellProtocol::kIdExit, 1));
142
143    ASSERT_TRUE(read_protocol_->Read());
144    ASSERT_EQ(ShellProtocol::kIdExit, read_protocol_->id());
145    ASSERT_EQ(20, read_protocol_->data()[0]);
146}
147
148// Tests writing to a closed pipe.
149TEST_F(ShellProtocolTest, WriteToClosedPipeFail) {
150    adb_close(read_fd_);
151    read_fd_ = -1;
152
153    ASSERT_FALSE(write_protocol_->Write(ShellProtocol::kIdStdout, 0));
154}
155
156// Tests writing to a closed FD.
157TEST_F(ShellProtocolTest, WriteToClosedFdFail) {
158    adb_close(write_fd_);
159    write_fd_ = -1;
160
161    ASSERT_FALSE(write_protocol_->Write(ShellProtocol::kIdStdout, 0));
162}
163
164// Tests reading from a closed pipe.
165TEST_F(ShellProtocolTest, ReadFromClosedPipeFail) {
166    adb_close(write_fd_);
167    write_fd_ = -1;
168
169    ASSERT_FALSE(read_protocol_->Read());
170}
171
172// Tests reading from a closed FD.
173TEST_F(ShellProtocolTest, ReadFromClosedFdFail) {
174    adb_close(read_fd_);
175    read_fd_ = -1;
176
177    ASSERT_FALSE(read_protocol_->Read());
178}
179
180// Tests reading from a closed pipe that has a packet waiting. This checks that
181// even if the pipe closes before we can fully read its contents we will still
182// be able to access the last packets.
183TEST_F(ShellProtocolTest, ReadPacketFromClosedPipe) {
184    ShellProtocol::Id id = ShellProtocol::kIdStdout;
185    char data[] = "foo bar";
186
187    memcpy(write_protocol_->data(), data, sizeof(data));
188    ASSERT_TRUE(write_protocol_->Write(id, sizeof(data)));
189    adb_close(write_fd_);
190    write_fd_ = -1;
191
192    // First read should grab the packet.
193    ASSERT_TRUE(read_protocol_->Read());
194    ASSERT_TRUE(PacketEquals(read_protocol_, id, data, sizeof(data)));
195
196    // Second read should fail.
197    ASSERT_FALSE(read_protocol_->Read());
198}
199