set test "Invalid Server Client Arguments"
if {! [nss_p]} { unsupported $test; return }

# Test that stap on the server side will correctly accept/reject certain
# arguments in unprivileged mode.
set test_file $srcdir/systemtap.server/test.stp

set uname [exec uname -r]

# Test invalid combinations.
set invalid_options [list \
  "--unprivileged --client-options -B X=Y" \
  "--unprivileged --client-options -D X=Y" \
  "--unprivileged --client-options -I foo" \
  "--unprivileged --client-options -m test" \
  "--unprivileged --client-options -R foo" \
  "--unprivileged --client-options -k" \
  "--unprivileged --client-options -B X=Y -D X=Y -I foo -m test -R foo -k -r $uname" \
  "--client-options --unprivileged -B X=Y" \
  "--client-options --unprivileged -D X=Y" \
  "--client-options --unprivileged -I foo" \
  "--client-options --unprivileged -m test" \
  "--client-options --unprivileged -R foo" \
  "--client-options --unprivileged -k" \
  "--client-options --unprivileged -B X=Y -D X=Y -I foo -m test -R foo -k -r $uname" \
  "--client-options -B X=Y --unprivileged" \
  "--client-options -D X=Y --unprivileged" \
  "--client-options -I foo --unprivileged" \
  "--client-options -m test --unprivileged" \
  "--client-options -R foo --unprivileged" \
  "--client-options -k --unprivileged" \
  "--client-options -B X=Y -D X=Y -I foo -m test -R foo -k -r $uname --unprivileged" \
  "--client-options -R /path" \
  "--client-options --ldd" \
  "--client-options --tmpdir=/tmp" \
  "--client-options --use-server" \
  "--client-options --use-server-on-error" \
  "--client-options --list-servers" \
  "--client-options --trust-servers" \
  "--client-options --disable-cache" \
  "--client-options --poison-cache" \
  "--client-options --clean-cache" \
  "--client-options --all-modules" \
  "--client-options --remote=foo" \
  "--client-options --remote-prefix" \
  "--client-options --modinfo=foo" \
  "-D \"foo;bar\"" \
  "-D 2=4" \
  "--client-options -r /path" \
  "-S /path" \
  "--client-options -q" \
  "--client-options -k" \
]

foreach options $invalid_options {
    set error_regexp ".*(ERROR)|(You can't specify .* when --privilege=stapusr is specified).*"
    verbose -log "eval exec stap $options"
    catch {eval exec stap $test_file -p1 $options} res_stap
    verbose -log $res_stap
    # Replace the `uname -r` substring, which varies from run to run, with a generic
    # one, for reporting/regression purposes
    set options2 [string map "$uname UNAME" $options]
    if {[regexp $error_regexp $res_stap]} {
	pass "$test: $options2"
    } else {
	fail "$test: $options2"
    }

    # We should get the same result substituting --unprivileged with --privilege=stapusr
    if {[regsub -all {\-\-unprivileged} $options "--privilege=stapusr" options]} {
	verbose -log "eval exec stap $options"
	catch {eval exec stap $test_file -p1 $options} res_stap
	verbose -log $res_stap
        # Replace the `uname -r` substring, which varies from run to run, with a generic
        # one, for reporting/regression purposes
        set options2 [string map "$uname UNAME" $options]
	if {[regexp $error_regexp $res_stap]} {
	    pass "$test: $options2"
	} else {
	    fail "$test: $options2"
	}

	# We should get the same result substituting --privilege=stapusr with --privilege=stapsys
	if {[regsub -all {\-\-privilege=stapusr} $options "--privilege=stapsys" options]} {
	    set error_regexp ".*(ERROR)|(You can't specify .* when --privilege=stapsys is specified).*"
	    verbose -log "eval exec stap $options"
	    catch {eval exec stap $test_file -p1 $options} res_stap
	    verbose -log $res_stap
            # Replace the `uname -r` substring, which varies from run to run, with a generic
            # one, for reporting/regression purposes
            set options2 [string map "$uname UNAME" $options]
	    if {[regexp $error_regexp $res_stap]} {
		pass "$test: $options2"
	    } else {
		fail "$test: $options2"
	    }
	}
    }
}

# Test valid combinations
set test "Valid Server Client Arguments"

# stap_run_exact (used below) only works for 'make installcheck'
if {[info procs installtest_p] != "" && ![installtest_p]} { untested $test; return }

set no_error_result "# parse tree dump
# file $test_file
probe begin{
exit();
}

"

set valid_options [list \
  "-a i386" \
  "-B X=Y" \
  "-D X=Y" \
  "-I foo" \
  "-m test" \
  "-r $uname" \
  "-a i386 -B X=Y -D X=Y -I foo -m test -r $uname" \
  "--unprivileged" \
  "--unprivileged -a i386" \
  "--unprivileged -B X=Y" \
  "--unprivileged -D X=Y" \
  "--unprivileged -I foo" \
  "--unprivileged -m test" \
  "--unprivileged -R foo" \
  "--unprivileged -r $uname" \
  "--unprivileged -a i386 -B X=Y -D X=Y -I foo -m test -R foo -r $uname" \
  "--client-options" \
  "--client-options -a i386" \
  "--client-options -D X=Y" \
  "--client-options -I foo" \
  "--client-options -m test" \
  "--client-options -r $uname" \
  "--client-options -a i386 -D X=Y -I foo -m test -r $uname" \
  "--unprivileged --client-options" \
  "--client-options --unprivileged" \
  "--unprivileged -a i386 --client-options" \
  "--unprivileged -B X=Y --client-options" \
  "--unprivileged -D X=Y --client-options" \
  "--unprivileged -I foo --client-options" \
  "--unprivileged -m test --client-options" \
  "--unprivileged -R foo --client-options" \
  "--unprivileged -r $uname --client-options" \
  "--unprivileged -a i386 -B X=Y -D X=Y -I foo -m test -R foo -r $uname --client-options" \
  "-a i386 --unprivileged --client-options" \
  "-B X=Y --unprivileged --client-options" \
  "-D X=Y --unprivileged --client-options" \
  "-I foo --unprivileged --client-options" \
  "-m test --unprivileged --client-options" \
  "-R foo --unprivileged --client-options" \
  "-r $uname --unprivileged --client-options" \
  "-a i386 -B X=Y -D X=Y -I foo -m test -R foo -r $uname --unprivileged --client-options" \
  "-a i386 --client-options --unprivileged" \
  "-B X=Y --client-options --unprivileged" \
  "-D X=Y --client-options --unprivileged" \
  "-I foo --client-options --unprivileged" \
  "-m test --client-options --unprivileged" \
  "-R foo --client-options --unprivileged" \
  "-r $uname --client-options --unprivileged" \
  "-a i386 -B X=Y -D X=Y -I foo -m test -R foo -r $uname --client-options --unprivileged" \
]

set ::result_string "$no_error_result"

# avoid propagating issue similar to PR17301
set origdir $env(SYSTEMTAP_DIR)
catch {exec mktemp -d} tmpdir
set env(SYSTEMTAP_DIR) $tmpdir

foreach options $valid_options {
    # Replace the `uname -r` substring, which varies from run to run, with a generic
    # one, for reporting/regression purposes
    set options2 [string map "$uname UNAME" $options]
    eval stap_run_exact {"$test: $options2"} $test_file -p1 $options

    # We should get the same result substituting --unprivileged with --privilege=stapusr
    if {[regsub -all {\-\-unprivileged} $options "--privilege=stapusr" options]} {
        # Replace the `uname -r` substring, which varies from run to run, with a generic
        # one, for reporting/regression purposes
        set options2 [string map "$uname UNAME" $options]
	eval stap_run_exact {"$test: $options2"} $test_file -p1 $options

	# We should get the same result substituting --privilege=stapusr with --privilege=stapsys
	if {[regsub -all {\-\-privilege=stapusr} $options "--privilege=stapsys" options]} {
            # Replace the `uname -r` substring, which varies from run to run, with a generic
            # one, for reporting/regression purposes
            set options2 [string map "$uname UNAME" $options]
	    eval stap_run_exact {"$test: $options2"} $test_file -p1 $options
	}
    }
}

set env(SYSTEMTAP_DIR) $origdir
catch {exec rm -rf $tmpdir}
