You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by jm...@apache.org on 2007/06/25 18:21:16 UTC

svn commit: r550537 - in /spamassassin/trunk: MANIFEST spamc/libspamc.c spamc/spamc.c t/SATest.pm t/spamc_x_E_R.t t/spamc_x_e.t

Author: jm
Date: Mon Jun 25 09:21:15 2007
New Revision: 550537

URL: http://svn.apache.org/viewvc?view=rev&rev=550537
Log:
bug 5412: 'spamc -x -R' always returned 0, instead of the exit code, on error.  Bug 5478: in addition, 'spamc -x -e /command' would still run the command, even if errors meant that the filtered text would be unavailable, which contradicted -x.  fix both bugs

Added:
    spamassassin/trunk/t/spamc_x_E_R.t   (with props)
Modified:
    spamassassin/trunk/MANIFEST
    spamassassin/trunk/spamc/libspamc.c
    spamassassin/trunk/spamc/spamc.c
    spamassassin/trunk/t/SATest.pm
    spamassassin/trunk/t/spamc_x_e.t

Modified: spamassassin/trunk/MANIFEST
URL: http://svn.apache.org/viewvc/spamassassin/trunk/MANIFEST?view=diff&rev=550537&r1=550536&r2=550537
==============================================================================
--- spamassassin/trunk/MANIFEST (original)
+++ spamassassin/trunk/MANIFEST Mon Jun 25 09:21:15 2007
@@ -501,3 +501,6 @@
 t/root_spamd_x_paranoid.t
 t/root_spamd_virtual.t
 t/spamc_H.t
+t/root_spamd_x_u.t
+t/spamc_x_E_R.t
+t/spamc_x_e.t

Modified: spamassassin/trunk/spamc/libspamc.c
URL: http://svn.apache.org/viewvc/spamassassin/trunk/spamc/libspamc.c?view=diff&rev=550537&r1=550536&r2=550537
==============================================================================
--- spamassassin/trunk/spamc/libspamc.c (original)
+++ spamassassin/trunk/spamc/libspamc.c Mon Jun 25 09:21:15 2007
@@ -504,7 +504,18 @@
                       family, host, port, numloops + 1, connect_retries);
 #endif
 
-            status = timeout_connect(mysock, res->ai_addr, res->ai_addrlen);
+            /* this is special-cased so that we have an address we can
+             * safely use as an "always fail" test case */
+            if (!strcmp(host, "255.255.255.255")) {
+              libspamc_log(tp->flags, LOG_ERR,
+			 "connect to spamd on %s failed, %s is broadcast addr",
+			 host);
+              status = -1;
+            }
+            else {
+              status = timeout_connect(mysock, res->ai_addr, res->ai_addrlen);
+            }
+
 #else
 	    struct sockaddr_in addrbuf;
 	    const char *ipaddr;
@@ -524,11 +535,21 @@
 			 "dbg: connect(AF_INET) to spamd at %s (try #%d of %d)",
 			 ipaddr, numloops + 1, connect_retries);
 #endif
-	    
-	    status =
-	      timeout_connect(mysock, (struct sockaddr *) &addrbuf, sizeof(addrbuf));
-#endif
 
+            /* this is special-cased so that we have an address we can
+             * safely use as an "always fail" test case */
+            if (!strcmp(ipaddr, "255.255.255.255")) {
+              libspamc_log(tp->flags, LOG_ERR,
+			 "connect to spamd on %s failed, %s is broadcast addr",
+			 ipaddr);
+              status = -1;
+            }
+            else {
+              status = timeout_connect(mysock, (struct sockaddr *) &addrbuf,
+                        sizeof(addrbuf));
+            }
+
+#endif
 
             if (status != 0) {
                   origerr = spamc_get_errno();

Modified: spamassassin/trunk/spamc/spamc.c
URL: http://svn.apache.org/viewvc/spamassassin/trunk/spamc/spamc.c?view=diff&rev=550537&r1=550536&r2=550537
==============================================================================
--- spamassassin/trunk/spamc/spamc.c (original)
+++ spamassassin/trunk/spamc/spamc.c Mon Jun 25 09:21:15 2007
@@ -927,44 +927,45 @@
     free(username);
 
 /* FAIL: */
-    get_output_fd(&out_fd);
-
     result = m.is_spam;
     if ((flags & SPAMC_CHECK_ONLY) && result != EX_TOOBIG) {
+        get_output_fd(&out_fd);
 	/* probably, the write to stdout failed; we can still report exit code */
 	message_cleanup(&m);
 	ret = result;
     }
-    else if (flags & (SPAMC_CHECK_ONLY | SPAMC_REPORT | SPAMC_REPORT_IFSPAM)) {
-	full_write(out_fd, 1, "0/0\n", 4);
-	message_cleanup(&m);
-	ret = EX_NOTSPAM;
-    }
     else if (flags & (SPAMC_LEARN|SPAMC_PING) ) {
+        get_output_fd(&out_fd);
         message_cleanup(&m);
     }
-    else if (flags & SPAMC_SYMBOLS) {
-	/* bug 4991: -y should only output a blank line on connection failure */
-	full_write(out_fd, 1, "\n", 1);
-        message_cleanup(&m);
-        if (use_exit_code) {
-            ret = result;
-        }
-	else if (flags & SPAMC_SAFE_FALLBACK) {
-	    ret = EX_OK;
-	}
-    }
     else {
-	message_dump(STDIN_FILENO, out_fd, &m);
+        if (flags & (SPAMC_CHECK_ONLY | SPAMC_REPORT | SPAMC_REPORT_IFSPAM)) {
+            get_output_fd(&out_fd);
+            full_write(out_fd, 1, "0/0\n", 4);
+        }
+        else if (flags & SPAMC_SYMBOLS) {
+            /* bug 4991: -y should only output a blank line on connection failure */
+            get_output_fd(&out_fd);
+            full_write(out_fd, 1, "\n", 1);
+        }
+        else {
+            /* bug 5412: spamc -x should not output the message on error */
+            if ((flags & SPAMC_SAFE_FALLBACK) && ret != EX_TOOBIG) {
+                get_output_fd(&out_fd);
+                message_dump(STDIN_FILENO, out_fd, &m);
+            }
+            /* else, do NOT get_output_fd() (bug 5478) */
+        }
+
 	message_cleanup(&m);
 	if (ret == EX_TOOBIG) {
 	    ret = 0;
 	}
-        else if (use_exit_code) {
-            ret = result;
-        }
 	else if (flags & SPAMC_SAFE_FALLBACK) {
 	    ret = EX_OK;
+        }
+        else if (use_exit_code) {
+            ret = result;
 	}
     }
     

Modified: spamassassin/trunk/t/SATest.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/SATest.pm?view=diff&rev=550537&r1=550536&r2=550537
==============================================================================
--- spamassassin/trunk/t/SATest.pm (original)
+++ spamassassin/trunk/t/SATest.pm Mon Jun 25 09:21:15 2007
@@ -309,11 +309,15 @@
 sub scrunwithstderr {
   spamcrun (@_, 1);
 }
+sub scrunwantfail {
+  spamcrun (@_, 1, 1);
+}
 
 sub spamcrun {
   my $args = shift;
   my $read_sub = shift;
   my $capture_stderr = shift;
+  my $expect_failure = shift;
 
   if (defined $ENV{'SC_ARGS'}) {
     $args = $ENV{'SC_ARGS'} . " ". $args;
@@ -343,13 +347,19 @@
   }
 
   $sa_exitcode = ($?>>8);
-  if ($sa_exitcode != 0) { stop_spamd(); return undef; }
+  if (!$expect_failure) {
+    if ($sa_exitcode != 0) { stop_spamd(); return undef; }
+  }
 
   %found = ();
   %found_anti = ();
   &checkfile ("d.$testname/out.${Test::ntest}", $read_sub) if (defined $read_sub);
 
-  ($sa_exitcode == 0);
+  if ($expect_failure) {
+    ($sa_exitcode != 0);
+  } else {
+    ($sa_exitcode == 0);
+  }
 }
 
 sub spamcrun_background {

Added: spamassassin/trunk/t/spamc_x_E_R.t
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/spamc_x_E_R.t?view=auto&rev=550537
==============================================================================
--- spamassassin/trunk/t/spamc_x_E_R.t (added)
+++ spamassassin/trunk/t/spamc_x_E_R.t Mon Jun 25 09:21:15 2007
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+
+use lib '.'; use lib 't';
+use SATest; sa_t_init("spamc_x_E_R");
+
+our $DO_RUN = !$SKIP_SPAMD_TESTS;
+
+use Test; plan tests => ($DO_RUN ? 31 : 0);
+
+exit unless $DO_RUN;
+
+# ---------------------------------------------------------------------------
+# test case for bug 5412; exit status with -x/-E/-R combos
+
+%patterns = ( );
+
+ok(start_spamd("-L"));
+
+# ----------------------------------------------------------------------
+# nonspam mails -- return 0
+ok(scrun("-E < data/nice/001", \&patterns_run_cb));
+ok(scrun("-R < data/nice/001", \&patterns_run_cb));
+ok(scrun("-x -E < data/nice/001", \&patterns_run_cb));
+ok(scrun("-x -R < data/nice/001", \&patterns_run_cb));
+ok(scrun("-x -R -E < data/nice/001", \&patterns_run_cb));
+
+# ----------------------------------------------------------------------
+# spam mails
+ok(scrun("-R < data/spam/001", \&patterns_run_cb));
+ok(scrun("-x -R < data/spam/001", \&patterns_run_cb));
+
+# returns 1; this will kill spamd as a side-effect
+ok(scrunwantfail("-x -E < data/spam/001", \&patterns_run_cb));
+stop_spamd(); $spamd_pid = undef; $spamd_already_killed = undef;
+ok(start_spamd("-L"));
+
+# returns 1; this will kill spamd
+ok(scrunwantfail("-E < data/spam/001", \&patterns_run_cb));
+stop_spamd(); $spamd_pid = undef; $spamd_already_killed = undef;
+ok(start_spamd("-L"));
+
+# returns 1; this will kill spamd
+ok(scrunwantfail("-x -R -E < data/spam/001", \&patterns_run_cb));
+stop_spamd(); # just to be sure
+
+# ----------------------------------------------------------------------
+# error conditions
+$spamdhost = '255.255.255.255'; # cause "connection failed" errors
+
+# these should have exit code == 0
+ok(scrun("--connect-retries 1 -E < data/spam/001", \&patterns_run_cb));
+ok(scrun("--connect-retries 1 -R < data/spam/001", \&patterns_run_cb));
+
+# we do not want to see the output with -x on error
+%patterns = ();
+%anti_patterns = (
+  q{ Subject: There yours for FREE!}, 'subj',
+  q{ X-Spam-Flag: YES}, 'flag',
+);
+
+# this should have exit code != 0
+clear_pattern_counters();
+ok(scrunwantfail("--connect-retries 1 -x < data/spam/001", \&patterns_run_cb));
+ok ok_all_patterns();
+
+# this should have exit code != 0
+clear_pattern_counters();
+ok(scrunwantfail("--connect-retries 1 -x -R < data/spam/001", \&patterns_run_cb));
+ok ok_all_patterns();
+
+# this should have exit code != 0
+clear_pattern_counters();
+ok(scrunwantfail("--connect-retries 1 -x -E -R < data/spam/001", \&patterns_run_cb));
+ok ok_all_patterns();
+
+# this should have exit code != 0
+clear_pattern_counters();
+ok(scrunwantfail("--connect-retries 1 -x -E < data/spam/001", \&patterns_run_cb));
+ok ok_all_patterns();
+
+# ----------------------------------------------------------------------
+

Propchange: spamassassin/trunk/t/spamc_x_E_R.t
------------------------------------------------------------------------------
    svn:executable = *

Modified: spamassassin/trunk/t/spamc_x_e.t
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/spamc_x_e.t?view=diff&rev=550537&r1=550536&r2=550537
==============================================================================
--- spamassassin/trunk/t/spamc_x_e.t (original)
+++ spamassassin/trunk/t/spamc_x_e.t Mon Jun 25 09:21:15 2007
@@ -3,9 +3,7 @@
 use lib '.'; use lib 't';
 use SATest; sa_t_init("spamc_x_e");
 
-# only run for localhost!
-our $DO_RUN = conf_bool('run_net_tests')
-                    && !$SKIP_SPAMD_TESTS;
+our $DO_RUN = !$SKIP_SPAMD_TESTS;
 
 use Test; plan tests => ($DO_RUN ? 7 : 0);
 
@@ -23,7 +21,7 @@
 
 %patterns = ( );
 %anti_patterns = ( 'Fine' => 'Fine' );
-$spamdhost = '0.0.0.1'; # cause "connection failed" errors
+$spamdhost = '255.255.255.255'; # cause "connection failed" errors
 
 ok !spamcrun("-x -e /bin/echo Fine < data/nice/001", \&patterns_run_cb);
 ok ok_all_patterns();