Skip to content

AccessoryModeProtocolVersionQuery_8ut_8cpp

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

title: src/USB/AccessoryModeProtocolVersionQuery.ut.cpp


src/USB/AccessoryModeProtocolVersionQuery.ut.cpp

Namespaces

Name
aasdk
aasdk::usb
aasdk::usb::ut

Classes

Name
class aasdk::usb::ut::AccessoryModeProtocolVersionQueryUnitTest

Source code

// This file is part of aasdk library project.
// Copyright (C) 2018 f1x.studio (Michal Szwaj)
// Copyright (C) 2024 CubeOne (Simon Dean - simon.dean@cubeone.co.uk)
// Copyright (C) 2026 OpenCarDev (Matthew Hilton - matthilton2005@gmail.com)
//
// 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 <aasdk/USB/UT/USBWrapper.mock.hpp>
#include <aasdk/USB/UT/USBEndpoint.mock.hpp>
#include <aasdk/USB/UT/AccessoryModeQueryPromiseHandler.mock.hpp>
#include <aasdk/USB/AccessoryModeProtocolVersionQuery.hpp>


namespace aasdk
{
namespace usb
{
namespace ut
{

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

class AccessoryModeProtocolVersionQueryUnitTest : public testing::Test
{
protected:
    AccessoryModeProtocolVersionQueryUnitTest()
      : usbEndpointMock_(std::make_shared<USBEndpointMock>())
      , usbEndpoint_(usbEndpointMock_.get(), [](auto*) {})
      , promise_(IAccessoryModeQuery::Promise::defer(ioService_))
    {
        promise_->then(std::bind(&AccessoryModeQueryPromiseHandlerMock::onResolve, &promiseHandlerMock_, std::placeholders::_1),
                      std::bind(&AccessoryModeQueryPromiseHandlerMock::onReject, &promiseHandlerMock_, std::placeholders::_1));
    }

    void scenario_ValidProtocolVersion(uint16_t protocolVersion)
    {
        common::DataBuffer buffer;
        IUSBEndpoint::Promise::Pointer usbEndpointPromise;
        EXPECT_CALL(*usbEndpointMock_, controlTransfer(_, _, _)).WillOnce(DoAll(SaveArg<0>(&buffer), SaveArg<2>(&usbEndpointPromise)));
        EXPECT_CALL(usbWrapperMock_, fillControlSetup(NotNull(), LIBUSB_ENDPOINT_IN | USB_TYPE_VENDOR, ACC_REQ_GET_PROTOCOL, 0, 0, sizeof(protocolVersion)));

        AccessoryModeProtocolVersionQuery::Pointer query(std::make_shared<AccessoryModeProtocolVersionQuery>(ioService_, usbWrapperMock_, usbEndpointMock_));
        query->start(std::move(promise_));
        ioService_.run();
        ioService_.reset();

        reinterpret_cast<uint16_t&>(buffer.data[8]) = protocolVersion;
        usbEndpointPromise->resolve(buffer.size);

        EXPECT_CALL(promiseHandlerMock_, onReject(_)).Times(0);
        EXPECT_CALL(promiseHandlerMock_, onResolve(usbEndpoint_));
        ioService_.run();
    }

    boost::asio::io_service ioService_;
    USBWrapperMock usbWrapperMock_;
    std::shared_ptr<USBEndpointMock> usbEndpointMock_;
    IUSBEndpoint::Pointer usbEndpoint_;
    AccessoryModeQueryPromiseHandlerMock promiseHandlerMock_;
    IAccessoryModeQuery::Promise::Pointer promise_;

    static constexpr uint32_t USB_TYPE_VENDOR = 0x40;
    static constexpr uint32_t ACC_REQ_GET_PROTOCOL = 51;
};

TEST_F(AccessoryModeProtocolVersionQueryUnitTest, AccessoryModeProtocolVersionQuery_ProtcolVersion1)
{
    this->scenario_ValidProtocolVersion(1);
}

TEST_F(AccessoryModeProtocolVersionQueryUnitTest, AccessoryModeProtocolVersionQuery_ProtcolVersion2)
{
    this->scenario_ValidProtocolVersion(2);
}

TEST_F(AccessoryModeProtocolVersionQueryUnitTest, AccessoryModeProtocolVersionQuery_InvalidProtocolVersion)
{
    const uint16_t protocolVersion = 3;

    common::DataBuffer buffer;
    IUSBEndpoint::Promise::Pointer usbEndpointPromise;
    EXPECT_CALL(*usbEndpointMock_, controlTransfer(_, _, _)).WillOnce(DoAll(SaveArg<0>(&buffer), SaveArg<2>(&usbEndpointPromise)));
    EXPECT_CALL(usbWrapperMock_, fillControlSetup(NotNull(), LIBUSB_ENDPOINT_IN | USB_TYPE_VENDOR, ACC_REQ_GET_PROTOCOL, 0, 0, sizeof(protocolVersion)));

    AccessoryModeProtocolVersionQuery::Pointer query(std::make_shared<AccessoryModeProtocolVersionQuery>(ioService_, usbWrapperMock_, usbEndpointMock_));
    query->start(std::move(promise_));
    ioService_.run();
    ioService_.reset();

    reinterpret_cast<uint16_t&>(buffer.data[8]) = protocolVersion;
    usbEndpointPromise->resolve(buffer.size);

    EXPECT_CALL(promiseHandlerMock_, onReject(error::Error(error::ErrorCode::USB_AOAP_PROTOCOL_VERSION)));
    EXPECT_CALL(promiseHandlerMock_, onResolve(_)).Times(0);
    ioService_.run();
}

TEST_F(AccessoryModeProtocolVersionQueryUnitTest, AccessoryModeProtocolVersionQuery_TransferError)
{
    const uint16_t protocolVersion = 1;

    common::DataBuffer buffer;
    IUSBEndpoint::Promise::Pointer usbEndpointPromise;
    EXPECT_CALL(*usbEndpointMock_, controlTransfer(_, _, _)).WillOnce(DoAll(SaveArg<0>(&buffer), SaveArg<2>(&usbEndpointPromise)));
    EXPECT_CALL(usbWrapperMock_, fillControlSetup(NotNull(), LIBUSB_ENDPOINT_IN | USB_TYPE_VENDOR, ACC_REQ_GET_PROTOCOL, 0, 0, sizeof(protocolVersion)));

    AccessoryModeProtocolVersionQuery::Pointer query(std::make_shared<AccessoryModeProtocolVersionQuery>(ioService_, usbWrapperMock_, usbEndpointMock_));
    query->start(std::move(promise_));
    ioService_.run();
    ioService_.reset();

    reinterpret_cast<uint16_t&>(buffer.data[8]) = protocolVersion;
    const error::Error transferError(error::ErrorCode::USB_TRANSFER, LIBUSB_TRANSFER_ERROR);
    usbEndpointPromise->reject(transferError);

    EXPECT_CALL(promiseHandlerMock_, onReject(transferError));
    EXPECT_CALL(promiseHandlerMock_, onResolve(_)).Times(0);
    ioService_.run();
}

TEST_F(AccessoryModeProtocolVersionQueryUnitTest, AccessoryModeProtocolVersionQuery_RejectWhenInProgress)
{
    const uint16_t protocolVersion = 1;

    common::DataBuffer buffer;
    IUSBEndpoint::Promise::Pointer usbEndpointPromise;
    EXPECT_CALL(*usbEndpointMock_, controlTransfer(_, _, _)).WillOnce(DoAll(SaveArg<0>(&buffer), SaveArg<2>(&usbEndpointPromise)));
    EXPECT_CALL(usbWrapperMock_, fillControlSetup(NotNull(), LIBUSB_ENDPOINT_IN | USB_TYPE_VENDOR, ACC_REQ_GET_PROTOCOL, 0, 0, sizeof(protocolVersion)));

    AccessoryModeProtocolVersionQuery::Pointer query(std::make_shared<AccessoryModeProtocolVersionQuery>(ioService_, usbWrapperMock_, usbEndpointMock_));
    query->start(std::move(promise_));
    ioService_.run();
    ioService_.reset();

    AccessoryModeQueryPromiseHandlerMock secondPromiseHandlerMock;
    auto secondPromise = IAccessoryModeQuery::Promise::defer(ioService_);
    secondPromise->then(std::bind(&AccessoryModeQueryPromiseHandlerMock::onResolve, &secondPromiseHandlerMock, std::placeholders::_1),
                       std::bind(&AccessoryModeQueryPromiseHandlerMock::onReject, &secondPromiseHandlerMock, std::placeholders::_1));

    EXPECT_CALL(secondPromiseHandlerMock, onReject(error::Error(error::ErrorCode::OPERATION_IN_PROGRESS)));
    EXPECT_CALL(secondPromiseHandlerMock, onResolve(_)).Times(0);
    query->start(std::move(secondPromise));
    ioService_.run();
}

}
}
}

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

Clone this wiki locally