Atrinik Server  4.0
check.c
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
25 /* This is the main file for unit tests. From here, we call all unit
26  * test functions. */
27 
28 #include <global.h>
29 #include <check.h>
30 #include <checkstd.h>
31 #include <check_proto.h>
32 #include <toolkit/string.h>
33 #include <player.h>
34 #include <object.h>
35 #include <toolkit/path.h>
36 
37 static int saved_argc;
38 static char **saved_argv;
39 static enum fork_status fork_st;
40 
41 /*
42  * Setup function.
43  */
44 void check_setup(void)
45 {
46  init(saved_argc, saved_argv);
47 }
48 
49 /*
50  * Cleanup function.
51  */
52 void check_teardown(void)
53 {
54  cleanup();
55 
56  if (fork_st != CK_FORK) {
57  size_t num = memory_check_leak(false);
58 
59  if (num != 0) {
60  fprintf(stderr, "%" PRIu64 " memory leaks detected!\n",
61  (uint64_t) num);
62  abort();
63  }
64  } else {
65  size_t num = memory_check_leak(true);
66 
67  if (num != 0) {
68  ck_abort_msg("%" PRIu64 " memory leaks detected!", (uint64_t) num);
69  }
70  }
71 }
72 
73 /*
74  * Test setup function.
75  */
76 void check_test_setup(void)
77 {
78  if (fork_st != CK_FORK) {
79  return;
80  }
81 }
82 
83 /*
84  * Test cleanup function.
85  */
86 void check_test_teardown(void)
87 {
88  if (fork_st != CK_FORK) {
89  return;
90  }
91 }
92 
93 /*
94  * Sets up environment for doing tests related to players.
95  */
96 void check_setup_env_pl(mapstruct **map, object **pl)
97 {
98  HARD_ASSERT(map != NULL);
99  HARD_ASSERT(pl != NULL);
100 
101  *map = get_empty_map(24, 24);
102  ck_assert(*map != NULL);
103 
104  *pl = player_get_dummy(NULL, NULL);
105  ck_assert(*pl != NULL);
106 
107  object_remove(*pl, 0);
108 
109  *pl = object_insert_map(*pl, *map, NULL, 0);
110  ck_assert(*pl != NULL);
111 }
112 
113 /*
114  * Runs the specified test suite.
115  */
116 void check_run_suite(Suite *suite, const char *file)
117 {
118  SRunner *srunner;
119  char *sub, buf[HUGE_BUF], buf2[HUGE_BUF];
120 
121  toolkit_import(string);
122  toolkit_import(path);
123 
124  srunner = srunner_create(suite);
125  fork_st = srunner_fork_status(srunner);
126 
127  sub = string_last(file, "tests/");
128 
129  if (sub == NULL) {
130  log_error("Bad filename for suite: %s", file);
131  abort();
132  }
133 
134  sub = string_sub(sub, 0, -2);
135  path_ensure_directories(sub);
136  snprintf(VS(buf2), "%s.xml", sub);
137  srunner_set_xml(srunner, buf2);
138  snprintf(VS(buf), "%s.out", sub);
139  srunner_set_log(srunner, buf);
140  efree(sub);
141 
142  srunner_run_all(srunner, CK_ENV);
143  srunner_ntests_failed(srunner);
144  srunner_free(srunner);
145 }
146 
147 /* The main unit test function. Calls other functions to do the unit
148  * tests. */
149 void check_main(int argc, char **argv)
150 {
151  int i;
152 
153  toolkit_import(string);
154 
155  saved_argc = argc;
156  saved_argv = malloc(sizeof(*argv) * argc);
157 
158  if (saved_argv == NULL) {
159  log_error("OOM.");
160  abort();
161  }
162 
163  for (i = 0; i < argc; i++) {
164  saved_argv[i] = strdup(argv[i]);
165 
166  if (saved_argv[i] == NULL) {
167  log_error("OOM.");
168  abort();
169  }
170  }
171 
172  /* bugs */
173  check_bug_cursed_treasures();
174 
175  /* unit/commands */
176  check_commands_object();
177 
178  /* unit/server */
179  check_server_arch();
180  check_server_attack();
181  check_server_ban();
182  check_server_bank();
183  check_server_cache();
184  check_server_object();
185  check_server_re_cmp();
186  check_server_shop();
187 
188  /* unit/toolkit */
189  check_server_math();
190  check_server_memory();
191  check_server_packet();
192  check_server_pbkdf2();
193  check_server_shstr();
194  check_server_string();
195  check_server_stringbuffer();
196 
197  /* unit/types */
198  check_types_light_apply();
199  check_types_sound_ambient();
200 
201  for (i = 0; i < argc; i++) {
202  free(saved_argv[i]);
203  }
204 
205  free(saved_argv);
206 }
void object_remove(object *op, int flags)
Definition: object.c:1623
void init(int argc, char **argv)
Definition: init.c:1215
object * player_get_dummy(const char *name, const char *host)
Definition: player.c:2583
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.c:985
void cleanup(void)
Definition: init.c:194
uint64_t num
Number of successful updates.
Definition: metaserver.c:43
object * object_insert_map(object *op, mapstruct *m, object *originator, int flag)
Definition: object.c:1741
Definition: map.h:536