1#!/bin/sh
2##
3##  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
4##
5##  Use of this source code is governed by a BSD-style license
6##  that can be found in the LICENSE file in the root of the source
7##  tree. An additional intellectual property rights grant can be found
8##  in the file PATENTS.  All contributing project authors may
9##  be found in the AUTHORS file in the root of the source tree.
10##
11##  This file contains shell code shared by test scripts for libvpx tools.
12
13# Use $VPX_TEST_TOOLS_COMMON_SH as a pseudo include guard.
14if [ -z "${VPX_TEST_TOOLS_COMMON_SH}" ]; then
15VPX_TEST_TOOLS_COMMON_SH=included
16
17set -e
18devnull='> /dev/null 2>&1'
19VPX_TEST_PREFIX=""
20
21elog() {
22  echo "$@" 1>&2
23}
24
25vlog() {
26  if [ "${VPX_TEST_VERBOSE_OUTPUT}" = "yes" ]; then
27    echo "$@"
28  fi
29}
30
31# Sets $VPX_TOOL_TEST to the name specified by positional parameter one.
32test_begin() {
33  VPX_TOOL_TEST="${1}"
34}
35
36# Clears the VPX_TOOL_TEST variable after confirming that $VPX_TOOL_TEST matches
37# positional parameter one.
38test_end() {
39  if [ "$1" != "${VPX_TOOL_TEST}" ]; then
40    echo "FAIL completed test mismatch!."
41    echo "  completed test: ${1}"
42    echo "  active test: ${VPX_TOOL_TEST}."
43    return 1
44  fi
45  VPX_TOOL_TEST='<unset>'
46}
47
48# Echoes the target configuration being tested.
49test_configuration_target() {
50  vpx_config_mk="${LIBVPX_CONFIG_PATH}/config.mk"
51  # Find the TOOLCHAIN line, split it using ':=' as the field separator, and
52  # print the last field to get the value. Then pipe the value to tr to consume
53  # any leading/trailing spaces while allowing tr to echo the output to stdout.
54  awk -F ':=' '/TOOLCHAIN/ { print $NF }' "${vpx_config_mk}" | tr -d ' '
55}
56
57# Trap function used for failure reports and tool output directory removal.
58# When the contents of $VPX_TOOL_TEST do not match the string '<unset>', reports
59# failure of test stored in $VPX_TOOL_TEST.
60cleanup() {
61  if [ -n "${VPX_TOOL_TEST}" ] && [ "${VPX_TOOL_TEST}" != '<unset>' ]; then
62    echo "FAIL: $VPX_TOOL_TEST"
63  fi
64  if [ -n "${VPX_TEST_OUTPUT_DIR}" ] && [ -d "${VPX_TEST_OUTPUT_DIR}" ]; then
65    rm -rf "${VPX_TEST_OUTPUT_DIR}"
66  fi
67}
68
69# Echoes the git hash portion of the VERSION_STRING variable defined in
70# $LIBVPX_CONFIG_PATH/config.mk to stdout, or the version number string when
71# no git hash is contained in VERSION_STRING.
72config_hash() {
73  vpx_config_mk="${LIBVPX_CONFIG_PATH}/config.mk"
74  # Find VERSION_STRING line, split it with "-g" and print the last field to
75  # output the git hash to stdout.
76  vpx_version=$(awk -F -g '/VERSION_STRING/ {print $NF}' "${vpx_config_mk}")
77  # Handle two situations here:
78  # 1. The default case: $vpx_version is a git hash, so echo it unchanged.
79  # 2. When being run a non-dev tree, the -g portion is not present in the
80  #    version string: It's only the version number.
81  #    In this case $vpx_version is something like 'VERSION_STRING=v1.3.0', so
82  #    we echo only what is after the '='.
83  echo "${vpx_version##*=}"
84}
85
86# Echoes the short form of the current git hash.
87current_hash() {
88  if git --version > /dev/null 2>&1; then
89    (cd "$(dirname "${0}")"
90    git rev-parse --short HEAD)
91  else
92    # Return the config hash if git is unavailable: Fail silently, git hashes
93    # are used only for warnings.
94    config_hash
95  fi
96}
97
98# Echoes warnings to stdout when git hash in vpx_config.h does not match the
99# current git hash.
100check_git_hashes() {
101  hash_at_configure_time=$(config_hash)
102  hash_now=$(current_hash)
103
104  if [ "${hash_at_configure_time}" != "${hash_now}" ]; then
105    echo "Warning: git hash has changed since last configure."
106  fi
107}
108
109# This script requires that the LIBVPX_BIN_PATH, LIBVPX_CONFIG_PATH, and
110# LIBVPX_TEST_DATA_PATH variables are in the environment: Confirm that
111# the variables are set and that they all evaluate to directory paths.
112verify_vpx_test_environment() {
113  if [ ! -d "${LIBVPX_BIN_PATH}" ]; then
114    echo "The LIBVPX_BIN_PATH environment variable must be set."
115    return 1
116  fi
117  if [ ! -d "${LIBVPX_CONFIG_PATH}" ]; then
118    echo "The LIBVPX_CONFIG_PATH environment variable must be set."
119    return 1
120  fi
121  if [ ! -d "${LIBVPX_TEST_DATA_PATH}" ]; then
122    echo "The LIBVPX_TEST_DATA_PATH environment variable must be set."
123    return 1
124  fi
125}
126
127# Greps vpx_config.h in LIBVPX_CONFIG_PATH for positional parameter one, which
128# should be a LIBVPX preprocessor flag. Echoes yes to stdout when the feature
129# is available.
130vpx_config_option_enabled() {
131  vpx_config_option="${1}"
132  vpx_config_file="${LIBVPX_CONFIG_PATH}/vpx_config.h"
133  config_line=$(grep "${vpx_config_option}" "${vpx_config_file}")
134  if echo "${config_line}" | egrep -q '1$'; then
135    echo yes
136  fi
137}
138
139# Echoes yes when output of test_configuration_target() contains win32 or win64.
140is_windows_target() {
141  if test_configuration_target \
142     | grep -q -e win32 -e win64 > /dev/null 2>&1; then
143    echo yes
144  fi
145}
146
147# Echoes yes to stdout when the file named by positional parameter one exists
148# in LIBVPX_BIN_PATH, and is executable.
149vpx_tool_available() {
150  local tool_name="$1"
151  local tool="${LIBVPX_BIN_PATH}/${tool_name}${VPX_TEST_EXE_SUFFIX}"
152  [ -x "${tool}" ] && echo yes
153}
154
155# Echoes yes to stdout when vpx_config_option_enabled() reports yes for
156# CONFIG_VP8_DECODER.
157vp8_decode_available() {
158  [ "$(vpx_config_option_enabled CONFIG_VP8_DECODER)" = "yes" ] && echo yes
159}
160
161# Echoes yes to stdout when vpx_config_option_enabled() reports yes for
162# CONFIG_VP8_ENCODER.
163vp8_encode_available() {
164  [ "$(vpx_config_option_enabled CONFIG_VP8_ENCODER)" = "yes" ] && echo yes
165}
166
167# Echoes yes to stdout when vpx_config_option_enabled() reports yes for
168# CONFIG_VP9_DECODER.
169vp9_decode_available() {
170  [ "$(vpx_config_option_enabled CONFIG_VP9_DECODER)" = "yes" ] && echo yes
171}
172
173# Echoes yes to stdout when vpx_config_option_enabled() reports yes for
174# CONFIG_VP9_ENCODER.
175vp9_encode_available() {
176  [ "$(vpx_config_option_enabled CONFIG_VP9_ENCODER)" = "yes" ] && echo yes
177}
178
179# Echoes yes to stdout when vpx_config_option_enabled() reports yes for
180# CONFIG_WEBM_IO.
181webm_io_available() {
182  [ "$(vpx_config_option_enabled CONFIG_WEBM_IO)" = "yes" ] && echo yes
183}
184
185# Filters strings from positional parameter one using the filter specified by
186# positional parameter two. Filter behavior depends on the presence of a third
187# positional parameter. When parameter three is present, strings that match the
188# filter are excluded. When omitted, strings matching the filter are included.
189# The filtered string is echoed to stdout.
190filter_strings() {
191  strings=${1}
192  filter=${2}
193  exclude=${3}
194
195  if [ -n "${exclude}" ]; then
196    # When positional parameter three exists the caller wants to remove strings.
197    # Tell grep to invert matches using the -v argument.
198    exclude='-v'
199  else
200    unset exclude
201  fi
202
203  if [ -n "${filter}" ]; then
204    for s in ${strings}; do
205      if echo "${s}" | egrep -q ${exclude} "${filter}" > /dev/null 2>&1; then
206        filtered_strings="${filtered_strings} ${s}"
207      fi
208    done
209  else
210    filtered_strings="${strings}"
211  fi
212  echo "${filtered_strings}"
213}
214
215# Runs user test functions passed via positional parameters one and two.
216# Functions in positional parameter one are treated as environment verification
217# functions and are run unconditionally. Functions in positional parameter two
218# are run according to the rules specified in vpx_test_usage().
219run_tests() {
220  local env_tests="verify_vpx_test_environment $1"
221  local tests_to_filter="$2"
222  local test_name="${VPX_TEST_NAME}"
223
224  if [ -z "${test_name}" ]; then
225    test_name="$(basename "${0%.*}")"
226  fi
227
228  if [ "${VPX_TEST_RUN_DISABLED_TESTS}" != "yes" ]; then
229    # Filter out DISABLED tests.
230    tests_to_filter=$(filter_strings "${tests_to_filter}" ^DISABLED exclude)
231  fi
232
233  if [ -n "${VPX_TEST_FILTER}" ]; then
234    # Remove tests not matching the user's filter.
235    tests_to_filter=$(filter_strings "${tests_to_filter}" ${VPX_TEST_FILTER})
236  fi
237
238  local tests_to_run="${env_tests} ${tests_to_filter}"
239
240  check_git_hashes
241
242  # Run tests.
243  for test in ${tests_to_run}; do
244    test_begin "${test}"
245    vlog "  RUN  ${test}"
246    "${test}"
247    vlog "  PASS ${test}"
248    test_end "${test}"
249  done
250
251  local tested_config="$(test_configuration_target) @ $(current_hash)"
252  echo "${test_name}: Done, all tests pass for ${tested_config}."
253}
254
255vpx_test_usage() {
256cat << EOF
257  Usage: ${0##*/} [arguments]
258    --bin-path <path to libvpx binaries directory>
259    --config-path <path to libvpx config directory>
260    --filter <filter>: User test filter. Only tests matching filter are run.
261    --run-disabled-tests: Run disabled tests.
262    --help: Display this message and exit.
263    --test-data-path <path to libvpx test data directory>
264    --show-program-output: Shows output from all programs being tested.
265    --prefix: Allows for a user specified prefix to be inserted before all test
266              programs. Grants the ability, for example, to run test programs
267              within valgrind.
268    --verbose: Verbose output.
269
270    When the --bin-path option is not specified the script attempts to use
271    \$LIBVPX_BIN_PATH and then the current directory.
272
273    When the --config-path option is not specified the script attempts to use
274    \$LIBVPX_CONFIG_PATH and then the current directory.
275
276    When the -test-data-path option is not specified the script attempts to use
277    \$LIBVPX_TEST_DATA_PATH and then the current directory.
278EOF
279}
280
281# Returns non-zero (failure) when required environment variables are empty
282# strings.
283vpx_test_check_environment() {
284  if [ -z "${LIBVPX_BIN_PATH}" ] || \
285     [ -z "${LIBVPX_CONFIG_PATH}" ] || \
286     [ -z "${LIBVPX_TEST_DATA_PATH}" ]; then
287    return 1
288  fi
289}
290
291# Parse the command line.
292while [ -n "$1" ]; do
293  case "$1" in
294    --bin-path)
295      LIBVPX_BIN_PATH="$2"
296      shift
297      ;;
298    --config-path)
299      LIBVPX_CONFIG_PATH="$2"
300      shift
301      ;;
302    --filter)
303      VPX_TEST_FILTER="$2"
304      shift
305      ;;
306    --run-disabled-tests)
307      VPX_TEST_RUN_DISABLED_TESTS=yes
308      ;;
309    --help)
310      vpx_test_usage
311      exit
312      ;;
313    --test-data-path)
314      LIBVPX_TEST_DATA_PATH="$2"
315      shift
316      ;;
317    --prefix)
318      VPX_TEST_PREFIX="$2"
319      shift
320      ;;
321    --verbose)
322      VPX_TEST_VERBOSE_OUTPUT=yes
323      ;;
324    --show-program-output)
325      devnull=
326      ;;
327    *)
328      vpx_test_usage
329      exit 1
330      ;;
331  esac
332  shift
333done
334
335# Handle running the tests from a build directory without arguments when running
336# the tests on *nix/macosx.
337LIBVPX_BIN_PATH="${LIBVPX_BIN_PATH:-.}"
338LIBVPX_CONFIG_PATH="${LIBVPX_CONFIG_PATH:-.}"
339LIBVPX_TEST_DATA_PATH="${LIBVPX_TEST_DATA_PATH:-.}"
340
341# Create a temporary directory for output files, and a trap to clean it up.
342if [ -n "${TMPDIR}" ]; then
343  VPX_TEST_TEMP_ROOT="${TMPDIR}"
344elif [ -n "${TEMPDIR}" ]; then
345  VPX_TEST_TEMP_ROOT="${TEMPDIR}"
346else
347  VPX_TEST_TEMP_ROOT=/tmp
348fi
349
350VPX_TEST_RAND=$(awk 'BEGIN { srand(); printf "%d\n",(rand() * 32768)}')
351VPX_TEST_OUTPUT_DIR="${VPX_TEST_TEMP_ROOT}/vpx_test_${VPX_TEST_RAND}"
352
353if ! mkdir -p "${VPX_TEST_OUTPUT_DIR}" || \
354   [ ! -d "${VPX_TEST_OUTPUT_DIR}" ]; then
355  echo "${0##*/}: Cannot create output directory, giving up."
356  echo "${0##*/}:   VPX_TEST_OUTPUT_DIR=${VPX_TEST_OUTPUT_DIR}"
357  exit 1
358fi
359
360if [ "$(is_windows_target)" = "yes" ]; then
361  VPX_TEST_EXE_SUFFIX=".exe"
362fi
363
364# Variables shared by tests.
365VP8_IVF_FILE="${LIBVPX_TEST_DATA_PATH}/vp80-00-comprehensive-001.ivf"
366VP9_IVF_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-09-subpixel-00.ivf"
367
368VP9_WEBM_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-00-quantizer-00.webm"
369
370YUV_RAW_INPUT="${LIBVPX_TEST_DATA_PATH}/hantro_collage_w352h288.yuv"
371YUV_RAW_INPUT_WIDTH=352
372YUV_RAW_INPUT_HEIGHT=288
373
374# Setup a trap function to clean up after tests complete.
375trap cleanup EXIT
376
377vlog "$(basename "${0%.*}") test configuration:
378  LIBVPX_BIN_PATH=${LIBVPX_BIN_PATH}
379  LIBVPX_CONFIG_PATH=${LIBVPX_CONFIG_PATH}
380  LIBVPX_TEST_DATA_PATH=${LIBVPX_TEST_DATA_PATH}
381  VP8_IVF_FILE=${VP8_IVF_FILE}
382  VP9_IVF_FILE=${VP9_IVF_FILE}
383  VP9_WEBM_FILE=${VP9_WEBM_FILE}
384  VPX_TEST_EXE_SUFFIX=${VPX_TEST_EXE_SUFFIX}
385  VPX_TEST_FILTER=${VPX_TEST_FILTER}
386  VPX_TEST_OUTPUT_DIR=${VPX_TEST_OUTPUT_DIR}
387  VPX_TEST_PREFIX=${VPX_TEST_PREFIX}
388  VPX_TEST_RAND=${VPX_TEST_RAND}
389  VPX_TEST_RUN_DISABLED_TESTS=${VPX_TEST_RUN_DISABLED_TESTS}
390  VPX_TEST_SHOW_PROGRAM_OUTPUT=${VPX_TEST_SHOW_PROGRAM_OUTPUT}
391  VPX_TEST_TEMP_ROOT=${VPX_TEST_TEMP_ROOT}
392  VPX_TEST_VERBOSE_OUTPUT=${VPX_TEST_VERBOSE_OUTPUT}
393  YUV_RAW_INPUT=${YUV_RAW_INPUT}
394  YUV_RAW_INPUT_WIDTH=${YUV_RAW_INPUT_WIDTH}
395  YUV_RAW_INPUT_HEIGHT=${YUV_RAW_INPUT_HEIGHT}"
396
397fi  # End $VPX_TEST_TOOLS_COMMON_SH pseudo include guard.
398