Skip to content

TCPEndpoint_8ut_8cpp

github-actions edited this page Mar 15, 2026 · 3 revisions

title: src/TCP/TCPEndpoint.ut.cpp


src/TCP/TCPEndpoint.ut.cpp

Namespaces

Name
aasdk
aasdk::tcp
aasdk::tcp::ut

Classes

Name
class aasdk::tcp::ut::TCPEndpointUnitTest

Source code

/*
*  This file is part of aasdk library project.
*  Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
*  aasdk is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 3 of the License, or
*  (at your option) any later version.

*  aasdk is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with aasdk. If not, see <http://www.gnu.org/licenses/>.
*/

#include <gtest/gtest.h>
#include <aasdk/TCP/UT/TCPWrapper.mock.hpp>
#include <aasdk/TCP/UT/TCPEndpointPromiseHandler.mock.hpp>
#include <aasdk/TCP/TCPEndpoint.hpp>


namespace aasdk
{
namespace tcp
{
namespace ut
{

using ::testing::_;
using ::testing::DoAll;
using ::testing::SaveArg;

class TCPEndpointUnitTest : public testing::Test
{
protected:
    TCPEndpointUnitTest()
        : socket_(std::make_shared<boost::asio::ip::tcp::socket>(ioService_))
        , promise_(ITCPEndpoint::Promise::defer(ioService_))
    {
        promise_->then(std::bind(&TCPEndpointPromiseHandlerMock::onResolve, &promiseHandlerMock_, std::placeholders::_1),
                       std::bind(&TCPEndpointPromiseHandlerMock::onReject, &promiseHandlerMock_, std::placeholders::_1));
    }

    TCPWrapperMock tcpWrapperMock_;
    TCPEndpointPromiseHandlerMock promiseHandlerMock_;
    boost::asio::io_service ioService_;
    ITCPEndpoint::SocketPointer socket_;
    ITCPEndpoint::Promise::Pointer promise_;
};

TEST_F(TCPEndpointUnitTest, TCPEndpoint_Receive)
{
    auto tcpEndpoint = std::make_shared<TCPEndpoint>(tcpWrapperMock_, std::move(socket_));

    common::DataBuffer buffer;
    ITCPWrapper::Handler handler;
    EXPECT_CALL(tcpWrapperMock_, asyncRead(_, _, _)).WillOnce(DoAll(SaveArg<1>(&buffer), SaveArg<2>(&handler)));

    common::Data actualData(100, 0);
    tcpEndpoint->receive(common::DataBuffer(actualData), std::move(promise_));

    const common::Data expectedData(actualData.size(), 0x5F);
    std::copy(expectedData.begin(), expectedData.end(), buffer.data);

    EXPECT_CALL(promiseHandlerMock_, onResolve(expectedData.size()));
    EXPECT_CALL(promiseHandlerMock_, onReject(_)).Times(0);
    handler(boost::system::error_code(), expectedData.size());

    ioService_.run();

    EXPECT_THAT(actualData, testing::ContainerEq(expectedData));
}

TEST_F(TCPEndpointUnitTest, TCPEndpoint_ReceiveError)
{
    auto tcpEndpoint = std::make_shared<TCPEndpoint>(tcpWrapperMock_, std::move(socket_));

    common::DataBuffer buffer;
    ITCPWrapper::Handler handler;
    EXPECT_CALL(tcpWrapperMock_, asyncRead(_, _, _)).WillOnce(DoAll(SaveArg<1>(&buffer), SaveArg<2>(&handler)));

    common::Data actualData(100, 0);
    tcpEndpoint->receive(common::DataBuffer(actualData), std::move(promise_));

    EXPECT_CALL(promiseHandlerMock_, onResolve(_)).Times(0);
    EXPECT_CALL(promiseHandlerMock_, onReject(error::Error(error::ErrorCode::TCP_TRANSFER, boost::asio::error::bad_descriptor)));
    handler(boost::asio::error::bad_descriptor, 0);

    ioService_.run();
}

TEST_F(TCPEndpointUnitTest, TCPEndpoint_Send)
{
    auto tcpEndpoint = std::make_shared<TCPEndpoint>(tcpWrapperMock_, std::move(socket_));

    common::Data actualData(100, 0);
    common::DataConstBuffer buffer(actualData);
    ITCPWrapper::Handler handler;
    EXPECT_CALL(tcpWrapperMock_, asyncWrite(_, buffer, _)).WillOnce(SaveArg<2>(&handler));
    tcpEndpoint->send(common::DataConstBuffer(actualData), std::move(promise_));

    EXPECT_CALL(promiseHandlerMock_, onResolve(actualData.size()));
    EXPECT_CALL(promiseHandlerMock_, onReject(_)).Times(0);
    handler(boost::system::error_code(), actualData.size());

    ioService_.run();
}

TEST_F(TCPEndpointUnitTest, TCPEndpoint_SendError)
{
    auto tcpEndpoint = std::make_shared<TCPEndpoint>(tcpWrapperMock_, std::move(socket_));

    common::Data actualData(100, 0);
    common::DataConstBuffer buffer(actualData);
    ITCPWrapper::Handler handler;
    EXPECT_CALL(tcpWrapperMock_, asyncWrite(_, buffer, _)).WillOnce(SaveArg<2>(&handler));
    tcpEndpoint->send(common::DataConstBuffer(actualData), std::move(promise_));

    EXPECT_CALL(promiseHandlerMock_, onResolve(_)).Times(0);
    EXPECT_CALL(promiseHandlerMock_, onReject(error::Error(error::ErrorCode::OPERATION_ABORTED)));
    handler(boost::asio::error::operation_aborted, 0);

    ioService_.run();
}

}
}
}

Updated on 2026-03-15 at 09:02:41 +0000

Clone this wiki locally