Skip to content

Commit

Permalink
chore: add to_string functions for netprof enums
Browse files Browse the repository at this point in the history
Also fixed u64 formatting in the netprof auto test
  • Loading branch information
JFreegman committed Nov 28, 2024
1 parent ce1b21c commit 8795684
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 7 deletions.
13 changes: 7 additions & 6 deletions auto_tests/netprof_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* TODO(JFreegman): test TCP packets as well
*/

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

Expand Down Expand Up @@ -91,18 +92,18 @@ static void test_netprof(AutoTox *autotoxes)

const uint64_t total_packets = total_sent_count + total_recv_count;
ck_assert_msg(total_packets == UDP_count_sent1 + UDP_count_recv1,
"%lu does not match %lu\n", total_packets, UDP_count_sent1 + UDP_count_recv1);
"%" PRIu64 "does not match %" PRIu64 "\n", total_packets, UDP_count_sent1 + UDP_count_recv1);

ck_assert_msg(total_sent_count == UDP_count_sent1, "%lu does not match %lu\n", total_sent_count, UDP_count_sent1);
ck_assert_msg(total_recv_count == UDP_count_recv1, "%lu does not match %lu\n", total_recv_count, UDP_count_recv1);
ck_assert_msg(total_sent_count == UDP_count_sent1, "%" PRIu64 " does not match %" PRIu64 "\n", total_sent_count, UDP_count_sent1);
ck_assert_msg(total_recv_count == UDP_count_recv1, "%" PRIu64 " does not match %" PRIu64"\n", total_recv_count, UDP_count_recv1);


const uint64_t total_bytes = total_sent_bytes + total_recv_bytes;
ck_assert_msg(total_bytes == UDP_bytes_sent1 + UDP_bytes_recv1,
"%lu does not match %lu\n", total_bytes, UDP_bytes_sent1 + UDP_bytes_recv1);
"%" PRIu64 "does not match %" PRIu64 "\n", total_bytes, UDP_bytes_sent1 + UDP_bytes_recv1);

ck_assert_msg(total_sent_bytes == UDP_bytes_sent1, "%lu does not match %lu\n", total_sent_bytes, UDP_bytes_sent1);
ck_assert_msg(total_recv_bytes == UDP_bytes_recv1, "%lu does not match %lu\n", total_recv_bytes, UDP_bytes_recv1);
ck_assert_msg(total_sent_bytes == UDP_bytes_sent1, "%" PRIu64 " does not match %" PRIu64 "\n", total_sent_bytes, UDP_bytes_sent1);
ck_assert_msg(total_recv_bytes == UDP_bytes_recv1, "%" PRIu64 " does not match %" PRIu64 "\n", total_recv_bytes, UDP_bytes_recv1);
}

int main(void)
Expand Down
1 change: 1 addition & 0 deletions toxcore/net_profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/* The max number of packet ID's (must fit inside one byte) */
#define NET_PROF_MAX_PACKET_IDS 256

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

/** Specifies whether the query is for sent or received packets. */
Expand Down
116 changes: 115 additions & 1 deletion toxcore/tox_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Copyright © 2016-2021 The TokTok team.
*/
#include "tox.h"
#include "tox_private.h"

#include <stdlib.h>

#include "ccompat.h"
#include "tox_private.h"

#define SET_ERROR_PARAMETER(param, x) \
do { \
Expand Down Expand Up @@ -1722,3 +1722,117 @@ const char *tox_group_mod_event_to_string(Tox_Group_Mod_Event value)

return "<invalid Tox_Group_Mod_Event>";
}
const char *tox_netprof_packet_id_to_string(Tox_Netprof_Packet_Id value) {
switch (value) {
case TOX_NETPROF_PACKET_ID_ZERO:
return "TOX_NETPROF_PACKET_ID_ZERO";
case TOX_NETPROF_PACKET_ID_ONE:
return "TOX_NETPROF_PACKET_ID_ONE";
case TOX_NETPROF_PACKET_ID_TWO:
return "TOX_NETPROF_PACKET_ID_TWO";
case TOX_NETPROF_PACKET_ID_TCP_DISCONNECT:
return "TOX_NETPROF_PACKET_ID_TCP_DISCONNECT";
case TOX_NETPROF_PACKET_ID_FOUR:
return "TOX_NETPROF_PACKET_ID_FOUR";
case TOX_NETPROF_PACKET_ID_TCP_PONG:
return "TOX_NETPROF_PACKET_ID_TCP_PONG";
case TOX_NETPROF_PACKET_ID_TCP_OOB_SEND:
return "TOX_NETPROF_PACKET_ID_TCP_OOB_SEND";
case TOX_NETPROF_PACKET_ID_TCP_OOB_RECV:
return "TOX_NETPROF_PACKET_ID_TCP_OOB_RECV";
case TOX_NETPROF_PACKET_ID_TCP_ONION_REQUEST:
return "TOX_NETPROF_PACKET_ID_TCP_ONION_REQUEST";
case TOX_NETPROF_PACKET_ID_TCP_ONION_RESPONSE:
return "TOX_NETPROF_PACKET_ID_TCP_ONION_RESPONSE";
case TOX_NETPROF_PACKET_ID_TCP_DATA:
return "TOX_NETPROF_PACKET_ID_TCP_DATA";
case TOX_NETPROF_PACKET_ID_COOKIE_REQUEST:
return "TOX_NETPROF_PACKET_ID_COOKIE_REQUEST";
case TOX_NETPROF_PACKET_ID_COOKIE_RESPONSE:
return "TOX_NETPROF_PACKET_ID_COOKIE_RESPONSE";
case TOX_NETPROF_PACKET_ID_CRYPTO_HS:
return "TOX_NETPROF_PACKET_ID_CRYPTO_HS";
case TOX_NETPROF_PACKET_ID_CRYPTO_DATA:
return "TOX_NETPROF_PACKET_ID_CRYPTO_DATA";
case TOX_NETPROF_PACKET_ID_CRYPTO:
return "TOX_NETPROF_PACKET_ID_CRYPTO";
case TOX_NETPROF_PACKET_ID_LAN_DISCOVERY:
return "TOX_NETPROF_PACKET_ID_LAN_DISCOVERY";
case TOX_NETPROF_PACKET_ID_GC_HANDSHAKE:
return "TOX_NETPROF_PACKET_ID_GC_HANDSHAKE";
case TOX_NETPROF_PACKET_ID_GC_LOSSLESS:
return "TOX_NETPROF_PACKET_ID_GC_LOSSLESS";
case TOX_NETPROF_PACKET_ID_GC_LOSSY:
return "TOX_NETPROF_PACKET_ID_GC_LOSSY";
case TOX_NETPROF_PACKET_ID_ONION_SEND_INITIAL:
return "TOX_NETPROF_PACKET_ID_ONION_SEND_INITIAL";
case TOX_NETPROF_PACKET_ID_ONION_SEND_1:
return "TOX_NETPROF_PACKET_ID_ONION_SEND_1";
case TOX_NETPROF_PACKET_ID_ONION_SEND_2:
return "TOX_NETPROF_PACKET_ID_ONION_SEND_2";
case TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST_OLD:
return "TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST_OLD";
case TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE_OLD:
return "TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE_OLD";
case TOX_NETPROF_PACKET_ID_ONION_DATA_REQUEST:
return "TOX_NETPROF_PACKET_ID_ONION_DATA_REQUEST";
case TOX_NETPROF_PACKET_ID_ONION_DATA_RESPONSE:
return "TOX_NETPROF_PACKET_ID_ONION_DATA_RESPONSE";
case TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST:
return "TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST";
case TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE:
return "TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE";
case TOX_NETPROF_PACKET_ID_ONION_RECV_3:
return "TOX_NETPROF_PACKET_ID_ONION_RECV_3";
case TOX_NETPROF_PACKET_ID_ONION_RECV_2:
return "TOX_NETPROF_PACKET_ID_ONION_RECV_2";
case TOX_NETPROF_PACKET_ID_ONION_RECV_1:
return "TOX_NETPROF_PACKET_ID_ONION_RECV_1";
case TOX_NETPROF_PACKET_ID_FORWARD_REQUEST:
return "TOX_NETPROF_PACKET_ID_FORWARD_REQUEST";
case TOX_NETPROF_PACKET_ID_FORWARDING:
return "TOX_NETPROF_PACKET_ID_FORWARDING";
case TOX_NETPROF_PACKET_ID_FORWARD_REPLY:
return "TOX_NETPROF_PACKET_ID_FORWARD_REPLY";
case TOX_NETPROF_PACKET_ID_DATA_SEARCH_REQUEST:
return "TOX_NETPROF_PACKET_ID_DATA_SEARCH_REQUEST";
case TOX_NETPROF_PACKET_ID_DATA_SEARCH_RESPONSE:
return "TOX_NETPROF_PACKET_ID_DATA_SEARCH_RESPONSE";
case TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_REQUEST:
return "TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_REQUEST";
case TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_RESPONSE:
return "TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_RESPONSE";
case TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_REQUEST:
return "TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_REQUEST";
case TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_RESPONSE:
return "TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_RESPONSE";
case TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO:
return "TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO";
}

return "<invalid Tox_Netprof_Packet_Id>";
}
const char *tox_netprof_packet_type_to_string(Tox_Netprof_Packet_Type value) {
switch (value) {
case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT:
return "TOX_NETPROF_PACKET_TYPE_TCP_CLIENT";
case TOX_NETPROF_PACKET_TYPE_TCP_SERVER:
return "TOX_NETPROF_PACKET_TYPE_TCP_SERVER";
case TOX_NETPROF_PACKET_TYPE_TCP:
return "TOX_NETPROF_PACKET_TYPE_TCP";
case TOX_NETPROF_PACKET_TYPE_UDP:
return "TOX_NETPROF_PACKET_TYPE_UDP";
}

return "<invalid Tox_Netprof_Packet_Type>";
}
const char *tox_netprof_direction_to_string(Tox_Netprof_Direction value) {
switch (value) {
case TOX_NETPROF_DIRECTION_SENT:
return "TOX_NETPROF_DIRECTION_SENT";
case TOX_NETPROF_DIRECTION_RECV:
return "TOX_NETPROF_DIRECTION_RECV";
}

return "<invalid Tox_Netprof_Direction>";
}
6 changes: 6 additions & 0 deletions toxcore/tox_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ typedef enum Tox_Netprof_Packet_Id {
TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO = 0xf0,
} Tox_Netprof_Packet_Id;

const char *tox_netprof_packet_id_to_string(Tox_Netprof_Packet_Id value);

/**
* Specifies the packet type for a given query.
*/
Expand All @@ -370,6 +372,8 @@ typedef enum Tox_Netprof_Packet_Type {
TOX_NETPROF_PACKET_TYPE_UDP,
} Tox_Netprof_Packet_Type;

const char *tox_netprof_packet_type_to_string(Tox_Netprof_Packet_Type value);

/**
* Specifies the packet direction for a given query.
*/
Expand All @@ -385,6 +389,8 @@ typedef enum Tox_Netprof_Direction {
TOX_NETPROF_DIRECTION_RECV,
} Tox_Netprof_Direction;

const char *tox_netprof_direction_to_string(Tox_Netprof_Direction value);

/**
* Return the number of packets sent or received for a specific packet ID.
*
Expand Down

0 comments on commit 8795684

Please sign in to comment.