[syslog-ng] [PATCH (3.5) 1/2] tests: Add a testcase for template_escape()

Gergely Nagy algernon at balabit.hu
Wed Jun 12 15:10:45 CEST 2013


In order not to break template_escape() functionality, add a test case
for basic functionality testing.

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 libtest/template_lib.c     |   17 +++++++++++++----
 libtest/template_lib.h     |    5 +++--
 tests/unit/test_template.c |   10 +++++++++-
 3 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/libtest/template_lib.c b/libtest/template_lib.c
index 38dfeff..7992608 100644
--- a/libtest/template_lib.c
+++ b/libtest/template_lib.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012 BalaBit IT Ltd, Budapest, Hungary
+ * Copyright (c) 2012-2013 BalaBit IT Ltd, Budapest, Hungary
  * Copyright (c) 2012 Balázs Scheidler
  *
  * This library is free software; you can redistribute it and/or
@@ -53,6 +53,7 @@ create_sample_message(void)
   log_msg_set_value(msg, log_msg_get_value_handle("APP.STRIP3"), "     value     ", -1);
   log_msg_set_value(msg, log_msg_get_value_handle("APP.STRIP4"), "value", -1);
   log_msg_set_value(msg, log_msg_get_value_handle("APP.STRIP5"), "", -1);
+  log_msg_set_value(msg, log_msg_get_value_handle("APP.QVALUE"), "\"value\"", -1);
   log_msg_set_match(msg, 0, "whole-match", -1);
   log_msg_set_match(msg, 1, "first-match", -1);
   log_msg_set_tag_by_name(msg, "alma");
@@ -70,12 +71,13 @@ create_sample_message(void)
 }
 
 LogTemplate *
-compile_template(const gchar *template)
+compile_template(const gchar *template, gboolean escaping)
 {
   LogTemplate *templ = log_template_new(configuration, NULL);
   gboolean success;
   GError *error = NULL;
 
+  log_template_set_escape(templ, escaping);
   success = log_template_compile(templ, template, &error);
   assert_true(success, "template expected to compile cleanly, but it didn't, template=%s, error=%s", template, error ? error->message : "(none)");
   g_clear_error(&error);
@@ -86,6 +88,13 @@ compile_template(const gchar *template)
 void
 assert_template_format(const gchar *template, const gchar *expected)
 {
+  assert_template_format_with_escaping(template, FALSE, expected);
+}
+
+void
+assert_template_format_with_escaping(const gchar *template, gboolean escaping,
+                                     const gchar *expected)
+{
   LogMessage *msg;
   LogTemplate *templ;
   GString *res = g_string_sized_new(128);
@@ -93,7 +102,7 @@ assert_template_format(const gchar *template, const gchar *expected)
 
   msg = create_sample_message();
 
-  templ = compile_template(template);
+  templ = compile_template(template, escaping);
   log_template_format(templ, msg, NULL, LTZ_LOCAL, 999, context_id, res);
   assert_string(res->str, expected, "template test failed, template=%s", template);
   log_template_unref(templ);
@@ -113,7 +122,7 @@ assert_template_format_with_context(const gchar *template, const gchar *expected
   msg = create_sample_message();
   context[0] = context[1] = msg;
 
-  templ = compile_template(template);
+  templ = compile_template(template, FALSE);
 
   log_template_format_with_context(templ, context, 2, NULL, LTZ_LOCAL, 999, context_id, res);
   assert_string(res->str, expected, "context template test failed, template=%s", template);
diff --git a/libtest/template_lib.h b/libtest/template_lib.h
index 760858c..f875e4f 100644
--- a/libtest/template_lib.h
+++ b/libtest/template_lib.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012 BalaBit IT Ltd, Budapest, Hungary
+ * Copyright (c) 2012-2013 BalaBit IT Ltd, Budapest, Hungary
  * Copyright (c) 2012 Balázs Scheidler
  *
  * This library is free software; you can redistribute it and/or
@@ -29,11 +29,12 @@
 #include "templates.h"
 
 void assert_template_format(const gchar *template, const gchar *expected);
+void assert_template_format_with_escaping(const gchar *template, gboolean escaping, const gchar *expected);
 void assert_template_format_with_context(const gchar *template, const gchar *expected);
 void assert_template_failure(const gchar *template, const gchar *expected_failure);
 
 LogMessage *create_sample_message(void);
-LogTemplate *compile_template(const gchar *template);
+LogTemplate *compile_template(const gchar *template, gboolean escaping);
 void init_template_tests(void);
 void deinit_template_tests(void);
 
diff --git a/tests/unit/test_template.c b/tests/unit/test_template.c
index e5ff1a0..8546dd4 100644
--- a/tests/unit/test_template.c
+++ b/tests/unit/test_template.c
@@ -53,7 +53,7 @@ assert_template_format_multi_thread(const gchar *template, const gchar *expected
   gint i;
 
   msg = create_sample_message();
-  templ = compile_template(template);
+  templ = compile_template(template, FALSE);
   args[0] = msg;
   args[1] = templ;
   args[2] = (gpointer) expected;
@@ -248,6 +248,13 @@ test_multi_thread(void)
   assert_template_format_multi_thread("dani $(echo $HOST $DATE $(echo huha)) balint", "dani bzorp Feb 11 10:34:56.000 huha balint");
 }
 
+static void
+test_escaping(void)
+{
+  assert_template_format_with_escaping("${APP.QVALUE}", FALSE, "\"value\"");
+  assert_template_format_with_escaping("${APP.QVALUE}", TRUE, "\\\"value\\\"");
+}
+
 int
 main(int argc G_GNUC_UNUSED, char *argv[] G_GNUC_UNUSED)
 {
@@ -269,6 +276,7 @@ main(int argc G_GNUC_UNUSED, char *argv[] G_GNUC_UNUSED)
   test_syntax_errors();
   test_compat();
   test_multi_thread();
+  test_escaping();
 
   /* multi-threaded expansion */
 
-- 
1.7.10.4




More information about the syslog-ng mailing list