2017-10-07 20:17:04 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
|
|
|
|
|
|
|
#include "orderfile.h"
|
|
|
|
|
2018-12-02 15:51:35 +01:00
|
|
|
#include "direction.h"
|
|
|
|
|
2018-02-14 20:00:48 +01:00
|
|
|
#include <kernel/calendar.h>
|
2017-10-07 20:17:04 +02:00
|
|
|
#include <kernel/faction.h>
|
2018-12-02 15:51:35 +01:00
|
|
|
#include <kernel/order.h>
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
|
|
|
|
#include <util/base36.h>
|
|
|
|
#include <util/keyword.h>
|
|
|
|
#include <util/language.h>
|
|
|
|
#include <util/lists.h>
|
2018-01-13 08:51:40 +01:00
|
|
|
#include <util/message.h>
|
2018-12-02 15:51:35 +01:00
|
|
|
#include <util/param.h>
|
|
|
|
#include <util/password.h>
|
2017-10-07 20:17:04 +02:00
|
|
|
|
|
|
|
#include <CuTest.h>
|
|
|
|
#include <tests.h>
|
|
|
|
|
|
|
|
static const char *getbuf_null(void *data)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_read_orders(CuTest *tc) {
|
|
|
|
input in;
|
|
|
|
test_setup();
|
|
|
|
in.getbuf = getbuf_null;
|
|
|
|
in.data = NULL;
|
|
|
|
CuAssertIntEquals(tc, 0, read_orders(&in));
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2017-10-07 20:17:04 +02:00
|
|
|
}
|
|
|
|
|
2018-12-02 15:51:35 +01:00
|
|
|
static const char *getbuf_strings(void *data)
|
|
|
|
{
|
|
|
|
strlist **listp = (strlist **)data;
|
|
|
|
if (listp && *listp) {
|
|
|
|
strlist *list = *listp;
|
|
|
|
*listp = list->next;
|
|
|
|
return list->s;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_unit_orders(CuTest *tc) {
|
|
|
|
input in;
|
|
|
|
unit *u;
|
|
|
|
faction *f;
|
2018-12-02 15:53:21 +01:00
|
|
|
strlist *orders = NULL, *backup;
|
2018-12-02 15:51:35 +01:00
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
test_setup();
|
|
|
|
u = test_create_unit(f = test_create_faction(NULL), test_create_plain(0, 0));
|
|
|
|
f->locale = test_create_locale();
|
|
|
|
u->orders = create_order(K_ENTERTAIN, f->locale, NULL);
|
|
|
|
faction_setpassword(f, password_hash("password", PASSWORD_DEFAULT));
|
|
|
|
snprintf(buf, sizeof(buf), "%s %s %s",
|
|
|
|
LOC(f->locale, parameters[P_FACTION]), itoa36(f->no), "password");
|
|
|
|
addstrlist(&orders, buf);
|
|
|
|
snprintf(buf, sizeof(buf), "%s %s",
|
|
|
|
LOC(f->locale, parameters[P_UNIT]), itoa36(u->no));
|
|
|
|
addstrlist(&orders, buf);
|
|
|
|
snprintf(buf, sizeof(buf), "%s %s", keyword_name(K_MOVE, f->locale),
|
|
|
|
LOC(f->locale, shortdirections[D_WEST]));
|
2018-12-02 15:53:21 +01:00
|
|
|
backup = orders;
|
2018-12-02 15:51:35 +01:00
|
|
|
addstrlist(&orders, buf);
|
|
|
|
in.data = &orders;
|
|
|
|
in.getbuf = getbuf_strings;
|
|
|
|
CuAssertIntEquals(tc, 0, read_orders(&in));
|
|
|
|
CuAssertPtrNotNull(tc, u->old_orders);
|
|
|
|
CuAssertPtrNotNull(tc, u->orders);
|
2018-12-02 15:53:21 +01:00
|
|
|
CuAssertPtrEquals(tc, NULL, orders);
|
2018-12-02 15:51:35 +01:00
|
|
|
CuAssertIntEquals(tc, K_MOVE, getkeyword(u->orders));
|
|
|
|
CuAssertIntEquals(tc, K_ENTERTAIN, getkeyword(u->old_orders));
|
|
|
|
CuAssertIntEquals(tc, UFL_ORDERS, u->flags & UFL_ORDERS);
|
2018-12-02 15:53:21 +01:00
|
|
|
freestrlist(backup);
|
2018-12-02 15:51:35 +01:00
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:17:04 +02:00
|
|
|
typedef struct order_list {
|
2017-10-07 20:25:07 +02:00
|
|
|
const char **orders;
|
2017-10-07 20:17:04 +02:00
|
|
|
int next;
|
|
|
|
} order_list;
|
|
|
|
|
|
|
|
static const char *getbuf_list(void *data)
|
|
|
|
{
|
|
|
|
order_list * olist = (order_list *)data;
|
|
|
|
return olist->orders[olist->next++];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_faction_password_okay(CuTest *tc) {
|
|
|
|
input in;
|
|
|
|
faction *f;
|
|
|
|
order_list olist;
|
2017-10-07 20:25:07 +02:00
|
|
|
const char *orders[] = { "ERESSEA 1 password", NULL };
|
2017-10-07 20:17:04 +02:00
|
|
|
|
|
|
|
test_setup();
|
|
|
|
f = test_create_faction(NULL);
|
|
|
|
renumber_faction(f, 1);
|
|
|
|
CuAssertIntEquals(tc, 1, f->no);
|
|
|
|
faction_setpassword(f, "password");
|
|
|
|
f->lastorders = turn - 1;
|
2017-10-07 20:25:07 +02:00
|
|
|
olist.orders = orders;
|
2017-10-07 20:17:04 +02:00
|
|
|
olist.next = 0;
|
|
|
|
in.getbuf = getbuf_list;
|
|
|
|
in.data = &olist;
|
|
|
|
CuAssertIntEquals(tc, 0, read_orders(&in));
|
|
|
|
CuAssertIntEquals(tc, 2, olist.next);
|
|
|
|
CuAssertIntEquals(tc, turn, f->lastorders);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2017-10-07 20:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_faction_password_bad(CuTest *tc) {
|
|
|
|
input in;
|
|
|
|
faction *f;
|
|
|
|
order_list olist;
|
2017-10-07 20:25:07 +02:00
|
|
|
const char *orders[] = { "ERESSEA 1 password", NULL };
|
2017-10-07 20:17:04 +02:00
|
|
|
|
|
|
|
test_setup();
|
2018-05-18 19:58:49 +02:00
|
|
|
mt_create_va(mt_new("wrongpasswd", NULL), "password:string", MT_NEW_END);
|
2018-01-13 08:51:40 +01:00
|
|
|
|
2017-10-07 20:17:04 +02:00
|
|
|
f = test_create_faction(NULL);
|
|
|
|
renumber_faction(f, 1);
|
|
|
|
CuAssertIntEquals(tc, 1, f->no);
|
2017-10-07 20:25:07 +02:00
|
|
|
faction_setpassword(f, "patzword");
|
2017-10-07 20:17:04 +02:00
|
|
|
f->lastorders = turn - 1;
|
2017-10-07 20:25:07 +02:00
|
|
|
olist.orders = orders;
|
2017-10-07 20:17:04 +02:00
|
|
|
olist.next = 0;
|
|
|
|
in.getbuf = getbuf_list;
|
|
|
|
in.data = &olist;
|
|
|
|
CuAssertIntEquals(tc, 0, read_orders(&in));
|
|
|
|
CuAssertIntEquals(tc, 2, olist.next);
|
|
|
|
CuAssertIntEquals(tc, turn - 1, f->lastorders);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2017-10-07 20:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CuSuite *get_orderfile_suite(void)
|
|
|
|
{
|
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_read_orders);
|
2018-12-02 15:51:35 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_unit_orders);
|
2017-10-07 20:17:04 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_faction_password_okay);
|
|
|
|
SUITE_ADD_TEST(suite, test_faction_password_bad);
|
|
|
|
|
|
|
|
return suite;
|
|
|
|
}
|