Skip to content

Commit

Permalink
cleanup: Remove more boolean conversions (and a bugfix).
Browse files Browse the repository at this point in the history
These were found by the new stronger type check in cimple. The one
bugfix is in `crypto_sha512_cmp`, which used to think `crypto_verify_32`
returns bool while actually it's -1/0/1.
  • Loading branch information
iphydf committed Feb 26, 2022
1 parent e230ad4 commit 163e9c3
Show file tree
Hide file tree
Showing 29 changed files with 420 additions and 398 deletions.
22 changes: 11 additions & 11 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ static void test_basic(void)
ck_assert_msg(packet_resp_plain[0] == TCP_PACKET_ROUTING_RESPONSE, "Server sent the wrong packet id: %u",
packet_resp_plain[0]);
ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection.");
ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "Server sent the wrong public key.");
ck_assert_msg(pk_equal(packet_resp_plain + 2, f_public_key), "Server sent the wrong public key.");

// Closing connections.
kill_sock(sock);
Expand Down Expand Up @@ -315,14 +315,14 @@ static void test_some(void)
ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "Key in response packet wrong.");
ck_assert_msg(pk_equal(data + 2, con3->public_key), "Key in response packet wrong.");

// Connection 3
len = read_packet_sec_TCP(logger, con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong.");
ck_assert_msg(pk_equal(data + 2, con1->public_key), "Key in response packet wrong.");

uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet????

Expand Down Expand Up @@ -458,7 +458,7 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint
return 1;
}

if (public_key_cmp(public_key, oob_pubkey) != 0) {
if (!pk_equal(public_key, oob_pubkey)) {
return 1;
}

Expand Down Expand Up @@ -563,7 +563,7 @@ static void test_client(void)
// All callback methods save data should have run during the above network prodding.
ck_assert_msg(oob_data_callback_good == 1, "OOB callback not called");
ck_assert_msg(response_callback_good == 1, "Response callback not called.");
ck_assert_msg(public_key_cmp(response_callback_public_key, f2_public_key) == 0, "Wrong public key.");
ck_assert_msg(pk_equal(response_callback_public_key, f2_public_key), "Wrong public key.");
ck_assert_msg(status_callback_good == 1, "Status callback not called.");
ck_assert_msg(status_callback_status == 2, "Wrong status callback status.");
ck_assert_msg(status_callback_connection_id == response_callback_connection_id,
Expand Down Expand Up @@ -680,17 +680,17 @@ static void test_tcp_connection(void)
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");
ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");

TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");

crypto_new_keypair(self_public_key, self_secret_key);
TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");

IP_Port ip_port_tcp_s;

Expand Down Expand Up @@ -788,17 +788,17 @@ static void test_tcp_connection2(void)
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");
ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");

TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");

crypto_new_keypair(self_public_key, self_secret_key);
TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");

IP_Port ip_port_tcp_s;

Expand Down
14 changes: 7 additions & 7 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static int client_in_list(Client_data *list, uint32_t length, const uint8_t *pub
uint32_t i;

for (i = 0; i < length; ++i) {
if (id_equal(public_key, list[i].public_key)) {
if (pk_equal(public_key, list[i].public_key)) {
return i;
}
}
Expand Down Expand Up @@ -106,7 +106,7 @@ static void test_addto_lists_update(DHT *dht,
// check ip_port update for existing id
test = random_u32() % length;
test_ipp.port = random_u32() % TOX_PORT_DEFAULT;
id_copy(test_id, list[test].public_key);
pk_copy(test_id, list[test].public_key);

used = addto_lists(dht, &test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
Expand All @@ -120,7 +120,7 @@ static void test_addto_lists_update(DHT *dht,
test2 = random_u32() % (length / 2) + length / 2;

ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port);
id_copy(test_id, list[test2].public_key);
pk_copy(test_id, list[test2].public_key);

if (ipv6) {
list[test2].assoc6.ip_port.port = -1;
Expand All @@ -139,7 +139,7 @@ static void test_addto_lists_update(DHT *dht,
test2 = random_u32() % (length / 2) + length / 2;

ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port);
id_copy(test_id, list[test1].public_key);
pk_copy(test_id, list[test1].public_key);

if (ipv6) {
list[test1].assoc6.ip_port.port = -1;
Expand Down Expand Up @@ -173,9 +173,9 @@ static void test_addto_lists_bad(DHT *dht,
test3 = random_u32() % (length / 3) + 2 * length / 3;
ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen");

id_copy((uint8_t *)&test_id1, list[test1].public_key);
id_copy((uint8_t *)&test_id2, list[test2].public_key);
id_copy((uint8_t *)&test_id3, list[test3].public_key);
pk_copy((uint8_t *)&test_id1, list[test1].public_key);
pk_copy((uint8_t *)&test_id2, list[test2].public_key);
pk_copy((uint8_t *)&test_id3, list[test3].public_key);

// mark nodes as "bad"
if (ipv6) {
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
aea24cfae8db82511a298e2dbedb6634145732190ebcad361690cb6ca6560d7c /usr/local/bin/tox-bootstrapd
a80ea98f55b82eb5d60baa5d7fc32913ff964b04b931a17b74233148a27786f5 /usr/local/bin/tox-bootstrapd
1 change: 1 addition & 0 deletions testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sh_test(
size = "small",
srcs = ["//hs-tokstyle/tools:check-cimple"],
args = ["$(locations %s)" % f for f in CIMPLE_FILES] + [
"-Wno-boolean-return",
"-Wno-callback-names",
"-Wno-enum-names",
"-Wno-memcpy-structs",
Expand Down
4 changes: 2 additions & 2 deletions toxav/msi.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data
const uint8_t *it = data;
int size_constraint = length;

while (*it) {/* until end byte is hit */
while (*it != 0) {/* until end byte is hit */
switch (*it) {
case ID_REQUEST: {
if (!check_size(log, it, &size_constraint, 1) ||
Expand Down Expand Up @@ -435,7 +435,7 @@ static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data
}
}

if (dest->request.exists == false) {
if (!dest->request.exists) {
LOGGER_ERROR(log, "Invalid request field!");
return -1;
}
Expand Down
14 changes: 7 additions & 7 deletions toxav/toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, ui
Toxav_Err_Answer rc = TOXAV_ERR_ANSWER_OK;
ToxAVCall *call;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -612,7 +612,7 @@ static Toxav_Err_Call_Control call_control_handle(ToxAVCall *call, Toxav_Call_Co
}
static Toxav_Err_Call_Control call_control(ToxAV *av, uint32_t friend_number, Toxav_Call_Control control)
{
if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
return TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND;
}

Expand Down Expand Up @@ -644,7 +644,7 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra
Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK;
ToxAVCall *call;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -716,7 +716,7 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra
Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK;
ToxAVCall *call;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -802,7 +802,7 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc
Toxav_Err_Send_Frame rc = TOXAV_ERR_SEND_FRAME_OK;
ToxAVCall *call;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -929,7 +929,7 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u

int vpx_encode_flags = 0;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -1238,7 +1238,7 @@ static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *er
Toxav_Err_Call rc = TOXAV_ERR_CALL_OK;
ToxAVCall *call = nullptr;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_CALL_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down
Loading

0 comments on commit 163e9c3

Please sign in to comment.