| 1 |
/* |
| 2 |
* torture_tokens.c - Tests for tokens list handling |
| 3 |
* |
| 4 |
* This file is part of the SSH Library |
| 5 |
* |
| 6 |
* Copyright (c) 2019 by Red Hat, Inc. |
| 7 |
* |
| 8 |
* Author: Anderson Toshiyuki Sasaki <[email protected]> |
| 9 |
* |
| 10 |
* The SSH Library is free software; you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU Lesser General Public License as published by |
| 12 |
* the Free Software Foundation; either version 2.1 of the License, or (at your |
| 13 |
* option) any later version. |
| 14 |
* |
| 15 |
* The SSH Library is distributed in the hope that it will be useful, but |
| 16 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 17 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 18 |
* License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public License |
| 21 |
* along with the SSH Library; see the file COPYING. If not, write to |
| 22 |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 23 |
* MA 02111-1307, USA. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#define LIBSSH_STATIC |
| 29 |
|
| 30 |
#include "torture.h" |
| 31 |
#include "libssh/token.h" |
| 32 |
#include "libssh/priv.h" |
| 33 |
|
| 34 |
static void torture_find_matching(UNUSED_PARAM(void **state)) |
| 35 |
{ |
| 36 |
char *matching; |
| 37 |
|
| 38 |
/* Match with single token */ |
| 39 |
matching = ssh_find_matching("a,b,c", "b"); |
| 40 |
assert_non_null(matching); |
| 41 |
assert_string_equal(matching, "b"); |
| 42 |
SAFE_FREE(matching); |
| 43 |
|
| 44 |
/* Match sequence, get first preferred */ |
| 45 |
matching = ssh_find_matching("a,b,c", "b,c"); |
| 46 |
assert_non_null(matching); |
| 47 |
assert_string_equal(matching, "b"); |
| 48 |
SAFE_FREE(matching); |
| 49 |
|
| 50 |
/* Only one token allowed */ |
| 51 |
matching = ssh_find_matching("c", "a,b,c"); |
| 52 |
assert_non_null(matching); |
| 53 |
assert_string_equal(matching, "c"); |
| 54 |
SAFE_FREE(matching); |
| 55 |
|
| 56 |
/* Different order in allowed and preferred; gets preferred */ |
| 57 |
matching = ssh_find_matching("c,b,a", "a,b,c"); |
| 58 |
assert_non_null(matching); |
| 59 |
assert_string_equal(matching, "a"); |
| 60 |
SAFE_FREE(matching); |
| 61 |
|
| 62 |
/* No matching returns NULL */ |
| 63 |
matching = ssh_find_matching("c,b,a", "d,e,f"); |
| 64 |
assert_null(matching); |
| 65 |
} |
| 66 |
|
| 67 |
static void torture_find_all_matching(UNUSED_PARAM(void **state)) |
| 68 |
{ |
| 69 |
char *matching; |
| 70 |
|
| 71 |
/* Match with single token */ |
| 72 |
matching = ssh_find_all_matching("a,b,c", "b"); |
| 73 |
assert_non_null(matching); |
| 74 |
assert_string_equal(matching, "b"); |
| 75 |
SAFE_FREE(matching); |
| 76 |
|
| 77 |
/* Match sequence, get first preferred */ |
| 78 |
matching = ssh_find_all_matching("a,b,c", "b,c"); |
| 79 |
assert_non_null(matching); |
| 80 |
assert_string_equal(matching, "b,c"); |
| 81 |
SAFE_FREE(matching); |
| 82 |
|
| 83 |
/* Only one token allowed */ |
| 84 |
matching = ssh_find_all_matching("c", "a,b,c"); |
| 85 |
assert_non_null(matching); |
| 86 |
assert_string_equal(matching, "c"); |
| 87 |
SAFE_FREE(matching); |
| 88 |
|
| 89 |
/* Different order in allowed and preferred; gets preferred */ |
| 90 |
matching = ssh_find_all_matching("c,b,a", "a,c,b"); |
| 91 |
assert_non_null(matching); |
| 92 |
assert_string_equal(matching, "a,c,b"); |
| 93 |
SAFE_FREE(matching); |
| 94 |
|
| 95 |
/* No matching returns NULL */ |
| 96 |
matching = ssh_find_all_matching("c,b,a", "d,e,f"); |
| 97 |
assert_null(matching); |
| 98 |
} |
| 99 |
|
| 100 |
static void tokenize_compare_expected(const char *chain, const char **expected, |
| 101 |
size_t num_expected) |
| 102 |
{ |
| 103 |
struct ssh_tokens_st *tokens; |
| 104 |
size_t i; |
| 105 |
|
| 106 |
tokens = ssh_tokenize(chain, ','); |
| 107 |
assert_non_null(tokens); |
| 108 |
|
| 109 |
if (expected != NULL) { |
| 110 |
assert_non_null(tokens->tokens); |
| 111 |
for (i = 0; i < num_expected; i++) { |
| 112 |
assert_non_null(tokens->tokens[i]); |
| 113 |
assert_non_null(expected[i]); |
| 114 |
assert_string_equal(tokens->tokens[i], expected[i]); |
| 115 |
} |
| 116 |
|
| 117 |
assert_null(tokens->tokens[i]); |
| 118 |
|
| 119 |
i = 0; |
| 120 |
printf("Tokenizing \"%s\" resulted in: ", chain); |
| 121 |
while (tokens->tokens[i]) { |
| 122 |
printf("\"%s\" ", tokens->tokens[i++]); |
| 123 |
} |
| 124 |
printf("\n"); |
| 125 |
} |
| 126 |
|
| 127 |
ssh_tokens_free(tokens); |
| 128 |
} |
| 129 |
|
| 130 |
static void torture_tokens_sanity(UNUSED_PARAM(void **state)) |
| 131 |
{ |
| 132 |
const char *simple[] = {"a", "b", "c"}; |
| 133 |
const char *colon_first[] = {"", "a", "b", "c"}; |
| 134 |
const char *colon_end[] = {"a", "b", "c"}; |
| 135 |
const char *colon_both[] = {"", "a", "b", "c"}; |
| 136 |
const char *single[] = {"abc"}; |
| 137 |
const char *empty[] = {""}; |
| 138 |
const char *single_colon[] = {""}; |
| 139 |
|
| 140 |
tokenize_compare_expected("a,b,c", simple, 3); |
| 141 |
tokenize_compare_expected(",a,b,c", colon_first, 4); |
| 142 |
tokenize_compare_expected("a,b,c,", colon_end, 3); |
| 143 |
tokenize_compare_expected(",a,b,c,", colon_both, 4); |
| 144 |
tokenize_compare_expected("abc", single, 1); |
| 145 |
tokenize_compare_expected("", empty, 1); |
| 146 |
tokenize_compare_expected(",", single_colon, 1); |
| 147 |
} |
| 148 |
|
| 149 |
static void torture_remove_duplicate(UNUSED_PARAM(void **state)) |
| 150 |
{ |
| 151 |
|
| 152 |
const char *simple[] = {"a,a,b,b,c,c", |
| 153 |
"a,b,c,a,b,c", |
| 154 |
"a,b,c,c,b,a", |
| 155 |
"a,a,,b,b,,c,c", |
| 156 |
",a,a,b,b,c,c", |
| 157 |
"a,a,b,b,c,c,"}; |
| 158 |
const char *empty[] = {"", |
| 159 |
",,,,,,,,,", |
| 160 |
NULL}; |
| 161 |
char *ret = NULL; |
| 162 |
int i; |
| 163 |
|
| 164 |
for (i = 0; i < 6; i++) { |
| 165 |
ret = ssh_remove_duplicates(simple[i]); |
| 166 |
assert_non_null(ret); |
| 167 |
assert_string_equal("a,b,c", ret); |
| 168 |
printf("simple[%d] resulted in '%s'\n", i, ret); |
| 169 |
SAFE_FREE(ret); |
| 170 |
} |
| 171 |
|
| 172 |
for (i = 0; i < 3; i++) { |
| 173 |
ret = ssh_remove_duplicates(empty[i]); |
| 174 |
if (ret != NULL) { |
| 175 |
printf("empty[%d] resulted in '%s'\n", i, ret); |
| 176 |
} |
| 177 |
assert_null(ret); |
| 178 |
} |
| 179 |
|
| 180 |
ret = ssh_remove_duplicates("a"); |
| 181 |
assert_non_null(ret); |
| 182 |
assert_string_equal("a", ret); |
| 183 |
SAFE_FREE(ret); |
| 184 |
} |
| 185 |
|
| 186 |
static void torture_append_without_duplicate(UNUSED_PARAM(void **state)) |
| 187 |
{ |
| 188 |
const char *s1[] = {"a,a,b,b,c,c", |
| 189 |
"a,b,c,a,b,c", |
| 190 |
"a,b,c,c,b,a", |
| 191 |
"a,a,,b,b,,c,c", |
| 192 |
",a,a,b,b,c,c", |
| 193 |
"a,a,b,b,c,c,"}; |
| 194 |
const char *s2[] = {"a,a,b,b,c,c,d,d", |
| 195 |
"a,b,c,d,a,b,c,d", |
| 196 |
"a,b,c,d,d,c,b,a", |
| 197 |
"a,a,,b,b,,c,c,,d,d", |
| 198 |
",a,a,b,b,c,c,d,d", |
| 199 |
"a,a,b,b,c,c,d,d,", |
| 200 |
"d"}; |
| 201 |
const char *empty[] = {"", |
| 202 |
",,,,,,,,,", |
| 203 |
NULL, |
| 204 |
NULL}; |
| 205 |
char *ret = NULL; |
| 206 |
int i, j; |
| 207 |
|
| 208 |
ret = ssh_append_without_duplicates("a", "a"); |
| 209 |
assert_non_null(ret); |
| 210 |
assert_string_equal("a", ret); |
| 211 |
SAFE_FREE(ret); |
| 212 |
|
| 213 |
ret = ssh_append_without_duplicates("a", "b"); |
| 214 |
assert_non_null(ret); |
| 215 |
assert_string_equal("a,b", ret); |
| 216 |
SAFE_FREE(ret); |
| 217 |
|
| 218 |
ret = ssh_append_without_duplicates("a", NULL); |
| 219 |
assert_non_null(ret); |
| 220 |
assert_string_equal("a", ret); |
| 221 |
SAFE_FREE(ret); |
| 222 |
|
| 223 |
ret = ssh_append_without_duplicates(NULL, "b"); |
| 224 |
assert_non_null(ret); |
| 225 |
assert_string_equal("b", ret); |
| 226 |
SAFE_FREE(ret); |
| 227 |
|
| 228 |
for (i = 0; i < 6; i++) { |
| 229 |
for (j = 0; j < 7; j++) { |
| 230 |
ret = ssh_append_without_duplicates(s1[i], s2[j]); |
| 231 |
assert_non_null(ret); |
| 232 |
printf("s1[%d] + s2[%d] resulted in '%s'\n", i, j, ret); |
| 233 |
assert_string_equal("a,b,c,d", ret); |
| 234 |
SAFE_FREE(ret); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
for (i = 0; i < 6; i++) { |
| 239 |
for (j = 0; j < 3; j++) { |
| 240 |
ret = ssh_append_without_duplicates(s1[i], empty[j]); |
| 241 |
assert_non_null(ret); |
| 242 |
printf("s1[%d] + empty[%d] resulted in '%s'\n", i, j, ret); |
| 243 |
assert_string_equal("a,b,c", ret); |
| 244 |
SAFE_FREE(ret); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
for (i = 0; i < 3; i++) { |
| 249 |
for (j = 0; j < 6; j++) { |
| 250 |
ret = ssh_append_without_duplicates(empty[i], s1[j]); |
| 251 |
assert_non_null(ret); |
| 252 |
printf("empty[%d] + s1[%d] resulted in '%s'\n", i, j, ret); |
| 253 |
assert_string_equal("a,b,c", ret); |
| 254 |
SAFE_FREE(ret); |
| 255 |
} |
| 256 |
} |
| 257 |
for (i = 0; i < 4; i++) { |
| 258 |
for (j = 0; j < 4; j++) { |
| 259 |
ret = ssh_append_without_duplicates(empty[i], empty[j]); |
| 260 |
if (ret != NULL) { |
| 261 |
printf("empty[%d] + empty[%d] resulted in '%s'\n", i, j, ret); |
| 262 |
} |
| 263 |
assert_null(ret); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
static void torture_remove_all_matching (UNUSED_PARAM(void** state)) { |
| 269 |
char *p; |
| 270 |
|
| 271 |
p = ssh_remove_all_matching(NULL, NULL); |
| 272 |
assert_null(p); |
| 273 |
|
| 274 |
p = ssh_remove_all_matching("don't remove", NULL); |
| 275 |
assert_non_null(p); |
| 276 |
assert_string_equal(p, "don't remove"); |
| 277 |
free(p); |
| 278 |
|
| 279 |
p = ssh_remove_all_matching("a,b,c", "b"); |
| 280 |
assert_non_null(p); |
| 281 |
assert_string_equal(p, "a,c"); |
| 282 |
free(p); |
| 283 |
|
| 284 |
p = ssh_remove_all_matching("a,b,c", "a,b"); |
| 285 |
assert_non_null(p); |
| 286 |
assert_string_equal(p, "c"); |
| 287 |
free(p); |
| 288 |
|
| 289 |
p = ssh_remove_all_matching("a,b,c", "d"); |
| 290 |
assert_non_null(p); |
| 291 |
assert_string_equal(p, "a,b,c"); |
| 292 |
free(p); |
| 293 |
|
| 294 |
p = ssh_remove_all_matching("a,b,c", "a,b,c"); |
| 295 |
assert_null(p); |
| 296 |
} |
| 297 |
|
| 298 |
static void torture_prefix_without_duplicates (UNUSED_PARAM(void** state)) { |
| 299 |
char *p; |
| 300 |
|
| 301 |
p = ssh_prefix_without_duplicates(NULL, NULL); |
| 302 |
assert_null(p); |
| 303 |
|
| 304 |
p = ssh_prefix_without_duplicates("a,b,c", NULL); |
| 305 |
assert_non_null(p); |
| 306 |
assert_string_equal(p, "a,b,c"); |
| 307 |
free(p); |
| 308 |
|
| 309 |
p = ssh_prefix_without_duplicates("a,b,c", "a"); |
| 310 |
assert_non_null(p); |
| 311 |
assert_string_equal(p, "a,b,c"); |
| 312 |
free(p); |
| 313 |
|
| 314 |
p = ssh_prefix_without_duplicates("a,b,c", "b"); |
| 315 |
assert_non_null(p); |
| 316 |
assert_string_equal(p, "b,a,c"); |
| 317 |
free(p); |
| 318 |
|
| 319 |
p = ssh_prefix_without_duplicates("a,b,c", "x"); |
| 320 |
assert_non_null(p); |
| 321 |
assert_string_equal(p, "x,a,b,c"); |
| 322 |
free(p); |
| 323 |
|
| 324 |
p = ssh_prefix_without_duplicates("a,b,c", "c,x"); |
| 325 |
assert_non_null(p); |
| 326 |
assert_string_equal(p, "c,x,a,b"); |
| 327 |
free(p); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
int torture_run_tests(void) |
| 332 |
{ |
| 333 |
int rc; |
| 334 |
struct CMUnitTest tests[] = { |
| 335 |
cmocka_unit_test(torture_tokens_sanity), |
| 336 |
cmocka_unit_test(torture_find_matching), |
| 337 |
cmocka_unit_test(torture_find_all_matching), |
| 338 |
cmocka_unit_test(torture_remove_duplicate), |
| 339 |
cmocka_unit_test(torture_append_without_duplicate), |
| 340 |
cmocka_unit_test(torture_remove_all_matching), |
| 341 |
cmocka_unit_test(torture_prefix_without_duplicates), |
| 342 |
}; |
| 343 |
|
| 344 |
ssh_init(); |
| 345 |
torture_filter_tests(tests); |
| 346 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 347 |
ssh_finalize(); |
| 348 |
return rc; |
| 349 |
} |