diff options
| author | srdusr <[email protected]> | 2024-05-14 01:42:00 +0200 |
|---|---|---|
| committer | srdusr <[email protected]> | 2024-05-14 01:42:00 +0200 |
| commit | 08332a4195956611db80a2cfe3710d760cbd6acf (patch) | |
| tree | 0cb5cdf9fdcfdd8dc8c129a33575ad9b182d5c01 /tests/test_capture_queue.cpp | |
| download | packeteer-08332a4195956611db80a2cfe3710d760cbd6acf.tar.gz packeteer-08332a4195956611db80a2cfe3710d760cbd6acf.zip | |
Initial commit: wireframe packet capture/analysis tool
Terminal packet capture and analysis tool built to learn the C++
memory model (byte layout, alignment, endianness, std::span over
unowned buffers) via a real capture pipeline.
- Hand-rolled L2-L4 decoders (Ethernet, IPv4, IPv6 with extension
header walking, TCP, UDP) over std::span, no struct-casting
- L7 dissector interface with DNS, HTTP, and TLS SNI implementations
- pcapng read/write for Wireshark-compatible capture files
- Bounded capture queue: drop-on-backpressure for live capture,
blocking push for faithful file replay
- Kernel-level BPF filtering (-f) and a separate display-only search
(-g / interactive) that doesn't touch what's captured
- Replay mode (-r) reads a saved pcapng file back through the same
pipeline as live capture, no root or live device needed
- pcap_stats() surfaces kernel/interface drops invisible to the
capture queue's own counter
- Three frontends sharing one CaptureSession setup path: CLI, TUI
(FTXUI, primary), GUI (Dear ImGui + SDL3, secondary)
- 89 unit tests (doctest) plus 9 libFuzzer harnesses covering every
hand-rolled parser; fuzzing found and fixed a real OOM in the
pcapng reader (unbounded allocation from an untrusted length field)
Diffstat (limited to 'tests/test_capture_queue.cpp')
| -rw-r--r-- | tests/test_capture_queue.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/tests/test_capture_queue.cpp b/tests/test_capture_queue.cpp new file mode 100644 index 0000000..da2da28 --- /dev/null +++ b/tests/test_capture_queue.cpp @@ -0,0 +1,122 @@ +#include <doctest/doctest.h> + +#include <atomic> +#include <chrono> +#include <thread> + +#include "wireframe/capture_queue.hpp" + +using namespace wireframe; + +TEST_CASE("try_push/pop returns packets in FIFO order") { + CaptureQueue queue(4); + for (std::uint32_t i = 0; i < 3; ++i) { + CapturedPacket p; + p.ts_sec = i; + p.data = {static_cast<unsigned char>(i)}; + CHECK(queue.try_push(std::move(p))); + } + for (std::uint32_t i = 0; i < 3; ++i) { + auto p = queue.pop(); + REQUIRE(p.has_value()); + CHECK(p->ts_sec == i); + } + CHECK(queue.dropped() == 0); +} + +TEST_CASE("try_push drops and counts once the queue is full") { + CaptureQueue queue(2); + CapturedPacket a, b, c; + CHECK(queue.try_push(std::move(a))); + CHECK(queue.try_push(std::move(b))); + CHECK_FALSE(queue.try_push(std::move(c))); // full: dropped, not blocked + CHECK(queue.dropped() == 1); +} + +TEST_CASE("stop() drains items already queued before pop() returns nullopt") { + CaptureQueue queue(4); + CapturedPacket a, b; + queue.try_push(std::move(a)); + queue.try_push(std::move(b)); + queue.stop(); + + CHECK(queue.pop().has_value()); + CHECK(queue.pop().has_value()); + CHECK_FALSE(queue.pop().has_value()); // drained and stopped +} + +TEST_CASE("pop() blocks until a packet is pushed") { + CaptureQueue queue(4); + std::thread producer([&] { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + CapturedPacket p; + p.data = {0x42}; + queue.try_push(std::move(p)); + }); + + auto p = queue.pop(); + REQUIRE(p.has_value()); + CHECK(p->data[0] == 0x42); + producer.join(); +} + +TEST_CASE("push() succeeds immediately when there's room") { + CaptureQueue queue(4); + CapturedPacket p; + p.data = {0x01}; + CHECK(queue.push(std::move(p))); + CHECK(queue.dropped() == 0); +} + +TEST_CASE("push() blocks for room instead of dropping, unlike try_push()") { + CaptureQueue queue(1); + CapturedPacket a; + a.data = {0xAA}; + CHECK(queue.try_push(std::move(a))); // fills the only slot + + std::atomic<bool> pushed{false}; + std::thread producer([&] { + CapturedPacket b; + b.data = {0xBB}; + CHECK(queue.push(std::move(b))); // must block until the pop() below frees room + pushed.store(true); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + CHECK_FALSE(pushed.load()); // still blocked: queue was full this whole time + + auto first = queue.pop(); // frees a slot + REQUIRE(first.has_value()); + CHECK(first->data[0] == 0xAA); + + producer.join(); + CHECK(pushed.load()); + CHECK(queue.dropped() == 0); // never dropped - it waited instead + + auto second = queue.pop(); + REQUIRE(second.has_value()); + CHECK(second->data[0] == 0xBB); +} + +TEST_CASE("push() returns false without pushing if stop() is called while it's waiting") { + CaptureQueue queue(1); + CapturedPacket a; + a.data = {0xAA}; + queue.try_push(std::move(a)); // fill the only slot + + std::atomic<bool> result_ready{false}; + std::atomic<bool> push_result{true}; + std::thread producer([&] { + CapturedPacket b; + b.data = {0xBB}; + push_result.store(queue.push(std::move(b))); + result_ready.store(true); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + queue.stop(); + producer.join(); + + CHECK(result_ready.load()); + CHECK_FALSE(push_result.load()); +} |