| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <global.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <check.h> |
| 35 |
#include <loader.h> |
| 36 |
#include <toolkit_common.h> |
| 37 |
#include <sproto.h> |
| 38 |
#include <unistd.h> |
| 39 |
#include <toolkit_server.h> |
| 40 |
|
| 41 |
static void setup(void) { |
| 42 |
} |
| 43 |
|
| 44 |
static void teardown(void) { |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
START_TEST(test_account_check_string) { |
| 53 |
char longname[MAX_NAME+3]; |
| 54 |
int i; |
| 55 |
|
| 56 |
i=account_check_string(" abcd"); |
| 57 |
fail_unless(i != 0, "string started with spaces should not be considered valid"); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
i=account_check_string("abcd:"); |
| 62 |
fail_unless(i != 0, "string with colons should not be considered valid"); |
| 63 |
i=account_check_string("a;bcd"); |
| 64 |
fail_unless(i != 0, "string with semicolons should not be considered valid"); |
| 65 |
i=account_check_string("a/bcd"); |
| 66 |
fail_unless(i != 0, "string with slash should not be considered valid"); |
| 67 |
i=account_check_string("a'bcd"); |
| 68 |
fail_unless(i != 0, "string with quote should not be considered valid"); |
| 69 |
|
| 70 |
i=account_check_string("abc\n\nefg"); |
| 71 |
fail_unless(i != 0, "string with non printable characters should not be considered valid"); |
| 72 |
|
| 73 |
i=account_check_string("$abc"); |
| 74 |
fail_unless(i != 0, "string starting with non alphanumeric should not be considered valid"); |
| 75 |
|
| 76 |
i=account_check_string("abc "); |
| 77 |
fail_unless(i != 0, "string ending in space should not be considered valid"); |
| 78 |
|
| 79 |
for (i=0; i<=MAX_NAME; i++) { |
| 80 |
longname[i]='a'; |
| 81 |
} |
| 82 |
longname[i]='\0'; |
| 83 |
i=account_check_string(longname); |
| 84 |
|
| 85 |
fail_unless(i != 0, "Too long string should not be considered valid"); |
| 86 |
|
| 87 |
i=account_check_string("Some Body"); |
| 88 |
fail_unless(i == 0, "Valid string not considered valid"); |
| 89 |
|
| 90 |
} |
| 91 |
END_TEST |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
START_TEST(test_account_add_account) { |
| 97 |
int i,j; |
| 98 |
char names[50]; |
| 99 |
char **char_names; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
i=account_new("Some Body", "mypassword"); |
| 106 |
fail_unless(i == 0, "Could not add valid account, got code %d", i); |
| 107 |
|
| 108 |
i=account_new("Some Body", "mypassword"); |
| 109 |
fail_unless(i != 0, "Duplicate account successfully added"); |
| 110 |
|
| 111 |
|
| 112 |
i=account_new("No Body", "mypassword"); |
| 113 |
fail_unless(i == 0, "Could not add valid account, got code %d", i); |
| 114 |
|
| 115 |
|
| 116 |
i=account_new("Every Body", "hispassword"); |
| 117 |
fail_unless(i == 0, "Could not add valid account"); |
| 118 |
|
| 119 |
i=account_link("foobar", "foobar"); |
| 120 |
fail_unless(i != 0, "Added player to non existent character name"); |
| 121 |
|
| 122 |
i=account_link("Some Body", "foobar"); |
| 123 |
fail_unless(i == 0, "Failed to add player to valid account"); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
for (j=0; j<30; j++) { |
| 130 |
sprintf(names,"char-%02d", j); |
| 131 |
i |= account_link("No Body", names); |
| 132 |
} |
| 133 |
fail_unless(i != 0, "Too many players added to account"); |
| 134 |
|
| 135 |
char_names = account_get_players_for_account("foobar"); |
| 136 |
fail_unless(char_names == NULL, "Got non null value for players on account with non existant accoun"); |
| 137 |
|
| 138 |
char_names = account_get_players_for_account("No Body"); |
| 139 |
fail_unless(char_names != NULL, "Got null value for players on account"); |
| 140 |
|
| 141 |
|
| 142 |
accounts_save(); |
| 143 |
} |
| 144 |
END_TEST |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
START_TEST(test_account_load_entries) { |
| 150 |
int i,j; |
| 151 |
char **char_names; |
| 152 |
const char *ae; |
| 153 |
|
| 154 |
accounts_clear(); |
| 155 |
accounts_load(); |
| 156 |
|
| 157 |
ae = account_exists("Every Body"); |
| 158 |
fail_unless(ae != NULL, "Could not find valid account"); |
| 159 |
ae = account_exists("Some Body"); |
| 160 |
fail_unless(ae != NULL, "Could not find valid account"); |
| 161 |
|
| 162 |
char_names = account_get_players_for_account("No Body"); |
| 163 |
fail_unless(char_names != NULL, "Got null value for players on account"); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
i=0; |
| 170 |
while (char_names[i] != NULL) { |
| 171 |
j=strlen(char_names[i]); |
| 172 |
i++; |
| 173 |
} |
| 174 |
char_names = account_get_players_for_account("Some Body"); |
| 175 |
fail_unless(char_names != NULL, "Got null value for players on account"); |
| 176 |
fail_unless(strcmp(char_names[0],"foobar") == 0, "Can not match on name we put in"); |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
char_names = account_get_players_for_account("foobar"); |
| 183 |
fail_unless(char_names == NULL, "Got non null value for players on account with non existant account"); |
| 184 |
|
| 185 |
i = account_remove_player("foobar", "1foobar"); |
| 186 |
fail_unless(i!=0, "Got successsful return code on account_remove_player_from_account with non existent account"); |
| 187 |
|
| 188 |
i = account_remove_player("No Body", "1foobar"); |
| 189 |
fail_unless(i!=0, "Got successsful return code on account_remove_player_from_account with non existent player"); |
| 190 |
|
| 191 |
char_names = account_get_players_for_account("No Body"); |
| 192 |
j=0; |
| 193 |
while (char_names[j] != NULL) { |
| 194 |
j++; |
| 195 |
} |
| 196 |
|
| 197 |
i = account_remove_player("No Body", "char-01"); |
| 198 |
fail_unless(i==0, "Got error removing player when it should have worked"); |
| 199 |
|
| 200 |
char_names = account_get_players_for_account("No Body"); |
| 201 |
|
| 202 |
i=0; |
| 203 |
while (char_names[i] != NULL) { |
| 204 |
i++; |
| 205 |
} |
| 206 |
fail_unless((i+1) == j, "Player removal get unexpected result - %d != %d", i+1, j); |
| 207 |
} |
| 208 |
END_TEST |
| 209 |
|
| 210 |
static Suite *account_suite(void) { |
| 211 |
Suite *s = suite_create("account"); |
| 212 |
TCase *tc_core = tcase_create("Core"); |
| 213 |
|
| 214 |
|
| 215 |
tcase_add_checked_fixture(tc_core, setup, teardown); |
| 216 |
|
| 217 |
suite_add_tcase(s, tc_core); |
| 218 |
tcase_add_test(tc_core, test_account_check_string); |
| 219 |
tcase_add_test(tc_core, test_account_add_account); |
| 220 |
tcase_add_test(tc_core, test_account_load_entries); |
| 221 |
|
| 222 |
return s; |
| 223 |
} |
| 224 |
|
| 225 |
int main(void) { |
| 226 |
int nf; |
| 227 |
Suite *s = account_suite(); |
| 228 |
SRunner *sr = srunner_create(s); |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
settings.debug = 0; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
settings.localdir = strdup_local("/tmp/"); |
| 240 |
clean_test_account_data(); |
| 241 |
init(0, NULL); |
| 242 |
|
| 243 |
srunner_set_xml(sr, LOGDIR "/unit/server/account.xml"); |
| 244 |
srunner_set_log(sr, LOGDIR "/unit/server/account.out"); |
| 245 |
srunner_run_all(sr, CK_ENV); |
| 246 |
nf = srunner_ntests_failed(sr); |
| 247 |
srunner_free(sr); |
| 248 |
clean_test_account_data(); |
| 249 |
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 250 |
} |