| 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 |
#include "../src/config.h" |
| 30 |
#include "../src/link.h" |
| 31 |
#include "../src/util.h" |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <unity.h> |
| 36 |
|
| 37 |
void setUp(void) |
| 38 |
{ |
| 39 |
Config_init(); |
| 40 |
} |
| 41 |
|
| 42 |
void tearDown(void) |
| 43 |
{ |
| 44 |
CONFIG.external_links = 0; |
| 45 |
if (ROOT_LINK_TBL != NULL) { |
| 46 |
LinkTable_free(ROOT_LINK_TBL); |
| 47 |
ROOT_LINK_TBL = NULL; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
void test_is_external_url_http(void) |
| 56 |
{ |
| 57 |
TEST_ASSERT_EQUAL_INT(1, is_external_url("http://example.com/file.iso")); |
| 58 |
} |
| 59 |
|
| 60 |
void test_is_external_url_https(void) |
| 61 |
{ |
| 62 |
TEST_ASSERT_EQUAL_INT(1, is_external_url("https://example.com/file.iso")); |
| 63 |
} |
| 64 |
|
| 65 |
void test_is_external_url_relative(void) |
| 66 |
{ |
| 67 |
TEST_ASSERT_EQUAL_INT(0, is_external_url("file.iso")); |
| 68 |
} |
| 69 |
|
| 70 |
void test_is_external_url_absolute_path(void) |
| 71 |
{ |
| 72 |
TEST_ASSERT_EQUAL_INT(0, is_external_url("/path/file.iso")); |
| 73 |
} |
| 74 |
|
| 75 |
void test_is_external_url_ftp(void) |
| 76 |
{ |
| 77 |
|
| 78 |
TEST_ASSERT_EQUAL_INT(0, is_external_url("ftp://example.com/file.iso")); |
| 79 |
} |
| 80 |
|
| 81 |
void test_is_external_url_null(void) |
| 82 |
{ |
| 83 |
TEST_ASSERT_EQUAL_INT(0, is_external_url(NULL)); |
| 84 |
} |
| 85 |
|
| 86 |
void test_is_external_url_uppercase(void) |
| 87 |
{ |
| 88 |
TEST_ASSERT_EQUAL_INT(1, is_external_url("HTTP://example.com/file.iso")); |
| 89 |
TEST_ASSERT_EQUAL_INT(1, is_external_url("HTTPS://example.com/file.iso")); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
void test_is_cross_origin_different_host(void) |
| 97 |
{ |
| 98 |
TEST_ASSERT_EQUAL_INT( |
| 99 |
1, is_cross_origin("http://localhost/", "http://example.com/f.iso")); |
| 100 |
} |
| 101 |
|
| 102 |
void test_is_cross_origin_same_host(void) |
| 103 |
{ |
| 104 |
TEST_ASSERT_EQUAL_INT( |
| 105 |
0, is_cross_origin("http://localhost/", "http://localhost/file.iso")); |
| 106 |
} |
| 107 |
|
| 108 |
void test_is_cross_origin_different_scheme(void) |
| 109 |
{ |
| 110 |
TEST_ASSERT_EQUAL_INT( |
| 111 |
1, is_cross_origin("http://example.com/", "https://example.com/f.iso")); |
| 112 |
} |
| 113 |
|
| 114 |
void test_is_cross_origin_different_port(void) |
| 115 |
{ |
| 116 |
TEST_ASSERT_EQUAL_INT(1, is_cross_origin("http://localhost:8080/", |
| 117 |
"http://localhost:9090/f.iso")); |
| 118 |
} |
| 119 |
|
| 120 |
void test_is_cross_origin_same_host_with_port(void) |
| 121 |
{ |
| 122 |
TEST_ASSERT_EQUAL_INT(0, is_cross_origin("http://localhost:8080/", |
| 123 |
"http://localhost:8080/f.iso")); |
| 124 |
} |
| 125 |
|
| 126 |
void test_is_cross_origin_no_trailing_slash_same(void) |
| 127 |
{ |
| 128 |
TEST_ASSERT_EQUAL_INT( |
| 129 |
0, is_cross_origin("http://example.com", "http://example.com/f.iso")); |
| 130 |
} |
| 131 |
|
| 132 |
void test_is_cross_origin_no_trailing_slash_different(void) |
| 133 |
{ |
| 134 |
TEST_ASSERT_EQUAL_INT( |
| 135 |
1, is_cross_origin("http://example.com", "http://other.com/f.iso")); |
| 136 |
} |
| 137 |
|
| 138 |
void test_is_cross_origin_both_no_trailing_slash_same(void) |
| 139 |
{ |
| 140 |
TEST_ASSERT_EQUAL_INT( |
| 141 |
0, is_cross_origin("http://example.com", "http://example.com")); |
| 142 |
} |
| 143 |
|
| 144 |
void test_is_cross_origin_null(void) |
| 145 |
{ |
| 146 |
TEST_ASSERT_EQUAL_INT(1, is_cross_origin(NULL, "http://example.com/")); |
| 147 |
TEST_ASSERT_EQUAL_INT(1, is_cross_origin("http://example.com/", NULL)); |
| 148 |
TEST_ASSERT_EQUAL_INT(1, is_cross_origin(NULL, NULL)); |
| 149 |
} |
| 150 |
|
| 151 |
void test_is_cross_origin_case_insensitive(void) |
| 152 |
{ |
| 153 |
TEST_ASSERT_EQUAL_INT( |
| 154 |
0, is_cross_origin("http://LOCALHOST/", "http://localhost/file.iso")); |
| 155 |
TEST_ASSERT_EQUAL_INT( |
| 156 |
0, is_cross_origin("HTTP://localhost/", "http://localhost/file.iso")); |
| 157 |
} |
| 158 |
|
| 159 |
void test_is_cross_origin_with_query_and_fragment(void) |
| 160 |
{ |
| 161 |
|
| 162 |
TEST_ASSERT_EQUAL_INT( |
| 163 |
0, is_cross_origin("http://localhost/", |
| 164 |
"http://localhost?redirect=/foo/bar")); |
| 165 |
TEST_ASSERT_EQUAL_INT(0, |
| 166 |
is_cross_origin("http://localhost?redirect=/foo/bar", |
| 167 |
"http://localhost/file.iso")); |
| 168 |
|
| 169 |
|
| 170 |
TEST_ASSERT_EQUAL_INT(0, is_cross_origin("http://localhost?auth=1", |
| 171 |
"http://localhost/file.iso")); |
| 172 |
TEST_ASSERT_EQUAL_INT(0, is_cross_origin("http://localhost#fragment", |
| 173 |
"http://localhost?auth=1")); |
| 174 |
TEST_ASSERT_EQUAL_INT(1, is_cross_origin("http://localhost?auth=1", |
| 175 |
"http://otherhost?auth=1")); |
| 176 |
} |
| 177 |
|
| 178 |
void test_is_cross_origin_default_port_normalization_http(void) |
| 179 |
{ |
| 180 |
TEST_ASSERT_EQUAL_INT(0, is_cross_origin("http://example.com/", |
| 181 |
"http://example.com:80/f.iso")); |
| 182 |
TEST_ASSERT_EQUAL_INT(0, is_cross_origin("http://example.com:80/", |
| 183 |
"http://example.com/f.iso")); |
| 184 |
} |
| 185 |
|
| 186 |
void test_is_cross_origin_default_port_normalization_https(void) |
| 187 |
{ |
| 188 |
TEST_ASSERT_EQUAL_INT(0, is_cross_origin("https://example.com/", |
| 189 |
"https://example.com:443/f.iso")); |
| 190 |
TEST_ASSERT_EQUAL_INT(0, is_cross_origin("https://example.com:443/", |
| 191 |
"https://example.com/f.iso")); |
| 192 |
} |
| 193 |
|
| 194 |
void test_is_cross_origin_non_default_port_not_equal(void) |
| 195 |
{ |
| 196 |
TEST_ASSERT_EQUAL_INT(1, is_cross_origin("http://example.com/", |
| 197 |
"http://example.com:8080/f.iso")); |
| 198 |
TEST_ASSERT_EQUAL_INT(1, is_cross_origin("https://example.com/", |
| 199 |
"https://example.com:8443/f.iso")); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
void test_external_url_to_filename_simple(void) |
| 208 |
{ |
| 209 |
char *name = external_url_to_filename("http://example.com/file.iso"); |
| 210 |
TEST_ASSERT_NOT_NULL(name); |
| 211 |
TEST_ASSERT_EQUAL_STRING("file.iso", name); |
| 212 |
FREE(name); |
| 213 |
} |
| 214 |
|
| 215 |
void test_external_url_to_filename_nested_path(void) |
| 216 |
{ |
| 217 |
char *name = external_url_to_filename("http://example.com/a/b/file.iso"); |
| 218 |
TEST_ASSERT_NOT_NULL(name); |
| 219 |
TEST_ASSERT_EQUAL_STRING("file.iso", name); |
| 220 |
FREE(name); |
| 221 |
} |
| 222 |
|
| 223 |
void test_external_url_to_filename_dir(void) |
| 224 |
{ |
| 225 |
char *name = external_url_to_filename("http://example.com/subdir/"); |
| 226 |
TEST_ASSERT_NOT_NULL(name); |
| 227 |
TEST_ASSERT_EQUAL_STRING("subdir", name); |
| 228 |
FREE(name); |
| 229 |
} |
| 230 |
|
| 231 |
void test_external_url_to_filename_encoded(void) |
| 232 |
{ |
| 233 |
char *name = external_url_to_filename("http://example.com/my%20file.iso"); |
| 234 |
TEST_ASSERT_NOT_NULL(name); |
| 235 |
TEST_ASSERT_EQUAL_STRING("my file.iso", name); |
| 236 |
FREE(name); |
| 237 |
} |
| 238 |
|
| 239 |
void test_external_url_to_filename_query(void) |
| 240 |
{ |
| 241 |
char *name = external_url_to_filename("http://example.com/file.iso?v=1"); |
| 242 |
TEST_ASSERT_NOT_NULL(name); |
| 243 |
TEST_ASSERT_EQUAL_STRING("file.iso", name); |
| 244 |
FREE(name); |
| 245 |
} |
| 246 |
|
| 247 |
void test_external_url_to_filename_root(void) |
| 248 |
{ |
| 249 |
*/ |
| 250 |
char *name = external_url_to_filename("http://example.com/"); |
| 251 |
TEST_ASSERT_NOT_NULL(name); |
| 252 |
TEST_ASSERT_EQUAL_STRING("", name); |
| 253 |
FREE(name); |
| 254 |
} |
| 255 |
|
| 256 |
void test_external_url_to_filename_null(void) |
| 257 |
{ |
| 258 |
char *name = external_url_to_filename(NULL); |
| 259 |
TEST_ASSERT_NOT_NULL(name); |
| 260 |
TEST_ASSERT_EQUAL_STRING("", name); |
| 261 |
FREE(name); |
| 262 |
} |
| 263 |
|
| 264 |
void test_external_url_to_filename_fragment(void) |
| 265 |
{ |
| 266 |
char *name |
| 267 |
= external_url_to_filename("http://example.com/file.iso#section"); |
| 268 |
TEST_ASSERT_NOT_NULL(name); |
| 269 |
TEST_ASSERT_EQUAL_STRING("file.iso", name); |
| 270 |
FREE(name); |
| 271 |
} |
| 272 |
|
| 273 |
void test_external_url_to_filename_query_and_fragment(void) |
| 274 |
{ |
| 275 |
char *name |
| 276 |
= external_url_to_filename("http://example.com/file.iso?v=1#section"); |
| 277 |
TEST_ASSERT_NOT_NULL(name); |
| 278 |
TEST_ASSERT_EQUAL_STRING("file.iso", name); |
| 279 |
FREE(name); |
| 280 |
} |
| 281 |
|
| 282 |
void test_external_url_to_filename_encoded_slashes(void) |
| 283 |
{ |
| 284 |
char *name = external_url_to_filename( |
| 285 |
"http://example.com/some%2Fnested%2Ffile.iso"); |
| 286 |
TEST_ASSERT_NOT_NULL(name); |
| 287 |
TEST_ASSERT_EQUAL_STRING("some_nested_file.iso", name); |
| 288 |
FREE(name); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
void test_HTML_external_link_file(void) |
| 296 |
{ |
| 297 |
CONFIG.external_links = 1; |
| 298 |
LinkTable *tbl = LinkTable_alloc("http://localhost/"); |
| 299 |
LinkTable_parse_html(tbl, "http://localhost/", |
| 300 |
"<a href=\"http://external.com/file.iso\">" |
| 301 |
"file.iso</a>"); |
| 302 |
|
| 303 |
|
| 304 |
TEST_ASSERT_EQUAL_INT(2, tbl->size); |
| 305 |
TEST_ASSERT_EQUAL_STRING("file.iso", tbl->links[1]->linkname); |
| 306 |
TEST_ASSERT_EQUAL_STRING("http://external.com/file.iso", |
| 307 |
tbl->links[1]->f_url); |
| 308 |
TEST_ASSERT_EQUAL_INT(LINK_UNINITIALISED_FILE, tbl->links[1]->type); |
| 309 |
LinkTable_free(tbl); |
| 310 |
CONFIG.external_links = 0; |
| 311 |
} |
| 312 |
|
| 313 |
void test_HTML_external_link_dir(void) |
| 314 |
{ |
| 315 |
CONFIG.external_links = 1; |
| 316 |
LinkTable *tbl = LinkTable_alloc("http://localhost/"); |
| 317 |
LinkTable_parse_html(tbl, "http://localhost/", |
| 318 |
"<a href=\"http://external.com/subdir/\">" |
| 319 |
"subdir</a>" |
| 320 |
"<a href=\"http://external.com/subdir2/?q=1\">" |
| 321 |
"subdir2</a>" |
| 322 |
"<a href=\"http://external.com/subdir3/#fragment\">" |
| 323 |
"subdir3</a>"); |
| 324 |
|
| 325 |
TEST_ASSERT_EQUAL_INT(4, tbl->size); |
| 326 |
TEST_ASSERT_EQUAL_STRING("subdir", tbl->links[1]->linkname); |
| 327 |
TEST_ASSERT_EQUAL_INT(LINK_UNINITIALISED_DIR, tbl->links[1]->type); |
| 328 |
|
| 329 |
TEST_ASSERT_EQUAL_STRING("subdir2", tbl->links[2]->linkname); |
| 330 |
TEST_ASSERT_EQUAL_INT(LINK_UNINITIALISED_DIR, tbl->links[2]->type); |
| 331 |
|
| 332 |
TEST_ASSERT_EQUAL_STRING("subdir3", tbl->links[3]->linkname); |
| 333 |
TEST_ASSERT_EQUAL_INT(LINK_UNINITIALISED_DIR, tbl->links[3]->type); |
| 334 |
|
| 335 |
LinkTable_free(tbl); |
| 336 |
CONFIG.external_links = 0; |
| 337 |
} |
| 338 |
|
| 339 |
void test_HTML_external_link_disabled(void) |
| 340 |
{ |
| 341 |
CONFIG.external_links = 0; |
| 342 |
LinkTable *tbl = LinkTable_alloc("http://localhost/"); |
| 343 |
LinkTable_parse_html(tbl, "http://localhost/", |
| 344 |
"<a href=\"http://external.com/file.iso\">" |
| 345 |
"file.iso</a>"); |
| 346 |
|
| 347 |
|
| 348 |
TEST_ASSERT_EQUAL_INT(1, tbl->size); |
| 349 |
LinkTable_free(tbl); |
| 350 |
} |
| 351 |
|
| 352 |
void test_HTML_external_link_same_origin(void) |
| 353 |
{ |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
CONFIG.external_links = 1; |
| 361 |
LinkTable *tbl = LinkTable_alloc("http://localhost/"); |
| 362 |
LinkTable_parse_html(tbl, "http://localhost/", |
| 363 |
"<a href=\"http://localhost/file.iso\">" |
| 364 |
"file.iso</a>"); |
| 365 |
|
| 366 |
*/ |
| 367 |
TEST_ASSERT_EQUAL_INT(1, tbl->size); |
| 368 |
LinkTable_free(tbl); |
| 369 |
CONFIG.external_links = 0; |
| 370 |
} |
| 371 |
|
| 372 |
void test_HTML_external_link_dedup_first_wins(void) |
| 373 |
{ |
| 374 |
CONFIG.external_links = 1; |
| 375 |
LinkTable *tbl = LinkTable_alloc("http://localhost/"); |
| 376 |
|
| 377 |
LinkTable_parse_html(tbl, "http://localhost/", |
| 378 |
"<a href=\"http://server-a.com/file.iso\">file.iso" |
| 379 |
"</a><a href=\"http://server-b.com/file.iso\">" |
| 380 |
"file.iso</a>"); |
| 381 |
|
| 382 |
|
| 383 |
TEST_ASSERT_EQUAL_INT(2, tbl->size); |
| 384 |
TEST_ASSERT_EQUAL_STRING("http://server-a.com/file.iso", |
| 385 |
tbl->links[1]->f_url); |
| 386 |
LinkTable_free(tbl); |
| 387 |
CONFIG.external_links = 0; |
| 388 |
} |
| 389 |
|
| 390 |
void test_HTML_external_link_dot_and_dotdot(void) |
| 391 |
{ |
| 392 |
CONFIG.external_links = 1; |
| 393 |
LinkTable *tbl = LinkTable_alloc("http://localhost/"); |
| 394 |
LinkTable_parse_html( |
| 395 |
tbl, "http://localhost/", |
| 396 |
"<a href=\"http://external.com/.\">dot</a>" |
| 397 |
"<a href=\"http://external.com/..\">dotdot</a>" |
| 398 |
"<a href=\"http://external.com/file.iso\">file.iso</a>"); |
| 399 |
|
| 400 |
|
| 401 |
TEST_ASSERT_EQUAL_INT(2, tbl->size); |
| 402 |
TEST_ASSERT_EQUAL_STRING("file.iso", tbl->links[1]->linkname); |
| 403 |
LinkTable_free(tbl); |
| 404 |
CONFIG.external_links = 0; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
void test_Link_preserves_preset_f_url(void) |
| 412 |
{ |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
LinkTable *tbl = LinkTable_alloc("http://localhost/"); |
| 417 |
|
| 418 |
Link *ext = CALLOC(1, sizeof(Link)); |
| 419 |
strncpy(ext->linkname, "file.iso", NAME_MAX); |
| 420 |
strncpy(ext->linkpath, "file.iso", NAME_MAX); |
| 421 |
strncpy(ext->f_url, "http://external.com/file.iso", PATH_MAX); |
| 422 |
ext->type = LINK_UNINITIALISED_FILE; |
| 423 |
LinkTable_add(tbl, ext); |
| 424 |
|
| 425 |
TEST_ASSERT_EQUAL_STRING("http://external.com/file.iso", ext->f_url); |
| 426 |
LinkTable_free(tbl); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
void test_url_to_cache_path_null(void) |
| 434 |
{ |
| 435 |
TEST_ASSERT_NULL(url_to_cache_path(NULL)); |
| 436 |
} |
| 437 |
|
| 438 |
void test_url_to_cache_path_local(void) |
| 439 |
{ |
| 440 |
char *path = url_to_cache_path("http://localhost/my%20file.iso"); |
| 441 |
TEST_ASSERT_NOT_NULL(path); |
| 442 |
TEST_ASSERT_EQUAL_STRING("http://localhost/my file.iso", path); |
| 443 |
FREE(path); |
| 444 |
} |
| 445 |
|
| 446 |
void test_url_to_cache_path_external_sanitization(void) |
| 447 |
{ |
| 448 |
ROOT_LINK_TBL = LinkTable_alloc("http://localhost/"); |
| 449 |
char *path = url_to_cache_path("http://external.com/my%20file.iso?param=1"); |
| 450 |
TEST_ASSERT_NOT_NULL(path); |
| 451 |
TEST_ASSERT_EQUAL_STRING("http___external.com_my file.iso?param=1", path); |
| 452 |
FREE(path); |
| 453 |
LinkTable_free(ROOT_LINK_TBL); |
| 454 |
ROOT_LINK_TBL = NULL; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
void test_LinkTable_alloc(void) |
| 462 |
{ |
| 463 |
LinkTable *table = LinkTable_alloc("https://example.com/dir/"); |
| 464 |
TEST_ASSERT_NOT_NULL(table); |
| 465 |
TEST_ASSERT_EQUAL_INT(1, table->size); |
| 466 |
TEST_ASSERT_NOT_NULL(table->links); |
| 467 |
TEST_ASSERT_NOT_NULL(table->links[0]); |
| 468 |
TEST_ASSERT_EQUAL_STRING("https://example.com/dir/", |
| 469 |
table->links[0]->f_url); |
| 470 |
TEST_ASSERT_EQUAL_STRING("", table->links[0]->linkname); |
| 471 |
LinkTable_free(table); |
| 472 |
} |
| 473 |
|
| 474 |
void test_LinkTable_add(void) |
| 475 |
{ |
| 476 |
LinkTable *table = LinkTable_alloc("https://example.com/dir/"); |
| 477 |
TEST_ASSERT_NOT_NULL(table); |
| 478 |
TEST_ASSERT_EQUAL_INT(1, table->size); |
| 479 |
|
| 480 |
Link *new_link = CALLOC(1, sizeof(Link)); |
| 481 |
TEST_ASSERT_NOT_NULL(new_link); |
| 482 |
strncpy(new_link->linkname, "file.txt", NAME_MAX); |
| 483 |
new_link->type = LINK_FILE; |
| 484 |
|
| 485 |
LinkTable_add(table, new_link); |
| 486 |
|
| 487 |
TEST_ASSERT_EQUAL_INT(2, table->size); |
| 488 |
TEST_ASSERT_EQUAL_PTR(new_link, table->links[1]); |
| 489 |
|
| 490 |
LinkTable_free(table); |
| 491 |
} |
| 492 |
|
| 493 |
void test_Link_download_zero_length(void) |
| 494 |
{ |
| 495 |
Link link = {0}; |
| 496 |
link.content_length = 0; |
| 497 |
link.type = LINK_FILE; |
| 498 |
strncpy(link.linkname, "empty.txt", NAME_MAX); |
| 499 |
strncpy(link.f_url, "https://example.com/empty.txt", PATH_MAX); |
| 500 |
|
| 501 |
char buf[10]; |
| 502 |
memset(buf, 0xff, sizeof(buf)); |
| 503 |
long res = Link_download(&link, buf, sizeof(buf), 0, NULL); |
| 504 |
TEST_ASSERT_EQUAL_INT(0, res); |
| 505 |
|
| 506 |
for (size_t i = 0; i < sizeof(buf); i++) { |
| 507 |
TEST_ASSERT_EQUAL_HEX8(0xff, (unsigned char)buf[i]); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
void test_link_linknames_equal(void) |
| 512 |
{ |
| 513 |
TEST_ASSERT_TRUE(link_linknames_equal("file.txt", "file.txt")); |
| 514 |
TEST_ASSERT_TRUE(link_linknames_equal("file.txt", "file.txt/")); |
| 515 |
TEST_ASSERT_TRUE(link_linknames_equal("file.txt/", "file.txt")); |
| 516 |
TEST_ASSERT_TRUE(link_linknames_equal("dir/", "dir/")); |
| 517 |
TEST_ASSERT_TRUE(link_linknames_equal("dir", "dir/")); |
| 518 |
|
| 519 |
TEST_ASSERT_FALSE(link_linknames_equal("file.txt", "other.txt")); |
| 520 |
TEST_ASSERT_FALSE(link_linknames_equal("file.txt", "file.txt//")); |
| 521 |
TEST_ASSERT_FALSE(link_linknames_equal("", "a/")); |
| 522 |
TEST_ASSERT_FALSE(link_linknames_equal("a", "b")); |
| 523 |
} |
| 524 |
|
| 525 |
void test_link_hash_str(void) |
| 526 |
{ |
| 527 |
unsigned int h1 = link_hash_str("file.txt"); |
| 528 |
unsigned int h2 = link_hash_str("file.txt/"); |
| 529 |
unsigned int h3 = link_hash_str("file.txt//"); |
| 530 |
TEST_ASSERT_EQUAL_UINT(h1, h2); |
| 531 |
TEST_ASSERT_EQUAL_UINT(h1, h3); |
| 532 |
|
| 533 |
unsigned int h4 = link_hash_str("dir"); |
| 534 |
unsigned int h5 = link_hash_str("dir/"); |
| 535 |
TEST_ASSERT_EQUAL_UINT(h4, h5); |
| 536 |
|
| 537 |
unsigned int h6 = link_hash_str("other"); |
| 538 |
TEST_ASSERT_NOT_EQUAL(h1, h6); |
| 539 |
} |
| 540 |
|
| 541 |
void test_LinkHashSet(void) |
| 542 |
{ |
| 543 |
LinkHashSet *set = LinkHashSet_new(4); |
| 544 |
TEST_ASSERT_NOT_NULL(set); |
| 545 |
|
| 546 |
TEST_ASSERT_EQUAL_INT(1, LinkHashSet_add(set, "file.txt")); |
| 547 |
TEST_ASSERT_EQUAL_INT(0, LinkHashSet_add(set, "file.txt")); |
| 548 |
TEST_ASSERT_EQUAL_INT(0, LinkHashSet_add(set, "file.txt/")); |
| 549 |
TEST_ASSERT_EQUAL_INT(1, LinkHashSet_add(set, "file.txt//")); |
| 550 |
|
| 551 |
TEST_ASSERT_EQUAL_INT(1, LinkHashSet_add(set, "other.txt")); |
| 552 |
TEST_ASSERT_EQUAL_INT(1, LinkHashSet_add(set, "another.txt")); |
| 553 |
|
| 554 |
|
| 555 |
TEST_ASSERT_EQUAL_INT(1, LinkHashSet_add(set, "resize1.txt")); |
| 556 |
TEST_ASSERT_EQUAL_INT(1, LinkHashSet_add(set, "resize2.txt")); |
| 557 |
TEST_ASSERT_EQUAL_INT(0, LinkHashSet_add(set, "resize1.txt")); |
| 558 |
|
| 559 |
LinkHashSet_free(set); |
| 560 |
} |
| 561 |
|
| 562 |
void test_LinkTable_parse_html_duplicates(void) |
| 563 |
{ |
| 564 |
LinkTable *table = LinkTable_alloc("https://example.com/dir/"); |
| 565 |
TEST_ASSERT_NOT_NULL(table); |
| 566 |
TEST_ASSERT_EQUAL_INT(1, table->size); |
| 567 |
|
| 568 |
const char *html = "<html>\n" |
| 569 |
"<body>\n" |
| 570 |
" <a href=\"a.txt\">a.txt</a>\n" |
| 571 |
" <a href=\"a.txt\">a.txt duplicate</a>\n" |
| 572 |
" <a href=\"a.txt/\">a.txt slash duplicate</a>\n" |
| 573 |
" <a href=\"b.txt\">b.txt</a>\n" |
| 574 |
" <a href=\"b.txt/\">b.txt slash duplicate</a>\n" |
| 575 |
"</body>\n" |
| 576 |
"</html>\n"; |
| 577 |
|
| 578 |
LinkTable_parse_html(table, "https://example.com/dir/", html); |
| 579 |
|
| 580 |
|
| 581 |
TEST_ASSERT_EQUAL_INT(3, table->size); |
| 582 |
|
| 583 |
TEST_ASSERT_EQUAL_STRING("", table->links[0]->linkname); |
| 584 |
TEST_ASSERT_EQUAL_STRING("a.txt", table->links[1]->linkname); |
| 585 |
TEST_ASSERT_EQUAL_STRING("b.txt", table->links[2]->linkname); |
| 586 |
|
| 587 |
LinkTable_free(table); |
| 588 |
} |
| 589 |
|
| 590 |
int main(void) |
| 591 |
{ |
| 592 |
UNITY_BEGIN(); |
| 593 |
|
| 594 |
|
| 595 |
RUN_TEST(test_is_external_url_http); |
| 596 |
RUN_TEST(test_is_external_url_https); |
| 597 |
RUN_TEST(test_is_external_url_relative); |
| 598 |
RUN_TEST(test_is_external_url_absolute_path); |
| 599 |
RUN_TEST(test_is_external_url_ftp); |
| 600 |
RUN_TEST(test_is_external_url_null); |
| 601 |
RUN_TEST(test_is_external_url_uppercase); |
| 602 |
|
| 603 |
|
| 604 |
RUN_TEST(test_is_cross_origin_different_host); |
| 605 |
RUN_TEST(test_is_cross_origin_same_host); |
| 606 |
RUN_TEST(test_is_cross_origin_different_scheme); |
| 607 |
RUN_TEST(test_is_cross_origin_different_port); |
| 608 |
RUN_TEST(test_is_cross_origin_same_host_with_port); |
| 609 |
RUN_TEST(test_is_cross_origin_no_trailing_slash_same); |
| 610 |
RUN_TEST(test_is_cross_origin_no_trailing_slash_different); |
| 611 |
RUN_TEST(test_is_cross_origin_both_no_trailing_slash_same); |
| 612 |
RUN_TEST(test_is_cross_origin_null); |
| 613 |
RUN_TEST(test_is_cross_origin_case_insensitive); |
| 614 |
RUN_TEST(test_is_cross_origin_with_query_and_fragment); |
| 615 |
RUN_TEST(test_is_cross_origin_default_port_normalization_http); |
| 616 |
RUN_TEST(test_is_cross_origin_default_port_normalization_https); |
| 617 |
RUN_TEST(test_is_cross_origin_non_default_port_not_equal); |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
RUN_TEST(test_external_url_to_filename_simple); |
| 622 |
RUN_TEST(test_external_url_to_filename_nested_path); |
| 623 |
RUN_TEST(test_external_url_to_filename_dir); |
| 624 |
RUN_TEST(test_external_url_to_filename_encoded); |
| 625 |
RUN_TEST(test_external_url_to_filename_query); |
| 626 |
RUN_TEST(test_external_url_to_filename_root); |
| 627 |
RUN_TEST(test_external_url_to_filename_null); |
| 628 |
RUN_TEST(test_external_url_to_filename_fragment); |
| 629 |
RUN_TEST(test_external_url_to_filename_query_and_fragment); |
| 630 |
RUN_TEST(test_external_url_to_filename_encoded_slashes); |
| 631 |
|
| 632 |
|
| 633 |
RUN_TEST(test_HTML_external_link_file); |
| 634 |
RUN_TEST(test_HTML_external_link_dir); |
| 635 |
RUN_TEST(test_HTML_external_link_disabled); |
| 636 |
RUN_TEST(test_HTML_external_link_same_origin); |
| 637 |
RUN_TEST(test_HTML_external_link_dedup_first_wins); |
| 638 |
RUN_TEST(test_HTML_external_link_dot_and_dotdot); |
| 639 |
RUN_TEST(test_Link_preserves_preset_f_url); |
| 640 |
|
| 641 |
RUN_TEST(test_url_to_cache_path_null); |
| 642 |
RUN_TEST(test_url_to_cache_path_local); |
| 643 |
RUN_TEST(test_url_to_cache_path_external_sanitization); |
| 644 |
|
| 645 |
|
| 646 |
RUN_TEST(test_LinkTable_alloc); |
| 647 |
RUN_TEST(test_LinkTable_add); |
| 648 |
RUN_TEST(test_Link_download_zero_length); |
| 649 |
RUN_TEST(test_link_linknames_equal); |
| 650 |
RUN_TEST(test_link_hash_str); |
| 651 |
RUN_TEST(test_LinkHashSet); |
| 652 |
RUN_TEST(test_LinkTable_parse_html_duplicates); |
| 653 |
return UNITY_END(); |
| 654 |
} |