srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_igmp.cpp
blob: b87d88d9d00433120825ce69541d3278c71c4dea (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
#include <doctest/doctest.h>

#include <array>
#include <vector>

#include "packeteer/net/igmp.hpp"

using namespace packeteer::net;

TEST_CASE("parse_igmp decodes a general membership query with an all-zero group") {
    std::vector<unsigned char> bytes = {0x11, 0x64, 0x00, 0x00, 0, 0, 0, 0};
    auto igmp = parse_igmp(bytes);
    REQUIRE(igmp.has_value());
    CHECK(igmp->type == kIgmpMembershipQuery);
    REQUIRE(igmp->group.has_value());
    CHECK(igmp->group->bytes == std::array<unsigned char, 4>{0, 0, 0, 0});
}

TEST_CASE("parse_igmp decodes a v2 membership report with a real group address") {
    std::vector<unsigned char> bytes = {0x16, 0x00, 0x00, 0x00, 239, 255, 255, 250};
    auto igmp = parse_igmp(bytes);
    REQUIRE(igmp.has_value());
    CHECK(igmp->type == kIgmpV2MembershipReport);
    REQUIRE(igmp->group.has_value());
    CHECK(igmp->group->bytes == std::array<unsigned char, 4>{239, 255, 255, 250});
}

TEST_CASE("parse_igmp rejects a payload shorter than the fixed header") {
    std::vector<unsigned char> bytes(4, 0);
    CHECK_FALSE(parse_igmp(bytes).has_value());
}

TEST_CASE("parse_igmp leaves group unset for a v3 report, whose bytes[4:8] aren't an address") {
    // Real capture from this exact bug: bytes[4:8] here are Reserved
    // (2 bytes, zero) + Number of Group Records (2 bytes, = 1) --
    // reading that as an IPv4 address produced the nonsense
    // "group=0.0.0.1" before this was fixed. IGMPv3's actual group
    // address(es) live inside the group records that follow, which
    // this dissector doesn't decode (see the header comment).
    std::vector<unsigned char> bytes = {0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
    auto igmp = parse_igmp(bytes);
    REQUIRE(igmp.has_value());
    CHECK(igmp->type == kIgmpV3MembershipReport);
    CHECK_FALSE(igmp->group.has_value());
}

TEST_CASE("igmp_type_name names known types and falls back to hex for unknown ones") {
    CHECK(igmp_type_name(kIgmpMembershipQuery) == "Membership Query");
    CHECK(igmp_type_name(kIgmpLeaveGroup) == "Leave Group");
    CHECK(igmp_type_name(0x99) == "type=0x99");
}