From 9ac05666eac58d136337b9646fdf14c5a2c9f042 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 21 Feb 2016 10:10:26 +0100 Subject: [PATCH] add a test for succesful read/write of password TODO: do this for different datafile versions (BADCRYPT, etc) --- src/kernel/save.c | 33 ++++++++++++++++++++++++++------- src/kernel/save.h | 3 +++ src/kernel/save.test.c | 16 ++++++++++++++++ src/kernel/version.h | 4 ++-- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/kernel/save.c b/src/kernel/save.c index e6ef8b8ce..40ff63f3d 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1176,6 +1176,30 @@ static char * getpasswd(int fno) { } return NULL; } + +static void read_password(gamedata *data, faction *f) { + char name[128]; + READ_STR(data->store, name, sizeof(name)); + if (data->version == BADCRYPT_VERSION) { + f->passw = getpasswd(f->no); + } + else { + f->passw = _strdup(name); + } +} + +void _test_read_password(gamedata *data, faction *f) { + read_password(data, f); +} + +static void write_password(gamedata *data, const faction *f) { + WRITE_TOK(data->store, (const char *)f->passw); +} + +void _test_write_password(gamedata *data, const faction *f) { + write_password(data, f); +} + /** Reads a faction from a file. * This function requires no context, can be called in any state. The * faction may not already exist, however. @@ -1242,12 +1266,7 @@ faction *readfaction(struct gamedata * data) set_email(&f->email, ""); } - READ_STR(data->store, name, sizeof(name)); - if (data->version < CRYPT_VERSION) { - f->passw = _strdup(name); - } else { - f->passw = getpasswd(f->no); - } + read_password(data, f); if (data->version < NOOVERRIDE_VERSION) { READ_STR(data->store, 0, 0); } @@ -1354,7 +1373,7 @@ void writefaction(struct gamedata *data, const faction * f) WRITE_STR(data->store, (const char *)f->name); WRITE_STR(data->store, (const char *)f->banner); WRITE_STR(data->store, f->email); - WRITE_TOK(data->store, (const char *)f->passw); + write_password(data, f); WRITE_TOK(data->store, locale_name(f->locale)); WRITE_INT(data->store, f->lastorders); WRITE_INT(data->store, f->age); diff --git a/src/kernel/save.h b/src/kernel/save.h index ca364fc9c..cedd5befc 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -83,6 +83,9 @@ extern "C" { struct gamedata *gamedata_open(const char *filename, const char *mode); void gamedata_close(struct gamedata *data); + /* test-only functions that give access to internal implementation details (BAD) */ + void _test_write_password(struct gamedata *data, const struct faction *f); + void _test_read_password(struct gamedata *data, struct faction *f); #ifdef __cplusplus } #endif diff --git a/src/kernel/save.test.c b/src/kernel/save.test.c index 403eaa059..7e68079f2 100644 --- a/src/kernel/save.test.c +++ b/src/kernel/save.test.c @@ -59,10 +59,26 @@ static void test_readwrite_unit(CuTest * tc) test_cleanup(); } +static void test_read_password(CuTest *tc) { + const char *path = "test.dat"; + gamedata *data; + faction *f; + f = test_create_faction(0); + faction_setpassword(f, "secret"); + data = gamedata_open(path, "wb"); + _test_write_password(data, f); + gamedata_close(data); + data = gamedata_open(path, "rb"); + _test_read_password(data, f); + gamedata_close(data); + CuAssertTrue(tc, checkpasswd(f, "secret")); +} + CuSuite *get_save_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_readwrite_data); SUITE_ADD_TEST(suite, test_readwrite_unit); + SUITE_ADD_TEST(suite, test_read_password); return suite; } diff --git a/src/kernel/version.h b/src/kernel/version.h index a7a913099..c614ccebe 100644 --- a/src/kernel/version.h +++ b/src/kernel/version.h @@ -33,9 +33,9 @@ #define SPELL_LEVEL_VERSION 348 /* f->max_spelllevel gets stored, not calculated */ #define OWNER_3_VERSION 349 /* regions store last owner, not last alliance */ #define ATTRIBOWNER_VERSION 350 /* all attrib_type functions know who owns the attribute */ -#define CRYPT_VERSION 351 /* passwords are encrypted */ +#define BADCRYPT_VERSION 351 /* passwords are encrypted */ #define RELEASE_VERSION ATTRIBOWNER_VERSION /* current datafile */ #define MIN_VERSION INTPAK_VERSION /* minimal datafile we support */ -#define MAX_VERSION CRYPT_VERSION /* change this if we can need to read the future datafile, and we can do so */ +#define MAX_VERSION BADCRYPT_VERSION /* change this if we can need to read the future datafile, and we can do so */ #define STREAM_VERSION 2 /* internal encoding of binary files */