add a test for succesful read/write of password

TODO: do this for different datafile versions (BADCRYPT, etc)
This commit is contained in:
Enno Rehling 2016-02-21 10:10:26 +01:00
parent 2094c4964c
commit 9ac05666ea
4 changed files with 47 additions and 9 deletions

View File

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

View File

@ -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

View File

@ -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;
}

View File

@ -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 */