123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- #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);
- }
- 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);
- };
-
- bool Write(const std::string& message) {
- return transport_->Write(message.data(), message.length()) ==
- static_cast<ssize_t>(message.length());
- }
-
- 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;
- }
-
-
- 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"));
- }
- 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) {
-
- 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) {
-
- 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));
- }
- 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());
-
-
- char buffer[16];
- EXPECT_EQ(-1, transport_->Read(buffer, sizeof(buffer)));
- EXPECT_EQ(-1, transport_->Write("foo", 3));
- }
|