More fixed_width ints and incorporating file_saving_test.c

The file_saving_test.c was not included in the cmake list
and thus was ignored by travis and "make check". I found this
out while introducing ck_assert_msg into the integration test.

Furthermore, removed some variable width integers from encryptsave_test.c,
and the SRunner utilization. Implemmented ck_assert_msg, reorganized some
loops, and removed some longs in file_transfer_test.c.
This commit is contained in:
hugbubby 2018-07-15 20:55:45 -07:00 committed by iphydf
parent f627a26a7b
commit c4d58403f9
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
4 changed files with 81 additions and 149 deletions

View File

@ -414,6 +414,7 @@ auto_test(crypto MSVC_DONT_BUILD)
auto_test(dht MSVC_DONT_BUILD) auto_test(dht MSVC_DONT_BUILD)
auto_test(encryptsave) auto_test(encryptsave)
auto_test(file_transfer) auto_test(file_transfer)
auto_test(file_saving)
auto_test(friend_connection) auto_test(friend_connection)
auto_test(friend_request) auto_test(friend_request)
auto_test(invalid_proxy) auto_test(invalid_proxy)

View File

@ -40,22 +40,21 @@ static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8
} }
} }
START_TEST(test_known_kdf) static void test_known_kdf(void)
{ {
unsigned char out[CRYPTO_SHARED_KEY_SIZE]; unsigned char out[CRYPTO_SHARED_KEY_SIZE];
int res = crypto_pwhash_scryptsalsa208sha256(out, int16_t res = crypto_pwhash_scryptsalsa208sha256(out,
CRYPTO_SHARED_KEY_SIZE, CRYPTO_SHARED_KEY_SIZE,
pw, pw,
pwlen, pwlen,
test_salt, test_salt,
crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE * 8, crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE * 8,
crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE); crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE);
ck_assert_msg(res != -1, "crypto function failed"); ck_assert_msg(res != -1, "crypto function failed");
ck_assert_msg(memcmp(out, known_key, CRYPTO_SHARED_KEY_SIZE) == 0, "derived key is wrong"); ck_assert_msg(memcmp(out, known_key, CRYPTO_SHARED_KEY_SIZE) == 0, "derived key is wrong");
} }
END_TEST
START_TEST(test_save_friend) static void test_save_friend(void)
{ {
Tox *tox1 = tox_new_log(nullptr, nullptr, nullptr); Tox *tox1 = tox_new_log(nullptr, nullptr, nullptr);
Tox *tox2 = tox_new_log(nullptr, nullptr, nullptr); Tox *tox2 = tox_new_log(nullptr, nullptr, nullptr);
@ -137,9 +136,8 @@ START_TEST(test_save_friend)
tox_kill(tox3); tox_kill(tox3);
tox_kill(tox4); tox_kill(tox4);
} }
END_TEST
START_TEST(test_keys) static void test_keys(void)
{ {
TOX_ERR_ENCRYPTION encerr; TOX_ERR_ENCRYPTION encerr;
TOX_ERR_DECRYPTION decerr; TOX_ERR_DECRYPTION decerr;
@ -188,31 +186,13 @@ START_TEST(test_keys)
tox_pass_key_free(key2); tox_pass_key_free(key2);
tox_pass_key_free(key); tox_pass_key_free(key);
} }
END_TEST
static Suite *encryptsave_suite(void)
{
Suite *s = suite_create("encryptsave");
DEFTESTCASE_SLOW(known_kdf, 60);
DEFTESTCASE_SLOW(save_friend, 20);
DEFTESTCASE_SLOW(keys, 30);
return s;
}
int main(void) int main(void)
{ {
setvbuf(stdout, nullptr, _IONBF, 0); setvbuf(stdout, nullptr, _IONBF, 0);
test_known_kdf();
test_save_friend();
test_keys();
Suite *encryptsave = encryptsave_suite(); return 0;
SRunner *test_runner = srunner_create(encryptsave);
int number_failed = 0;
srunner_run_all(test_runner, CK_NORMAL);
number_failed = srunner_ntests_failed(test_runner);
srunner_free(test_runner);
return number_failed;
} }

View File

@ -33,6 +33,8 @@
#include "../testing/misc_tools.h" #include "../testing/misc_tools.h"
#include "../toxcore/ccompat.h" #include "../toxcore/ccompat.h"
#include "check_compat.h"
#include "../toxencryptsave/toxencryptsave.h" #include "../toxencryptsave/toxencryptsave.h"
static const char *pphrase = "bar"; static const char *pphrase = "bar";
@ -60,11 +62,9 @@ static void save_data_encrypted(void)
TOX_ERR_ENCRYPTION eerr; TOX_ERR_ENCRYPTION eerr;
if (!tox_pass_encrypt(clear, size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)pphrase, strlen(pphrase), cipher, ck_assert_msg(tox_pass_encrypt(clear, size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)pphrase,
&eerr)) { strlen(pphrase), cipher,
fprintf(stderr, "error: could not encrypt, error code %d\n", eerr); &eerr), "Could not encrypt, error code %d.", eerr);
exit(4);
}
size_t written_value = fwrite(cipher, sizeof(*cipher), size, f); size_t written_value = fwrite(cipher, sizeof(*cipher), size, f);
printf("written written_value = %u of %u\n", (unsigned)written_value, (unsigned)size); printf("written written_value = %u of %u\n", (unsigned)written_value, (unsigned)size);
@ -79,20 +79,18 @@ static void load_data_decrypted(void)
{ {
FILE *f = fopen(savefile, "r"); FILE *f = fopen(savefile, "r");
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
long size = ftell(f); int64_t size = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
uint8_t *cipher = (uint8_t *)malloc(size); uint8_t *cipher = (uint8_t *)malloc(size);
uint8_t *clear = (uint8_t *)malloc(size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH); uint8_t *clear = (uint8_t *)malloc(size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
size_t read_value = fread(cipher, sizeof(*cipher), size, f); size_t read_value = fread(cipher, sizeof(*cipher), size, f);
printf("read read_vavue = %u of %ld\n", (unsigned)read_value, size); printf("Read read_vavue = %u of %ld\n", (unsigned)read_value, size);
TOX_ERR_DECRYPTION derr; TOX_ERR_DECRYPTION derr;
if (!tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr)) { ck_assert_msg(tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr),
fprintf(stderr, "error: could not decrypt, error code %d\n", derr); "Could not decrypt, error code %d.", derr);
exit(3);
}
struct Tox_Options *options = tox_options_new(nullptr); struct Tox_Options *options = tox_options_new(nullptr);
@ -106,19 +104,14 @@ static void load_data_decrypted(void)
tox_options_free(options); tox_options_free(options);
if (t == nullptr) { ck_assert_msg(t != nullptr, "tox_new returned the error value %d", err);
fprintf(stderr, "error: tox_new returned the error value %d\n", err);
return;
}
uint8_t readname[TOX_MAX_NAME_LENGTH]; uint8_t readname[TOX_MAX_NAME_LENGTH];
tox_self_get_name(t, readname); tox_self_get_name(t, readname);
readname[tox_self_get_name_size(t)] = '\0'; readname[tox_self_get_name_size(t)] = '\0';
if (strcmp((const char *)readname, name)) { ck_assert_msg(strcmp((const char *)readname, name) == 0,
fprintf(stderr, "error: name returned by tox_self_get_name does not match expected result\n"); "name returned by tox_self_get_name does not match expected result");
exit(2);
}
free(cipher); free(cipher);
free(clear); free(clear);
@ -132,11 +125,7 @@ int main(void)
save_data_encrypted(); save_data_encrypted();
load_data_decrypted(); load_data_decrypted();
int ret = remove(savefile); ck_assert_msg(remove(savefile) == 0, "Could not remove the savefile.");
if (ret != 0) {
fprintf(stderr, "error: could not remove savefile\n");
}
return 0; return 0;
} }

View File

@ -40,29 +40,20 @@ static uint64_t file_size;
static void tox_file_receive(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t filesize, static void tox_file_receive(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t filesize,
const uint8_t *filename, size_t filename_length, void *userdata) const uint8_t *filename, size_t filename_length, void *userdata)
{ {
if (kind != TOX_FILE_KIND_DATA) { ck_assert_msg(kind == TOX_FILE_KIND_DATA, "bad kind");
ck_abort_msg("Bad kind");
}
if (!(filename_length == sizeof("Gentoo.exe") && memcmp(filename, "Gentoo.exe", sizeof("Gentoo.exe")) == 0)) { ck_assert_msg(filename_length == sizeof("Gentoo.exe")
ck_abort_msg("Bad filename"); && memcmp(filename, "Gentoo.exe", sizeof("Gentoo.exe")) == 0, "bad filename");
}
uint8_t file_id[TOX_FILE_ID_LENGTH]; uint8_t file_id[TOX_FILE_ID_LENGTH];
if (!tox_file_get_file_id(tox, friend_number, file_number, file_id, nullptr)) { ck_assert_msg(tox_file_get_file_id(tox, friend_number, file_number, file_id, nullptr), "tox_file_get_file_id error");
ck_abort_msg("tox_file_get_file_id error");
}
if (memcmp(file_id, file_cmp_id, TOX_FILE_ID_LENGTH) != 0) { ck_assert_msg(memcmp(file_id, file_cmp_id, TOX_FILE_ID_LENGTH) == 0, "bad file_id");
ck_abort_msg("bad file_id");
}
uint8_t empty[TOX_FILE_ID_LENGTH] = {0}; uint8_t empty[TOX_FILE_ID_LENGTH] = {0};
if (memcmp(empty, file_cmp_id, TOX_FILE_ID_LENGTH) == 0) { ck_assert_msg(memcmp(empty, file_cmp_id, TOX_FILE_ID_LENGTH) != 0, "empty file_id");
ck_abort_msg("empty file_id");
}
file_size = filesize; file_size = filesize;
@ -71,28 +62,23 @@ static void tox_file_receive(Tox *tox, uint32_t friend_number, uint32_t file_num
TOX_ERR_FILE_SEEK err_s; TOX_ERR_FILE_SEEK err_s;
if (!tox_file_seek(tox, friend_number, file_number, 1337, &err_s)) { ck_assert_msg(tox_file_seek(tox, friend_number, file_number, 1337, &err_s), "tox_file_seek error");
ck_abort_msg("tox_file_seek error");
}
ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_OK, "tox_file_seek wrong error"); ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_OK, "tox_file_seek wrong error");
} else { } else {
sending_pos = size_recv = 0; sending_pos = size_recv = 0;
} }
TOX_ERR_FILE_CONTROL error; TOX_ERR_FILE_CONTROL error;
if (tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, &error)) { ck_assert_msg(tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, &error),
++file_accepted; "tox_file_control failed. %i", error);
} else { ++file_accepted;
ck_abort_msg("tox_file_control failed. %i", error);
}
TOX_ERR_FILE_SEEK err_s; TOX_ERR_FILE_SEEK err_s;
if (tox_file_seek(tox, friend_number, file_number, 1234, &err_s)) { ck_assert_msg(!tox_file_seek(tox, friend_number, file_number, 1234, &err_s), "tox_file_seek no error");
ck_abort_msg("tox_file_seek no error");
}
ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_DENIED, "tox_file_seek wrong error"); ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_DENIED, "tox_file_seek wrong error");
} }
@ -114,27 +100,19 @@ static bool file_sending_done;
static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position,
size_t length, void *user_data) size_t length, void *user_data)
{ {
if (!sendf_ok) { ck_assert_msg(sendf_ok, "didn't get resume control");
ck_abort_msg("Didn't get resume control");
}
if (sending_pos != position) { ck_assert_msg(sending_pos == position, "bad position %lu", (unsigned long)position);
ck_abort_msg("Bad position %llu", (unsigned long long)position);
}
if (length == 0) { if (length == 0) {
if (file_sending_done) { ck_assert_msg(!file_sending_done, "file sending already done");
ck_abort_msg("File sending already done.");
}
file_sending_done = 1; file_sending_done = 1;
return; return;
} }
if (position + length > max_sending) { if (position + length > max_sending) {
if (m_send_reached) { ck_assert_msg(!m_send_reached, "requested done file transfer");
ck_abort_msg("Requested done file transfer.");
}
length = max_sending - position; length = max_sending - position;
m_send_reached = 1; m_send_reached = 1;
@ -146,9 +124,9 @@ static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t fi
TOX_ERR_FILE_SEND_CHUNK error; TOX_ERR_FILE_SEND_CHUNK error;
tox_file_send_chunk(tox, friend_number, file_number, position, f_data, length, &error); tox_file_send_chunk(tox, friend_number, file_number, position, f_data, length, &error);
if (error != TOX_ERR_FILE_SEND_CHUNK_OK) {
ck_abort_msg("Could not send chunk, error num=%d pos=%d len=%d", (int)error, (int)position, (int)length); ck_assert_msg(error == TOX_ERR_FILE_SEND_CHUNK_OK,
} "could not send chunk, error num=%d pos=%d len=%d", (int)error, (int)position, (int)length);
++sending_num; ++sending_num;
sending_pos += length; sending_pos += length;
@ -160,9 +138,7 @@ static bool file_recv;
static void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data, static void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data,
size_t length, void *user_data) size_t length, void *user_data)
{ {
if (size_recv != position) { ck_assert_msg(size_recv == position, "bad position");
ck_abort_msg("Bad position");
}
if (length == 0) { if (length == 0) {
file_recv = 1; file_recv = 1;
@ -173,11 +149,9 @@ static void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uin
memset(f_data, num, length); memset(f_data, num, length);
++num; ++num;
if (memcmp(f_data, data, length) == 0) { ck_assert_msg(memcmp(f_data, data, length) == 0, "FILE_CORRUPTED");
size_recv += length;
} else { size_recv += length;
ck_abort_msg("FILE_CORRUPTED");
}
} }
static void file_transfer_test(void) static void file_transfer_test(void)
@ -233,7 +207,7 @@ static void file_transfer_test(void)
file_accepted = file_size = sendf_ok = size_recv = 0; file_accepted = file_size = sendf_ok = size_recv = 0;
file_recv = 0; file_recv = 0;
max_sending = UINT64_MAX; max_sending = UINT64_MAX;
long long unsigned int f_time = time(nullptr); uint64_t f_time = time(nullptr);
tox_callback_file_recv_chunk(tox3, write_file); tox_callback_file_recv_chunk(tox3, write_file);
tox_callback_file_recv_control(tox2, file_print_control); tox_callback_file_recv_control(tox2, file_print_control);
tox_callback_file_chunk_request(tox2, tox_file_chunk_request); tox_callback_file_chunk_request(tox2, tox_file_chunk_request);
@ -260,15 +234,13 @@ static void file_transfer_test(void)
tox_iterate(tox3, nullptr); tox_iterate(tox3, nullptr);
if (file_sending_done) { if (file_sending_done) {
if (sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv ck_assert_msg(sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv
&& file_accepted == 1) { && file_accepted == 1,
break; "Something went wrong in file transfer %u %u %u %u %u %u %lu %lu %lu",
} sendf_ok, file_recv, totalf_size == file_size, size_recv == file_size, sending_pos == size_recv,
file_accepted == 1, (unsigned long)totalf_size, (unsigned long)size_recv,
ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %lu %lu %lu", sendf_ok, file_recv, (unsigned long)sending_pos);
totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, break;
(unsigned long)totalf_size, (unsigned long)size_recv,
(unsigned long)sending_pos);
} }
uint32_t tox1_interval = tox_iteration_interval(tox1); uint32_t tox1_interval = tox_iteration_interval(tox1);
@ -290,7 +262,7 @@ static void file_transfer_test(void)
(unsigned long)totalf_size, (unsigned long)size_recv, (unsigned long)totalf_size, (unsigned long)size_recv,
(unsigned long)sending_pos); (unsigned long)sending_pos);
printf("100MiB file sent in %llu seconds\n", time(nullptr) - f_time); printf("100MiB file sent in %lu seconds\n", (unsigned long)(time(nullptr) - f_time));
printf("Starting file streaming transfer test.\n"); printf("Starting file streaming transfer test.\n");
@ -320,23 +292,11 @@ static void file_transfer_test(void)
max_sending = 100 * 1024; max_sending = 100 * 1024;
m_send_reached = 0; m_send_reached = 0;
while (1) { while (!file_sending_done) {
tox_iterate(tox1, nullptr); tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr); tox_iterate(tox2, nullptr);
tox_iterate(tox3, nullptr); tox_iterate(tox3, nullptr);
if (file_sending_done) {
if (sendf_ok && file_recv && m_send_reached && totalf_size == file_size && size_recv == max_sending
&& sending_pos == size_recv && file_accepted == 1) {
break;
}
ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %u %llu %llu %llu %llu", sendf_ok, file_recv,
m_send_reached, totalf_size == file_size, size_recv == max_sending, sending_pos == size_recv, file_accepted == 1,
(unsigned long long)totalf_size, (unsigned long long)file_size,
(unsigned long long)size_recv, (unsigned long long)sending_pos);
}
uint32_t tox1_interval = tox_iteration_interval(tox1); uint32_t tox1_interval = tox_iteration_interval(tox1);
uint32_t tox2_interval = tox_iteration_interval(tox2); uint32_t tox2_interval = tox_iteration_interval(tox2);
uint32_t tox3_interval = tox_iteration_interval(tox3); uint32_t tox3_interval = tox_iteration_interval(tox3);
@ -344,7 +304,14 @@ static void file_transfer_test(void)
c_sleep(min_u32(tox1_interval, min_u32(tox2_interval, tox3_interval))); c_sleep(min_u32(tox1_interval, min_u32(tox2_interval, tox3_interval)));
} }
printf("Starting file 0 transfer test.\n"); ck_assert_msg(sendf_ok && file_recv && m_send_reached && totalf_size == file_size && size_recv == max_sending
&& sending_pos == size_recv && file_accepted == 1,
"something went wrong in file transfer %u %u %u %u %u %u %u %lu %lu %lu %lu", sendf_ok, file_recv,
m_send_reached, totalf_size == file_size, size_recv == max_sending, sending_pos == size_recv, file_accepted == 1,
(unsigned long)totalf_size, (unsigned long)file_size,
(unsigned long)size_recv, (unsigned long)sending_pos);
printf("starting file 0 transfer test.\n");
file_sending_done = 0; file_sending_done = 0;
file_accepted = 0; file_accepted = 0;
@ -369,30 +336,25 @@ static void file_transfer_test(void)
ck_assert_msg(tox_file_get_file_id(tox2, 0, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id failed"); ck_assert_msg(tox_file_get_file_id(tox2, 0, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id failed");
ck_assert_msg(gfierr == TOX_ERR_FILE_GET_OK, "wrong error"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_OK, "wrong error");
while (1) { while (!file_sending_done) {
tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr);
tox_iterate(tox3, nullptr);
if (file_sending_done) {
if (sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv
&& file_accepted == 1) {
break;
}
ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv,
totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1,
(unsigned long long)totalf_size, (unsigned long long)size_recv,
(unsigned long long)sending_pos);
}
uint32_t tox1_interval = tox_iteration_interval(tox1); uint32_t tox1_interval = tox_iteration_interval(tox1);
uint32_t tox2_interval = tox_iteration_interval(tox2); uint32_t tox2_interval = tox_iteration_interval(tox2);
uint32_t tox3_interval = tox_iteration_interval(tox3); uint32_t tox3_interval = tox_iteration_interval(tox3);
c_sleep(min_u32(tox1_interval, min_u32(tox2_interval, tox3_interval))); c_sleep(min_u32(tox1_interval, min_u32(tox2_interval, tox3_interval)));
tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr);
tox_iterate(tox3, nullptr);
} }
ck_assert_msg(sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size
&& sending_pos == size_recv && file_accepted == 1,
"something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv,
totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1,
(unsigned long long)totalf_size, (unsigned long long)size_recv,
(unsigned long long)sending_pos);
printf("file_transfer_test succeeded, took %llu seconds\n", time(nullptr) - cur_time); printf("file_transfer_test succeeded, took %llu seconds\n", time(nullptr) - cur_time);
tox_kill(tox1); tox_kill(tox1);