Files
roro9stack/test/test_traffic/test_traffic.cpp
T
twislaandClaude Opus 5.5 70bb2a4137 S1 #11: bytes read and written, counted per network service
A Counted<> wrapper around the network clients adds what goes through
their buffer read and write to a per-service counter (IRC, Gemini, Debug
Console, Updates); the single-byte calls and print() end up there, so
each byte counts once. `net` prints the totals. For TLS it's the plain
text the service sees.

Checked on the device: a Gemini fetch counts 164,986 in (a 164,970-byte
page and its 16-byte header) and 42 out (the URL and CRLF).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
2026-10-06 01:10:55 +02:00

55 lines
1.9 KiB
C++

#include <unity.h>
#include "traffic.h"
using namespace roro::net;
void setUp() { resetTraffic(); }
void tearDown() {}
void test_counts_per_service() {
received(User::Irc, 100);
received(User::Irc, 20);
sent(User::Irc, 7);
sent(User::Gemini, 40);
TEST_ASSERT_EQUAL_UINT32(120, traffic(User::Irc).in);
TEST_ASSERT_EQUAL_UINT32(7, traffic(User::Irc).out);
TEST_ASSERT_EQUAL_UINT32(0, traffic(User::Gemini).in);
TEST_ASSERT_EQUAL_UINT32(40, traffic(User::Gemini).out);
TEST_ASSERT_EQUAL_UINT32(0, traffic(User::Updates).in);
}
void test_names() {
TEST_ASSERT_EQUAL_STRING("IRC", userName(User::Irc));
TEST_ASSERT_EQUAL_STRING("Gemini", userName(User::Gemini));
TEST_ASSERT_EQUAL_STRING("Debug Console", userName(User::DebugConsole));
TEST_ASSERT_EQUAL_STRING("Updates", userName(User::Updates));
}
// Bytes a second between two readings; the counter wraps at 4 GB like any uint32.
void test_rate() {
TEST_ASSERT_EQUAL_UINT32(1000, bytesPerSecond(5000, 6000, 1000));
TEST_ASSERT_EQUAL_UINT32(500, bytesPerSecond(0, 1000, 2000));
TEST_ASSERT_EQUAL_UINT32(0, bytesPerSecond(7, 7, 1000));
TEST_ASSERT_EQUAL_UINT32(20, bytesPerSecond(0xFFFFFFF6u, 10, 1000));
TEST_ASSERT_EQUAL_UINT32(0, bytesPerSecond(1, 2, 0));
}
void test_sizes_for_humans() {
TEST_ASSERT_EQUAL_STRING("0 B", formatTraffic(0).c_str());
TEST_ASSERT_EQUAL_STRING("999 B", formatTraffic(999).c_str());
TEST_ASSERT_EQUAL_STRING("1.5 KB", formatTraffic(1536).c_str());
TEST_ASSERT_EQUAL_STRING("12 KB", formatTraffic(12 * 1024 + 100).c_str());
TEST_ASSERT_EQUAL_STRING("1.7 MB", formatTraffic(1774688).c_str());
TEST_ASSERT_EQUAL_STRING("48 MB", formatTraffic(48u * 1024 * 1024).c_str());
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_counts_per_service);
RUN_TEST(test_names);
RUN_TEST(test_rate);
RUN_TEST(test_sizes_for_humans);
return UNITY_END();
}