Crossfire Server, Trunk
check_account_char.cpp
Go to the documentation of this file.
1 /*
2  * This is the unit tests file for server/account.c
3  */
4 
5 #include "global.h"
6 
7 #include <check.h>
8 #include <stdlib.h>
9 #include <sys/stat.h>
10 
11 #include "account_char.h"
12 #include "loader.h"
13 #include "sproto.h"
14 #include "toolkit_common.h"
15 #include "toolkit_server.h"
16 
17 static void setup(void) {
18 }
19 
20 static void teardown(void) {
21  /* put any cleanup steps here, they will be run after each testcase */
22 }
23 
24 
25 
26 /* This test tries to add some accounts. As a final action, we try
27  * to save the account file out.
28  */
29 START_TEST(test_account_char_add) {
30  player *pl;
31  Account_Chars *chars;
32  char path[MAX_BUF];
33 
34  pl = static_cast<player *>(calloc(1, sizeof(player)));
35  chars = account_char_load("testaccount");
36 
37  /* The account_character code takes a player structure to
38  * fill in the values, so we create a fake one here -
39  * we just fill in the fields that are used.
40  */
41  pl->ob = create_archetype("human_player");
42  pl->ob->level = 1;
43  pl->ob->name = add_string("test character");
44  pl->ob->contr = pl;
45  strcpy(pl->maplevel, "test map");
46 
47  account_char_add(chars, pl);
48  fail_unless(chars->chars != NULL, "account_char_add returned NULL on initial character");
49 
50  pl->ob->level = 2;
51  account_char_add(chars, pl);
52  fail_unless(chars->chars != NULL, "account_char_add returned NULL on update character");
53  fail_unless(chars->chars->next == NULL, "account_char_add added to list, not updated existing entry");
54 
55  account_char_remove(chars, pl->ob->name);
56  fail_unless(chars->chars == NULL, "account_char_remove returned non NULL on final character removal");
57 
58  account_char_add(chars, pl);
59  fail_unless(chars->chars != NULL, "account_char_add returned NULL on initial character");
60 
61  pl->ob->name = add_string("char 2");
62  pl->party = party_form(pl->ob, "rockon");
63 
64  account_char_add(chars, pl);
65  fail_unless(chars->chars != NULL, "account_char_add returned NULL on initial character");
66  fail_unless(chars->chars->next != NULL, "account_char_add did not set next pointer!");
67 
68  sprintf(path,"%s/account", settings.localdir);
69  mkdir(path, S_IRWXU);
70 
71  /* This does not return anything, but this at least checks for
72  * core dumps, etc
73  */
74  account_char_save(chars);
75 
76  /* Like above, this returns nothing but does check for core dumps */
77  account_char_free(chars);
78 }
79 END_TEST
80 
81 /* This tests the load logic. Since the only data were are loading is the data from above,
82  * we use that knowledge to check for proper existence accounts, etc.
83  */
84 START_TEST(test_account_char_load) {
85  Account_Chars *chars;
86  object *ob = create_archetype("human_player");
87 
88  chars = account_char_load("testaccount");
89  fail_unless(chars != NULL, "account_char_load returned NULL");
90  fail_unless(chars->chars != NULL, "account_char_load didn't load the file");
91 
92  /* As of now, the account order is in FIFO order */
93 
94  fail_unless(!strcmp(chars->chars->name, "test character"),
95  "Name for first character is not test char");
96 
97  fail_unless(!strcmp(chars->chars->race, ob->race),
98  "Race for first character does not match");
99 
100  fail_unless(chars->chars->level == 2,
101  "Level for first character is not 2");
102 
103  fail_unless(!strcmp(chars->chars->face, ob->face->name),
104  "Face for first character does not match");
105 
106  fail_unless(chars->chars->party[0] == 0,
107  "Party for first character is not blank");
108 
109  fail_unless(!strcmp(chars->chars->map, "test map"),
110  "Map for first character does not match");
111 
112  fail_unless(chars->chars->next != NULL, "account_char_load only loaded one character");
113 
114  /* The presumption here is that if it loaded the first entry
115  * successfully, so it should the second, but we do check for the fields
116  * which are different.
117  */
118 
119  fail_unless(!strcmp(chars->chars->next->name, "char 2"),
120  "Name for second character does not match");
121 
122  fail_unless(!strcmp(chars->chars->next->party, "rockon"),
123  "Party for second character does not match");
124 
125  account_char_free(chars);
126 }
127 
128 END_TEST
129 
130 START_TEST(test_account_char_load_duplicate) {
131  Account_Chars *first, *second;
132 
133  first = account_char_load("dummy");
134  second = account_char_load("dummy");
135  fail_unless(first == second, "account_char_load should return the same structure for the same name");
136 
137  account_char_free(first);
138  account_char_free(second);
139 }
140 END_TEST
141 
142 static Suite *account_suite(void) {
143  Suite *s = suite_create("account_char");
144  TCase *tc_core = tcase_create("Core");
145 
146  /*setup and teardown will be called before each test in testcase 'tc_core' */
147  tcase_add_checked_fixture(tc_core, setup, teardown);
148 
149  suite_add_tcase(s, tc_core);
150  tcase_add_test(tc_core, test_account_char_add);
151  tcase_add_test(tc_core, test_account_char_load);
152  tcase_add_test(tc_core, test_account_char_load_duplicate);
153 
154  return s;
155 }
156 
157 int main(void) {
158  int nf;
159  Suite *s = account_suite();
160  SRunner *sr = srunner_create(s);
161 
162  /* If you wish to debug the program, uncomment this line. */
163  /*srunner_set_fork_status (sr, CK_NOFORK);*/
164 
166  /* Not sure if there is a better place to put this file - basically,
167  * the account code usings the localdir to determine where to read/write
168  * the accounts file from - we don't want to be altering the real version of
169  * that file.
170  */
171  settings.localdir = strdup_local("/tmp/");
172  cctk_setdatadir(SOURCE_ROOT "lib");
174  init(0, NULL);
175 
176  srunner_set_xml(sr, LOGDIR "/unit/server/account_char.xml");
177  srunner_set_log(sr, LOGDIR "/unit/server/account_char.out");
178  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
179  nf = srunner_ntests_failed(sr);
180  srunner_free(sr);
181 
183  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
184 }
global.h
settings
struct Settings settings
Definition: init.cpp:139
llevError
@ llevError
Definition: logger.h:11
strdup_local
#define strdup_local
Definition: compat.h:29
cctk_setdatadir
void cctk_setdatadir(const char *datadir)
Definition: toolkit_common.cpp:69
account_char_struct::level
uint8_t level
Definition: account_char.h:16
account_char_struct::map
sstring map
Definition: account_char.h:19
pl
Definition: player.h:105
guildjoin.ob
ob
Definition: guildjoin.py:42
pl::ob
object * ob
Definition: player.h:176
account_char_struct::name
sstring name
Definition: account_char.h:13
toolkit_server.h
account_suite
static END_TEST Suite * account_suite(void)
Definition: check_account_char.cpp:142
account_chars_struct
Definition: account_char.h:28
setup
static void setup(void)
Definition: check_account_char.cpp:17
pl::maplevel
char maplevel[MAX_BUF]
Definition: player.h:109
toolkit_common.h
Settings::debug
LogLevel debug
Definition: global.h:243
obj::name
sstring name
Definition: object.h:317
account_char_free
void account_char_free(Account_Chars *chars)
Definition: account_char.cpp:374
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
python_init.path
path
Definition: python_init.py:8
clean_test_account_data
void clean_test_account_data(void)
Definition: toolkit_server.cpp:5
account_char_struct::race
sstring race
Definition: account_char.h:15
account_char_struct::next
struct account_char_struct * next
Definition: account_char.h:21
sproto.h
START_TEST
START_TEST(test_account_char_add)
Definition: check_account_char.cpp:29
party_form
partylist * party_form(object *op, const char *partyname)
Definition: party.cpp:40
MAX_BUF
#define MAX_BUF
Definition: define.h:35
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:279
main
int main(void)
Definition: check_account_char.cpp:157
obj::contr
struct pl * contr
Definition: object.h:282
account_char_struct::party
sstring party
Definition: account_char.h:18
account_chars_struct::chars
Account_Char * chars
Definition: account_char.h:31
init
void init(int argc, char **argv)
Definition: init.cpp:1108
account_char_struct::face
sstring face
Definition: account_char.h:17
account_char_add
void account_char_add(Account_Chars *chars, player *pl)
Definition: account_char.cpp:229
loader.h
account_char.h
account_char_load
Account_Chars * account_char_load(const char *account_name)
Definition: account_char.cpp:147
pl::party
partylist * party
Definition: player.h:202
account_char_save
void account_char_save(Account_Chars *chars)
Definition: account_char.cpp:179
altar_valkyrie.pl
pl
Definition: altar_valkyrie.py:28
teardown
static void teardown(void)
Definition: check_account_char.cpp:20
account_char_remove
void account_char_remove(Account_Chars *chars, const char *pl_name)
Definition: account_char.cpp:336
obj::level
int16_t level
Definition: object.h:359
Settings::localdir
const char * localdir
Definition: global.h:249