blob: 7d80d76f562e31d61d1fed7e657d4bd539d6b176 [file] [log] [blame]
David Pursell2ec418a2016-01-20 08:32:08 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "tcp.h"
30
31#include <gtest/gtest.h>
32
33#include "socket_mock.h"
34
35TEST(TcpConnectTest, TestSuccess) {
36 std::unique_ptr<SocketMock> mock(new SocketMock);
37 mock->ExpectSend("FB01");
38 mock->AddReceive("FB01");
39
40 std::string error;
41 EXPECT_NE(nullptr, tcp::internal::Connect(std::move(mock), &error));
42 EXPECT_EQ("", error);
43}
44
45TEST(TcpConnectTest, TestSendFailure) {
46 std::unique_ptr<SocketMock> mock(new SocketMock);
47 mock->ExpectSendFailure("FB01");
48
49 std::string error;
50 EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error));
51 EXPECT_NE(std::string::npos, error.find("Failed to send initialization message"));
52}
53
54TEST(TcpConnectTest, TestNoResponseFailure) {
55 std::unique_ptr<SocketMock> mock(new SocketMock);
56 mock->ExpectSend("FB01");
57 mock->AddReceiveFailure();
58
59 std::string error;
60 EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error));
61 EXPECT_NE(std::string::npos, error.find("No initialization message received"));
62}
63
64TEST(TcpConnectTest, TestBadResponseFailure) {
65 std::unique_ptr<SocketMock> mock(new SocketMock);
66 mock->ExpectSend("FB01");
67 mock->AddReceive("XX01");
68
69 std::string error;
70 EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error));
71 EXPECT_NE(std::string::npos, error.find("Unrecognized initialization message"));
72}
73
74TEST(TcpConnectTest, TestUnknownVersionFailure) {
75 std::unique_ptr<SocketMock> mock(new SocketMock);
76 mock->ExpectSend("FB01");
77 mock->AddReceive("FB02");
78
79 std::string error;
80 EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error));
81 EXPECT_EQ("Unknown TCP protocol version 02 (host version 01)", error);
82}
83
84// Fixture to configure a SocketMock for a successful TCP connection.
85class TcpTest : public ::testing::Test {
86 protected:
87 void SetUp() override {
88 mock_ = new SocketMock;
89 mock_->ExpectSend("FB01");
90 mock_->AddReceive("FB01");
91
92 std::string error;
93 transport_ = tcp::internal::Connect(std::unique_ptr<Socket>(mock_), &error);
94 ASSERT_NE(nullptr, transport_);
95 ASSERT_EQ("", error);
96 };
97
98 // Writes |message| to |transport_|, returns true on success.
99 bool Write(const std::string& message) {
100 return transport_->Write(message.data(), message.length()) ==
101 static_cast<ssize_t>(message.length());
102 }
103
104 // Reads from |transport_|, returns true if it matches |message|.
105 bool Read(const std::string& message) {
106 std::string buffer(message.length(), '\0');
107 return transport_->Read(&buffer[0], buffer.length()) ==
108 static_cast<ssize_t>(message.length()) &&
109 buffer == message;
110 }
111
112 // Use a raw SocketMock* here because we pass ownership to the Transport object, but we still
113 // need access to configure mock expectations.
114 SocketMock* mock_ = nullptr;
115 std::unique_ptr<Transport> transport_;
116};
117
118TEST_F(TcpTest, TestWriteSuccess) {
119 mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0, 0, 3} + "foo");
120
121 EXPECT_TRUE(Write("foo"));
122}
123
124TEST_F(TcpTest, TestReadSuccess) {
125 mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 3});
126 mock_->AddReceive("foo");
127
128 EXPECT_TRUE(Read("foo"));
129}
130
131// Tests that fragmented TCP reads are handled properly.
132TEST_F(TcpTest, TestReadFragmentSuccess) {
133 mock_->AddReceive(std::string{0, 0, 0, 0});
134 mock_->AddReceive(std::string{0, 0, 0, 3});
135 mock_->AddReceive("f");
136 mock_->AddReceive("o");
137 mock_->AddReceive("o");
138
139 EXPECT_TRUE(Read("foo"));
140}
141
142TEST_F(TcpTest, TestLargeWriteSuccess) {
143 // 0x100000 = 1MiB.
144 std::string data(0x100000, '\0');
145 for (size_t i = 0; i < data.length(); ++i) {
146 data[i] = i;
147 }
148 mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0x10, 0, 0} + data);
149
150 EXPECT_TRUE(Write(data));
151}
152
153TEST_F(TcpTest, TestLargeReadSuccess) {
154 // 0x100000 = 1MiB.
155 std::string data(0x100000, '\0');
156 for (size_t i = 0; i < data.length(); ++i) {
157 data[i] = i;
158 }
159 mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0x10, 0, 0});
160 mock_->AddReceive(data);
161
162 EXPECT_TRUE(Read(data));
163}
164
165// Tests a few sample fastboot protocol commands.
166TEST_F(TcpTest, TestFastbootProtocolSuccess) {
167 mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0, 0, 14} + "getvar:version");
168 mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 7});
169 mock_->AddReceive("OKAY0.4");
170
171 mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0, 0, 10} + "getvar:all");
172 mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 16});
173 mock_->AddReceive("INFOversion: 0.4");
174 mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 12});
175 mock_->AddReceive("INFOfoo: bar");
176 mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 4});
177 mock_->AddReceive("OKAY");
178
179 EXPECT_TRUE(Write("getvar:version"));
180 EXPECT_TRUE(Read("OKAY0.4"));
181
182 EXPECT_TRUE(Write("getvar:all"));
183 EXPECT_TRUE(Read("INFOversion: 0.4"));
184 EXPECT_TRUE(Read("INFOfoo: bar"));
185 EXPECT_TRUE(Read("OKAY"));
186}
187
188TEST_F(TcpTest, TestReadLengthFailure) {
189 mock_->AddReceiveFailure();
190
191 char buffer[16];
192 EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer)));
193}
194
195TEST_F(TcpTest, TestReadDataFailure) {
196 mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 3});
197 mock_->AddReceiveFailure();
198
199 char buffer[16];
200 EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer)));
201}
202
203TEST_F(TcpTest, TestWriteFailure) {
204 mock_->ExpectSendFailure(std::string{0, 0, 0, 0, 0, 0, 0, 3} + "foo");
205
206 EXPECT_EQ(-1, transport_->Write("foo", 3));
207}
208
209TEST_F(TcpTest, TestTransportClose) {
210 EXPECT_EQ(0, transport_->Close());
211
212 // After closing, Transport Read()/Write() should return -1 without actually attempting any
213 // network operations.
214 char buffer[16];
215 EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer)));
216 EXPECT_EQ(-1, transport_->Write("foo", 3));
217}