#!/usr/bin/perl -T

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

use Test::More;
plan tests => 21;

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

tstpre ("
loadplugin Mail::SpamAssassin::Plugin::AuthRes
");

# common network config for all blocks
my $networks = "
  clear_internal_networks
  clear_trusted_networks
  internal_networks 212.17.35.15
  trusted_networks 212.17.35.15
";

# == 1. Basic parsing + aggregation ==

tstlocalrules ("
  header AR_SPF_PASS   eval:check_authres_result('spf', 'pass')
  score  AR_SPF_PASS   1.0
  header AR_DKIM_PASS  eval:check_authres_result('dkim', 'pass')
  score  AR_DKIM_PASS  1.0
  header AR_DMARC_FAIL eval:check_authres_result('dmarc', 'fail')
  score  AR_DMARC_FAIL 1.0
  header AR_DKIM_FAIL  eval:check_authres_result('dkim', 'fail')
  score  AR_DKIM_FAIL  1.0
");

tstprefs($networks);

%patterns = (
  q{ 1.0 AR_SPF_PASS },   'basic_spf_pass',
  q{ 1.0 AR_DKIM_PASS },  'basic_dkim_pass',
  q{ 1.0 AR_DMARC_FAIL }, 'basic_dmarc_fail',
  'authres: results: dkim=pass dmarc=fail spf=pass', 'basic_results',
);
%anti_patterns = (
  q{ 1.0 AR_DKIM_FAIL },  'basic_dkim_fail_suppressed',
);

ok sarun ("-D authres -L -t < data/nice/authres_basic 2>&1", \&patterns_run_cb);
ok_all_patterns();


# == 2. "none" method ==

tstlocalrules ("
  header AR_SPF_MISSING eval:check_authres_result('spf', 'missing')
  score  AR_SPF_MISSING 1.0
");

tstprefs($networks);

%patterns = (
  'authres: skipping header, method none', 'none_skip',
  q{ 1.0 AR_SPF_MISSING }, 'none_spf_missing',
);
%anti_patterns = (
  'authres: results:', 'none_no_results',
);

ok sarun ("-D authres -L -t < data/nice/authres_none 2>&1", \&patterns_run_cb);
ok_all_patterns();


# == 3. Error handling ==

tstlocalrules ("
  header AR_SPF_PASS   eval:check_authres_result('spf', 'pass')
  score  AR_SPF_PASS   1.0
");

tstprefs($networks);

%patterns = (
  'authres: skipping header, missing authserv', 'errors_missing_authserv',
  'authres: results: spf=pass', 'errors_valid_results',
);
%anti_patterns = ();

ok sarun ("-D authres -L -t < data/nice/authres_errors 2>&1", \&patterns_run_cb);
ok_all_patterns();


# == 4. check_authres_result 'missing' ==

tstlocalrules ("
  header AR_ARC_MISSING   eval:check_authres_result('arc', 'missing')
  score  AR_ARC_MISSING   1.0
  header AR_DMARC_MISSING eval:check_authres_result('dmarc', 'missing')
  score  AR_DMARC_MISSING 1.0
");

tstprefs($networks);

%patterns = (
  q{ 1.0 AR_ARC_MISSING }, 'missing_arc',
);
%anti_patterns = (
  q{ 1.0 AR_DMARC_MISSING }, 'missing_dmarc_not_hit',
);

ok sarun ("-D authres -L -t < data/nice/authres_basic 2>&1", \&patterns_run_cb);
ok_all_patterns();


# == 5. Real-world header generated by Mail::Milter::Authentication with extended
# dmarc properties (policy.published-domain-policy etc) and non-standard methods (x-tls)
# should not cause the entire header to fail.

tstlocalrules ("
  header AR_SPF_PASS    eval:check_authres_result('spf', 'pass')
  score  AR_SPF_PASS    1.0
  header AR_DKIM_PASS   eval:check_authres_result('dkim', 'pass')
  score  AR_DKIM_PASS   1.0
  header AR_DMARC_PASS  eval:check_authres_result('dmarc', 'pass')
  score  AR_DMARC_PASS  1.0
");

tstprefs($networks);

%patterns = (
  q{ 1.0 AR_SPF_PASS },   'realworld_spf_pass',
  q{ 1.0 AR_DKIM_PASS },  'realworld_dkim_pass',
  q{ 1.0 AR_DMARC_PASS }, 'realworld_dmarc_pass',
);
%anti_patterns = (
  'authres: skipping header', 'realworld_no_skip',
);

ok sarun ("-D authres -L -t < data/nice/authres_realworld 2>&1", \&patterns_run_cb);
ok_all_patterns();
