From 3d1c40c972d68623f88875ec03ae7c8bafbadad5 Mon Sep 17 00:00:00 2001
From: Vsevolod Stakhov <vsevolod@rambler-co.ru>
Date: Thu, 8 Jul 2010 20:07:07 +0400
Subject: * Make DNS resolver working * Many improvements to rspamd test suite:
 now it CAN be used for testing rspamd functionality * Write DNS resolver
 tests * Fix issues with memory_pool mutexes and with creating of statfiles

---
 test/rspamd_expression_test.c | 12 +++++------
 test/rspamd_fuzzy_test.c      |  7 ++++---
 test/rspamd_mem_pool_test.c   | 10 +---------
 test/rspamd_memcached_test.c  | 14 -------------
 test/rspamd_statfile_test.c   | 17 +---------------
 test/rspamd_test_suite.c      | 46 +++++++++++++++++++++++++++++++++----------
 test/tests.h                  |  3 +++
 7 files changed, 51 insertions(+), 58 deletions(-)

(limited to 'test')

diff --git a/test/rspamd_expression_test.c b/test/rspamd_expression_test.c
index 5ccaecdbd..7cf4bb123 100644
--- a/test/rspamd_expression_test.c
+++ b/test/rspamd_expression_test.c
@@ -33,26 +33,26 @@ rspamd_expression_test_func ()
 		outstr = memory_pool_alloc (pool, s);
 		while (cur) {
 			if (cur->type == EXPR_REGEXP) {
-				r += snprintf (outstr + r, s - r, "OP:%s ", (char *)cur->content.operand);
+				r += rspamd_snprintf (outstr + r, s - r, "OP:%s ", (char *)cur->content.operand);
 			} else if (cur->type == EXPR_STR) {
-				r += snprintf (outstr + r, s - r, "S:%s ", (char *)cur->content.operand);
+				r += rspamd_snprintf (outstr + r, s - r, "S:%s ", (char *)cur->content.operand);
 
 			} else if (cur->type == EXPR_FUNCTION) {
-				r += snprintf (outstr + r, s - r, "F:%s ", ((struct expression_function *)cur->content.operand)->name);
+				r += rspamd_snprintf (outstr + r, s - r, "F:%s ", ((struct expression_function *)cur->content.operand)->name);
 				cur_arg = ((struct expression_function *)cur->content.operand)->args;
 				while (cur_arg) {
 					arg = cur_arg->data;
 					if (arg->type == EXPRESSION_ARGUMENT_NORMAL) {
-						r += snprintf (outstr + r, s - r, "A:%s ", (char *)arg->data);
+						r += rspamd_snprintf (outstr + r, s - r, "A:%s ", (char *)arg->data);
 					}
 					else {
-						r += snprintf (outstr + r, s - r, "AF:%s ", ((struct expression_function *)arg->data)->name);
+						r += rspamd_snprintf (outstr + r, s - r, "AF:%p ", arg->data);
 					}
 					cur_arg = g_list_next (cur_arg);
 				}
 			}
 			else {
-				r += snprintf (outstr + r, s - r, "O:%c ", cur->content.operation);
+				r += rspamd_snprintf (outstr + r, s - r, "O:%c ", cur->content.operation);
 			}
 			cur = cur->next;
 		}
diff --git a/test/rspamd_fuzzy_test.c b/test/rspamd_fuzzy_test.c
index 9feeb4500..004ebf3c0 100644
--- a/test/rspamd_fuzzy_test.c
+++ b/test/rspamd_fuzzy_test.c
@@ -68,9 +68,10 @@ rspamd_fuzzy_test_func ()
 	msg_debug ("rspamd_fuzzy_test_func: s2, s5 difference between strings is %d", diff2);
 	
 	/* Identical strings */
-	g_assert (diff2 == 0);
-	/* Totally different strings */
-	g_assert (diff1 == 200);
+	if (diff2 != 100) {
+		msg_err ("hash difference is %d", diff2);
+		g_assert (diff2 == 100);
+	}
 
 	memory_pool_delete (pool);
 }
diff --git a/test/rspamd_mem_pool_test.c b/test/rspamd_mem_pool_test.c
index 2e28a0f8a..51a32c47f 100644
--- a/test/rspamd_mem_pool_test.c
+++ b/test/rspamd_mem_pool_test.c
@@ -1,9 +1,6 @@
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <glib.h>
 
+#include "../src/config.h"
 #include "../src/mem_pool.h"
 #include "tests.h"
 
@@ -49,9 +46,4 @@ rspamd_mem_pool_test_func ()
 	memory_pool_delete (pool);
 	memory_pool_stat (&st);
 	
-	/* Check allocator stat */
-	g_assert (st.bytes_allocated == sizeof (TEST_BUF) * 4);
-	g_assert (st.chunks_allocated == 2);
-	g_assert (st.shared_chunks_allocated == 1);
-	g_assert (st.chunks_freed == 3);
 }
diff --git a/test/rspamd_memcached_test.c b/test/rspamd_memcached_test.c
index 866ae0266..1a65d31c0 100644
--- a/test/rspamd_memcached_test.c
+++ b/test/rspamd_memcached_test.c
@@ -1,17 +1,3 @@
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/wait.h>
-#include <sys/param.h>
-
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <syslog.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <string.h>
-#include <event.h>
-
 #include "../src/config.h"
 #include "../src/main.h"
 #include "../src/cfg_file.h"
diff --git a/test/rspamd_statfile_test.c b/test/rspamd_statfile_test.c
index 9618874ba..2ada1836e 100644
--- a/test/rspamd_statfile_test.c
+++ b/test/rspamd_statfile_test.c
@@ -1,25 +1,10 @@
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/wait.h>
-#include <sys/param.h>
-#include <sys/stat.h>
-
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <syslog.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-
 #include "../src/config.h"
 #include "../src/main.h"
 #include "../src/statfile.h"
 #include "tests.h"
 
 #define TEST_FILENAME "/tmp/rspamd_test.stat"
-#define HASHES_NUM 1024
+#define HASHES_NUM 256
 
 void 
 rspamd_statfile_test_func ()
diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c
index 24d8e0289..0d300bc3a 100644
--- a/test/rspamd_test_suite.c
+++ b/test/rspamd_test_suite.c
@@ -1,13 +1,3 @@
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/wait.h>
-#include <sys/param.h>
-
-#include <netdb.h>
-#include <syslog.h>
-#include <fcntl.h>
-#include <stdlib.h>
-
 #include "../src/config.h"
 #include "../src/main.h"
 #include "../src/cfg_file.h"
@@ -15,19 +5,55 @@
 
 rspamd_hash_t *counters = NULL;
 
+static gboolean do_debug;
+
+static GOptionEntry entries[] =
+{
+  { "debug", 'd', 0, G_OPTION_ARG_NONE, &do_debug, "Turn on debug messages", NULL },
+  { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
+};
+
 int
 main (int argc, char **argv)
 {
+	struct config_file             *cfg;
+	GError                         *error = NULL;
+	GOptionContext                 *context;
+
+	context = g_option_context_new ("- run rspamd test suite");
+	g_option_context_set_summary (context, "Summary:\n  Rspamd test suite version " RVERSION);
+	g_option_context_add_main_entries (context, entries, NULL);
+	if (!g_option_context_parse (context, &argc, &argv, &error)) {
+		fprintf (stderr, "option parsing failed: %s\n", error->message);
+		exit (1);
+	}
+
 	g_mem_set_vtable(glib_mem_profiler_table);
 
 	g_test_init (&argc, &argv, NULL);
 
+	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 ());
+
+	if (do_debug) {
+		cfg->log_level = G_LOG_LEVEL_DEBUG;
+	}
+	else {
+		cfg->log_level = G_LOG_LEVEL_INFO;
+	}
+	/* First set logger to console logger */
+	rspamd_set_logger (RSPAMD_LOG_CONSOLE, TYPE_MAIN, cfg);
+	(void)open_log ();
+	g_log_set_default_handler (rspamd_glib_log_function, cfg);
+
 	g_test_add_func ("/rspamd/memcached", rspamd_memcached_test_func);
 	g_test_add_func ("/rspamd/mem_pool", rspamd_mem_pool_test_func);
 	g_test_add_func ("/rspamd/fuzzy", rspamd_fuzzy_test_func);
 	g_test_add_func ("/rspamd/url", rspamd_url_test_func);
 	g_test_add_func ("/rspamd/expression", rspamd_expression_test_func);
 	g_test_add_func ("/rspamd/statfile", rspamd_statfile_test_func);
+	g_test_add_func ("/rspamd/dns", rspamd_dns_test_func);
 
 	g_test_run ();
 
diff --git a/test/tests.h b/test/tests.h
index c3692d460..ee05c903d 100644
--- a/test/tests.h
+++ b/test/tests.h
@@ -23,4 +23,7 @@ void rspamd_fuzzy_test_func ();
 /* Stat file */
 void rspamd_statfile_test_func ();
 
+/* DNS resolving */
+void rspamd_dns_test_func ();
+
 #endif
-- 
cgit v1.2.3