diff options
Diffstat (limited to 'tests/test_mdns.cpp')
| -rw-r--r-- | tests/test_mdns.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/test_mdns.cpp b/tests/test_mdns.cpp new file mode 100644 index 0000000..cad77e7 --- /dev/null +++ b/tests/test_mdns.cpp @@ -0,0 +1,60 @@ +#include <doctest/doctest.h> + +#include <vector> + +#include "wireframe/l7/mdns.hpp" + +using namespace wireframe::net; + +namespace { + +// A typical mDNS query for a ".local" hostname, id=0 per RFC 6762 +// 18.1's convention for multicast queries. +std::vector<unsigned char> mdns_query() { + return { + 0x00, 0x00, // id = 0 + 0x00, 0x00, // flags: query + 0x00, 0x01, // qdcount = 1 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 6, 'm', 'y', 'h', 'o', 's', 't', 5, 'l', 'o', 'c', 'a', 'l', 0, + 0x00, 0x01, // qtype = A + 0x00, 0x01, // qclass = IN + }; +} + +} // namespace + +TEST_CASE("MdnsDissector claims port 5353") { + MdnsDissector dissector; + CHECK(dissector.port() == kMdnsPort); +} + +TEST_CASE("MdnsDissector summarizes a query without an id= field") { + MdnsDissector dissector; + auto summary = dissector.summarize(mdns_query()); + REQUIRE(summary.has_value()); + CHECK(summary->substr(0, 10) == "mDNS query"); + CHECK(summary->find("myhost.local") != std::string::npos); + CHECK(summary->find("id=") == std::string::npos); +} + +TEST_CASE("MdnsDissector summarizes a response with ancount") { + MdnsDissector dissector; + std::vector<unsigned char> bytes = { + 0x00, 0x00, + 0x84, 0x00, // flags: QR=1 (response), AA=1 + 0x00, 0x00, // qdcount = 0 (typical for an mDNS response) + 0x00, 0x01, // ancount = 1 + 0x00, 0x00, 0x00, 0x00, + }; + auto summary = dissector.summarize(bytes); + REQUIRE(summary.has_value()); + CHECK(summary->substr(0, 13) == "mDNS response"); + CHECK(summary->find("ancount=1") != std::string::npos); +} + +TEST_CASE("MdnsDissector::summarize returns nullopt for a truncated payload") { + MdnsDissector dissector; + std::vector<unsigned char> bytes(5, 0); + CHECK_FALSE(dissector.summarize(bytes).has_value()); +} |