#include "common.hpp"
#include "RTC/SCTP/packet/Chunk.hpp"
#include "RTC/SCTP/packet/chunks/UnknownChunk.hpp"
#include "test/include/RTC/SCTP/sctpCommon.hpp"
#include <catch2/catch_test_macros.hpp>
#include <cstring> // std::memset()

SCENARIO("SCTP Unknown Chunk", "[serializable][sctp][chunk]")
{
	sctpCommon::ResetBuffers();

	SECTION("UnknownChunk::Parse() succeeds")
	{
		// clang-format off
		alignas(4) uint8_t buffer[] =
		{
			// Type:0xEE (UNKNOWN), Flags: 0b1100, Length: 7
			0xEE, 0b10001100, 0x00, 0x07,
			// Unknown value: 0xAABBCC, 1 byte of padding
			0xAA, 0xBB, 0xCC, 0x00,
			// Extra bytes that should be ignored
			0xAA, 0xBB, 0xCC
		};
		// clang-format on

		auto* chunk = RTC::SCTP::UnknownChunk::Parse(buffer, sizeof(buffer));

		// NOTE: Chunk type is 0xEE (0b11101110) so first 2 bits are 11, meaning
		// that the action to take if we receive this chunk type is SKIP_AND_REPORT.

		CHECK_SCTP_CHUNK(
		  /*chunk*/ chunk,
		  /*buffer*/ buffer,
		  /*bufferLength*/ sizeof(buffer),
		  /*length*/ 8,
		  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
		  /*chunkType*/ static_cast<RTC::SCTP::Chunk::ChunkType>(0xEE),
		  /*unknownType*/ true,
		  /*actionForUnknownChunkType*/ RTC::SCTP::Chunk::ActionForUnknownChunkType::SKIP_AND_REPORT,
		  /*flags*/ 0b10001100,
		  /*canHaveParameters*/ false,
		  /*parametersCount*/ 0,
		  /*canHaveErrorCauses*/ false,
		  /*errorCausesCount*/ 0);

		REQUIRE(chunk->HasUnknownValue() == true);
		REQUIRE(chunk->GetUnknownValueLength() == 3);
		REQUIRE(chunk->GetUnknownValue()[0] == 0xAA);
		REQUIRE(chunk->GetUnknownValue()[1] == 0xBB);
		REQUIRE(chunk->GetUnknownValue()[2] == 0xCC);

		/* Serialize it. */

		chunk->Serialize(sctpCommon::SerializeBuffer, sizeof(sctpCommon::SerializeBuffer));

		std::memset(buffer, 0x00, sizeof(buffer));

		CHECK_SCTP_CHUNK(
		  /*chunk*/ chunk,
		  /*buffer*/ sctpCommon::SerializeBuffer,
		  /*bufferLength*/ sizeof(sctpCommon::SerializeBuffer),
		  /*length*/ 8,
		  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
		  /*chunkType*/ static_cast<RTC::SCTP::Chunk::ChunkType>(0xEE),
		  /*unknownType*/ true,
		  /*actionForUnknownChunkType*/ RTC::SCTP::Chunk::ActionForUnknownChunkType::SKIP_AND_REPORT,
		  /*flags*/ 0b10001100,
		  /*canHaveParameters*/ false,
		  /*parametersCount*/ 0,
		  /*canHaveErrorCauses*/ false,
		  /*errorCausesCount*/ 0);

		REQUIRE(chunk->HasUnknownValue() == true);
		REQUIRE(chunk->GetUnknownValueLength() == 3);
		REQUIRE(chunk->GetUnknownValue()[0] == 0xAA);
		REQUIRE(chunk->GetUnknownValue()[1] == 0xBB);
		REQUIRE(chunk->GetUnknownValue()[2] == 0xCC);

		/* Clone it. */

		auto* clonedChunk = chunk->Clone(sctpCommon::CloneBuffer, sizeof(sctpCommon::CloneBuffer));

		std::memset(sctpCommon::SerializeBuffer, 0x00, sizeof(sctpCommon::SerializeBuffer));

		delete chunk;

		CHECK_SCTP_CHUNK(
		  /*chunk*/ clonedChunk,
		  /*buffer*/ sctpCommon::CloneBuffer,
		  /*bufferLength*/ sizeof(sctpCommon::CloneBuffer),
		  /*length*/ 8,
		  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
		  /*chunkType*/ static_cast<RTC::SCTP::Chunk::ChunkType>(0xEE),
		  /*unknownType*/ true,
		  /*actionForUnknownChunkType*/ RTC::SCTP::Chunk::ActionForUnknownChunkType::SKIP_AND_REPORT,
		  /*flags*/ 0b10001100,
		  /*canHaveParameters*/ false,
		  /*parametersCount*/ 0,
		  /*canHaveErrorCauses*/ false,
		  /*errorCausesCount*/ 0);

		REQUIRE(clonedChunk->HasUnknownValue() == true);
		REQUIRE(clonedChunk->GetUnknownValueLength() == 3);
		REQUIRE(clonedChunk->GetUnknownValue()[0] == 0xAA);
		REQUIRE(clonedChunk->GetUnknownValue()[1] == 0xBB);
		REQUIRE(clonedChunk->GetUnknownValue()[2] == 0xCC);

		delete clonedChunk;
	}
}
