]> source.dussan.org Git - rspamd.git/commitdiff
* Write tests for DKIM signature and key parsers.
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Mon, 28 May 2012 23:07:23 +0000 (03:07 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Mon, 28 May 2012 23:07:23 +0000 (03:07 +0400)
Fix DKIM parsers.

src/dkim.c
test/CMakeLists.txt
test/rspamd_dkim_test.c [new file with mode: 0644]
test/rspamd_test_suite.c
test/tests.h

index e1115a318dc3ce16852c3afd3035bb2a19ed5408..492527e2b5dd79a99f0ab7dde797a54b9980cdc6 100644 (file)
@@ -135,17 +135,17 @@ rspamd_dkim_parse_canonalg (rspamd_dkim_context_t* ctx, const gchar *param, gsiz
        }
        else {
                /* First check header */
-               if (sl == 6 && memcmp (param, "simple", len) == 0) {
+               if (sl == 6 && memcmp (param, "simple", sl) == 0) {
                        ctx->header_canon_type = DKIM_CANON_SIMPLE;
                }
-               else if (sl == 7 && memcmp (param, "relaxed", len) == 0) {
+               else if (sl == 7 && memcmp (param, "relaxed", sl) == 0) {
                        ctx->header_canon_type = DKIM_CANON_RELAXED;
                }
                else {
                        goto err;
                }
                /* Check body */
-               len = len - sl - 1;
+               len -= sl + 1;
                slash ++;
                if (len == 6 && memcmp (slash, "simple", len) == 0) {
                        ctx->body_canon_type = DKIM_CANON_SIMPLE;
@@ -158,7 +158,7 @@ rspamd_dkim_parse_canonalg (rspamd_dkim_context_t* ctx, const gchar *param, gsiz
        }
 
 err:
-       g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_A, "invalid dkim sign algorithm");
+       g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_A, "invalid dkim canonization algorithm");
        return FALSE;
 }
 
@@ -189,14 +189,8 @@ rspamd_dkim_parse_hdrlist (rspamd_dkim_context_t* ctx, const gchar *param, gsize
        while (p <= end) {
                if ((*p == ':' || p == end) && p - c > 0) {
                        /* Insert new header to the list */
-                       if (p == end) {
-                               h = memory_pool_alloc (ctx->pool, p - c + 1);
-                               rspamd_strlcpy (h, c, p - c + 1);
-                       }
-                       else {
-                               h = memory_pool_alloc (ctx->pool, p - c);
-                               rspamd_strlcpy (h, c, p - c);
-                       }
+                       h = memory_pool_alloc (ctx->pool, p - c + 1);
+                       rspamd_strlcpy (h, c, p - c + 1);
                        /* Check mandatory from */
                        if (!from_found && g_ascii_strcasecmp (h, "from") == 0) {
                                from_found = TRUE;
@@ -357,8 +351,8 @@ rspamd_create_dkim_context (const gchar *sig, memory_pool_t *pool, GError **err)
                                tag = c;
                        }
                        else {
-                               p ++;
                                taglen ++;
+                               p ++;
                        }
                        break;
                case DKIM_STATE_AFTER_TAG:
@@ -432,21 +426,30 @@ rspamd_create_dkim_context (const gchar *sig, memory_pool_t *pool, GError **err)
                        }
                        if (state != DKIM_STATE_ERROR) {
                                /* Skip spaces */
-                               p ++;
                                state = DKIM_STATE_SKIP_SPACES;
                                next_state = DKIM_STATE_VALUE;
                        }
                        break;
                case DKIM_STATE_VALUE:
                        if (*p == ';') {
-                               if (param == DKIM_PARAM_UNKNOWN || !parser_funcs[param](new, c, p - c - 1, err)) {
+                               if (param == DKIM_PARAM_UNKNOWN || !parser_funcs[param](new, c, p - c, err)) {
                                        state = DKIM_STATE_ERROR;
                                }
+                               else {
+                                       state = DKIM_STATE_SKIP_SPACES;
+                                       next_state = DKIM_STATE_TAG;
+                                       p ++;
+                                       taglen = 0;
+                               }
                        }
                        else if (p == end) {
-                               if (param == DKIM_PARAM_UNKNOWN || !parser_funcs[param](new, c, p - c, err)) {
+                               if (param == DKIM_PARAM_UNKNOWN || !parser_funcs[param](new, c, p - c + 1, err)) {
                                        state = DKIM_STATE_ERROR;
                                }
+                               else {
+                                       /* Finish processing */
+                                       p ++;
+                               }
                        }
                        else {
                                p ++;
@@ -647,6 +650,9 @@ rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err)
                                len = (p == end) ? p - c : p - c - 1;
                                return rspamd_dkim_make_key (c, len, err);
                        }
+                       else {
+                               p ++;
+                       }
                        break;
                }
        }
index 5e20e25e4bcb06a92b29ebc0f2a1102bf8c94c14..322229cadb1c70a01d2f9296e075ce4534835f8f 100644 (file)
@@ -6,7 +6,8 @@ SET(TESTSRC             rspamd_expression_test.c
                                rspamd_test_suite.c
                                rspamd_url_test.c
                                rspamd_dns_test.c
-                               rspamd_async_test.c)
+                               rspamd_async_test.c
+                               rspamd_dkim_test.c)
 
 ADD_EXECUTABLE(rspamd-test EXCLUDE_FROM_ALL ${TESTSRC})
 SET_TARGET_PROPERTIES(rspamd-test PROPERTIES LINKER_LANGUAGE C)
@@ -34,3 +35,6 @@ IF(ENABLE_LUAJIT MATCHES "ON")
 ELSE(ENABLE_LUAJIT MATCHES "ON")
     TARGET_LINK_LIBRARIES(rspamd-test "${LUA_LIBRARY}")
 ENDIF(ENABLE_LUAJIT MATCHES "ON")
+IF(OPENSSL_FOUND)
+       TARGET_LINK_LIBRARIES(rspamd-test ${OPENSSL_LIBRARIES})
+ENDIF(OPENSSL_FOUND)
diff --git a/test/rspamd_dkim_test.c b/test/rspamd_dkim_test.c
new file mode 100644 (file)
index 0000000..ac9a88d
--- /dev/null
@@ -0,0 +1,94 @@
+/* Copyright (c) 2011, Vsevolod Stakhov
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *       * Redistributions of source code must retain the above copyright
+ *         notice, this list of conditions and the following disclaimer.
+ *       * Redistributions in binary form must reproduce the above copyright
+ *         notice, this list of conditions and the following disclaimer in the
+ *         documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "tests.h"
+#include "main.h"
+#include "dkim.h"
+
+static const gchar test_dkim_sig[] = "v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; "
+               "bh=xTpleJNUaXMzaRU2xnfInn4n9hf/UHzWSuYzJ3s2WBc=; "
+        "b=cPya7FKbcJnCqMlCETci4ZlQTI/Tfw8y1/+AUSU+YBaDgLhsZMDPQMO0zzMvQM+c+E"
+        "i/J+BAckB9JRyPr9xXtV0ORSmUkFgeMyURopwdNzKQ9UB/JnGHj6i11ceV3b20UAYiIu"
+        "qrXhJD+YEgHYXVtfzTC4OLTJGjEQguEn2+RtlZV60aWsPizK+mqlVO4G57RGklp0vnqB"
+        "oc2XUGMVmOaCvVpQkJdJud1r5aKLhr9Bs0sM8/MaYugwmdSk2rLJUMfUPRyUmIGv3BAG"
+        "bwdvXghyl7HNOqPYwXvk/B8C7++k0VUUOix5M/XrcBMNyJu2fMZJMD8KSn3udFjp9vZ6"
+        "pRqg==";
+
+extern struct event_base *base;
+
+static void
+test_key_handler (rspamd_dkim_key_t *key, gsize keylen, rspamd_dkim_context_t *ctx, gpointer ud, GError *err)
+{
+       struct rspamd_async_session *s = ud;
+       g_assert (key != NULL);
+
+       destroy_session (s);
+}
+
+static gboolean
+session_fin (gpointer unused)
+{
+       struct timeval tv;
+
+       tv.tv_sec = 0;
+       tv.tv_usec = 0;
+       event_loopexit (&tv);
+
+       return TRUE;
+}
+
+void
+rspamd_dkim_test_func ()
+{
+       rspamd_dkim_context_t *ctx;
+       rspamd_dkim_key_t *key;
+       memory_pool_t *pool;
+       struct rspamd_dns_resolver *resolver;
+       struct config_file *cfg;
+       GError *err = NULL;
+       struct rspamd_async_session *s;
+
+       cfg = (struct config_file *)g_malloc (sizeof (struct config_file));
+       bzero (cfg, sizeof (struct config_file));
+       cfg->cfg_pool = memory_pool_new (memory_pool_get_size ());
+       cfg->dns_retransmits = 10;
+       cfg->dns_timeout = 1000;
+
+       pool = memory_pool_new (memory_pool_get_size ());
+
+       resolver = dns_resolver_init (base, cfg);
+
+       g_assert (resolver != NULL);
+
+       ctx = rspamd_create_dkim_context (test_dkim_sig, pool, &err);
+
+       g_assert (ctx != NULL);
+
+       /* Key part */
+       s = new_async_session (pool, session_fin, NULL, NULL, NULL);
+
+       g_assert (rspamd_get_dkim_key (ctx, resolver, s, test_key_handler, s));
+
+       event_base_loop (base, 0);
+}
index 5f9a770971536de7d423a12268a82efb3ee8c582..283cae57d3daa5a638ea1012c32c85d63ea53997 100644 (file)
@@ -69,6 +69,7 @@ main (int argc, char **argv)
        g_test_add_func ("/rspamd/statfile", rspamd_statfile_test_func);
        g_test_add_func ("/rspamd/dns", rspamd_dns_test_func);
        g_test_add_func ("/rspamd/aio", rspamd_async_test_func);
+       g_test_add_func ("/rspamd/dkim", rspamd_dkim_test_func);
 
        g_test_run ();
 
index be2c29fcadc2a0fb2772625ef50e4b96a1e24dc5..135bfd1630659ba49855a96a11b946bd6ad5b259 100644 (file)
@@ -29,4 +29,7 @@ void rspamd_dns_test_func ();
 /* Async IO */
 void rspamd_async_test_func ();
 
+/* DKIM test */
+void rspamd_dkim_test_func ();
+
 #endif