Skip to content

Commit 9e5693d

Browse files
committed
chore: add to_string functions for netprof enums
Also fixed u64 formatting in the netprof auto test
1 parent 52d915e commit 9e5693d

File tree

5 files changed

+141
-16
lines changed

5 files changed

+141
-16
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ set(toxcore_SOURCES
307307
toxcore/mem.h
308308
toxcore/mono_time.c
309309
toxcore/mono_time.h
310-
toxcore/net_profile.c
311-
toxcore/net_profile.h
312310
toxcore/net_crypto.c
313311
toxcore/net_crypto.h
312+
toxcore/net_profile.c
313+
toxcore/net_profile.h
314314
toxcore/network.c
315315
toxcore/network.h
316316
toxcore/onion_announce.c

auto_tests/netprof_test.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* TODO(JFreegman): test TCP packets as well
33
*/
44

5+
#include <inttypes.h>
56
#include <stdint.h>
67
#include <stdio.h>
78

@@ -32,22 +33,22 @@ static void test_netprof(AutoTox *autotoxes)
3233
const Tox *tox1 = autotoxes[0].tox;
3334

3435
const uint64_t UDP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
35-
TOX_NETPROF_DIRECTION_SENT);
36+
TOX_NETPROF_DIRECTION_SENT);
3637
const uint64_t UDP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
37-
TOX_NETPROF_DIRECTION_RECV);
38+
TOX_NETPROF_DIRECTION_RECV);
3839
const uint64_t TCP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
39-
TOX_NETPROF_DIRECTION_SENT);
40+
TOX_NETPROF_DIRECTION_SENT);
4041
const uint64_t TCP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
41-
TOX_NETPROF_DIRECTION_RECV);
42+
TOX_NETPROF_DIRECTION_RECV);
4243

4344
const uint64_t UDP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
44-
TOX_NETPROF_DIRECTION_SENT);
45+
TOX_NETPROF_DIRECTION_SENT);
4546
const uint64_t UDP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
46-
TOX_NETPROF_DIRECTION_RECV);
47+
TOX_NETPROF_DIRECTION_RECV);
4748
const uint64_t TCP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
48-
TOX_NETPROF_DIRECTION_SENT);
49+
TOX_NETPROF_DIRECTION_SENT);
4950
const uint64_t TCP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
50-
TOX_NETPROF_DIRECTION_RECV);
51+
TOX_NETPROF_DIRECTION_RECV);
5152

5253
ck_assert(UDP_count_recv1 > 0 && UDP_count_sent1 > 0);
5354
ck_assert(UDP_bytes_recv1 > 0 && UDP_bytes_sent1 > 0);
@@ -91,18 +92,18 @@ static void test_netprof(AutoTox *autotoxes)
9192

9293
const uint64_t total_packets = total_sent_count + total_recv_count;
9394
ck_assert_msg(total_packets == UDP_count_sent1 + UDP_count_recv1,
94-
"%lu does not match %lu\n", total_packets, UDP_count_sent1 + UDP_count_recv1);
95+
"%" PRIu64 "does not match %" PRIu64 "\n", total_packets, UDP_count_sent1 + UDP_count_recv1);
9596

96-
ck_assert_msg(total_sent_count == UDP_count_sent1, "%lu does not match %lu\n", total_sent_count, UDP_count_sent1);
97-
ck_assert_msg(total_recv_count == UDP_count_recv1, "%lu does not match %lu\n", total_recv_count, UDP_count_recv1);
97+
ck_assert_msg(total_sent_count == UDP_count_sent1, "%" PRIu64 " does not match %" PRIu64 "\n", total_sent_count, UDP_count_sent1);
98+
ck_assert_msg(total_recv_count == UDP_count_recv1, "%" PRIu64 " does not match %" PRIu64"\n", total_recv_count, UDP_count_recv1);
9899

99100

100101
const uint64_t total_bytes = total_sent_bytes + total_recv_bytes;
101102
ck_assert_msg(total_bytes == UDP_bytes_sent1 + UDP_bytes_recv1,
102-
"%lu does not match %lu\n", total_bytes, UDP_bytes_sent1 + UDP_bytes_recv1);
103+
"%" PRIu64 "does not match %" PRIu64 "\n", total_bytes, UDP_bytes_sent1 + UDP_bytes_recv1);
103104

104-
ck_assert_msg(total_sent_bytes == UDP_bytes_sent1, "%lu does not match %lu\n", total_sent_bytes, UDP_bytes_sent1);
105-
ck_assert_msg(total_recv_bytes == UDP_bytes_recv1, "%lu does not match %lu\n", total_recv_bytes, UDP_bytes_recv1);
105+
ck_assert_msg(total_sent_bytes == UDP_bytes_sent1, "%" PRIu64 " does not match %" PRIu64 "\n", total_sent_bytes, UDP_bytes_sent1);
106+
ck_assert_msg(total_recv_bytes == UDP_bytes_recv1, "%" PRIu64 " does not match %" PRIu64 "\n", total_recv_bytes, UDP_bytes_recv1);
106107
}
107108

108109
int main(void)

toxcore/net_profile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/* The max number of packet ID's (must fit inside one byte) */
1919
#define NET_PROF_MAX_PACKET_IDS 256
2020

21+
/* If passed to a netprof function as a nullptr the function will have no effect. */
2122
typedef struct Net_Profile Net_Profile;
2223

2324
/** Specifies whether the query is for sent or received packets. */

toxcore/tox_api.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,3 +1722,120 @@ const char *tox_group_mod_event_to_string(Tox_Group_Mod_Event value)
17221722

17231723
return "<invalid Tox_Group_Mod_Event>";
17241724
}
1725+
const char *tox_netprof_packet_id_to_string(Tox_Netprof_Packet_Id value)
1726+
{
1727+
switch (value) {
1728+
case TOX_NETPROF_PACKET_ID_ZERO:
1729+
return "TOX_NETPROF_PACKET_ID_ZERO";
1730+
case TOX_NETPROF_PACKET_ID_ONE:
1731+
return "TOX_NETPROF_PACKET_ID_ONE";
1732+
case TOX_NETPROF_PACKET_ID_TWO:
1733+
return "TOX_NETPROF_PACKET_ID_TWO";
1734+
case TOX_NETPROF_PACKET_ID_TCP_DISCONNECT:
1735+
return "TOX_NETPROF_PACKET_ID_TCP_DISCONNECT";
1736+
case TOX_NETPROF_PACKET_ID_FOUR:
1737+
return "TOX_NETPROF_PACKET_ID_FOUR";
1738+
case TOX_NETPROF_PACKET_ID_TCP_PONG:
1739+
return "TOX_NETPROF_PACKET_ID_TCP_PONG";
1740+
case TOX_NETPROF_PACKET_ID_TCP_OOB_SEND:
1741+
return "TOX_NETPROF_PACKET_ID_TCP_OOB_SEND";
1742+
case TOX_NETPROF_PACKET_ID_TCP_OOB_RECV:
1743+
return "TOX_NETPROF_PACKET_ID_TCP_OOB_RECV";
1744+
case TOX_NETPROF_PACKET_ID_TCP_ONION_REQUEST:
1745+
return "TOX_NETPROF_PACKET_ID_TCP_ONION_REQUEST";
1746+
case TOX_NETPROF_PACKET_ID_TCP_ONION_RESPONSE:
1747+
return "TOX_NETPROF_PACKET_ID_TCP_ONION_RESPONSE";
1748+
case TOX_NETPROF_PACKET_ID_TCP_DATA:
1749+
return "TOX_NETPROF_PACKET_ID_TCP_DATA";
1750+
case TOX_NETPROF_PACKET_ID_COOKIE_REQUEST:
1751+
return "TOX_NETPROF_PACKET_ID_COOKIE_REQUEST";
1752+
case TOX_NETPROF_PACKET_ID_COOKIE_RESPONSE:
1753+
return "TOX_NETPROF_PACKET_ID_COOKIE_RESPONSE";
1754+
case TOX_NETPROF_PACKET_ID_CRYPTO_HS:
1755+
return "TOX_NETPROF_PACKET_ID_CRYPTO_HS";
1756+
case TOX_NETPROF_PACKET_ID_CRYPTO_DATA:
1757+
return "TOX_NETPROF_PACKET_ID_CRYPTO_DATA";
1758+
case TOX_NETPROF_PACKET_ID_CRYPTO:
1759+
return "TOX_NETPROF_PACKET_ID_CRYPTO";
1760+
case TOX_NETPROF_PACKET_ID_LAN_DISCOVERY:
1761+
return "TOX_NETPROF_PACKET_ID_LAN_DISCOVERY";
1762+
case TOX_NETPROF_PACKET_ID_GC_HANDSHAKE:
1763+
return "TOX_NETPROF_PACKET_ID_GC_HANDSHAKE";
1764+
case TOX_NETPROF_PACKET_ID_GC_LOSSLESS:
1765+
return "TOX_NETPROF_PACKET_ID_GC_LOSSLESS";
1766+
case TOX_NETPROF_PACKET_ID_GC_LOSSY:
1767+
return "TOX_NETPROF_PACKET_ID_GC_LOSSY";
1768+
case TOX_NETPROF_PACKET_ID_ONION_SEND_INITIAL:
1769+
return "TOX_NETPROF_PACKET_ID_ONION_SEND_INITIAL";
1770+
case TOX_NETPROF_PACKET_ID_ONION_SEND_1:
1771+
return "TOX_NETPROF_PACKET_ID_ONION_SEND_1";
1772+
case TOX_NETPROF_PACKET_ID_ONION_SEND_2:
1773+
return "TOX_NETPROF_PACKET_ID_ONION_SEND_2";
1774+
case TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST_OLD:
1775+
return "TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST_OLD";
1776+
case TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE_OLD:
1777+
return "TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE_OLD";
1778+
case TOX_NETPROF_PACKET_ID_ONION_DATA_REQUEST:
1779+
return "TOX_NETPROF_PACKET_ID_ONION_DATA_REQUEST";
1780+
case TOX_NETPROF_PACKET_ID_ONION_DATA_RESPONSE:
1781+
return "TOX_NETPROF_PACKET_ID_ONION_DATA_RESPONSE";
1782+
case TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST:
1783+
return "TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST";
1784+
case TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE:
1785+
return "TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE";
1786+
case TOX_NETPROF_PACKET_ID_ONION_RECV_3:
1787+
return "TOX_NETPROF_PACKET_ID_ONION_RECV_3";
1788+
case TOX_NETPROF_PACKET_ID_ONION_RECV_2:
1789+
return "TOX_NETPROF_PACKET_ID_ONION_RECV_2";
1790+
case TOX_NETPROF_PACKET_ID_ONION_RECV_1:
1791+
return "TOX_NETPROF_PACKET_ID_ONION_RECV_1";
1792+
case TOX_NETPROF_PACKET_ID_FORWARD_REQUEST:
1793+
return "TOX_NETPROF_PACKET_ID_FORWARD_REQUEST";
1794+
case TOX_NETPROF_PACKET_ID_FORWARDING:
1795+
return "TOX_NETPROF_PACKET_ID_FORWARDING";
1796+
case TOX_NETPROF_PACKET_ID_FORWARD_REPLY:
1797+
return "TOX_NETPROF_PACKET_ID_FORWARD_REPLY";
1798+
case TOX_NETPROF_PACKET_ID_DATA_SEARCH_REQUEST:
1799+
return "TOX_NETPROF_PACKET_ID_DATA_SEARCH_REQUEST";
1800+
case TOX_NETPROF_PACKET_ID_DATA_SEARCH_RESPONSE:
1801+
return "TOX_NETPROF_PACKET_ID_DATA_SEARCH_RESPONSE";
1802+
case TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_REQUEST:
1803+
return "TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_REQUEST";
1804+
case TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_RESPONSE:
1805+
return "TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_RESPONSE";
1806+
case TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_REQUEST:
1807+
return "TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_REQUEST";
1808+
case TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_RESPONSE:
1809+
return "TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_RESPONSE";
1810+
case TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO:
1811+
return "TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO";
1812+
}
1813+
1814+
return "<invalid Tox_Netprof_Packet_Id>";
1815+
}
1816+
const char *tox_netprof_packet_type_to_string(Tox_Netprof_Packet_Type value)
1817+
{
1818+
switch (value) {
1819+
case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT:
1820+
return "TOX_NETPROF_PACKET_TYPE_TCP_CLIENT";
1821+
case TOX_NETPROF_PACKET_TYPE_TCP_SERVER:
1822+
return "TOX_NETPROF_PACKET_TYPE_TCP_SERVER";
1823+
case TOX_NETPROF_PACKET_TYPE_TCP:
1824+
return "TOX_NETPROF_PACKET_TYPE_TCP";
1825+
case TOX_NETPROF_PACKET_TYPE_UDP:
1826+
return "TOX_NETPROF_PACKET_TYPE_UDP";
1827+
}
1828+
1829+
return "<invalid Tox_Netprof_Packet_Type>";
1830+
}
1831+
const char *tox_netprof_direction_to_string(Tox_Netprof_Direction value)
1832+
{
1833+
switch (value) {
1834+
case TOX_NETPROF_DIRECTION_SENT:
1835+
return "TOX_NETPROF_DIRECTION_SENT";
1836+
case TOX_NETPROF_DIRECTION_RECV:
1837+
return "TOX_NETPROF_DIRECTION_RECV";
1838+
}
1839+
1840+
return "<invalid Tox_Netprof_Direction>";
1841+
}

toxcore/tox_private.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ typedef enum Tox_Netprof_Packet_Id {
345345
TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO = 0xf0,
346346
} Tox_Netprof_Packet_Id;
347347

348+
const char *tox_netprof_packet_id_to_string(Tox_Netprof_Packet_Id value);
349+
348350
/**
349351
* Specifies the packet type for a given query.
350352
*/
@@ -370,6 +372,8 @@ typedef enum Tox_Netprof_Packet_Type {
370372
TOX_NETPROF_PACKET_TYPE_UDP,
371373
} Tox_Netprof_Packet_Type;
372374

375+
const char *tox_netprof_packet_type_to_string(Tox_Netprof_Packet_Type value);
376+
373377
/**
374378
* Specifies the packet direction for a given query.
375379
*/
@@ -385,6 +389,8 @@ typedef enum Tox_Netprof_Direction {
385389
TOX_NETPROF_DIRECTION_RECV,
386390
} Tox_Netprof_Direction;
387391

392+
const char *tox_netprof_direction_to_string(Tox_Netprof_Direction value);
393+
388394
/**
389395
* Return the number of packets sent or received for a specific packet ID.
390396
*

0 commit comments

Comments
 (0)