Skip to content

Commit

Permalink
More fixed_width ints and incorporating file_saving_test.c
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hugbubby committed Jul 20, 2018
1 parent a672b3c commit 3917c41
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 156 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -417,6 +417,7 @@ auto_test(crypto MSVC_DONT_BUILD)
auto_test(dht MSVC_DONT_BUILD)
auto_test(encryptsave)
auto_test(file_transfer)
auto_test(file_saving)
auto_test(friend_connection)
auto_test(friend_request)
auto_test(invalid_proxy)
Expand Down
49 changes: 14 additions & 35 deletions auto_tests/encryptsave_test.c
Expand Up @@ -43,22 +43,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()
{
unsigned char out[CRYPTO_SHARED_KEY_SIZE];
int res = crypto_pwhash_scryptsalsa208sha256(out,
CRYPTO_SHARED_KEY_SIZE,
pw,
pwlen,
test_salt,
crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE * 8,
crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE);
int16_t res = crypto_pwhash_scryptsalsa208sha256(out,
CRYPTO_SHARED_KEY_SIZE,
pw,
pwlen,
test_salt,
crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE * 8,
crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE);
ck_assert_msg(res != -1, "crypto function failed");
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()
{
Tox *tox1 = tox_new_log(nullptr, nullptr, nullptr);
Tox *tox2 = tox_new_log(nullptr, nullptr, nullptr);
Expand Down Expand Up @@ -140,9 +139,8 @@ START_TEST(test_save_friend)
tox_kill(tox3);
tox_kill(tox4);
}
END_TEST

START_TEST(test_keys)
static void test_keys()
{
TOX_ERR_ENCRYPTION encerr;
TOX_ERR_DECRYPTION decerr;
Expand Down Expand Up @@ -191,32 +189,13 @@ START_TEST(test_keys)
tox_pass_key_free(key2);
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)
{
setvbuf(stdout, nullptr, _IONBF, 0);
srand((unsigned int) time(nullptr));

Suite *encryptsave = encryptsave_suite();
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);
test_known_kdf();
test_save_friend();
test_keys();

return number_failed;
return 0;
}
36 changes: 12 additions & 24 deletions auto_tests/file_saving_test.c
Expand Up @@ -27,6 +27,7 @@
#include <stdlib.h>
#include <string.h>

#include "check_compat.h"
#include "helpers.h"

#include "../toxencryptsave/toxencryptsave.h"
Expand Down Expand Up @@ -54,11 +55,9 @@ static void save_data_encrypted(void)

TOX_ERR_ENCRYPTION eerr;

if (!tox_pass_encrypt(clear, size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)pphrase, strlen(pphrase), cipher,
&eerr)) {
fprintf(stderr, "error: could not encrypt, error code %d\n", eerr);
exit(4);
}
ck_assert_msg(tox_pass_encrypt(clear, size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)pphrase,
strlen(pphrase), cipher,
&eerr), "Could not encrypt, error code %d.", eerr);

size_t written_value = fwrite(cipher, sizeof(*cipher), size, f);
printf("written written_value = %u of %u\n", (unsigned)written_value, (unsigned)size);
Expand All @@ -73,20 +72,18 @@ static void load_data_decrypted(void)
{
FILE *f = fopen(savefile, "r");
fseek(f, 0, SEEK_END);
long size = ftell(f);
int64_t size = ftell(f);
fseek(f, 0, SEEK_SET);

uint8_t *cipher = (uint8_t *)malloc(size);
uint8_t *clear = (uint8_t *)malloc(size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
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", (uint8_t)read_value, size);

TOX_ERR_DECRYPTION derr;

if (!tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr)) {
fprintf(stderr, "error: could not decrypt, error code %d\n", derr);
exit(3);
}
ck_assert_msg(tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr),
"Could not decrypt, error code %d.", derr);

struct Tox_Options *options = tox_options_new(nullptr);

Expand All @@ -100,19 +97,14 @@ static void load_data_decrypted(void)

tox_options_free(options);

if (t == nullptr) {
fprintf(stderr, "error: tox_new returned the error value %d\n", err);
return;
}
ck_assert_msg(t != nullptr, "tox_new returned the error value %d", err);

uint8_t readname[TOX_MAX_NAME_LENGTH];
tox_self_get_name(t, readname);
readname[tox_self_get_name_size(t)] = '\0';

if (strcmp((const char *)readname, name)) {
fprintf(stderr, "error: name returned by tox_self_get_name does not match expected result\n");
exit(2);
}
ck_assert_msg(strcmp((const char *)readname, name) == 0,
"name returned by tox_self_get_name does not match expected result");

free(cipher);
free(clear);
Expand All @@ -126,11 +118,7 @@ int main(void)
save_data_encrypted();
load_data_decrypted();

int ret = remove(savefile);

if (ret != 0) {
fprintf(stderr, "error: could not remove savefile\n");
}
ck_assert_msg(remove(savefile) == 0, "Could not remove the savefile.");

return 0;
}

0 comments on commit 3917c41

Please sign in to comment.