test password reading and writing for BADCRYPT_VERSION (requires external file)

This commit is contained in:
Enno Rehling 2016-02-21 10:25:18 +01:00
parent 9ac05666ea
commit dd94771ed8
1 changed files with 41 additions and 0 deletions

View File

@ -5,9 +5,12 @@
#include "unit.h"
#include "faction.h"
#include "version.h"
#include <util/base36.h>
#include <CuTest.h>
#include <tests.h>
#include <storage.h>
#include <stdio.h>
static void test_readwrite_data(CuTest * tc)
@ -42,6 +45,8 @@ static void test_readwrite_unit(CuTest * tc)
sprintf(path, "%s/%s", datapath(), filename);
data = gamedata_open(path, "wb");
CuAssertPtrNotNull(tc, data);
write_unit(data, u);
gamedata_close(data);
@ -49,6 +54,7 @@ static void test_readwrite_unit(CuTest * tc)
f = test_create_faction(0);
renumber_faction(f, fno);
data = gamedata_open(path, "rb");
CuAssertPtrNotNull(tc, data);
u = read_unit(data);
gamedata_close(data);
@ -66,12 +72,46 @@ static void test_read_password(CuTest *tc) {
f = test_create_faction(0);
faction_setpassword(f, "secret");
data = gamedata_open(path, "wb");
CuAssertPtrNotNull(tc, data);
_test_write_password(data, f);
gamedata_close(data);
data = gamedata_open(path, "rb");
CuAssertPtrNotNull(tc, data);
_test_read_password(data, f);
gamedata_close(data);
CuAssertTrue(tc, checkpasswd(f, "secret"));
CuAssertIntEquals(tc, 0, remove(path));
}
static void test_read_password_external(CuTest *tc) {
const char *path = "test.dat", *pwfile = "passwords.txt";
gamedata *data;
faction *f;
FILE * F;
remove(pwfile);
f = test_create_faction(0);
faction_setpassword(f, "secret");
CuAssertPtrNotNull(tc, f->passw);
data = gamedata_open(path, "wb");
CuAssertPtrNotNull(tc, data);
WRITE_TOK(data->store, (const char *)f->passw);
WRITE_TOK(data->store, (const char *)f->passw);
gamedata_close(data);
data = gamedata_open(path, "rb");
CuAssertPtrNotNull(tc, data);
data->version = BADCRYPT_VERSION;
_test_read_password(data, f);
CuAssertPtrEquals(tc, 0, f->passw);
F = fopen(pwfile, "wt");
fprintf(F, "%s:secret\n", itoa36(f->no));
fclose(F);
_test_read_password(data, f);
CuAssertPtrNotNull(tc, f->passw);
gamedata_close(data);
CuAssertTrue(tc, checkpasswd(f, "secret"));
CuAssertIntEquals(tc, 0, remove(path));
CuAssertIntEquals(tc, 0, remove(pwfile));
}
CuSuite *get_save_suite(void)
@ -80,5 +120,6 @@ CuSuite *get_save_suite(void)
SUITE_ADD_TEST(suite, test_readwrite_data);
SUITE_ADD_TEST(suite, test_readwrite_unit);
SUITE_ADD_TEST(suite, test_read_password);
SUITE_ADD_TEST(suite, test_read_password_external);
return suite;
}