| David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
|  | 35 | TEST(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 |  | 
| David Pursell | aad72a5 | 2016-02-17 10:00:09 -0800 | [diff] [blame] | 45 | TEST(TcpConnectTest, TestNewerVersionSuccess) { | 
|  | 46 | std::unique_ptr<SocketMock> mock(new SocketMock); | 
|  | 47 | mock->ExpectSend("FB01"); | 
|  | 48 | mock->AddReceive("FB99"); | 
|  | 49 |  | 
|  | 50 | std::string error; | 
|  | 51 | EXPECT_NE(nullptr, tcp::internal::Connect(std::move(mock), &error)); | 
|  | 52 | EXPECT_EQ("", error); | 
|  | 53 | } | 
|  | 54 |  | 
| David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 55 | TEST(TcpConnectTest, TestSendFailure) { | 
|  | 56 | std::unique_ptr<SocketMock> mock(new SocketMock); | 
|  | 57 | mock->ExpectSendFailure("FB01"); | 
|  | 58 |  | 
|  | 59 | std::string error; | 
|  | 60 | EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error)); | 
|  | 61 | EXPECT_NE(std::string::npos, error.find("Failed to send initialization message")); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | TEST(TcpConnectTest, TestNoResponseFailure) { | 
|  | 65 | std::unique_ptr<SocketMock> mock(new SocketMock); | 
|  | 66 | mock->ExpectSend("FB01"); | 
|  | 67 | mock->AddReceiveFailure(); | 
|  | 68 |  | 
|  | 69 | std::string error; | 
|  | 70 | EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error)); | 
|  | 71 | EXPECT_NE(std::string::npos, error.find("No initialization message received")); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | TEST(TcpConnectTest, TestBadResponseFailure) { | 
|  | 75 | std::unique_ptr<SocketMock> mock(new SocketMock); | 
|  | 76 | mock->ExpectSend("FB01"); | 
|  | 77 | mock->AddReceive("XX01"); | 
|  | 78 |  | 
|  | 79 | std::string error; | 
|  | 80 | EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error)); | 
|  | 81 | EXPECT_NE(std::string::npos, error.find("Unrecognized initialization message")); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | TEST(TcpConnectTest, TestUnknownVersionFailure) { | 
|  | 85 | std::unique_ptr<SocketMock> mock(new SocketMock); | 
|  | 86 | mock->ExpectSend("FB01"); | 
| David Pursell | aad72a5 | 2016-02-17 10:00:09 -0800 | [diff] [blame] | 87 | mock->AddReceive("FB00"); | 
| David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 88 |  | 
|  | 89 | std::string error; | 
|  | 90 | EXPECT_EQ(nullptr, tcp::internal::Connect(std::move(mock), &error)); | 
| David Pursell | aad72a5 | 2016-02-17 10:00:09 -0800 | [diff] [blame] | 91 | EXPECT_EQ("Unknown TCP protocol version 00 (host version 01)", error); | 
| David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
|  | 94 | // Fixture to configure a SocketMock for a successful TCP connection. | 
|  | 95 | class TcpTest : public ::testing::Test { | 
|  | 96 | protected: | 
|  | 97 | void SetUp() override { | 
|  | 98 | mock_ = new SocketMock; | 
|  | 99 | mock_->ExpectSend("FB01"); | 
|  | 100 | mock_->AddReceive("FB01"); | 
|  | 101 |  | 
|  | 102 | std::string error; | 
|  | 103 | transport_ = tcp::internal::Connect(std::unique_ptr<Socket>(mock_), &error); | 
|  | 104 | ASSERT_NE(nullptr, transport_); | 
|  | 105 | ASSERT_EQ("", error); | 
|  | 106 | }; | 
|  | 107 |  | 
|  | 108 | // Writes |message| to |transport_|, returns true on success. | 
|  | 109 | bool Write(const std::string& message) { | 
|  | 110 | return transport_->Write(message.data(), message.length()) == | 
|  | 111 | static_cast<ssize_t>(message.length()); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | // Reads from |transport_|, returns true if it matches |message|. | 
|  | 115 | bool Read(const std::string& message) { | 
|  | 116 | std::string buffer(message.length(), '\0'); | 
|  | 117 | return transport_->Read(&buffer[0], buffer.length()) == | 
|  | 118 | static_cast<ssize_t>(message.length()) && | 
|  | 119 | buffer == message; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | // Use a raw SocketMock* here because we pass ownership to the Transport object, but we still | 
|  | 123 | // need access to configure mock expectations. | 
|  | 124 | SocketMock* mock_ = nullptr; | 
|  | 125 | std::unique_ptr<Transport> transport_; | 
|  | 126 | }; | 
|  | 127 |  | 
|  | 128 | TEST_F(TcpTest, TestWriteSuccess) { | 
|  | 129 | mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0, 0, 3} + "foo"); | 
|  | 130 |  | 
|  | 131 | EXPECT_TRUE(Write("foo")); | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | TEST_F(TcpTest, TestReadSuccess) { | 
|  | 135 | mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 3}); | 
|  | 136 | mock_->AddReceive("foo"); | 
|  | 137 |  | 
|  | 138 | EXPECT_TRUE(Read("foo")); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | // Tests that fragmented TCP reads are handled properly. | 
|  | 142 | TEST_F(TcpTest, TestReadFragmentSuccess) { | 
|  | 143 | mock_->AddReceive(std::string{0, 0, 0, 0}); | 
|  | 144 | mock_->AddReceive(std::string{0, 0, 0, 3}); | 
|  | 145 | mock_->AddReceive("f"); | 
|  | 146 | mock_->AddReceive("o"); | 
|  | 147 | mock_->AddReceive("o"); | 
|  | 148 |  | 
|  | 149 | EXPECT_TRUE(Read("foo")); | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | TEST_F(TcpTest, TestLargeWriteSuccess) { | 
|  | 153 | // 0x100000 = 1MiB. | 
|  | 154 | std::string data(0x100000, '\0'); | 
|  | 155 | for (size_t i = 0; i < data.length(); ++i) { | 
|  | 156 | data[i] = i; | 
|  | 157 | } | 
|  | 158 | mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0x10, 0, 0} + data); | 
|  | 159 |  | 
|  | 160 | EXPECT_TRUE(Write(data)); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | TEST_F(TcpTest, TestLargeReadSuccess) { | 
|  | 164 | // 0x100000 = 1MiB. | 
|  | 165 | std::string data(0x100000, '\0'); | 
|  | 166 | for (size_t i = 0; i < data.length(); ++i) { | 
|  | 167 | data[i] = i; | 
|  | 168 | } | 
|  | 169 | mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0x10, 0, 0}); | 
|  | 170 | mock_->AddReceive(data); | 
|  | 171 |  | 
|  | 172 | EXPECT_TRUE(Read(data)); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | // Tests a few sample fastboot protocol commands. | 
|  | 176 | TEST_F(TcpTest, TestFastbootProtocolSuccess) { | 
|  | 177 | mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0, 0, 14} + "getvar:version"); | 
|  | 178 | mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 7}); | 
|  | 179 | mock_->AddReceive("OKAY0.4"); | 
|  | 180 |  | 
|  | 181 | mock_->ExpectSend(std::string{0, 0, 0, 0, 0, 0, 0, 10} + "getvar:all"); | 
|  | 182 | mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 16}); | 
|  | 183 | mock_->AddReceive("INFOversion: 0.4"); | 
|  | 184 | mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 12}); | 
|  | 185 | mock_->AddReceive("INFOfoo: bar"); | 
|  | 186 | mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 4}); | 
|  | 187 | mock_->AddReceive("OKAY"); | 
|  | 188 |  | 
|  | 189 | EXPECT_TRUE(Write("getvar:version")); | 
|  | 190 | EXPECT_TRUE(Read("OKAY0.4")); | 
|  | 191 |  | 
|  | 192 | EXPECT_TRUE(Write("getvar:all")); | 
|  | 193 | EXPECT_TRUE(Read("INFOversion: 0.4")); | 
|  | 194 | EXPECT_TRUE(Read("INFOfoo: bar")); | 
|  | 195 | EXPECT_TRUE(Read("OKAY")); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | TEST_F(TcpTest, TestReadLengthFailure) { | 
|  | 199 | mock_->AddReceiveFailure(); | 
|  | 200 |  | 
|  | 201 | char buffer[16]; | 
|  | 202 | EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer))); | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | TEST_F(TcpTest, TestReadDataFailure) { | 
|  | 206 | mock_->AddReceive(std::string{0, 0, 0, 0, 0, 0, 0, 3}); | 
|  | 207 | mock_->AddReceiveFailure(); | 
|  | 208 |  | 
|  | 209 | char buffer[16]; | 
|  | 210 | EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer))); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | TEST_F(TcpTest, TestWriteFailure) { | 
|  | 214 | mock_->ExpectSendFailure(std::string{0, 0, 0, 0, 0, 0, 0, 3} + "foo"); | 
|  | 215 |  | 
|  | 216 | EXPECT_EQ(-1, transport_->Write("foo", 3)); | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | TEST_F(TcpTest, TestTransportClose) { | 
|  | 220 | EXPECT_EQ(0, transport_->Close()); | 
|  | 221 |  | 
|  | 222 | // After closing, Transport Read()/Write() should return -1 without actually attempting any | 
|  | 223 | // network operations. | 
|  | 224 | char buffer[16]; | 
|  | 225 | EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer))); | 
|  | 226 | EXPECT_EQ(-1, transport_->Write("foo", 3)); | 
|  | 227 | } |