#!/usr/bin/perl -T
#
# test for http://bz.apache.org/SpamAssassin/show_bug.cgi?id=5574#c12 .

use lib '.'; use lib 't';
use SATest; sa_t_init("root_spamd_u_dcc");

use constant HAS_SUDO => $RUNNING_ON_WINDOWS || eval { $_ = untaint_cmd("which sudo 2>/dev/null"); chomp; -x };

use Test::More;
plan skip_all => "root tests disabled" unless conf_bool('run_root_tests');
plan skip_all => "DCC tests disabled" unless conf_bool('run_dcc_tests');
plan skip_all => "not running tests as root" unless eval { ($> == 0); };
plan skip_all => "sudo executable not found in path" unless HAS_SUDO;
plan tests => 23;

# ---------------------------------------------------------------------------

%patterns = (
  q{ spam reported to DCC }, 'dcc report',
);

tstprefs ("
  loadplugin Mail::SpamAssassin::Plugin::DCC
  dcc_timeout 30
");

ok sarun ("-t -D info -r < data/spam/gtubedcc.eml 2>&1", \&patterns_run_cb);
# ok_all_patterns();

# ---------------------------------------------------------------------------

%patterns = (

q{ X-Spam-Status: Yes, score=}, 'status',
q{ X-Spam-Flag: YES}, 'flag',
q{ X-Spam-Level: **********}, 'stars',

);

# run spamc as unpriv uid
$spamc = "sudo -u nobody $spamc";
# ensure it is readable by all
diag "Test will fail if run in directory not accessible by 'nobody' as is typical for a home directory";
chmod 01755, $workdir;

$SIG{ALRM} = sub { stop_spamd(); die "timed out"; };
alarm 60;
ok(start_spamd("-c -H -m1"));
alarm 0;

# run a few times to ensure that the child can process more than
# one message successfully. do not bother looking for the dcc
# result; we just want to ensure that the check did not cause
# the spamd kids to get hung
for my $try (1 .. 5) {
  $SIG{ALRM} = sub { stop_spamd(); die "timed out"; };
  alarm 10;
  ok(spamcrun("< data/spam/gtubedcc.eml", \&patterns_run_cb));
  alarm 0;
  ok_all_patterns();
}

ok(stop_spamd());

