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 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
|
#!/bin/bash
# Bash functions to run an executable for testing.
#
# Version: 20161115
#
# When CHECK_WITH_GDB is set to a non-empty value the test executable
# is run with gdb, otherwise it is run without.
#
# When CHECK_WITH_STDERR is set to a non-empty value the test executable
# is run with error output to stderr.
#
# When CHECK_WITH_VALGRIND is set to a non-empty value the test executable
# is run with valgrind, otherwise it is run without.
EXIT_SUCCESS=0;
EXIT_FAILURE=1;
EXIT_IGNORE=77;
# Checks the availability of a binary and exits if not available.
#
# Arguments:
# a string containing the name of the binary
#
assert_availability_binary()
{
local BINARY=$1;
which ${BINARY} > /dev/null 2>&1;
if test $? -ne ${EXIT_SUCCESS};
then
echo "Missing binary: ${BINARY}";
echo "";
exit ${EXIT_FAILURE};
fi
}
# Checks the availability of binaries and exits if not available.
#
# Globals:
# CHECK_WITH_GDB
# CHECK_WITH_VALGRIND
#
assert_availability_binaries()
{
assert_availability_binary cat;
assert_availability_binary cut;
assert_availability_binary diff;
assert_availability_binary file;
assert_availability_binary gzip;
assert_availability_binary ls;
assert_availability_binary readlink;
assert_availability_binary sed;
assert_availability_binary tr;
assert_availability_binary uname;
assert_availability_binary wc;
assert_availability_binary zcat;
if test -n "${CHECK_WITH_GDB}";
then
assert_availability_binary gdb;
elif test -n "${CHECK_WITH_VALGRIND}";
then
assert_availability_binary valgrind;
fi
}
# Checks if the input directory is in the ignore list.
#
# Arguments:
# a string containing the path of the test input directory
# a string containing space separated ignore list
#
# Returns:
# an integer containing the exit status to indicate the input directory
# was found in the ignore list.
#
check_for_directory_in_ignore_list()
{
local INPUT_DIRECTORY=$1;
local IGNORE_LIST=$@;
local INPUT_BASENAME=`basename ${INPUT_DIRECTORY}`;
for LIST_ELEMENT in `echo "${IGNORE_LIST}" | tr ' ' '\n'`;
do
if test "${LIST_ELEMENT}" = "${INPUT_BASENAME}";
then
return ${EXIT_SUCCESS};
fi
done
return ${EXIT_FAILURE};
}
# Searches for the binary variant of the executable in case the test executable
# refers to a libtool shell script.
#
# Arguments:
# a string containing the path of the test executable
#
# Returns:
# a string containing the path of the binary variant of the test executable
#
find_binary_executable()
{
local TEST_EXECUTABLE=$1;
TEST_EXECUTABLE=$( readlink_f "${TEST_EXECUTABLE}" );
# Note that the behavior of `file -bi` is not helpful on Mac OS X.
local EXECUTABLE_TYPE=`file -b ${TEST_EXECUTABLE}`;
# Check if the test executable is a libtool shell script.
# Linux: POSIX shell script, ASCII text executable, with very long lines
# Mac OS X: POSIX shell script text executable
echo "${EXECUTABLE_TYPE}" | grep "POSIX shell script" > /dev/null 2>&1;
RESULT=$?;
if test ${RESULT} -eq ${EXIT_SUCCESS};
then
local TEST_EXECUTABLE_BASENAME=`basename ${TEST_EXECUTABLE}`;
local TEST_EXECUTABLE_DIRNAME=`dirname ${TEST_EXECUTABLE}`;
TEST_EXECUTABLE="${TEST_EXECUTABLE_DIRNAME}/.libs/${TEST_EXECUTABLE_BASENAME}";
if test -x ${TEST_EXECUTABLE};
then
# Note that the behavior of `file -bi` is not helpful on Mac OS X.
EXECUTABLE_TYPE=`file -b ${TEST_EXECUTABLE}`;
# Linux: ELF 64-bit LSB executable, x86-64, ...
# Mac OS X: Mach-O 64-bit executable x86_64
echo "${EXECUTABLE_TYPE}" | grep "executable" > /dev/null 2>&1;
RESULT=$?;
if test ${RESULT} -ne ${EXIT_SUCCESS};
then
echo "Invalid test executable: ${TEST_EXECUTABLE}";
exit ${EXIT_FAILURE};
fi
fi
fi
echo ${TEST_EXECUTABLE};
}
# Searches for the path to the binary variant of the library.
#
# Arguments:
# a string containing the path of the test executable
#
# Returns:
# a string containing the path of the binary variant of the library.
#
find_binary_library_path()
{
local TEST_EXECUTABLE=$1;
local LIBRARY_NAME="${TEST_EXECUTABLE}";
echo ${LIBRARY_NAME} | grep 'tools' > /dev/null 2>&1;
if test $? -eq ${EXIT_SUCCESS};
then
LIBRARY_NAME=`dirname ${LIBRARY_NAME}`;
LIBRARY_NAME=`dirname ${LIBRARY_NAME}`;
LIBRARY_NAME=`basename ${LIBRARY_NAME} | sed 's/\(.*\)tools$/lib\1/'`;
else
LIBRARY_NAME=`basename ${LIBRARY_NAME} | sed 's/^py//' | sed 's/^\([^_]*\)_test_.*$/lib\1/'`;
fi
TEST_EXECUTABLE=`dirname ${TEST_EXECUTABLE}`;
TEST_EXECUTABLE=`dirname ${TEST_EXECUTABLE}`;
TEST_EXECUTABLE=`dirname ${TEST_EXECUTABLE}`;
local LIBRARY_PATH="${TEST_EXECUTABLE}/${LIBRARY_NAME}/.libs";
if ! test -d "${LIBRARY_PATH}";
then
LIBRARY_PATH="../${LIBRARY_NAME}/.libs";
fi
echo "${LIBRARY_PATH}";
}
# Searches for the path to the binary variant of the Python module
#
# Globals:
# PYTHON_VERSION
#
# Arguments:
# a string containing the path of the test executable
#
# Returns:
# a string containing the path of the binary variant of the Python module
#
find_binary_python_module_path()
{
local TEST_EXECUTABLE=$1;
local PYTHON_MODULE_NAME=`basename ${TEST_EXECUTABLE} | sed 's/^py\(.*\)_test_.*$/py\1/'`;
TEST_EXECUTABLE=`dirname ${TEST_EXECUTABLE}`;
TEST_EXECUTABLE=`dirname ${TEST_EXECUTABLE}`;
TEST_EXECUTABLE=`dirname ${TEST_EXECUTABLE}`;
PYTHON_VERSION=`echo ${PYTHON_VERSION} | cut -c1`;
local PYTHON_MODULE_PATH="${TEST_EXECUTABLE}/${PYTHON_MODULE_NAME}-python${PYTHON_VERSION}/.libs";
if ! test -d "${PYTHON_MODULE_PATH}";
then
PYTHON_MODULE_PATH="../${PYTHON_MODULE_NAME}-python${PYTHON_VERSION}/.libs";
fi
if ! test -d "${PYTHON_MODULE_PATH}";
then
PYTHON_MODULE_PATH="${TEST_EXECUTABLE}/${PYTHON_MODULE_NAME}/.libs";
fi
if ! test -d "${PYTHON_MODULE_PATH}";
then
PYTHON_MODULE_PATH="../${PYTHON_MODULE_NAME}/.libs";
fi
echo "${PYTHON_MODULE_PATH}";
}
# Determines the test option file.
#
# Arguments:
# a string containing the path of the test set directory
# a string containing the path of the test input file
# a string containing the name of the option set
#
# Returns:
# a string containing the test input files
#
get_testion_option_file()
{
local TEST_SET_DIRECTORY=$1;
local INPUT_FILE=$2;
local OPTION_SET=$3;
local INPUT_NAME=`basename "${INPUT_FILE}"`;
local OPTION_FILE="${TEST_SET_DIRECTORY}/${INPUT_NAME}.${OPTION_SET}";
echo "${OPTION_FILE}";
}
# Determines the test profile directory.
# The directory is created if it does not exist.
#
# Arguments:
# a string containing the path of the test input directory
# a string containing the name of the test profile
#
# Returns:
# a string containing the path of the test profile directory
#
get_test_profile_directory()
{
local TEST_INPUT_DIRECTORY=$1;
local TEST_PROFILE=$2;
local TEST_PROFILE_DIRECTORY="${TEST_INPUT_DIRECTORY}/.${TEST_PROFILE}";
if ! test -d "${TEST_PROFILE_DIRECTORY}";
then
mkdir "${TEST_PROFILE_DIRECTORY}";
fi
echo "${TEST_PROFILE_DIRECTORY}";
}
# Determines the test set directory.
# The directory is created if it does not exist.
#
# Arguments:
# a string containing the path of the test profile directory
# a string containing the path of the test set input directory
#
# Returns:
# a string containing the path of the test set directory
#
get_test_set_directory()
{
local TEST_PROFILE_DIRECTORY=$1;
local TEST_SET_INPUT_DIRECTORY=$2;
local TEST_SET=`basename ${TEST_SET_INPUT_DIRECTORY}`;
local TEST_SET_DIRECTORY="${TEST_PROFILE_DIRECTORY}/${TEST_SET}";
if ! test -d "${TEST_SET_DIRECTORY}";
then
mkdir "${TEST_SET_DIRECTORY}";
fi
echo "${TEST_SET_DIRECTORY}";
}
# Provides a cross-platform variant of "readlink -f"
#
# Arguments:
# a string containing a path
#
# Returns:
# a string containing the path with all symbolic links resolved
#
readlink_f() {
local TARGET="$1"
if test -f "${TARGET}";
then
while test -L "${TARGET}";
do
TARGET=`readlink "${TARGET}"`;
done
fi
local BASENAME=`basename "${TARGET}"`;
local DIRNAME=`dirname "${TARGET}"`;
DIRNAME=`(cd "${DIRNAME}" && pwd -P)`;
echo "${DIRNAME}/${BASENAME}";
}
# Reads the test profile ignore file if it exists.
#
# Arguments:
# a string containing the path of the test profile directory
#
# Returns:
# a string containing a space separated ignore list
#
read_ignore_list()
{
local TEST_PROFILE_DIRECTORY=$1;
local IGNORE_FILE="${TEST_PROFILE_DIRECTORY}/ignore";
local IGNORE_LIST="";
if test -f "${IGNORE_FILE}";
then
IGNORE_LIST=`cat ${IGNORE_FILE} | sed '/^#/d'`;
fi
echo ${IGNORE_LIST};
}
# Reads the test set option file.
#
# Arguments:
# a string containing the path of the test set directory
# a string containing the path of the test input file
# a string containing the name of the option set
#
# Returns:
# a string containing the ignore list
#
read_option_file()
{
local TEST_SET_DIRECTORY=$1;
local INPUT_FILE=$2;
local OPTION_SET=$3;
local OPTION_FILE=$(get_testion_option_file "${TEST_SET_DIRECTORY}" "${INPUT_FILE}" "${OPTION_SET}");
local OPTIONS=()
local OPTIONS_STRING=`cat "${OPTION_FILE}" | head -n 1 | sed 's/[\r\n]*$//'`;
echo "${OPTIONS_STRING}";
}
# Runs the test with optional arguments.
#
# Globals:
# CHECK_WITH_GDB
# CHECK_WITH_STDERR
# CHECK_WITH_VALGRIND
# PYTHON_VERSION
#
# Arguments:
# a string containing the test description
# a string containing the path of the test executable
# an array containing the arguments for the test executable
#
# Returns:
# an integer containg the exit status of the test executable
#
run_test_with_arguments()
{
local TEST_DESCRIPTION=$1;
local TEST_EXECUTABLE=$2;
shift 2;
local ARGUMENTS=$@;
if ! test -f "${TEST_EXECUTABLE}";
then
echo "Missing test executable: ${TEST_EXECUTABLE}";
echo "";
return ${EXIT_FAILURE};
fi
local PLATFORM=`uname -s`;
# Note that the behavior of `file -bi` is not helpful on Mac OS X.
local EXECUTABLE_TYPE=`file -b ${TEST_EXECUTABLE}`;
# Check if the test executable is a Python script.
# Linux: Python script, ASCII text executable
# Mac OS X: a python script text executable
echo "${EXECUTABLE_TYPE}" | grep -i "python script" > /dev/null 2>&1;
local IS_PYTHON_SCRIPT=$?;
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
local PYTHON=`which python${PYTHON_VERSION} 2> /dev/null`;
if ! test -x ${PYTHON};
then
echo "Missing executable: ${PYTHON}";
exit ${EXIT_FAILURE};
fi
fi
local RESULT=0;
if test -n "${CHECK_WITH_GDB}";
then
local TEST_EXECUTABLE=$( find_binary_executable ${TEST_EXECUTABLE} );
local LIBRARY_PATH=$( find_binary_library_path ${TEST_EXECUTABLE} );
local PYTHON_MODULE_PATH=$( find_binary_python_module_path ${TEST_EXECUTABLE} );
if test "${PLATFORM}" = "Darwin";
then
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" gdb -ex "set non-stop on" -ex "run" -ex "quit" --args "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
else
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" gdb -ex "set non-stop on" -ex "run" -ex "quit" --args "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
fi
else
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
LD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" gdb -ex "set non-stop on" -ex "run" -ex "quit" --args "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
else
LD_LIBRARY_PATH="${LIBRARY_PATH}" gdb -ex "set non-stop on" -ex "run" -ex "quit" --args "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
fi
fi
elif test -n "${CHECK_WITH_VALGRIND}";
then
local TEST_EXECUTABLE=$( find_binary_executable ${TEST_EXECUTABLE} );
local LIBRARY_PATH=$( find_binary_library_path ${TEST_EXECUTABLE} );
local PYTHON_MODULE_PATH=$( find_binary_python_module_path ${TEST_EXECUTABLE} );
local VALGRIND_LOG="valgrind.log-$$";
local VALGRIND_OPTIONS=("--tool=memcheck" "--leak-check=full" "--show-leak-kinds=definite,indirect,possible" "--track-origins=yes" "--log-file=${VALGRIND_LOG}");
if test "${PLATFORM}" = "Darwin";
then
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" valgrind ${VALGRIND_OPTIONS[@]} "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
else
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" valgrind ${VALGRIND_OPTIONS[@]} "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
fi
else
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
LD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" valgrind ${VALGRIND_OPTIONS[@]} "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
else
LD_LIBRARY_PATH="${LIBRARY_PATH}" valgrind ${VALGRIND_OPTIONS[@]} "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
fi
fi
if test ${RESULT} -eq ${EXIT_SUCCESS};
then
grep "All heap blocks were freed -- no leaks are possible" ${VALGRIND_LOG} > /dev/null 2>&1;
if test $? -ne ${EXIT_SUCCESS};
then
# Ignore "still reachable"
# Also see: http://valgrind.org/docs/manual/faq.html#faq.deflost
grep "definitely lost: 0 bytes in 0 blocks" ${VALGRIND_LOG} > /dev/null 2>&1;
RESULT_DIRECTLY_LOST=$?;
grep "indirectly lost: 0 bytes in 0 blocks" ${VALGRIND_LOG} > /dev/null 2>&1;
RESULT_INDIRECTLY_LOST=$?;
grep "possibly lost: 0 bytes in 0 blocks" ${VALGRIND_LOG} > /dev/null 2>&1;
RESULT_POSSIBLY_LOST=$?;
grep "suppressed: 0 bytes in 0 blocks" ${VALGRIND_LOG} > /dev/null 2>&1;
RESULT_SUPPRESSED=$?;
if test ${RESULT_DIRECTLY_LOST} -ne ${EXIT_SUCCESS} || test ${RESULT_INDIRECTLY_LOST} -ne ${EXIT_SUCCESS} || test ${RESULT_POSSIBLY_LOST} -ne ${EXIT_SUCCESS} || test ${RESULT_SUPPRESSED} -ne ${EXIT_SUCCESS};
then
echo "Memory leakage detected.";
cat ${VALGRIND_LOG};
RESULT=${EXIT_FAILURE};
fi
fi
# Detect valgrind warnings.
local NUMBER_OF_LINES=`wc -l ${VALGRIND_LOG} | awk '{ print $1 }'`;
if test ${NUMBER_OF_LINES} -ne 15 && test ${NUMBER_OF_LINES} -ne 22;
then
echo "Unsupported number of lines: ${NUMBER_OF_LINES}";
cat ${VALGRIND_LOG};
RESULT=${EXIT_FAILURE};
fi
fi
rm -f ${VALGRIND_LOG};
elif test ${IS_PYTHON_SCRIPT} -eq 0;
then
if ! test -f "${TEST_EXECUTABLE}";
then
echo "Invalid test Python script: ${TEST_EXECUTABLE}";
echo "";
return ${EXIT_FAILURE};
fi
local LIBRARY_PATH=$( find_binary_library_path ${TEST_EXECUTABLE} );
local PYTHON_MODULE_PATH=$( find_binary_python_module_path ${TEST_EXECUTABLE} );
if test "${PLATFORM}" = "Darwin";
then
if test -n "${CHECK_WITH_STDERR}";
then
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
else
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} 2> /dev/null;
RESULT=$?;
fi
else
if test -n "${CHECK_WITH_STDERR}";
then
LD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]};
RESULT=$?;
else
LD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} 2> /dev/null;
RESULT=$?;
fi
fi
else
if ! test -x "${TEST_EXECUTABLE}";
then
echo "Invalid test executable: ${TEST_EXECUTABLE}";
echo "";
return ${EXIT_FAILURE};
fi
if test -n "${CHECK_WITH_STDERR}";
then
${TEST_EXECUTABLE} ${ARGUMENTS[@]};
RESULT=$?;
else
${TEST_EXECUTABLE} ${ARGUMENTS[@]} 2> /dev/null;
RESULT=$?;
fi
fi
if test -n "${TEST_DESCRIPTION}";
then
echo -n "${TEST_DESCRIPTION} ";
if test ${RESULT} -ne ${EXIT_SUCCESS};
then
echo " (FAIL)";
else
echo " (PASS)";
fi
fi
return ${RESULT};
}
# Runs the test with an input file and optional arguments.
#
# Globals:
# CHECK_WITH_GDB
# CHECK_WITH_STDERR
# CHECK_WITH_VALGRIND
# PYTHON_VERSION
#
# Arguments:
# a string containing the path of the test executable
# a string containing the path of the test input file
# an array containing the arguments for the test executable
#
# Returns:
# an integer containg the exit status of the test executable
#
run_test_with_input_and_arguments()
{
local TEST_EXECUTABLE=$1;
local INPUT_FILE=$2;
shift 2;
local ARGUMENTS=$@;
if ! test -f "${TEST_EXECUTABLE}";
then
echo "Missing test executable: ${TEST_EXECUTABLE}";
echo "";
return ${EXIT_FAILURE};
fi
local PLATFORM=`uname -s`;
# Note that the behavior of `file -bi` is not helpful on Mac OS X.
local EXECUTABLE_TYPE=`file -b ${TEST_EXECUTABLE}`;
# Check if the test executable is a Python script.
# Linux: Python script, ASCII text executable
# Mac OS X: a python script text executable
echo "${EXECUTABLE_TYPE}" | grep -i "python script" > /dev/null 2>&1;
local IS_PYTHON_SCRIPT=$?;
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
local PYTHON=`which python${PYTHON_VERSION} 2> /dev/null`;
if ! test -x ${PYTHON};
then
echo "Missing executable: ${PYTHON}";
exit ${EXIT_FAILURE};
fi
fi
local RESULT=0;
if test -n "${CHECK_WITH_GDB}";
then
local TEST_EXECUTABLE=$( find_binary_executable ${TEST_EXECUTABLE} );
local LIBRARY_PATH=$( find_binary_library_path ${TEST_EXECUTABLE} );
local PYTHON_MODULE_PATH=$( find_binary_python_module_path ${TEST_EXECUTABLE} );
if test "${PLATFORM}" = "Darwin";
then
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" gdb -ex "set non-stop on" -ex "run" -ex "quit" --args "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
else
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" gdb -ex "set non-stop on" -ex "run" -ex "quit" --args "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
fi
else
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
LD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" gdb -ex "set non-stop on" -ex "run" -ex "quit" --args "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
else
LD_LIBRARY_PATH="${LIBRARY_PATH}" gdb -ex "set non-stop on" -ex "run" -ex "quit" --args "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
fi
fi
elif test -n "${CHECK_WITH_VALGRIND}";
then
local TEST_EXECUTABLE=$( find_binary_executable ${TEST_EXECUTABLE} );
local LIBRARY_PATH=$( find_binary_library_path ${TEST_EXECUTABLE} );
local PYTHON_MODULE_PATH=$( find_binary_python_module_path ${TEST_EXECUTABLE} );
local VALGRIND_LOG="valgrind.log-$$";
local VALGRIND_OPTIONS=("--tool=memcheck" "--leak-check=full" "--show-leak-kinds=definite,indirect,possible" "--track-origins=yes" "--log-file=${VALGRIND_LOG}");
if test "${PLATFORM}" = "Darwin";
then
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" valgrind ${VALGRIND_OPTIONS[@]} "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
else
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" valgrind ${VALGRIND_OPTIONS[@]} "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
fi
else
if test ${IS_PYTHON_SCRIPT} -eq 0;
then
LD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" valgrind ${VALGRIND_OPTIONS[@]} "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
else
LD_LIBRARY_PATH="${LIBRARY_PATH}" valgrind ${VALGRIND_OPTIONS[@]} "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
fi
fi
if test ${RESULT} -eq ${EXIT_SUCCESS};
then
grep "All heap blocks were freed -- no leaks are possible" ${VALGRIND_LOG} > /dev/null 2>&1;
if test $? -ne ${EXIT_SUCCESS};
then
# Ignore "still reachable"
# Also see: http://valgrind.org/docs/manual/faq.html#faq.deflost
grep "definitely lost: 0 bytes in 0 blocks" ${VALGRIND_LOG} > /dev/null 2>&1;
RESULT_DIRECTLY_LOST=$?;
grep "indirectly lost: 0 bytes in 0 blocks" ${VALGRIND_LOG} > /dev/null 2>&1;
RESULT_INDIRECTLY_LOST=$?;
grep "possibly lost: 0 bytes in 0 blocks" ${VALGRIND_LOG} > /dev/null 2>&1;
RESULT_POSSIBLY_LOST=$?;
grep "suppressed: 0 bytes in 0 blocks" ${VALGRIND_LOG} > /dev/null 2>&1;
RESULT_SUPPRESSED=$?;
if test ${RESULT_DIRECTLY_LOST} -ne ${EXIT_SUCCESS} || test ${RESULT_INDIRECTLY_LOST} -ne ${EXIT_SUCCESS} || test ${RESULT_POSSIBLY_LOST} -ne ${EXIT_SUCCESS} || test ${RESULT_SUPPRESSED} -ne ${EXIT_SUCCESS};
then
echo "Memory leakage detected.";
cat ${VALGRIND_LOG};
RESULT=${EXIT_FAILURE};
fi
fi
# Detect valgrind warnings.
local NUMBER_OF_LINES=`wc -l ${VALGRIND_LOG} | awk '{ print $1 }'`;
if test ${NUMBER_OF_LINES} -ne 15 && test ${NUMBER_OF_LINES} -ne 22;
then
echo "Unsupported number of lines: ${NUMBER_OF_LINES}";
cat ${VALGRIND_LOG};
RESULT=${EXIT_FAILURE};
fi
fi
rm -f ${VALGRIND_LOG};
elif test ${IS_PYTHON_SCRIPT} -eq 0;
then
if ! test -f "${TEST_EXECUTABLE}";
then
echo "Invalid test Python script: ${TEST_EXECUTABLE}";
echo "";
return ${EXIT_FAILURE};
fi
local LIBRARY_PATH=$( find_binary_library_path ${TEST_EXECUTABLE} );
local PYTHON_MODULE_PATH=$( find_binary_python_module_path ${TEST_EXECUTABLE} );
if test "${PLATFORM}" = "Darwin";
then
if test -n "${CHECK_WITH_STDERR}";
then
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
else
DYLD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}" 2> /dev/null;
RESULT=$?;
fi
else
if test -n "${CHECK_WITH_STDERR}";
then
LD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
else
LD_LIBRARY_PATH="${LIBRARY_PATH}" PYTHONPATH="${PYTHON_MODULE_PATH}" "${PYTHON}" "${TEST_EXECUTABLE}" ${ARGUMENTS[@]} "${INPUT_FILE}" 2> /dev/null;
RESULT=$?;
fi
fi
else
if ! test -x "${TEST_EXECUTABLE}";
then
echo "Invalid test executable: ${TEST_EXECUTABLE}";
echo "";
return ${EXIT_FAILURE};
fi
if test -n "${CHECK_WITH_STDERR}";
then
${TEST_EXECUTABLE} ${ARGUMENTS[@]} "${INPUT_FILE}";
RESULT=$?;
else
${TEST_EXECUTABLE} ${ARGUMENTS[@]} "${INPUT_FILE}" 2> /dev/null;
RESULT=$?;
fi
fi
return ${RESULT};
}
# Runs the test on the input file.
#
# Note that this function is not intended to be directly invoked
# from outside the test runner script.
#
# Arguments:
# a string containing the path of the test set directory
# a string containing the description of the test
# a string containing the test mode
# a string containing the name of the option set
# a string containing the path of the test executable
# a string containing the path of the test input file
# an array containing the arguments for the test executable
#
# Returns:
# an integer containg the exit status of the test executable
#
run_test_on_input_file()
{
local TEST_SET_DIRECTORY=$1;
local TEST_DESCRIPTION=$2;
local TEST_MODE=$3;
local OPTION_SET=$4;
local TEST_EXECUTABLE=$5;
local INPUT_FILE=$6;
shift 6;
local ARGUMENTS=$@;
local INPUT_NAME=`basename "${INPUT_FILE}"`;
local OPTIONS=();
local TEST_OUTPUT="${INPUT_NAME}";
if test -n "${OPTION_SET}";
then
OPTIONS_STRING=$(read_option_file "${TEST_SET_DIRECTORY}" "${INPUT_FILE}" "${OPTION_SET}");
IFS=" " read -a OPTIONS <<< "${OPTIONS_STRING}";
TEST_OUTPUT="${INPUT_NAME}-${OPTION_SET}";
fi
local TMPDIR="tmp$$";
local RESULT=0;
rm -rf ${TMPDIR};
mkdir ${TMPDIR};
if test "${TEST_MODE}" = "with_callback";
then
test_callback "${TMPDIR}" "${TEST_SET_DIRECTORY}" "${TEST_OUTPUT}" "${TEST_EXECUTABLE}" "${TEST_INPUT}" ${ARGUMENTS[@]} ${OPTIONS[@]};
RESULT=$?;
elif test "${TEST_MODE}" = "with_stdout_reference";
then
TEST_EXECUTABLE=$( readlink_f "${TEST_EXECUTABLE}" );
if ! test -x ${TEST_EXECUTABLE};
then
echo "Invalid test executable: ${TEST_EXECUTABLE}";
echo "";
return ${EXIT_FAILURE};
fi
local INPUT_FILE_FULL_PATH=$( readlink_f "${INPUT_FILE}" );
local TEST_LOG="${TEST_OUTPUT}.log";
(cd ${TMPDIR} && run_test_with_input_and_arguments "${TEST_EXECUTABLE}" "${INPUT_FILE_FULL_PATH}" ${ARGUMENTS[@]} ${OPTIONS[@]} | sed '1,2d' > "${TEST_LOG}");
RESULT=$?;
local TEST_RESULTS="${TMPDIR}/${TEST_LOG}";
local STORED_TEST_RESULTS="${TEST_SET_DIRECTORY}/${TEST_LOG}.gz";
if test -f "${STORED_TEST_RESULTS}";
then
# Using zcat here since zdiff has issues on Mac OS X.
# Note that zcat on Mac OS X requires the input from stdin.
zcat < "${STORED_TEST_RESULTS}" | diff "${TEST_RESULTS}" -;
RESULT=$?;
else
gzip "${TEST_RESULTS}";
mv "${TEST_RESULTS}.gz" ${TEST_SET_DIRECTORY};
fi
else
run_test_with_input_and_arguments "${TEST_EXECUTABLE}" "${INPUT_FILE}" ${ARGUMENTS[@]} ${OPTIONS[@]};
RESULT=$?;
fi
rm -rf ${TMPDIR};
if test -n "${TEST_DESCRIPTION}";
then
if test -z "${OPTION_SET}";
then
echo -n "${TEST_DESCRIPTION} with input: ${INPUT_FILE}";
else
echo -n "${TEST_DESCRIPTION} with option: ${OPTION_SET} and input: ${INPUT_FILE}";
fi
if test ${RESULT} -ne ${EXIT_SUCCESS};
then
echo " (FAIL)";
else
echo " (PASS)";
fi
fi
return ${RESULT};
}
# Runs the test with options on the input file.
#
# Note that this function is not intended to be directly invoked
# from outside the test runner script.
#
# Arguments:
# a string containing the path of the test set directory
# a string containing the description of the test
# a string containing the test mode
# a string containing the name of the option sets
# a string containing the path of the test executable
# a string containing the path of the test input file
# an array containing the arguments for the test executable
#
# Returns:
# an integer containg the exit status of the test executable
#
run_test_on_input_file_with_options()
{
local TEST_SET_DIRECTORY=$1;
local TEST_DESCRIPTION=$2;
local TEST_MODE=$3;
local OPTION_SETS=$4;
local TEST_EXECUTABLE=$5;
local INPUT_FILE=$6;
shift 6;
local ARGUMENTS=$@;
local RESULT=${EXIT_SUCCESS};
local TESTED_WITH_OPTIONS=0;
for OPTION_SET in `echo ${OPTION_SETS} | tr ' ' '\n'`;
do
local OPTION_FILE=$(get_testion_option_file "${TEST_SET_DIRECTORY}" "${INPUT_FILE}" "${OPTION_SET}");
if ! test -f ${OPTION_FILE};
then
continue
fi
run_test_on_input_file "${TEST_SET_DIRECTORY}" "${TEST_DESCRIPTION}" "${TEST_MODE}" "${OPTION_SET}" "${TEST_EXECUTABLE}" "${INPUT_FILE}" ${ARGUMENTS[@]};
RESULT=$?;
if test ${RESULT} -ne ${EXIT_SUCCESS};
then
break;
fi
TESTED_WITH_OPTIONS=1;
done
if test ${RESULT} -eq ${EXIT_SUCCESS} && test ${TESTED_WITH_OPTIONS} -eq 0;
then
run_test_on_input_file "${TEST_SET_DIRECTORY}" "${TEST_DESCRIPTION}" "${TEST_MODE}" "" "${TEST_EXECUTABLE}" "${INPUT_FILE}" ${ARGUMENTS[@]};
RESULT=$?;
fi
return ${RESULT};
}
# Runs the test on the input directory.
#
# Globals:
# CHECK_WITH_GDB
# CHECK_WITH_VALGRIND
#
# Arguments:
# a string containing the name of the test profile
# a string containing the description of the test
# a string containing the test mode, supported tests modes are:
# default: the test executable should be be run without any test
# conditions.
# with_callback: the test executable should be run and the callback
# function should be called afterwards. The name of the
# callback function is "test_callback".
# with_stdout_reference: the test executable should be run and its output
# to stdout, except for the first 2 lines, should
# be compared to a reference file, if available.
# Note the globals override the test mode.
# a string containing the name of the option sets
# a string containing the path of the test executable
# a string containing the path of the test input directory
# a string containing the input glob
# an array containing the arguments for the test executable
#
# Returns:
# an integer containg the exit status of the test executable
#
run_test_on_input_directory()
{
local TEST_PROFILE=$1;
local TEST_DESCRIPTION=$2;
local TEST_MODE=$3;
local OPTION_SETS=$4;
local TEST_EXECUTABLE=$5;
local TEST_INPUT_DIRECTORY=$6;
local INPUT_GLOB=$7;
shift 7;
local ARGUMENTS=$@;
assert_availability_binaries;
if ! test "${TEST_MODE}" = "default" && test "${TEST_MODE}" != "with_callback" && ! test "${TEST_MODE}" = "with_stdout_reference";
then
echo "Unsupported test mode: ${TEST_MODE}";
echo "";
return ${EXIT_FAILURE};
fi
if ! test -f "${TEST_EXECUTABLE}";
then
echo "Missing test executable: ${TEST_EXECUTABLE}";
echo "";
return ${EXIT_FAILURE};
fi
if ! test -d "${TEST_INPUT_DIRECTORY}";
then
echo "Test input directory: ${TEST_INPUT_DIRECTORY} not found.";
return ${EXIT_IGNORE};
fi
local RESULT=`ls ${TEST_INPUT_DIRECTORY}/* | tr ' ' '\n' | wc -l`;
if test ${RESULT} -eq ${EXIT_SUCCESS};
then
echo "No files or directories found in the test input directory: ${TEST_INPUT_DIRECTORY}";
return ${EXIT_IGNORE};
fi
local TEST_PROFILE_DIRECTORY=$(get_test_profile_directory "${TEST_INPUT_DIRECTORY}" "${TEST_PROFILE}");
local IGNORE_LIST=$(read_ignore_list "${TEST_PROFILE_DIRECTORY}");
RESULT=${EXIT_SUCCESS};
for TEST_SET_INPUT_DIRECTORY in ${TEST_INPUT_DIRECTORY}/*;
do
if ! test -d "${TEST_SET_INPUT_DIRECTORY}";
then
continue;
fi
if check_for_directory_in_ignore_list "${TEST_SET_INPUT_DIRECTORY}" "${IGNORE_LIST}";
then
continue;
fi
local TEST_SET_DIRECTORY=$(get_test_set_directory "${TEST_PROFILE_DIRECTORY}" "${TEST_SET_INPUT_DIRECTORY}");
local INPUT_FILES="";
if test -f "${TEST_SET_DIRECTORY}/files";
then
while read -r INPUT_FILE;
do
run_test_on_input_file_with_options "${TEST_SET_DIRECTORY}" "${TEST_DESCRIPTION}" "${TEST_MODE}" "${OPTION_SETS}" "${TEST_EXECUTABLE}" "${INPUT_FILE}" ${ARGUMENTS[@]};
RESULT=$?;
if test ${RESULT} -ne ${EXIT_SUCCESS};
then
return ${RESULT};
fi
done < <(cat ${TEST_SET_DIRECTORY}/files | sed "s?^?${TEST_SET_INPUT_DIRECTORY}/?");
else
while read -r INPUT_FILE;
do
run_test_on_input_file_with_options "${TEST_SET_DIRECTORY}" "${TEST_DESCRIPTION}" "${TEST_MODE}" "${OPTION_SETS}" "${TEST_EXECUTABLE}" "${INPUT_FILE}" ${ARGUMENTS[@]};
RESULT=$?;
if test ${RESULT} -ne ${EXIT_SUCCESS};
then
return ${RESULT};
fi
done < <(ls -1 ${TEST_SET_INPUT_DIRECTORY}/${INPUT_GLOB});
fi
done
return ${RESULT};
}
|