srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_rtcp.cpp
blob: 9dde6cc5ea6d871f48d4ac6a3dc68ab51838de0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <doctest/doctest.h>

#include <vector>

#include "packeteer/net/rtcp.hpp"

using namespace packeteer::net;

namespace {

std::vector<unsigned char> rtcp_packet(std::uint8_t packet_type, std::uint16_t length_words,
                                         std::size_t total_bytes) {
    std::vector<unsigned char> bytes(total_bytes, 0);
    bytes[0] = 0x80;  // version 2, no padding, RC/SC = 0
    bytes[1] = packet_type;
    bytes[2] = static_cast<unsigned char>(length_words >> 8);
    bytes[3] = static_cast<unsigned char>(length_words & 0xFF);
    return bytes;
}

}  // namespace

TEST_CASE("parse_rtcp_heuristic decodes a Sender Report") {
    auto bytes = rtcp_packet(kRtcpSenderReport, 5, 24);  // (5+1)*4 = 24 bytes
    auto rtcp = parse_rtcp_heuristic(bytes);
    REQUIRE(rtcp.has_value());
    CHECK(rtcp->version == 2);
    CHECK(rtcp->packet_type == kRtcpSenderReport);
    CHECK(rtcp->length_words == 5);
}

TEST_CASE("parse_rtcp_heuristic accepts the first packet of a longer compound datagram") {
    auto bytes = rtcp_packet(kRtcpReceiverReport, 1, 8);  // declares 8 bytes
    bytes.resize(40, 0);  // but the datagram continues with more RTCP packets after it
    auto rtcp = parse_rtcp_heuristic(bytes);
    REQUIRE(rtcp.has_value());
    CHECK(rtcp->packet_type == kRtcpReceiverReport);
}

TEST_CASE("parse_rtcp_heuristic rejects a packet type outside 200-204") {
    auto bytes = rtcp_packet(199, 1, 8);
    CHECK_FALSE(parse_rtcp_heuristic(bytes).has_value());

    auto bytes2 = rtcp_packet(205, 1, 8);
    CHECK_FALSE(parse_rtcp_heuristic(bytes2).has_value());
}

TEST_CASE("parse_rtcp_heuristic rejects version other than 2") {
    std::vector<unsigned char> bytes = {0x00, kRtcpBye, 0x00, 0x00};
    CHECK_FALSE(parse_rtcp_heuristic(bytes).has_value());
}

TEST_CASE("parse_rtcp_heuristic rejects a buffer shorter than the declared length") {
    auto bytes = rtcp_packet(kRtcpBye, 10, 8);  // declares (10+1)*4=44 bytes, buffer is only 8
    CHECK_FALSE(parse_rtcp_heuristic(bytes).has_value());
}

TEST_CASE("parse_rtcp_heuristic rejects a buffer shorter than the fixed header") {
    std::vector<unsigned char> bytes = {0x80, kRtcpBye};
    CHECK_FALSE(parse_rtcp_heuristic(bytes).has_value());
}

TEST_CASE("rtcp_packet_type_name names every known type") {
    CHECK(rtcp_packet_type_name(kRtcpSenderReport) == "SR");
    CHECK(rtcp_packet_type_name(kRtcpReceiverReport) == "RR");
    CHECK(rtcp_packet_type_name(kRtcpSourceDescription) == "SDES");
    CHECK(rtcp_packet_type_name(kRtcpBye) == "BYE");
    CHECK(rtcp_packet_type_name(kRtcpApp) == "APP");
}