MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::tests Namespace Reference

Namespaces

namespace  detail

Classes

class  mxlDomainFixture
struct  UdpDatagram

Functions

std::filesystem::path getDomainPath ()
std::filesystem::path makeTempDomain ()
std::string readFile (std::filesystem::path const &filepath)
std::vector< UdpDatagram > readUdpDatagrams (std::filesystem::path const &path)

Function Documentation

◆ getDomainPath()

std::filesystem::path mxl::tests::getDomainPath ( )
inline

Simple utility function to get the domain path for the tests. This will return a path to /dev/shm/mxl_domain on Linux and a path in the user's home directory ($HOME/mxl_domain) on macOS.

Returns
The path to the mxl domain directory. The directory may not exist yet.

Definition at line 90 of file Utils.hpp.

91 {
92#ifdef __linux__
93 return std::filesystem::path{"/dev/shm/mxl_domain"};
94#elif __APPLE__
95 if (auto const home = std::getenv("HOME"); home != nullptr)
96 {
97 return std::filesystem::path{home} / "mxl_domain";
98 }
99 else
100 {
101 throw std::runtime_error{"Environment variable HOME is not set."};
102 }
103#else
104# error "Unsupported platform. This is only implemented for Linux and macOS."
105#endif
106 }

◆ makeTempDomain()

std::filesystem::path mxl::tests::makeTempDomain ( )
inline

Definition at line 108 of file Utils.hpp.

109 {
110#ifdef __linux__
111 char tmpl[] = "/dev/shm/mxl_test_domainXXXXXX";
112#elif __APPLE__
113 char tmpl[] = "/tmp/mxl_test_domainXXXXXX";
114#else
115# error "Unsupported platform. This is only implemented for Linux and macOS."
116#endif
117 if (::mkdtemp(tmpl) == nullptr)
118 {
119 throw std::runtime_error("Failed to create temporary directory");
120 }
121 return std::filesystem::path{tmpl};
122 }

◆ readFile()

std::string mxl::tests::readFile ( std::filesystem::path const & filepath)
inline

Definition at line 78 of file Utils.hpp.

79 {
80 if (auto file = std::ifstream{filepath, std::ios::in | std::ios::binary}; file)
81 {
82 auto reader = std::stringstream{};
83 reader << file.rdbuf();
84 return reader.str();
85 }
86
87 throw std::runtime_error("Failed to open file: " + filepath.string());
88 }
mxlFlowReader reader

◆ readUdpDatagrams()

std::vector< UdpDatagram > mxl::tests::readUdpDatagrams ( std::filesystem::path const & path)
inline

Definition at line 47 of file PcapReader.hpp.

48 {
49 constexpr auto globalHeaderSize = std::size_t{24};
50 constexpr auto recordHeaderSize = std::size_t{16};
51 constexpr auto ethernetHeaderSize = std::size_t{14};
52 constexpr auto vlanTagSize = std::size_t{4};
53 constexpr auto udpHeaderSize = std::size_t{8};
54 constexpr auto linkTypeEthernet = std::uint32_t{1};
55 constexpr auto etherTypeVlan = std::uint16_t{0x8100};
56 constexpr auto etherTypeIpv4 = std::uint16_t{0x0800};
57 constexpr auto ipProtocolUdp = std::uint8_t{17};
58
59 auto const contents = readFile(path);
60 auto const file = std::span{reinterpret_cast<std::uint8_t const*>(contents.data()), contents.size()};
61 auto const fail = [&path](char const* reason)
62 {
63 throw std::runtime_error{"Invalid pcap file " + path.string() + ": " + reason};
64 };
65
66 if (file.size() < globalHeaderSize)
67 {
68 fail("missing global header");
69 }
70 auto const magic = detail::readPcap32(file, 0, true);
71 auto const bigEndian = (magic == 0xA1B2C3D4U) || (magic == 0xA1B23C4DU);
72 // timestamps are ignored
73 if (!bigEndian && (magic != 0xD4C3B2A1U) && (magic != 0x4D3CB2A1U))
74 {
75 fail("not a classic pcap file");
76 }
77 if (detail::readPcap32(file, 20, bigEndian) != linkTypeEthernet)
78 {
79 fail("link type is not Ethernet");
80 }
81
82 auto datagrams = std::vector<UdpDatagram>{};
83 auto offset = globalHeaderSize;
84 while (offset < file.size())
85 {
86 if (file.size() - offset < recordHeaderSize)
87 {
88 fail("truncated record header");
89 }
90 auto const capturedLength = detail::readPcap32(file, offset + 8, bigEndian);
91 auto const originalLength = detail::readPcap32(file, offset + 12, bigEndian);
92 offset += recordHeaderSize;
93 if ((file.size() - offset < capturedLength) || (capturedLength != originalLength))
94 {
95 fail("truncated packet");
96 }
97 auto const frame = file.subspan(offset, capturedLength);
98 offset += capturedLength;
99
100 if (frame.size() < ethernetHeaderSize)
101 {
102 continue;
103 }
104 auto ipOffset = ethernetHeaderSize;
105 auto etherType = detail::readBigEndian16(frame, 12);
106 if ((etherType == etherTypeVlan) && (frame.size() >= ethernetHeaderSize + vlanTagSize))
107 {
108 etherType = detail::readBigEndian16(frame, 16);
109 ipOffset += vlanTagSize;
110 }
111 if ((etherType != etherTypeIpv4) || (frame.size() < ipOffset + 20))
112 {
113 continue;
114 }
115
116 auto const ip = frame.subspan(ipOffset);
117 auto const ipHeaderSize = std::size_t{ip[0] & 0x0FU} * 4U;
118 auto const ipTotalLength = std::size_t{detail::readBigEndian16(ip, 2)};
119 // skip IP fragments only the first one has a UDP header and tests do not need reassembly
120 auto const fragmented = (detail::readBigEndian16(ip, 6) & 0x3FFFU) != 0;
121 if (((ip[0] >> 4U) != 4) || (ip[9] != ipProtocolUdp) || fragmented || (ipHeaderSize < 20) || (ipTotalLength > ip.size()) ||
122 (ipTotalLength < ipHeaderSize + udpHeaderSize))
123 {
124 continue;
125 }
126
127 auto const udp = ip.subspan(ipHeaderSize, ipTotalLength - ipHeaderSize);
128 auto const udpLength = std::size_t{detail::readBigEndian16(udp, 4)};
129 if ((udpLength < udpHeaderSize) || (udpLength > udp.size()))
130 {
131 fail("invalid UDP length");
132 }
133 auto const payload = udp.subspan(udpHeaderSize, udpLength - udpHeaderSize);
134 datagrams.push_back(UdpDatagram{
135 .dstPort = detail::readBigEndian16(udp, 2),
136 .payload = {payload.begin(), payload.end()},
137 });
138 }
139 return datagrams;
140 }
std::uint16_t readBigEndian16(std::span< std::uint8_t const > bytes, std::size_t offset)
std::uint32_t readPcap32(std::span< std::uint8_t const > bytes, std::size_t offset, bool bigEndian)
std::string readFile(std::filesystem::path const &filepath)
Definition Utils.hpp:78