cleanup: Add const where possible in auto tests.

Also one in `DHT_bootstrap.c`.
This commit is contained in:
iphydf 2024-01-25 21:57:31 +00:00
parent 6aa9e6850d
commit d6d67d56f3
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
17 changed files with 42 additions and 41 deletions

View File

@ -582,7 +582,7 @@ static void test_client(void)
do_tcp_connection(logger, mono_time, conn2, nullptr);
c_sleep(50);
uint8_t data[5] = {1, 2, 3, 4, 5};
const uint8_t data[5] = {1, 2, 3, 4, 5};
memcpy(oob_pubkey, f2_public_key, CRYPTO_PUBLIC_KEY_SIZE);
send_oob_packet(logger, conn2, f_public_key, data, 5);
send_routing_request(logger, conn, f2_public_key);

View File

@ -335,7 +335,7 @@ static void initialise_friend_graph(Graph_Type graph, uint32_t num_toxes, AutoTo
}
}
static void bootstrap_autotoxes(struct Tox_Options *options, uint32_t tox_count, const Run_Auto_Options *autotest_opts,
static void bootstrap_autotoxes(const Tox_Options *options, uint32_t tox_count, const Run_Auto_Options *autotest_opts,
AutoTox *autotoxes)
{
const bool udp_enabled = options != nullptr ? tox_options_get_udp_enabled(options) : true;
@ -363,7 +363,9 @@ static void bootstrap_autotoxes(struct Tox_Options *options, uint32_t tox_count,
}
}
void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(AutoTox *autotoxes),
typedef void autotox_test_cb(AutoTox *autotoxes);
void run_auto_test(struct Tox_Options *options, uint32_t tox_count, autotox_test_cb *test,
uint32_t state_size, Run_Auto_Options *autotest_opts)
{
printf("initialising %u toxes\n", tox_count);

View File

@ -108,7 +108,7 @@ static void handle_conference_connected(
}
static bool toxes_are_disconnected_from_group(uint32_t tox_count, AutoTox *autotoxes,
bool *disconnected)
const bool *disconnected)
{
uint32_t num_disconnected = 0;
@ -177,7 +177,7 @@ static bool all_connected_to_group(uint32_t tox_count, AutoTox *autotoxes)
* returns a random index at which a list of booleans is false
* (some such index is required to exist)
*/
static uint32_t random_false_index(const Random *rng, bool *list, const uint32_t length)
static uint32_t random_false_index(const Random *rng, const bool *list, const uint32_t length)
{
uint32_t index;
@ -197,7 +197,7 @@ static bool all_got_audio(AutoTox *autotoxes, const bool *disabled)
}
for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
State *state = (State *)autotoxes[i].state;
const State *state = (const State *)autotoxes[i].state;
if (disabled[i] ^ (state->received_audio_num
!= NUM_AV_GROUP_TOX - num_disabled - 1)) {

View File

@ -40,7 +40,7 @@ static void handle_conference_connected(
state->connected = true;
}
static void wait_connected(AutoTox *autotoxes, AutoTox *autotox, uint32_t friendnumber)
static void wait_connected(AutoTox *autotoxes, const AutoTox *autotox, uint32_t friendnumber)
{
do {
iterate_all_wait(autotoxes, NUM_INVITE_MERGE_TOX, ITERATION_INTERVAL);

View File

@ -116,7 +116,7 @@ static void handle_conference_connected(Tox *tox, const Tox_Event_Conference_Con
}
static void iterate_one(
Tox *tox, State *state, Tox_Dispatch *dispatch)
Tox *tox, State *state, const Tox_Dispatch *dispatch)
{
Tox_Err_Events_Iterate err;
Tox_Events *events = tox_events_iterate(tox, true, &err);
@ -129,7 +129,7 @@ static void iterate3_wait(
Tox *tox1, State *state1,
Tox *tox2, State *state2,
Tox *tox3, State *state3,
Tox_Dispatch *dispatch, int interval)
const Tox_Dispatch *dispatch, int interval)
{
iterate_one(tox1, state1, dispatch);
iterate_one(tox2, state2, dispatch);

View File

@ -97,7 +97,7 @@ static void handle_conference_message(
}
static bool toxes_are_disconnected_from_group(uint32_t tox_count, AutoTox *autotoxes,
bool *disconnected)
const bool *disconnected)
{
uint32_t num_disconnected = 0;
@ -179,7 +179,7 @@ static bool names_propagated(uint32_t tox_count, AutoTox *autotoxes)
* returns a random index at which a list of booleans is false
* (some such index is required to exist)
*/
static uint32_t random_false_index(const Random *rng, bool *list, const uint32_t length)
static uint32_t random_false_index(const Random *rng, const bool *list, const uint32_t length)
{
uint32_t index;

View File

@ -274,7 +274,7 @@ static void test_very_large_data(void)
const Random *rng = os_random();
ck_assert(rng != nullptr);
uint8_t nonce[CRYPTO_NONCE_SIZE] = {0};
const uint8_t nonce[CRYPTO_NONCE_SIZE] = {0};
uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t sk[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(rng, pk, sk);

View File

@ -53,7 +53,7 @@ static void tox_file_receive(Tox *tox, uint32_t friend_number, uint32_t file_num
ck_assert_msg(memcmp(file_id, file_cmp_id, TOX_FILE_ID_LENGTH) == 0, "bad file_id");
uint8_t empty[TOX_FILE_ID_LENGTH] = {0};
const uint8_t empty[TOX_FILE_ID_LENGTH] = {0};
ck_assert_msg(memcmp(empty, file_cmp_id, TOX_FILE_ID_LENGTH) != 0, "empty file_id");
@ -209,7 +209,6 @@ static void file_transfer_test(void)
file_accepted = file_size = sendf_ok = size_recv = 0;
file_recv = 0;
max_sending = UINT64_MAX;
uint64_t totalf_size = 100 * 1024 * 1024;
printf("Starting file streaming transfer test.\n");
@ -224,7 +223,7 @@ static void file_transfer_test(void)
tox_callback_file_chunk_request(tox2, tox_file_chunk_request);
tox_callback_file_recv_control(tox3, file_print_control);
tox_callback_file_recv(tox3, tox_file_receive);
totalf_size = UINT64_MAX;
const uint64_t totalf_size = UINT64_MAX;
Tox_File_Number fnum = tox_file_send(
tox2, 0, TOX_FILE_KIND_DATA, totalf_size, nullptr,
(const uint8_t *)"Gentoo.exe", sizeof("Gentoo.exe"), nullptr);

View File

@ -63,7 +63,7 @@ static void tox_file_receive(Tox *tox, const Tox_Event_File_Recv *event, void *u
ck_assert_msg(memcmp(file_id, file_cmp_id, TOX_FILE_ID_LENGTH) == 0, "bad file_id");
uint8_t empty[TOX_FILE_ID_LENGTH] = {0};
const uint8_t empty[TOX_FILE_ID_LENGTH] = {0};
ck_assert_msg(memcmp(empty, file_cmp_id, TOX_FILE_ID_LENGTH) != 0, "empty file_id");
@ -175,7 +175,7 @@ static void write_file(Tox *tox, const Tox_Event_File_Recv_Chunk *event, void *u
size_recv += length;
}
static void iterate_and_dispatch(Tox_Dispatch *dispatch, Tox *tox)
static void iterate_and_dispatch(const Tox_Dispatch *dispatch, Tox *tox)
{
Tox_Err_Events_Iterate err;
Tox_Events *events;

View File

@ -47,8 +47,8 @@ static void test_forwarded_request_cb(void *object, const IP_Port *forwarder,
const uint8_t *sendback, uint16_t sendback_length,
const uint8_t *data, uint16_t length, void *userdata)
{
Test_Data *test_data = (Test_Data *)object;
uint8_t *index = (uint8_t *)userdata;
const Test_Data *test_data = (const Test_Data *)object;
const uint8_t *index = (const uint8_t *)userdata;
if (length != 12 || memcmp("hello: ", data, 8) != 0) {
printf("[%u] got unexpected data of length %d\n", *index, length);
@ -66,7 +66,7 @@ static void test_forwarded_response_cb(void *object,
const uint8_t *data, uint16_t length, void *userdata)
{
Test_Data *test_data = (Test_Data *)object;
uint8_t *index = (uint8_t *)userdata;
const uint8_t *index = (const uint8_t *)userdata;
if (length != 12 || memcmp("reply: ", data, 8) != 0) {
printf("[%u] got unexpected data of length %d\n", *index, length);

View File

@ -26,7 +26,7 @@ static void accept_friend_request(Tox *tox, const Tox_Event_Friend_Request *even
tox_friend_add_norequest(tox, public_key, nullptr);
}
static void iterate2_wait(Tox_Dispatch *dispatch, Tox *tox1, Tox *tox2)
static void iterate2_wait(const Tox_Dispatch *dispatch, Tox *tox1, Tox *tox2)
{
Tox_Err_Events_Iterate err;
Tox_Events *events;

View File

@ -43,7 +43,7 @@ typedef struct State {
#define PEER_LIMIT 20
static void print_ip(Tox *tox, uint32_t groupnumber, uint32_t peer_id)
static void print_ip(const Tox *tox, uint32_t groupnumber, uint32_t peer_id)
{
Tox_Err_Group_Peer_Query err;
size_t length = tox_group_peer_get_ip_address_size(tox, groupnumber, peer_id, &err);
@ -276,7 +276,7 @@ static void group_announce_test(AutoTox *autotoxes)
Tox *tox0 = autotoxes[0].tox;
Tox *tox1 = autotoxes[1].tox;
State *state0 = (State *)autotoxes[0].state;
State *state1 = (State *)autotoxes[1].state;
const State *state1 = (const State *)autotoxes[1].state;
tox_events_callback_group_peer_join(autotoxes[0].dispatch, group_peer_join_handler);
tox_events_callback_group_peer_join(autotoxes[1].dispatch, group_peer_join_handler);

View File

@ -120,12 +120,12 @@ static void group_invite_test(AutoTox *autotoxes)
Tox *tox5 = autotoxes[5].tox;
Tox *tox6 = autotoxes[6].tox;
State *state0 = (State *)autotoxes[0].state;
State *state2 = (State *)autotoxes[2].state;
State *state3 = (State *)autotoxes[3].state;
State *state4 = (State *)autotoxes[4].state;
State *state5 = (State *)autotoxes[5].state;
State *state6 = (State *)autotoxes[6].state;
const State *state0 = (const State *)autotoxes[0].state;
const State *state2 = (const State *)autotoxes[2].state;
const State *state3 = (const State *)autotoxes[3].state;
const State *state4 = (const State *)autotoxes[4].state;
const State *state5 = (const State *)autotoxes[5].state;
const State *state6 = (const State *)autotoxes[6].state;
Tox_Err_Group_New new_err;
uint32_t groupnumber = tox_group_new(tox0, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *)"test", 4,

View File

@ -385,7 +385,7 @@ static void group_message_test(AutoTox *autotoxes)
ck_assert(rng != nullptr);
Tox *tox0 = autotoxes[0].tox;
Tox *tox1 = autotoxes[1].tox;
const Tox *tox1 = autotoxes[1].tox;
State *state0 = (State *)autotoxes[0].state;
State *state1 = (State *)autotoxes[1].state;

View File

@ -53,7 +53,7 @@ typedef struct State {
static bool all_peers_connected(AutoTox *autotoxes)
{
for (size_t i = 0; i < NUM_GROUP_TOXES; ++i) {
State *state = (State *)autotoxes[i].state;
const State *state = (const State *)autotoxes[i].state;
if (state->num_peers != NUM_GROUP_TOXES - 1) {
return false;
@ -146,7 +146,7 @@ static size_t get_state_index_by_nick(const AutoTox *autotoxes, size_t num_peers
ck_assert(name != nullptr && name_length <= TOX_MAX_NAME_LENGTH);
for (size_t i = 0; i < num_peers; ++i) {
State *state = (State *)autotoxes[i].state;
const State *state = (const State *)autotoxes[i].state;
if (memcmp(state->self_name, name, name_length) == 0) {
return i;
@ -319,7 +319,7 @@ static void check_self_role(AutoTox *autotoxes, uint32_t peer_id, Tox_Group_Role
Tox_Err_Group_Self_Query sq_err;
for (size_t i = 0; i < NUM_GROUP_TOXES; ++i) {
State *state = (State *)autotoxes[i].state;
const State *state = (const State *)autotoxes[i].state;
uint32_t self_peer_id = tox_group_self_get_peer_id(autotoxes[i].tox, state->group_number, &sq_err);
ck_assert(sq_err == TOX_ERR_GROUP_SELF_QUERY_OK);
@ -517,7 +517,7 @@ static void group_moderation_test(AutoTox *autotoxes)
/* all peers should be user role except founder */
for (size_t i = 1; i < NUM_GROUP_TOXES; ++i) {
State *state = (State *)autotoxes[i].state;
const State *state = (const State *)autotoxes[i].state;
self_role = tox_group_self_get_role(autotoxes[i].tox, state->group_number, &sq_err);
ck_assert(sq_err == TOX_ERR_GROUP_SELF_QUERY_OK);
ck_assert(self_role == TOX_GROUP_ROLE_USER);
@ -615,7 +615,7 @@ static void group_moderation_test(AutoTox *autotoxes)
/* the moderator about to be kicked changes the topic to trigger the founder to
* re-sign and redistribute it after the kick.
*/
State *state_x = (State *)autotoxes[idx].state;
const State *state_x = (const State *)autotoxes[idx].state;
Tox *tox_x = autotoxes[idx].tox;
Tox_Err_Group_Topic_Set topic_err;
tox_group_set_topic(tox_x, state_x->group_number, nullptr, 0, &topic_err);

View File

@ -37,7 +37,7 @@ static void do_onion(Mono_Time *mono_time, Onion *onion)
static int handled_test_1;
static int handle_test_1(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
{
Onion *onion = (Onion *)object;
const Onion *onion = (const Onion *)object;
const char req_message[] = "Install Gentoo";
uint8_t req_packet[1 + sizeof(req_message)];
@ -99,7 +99,7 @@ static uint8_t test_3_pub_key[CRYPTO_PUBLIC_KEY_SIZE];
static uint8_t test_3_ping_id[CRYPTO_SHA256_SIZE];
static int handle_test_3(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
{
Onion *onion = (Onion *)object;
const Onion *onion = (const Onion *)object;
if (length < ONION_ANNOUNCE_RESPONSE_MIN_SIZE || length > ONION_ANNOUNCE_RESPONSE_MAX_SIZE) {
return 1;
@ -135,7 +135,7 @@ static int handle_test_3(void *object, const IP_Port *source, const uint8_t *pac
static int handle_test_3_old(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
void *userdata)
{
Onion *onion = (Onion *)object;
const Onion *onion = (const Onion *)object;
if (length < ONION_ANNOUNCE_RESPONSE_MIN_SIZE || length > ONION_ANNOUNCE_RESPONSE_MAX_SIZE) {
return 1;
@ -171,7 +171,7 @@ static uint8_t nonce[CRYPTO_NONCE_SIZE];
static int handled_test_4;
static int handle_test_4(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
{
Onion *onion = (Onion *)object;
const Onion *onion = (const Onion *)object;
if (length != (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + sizeof("Install gentoo") +
CRYPTO_MAC_SIZE)) {
@ -288,7 +288,7 @@ static void test_basic(void)
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1);
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE_OLD, &handle_test_3_old, onion1);
ck_assert_msg((onion1_a != nullptr) && (onion2_a != nullptr), "Onion_Announce failed initializing.");
uint8_t zeroes[64] = {0};
const uint8_t zeroes[64] = {0};
random_bytes(rng, sb_data, sizeof(sb_data));
uint64_t s;
memcpy(&s, sb_data, sizeof(uint64_t));

View File

@ -184,7 +184,7 @@ int main(int argc, char *argv[])
#ifdef TCP_RELAY_ENABLED
#define NUM_PORTS 3
uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
const uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion, forwarding);
if (tcp_s == nullptr) {