Debian Codegraph @

test/unit/server/check_account_char.c

raw symbols SCIP
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_Char *chars;
32 char path[MAX_BUF];
33
34 pl = calloc(1, sizeof(player));
35
36 /* The account_character code takes a player structure to
37 * fill in the values, so we create a fake one here -
38 * we just fill in the fields that are used.
39 */
40 pl->ob = create_archetype("human_player");
41 pl->ob->level = 1;
42 pl->ob->name = add_string("test character");
43 pl->ob->contr = pl;
44 strcpy(pl->maplevel, "test map");
45
46 chars = account_char_add(NULL, pl);
47 fail_unless(chars != NULL, "account_char_add returned NULL on initial character");
48
49 pl->ob->level = 2;
50 chars = account_char_add(chars, pl);
51 fail_unless(chars != NULL, "account_char_add returned NULL on update character");
52 fail_unless(chars->next == NULL, "account_char_add added to list, not updated existing entry");
53
54 chars = account_char_remove(chars, pl->ob->name);
55 fail_unless(chars == NULL, "account_char_remove returned non NULL on final character removal");
56
57 chars = account_char_add(NULL, pl);
58 fail_unless(chars != NULL, "account_char_add returned NULL on initial character");
59
60 pl->ob->name = add_string("char 2");
61 pl->party = party_form(pl->ob, "rockon");
62
63 chars = account_char_add(chars, pl);
64 fail_unless(chars != NULL, "account_char_add returned NULL on initial character");
65 fail_unless(chars->next != NULL, "account_char_add did not set next pointer!");
66
67 sprintf(path,"%s/account", settings.localdir);
68 mkdir(path, S_IRWXU);
69
70 /* This does not return anything, but this at least checks for
71 * core dumps, etc
72 */
73 account_char_save("testaccount", chars);
74
75 /* Like above, this returns nothing but does check for core dumps */
76 account_char_free(chars);
77 }
78 END_TEST
79
80 /* This tests the load logic. Since the only data were are loading is the data from above,
81 * we use that knowledge to check for proper existence accounts, etc.
82 */
83 START_TEST(test_account_char_load) {
84 Account_Char *chars;
85 object *ob = create_archetype("human_player");
86
87 chars = account_char_load("testaccount");
88 fail_unless(chars != NULL, "account_char_load returned NULL");
89
90 /* As of now, the account order is in FIFO order */
91
92 fail_unless(!strcmp(chars->name, "test character"),
93 "Name for first character is not test char");
94
95 fail_unless(!strcmp(chars->race, ob->race),
96 "Race for first character does not match");
97
98 fail_unless(chars->level == 2,
99 "Level for first character is not 2");
100
101 fail_unless(!strcmp(chars->face, ob->face->name),
102 "Face for first character does not match");
103
104 fail_unless(chars->party[0] == 0,
105 "Party for first character is not blank");
106
107 fail_unless(!strcmp(chars->map, "test map"),
108 "Map for first character does not match");
109
110 fail_unless(chars->next != NULL, "account_char_load only loaded one character");
111
112 /* The presumption here is that if it loaded the first entry
113 * successfully, so it should the second, but we do check for the fields
114 * which are different.
115 */
116 chars = chars->next;
117
118 fail_unless(!strcmp(chars->name, "char 2"),
119 "Name for second character does not match");
120
121 fail_unless(!strcmp(chars->party, "rockon"),
122 "Party for second character does not match");
123 }
124
125 END_TEST
126
127 static Suite *account_suite(void) {
128 Suite *s = suite_create("account_char");
129 TCase *tc_core = tcase_create("Core");
130
131 /*setup and teardown will be called before each test in testcase 'tc_core' */
132 tcase_add_checked_fixture(tc_core, setup, teardown);
133
134 suite_add_tcase(s, tc_core);
135 tcase_add_test(tc_core, test_account_char_add);
136 tcase_add_test(tc_core, test_account_char_load);
137
138 return s;
139 }
140
141 int main(void) {
142 int nf;
143 Suite *s = account_suite();
144 SRunner *sr = srunner_create(s);
145
146 /* If you wish to debug the program, uncomment this line. */
147 /*srunner_set_fork_status (sr, CK_NOFORK);*/
148
149 settings.debug = 0;
150 /* Not sure if there is a better place to put this file - basically,
151 * the account code usings the localdir to determine where to read/write
152 * the accounts file from - we don't want to be altering the real version of
153 * that file.
154 */
155 settings.localdir = strdup_local("/tmp/");
156 clean_test_account_data();
157 init(0, NULL);
158
159 srunner_set_xml(sr, LOGDIR "/unit/server/account_char.xml");
160 srunner_set_log(sr, LOGDIR "/unit/server/account_char.out");
161 srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
162 nf = srunner_ntests_failed(sr);
163 srunner_free(sr);
164
165 clean_test_account_data();
166 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
167 }