Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More fixed_width ints and incorporating file_saving_test.c #1025

Merged
merged 1 commit into from Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -414,6 +414,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
48 changes: 14 additions & 34 deletions auto_tests/encryptsave_test.c
Expand Up @@ -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];
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(void)
{
Tox *tox1 = tox_new_log(nullptr, nullptr, nullptr);
Tox *tox2 = tox_new_log(nullptr, nullptr, nullptr);
Expand Down Expand Up @@ -137,9 +136,8 @@ START_TEST(test_save_friend)
tox_kill(tox3);
tox_kill(tox4);
}
END_TEST

START_TEST(test_keys)
static void test_keys(void)
{
TOX_ERR_ENCRYPTION encerr;
TOX_ERR_DECRYPTION decerr;
Expand Down Expand Up @@ -188,31 +186,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);
test_known_kdf();
test_save_friend();
test_keys();

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);

return number_failed;
return 0;
}
37 changes: 13 additions & 24 deletions auto_tests/file_saving_test.c
Expand Up @@ -33,6 +33,8 @@

#include "../testing/misc_tools.h"
#include "../toxcore/ccompat.h"
#include "check_compat.h"

#include "../toxencryptsave/toxencryptsave.h"

static const char *pphrase = "bar";
Expand Down Expand Up @@ -60,11 +62,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 @@ -79,20 +79,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", (unsigned)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 @@ -106,19 +104,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 @@ -132,11 +125,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;
}