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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
|
#include <stdio.h>
#include <string.h>
#ifdef WIN32
#include <Windows.h>
#include <direct.h>
#else
#include <fcntl.h>
#include <sys/wait.h>
#define _rmdir rmdir
#define _mkdir mkdir
#define CopyFile copyfile
#define DeleteFile deletefile
#endif
#include <limits.h>
#include <stdlib.h>
#include <libmediascan.h>
#include "../src/mediascan.h"
#include "../src/common.h"
#include "../src/database.h"
#include <CUnit/Basic.h>
#ifndef WIN32
void Sleep(int ms)
{
usleep(ms * 1000); // Convert to usec
} /* Sleep() */
int copyfile(char *source, char *dest, int not_used)
{
int childExitStatus;
pid_t pid;
if (!source || !dest) {
return FALSE;
}
pid = fork();
if (pid == 0) { /* child */
execl("/bin/cp", "/bin/cp", source, dest, (char *)0);
}
else if (pid < 0) {
return FALSE;
}
else {
/* parent - wait for child - this has all error handling, you
* could just call wait() as long as you are only expecting to
* have one child process at a time.
*/
pid_t ws = waitpid( pid, &childExitStatus, WNOHANG);
if (ws == -1)
{
return FALSE;
}
if( WIFEXITED(childExitStatus)) /* exit code in childExitStatus */
{
int status = WEXITSTATUS(childExitStatus); /* zero is normal exit */
/* handle non-zero as you wish */
}
}
return TRUE;
} /* copyfile() */
int deletefile(char *source)
{
int childExitStatus;
pid_t pid;
if (!source) {
return FALSE;
}
pid = fork();
if (pid == 0) { /* child */
execl("/bin/rm", "/bin/rm", source, (char *)0);
}
else if (pid < 0) {
return FALSE;
}
else {
/* parent - wait for child - this has all error handling, you
* could just call wait() as long as you are only expecting to
* have one child process at a time.
*/
pid_t ws = waitpid( pid, &childExitStatus, WNOHANG);
if (ws == -1)
{
return FALSE;
}
if( WIFEXITED(childExitStatus)) /* exit code in childExitStatus */
{
int status = WEXITSTATUS(childExitStatus); /* zero is normal exit */
/* handle non-zero as you wish */
}
}
return TRUE;
} /* deletefile() */
#endif
static int result_called = 0;
static int finish_called = 0;
static MediaScanResult result;
static void my_result_callback(MediaScan *s, MediaScanResult *r, void *userdata) {
result.type = r->type;
result.path = strdup(r->path);
result.flags = r->flags;
fprintf(stderr, "my_result_callback for %s\n", result.path);
if(r->error)
memcpy(result.error, r->error, sizeof(MediaScanError));
result.mime_type = strdup(r->mime_type);
result.dlna_profile = strdup(r->dlna_profile);
result.size = r->size;
result.mtime = r->mtime;
result.bitrate = r->bitrate;
result.duration_ms = r->duration_ms;
if(r->audio)
{
result.audio = malloc(sizeof(MediaScanAudio));
memcpy( result.audio, r->audio, sizeof(MediaScanAudio));
}
if(r->video)
{
result.video = malloc(sizeof(MediaScanVideo));
memcpy( result.video, r->video, sizeof(MediaScanVideo));
}
if(r->image)
{
result.image = malloc(sizeof(MediaScanImage));
memcpy( result.image, r->image, sizeof(MediaScanImage));
}
result_called++;
} /* my_result_callback() */
static void my_error_callback(MediaScan *s, MediaScanError *error, void *userdata) {
fprintf(stderr, "my_error_callback, err=%d, averr=%d\n", error->error_code, error->averror);
} /* my_error_callback() */
static void my_finish_callback(MediaScan *s, void *userdata) {
fprintf(stderr, "finish_callback\n");
finish_called = TRUE;
} /* my_finish_callback() */
///-------------------------------------------------------------------------------------------------
/// Test background api
///
/// @author Henry Bennett
/// @date 03/18/2011
///-------------------------------------------------------------------------------------------------
#define MAKE_PATH(str, path, file) { strcpy((str), (path)); strcat((str), "\\"); strcat((str), (file)); }
static void PathCopyFile(const char *file, const char *src_path, const char *dest_path)
{
char src[MAX_PATH_STR_LEN];
char dest[MAX_PATH_STR_LEN];
MAKE_PATH(src, src_path, file);
MAKE_PATH(dest, dest_path, file);
printf("Copying %s to %s\n", src, dest);
CopyFile(src, dest, FALSE);
}
static void test_background_api(void) {
const char *test_path = "C:\\Siojej3";
const char *data_path = "data\\video";
const char *data_file1 = "bars-mpeg1video-mp2.mpg";
const char *data_file2 = "bars-msmpeg4-mp2.asf";
const char *data_file3 = "bars-msmpeg4v2-mp2.avi";
const char *data_file4 = "bars-vp8-vorbis.webm";
const char *data_file5 = "wmv92-with-audio.wmv";
// char src[MAX_PATH_STR_LEN];
char dest[MAX_PATH_STR_LEN];
MediaScan *s = ms_create();
CU_ASSERT_FATAL(s != NULL);
// Do some setup for the test
CU_ASSERT( _mkdir(test_path) != -1 );
result_called = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
ms_watch_directory(s, test_path);
CU_ASSERT( result_called == 0 );
Sleep(1000); // Sleep 1 second
CU_ASSERT( result_called == 0 );
// Now copy a small video file to the test directory
PathCopyFile(data_file1, data_path, test_path );
CU_ASSERT( result_called == 0 );
Sleep(1000); // Sleep 1 second
// Now process the callbacks
ms_async_process(s);
CU_ASSERT( result_called == 1 );
result_called = 0;
PathCopyFile(data_file2, data_path, test_path );
Sleep(2000); // Sleep 1 second
// Now process the callbacks
ms_async_process(s);
CU_ASSERT( result_called == 1 );
reset_bdb(s);
result_called = 0;
MAKE_PATH(dest, test_path, data_file1);
printf("Deleting %s\n", dest);
CU_ASSERT( DeleteFile(dest) == TRUE);
Sleep(1500); // Sleep 500 milliseconds
MAKE_PATH(dest, test_path, data_file2);
printf("Deleting %s\n", dest);
CU_ASSERT( DeleteFile(dest) == TRUE);
Sleep(1500); // Sleep 500 milliseconds
PathCopyFile(data_file1, data_path, test_path );
Sleep(500); // Sleep 500 milliseconds
PathCopyFile(data_file2, data_path, test_path );
Sleep(1500); // Sleep 500 milliseconds
PathCopyFile(data_file3, data_path, test_path );
Sleep(500); // Sleep 500 milliseconds
PathCopyFile(data_file4, data_path, test_path );
Sleep(100); // Sleep 500 milliseconds
PathCopyFile(data_file5, data_path, test_path );
Sleep(500); // Sleep 500 milliseconds
// Now process the callbacks
ms_async_process(s);
CU_ASSERT( result_called == 5 );
ms_destroy(s);
// Clean up the test
MAKE_PATH(dest, test_path, data_file1);
DeleteFile(dest);
MAKE_PATH(dest, test_path, data_file2);
DeleteFile(dest);
MAKE_PATH(dest, test_path, data_file3);
DeleteFile(dest);
MAKE_PATH(dest, test_path, data_file4);
DeleteFile(dest);
MAKE_PATH(dest, test_path, data_file5);
DeleteFile(dest);
CU_ASSERT( _rmdir(test_path) != -1 );
} /* test_background_api() */
static void test_background_api2(void) {
const char *test_path = "C:\\4oij3";
const char *data_path = "data\\video";
const char *data_file1 = "bars-mpeg1video-mp2.mpg";
const char *data_file2 = "bars-msmpeg4-mp2.asf";
const char *data_file3 = "bars-msmpeg4v2-mp2.avi";
const char *data_file4 = "bars-vp8-vorbis.webm";
const char *data_file5 = "wmv92-with-audio.wmv";
// char src[MAX_PATH_STR_LEN];
char dest[MAX_PATH_STR_LEN];
MediaScan *s = ms_create();
CU_ASSERT_FATAL(s != NULL);
// Do some setup for the test
CU_ASSERT( _mkdir(test_path) != -1 );
result_called = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
ms_watch_directory(s, test_path);
Sleep(1000); // Sleep 1 second
// Now copy a small video file to the test directory
PathCopyFile(data_file1, data_path, test_path );
CU_ASSERT( result_called == 0 );
// Now process the callbacks
ms_async_process(s);
CU_ASSERT( result_called == 1 );
MAKE_PATH(dest, test_path, data_file1);
CU_ASSERT( DeleteFile(dest) == TRUE);
ms_destroy(s);
// Clean up the test
CU_ASSERT( _rmdir(test_path) != -1 );
} /* test_background_api2() */
static void test_background_api3(void) {
const char *test_path = "\\\\magento\\share";
const char *test_path2 = "C:\\4o34ij3";
const char *test_path3 = "Z:\\";
const char *test_path4 = "C:\\data";
MediaScan *s = ms_create();
CU_ASSERT_FATAL(s != NULL);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT( _mkdir(test_path2) != -1 );
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
ms_watch_directory(s, test_path);
CU_ASSERT(ms_errno == MSENO_ILLEGALPARAMETER); // If we got this errno, then we got the failure we wanted
// Test a directory that looks like a mapped network drive but isn't
ms_errno = 0;
ms_watch_directory(s, test_path2);
CU_ASSERT(ms_errno == 0);
// Now test a mapped network drive
ms_errno = 0;
ms_watch_directory(s, test_path3);
CU_ASSERT(ms_errno == MSENO_ILLEGALPARAMETER);
// Now test a NTFS mounted folder
// ms_errno = 0;
// ms_watch_directory(s, test_path4);
// CU_ASSERT(ms_errno == 0);
ms_destroy(s);
// Clean up the test
CU_ASSERT( _rmdir(test_path2) != -1 );
} /* test_background_api3() */
static void test_win32_shortcuts(void) {
const char *test_path = "data\\video\\shortcuts";
MediaScan *s = ms_create();
CU_ASSERT_FATAL(s != NULL);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->npaths == 0);
ms_add_path(s, test_path);
CU_ASSERT(s->npaths == 1);
ms_scan(s);
CU_ASSERT( result_called == 1 );
ms_destroy(s);
} /* test_win32_shortcuts() */
static void test_win32_shortcut_recursion(void) {
const char *test_path1 = "data\\recursion";
const char *test_path2 = "data\\recursion2";
const char *test_path3 = "data\\recursion3";
MediaScan *s;
s = ms_create();
CU_ASSERT_FATAL(s != NULL);
/* Test #1 of the initial scan functionality */
ms_set_flags(s, MS_RESCAN | MS_CLEARDB);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->npaths == 0);
ms_add_path(s, test_path1);
CU_ASSERT(s->npaths == 1);
ms_scan(s);
CU_ASSERT( result_called == 1 );
ms_destroy(s);
/* Test #2 of the rescan functionality */
s = ms_create();
CU_ASSERT_FATAL(s != NULL);
ms_set_flags(s, MS_FULL_SCAN);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->npaths == 0);
ms_add_path(s, test_path1);
CU_ASSERT(s->npaths == 1);
ms_scan(s);
CU_ASSERT( result_called == 1 );
ms_destroy(s);
/* Test #3 of a different directory structure */
s = ms_create();
CU_ASSERT_FATAL(s != NULL);
ms_set_flags(s, MS_FULL_SCAN);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->npaths == 0);
ms_add_path(s, test_path2);
CU_ASSERT(s->npaths == 1);
ms_scan(s);
CU_ASSERT( result_called == 3 );
ms_destroy(s);
/* Test #4 of a different directory structure */
s = ms_create();
CU_ASSERT_FATAL(s != NULL);
ms_set_flags(s, MS_FULL_SCAN);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->npaths == 0);
ms_add_path(s, test_path3);
CU_ASSERT(s->npaths == 1);
ms_scan(s);
CU_ASSERT( result_called == 2 );
ms_destroy(s);
} /* test_win32_shortcut_recursion() */
static void test_juke(void) {
const char *test_path1 = "E:\\workspace\\JUKE Test Media\\Music";
const char *test_path2 = "E:\\workspace\\JUKE Test Media\\Pictures";
const char *test_path3 = "E:\\workspace\\JUKE Test Media\\Videos";
MediaScan *s;
s = ms_create();
CU_ASSERT_FATAL(s != NULL);
/* Test #1 of the initial scan functionality */
ms_set_flags(s, MS_RESCAN | MS_CLEARDB);
ms_set_log_level(DEBUG);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->npaths == 0);
ms_add_path(s, test_path1);
ms_add_path(s, test_path2);
ms_add_path(s, test_path3);
CU_ASSERT(s->npaths == 3);
ms_scan(s);
CU_ASSERT( result_called == 1 );
ms_destroy(s);
} /* test_juke() */
static void test_juke_bad_file(void) {
// const char *test_path1 = "E:\\workspace\\JUKE Test Media\\Videos\\MP4 Test Videos\\TChaseMPEG4.mp4";
const char *test_path1 = "data\\video\\TChaseMPEG4.mp4";
MediaScan *s;
s = ms_create();
CU_ASSERT_FATAL(s != NULL);
/* Test #1 of the initial scan functionality */
ms_set_flags(s, MS_RESCAN | MS_CLEARDB);
ms_set_log_level(DEBUG);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
ms_scan_file(s, test_path1, TYPE_VIDEO);
CU_ASSERT( result_called == 1 );
ms_destroy(s);
} /* test_juke_bad_file() */
static void test_linux_shortcuts(void) {
const char *test_path = "data/video/linuxshortcuts";
// file is bars-mpeg4-aac.m4v
const char *test_file1 = "data/video/linuxshortcuts/dlna_abs.symlink";
const char *test_file2 = "data/video/linuxshortcuts/dlna_rel.symlink";
const char *test_file3 = "data/video/linuxshortcuts/MPEG_POS_NTSC-ac3_abs.symlink";
const char *test_file4 = "data/video/linuxshortcuts/MPEG_POS_NTSC-ac3_rel.symlink";
const char *dest_test_file1 = "data/video/dnla/MPEG_PS_NTSC-ac3.mpg";
char out_path[MAX_PATH_STR_LEN];
MediaScan *s = ms_create();
CU_ASSERT_FATAL(s != NULL);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->npaths == 0);
//ms_add_path(s, test_file1);
CU_ASSERT(s->npaths == 1);
ms_scan(s);
CU_ASSERT( result_called == 5 );
ms_destroy(s);
s = ms_create();
CU_ASSERT_FATAL(s != NULL);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
/*
CU_ASSERT(s->npaths == 0);
ms_add_path(s, test_file3);
CU_ASSERT(s->npaths == 1);
ms_scan(s);
CU_ASSERT( result_called == 5 );
*/
// if(isAlias(test_file1))
// printf("%s is a link\n", test_file1);
// if(isAlias(test_file2))
// printf("%s is a link\n", test_file2);
// if(isAlias(test_file3))
// printf("%s is a link\n", test_file3);
// if(isAlias(test_file4))
// printf("%s is a link\n", test_file4);
// CheckMacAlias(test_file4, out_path);
// printf("Points to %s\n", out_path);
result_called = 0;
ms_scan_file(s, test_file3, TYPE_UNKNOWN);
CU_ASSERT( result_called == 1 );
//reset_bdb(s);
//result_called = 0;
//ms_scan_file(s, test_file4, TYPE_UNKNOWN);
//CU_ASSERT( result_called == 1 );
ms_destroy(s);
} /* test_linux_shortcuts() */
static void test_mac_shortcuts(void) {
const char *test_path = "data/video/macshortcuts";
const char *test_file1 = "data/video/macshortcuts/avi_alias";
const char *test_file2 = "data/video/macshortcuts/dlna";
const char *test_file3 = "data/video/macshortcuts/dlna_link";
const char *test_file4 = "data/video/macshortcuts/bars-mpeg4-mp2.avi";
const char *test_file5 = "data/video/macshortcuts/dlna_link2";
const char *test_file6 = "data/video/macshortcuts/bars-mpeg4-mp2.avi";
const char *test_file7 = "data/video/macshortcuts/avi_link";
const char *dest_test_file4 = "/Users/Fox/workspace/libmediascan/test/data/video/bars-mpeg4-mp2.avi";
char out_path[MAX_PATH_STR_LEN];
MediaScan *s = ms_create();
ms_set_log_level(DEBUG);
CU_ASSERT_FATAL(s != NULL);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->npaths == 0);
ms_add_path(s, test_file3);
CU_ASSERT(s->npaths == 1);
ms_scan(s);
CU_ASSERT( result_called == 5 );
ms_destroy(s);
s = ms_create();
CU_ASSERT_FATAL(s != NULL);
// Do some setup for the test
result_called = 0;
ms_errno = 0;
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->npaths == 0);
ms_add_path(s, test_file5);
CU_ASSERT(s->npaths == 1);
ms_scan(s);
CU_ASSERT( result_called == 5 );
//if(isAlias(test_file6))
// printf("%s is a link\n", test_file6);
//ms_scan_file(s, test_file6, TYPE_UNKNOWN);
result_called = 0;
ms_errno = 0;
reset_bdb(s);
//CU_ASSERT( isAlias(test_file1));
ms_scan_file(s, test_file1, TYPE_UNKNOWN);
CU_ASSERT( result_called == 1 );
result_called = 0;
ms_errno = 0;
reset_bdb(s);
//CU_ASSERT(isAlias(test_file7));
ms_scan_file(s, test_file7, TYPE_UNKNOWN);
CU_ASSERT( result_called == 1 );
// if(isAlias(test_file2))
// printf("%s is a link\n", test_file2);
// if(isAlias(test_file3))
// printf("%s is a link\n", test_file3);
// if(isAlias(test_file4))
// printf("%s is a link\n", test_file4);
// CheckMacAlias(test_file4, out_path);
// printf("Points to %s\n", out_path);
// ms_scan_file(s, test_file4, TYPE_UNKNOWN);
ms_destroy(s);
} /* test_mac_shortcuts() */
static void test_async_api(void) {
long time1, time2;
#ifdef WIN32
const char dir[MAX_PATH_STR_LEN] = "data\\video\\dlna";
#else
const char dir[MAX_PATH_STR_LEN] = "data/video/dlna";
struct timeval now;
#endif
MediaScan *s = ms_create();
CU_ASSERT(s->npaths == 0);
ms_add_path(s, dir);
CU_ASSERT(s->npaths == 1);
CU_ASSERT( s->async == FALSE );
ms_set_async(s, FALSE);
CU_ASSERT( s->async == FALSE );
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
ms_scan(s);
CU_ASSERT( result_called == 5 );
result_called = 0;
reset_bdb(s);
CU_ASSERT( s->async == FALSE );
ms_set_async(s, TRUE);
CU_ASSERT( s->async == TRUE );
#ifdef WIN32
time1 = GetTickCount();
#else
gettimeofday(&now, NULL);
time1 = now.tv_sec;
#endif
ms_scan(s);
CU_ASSERT( result_called == 0 );
#ifdef WIN32
time2 = GetTickCount();
#else
gettimeofday(&now, NULL);
time2 = now.tv_sec;
#endif
// Verify that the function returns almost immediately
CU_ASSERT( time2 - time1 < 20 );
Sleep(1000); // Sleep 1 second
// Now process the callbacks
ms_async_process(s);
CU_ASSERT( result_called == 5 );
ms_destroy(s);
} /* test_async_api() */
#ifdef WIN32
/* (adapted from Perl) select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
#define SOCKET_TEST(x, y) \
do { \
if((x) == (y)) \
errno = WSAGetLastError(); \
} while (0)
#define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
#define TO_SOCKET(x) _get_osfhandle(x)
static int
win32_select(int nfds, fd_set* rd, fd_set* wr, fd_set* ex, const struct timeval* timeout)
{
int r;
int i, fd, save_errno = errno;
FD_SET nrd, nwr, nex;
bool just_sleep = TRUE;
FD_ZERO(&nrd);
FD_ZERO(&nwr);
FD_ZERO(&nex);
for (i = 0; i < nfds; i++) {
if (rd && FD_ISSET(i,rd)) {
fd = TO_SOCKET(i);
FD_SET((unsigned)fd, &nrd);
just_sleep = FALSE;
}
if (wr && FD_ISSET(i,wr)) {
fd = TO_SOCKET(i);
FD_SET((unsigned)fd, &nwr);
just_sleep = FALSE;
}
if (ex && FD_ISSET(i,ex)) {
fd = TO_SOCKET(i);
FD_SET((unsigned)fd, &nex);
just_sleep = FALSE;
}
}
/* winsock seems incapable of dealing with all three fd_sets being empty,
* so do the (millisecond) sleep as a special case
*/
if (just_sleep) {
if (timeout)
Sleep(timeout->tv_sec * 1000 +
timeout->tv_usec / 1000); /* do the best we can */
else
Sleep(UINT_MAX);
return 0;
}
errno = save_errno;
SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout));
save_errno = errno;
for (i = 0; i < nfds; i++) {
if (rd && FD_ISSET(i,rd)) {
fd = TO_SOCKET(i);
if (!FD_ISSET(fd, &nrd))
FD_CLR(i,rd);
}
if (wr && FD_ISSET(i,wr)) {
fd = TO_SOCKET(i);
if (!FD_ISSET(fd, &nwr))
FD_CLR(i,wr);
}
if (ex && FD_ISSET(i,ex)) {
fd = TO_SOCKET(i);
if (!FD_ISSET(fd, &nex))
FD_CLR(i,ex);
}
}
errno = save_errno;
return r;
}
static void test_async_api2(void) {
long time1, time2;
int fd;
FD_SET rd;
struct timeval timeout;
#ifdef WIN32
const char dir[MAX_PATH_STR_LEN] = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures";
#else
const char dir[MAX_PATH_STR_LEN] = "/Users/andy/Pictures";
#endif
MediaScan *s = ms_create();
ms_set_log_level(DEBUG);
CU_ASSERT(s->npaths == 0);
ms_add_path(s, dir);
CU_ASSERT(s->npaths == 1);
CU_ASSERT(s->on_result == NULL);
ms_set_result_callback(s, my_result_callback);
CU_ASSERT(s->on_result == my_result_callback);
CU_ASSERT(s->on_error == NULL);
ms_set_error_callback(s, my_error_callback);
CU_ASSERT(s->on_error == my_error_callback);
CU_ASSERT(s->on_finish == NULL);
ms_set_finish_callback(s, my_finish_callback);
CU_ASSERT(s->on_finish == my_finish_callback);
CU_ASSERT( s->async == FALSE );
ms_set_async(s, TRUE);
CU_ASSERT( s->async == TRUE );
//ms_set_cachedir(s, "\\tmp\\libmediascan");
ms_set_flags(s, MS_USE_EXTENSION);
finish_called = 0;
ms_scan(s);
CU_ASSERT( result_called == 0 );
while (!finish_called) {
// Use select() from Perl's win32 code
fd = ms_async_fd(s);
FD_ZERO(&rd);
FD_SET(fd, &rd);
timeout.tv_sec = 10;
timeout.tv_usec = 0;
fprintf(stderr, "** waiting in win32_select for fd %d\n", fd);
win32_select(fd + 1, &rd, NULL, NULL, &timeout);
fprintf(stderr, "** select returned, fd is set? %d\n", FD_ISSET(fd, &rd) ? 1 : 0);
if (FD_ISSET(fd, &rd)) {
ms_async_process(s);
fprintf(stderr, "** async_process returned\n");
}
}
CU_ASSERT( result_called == 8 );
ms_destroy(s);
} /* test_async_api2() */
#endif
///-------------------------------------------------------------------------------------------------
/// Setup background tests.
///
/// @author Henry Bennett
/// @date 03/22/2011
///-------------------------------------------------------------------------------------------------
int setupbackground_tests() {
CU_pSuite pSuite = NULL;
/* add a suite to the registry */
pSuite = CU_add_suite("Background Scanning", NULL, NULL);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the background scanning suite */
if (
// NULL == CU_add_test(pSuite, "Test background scanning API", test_background_api)
// NULL == CU_add_test(pSuite, "Test background scanning Deletion", test_background_api2)
//#if defined(WIN32)
// NULL == CU_add_test(pSuite, "Test Async scanning API 2", test_async_api2)
//#endif
// NULL == CU_add_test(pSuite, "Test edge cases of background scanning API", test_background_api3)
// NULL == CU_add_test(pSuite, "Test Juke DB", test_juke)
// NULL == CU_add_test(pSuite, "Test a bad file in Juke", test_juke_bad_file)
#if defined(WIN32)
// NULL == CU_add_test(pSuite, "Test Win32 shortcuts", test_win32_shortcuts)
//NULL == CU_add_test(pSuite, "Test Win32 shortcut infinite recursion", test_win32_shortcut_recursion)
#elif defined(__linux__)
NULL == CU_add_test(pSuite, "Test Linux shortcuts", test_linux_shortcuts)
#else
NULL == CU_add_test(pSuite, "Test Mac shortcuts", test_mac_shortcuts)
#endif
)
{
CU_cleanup_registry();
return CU_get_error();
}
return 0;
} /* setupbackground_tests() */
|