#include "common.hpp"
#include "MediaSoupErrors.hpp"
#include "RTC/SCTP/packet/Parameter.hpp"
#include "RTC/SCTP/packet/parameters/HeartbeatInfoParameter.hpp"
#include "test/include/RTC/SCTP/sctpCommon.hpp"
#include <catch2/catch_test_macros.hpp>
#include <cstring>
SCENARIO("Heartbeat Info Parameter (1)", "[serializable][sctp][parameter]")
{
sctpCommon::ResetBuffers();
SECTION("HeartbeatInfoParameter::Parse() succeeds")
{
alignas(4) uint8_t buffer[] =
{
0x00, 0x01, 0x00, 0x0B,
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77, 0x00,
0xAA, 0xBB, 0xCC, 0xDD,
0xAA, 0xBB, 0xCC, 0xDD,
0xAA, 0xBB, 0xCC, 0xDD,
0xAA,
};
auto* parameter = RTC::SCTP::HeartbeatInfoParameter::Parse(buffer, sizeof(buffer));
CHECK_SCTP_PARAMETER(
parameter,
buffer,
sizeof(buffer),
12,
RTC::SCTP::Parameter::ParameterType::HEARTBEAT_INFO,
false,
RTC::SCTP::Parameter::ActionForUnknownParameterType::STOP);
REQUIRE(parameter->HasInfo() == true);
REQUIRE(parameter->GetInfoLength() == 7);
REQUIRE(parameter->GetInfo()[0] == 0x11);
REQUIRE(parameter->GetInfo()[1] == 0x22);
REQUIRE(parameter->GetInfo()[2] == 0x33);
REQUIRE(parameter->GetInfo()[3] == 0x44);
REQUIRE(parameter->GetInfo()[4] == 0x55);
REQUIRE(parameter->GetInfo()[5] == 0x66);
REQUIRE(parameter->GetInfo()[6] == 0x77);
REQUIRE(parameter->GetInfo()[7] == 0x00);
parameter->Serialize(sctpCommon::SerializeBuffer, sizeof(sctpCommon::SerializeBuffer));
std::memset(buffer, 0x00, sizeof(buffer));
CHECK_SCTP_PARAMETER(
parameter,
sctpCommon::SerializeBuffer,
sizeof(sctpCommon::SerializeBuffer),
12,
RTC::SCTP::Parameter::ParameterType::HEARTBEAT_INFO,
false,
RTC::SCTP::Parameter::ActionForUnknownParameterType::STOP);
REQUIRE(parameter->HasInfo() == true);
REQUIRE(parameter->GetInfoLength() == 7);
REQUIRE(parameter->GetInfo()[0] == 0x11);
REQUIRE(parameter->GetInfo()[1] == 0x22);
REQUIRE(parameter->GetInfo()[2] == 0x33);
REQUIRE(parameter->GetInfo()[3] == 0x44);
REQUIRE(parameter->GetInfo()[4] == 0x55);
REQUIRE(parameter->GetInfo()[5] == 0x66);
REQUIRE(parameter->GetInfo()[6] == 0x77);
REQUIRE(parameter->GetInfo()[7] == 0x00);
auto* clonedParameter =
parameter->Clone(sctpCommon::CloneBuffer, sizeof(sctpCommon::CloneBuffer));
std::memset(sctpCommon::SerializeBuffer, 0x00, sizeof(sctpCommon::SerializeBuffer));
delete parameter;
CHECK_SCTP_PARAMETER(
clonedParameter,
sctpCommon::CloneBuffer,
sizeof(sctpCommon::CloneBuffer),
12,
RTC::SCTP::Parameter::ParameterType::HEARTBEAT_INFO,
false,
RTC::SCTP::Parameter::ActionForUnknownParameterType::STOP);
REQUIRE(clonedParameter->HasInfo() == true);
REQUIRE(clonedParameter->GetInfoLength() == 7);
REQUIRE(clonedParameter->GetInfo()[0] == 0x11);
REQUIRE(clonedParameter->GetInfo()[1] == 0x22);
REQUIRE(clonedParameter->GetInfo()[2] == 0x33);
REQUIRE(clonedParameter->GetInfo()[3] == 0x44);
REQUIRE(clonedParameter->GetInfo()[4] == 0x55);
REQUIRE(clonedParameter->GetInfo()[5] == 0x66);
REQUIRE(clonedParameter->GetInfo()[6] == 0x77);
REQUIRE(clonedParameter->GetInfo()[7] == 0x00);
delete clonedParameter;
}
SECTION("HeartbeatInfoParameter::Parse() fails")
{
alignas(4) uint8_t buffer1[] =
{
0x00, 0x06, 0x00, 0x0B,
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77, 0x00,
};
REQUIRE(!RTC::SCTP::HeartbeatInfoParameter::Parse(buffer1, sizeof(buffer1)));
alignas(4) uint8_t buffer2[] =
{
0x00, 0x01, 0x00, 0x03,
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77, 0x00,
};
REQUIRE(!RTC::SCTP::HeartbeatInfoParameter::Parse(buffer2, sizeof(buffer2)));
alignas(4) uint8_t buffer4[] =
{
0x00, 0x01, 0x00, 0x0B,
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77
};
REQUIRE(!RTC::SCTP::HeartbeatInfoParameter::Parse(buffer4, sizeof(buffer4)));
}
SECTION("HeartbeatInfoParameter::Factory() succeeds")
{
auto* parameter = RTC::SCTP::HeartbeatInfoParameter::Factory(
sctpCommon::FactoryBuffer, sizeof(sctpCommon::FactoryBuffer));
CHECK_SCTP_PARAMETER(
parameter,
sctpCommon::FactoryBuffer,
sizeof(sctpCommon::FactoryBuffer),
4,
RTC::SCTP::Parameter::ParameterType::HEARTBEAT_INFO,
false,
RTC::SCTP::Parameter::ActionForUnknownParameterType::STOP);
REQUIRE(parameter->HasInfo() == false);
REQUIRE(parameter->GetInfoLength() == 0);
parameter->SetInfo(sctpCommon::DataBuffer + 1000, 3000);
REQUIRE(parameter->GetLength() == 3004);
REQUIRE(parameter->HasInfo() == true);
REQUIRE(parameter->GetInfoLength() == 3000);
parameter->SetInfo(nullptr, 0);
REQUIRE(parameter->GetLength() == 4);
REQUIRE(parameter->HasInfo() == false);
REQUIRE(parameter->GetInfoLength() == 0);
parameter->SetInfo(sctpCommon::DataBuffer, 2);
REQUIRE(parameter->GetLength() == 8);
REQUIRE(parameter->HasInfo() == true);
REQUIRE(parameter->GetInfoLength() == 2);
parameter->SetInfo(sctpCommon::DataBuffer + 2000, 2000);
REQUIRE(parameter->GetLength() == 2004);
REQUIRE(parameter->HasInfo() == true);
REQUIRE(parameter->GetInfoLength() == 2000);
parameter->SetInfo(sctpCommon::DataBuffer, 5);
CHECK_SCTP_PARAMETER(
parameter,
sctpCommon::FactoryBuffer,
sizeof(sctpCommon::FactoryBuffer),
12,
RTC::SCTP::Parameter::ParameterType::HEARTBEAT_INFO,
false,
RTC::SCTP::Parameter::ActionForUnknownParameterType::STOP);
REQUIRE(parameter->HasInfo() == true);
REQUIRE(parameter->GetInfoLength() == 5);
REQUIRE(parameter->GetInfo()[0] == 0x00);
REQUIRE(parameter->GetInfo()[1] == 0x01);
REQUIRE(parameter->GetInfo()[2] == 0x02);
REQUIRE(parameter->GetInfo()[3] == 0x03);
REQUIRE(parameter->GetInfo()[4] == 0x04);
REQUIRE(parameter->GetInfo()[5] == 0x00);
REQUIRE(parameter->GetInfo()[6] == 0x00);
REQUIRE(parameter->GetInfo()[7] == 0x00);
auto* parsedParameter =
RTC::SCTP::HeartbeatInfoParameter::Parse(parameter->GetBuffer(), parameter->GetLength());
delete parameter;
CHECK_SCTP_PARAMETER(
parsedParameter,
sctpCommon::FactoryBuffer,
12,
12,
RTC::SCTP::Parameter::ParameterType::HEARTBEAT_INFO,
false,
RTC::SCTP::Parameter::ActionForUnknownParameterType::STOP);
REQUIRE(parsedParameter->HasInfo() == true);
REQUIRE(parsedParameter->GetInfoLength() == 5);
REQUIRE(parsedParameter->GetInfo()[0] == 0x00);
REQUIRE(parsedParameter->GetInfo()[1] == 0x01);
REQUIRE(parsedParameter->GetInfo()[2] == 0x02);
REQUIRE(parsedParameter->GetInfo()[3] == 0x03);
REQUIRE(parsedParameter->GetInfo()[4] == 0x04);
REQUIRE(parsedParameter->GetInfo()[5] == 0x00);
REQUIRE(parsedParameter->GetInfo()[6] == 0x00);
REQUIRE(parsedParameter->GetInfo()[7] == 0x00);
delete parsedParameter;
}
SECTION("HeartbeatInfoParameter::SetInfo() throws if infoLength is too big")
{
auto* parameter = RTC::SCTP::HeartbeatInfoParameter::Factory(
sctpCommon::ThrowBuffer, sizeof(sctpCommon::ThrowBuffer));
CHECK_SCTP_PARAMETER(
parameter,
sctpCommon::ThrowBuffer,
sizeof(sctpCommon::ThrowBuffer),
4,
RTC::SCTP::Parameter::ParameterType::HEARTBEAT_INFO,
false,
RTC::SCTP::Parameter::ActionForUnknownParameterType::STOP);
REQUIRE_THROWS_AS(parameter->SetInfo(sctpCommon::ThrowBuffer, 65535), MediaSoupError);
CHECK_SCTP_PARAMETER(
parameter,
sctpCommon::ThrowBuffer,
sizeof(sctpCommon::ThrowBuffer),
4,
RTC::SCTP::Parameter::ParameterType::HEARTBEAT_INFO,
false,
RTC::SCTP::Parameter::ActionForUnknownParameterType::STOP);
delete parameter;
}
}