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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
* Copyright (C) 2016 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "tcp.h"
#include <gtest/gtest.h>
#include "socket_mock.h"
TEST(TcpConnectTest, TestSuccess) {
std::unique_ptr<SocketMock> mock(new SocketMock);
mock->ExpectSend("FB01");
mock->AddReceive("FB01");
std::string error;
EXPECT_NE(nullptr, tcp::internal::Connect(std::move(mock), &error));
EXPECT_EQ("", error);
}
TEST(TcpConnectTest, TestNewerVersionSuccess) {
std::unique_ptr<SocketMock> mock(new SocketMock);
mock->ExpectSend("FB01");
mock->AddReceive("FB99");
std::string error;
EXPECT_NE(nullptr, tcp::internal::Connect(std::move(mock), &error));
EXPECT_EQ("", error);
}
TEST(TcpConnectTest, TestSendFailure) {
std::unique_ptr<SocketMock> mock(new SocketMock);
mock->ExpectSendFailure("FB01");
std::string error;
EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error));
EXPECT_NE(std::string::npos, error.find("Failed to send initialization message"));
}
TEST(TcpConnectTest, TestNoResponseFailure) {
std::unique_ptr<SocketMock> mock(new SocketMock);
mock->ExpectSend("FB01");
mock->AddReceiveFailure();
std::string error;
EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error));
EXPECT_NE(std::string::npos, error.find("No initialization message received"));
}
TEST(TcpConnectTest, TestBadResponseFailure) {
std::unique_ptr<SocketMock> mock(new SocketMock);
mock->ExpectSend("FB01");
mock->AddReceive("XX01");
std::string error;
EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error));
EXPECT_NE(std::string::npos, error.find("Unrecognized initialization message"));
}
TEST(TcpConnectTest, TestUnknownVersionFailure) {
std::unique_ptr<SocketMock> mock(new SocketMock);
mock->ExpectSend("FB01");
mock->AddReceive("FB00");
std::string error;
EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error));
EXPECT_EQ("Unknown TCP protocol version 00 (host version 01)", error);
}
// Fixture to configure a SocketMock for a successful TCP connection.
class TcpTest : public ::testing::Test {
protected:
void SetUp() override {
mock_ = new SocketMock;
mock_->ExpectSend("FB01");
mock_->AddReceive("FB01");
std::string error;
transport_ = tcp::internal::Connect(std::unique_ptr<Socket>(mock_), &error);
ASSERT_NE(nullptr, transport_);
ASSERT_EQ("", error);
};
// Writes |message| to |transport_|, returns true on success.
bool Write(const std::string& message) {
return transport_->Write(message.data(), message.length()) ==
static_cast<ssize_t>(message.length());
}
// Reads from |transport_|, returns true if it matches |message|.
bool Read(const std::string& message) {
std::string buffer(message.length(), '\0');
return transport_->Read(&buffer[0], buffer.length()) ==
static_cast<ssize_t>(message.length()) &&
buffer == message;
}
// Use a raw SocketMock* here because we pass ownership to the Transport object, but we still
// need access to configure mock expectations.
SocketMock* mock_ = nullptr;
std::unique_ptr<Transport> transport_;
};
TEST_F(TcpTest, TestWriteSuccess) {
mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0, 0, 3} + "foo");
EXPECT_TRUE(Write("foo"));
}
TEST_F(TcpTest, TestReadSuccess) {
mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 3});
mock_->AddReceive("foo");
EXPECT_TRUE(Read("foo"));
}
// Tests that fragmented TCP reads are handled properly.
TEST_F(TcpTest, TestReadFragmentSuccess) {
mock_->AddReceive(std::string{0, 0, 0, 0});
mock_->AddReceive(std::string{0, 0, 0, 3});
mock_->AddReceive("f");
mock_->AddReceive("o");
mock_->AddReceive("o");
EXPECT_TRUE(Read("foo"));
}
TEST_F(TcpTest, TestLargeWriteSuccess) {
// 0x100000 = 1MiB.
std::string data(0x100000, '\0');
for (size_t i = 0; i < data.length(); ++i) {
data[i] = i;
}
mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0x10, 0, 0} + data);
EXPECT_TRUE(Write(data));
}
TEST_F(TcpTest, TestLargeReadSuccess) {
// 0x100000 = 1MiB.
std::string data(0x100000, '\0');
for (size_t i = 0; i < data.length(); ++i) {
data[i] = i;
}
mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0x10, 0, 0});
mock_->AddReceive(data);
EXPECT_TRUE(Read(data));
}
// Tests a few sample fastboot protocol commands.
TEST_F(TcpTest, TestFastbootProtocolSuccess) {
mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0, 0, 14} + "getvar:version");
mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 7});
mock_->AddReceive("OKAY0.4");
mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0, 0, 10} + "getvar:all");
mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 16});
mock_->AddReceive("INFOversion: 0.4");
mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 12});
mock_->AddReceive("INFOfoo: bar");
mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 4});
mock_->AddReceive("OKAY");
EXPECT_TRUE(Write("getvar:version"));
EXPECT_TRUE(Read("OKAY0.4"));
EXPECT_TRUE(Write("getvar:all"));
EXPECT_TRUE(Read("INFOversion: 0.4"));
EXPECT_TRUE(Read("INFOfoo: bar"));
EXPECT_TRUE(Read("OKAY"));
}
TEST_F(TcpTest, TestReadLengthFailure) {
mock_->AddReceiveFailure();
char buffer[16];
EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer)));
}
TEST_F(TcpTest, TestReadDataFailure) {
mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 3});
mock_->AddReceiveFailure();
char buffer[16];
EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer)));
}
TEST_F(TcpTest, TestWriteFailure) {
mock_->ExpectSendFailure(std::string{0, 0, 0, 0, 0, 0, 0, 3} + "foo");
EXPECT_EQ(-1, transport_->Write("foo", 3));
}
TEST_F(TcpTest, TestTransportClose) {
EXPECT_EQ(0, transport_->Close());
// After closing, Transport Read()/Write() should return -1 without actually attempting any
// network operations.
char buffer[16];
EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer)));
EXPECT_EQ(-1, transport_->Write("foo", 3));
}
|