From 864d1c15e4a3b8df236af320312e8115c6bcd791 Mon Sep 17 00:00:00 2001 From: hugbubby Date: Sun, 15 Jul 2018 17:09:13 -0700 Subject: [PATCH] Using stdint instead of int/long Did my best to surmise the size requirements of these integers, will do the rest of the tests soon. Also added a todo and made an obsessive change to a for loop. --- auto_tests/conference_test.c | 40 ++++++++++++------------ auto_tests/conference_two_test.c | 6 ++-- auto_tests/crypto_test.c | 30 +++++++++--------- auto_tests/dht_test.c | 53 ++++++++++++++++---------------- 4 files changed, 64 insertions(+), 65 deletions(-) diff --git a/auto_tests/conference_test.c b/auto_tests/conference_test.c index caa00356..374274b8 100644 --- a/auto_tests/conference_test.c +++ b/auto_tests/conference_test.c @@ -27,7 +27,7 @@ static void handle_self_connection_status( Tox *tox, TOX_CONNECTION connection_status, void *user_data) { - const int id = *(int *)user_data; + const uint16_t id = *(uint16_t *)user_data; if (connection_status != TOX_CONNECTION_NONE) { printf("tox #%d: is now connected\n", id); @@ -39,7 +39,7 @@ static void handle_self_connection_status( static void handle_friend_connection_status( Tox *tox, uint32_t friendnumber, TOX_CONNECTION connection_status, void *user_data) { - const int id = *(int *)user_data; + const uint16_t id = *(uint16_t *)user_data; if (connection_status != TOX_CONNECTION_NONE) { printf("tox #%d: is now connected to friend %d\n", id, friendnumber); @@ -52,7 +52,7 @@ static void handle_conference_invite( Tox *tox, uint32_t friendnumber, TOX_CONFERENCE_TYPE type, const uint8_t *data, size_t length, void *user_data) { - const int id = *(int *)user_data; + const uint16_t id = *(uint16_t *)user_data; ck_assert_msg(type == TOX_CONFERENCE_TYPE_TEXT, "tox #%d: wrong conference type: %d", id, type); TOX_ERR_CONFERENCE_JOIN err; @@ -74,7 +74,7 @@ static void handle_conference_invite( } } -static unsigned int num_recv; +static uint16_t num_recv; static void handle_conference_message( Tox *tox, uint32_t groupnumber, uint32_t peernumber, TOX_MESSAGE_TYPE type, @@ -87,7 +87,7 @@ static void handle_conference_message( static void run_conference_tests(Tox **toxes, uint32_t *tox_index) { - for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) { + for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { tox_callback_conference_message(toxes[i], &handle_conference_message); } @@ -100,8 +100,8 @@ static void run_conference_tests(Tox **toxes, uint32_t *tox_index) err == TOX_ERR_CONFERENCE_SEND_MESSAGE_OK, "Failed to send group message."); num_recv = 0; - for (unsigned j = 0; j < 20; ++j) { - for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) { + for (uint8_t j = 0; j < 20; ++j) { + for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { tox_iterate(toxes[i], &tox_index[i]); } @@ -111,18 +111,18 @@ static void run_conference_tests(Tox **toxes, uint32_t *tox_index) c_sleep(25); ck_assert_msg(num_recv == NUM_GROUP_TOX, "Failed to recv group messages."); - for (unsigned k = NUM_GROUP_TOX; k != 0 ; --k) { + for (uint16_t k = NUM_GROUP_TOX; k != 0 ; --k) { tox_conference_delete(toxes[k - 1], 0, nullptr); - for (unsigned j = 0; j < 10; ++j) { - for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) { + for (uint8_t j = 0; j < 10; ++j) { + for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { tox_iterate(toxes[i], &tox_index[i]); } c_sleep(50); } - for (unsigned i = 0; i < k - 1; ++i) { + for (uint16_t i = 0; i < k - 1; ++i) { uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr); ck_assert_msg(peer_count == (k - 1), "\n\tBad number of group peers (post check)." "\n\t\t\tExpected: %u but tox_instance(%u) only has: %" PRIu32 "\n\n", @@ -144,7 +144,7 @@ static void test_many_group(void) printf("creating %d toxes\n", NUM_GROUP_TOX); - for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) { + for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { TOX_ERR_NEW err; tox_index[i] = i + 1; toxes[i] = tox_new_log(opts, &err, &tox_index[i]); @@ -186,7 +186,7 @@ static void test_many_group(void) while (online_count != NUM_GROUP_TOX) { online_count = 0; - for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) { + for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { tox_iterate(toxes[i], &tox_index[i]); online_count += tox_friend_get_connection_status(toxes[i], 0, nullptr) != TOX_CONNECTION_NONE; } @@ -197,7 +197,7 @@ static void test_many_group(void) c_sleep(1000); } - printf("friends connected, took %d seconds\n", (int)(time(nullptr) - cur_time)); + printf("friends connected, took %d seconds\n", (uint16_t)(time(nullptr) - cur_time)); ck_assert_msg(tox_conference_new(toxes[0], nullptr) != UINT32_MAX, "Failed to create group"); printf("tox #%d: inviting its first friend\n", tox_index[0]); @@ -206,7 +206,7 @@ static void test_many_group(void) "Failed to set group title"); // One iteration for all the invitations to happen. - for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) { + for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { tox_iterate(toxes[i], &tox_index[i]); } @@ -218,7 +218,7 @@ static void test_many_group(void) invited_count = 0; printf("current peer counts: ["); - for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) { + for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { tox_iterate(toxes[i], &tox_index[i]); uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr); invited_count += peer_count == NUM_GROUP_TOX; @@ -236,7 +236,7 @@ static void test_many_group(void) c_sleep(1000); } - for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) { + for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr); ck_assert_msg(peer_count == NUM_GROUP_TOX, "\n\tBad number of group peers (pre check)." @@ -250,17 +250,17 @@ static void test_many_group(void) ck_assert_msg(memcmp("Gentoo", title, ret) == 0, "Wrong title"); } - printf("group connected, took %d seconds\n", (int)(time(nullptr) - cur_time)); + printf("group connected, took %d seconds\n", (uint16_t)(time(nullptr) - cur_time)); run_conference_tests(toxes, tox_index); printf("tearing down toxes\n"); - for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) { + for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { tox_kill(toxes[i]); } - printf("test_many_group succeeded, took %d seconds\n", (int)(time(nullptr) - test_start_time)); + printf("test_many_group succeeded, took %d seconds\n", (uint16_t)(time(nullptr) - test_start_time)); } int main(void) diff --git a/auto_tests/conference_two_test.c b/auto_tests/conference_two_test.c index c3036165..8d834dc4 100644 --- a/auto_tests/conference_two_test.c +++ b/auto_tests/conference_two_test.c @@ -14,15 +14,15 @@ int main(void) { // Create toxes. - uint32_t id = 1; + uint8_t id = 1; Tox *tox1 = tox_new_log(nullptr, nullptr, &id); // Create two conferences and then exit. TOX_ERR_CONFERENCE_NEW err; tox_conference_new(tox1, &err); - ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, "failed to create conference 1: %d", err); + ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, "Failed to create conference 1: %d.", err); tox_conference_new(tox1, &err); - ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, "failed to create conference 2: %d", err); + ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, "Failed to create conference 2: %d.", err); tox_kill(tox1); diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c index 23c45dd0..c87ffa54 100644 --- a/auto_tests/crypto_test.c +++ b/auto_tests/crypto_test.c @@ -92,7 +92,7 @@ START_TEST(test_known) { unsigned char c[147]; unsigned char m[131]; - int clen, mlen; + uint16_t clen, mlen; ck_assert_msg(sizeof(c) == sizeof(m) + CRYPTO_MAC_SIZE * sizeof(unsigned char), "cyphertext should be CRYPTO_MAC_SIZE bytes longer than plaintext"); @@ -116,7 +116,7 @@ START_TEST(test_fast_known) unsigned char k[CRYPTO_SHARED_KEY_SIZE]; unsigned char c[147]; unsigned char m[131]; - int clen, mlen; + uint16_t clen, mlen; encrypt_precompute(bobpk, alicesk, k); @@ -158,11 +158,11 @@ START_TEST(test_endtoend) unsigned char m3[sizeof(m)]; unsigned char m4[sizeof(m)]; - int mlen; - int c1len, c2len, c3len, c4len; - int m1len, m2len, m3len, m4len; + uint16_t mlen; + uint16_t c1len, c2len, c3len, c4len; + uint16_t m1len, m2len, m3len, m4len; - int testno; + uint8_t testno; // Test 100 random messages and keypairs for (testno = 0; testno < 100; testno++) { @@ -188,7 +188,7 @@ START_TEST(test_endtoend) c4len = encrypt_data_symmetric(k2, n, m, mlen, c4); ck_assert_msg(c1len == c2len && c1len == c3len && c1len == c4len, "cyphertext lengths differ"); - ck_assert_msg(c1len == mlen + (int)CRYPTO_MAC_SIZE, "wrong cyphertext length"); + ck_assert_msg(c1len == mlen + (uint16_t)CRYPTO_MAC_SIZE, "wrong cyphertext length"); ck_assert_msg(memcmp(c1, c2, c1len) == 0 && memcmp(c1, c3, c1len) == 0 && memcmp(c1, c4, c1len) == 0, "crypertexts differ"); @@ -220,8 +220,8 @@ START_TEST(test_large_data) unsigned char m2[MAX_CRYPTO_PACKET_SIZE]; unsigned char c2[sizeof(m2) + CRYPTO_MAC_SIZE]; - int c1len, c2len; - int m1plen; + uint16_t c1len, c2len; + uint16_t m1plen; //Generate random messages rand_bytes(m1, sizeof(m1)); @@ -254,8 +254,8 @@ START_TEST(test_large_data_symmetric) unsigned char c1[sizeof(m1) + CRYPTO_MAC_SIZE]; unsigned char m1prime[sizeof(m1)]; - int c1len; - int m1plen; + uint16_t c1len; + uint16_t m1plen; //Generate random messages rand_bytes(m1, sizeof(m1)); @@ -282,9 +282,7 @@ static void increment_nonce_number_cmp(uint8_t *nonce, uint32_t num) num2 = num + num1; if (num2 < num1) { - uint32_t i; - - for (i = CRYPTO_NONCE_SIZE - sizeof(num1); i != 0; --i) { + for (uint16_t i = CRYPTO_NONCE_SIZE - sizeof(num1); i != 0; --i) { ++nonce[i - 1]; if (nonce[i - 1] != 0) { @@ -299,7 +297,7 @@ static void increment_nonce_number_cmp(uint8_t *nonce, uint32_t num) START_TEST(test_increment_nonce) { - long long unsigned int i; + uint32_t i; uint8_t n[CRYPTO_NONCE_SIZE]; @@ -362,7 +360,7 @@ int main(void) Suite *crypto = crypto_suite(); SRunner *test_runner = srunner_create(crypto); - int number_failed = 0; + uint8_t number_failed = 0; srunner_run_all(test_runner, CK_NORMAL); number_failed = srunner_ntests_failed(test_runner); diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index a63a55b0..dec803e5 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -94,9 +94,9 @@ static uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uin static int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key) { - int i; + uint32_t i; - for (i = 0; i < (int)length; ++i) { + for (i = 0; i < (uint32_t)length; ++i) { if (id_equal(public_key, list[i].public_key)) { return i; } @@ -110,7 +110,7 @@ static void test_addto_lists_update(DHT *dht, uint32_t length, IP_Port *ip_port) { - int used, test, test1, test2, found; + uint32_t used, test, test1, test2, found; IP_Port test_ipp; uint8_t test_id[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; @@ -229,7 +229,7 @@ static void test_addto_lists_possible_bad(DHT *dht, const uint8_t *comp_client_id) { // check "possibly bad" clients replacement - int used, test1, test2, test3; + uint32_t used, test1, test2, test3; uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], test_id3[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; @@ -263,9 +263,9 @@ static void test_addto_lists_possible_bad(DHT *dht, ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Client id is not in the list"); - int inlist_id1 = client_in_list(list, length, test_id1) >= 0; - int inlist_id2 = client_in_list(list, length, test_id2) >= 0; - int inlist_id3 = client_in_list(list, length, test_id3) >= 0; + bool inlist_id1 = client_in_list(list, length, test_id1) >= 0; + bool inlist_id2 = client_in_list(list, length, test_id2) >= 0; + bool inlist_id3 = client_in_list(list, length, test_id3) >= 0; ck_assert_msg(inlist_id1 + inlist_id2 + inlist_id3 == 2, "Wrong client removed"); @@ -337,7 +337,7 @@ static void test_addto_lists(IP ip) ip_port.ip = ip; ip_port.port = TOX_PORT_DEFAULT; uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; - int i, used; + uint16_t i, used; // check lists filling for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { @@ -426,11 +426,11 @@ static void print_pk(uint8_t *public_key) } static void test_add_to_list(uint8_t cmp_list[][CRYPTO_PUBLIC_KEY_SIZE + 1], - unsigned int length, const uint8_t *pk, + uint16_t length, const uint8_t *pk, const uint8_t *cmp_pk) { uint8_t p_b[CRYPTO_PUBLIC_KEY_SIZE]; - unsigned int i; + uint16_t i; for (i = 0; i < length; ++i) { if (!cmp_list[i][CRYPTO_PUBLIC_KEY_SIZE]) { @@ -465,7 +465,7 @@ static void test_list_main(void) uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][CRYPTO_PUBLIC_KEY_SIZE + 1]; memset(cmp_list1, 0, sizeof(cmp_list1)); - unsigned int i, j, k, l; + uint16_t i, j, k, l; for (i = 0; i < NUM_DHT; ++i) { IP ip; @@ -481,14 +481,14 @@ static void test_list_main(void) "Bound to wrong port: %d", net_port(dhts[i]->net)); } - for (j = 0; j < NUM_DHT; ++j) { - for (i = 1; i < NUM_DHT; ++i) { - test_add_to_list(cmp_list1[j], MAX_FRIEND_CLIENTS, dhts[(i + j) % NUM_DHT]->self_public_key, dhts[j]->self_public_key); + for (i = 0; i < NUM_DHT; ++i) { + for (j = 1; j < NUM_DHT; ++j) { + test_add_to_list(cmp_list1[i], MAX_FRIEND_CLIENTS, dhts[(i + j) % NUM_DHT]->self_public_key, dhts[i]->self_public_key); } } - for (j = 0; j < NUM_DHT; ++j) { - for (i = 0; i < NUM_DHT; ++i) { + for (i = 0; i < NUM_DHT; ++i) { + for (j = 0; j < NUM_DHT; ++j) { if (i == j) { continue; } @@ -498,7 +498,7 @@ static void test_list_main(void) ip_port.ip.ip.v4.uint32 = random_u32(); ip_port.port = random_u32() % (UINT16_MAX - 1); ++ip_port.port; - addto_lists(dhts[j], ip_port, dhts[i]->self_public_key); + addto_lists(dhts[i], ip_port, dhts[j]->self_public_key); } } @@ -512,7 +512,7 @@ static void test_list_main(void) } #endif - unsigned int m_count = 0; + uint16_t m_count = 0; for (l = 0; l < NUM_DHT; ++l) { for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { @@ -521,7 +521,7 @@ static void test_list_main(void) continue; } - unsigned int count = 0; + uint16_t count = 0; for (k = 0; k < LCLIENT_LIST; ++k) { if (memcmp(dhts[l]->self_public_key, dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key, @@ -550,7 +550,7 @@ static void test_list_main(void) ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count); Node_format ln[MAX_SENT_NODES]; - int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, net_family_unspec, 1, 0); + uint16_t n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, net_family_unspec, 1, 0); ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j); count = 0; @@ -589,7 +589,7 @@ static void test_list_main(void) START_TEST(test_list) { - unsigned int i; + uint8_t i; for (i = 0; i < 10; ++i) { test_list_main(); @@ -610,7 +610,7 @@ START_TEST(test_DHT_test) Logger *logs[NUM_DHT]; uint32_t index[NUM_DHT]; - unsigned int i, j; + uint32_t i, j; for (i = 0; i < NUM_DHT; ++i) { IP ip; @@ -631,6 +631,7 @@ START_TEST(test_DHT_test) } pairs[NUM_DHT_FRIENDS]; for (i = 0; i < NUM_DHT_FRIENDS; ++i) { + //TODO: Hugbubby say goto bad >:( loop_top: pairs[i].tox1 = random_u32() % NUM_DHT; pairs[i].tox2 = (pairs[i].tox1 + (random_u32() % (NUM_DHT - 1)) + 1) % NUM_DHT; @@ -694,7 +695,7 @@ START_TEST(test_dht_create_packet) uint8_t key[CRYPTO_SYMMETRIC_KEY_SIZE]; new_symmetric_key(key); - int length = dht_create_packet(key, key, NET_PACKET_GET_NODES, plain, sizeof(plain), pkt); + uint16_t length = dht_create_packet(key, key, NET_PACKET_GET_NODES, plain, sizeof(plain), pkt); ck_assert_msg(pkt[0] == NET_PACKET_GET_NODES, "Malformed packet."); ck_assert_msg(memcmp(pkt + 1, key, CRYPTO_SYMMETRIC_KEY_SIZE) == 0, "Malformed packet."); @@ -709,14 +710,14 @@ END_TEST static void dht_pack_unpack(const Node_format *nodes, size_t size, uint8_t *data, size_t length) { - int packed_size = pack_nodes(data, length, nodes, size); + uint16_t packed_size = pack_nodes(data, length, nodes, size); ck_assert_msg(packed_size != -1, "Wrong pack_nodes result"); uint16_t processed = 0; VLA(Node_format, nodes_unpacked, size); const uint8_t tcp_enabled = 1; - int unpacked_count = unpack_nodes(nodes_unpacked, size, &processed, data, length, tcp_enabled); + uint16_t unpacked_count = unpack_nodes(nodes_unpacked, size, &processed, data, length, tcp_enabled); ck_assert_msg(unpacked_count == size, "Wrong unpack_nodes result"); ck_assert_msg(processed == packed_size, "unpack_nodes did not process all data"); @@ -822,7 +823,7 @@ int main(void) Suite *dht = dht_suite(); SRunner *test_runner = srunner_create(dht); - int number_failed = 0; + uint8_t number_failed = 0; //srunner_set_fork_status(test_runner, CK_NOFORK); srunner_run_all(test_runner, CK_NORMAL); number_failed = srunner_ntests_failed(test_runner);