Skip to content

Commit

Permalink
cleanup: Add const where possible in auto tests.
Browse files Browse the repository at this point in the history
Also one in `DHT_bootstrap.c`.
  • Loading branch information
iphydf committed Jan 25, 2024
1 parent 6aa9e68 commit d6d67d5
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/conference_invite_merge_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/conference_simple_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/conference_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion auto_tests/crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions auto_tests/file_streaming_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/file_transfer_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/forwarding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/friend_request_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/group_general_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions auto_tests/group_invite_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/group_message_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions auto_tests/group_moderation_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d6d67d5

Please sign in to comment.