Atrinik Server  4.0
ban.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 #include <global.h>
26 #include <check.h>
27 #include <checkstd.h>
28 #include <check_proto.h>
29 #include <ban.h>
30 #include <player.h>
31 #include <object.h>
32 
33 START_TEST(test_ban_add)
34 {
35  ban_reset();
36  ck_assert_int_eq(ban_add(" "), BAN_BADSYNTAX);
37  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1), BAN_OK);
38  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1), BAN_EXIST);
39  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1 " *"), BAN_EXIST);
40  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1 " " ACCOUNT_TESTING_NAME),
41  BAN_OK);
42  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1 " " ACCOUNT_TESTING_NAME),
43  BAN_EXIST);
44  ck_assert_int_eq(ban_add("\"" PLAYER_TESTING_NAME2 "\" "
45  ACCOUNT_TESTING_NAME), BAN_OK);
46  ck_assert_int_eq(ban_add("\"" PLAYER_TESTING_NAME2 "\" "
47  ACCOUNT_TESTING_NAME), BAN_EXIST);
48  ck_assert_int_eq(ban_add("* * 88.88.88.88/32"), BAN_OK);
49  ck_assert_int_eq(ban_add("* * 88.88.88.88"), BAN_EXIST);
50  ck_assert_int_eq(ban_add("* * 88.88.88.88/0"), BAN_BADPLEN);
51  ck_assert_int_eq(ban_add("* * 88.88.88.88/64"), BAN_BADPLEN);
52  ck_assert_int_eq(ban_add("* * 88.88.88.677"), BAN_BADIP);
53  ck_assert_int_eq(ban_add("* * abc"), BAN_BADIP);
54 #ifdef HAVE_IPV6
55  ck_assert_int_eq(ban_add("* * 2001:cdba:9abc:5678::/64"), BAN_OK);
56  ck_assert_int_eq(ban_add("* * 2001:cdba:9abc:5678::/64"), BAN_EXIST);
57  ck_assert_int_eq(ban_add("* * 2001:cdba:0000:0000:0000:0000:3257:9652"),
58  BAN_OK);
59  ck_assert_int_eq(ban_add("* * 2001:cdba:0000:0000:0000:0000:3257:9652/128"),
60  BAN_EXIST);
61  ck_assert_int_eq(ban_add("* * 2001:cdba::3257:9652/128"), BAN_EXIST);
62  ck_assert_int_eq(ban_add("* * 2001:cdba:0000:0000:0000:0000:3257:9652/256"),
63  BAN_BADPLEN);
64  ck_assert_int_eq(ban_add("* * 2001:cdba:0000:0000:0000:0000:3257:9652/0"),
65  BAN_BADPLEN);
66 #endif
67  ban_reset();
68 }
69 END_TEST
70 
71 START_TEST(test_ban_remove)
72 {
73  ban_reset();
74  ck_assert_int_eq(ban_remove(" "), BAN_BADSYNTAX);
75  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1), BAN_OK);
76  ck_assert_int_eq(ban_remove(PLAYER_TESTING_NAME1), BAN_OK);
77  ck_assert_int_eq(ban_remove(PLAYER_TESTING_NAME1), BAN_NOTEXIST);
78  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1 " " ACCOUNT_TESTING_NAME),
79  BAN_OK);
80  ck_assert_int_eq(ban_remove(PLAYER_TESTING_NAME1 " " ACCOUNT_TESTING_NAME),
81  BAN_OK);
82  ck_assert_int_eq(ban_remove(PLAYER_TESTING_NAME1 " " ACCOUNT_TESTING_NAME),
83  BAN_NOTEXIST);
84  ck_assert_int_eq(ban_add("\"" PLAYER_TESTING_NAME2 "\" "
85  ACCOUNT_TESTING_NAME), BAN_OK);
86  ck_assert_int_eq(ban_add("* * 88.88.88.88/32"), BAN_OK);
87  ck_assert_int_eq(ban_remove("\"" PLAYER_TESTING_NAME2 "\" "
88  ACCOUNT_TESTING_NAME), BAN_OK);
89  ck_assert_int_eq(ban_remove("* * 88.88.88.88"), BAN_OK);
90 #ifdef HAVE_IPV6
91  ck_assert_int_eq(ban_add("* * 2001:cdba:9abc:5678::/64"), BAN_OK);
92  ck_assert_int_eq(ban_remove("* * 2001:cdba:9abc:5678::/64"), BAN_OK);
93  ck_assert_int_eq(ban_add("* * 2001:cdba:0000:0000:0000:0000:3257:9652"),
94  BAN_OK);
95  ck_assert_int_eq(ban_remove("* * 2001:cdba:0000:0000:0000:0000:3257:9652/"
96  "128"), BAN_OK);
97  ck_assert_int_eq(ban_remove("* * 2001:cdba:0000:0000:0000:0000:3257:9652/"
98  "128"), BAN_NOTEXIST);
99 #endif
100  ban_reset();
101  ck_assert_int_eq(ban_remove("#1"), BAN_BADID);
102  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1), BAN_OK);
103  ck_assert_int_eq(ban_remove("#1"), BAN_OK);
104  ck_assert_int_eq(ban_remove("#1"), BAN_REMOVED);
105  ck_assert_int_eq(ban_remove("#0"), BAN_BADID);
106  ck_assert_int_eq(ban_remove("#2"), BAN_BADID);
107  ban_reset();
108 }
109 END_TEST
110 
111 START_TEST(test_ban_check)
112 {
113  ban_reset();
114  object *pl = player_get_dummy(PLAYER_TESTING_NAME1, NULL);
115  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
116  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1), BAN_OK);
117  ck_assert(!ban_check(CONTR(pl)->cs, NULL));
118  ck_assert(ban_check(CONTR(pl)->cs, pl->name));
119  ban_reset();
120  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
121  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME2), BAN_OK);
122  ck_assert(!ban_check(CONTR(pl)->cs, NULL));
123  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
124  ban_reset();
125  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
126  ck_assert_int_eq(ban_add("* " ACCOUNT_TESTING_NAME), BAN_OK);
127  ck_assert(ban_check(CONTR(pl)->cs, pl->name));
128  ck_assert(ban_check(CONTR(pl)->cs, NULL));
129  ban_reset();
130  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
131  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1 " " ACCOUNT_TESTING_NAME),
132  BAN_OK);
133  ck_assert(ban_check(CONTR(pl)->cs, pl->name));
134  ck_assert(ban_check(CONTR(pl)->cs, NULL));
135  ban_reset();
136  pl = player_get_dummy(PLAYER_TESTING_NAME2, "8.8.8.8");
137  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
138  ck_assert_int_eq(ban_add("* * 8.8.8.8"), BAN_OK);
139  ck_assert(ban_check(CONTR(pl)->cs, NULL));
140  ck_assert(ban_check(CONTR(pl)->cs, pl->name));
141  ban_reset();
142  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
143  ck_assert_int_eq(ban_add("* * 8.8.4.4"), BAN_OK);
144  ck_assert(!ban_check(CONTR(pl)->cs, NULL));
145  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
146  ban_reset();
147  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
148  ck_assert_int_eq(ban_add("* * 8.8.0.0/16"), BAN_OK);
149  ck_assert(ban_check(CONTR(pl)->cs, NULL));
150  ck_assert(ban_check(CONTR(pl)->cs, pl->name));
151  ban_reset();
152  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
153  ck_assert_int_eq(ban_add("* * 7.7.0.0/16"), BAN_OK);
154  ck_assert(!ban_check(CONTR(pl)->cs, NULL));
155  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
156  ban_reset();
157  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
158  ck_assert_int_eq(ban_add("\"" PLAYER_TESTING_NAME2 "\" * 8.8.8.8"), BAN_OK);
159  ck_assert(ban_check(CONTR(pl)->cs, NULL));
160  ck_assert(ban_check(CONTR(pl)->cs, pl->name));
161  ban_reset();
162 #ifdef HAVE_IPV6
163  pl = player_get_dummy(PLAYER_TESTING_NAME2, "2001:cdba::3257:9652");
164  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
165  ck_assert_int_eq(ban_add("* * 2001:cdba::3257:9652"), BAN_OK);
166  ck_assert(ban_check(CONTR(pl)->cs, pl->name));
167  ck_assert(ban_check(CONTR(pl)->cs, NULL));
168  ban_reset();
169  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
170  ck_assert_int_eq(ban_add("* * 2001:cdba::3257:9653"), BAN_OK);
171  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
172  ck_assert(!ban_check(CONTR(pl)->cs, NULL));
173  ban_reset();
174  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
175  ck_assert_int_eq(ban_add("* * 2001:cdba::/64"), BAN_OK);
176  ck_assert(ban_check(CONTR(pl)->cs, pl->name));
177  ck_assert(ban_check(CONTR(pl)->cs, NULL));
178  ban_reset();
179  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
180  ck_assert_int_eq(ban_add("* * 2002:cdba::/64"), BAN_OK);
181  ck_assert(!ban_check(CONTR(pl)->cs, pl->name));
182  ck_assert(!ban_check(CONTR(pl)->cs, NULL));
183  ban_reset();
184 #endif
185 
186 }
187 END_TEST
188 
189 START_TEST(test_ban_reset)
190 {
191  ban_reset();
192  ck_assert_int_eq(ban_add(PLAYER_TESTING_NAME1), BAN_OK);
193  ban_reset();
194  ck_assert_int_eq(ban_remove("#1"), BAN_BADID);
195  ck_assert_int_eq(ban_remove(PLAYER_TESTING_NAME1), BAN_NOTEXIST);
196  ban_reset();
197 }
198 END_TEST
199 
200 static Suite *suite(void)
201 {
202  Suite *s = suite_create("ban");
203  TCase *tc_core = tcase_create("Core");
204 
205  tcase_add_unchecked_fixture(tc_core, check_setup, check_teardown);
206  tcase_add_checked_fixture(tc_core, check_test_setup, check_test_teardown);
207 
208  suite_add_tcase(s, tc_core);
209  tcase_add_test(tc_core, test_ban_add);
210  tcase_add_test(tc_core, test_ban_remove);
211  tcase_add_test(tc_core, test_ban_check);
212  tcase_add_test(tc_core, test_ban_reset);
213 
214  return s;
215 }
216 
217 void check_server_ban(void)
218 {
219  check_run_suite(suite(), __FILE__);
220 }
Invalid prefix length.
Definition: ban.h:47
Ban entry exists.
Definition: ban.h:42
void ban_reset(void)
Definition: ban.c:502
Bad ban ID.
Definition: ban.h:45
Ban entry doesn't exist.
Definition: ban.h:43
object * player_get_dummy(const char *name, const char *host)
Definition: player.c:2583
Invalid syntax.
Definition: ban.h:48
const char * name
Definition: object.h:168
ban_error_t ban_remove(const char *str)
Definition: ban.c:393
Ban was already removed.
Definition: ban.h:44
Invalid IP address.
Definition: ban.h:46
ban_error_t ban_add(const char *str)
Definition: ban.c:363
Success.
Definition: ban.h:41
bool ban_check(socket_struct *ns, const char *name)
Definition: ban.c:441