177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#!/bin/sh
277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#
377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# Define syntax testing primitives.
477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#
577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# Copyright (c) 2016-2017 The strace developers.
777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# All rights reserved.
877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#
977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# Redistribution and use in source and binary forms, with or without
1077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# modification, are permitted provided that the following conditions
1177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# are met:
1277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# 1. Redistributions of source code must retain the above copyright
1377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#    notice, this list of conditions and the following disclaimer.
1477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# 2. Redistributions in binary form must reproduce the above copyright
1577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#    notice, this list of conditions and the following disclaimer in the
1677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#    documentation and/or other materials provided with the distribution.
1777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# 3. The name of the author may not be used to endorse or promote products
1877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#    derived from this software without specific prior written permission.
1977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#
2077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
3177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes. "${srcdir=.}/init.sh"
3277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
3377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughescheck_exit_status_and_stderr()
3477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
3577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	$STRACE "$@" 2> "$LOG" &&
3677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		dump_log_and_fail_with \
3777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes			"strace $* failed to handle the error properly"
3877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	match_diff "$LOG" "$EXP" ||
3977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		dump_log_and_fail_with \
4077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes			"strace $* failed to print expected diagnostics"
4177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
4277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
4377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughescheck_exit_status_and_stderr_using_grep()
4477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
4577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	$STRACE "$@" 2> "$LOG" &&
4677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		dump_log_and_fail_with \
4777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes			"strace $* failed to handle the error properly"
4877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	match_grep "$LOG" "$EXP" ||
4977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		dump_log_and_fail_with \
5077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes			"strace $* failed to print expected diagnostics"
5177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
5277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
5377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughescheck_e()
5477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
5577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	local pattern="$1"; shift
5677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	cat > "$EXP" << __EOF__
57b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: $pattern
5877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes__EOF__
5977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	check_exit_status_and_stderr "$@"
6077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
6177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
6277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughescheck_e_using_grep()
6377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
6477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	local pattern="$1"; shift
6577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	cat > "$EXP" << __EOF__
66b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: $pattern
6777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes__EOF__
6877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	check_exit_status_and_stderr_using_grep "$@"
6977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
7077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
7177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughescheck_h()
7277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
7377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	local pattern="$1"; shift
7477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	cat > "$EXP" << __EOF__
75b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: $pattern
76b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott HughesTry '$STRACE_EXE -h' for more information.
7777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes__EOF__
7877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	check_exit_status_and_stderr "$@"
7977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
80