#!/bin/bash
#
# Copyright (C) 2012 Christian Babeux <christian.babeux@efficios.com>
#
# SPDX-License-Identifier: GPL-2.0-only
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License, version 2 only, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

TEST_DESC="Filtering - Invalid filters"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../..
SESSION_NAME="filter-invalid"
EVENT_NAME="bogus"
ENABLE_EVENT_STDERR="/tmp/invalid-filters-stderr"
TRACE_PATH=$(mktemp --tmpdir -d tmp.test_filtering_invalid_filters_trace_path.XXXXXX)
NUM_GLOBAL_TESTS=2
NUM_UST_TESTS=138
NUM_KERNEL_TESTS=138
NUM_TESTS=$(($NUM_UST_TESTS+$NUM_KERNEL_TESTS+$NUM_GLOBAL_TESTS))

source $TESTDIR/utils/utils.sh

function enable_lttng_event_filter
{
	domain="$1"
	sess_name="$2"
	event_name="$3"
	filter="$4"

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $event_name -s $sess_name \
		$domain --filter "$filter" 2> $ENABLE_EVENT_STDERR 1> /dev/null

	# Enable must fail
	if [ $? -eq 0 ]; then
		fail "Enable lttng event with filtering and invalid filter"
		return 1
	else
		pass "Enable lttng event with filtering and invalid filter"
		return 0
	fi
}

function test_invalid_filter
{
	domain="$1"
	test_invalid_filter="$2"

	diag "Test filter expression with invalid filter"
	diag "Filter: $test_invalid_filter"

	# Create session
	create_lttng_session_ok $SESSION_NAME $TRACE_PATH

	# Apply filter
	enable_lttng_event_filter $domain $SESSION_NAME $EVENT_NAME "$test_invalid_filter"

	# Destroy session
	destroy_lttng_session_ok $SESSION_NAME
}

function test_bytecode_limit
{
	domain="$1"

	# Current bytecode limitation is 65536 bytes long.
	# Generate a huge bytecode with some perl-fu
	BYTECODE_LIMIT=`perl -e 'print "intfield" . " && 1" x5460'`

	diag "Test filter bytecode limits (64KiB)"

	# Create session
	create_lttng_session_ok $SESSION_NAME $TRACE_PATH

	# Apply filter
	enable_lttng_event_filter $domain $SESSION_NAME $EVENT_NAME "$BYTECODE_LIMIT"

	# Destroy session
	destroy_lttng_session_ok $SESSION_NAME
}

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

OLDIFS="$IFS"
IFS=$'\n'
INVALID_FILTERS=(
		# Unsupported ops
		"intfield*1"
		"intfield/1"
		"intfield+1"
		"intfield-1"
		"1+11111-3333+1"
		"(1+2)*(55*666)"
		"1+2*55*666"
		"asdf + 1 > 1"
		"asdfas < 2332 || asdf + 1 > 1"
		"!+-+++-------+++++++++++-----!!--!44+1"
		"aaa||(gg)+(333----1)"
		"1+1"
		# Unmatched parenthesis
		"((((((((((((((intfield)))))))))))))"
		'0 || ("abc" != "def")) && (3 < 4)'
		"a->"
		"a-->a"
		"a.b.c->d.e.f+1"
		# String can\'t be root node
		"\"somestring\""
		# Unary op on string not allowed
		"!\"somestring\""
		# Comparison with string type not allowed
		"\"somestring\" > 42"
		"\"somestring\" > 42.0"
		"42 > \"somestring\""
		"42.0 > \"somestring\""
		# Logical operator with string type not allowed
		"\"somestring\" || 1"
		"1 || \"somestring\""
		"\$ctx == 0"
		"0 == \$ctx"
		# Only \$ctx is supported for now
		"\$global.value == 0"
		"0 == \$global.value"
		# Cannot compare two full star globbing patterns
		'"hello*world" == "yes*man"'
		'"hello*world" == "yesman*"'
		'"helloworld*" == "yes*man"'
		# May only use != and == operators when one of them is a full
		# star globbing pattern
		'"hello*world" < field'
		'"hello*world" <= field'
		'"hello*world" >= field'
		'"hello*world" > field'
		'"hello*world" || field'
		'"hello*world" && field'
		'field < "hello*world"'
		'field <= "hello*world"'
		'field >= "hello*world"'
		'field > "hello*world"'
		'field && "hello*world"'
		'field || "hello*world"'
		# Array expression must contain constant index
		'field[abc] == 1'
)
IFS="$OLDIFS"

start_lttng_sessiond
diag "Test UST filters"

i=0
while [ "$i" -lt "${#INVALID_FILTERS[@]}" ]; do
	echo "${INVALID_FILTERS[$i]}"
	test_invalid_filter -u "${INVALID_FILTERS[$i]}"
	let "i++"
done

test_bytecode_limit -u

check_skip_kernel_test "$NUM_KERNEL_TESTS" "Skipping kernel invalid filter tests." ||
{
	diag "Test kernel filters"
	i=0
	while [ "$i" -lt "${#INVALID_FILTERS[@]}" ]; do
		echo "${INVALID_FILTERS[$i]}"
		test_invalid_filter -k "${INVALID_FILTERS[$i]}"
		let "i++"
	done

	test_bytecode_limit -k
}
stop_lttng_sessiond

rm -f $ENABLE_EVENT_STDERR
rm -rf $TRACE_PATH
