#include #include #include #include #include #include "save.h" #include "unit.h" #include "group.h" #include "ally.h" #include "faction.h" #include "plane.h" #include "region.h" #include "version.h" #include #include #include #include #include #include #include #include #include #include #include #include #include static void test_readwrite_data(CuTest * tc) { const char *filename = "test.dat"; char path[MAX_PATH]; test_cleanup(); CuAssertIntEquals(tc, 0, writegame(filename)); CuAssertIntEquals(tc, 0, readgame(filename, false)); join_path(datapath(), filename, path, sizeof(path)); CuAssertIntEquals(tc, 0, remove(path)); test_cleanup(); } static void test_readwrite_unit(CuTest * tc) { gamedata data; storage store; struct unit *u; struct region *r; struct faction *f; int fno; test_cleanup(); r = test_create_region(0, 0, 0); f = test_create_faction(0); fno = f->no; u = test_create_unit(f, r); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); write_unit(&data, u); data.strm.api->rewind(data.strm.handle); free_gamedata(); f = test_create_faction(0); renumber_faction(f, fno); gamedata_init(&data, &store, RELEASE_VERSION); u = read_unit(&data); mstream_done(&data.strm); gamedata_done(&data); CuAssertPtrNotNull(tc, u); CuAssertPtrEquals(tc, f, u->faction); CuAssertPtrEquals(tc, 0, u->region); test_cleanup(); } static void test_readwrite_attrib(CuTest *tc) { gamedata data; storage store; attrib *a = NULL; test_cleanup(); key_set(&a, 41); key_set(&a, 42); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); write_attribs(data.store, a, NULL); a_removeall(&a, NULL); CuAssertPtrEquals(tc, 0, a); data.strm.api->rewind(data.strm.handle); read_attribs(&data, &a, NULL); mstream_done(&data.strm); gamedata_done(&data); CuAssertTrue(tc, key_get(a, 41)); CuAssertTrue(tc, key_get(a, 42)); a_removeall(&a, NULL); test_cleanup(); } static void test_readwrite_dead_faction_group(CuTest *tc) { faction *f, *f2; unit * u; group *g; ally *al; int fno; gamedata data; storage store; mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); test_cleanup(); f = test_create_faction(0); fno = f->no; CuAssertPtrEquals(tc, f, factions); CuAssertPtrEquals(tc, 0, f->next); f2 = test_create_faction(0); CuAssertPtrEquals(tc, f2, factions->next); u = test_create_unit(f2, test_create_region(0, 0, 0)); CuAssertPtrNotNull(tc, u); g = join_group(u, "group"); CuAssertPtrNotNull(tc, g); al = ally_add(&g->allies, f); CuAssertPtrNotNull(tc, al); CuAssertPtrEquals(tc, f, factions); destroyfaction(&factions); CuAssertTrue(tc, !f->_alive); CuAssertPtrEquals(tc, f2, factions); write_game(&data); free_gamedata(); f = f2 = NULL; data.strm.api->rewind(data.strm.handle); read_game(&data); CuAssertPtrEquals(tc, 0, findfaction(fno)); f2 = factions; CuAssertPtrNotNull(tc, f2); u = f2->units; CuAssertPtrNotNull(tc, u); g = get_group(u); CuAssertPtrNotNull(tc, g); CuAssertPtrEquals(tc, 0, g->allies); mstream_done(&data.strm); gamedata_done(&data); test_cleanup(); } static void test_readwrite_dead_faction_regionowner(CuTest *tc) { faction *f; region *r; gamedata data; storage store; mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); test_cleanup(); config_set("rules.region_owners", "1"); f = test_create_faction(0); test_create_unit(f, r = test_create_region(0, 0, 0)); region_set_owner(r, f, turn); destroyfaction(&factions); CuAssertTrue(tc, !f->_alive); remove_empty_units(); write_game(&data); free_gamedata(); f = NULL; data.strm.api->rewind(data.strm.handle); read_game(&data); mstream_done(&data.strm); gamedata_done(&data); f = factions; CuAssertPtrEquals(tc, 0, f); r = regions; CuAssertPtrNotNull(tc, r); CuAssertPtrEquals(tc, 0, region_get_owner(r)); test_cleanup(); } static void test_readwrite_dead_faction_changefaction(CuTest *tc) { gamedata data; storage store; faction *f, *f2; region *r; trigger *tr; unit * u; test_cleanup(); f = test_create_faction(0); f2 = test_create_faction(0); u = test_create_unit(f2, r = test_create_region(0, 0, 0)); tr = trigger_changefaction(u, f); add_trigger(&u->attribs, "timer", trigger_timeout(10, tr)); CuAssertPtrNotNull(tc, a_find(u->attribs, &at_eventhandler)); destroyfaction(&factions); CuAssertTrue(tc, !f->_alive); remove_empty_units(); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); write_game(&data); free_gamedata(); f = NULL; data.strm.api->rewind(data.strm.handle); read_game(&data); mstream_done(&data.strm); gamedata_done(&data); f = factions; CuAssertPtrNotNull(tc, f); r = regions; CuAssertPtrNotNull(tc, r); u = r->units; CuAssertPtrNotNull(tc, u); CuAssertPtrEquals(tc, 0, a_find(u->attribs, &at_eventhandler)); test_cleanup(); } static void test_readwrite_dead_faction_createunit(CuTest *tc) { gamedata data; storage store; faction *f, *f2; region *r; trigger *tr; unit * u; test_cleanup(); f = test_create_faction(0); f2 = test_create_faction(0); u = test_create_unit(f2, r = test_create_region(0, 0, 0)); tr = trigger_createunit(r, f, f->race, 1); add_trigger(&u->attribs, "timer", trigger_timeout(10, tr)); CuAssertPtrNotNull(tc, a_find(u->attribs, &at_eventhandler)); destroyfaction(&factions); CuAssertTrue(tc, !f->_alive); remove_empty_units(); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); write_game(&data); free_gamedata(); f = NULL; data.strm.api->rewind(data.strm.handle); read_game(&data); mstream_done(&data.strm); gamedata_done(&data); f = factions; CuAssertPtrNotNull(tc, f); r = regions; CuAssertPtrNotNull(tc, r); u = r->units; CuAssertPtrNotNull(tc, u); CuAssertPtrEquals(tc, 0, a_find(u->attribs, &at_eventhandler)); test_cleanup(); } static void test_read_password(CuTest *tc) { gamedata data; storage store; faction *f; f = test_create_faction(0); faction_setpassword(f, password_encode("secret", PASSWORD_DEFAULT)); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); _test_write_password(&data, f); data.strm.api->rewind(data.strm.handle); _test_read_password(&data, f); mstream_done(&data.strm); gamedata_done(&data); CuAssertTrue(tc, checkpasswd(f, "secret")); } static void test_read_password_external(CuTest *tc) { gamedata data; storage store; const char *pwfile = "passwords.txt"; faction *f; FILE * F; remove(pwfile); f = test_create_faction(0); faction_setpassword(f, password_encode("secret", PASSWORD_DEFAULT)); CuAssertPtrNotNull(tc, f->_password); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); WRITE_TOK(data.store, (const char *)f->_password); WRITE_TOK(data.store, (const char *)f->_password); data.strm.api->rewind(data.strm.handle); data.version = BADCRYPT_VERSION; _test_read_password(&data, f); CuAssertPtrEquals(tc, 0, f->_password); F = fopen(pwfile, "wt"); fprintf(F, "%s:secret\n", itoa36(f->no)); fclose(F); _test_read_password(&data, f); CuAssertPtrNotNull(tc, f->_password); mstream_done(&data.strm); gamedata_done(&data); CuAssertTrue(tc, checkpasswd(f, "secret")); CuAssertIntEquals(tc, 0, remove(pwfile)); } CuSuite *get_save_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_readwrite_attrib); SUITE_ADD_TEST(suite, test_readwrite_data); SUITE_ADD_TEST(suite, test_readwrite_unit); SUITE_ADD_TEST(suite, test_readwrite_dead_faction_createunit); SUITE_ADD_TEST(suite, test_readwrite_dead_faction_changefaction); SUITE_ADD_TEST(suite, test_readwrite_dead_faction_regionowner); DISABLE_TEST(suite, test_readwrite_dead_faction_group); SUITE_ADD_TEST(suite, test_read_password); SUITE_ADD_TEST(suite, test_read_password_external); return suite; }