Crossfire Server, Trunk  R20513
check_account_char.c
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 
16 static void setup(void) {
17 }
18 
19 static void teardown(void) {
20  /* put any cleanup steps here, they will be run after each testcase */
21 }
22 
23 
24 
25 /* This test tries to add some accounts. As a final action, we try
26  * to save the account file out.
27  */
28 START_TEST(test_account_char_add) {
29  player *pl;
30  Account_Char *chars;
31  char path[MAX_BUF];
32 
33  pl = calloc(1, sizeof(player));
34 
35  /* The account_character code takes a player structure to
36  * fill in the values, so we create a fake one here -
37  * we just fill in the fields that are used.
38  */
39  pl->ob = create_archetype("human_player");
40  pl->ob->level = 1;
41  pl->ob->name = add_string("test character");
42  pl->ob->contr = pl;
43  strcpy(pl->maplevel, "test map");
44 
45  chars = account_char_add(NULL, pl);
46  fail_unless(chars != NULL, "account_char_add returned NULL on initial character");
47 
48  pl->ob->level = 2;
49  chars = account_char_add(chars, pl);
50  fail_unless(chars != NULL, "account_char_add returned NULL on update character");
51  fail_unless(chars->next == NULL, "account_char_add added to list, not updated existing entry");
52 
53  chars = account_char_remove(chars, pl->ob->name);
54  fail_unless(chars == NULL, "account_char_remove returned non NULL on final character removal");
55 
56  chars = account_char_add(NULL, pl);
57  fail_unless(chars != NULL, "account_char_add returned NULL on initial character");
58 
59  pl->ob->name = add_string("char 2");
60  pl->party = party_form(pl->ob, "rockon");
61 
62  chars = account_char_add(chars, pl);
63  fail_unless(chars != NULL, "account_char_add returned NULL on initial character");
64  fail_unless(chars->next != NULL, "account_char_add did not set next pointer!");
65 
66  sprintf(path,"%s/account", settings.localdir);
67  mkdir(path, S_IRWXU);
68 
69  /* This does not return anything, but this at least checks for
70  * core dumps, etc
71  */
72  account_char_save("testaccount", chars);
73 
74  /* Like above, this returns nothing but does check for core dumps */
75  account_char_free(chars);
76 }
77 END_TEST
78 
79 /* This tests the load logic. Since the only data were are loading is the data from above,
80  * we use that knowledge to check for proper existence accounts, etc.
81  */
82 START_TEST(test_account_char_load) {
83  Account_Char *chars;
84  object *ob = create_archetype("human_player");
85 
86  chars = account_char_load("testaccount");
87  fail_unless(chars != NULL, "account_char_load returned NULL");
88 
89  /* As of now, the account order is in FIFO order */
90 
91  fail_unless(!strcmp(chars->name, "test character"),
92  "Name for first character is not test char");
93 
94  fail_unless(!strcmp(chars->race, ob->race),
95  "Race for first character does not match");
96 
97  fail_unless(chars->level == 2,
98  "Level for first character is not 2");
99 
100  fail_unless(!strcmp(chars->face, ob->face->name),
101  "Face for first character does not match");
102 
103  fail_unless(chars->party[0] == 0,
104  "Party for first character is not blank");
105 
106  fail_unless(!strcmp(chars->map, "test map"),
107  "Map for first character does not match");
108 
109  fail_unless(chars->next != NULL, "account_char_load only loaded one character");
110 
111  /* The presumption here is that if it loaded the first entry
112  * successfully, so it should the second, but we do check for the fields
113  * which are different.
114  */
115  chars = chars->next;
116 
117  fail_unless(!strcmp(chars->name, "char 2"),
118  "Name for second character does not match");
119 
120  fail_unless(!strcmp(chars->party, "rockon"),
121  "Party for second character does not match");
122 }
123 
124 END_TEST
125 
126 static Suite *account_suite(void) {
127  Suite *s = suite_create("account_char");
128  TCase *tc_core = tcase_create("Core");
129 
130  /*setup and teardown will be called before each test in testcase 'tc_core' */
131  tcase_add_checked_fixture(tc_core, setup, teardown);
132 
133  suite_add_tcase(s, tc_core);
134  tcase_add_test(tc_core, test_account_char_add);
135  tcase_add_test(tc_core, test_account_char_load);
136 
137  return s;
138 }
139 
140 int main(void) {
141  int nf;
142  Suite *s = account_suite();
143  SRunner *sr = srunner_create(s);
144 
145  /* If you wish to debug the program, uncomment this line. */
146  /*srunner_set_fork_status (sr, CK_NOFORK);*/
147 
148  settings.debug = 0;
149  /* Not sure if there is a better place to put this file - basically,
150  * the account code usings the localdir to determine where to read/write
151  * the accounts file from - we don't want to be altering the real version of
152  * that file.
153  */
154  settings.localdir = strdup_local("/tmp/");
156  init(0, NULL);
157 
158  srunner_set_xml(sr, LOGDIR "/unit/server/account_char.xml");
159  srunner_set_log(sr, LOGDIR "/unit/server/account_char.out");
160  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
161  nf = srunner_ntests_failed(sr);
162  srunner_free(sr);
163 
165  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
166 }
const char * party
Character this party belonged to.
Definition: account_char.h:32
static END_TEST Suite * account_suite(void)
One player.
Definition: player.h:92
const char * name
Name of this character/player.
Definition: account_char.h:27
const char * race
Human, goblin, dragon, etc.
Definition: object.h:318
#define strdup_local
Definition: compat.h:25
LogLevel debug
Default debugging level.
Definition: global.h:240
const char * map
Last map this character was on.
Definition: account_char.h:33
#define mkdir(__a, __b)
Definition: win32.h:57
uint8_t level
Level of this character.
Definition: account_char.h:30
void account_char_free(Account_Char *chars)
This frees all data associated with the character information.
Definition: account_char.c:332
Global type definitions and header inclusions.
partylist * party
Party this player is part of.
Definition: player.h:186
int main(void)
void clean_test_account_data(void)
Clean temporary account-related files, put into /tmp.
Definition: toolkit_server.c:5
struct account_char_struct * next
Definition: account_char.h:35
const char * name
Face name, as used by archetypes and such.
Definition: face.h:20
object * create_archetype(const char *name)
Finds which archetype matches the given name, and returns a new object containing a copy of the arche...
Definition: arch.c:620
Defines for loader.l / loader.c.
static void setup(void)
const char * name
The name of the object, obviously...
Definition: object.h:311
Characters associated with an account.n.
static void teardown(void)
Account_Char * account_char_load(const char *account_name)
For a given account name, load the character information and return it.
Definition: account_char.c:79
One character account.
Definition: account_char.h:26
struct pl * contr
Pointer to the player which control this object.
Definition: object.h:276
void init(int argc, char **argv)
This is the main server initialization function.
Definition: init.c:978
#define MAX_BUF
Used for all kinds of things.
Definition: define.h:35
Account_Char * account_char_remove(Account_Char *chars, const char *pl_name)
This removes a character on this account.
Definition: account_char.c:296
Account_Char * account_char_add(Account_Char *chars, player *pl)
This adds a player to the list of accounts.
Definition: account_char.c:192
object * ob
The object representing the player.
Definition: player.h:158
const char * localdir
Read/write data files.
Definition: global.h:245
void account_char_save(const char *account, Account_Char *chars)
Saves the character information for the given account.
Definition: account_char.c:145
struct Settings settings
Server settings.
Definition: init.c:40
sstring add_string(const char *str)
This will add &#39;str&#39; to the hash table.
Definition: shstr.c:124
START_TEST(test_account_char_add)
char maplevel[MAX_BUF]
On which level is the player?
Definition: player.h:96
partylist * party_form(object *op, const char *partyname)
Forms the party struct for a party called &#39;partyname&#39;.
Definition: party.c:40
const New_Face * face
Face with colors.
Definition: object.h:332
int16_t level
Level of creature or object.
Definition: object.h:351
const char * face
Face of this character.
Definition: account_char.h:31
const char * race
Race of this character.
Definition: account_char.h:29