#include "common.hpp"
#include "MediaSoupErrors.hpp"
#include "RTC/SCTP/packet/ErrorCause.hpp"
#include "RTC/SCTP/packet/errorCauses/ProtocolViolationErrorCause.hpp"
#include "RTC/SCTP/sctpCommon.hpp"
#include <catch2/catch_test_macros.hpp>
#include <cstring>
SCENARIO("Protocol Violation Error Cause (13)", "[serializable][sctp][errorcause]")
{
sctpCommon::ResetBuffers();
SECTION("ProtocolViolationErrorCause::Parse() succeeds")
{
alignas(4) uint8_t buffer[] =
{
0x00, 0x0D, 0x00, 0x0A,
0x65, 0x72, 0x72, 0x6F,
0x72, 0x31, 0x00, 0x00,
0xAA, 0xBB, 0xCC, 0xDD,
0xAA, 0xBB, 0xCC,
};
auto* errorCause = RTC::SCTP::ProtocolViolationErrorCause::Parse(buffer, sizeof(buffer));
CHECK_SCTP_ERROR_CAUSE(
errorCause,
buffer,
sizeof(buffer),
12,
RTC::SCTP::ErrorCause::ErrorCauseCode::PROTOCOL_VIOLATION,
false);
REQUIRE(errorCause->HasAdditionalInformation() == true);
REQUIRE(errorCause->GetAdditionalInformationLength() == 6);
REQUIRE(errorCause->GetAdditionalInformation()[0] == 0x65);
REQUIRE(errorCause->GetAdditionalInformation()[1] == 0x72);
REQUIRE(errorCause->GetAdditionalInformation()[2] == 0x72);
REQUIRE(errorCause->GetAdditionalInformation()[3] == 0x6F);
REQUIRE(errorCause->GetAdditionalInformation()[4] == 0x72);
REQUIRE(errorCause->GetAdditionalInformation()[5] == 0x31);
std::string additionalInfo(
reinterpret_cast<const char*>(errorCause->GetAdditionalInformation()),
errorCause->GetAdditionalInformationLength());
REQUIRE(additionalInfo == "error1");
REQUIRE(errorCause->GetAdditionalInformation()[6] == 0x00);
REQUIRE(errorCause->GetAdditionalInformation()[7] == 0x00);
errorCause->Serialize(sctpCommon::SerializeBuffer, sizeof(sctpCommon::SerializeBuffer));
std::memset(buffer, 0x00, sizeof(buffer));
CHECK_SCTP_ERROR_CAUSE(
errorCause,
sctpCommon::SerializeBuffer,
sizeof(sctpCommon::SerializeBuffer),
12,
RTC::SCTP::ErrorCause::ErrorCauseCode::PROTOCOL_VIOLATION,
false);
REQUIRE(errorCause->HasAdditionalInformation() == true);
REQUIRE(errorCause->GetAdditionalInformationLength() == 6);
REQUIRE(errorCause->GetAdditionalInformation()[0] == 0x65);
REQUIRE(errorCause->GetAdditionalInformation()[1] == 0x72);
REQUIRE(errorCause->GetAdditionalInformation()[2] == 0x72);
REQUIRE(errorCause->GetAdditionalInformation()[3] == 0x6F);
REQUIRE(errorCause->GetAdditionalInformation()[4] == 0x72);
REQUIRE(errorCause->GetAdditionalInformation()[5] == 0x31);
additionalInfo = std::string(
reinterpret_cast<const char*>(errorCause->GetAdditionalInformation()),
errorCause->GetAdditionalInformationLength());
REQUIRE(additionalInfo == "error1");
REQUIRE(errorCause->GetAdditionalInformation()[6] == 0x00);
REQUIRE(errorCause->GetAdditionalInformation()[7] == 0x00);
auto* clonedErrorCause =
errorCause->Clone(sctpCommon::CloneBuffer, sizeof(sctpCommon::CloneBuffer));
std::memset(sctpCommon::SerializeBuffer, 0x00, sizeof(sctpCommon::SerializeBuffer));
delete errorCause;
CHECK_SCTP_ERROR_CAUSE(
clonedErrorCause,
sctpCommon::CloneBuffer,
sizeof(sctpCommon::CloneBuffer),
12,
RTC::SCTP::ErrorCause::ErrorCauseCode::PROTOCOL_VIOLATION,
false);
REQUIRE(clonedErrorCause->HasAdditionalInformation() == true);
REQUIRE(clonedErrorCause->GetAdditionalInformationLength() == 6);
REQUIRE(clonedErrorCause->GetAdditionalInformation()[0] == 0x65);
REQUIRE(clonedErrorCause->GetAdditionalInformation()[1] == 0x72);
REQUIRE(clonedErrorCause->GetAdditionalInformation()[2] == 0x72);
REQUIRE(clonedErrorCause->GetAdditionalInformation()[3] == 0x6F);
REQUIRE(clonedErrorCause->GetAdditionalInformation()[4] == 0x72);
REQUIRE(clonedErrorCause->GetAdditionalInformation()[5] == 0x31);
additionalInfo = std::string(
reinterpret_cast<const char*>(clonedErrorCause->GetAdditionalInformation()),
clonedErrorCause->GetAdditionalInformationLength());
REQUIRE(additionalInfo == "error1");
REQUIRE(clonedErrorCause->GetAdditionalInformation()[6] == 0x00);
REQUIRE(clonedErrorCause->GetAdditionalInformation()[7] == 0x00);
delete clonedErrorCause;
}
SECTION("ProtocolViolationErrorCause::Parse() fails")
{
alignas(4) uint8_t buffer1[] =
{
0x03, 0xE7, 0x00, 0x08,
0x12, 0x34, 0x56, 0x78,
};
REQUIRE(!RTC::SCTP::ProtocolViolationErrorCause::Parse(buffer1, sizeof(buffer1)));
alignas(4) uint8_t buffer2[] =
{
0x00, 0x0D, 0x00, 0x07,
0x12, 0x34, 0x56,
};
REQUIRE(!RTC::SCTP::ProtocolViolationErrorCause::Parse(buffer2, sizeof(buffer2)));
}
SECTION("ProtocolViolationErrorCause::Factory() succeeds")
{
auto* errorCause = RTC::SCTP::ProtocolViolationErrorCause::Factory(
sctpCommon::FactoryBuffer, sizeof(sctpCommon::FactoryBuffer));
CHECK_SCTP_ERROR_CAUSE(
errorCause,
sctpCommon::FactoryBuffer,
sizeof(sctpCommon::FactoryBuffer),
4,
RTC::SCTP::ErrorCause::ErrorCauseCode::PROTOCOL_VIOLATION,
false);
REQUIRE(errorCause->HasAdditionalInformation() == false);
REQUIRE(errorCause->GetAdditionalInformationLength() == 0);
errorCause->SetAdditionalInformation(sctpCommon::DataBuffer + 1000, 3000);
REQUIRE(errorCause->GetLength() == 3004);
REQUIRE(errorCause->HasAdditionalInformation() == true);
REQUIRE(errorCause->GetAdditionalInformationLength() == 3000);
errorCause->SetAdditionalInformation(nullptr, 0);
REQUIRE(errorCause->GetLength() == 4);
REQUIRE(errorCause->HasAdditionalInformation() == false);
REQUIRE(errorCause->GetAdditionalInformationLength() == 0);
errorCause->SetAdditionalInformation("iñaki");
CHECK_SCTP_ERROR_CAUSE(
errorCause,
sctpCommon::FactoryBuffer,
sizeof(sctpCommon::FactoryBuffer),
12,
RTC::SCTP::ErrorCause::ErrorCauseCode::PROTOCOL_VIOLATION,
false);
REQUIRE(errorCause->HasAdditionalInformation() == true);
REQUIRE(errorCause->GetAdditionalInformationLength() == 6);
std::string additionalInfo(
reinterpret_cast<const char*>(errorCause->GetAdditionalInformation()),
errorCause->GetAdditionalInformationLength());
REQUIRE(additionalInfo == "iñaki");
REQUIRE(errorCause->GetAdditionalInformation()[6] == 0x00);
REQUIRE(errorCause->GetAdditionalInformation()[7] == 0x00);
auto* parsedErrorCause = RTC::SCTP::ProtocolViolationErrorCause::Parse(
errorCause->GetBuffer(), errorCause->GetLength());
delete errorCause;
CHECK_SCTP_ERROR_CAUSE(
parsedErrorCause,
sctpCommon::FactoryBuffer,
12,
12,
RTC::SCTP::ErrorCause::ErrorCauseCode::PROTOCOL_VIOLATION,
false);
REQUIRE(parsedErrorCause->HasAdditionalInformation() == true);
REQUIRE(parsedErrorCause->GetAdditionalInformationLength() == 6);
additionalInfo = std::string(
reinterpret_cast<const char*>(parsedErrorCause->GetAdditionalInformation()),
parsedErrorCause->GetAdditionalInformationLength());
REQUIRE(additionalInfo == "iñaki");
REQUIRE(parsedErrorCause->GetAdditionalInformation()[6] == 0x00);
REQUIRE(parsedErrorCause->GetAdditionalInformation()[7] == 0x00);
delete parsedErrorCause;
}
}