[PATCH 3/3] Test macros: Initial tests for escaping of macros

Max Sikstrom max.sikstrom at op5.com
Wed May 22 11:50:33 CEST 2013


From: Max Sikström <msikstrom at op5.com>

This tests escaping and url-encoding of some macros

This doesn't test all macros, but the functionality of escaping.

Signed-off-by: Max Sikström <msikstrom at op5.com>
---
 t-tap/Makefile.in   |    4 +
 t-tap/test_macros.c |  211 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 215 insertions(+), 0 deletions(-)
 create mode 100644 t-tap/test_macros.c

diff --git a/t-tap/Makefile.in b/t-tap/Makefile.in
index 3079ee0..324cb8b 100644
--- a/t-tap/Makefile.in
+++ b/t-tap/Makefile.in
@@ -18,6 +18,7 @@ TESTS += test_checks
 TESTS += test_strtoul
 TESTS += test_commands
 TESTS += test_downtime
+TESTS += test_macros
 
 XSD_OBJS = $(SRC_CGI)/statusdata-cgi.o $(SRC_CGI)/xstatusdata-cgi.o
 XSD_OBJS += $(SRC_CGI)/objects-cgi.o $(SRC_CGI)/xobjects-cgi.o
@@ -114,6 +115,9 @@ test_nagios_config: test_nagios_config.o $(CFG_OBJS) $(TAPOBJ)
 test_timeperiods: test_timeperiods.o $(TP_OBJS) $(TAPOBJ)
 	$(CC) $(CFLAGS) -o $@ $^ $(BROKER_LDFLAGS) $(LDFLAGS) $(MATHLIBS) $(SOCKETLIBS) $(BROKERLIBS) $(LIBS)
 
+test_macros: test_macros.o $(TP_OBJS) $(TAPOBJ)
+	$(CC) $(CFLAGS) -o $@ $^ $(BROKER_LDFLAGS) $(LDFLAGS) $(MATHLIBS) $(SOCKETLIBS) $(LIBS)
+
 test_xsddefault: test_xsddefault.o $(XSD_OBJS) $(TAPOBJ)
 	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
 
diff --git a/t-tap/test_macros.c b/t-tap/test_macros.c
new file mode 100644
index 0000000..ebc6a03
--- /dev/null
+++ b/t-tap/test_macros.c
@@ -0,0 +1,211 @@
+/*****************************************************************************
+ *
+ * test_macros.c - Test macro expansion and escaping
+ *
+ * Program: Nagios Core Testing
+ * License: GPL
+ *
+ * First Written:   2013-05-21
+ *
+ * Description:
+ *
+ * Tests expansion of macros and escaping.
+ *
+ * License:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ *****************************************************************************/
+
+#include <string.h>
+#include "../include/objects.h"
+#include "../include/nagios.h"
+#include "tap.h"
+
+/*****************************************************************************/
+/*                             Dummy functions                               */
+/*****************************************************************************/
+void logit(int data_type, int display, const char *fmt, ...) {
+}
+int my_sendall(int s, char *buf, int *len, int timeout) {
+	return 0;
+}
+void free_comment_data(void) {
+}
+
+int log_debug_info(int level, int verbosity, const char *fmt, ...) {
+	return 0;
+}
+
+int neb_free_callback_list(void) {
+	return 0;
+}
+
+int neb_deinit_modules(void) {
+	return 0;
+}
+void broker_program_state(int type, int flags, int attr,
+		struct timeval *timestamp) {
+}
+int neb_unload_all_modules(int flags, int reason) {
+	return 0;
+}
+int neb_add_module(char *filename, char *args, int should_be_loaded) {
+	return 0;
+}
+void broker_system_command(int type, int flags, int attr,
+		struct timeval start_time, struct timeval end_time, double exectime,
+		int timeout, int early_timeout, int retcode, char *cmd, char *output,
+		struct timeval *timestamp) {
+}
+
+timed_event *schedule_new_event(int event_type, int high_priority,
+		time_t run_time, int recurring, unsigned long event_interval,
+		void *timing_func, int compensate_for_time_change, void *event_data,
+		void *event_args, int event_options) {
+	return NULL ;
+}
+int my_tcp_connect(char *host_name, int port, int *sd, int timeout) {
+	return 0;
+}
+int my_recvall(int s, char *buf, int *len, int timeout) {
+	return 0;
+}
+int neb_free_module_list(void) {
+	return 0;
+}
+int close_command_file(void) {
+	return 0;
+}
+int close_log_file(void) {
+	return 0;
+}
+int fix_log_file_owner(uid_t uid, gid_t gid) {
+	return 0;
+}
+int handle_async_service_check_result(service *temp_service,
+		check_result *queued_check_result) {
+	return 0;
+}
+int handle_async_host_check_result(host *temp_host,
+		check_result *queued_check_result) {
+	return 0;
+}
+
+/*****************************************************************************/
+/*                             Local test environment                        */
+/*****************************************************************************/
+
+host test_host = { .name = "name'&%", .address = "address'&%", .notes_url =
+		"notes_url'&%($HOSTNOTES$)", .notes = "notes'&%($HOSTACTIONURL$)",
+		.action_url = "action_url'&%" };
+
+/*****************************************************************************/
+/*                             Helper functions                              */
+/*****************************************************************************/
+
+void init_environment() {
+	char *p;
+
+	my_free(illegal_output_chars);
+	illegal_output_chars = strdup("'&"); /* For this tests, remove ' and & */
+
+	/* This is a part of preflight check, which we can't run */
+	for (p = illegal_output_chars; *p; p++) {
+		illegal_output_char_map[(int) *p] = 1;
+	}
+}
+
+nagios_macros *setup_macro_object(void) {
+	nagios_macros *mac = (nagios_macros *) calloc(1, sizeof(nagios_macros));
+	grab_host_macros_r(mac, &test_host);
+	return mac;
+}
+
+#define RUN_MACRO_TEST(_STR, _EXPECT, _OPTS) \
+	do { \
+		if( OK == process_macros_r(mac, (_STR), &output, _OPTS ) ) {\
+			ok( 0 == strcmp( output, _EXPECT ), "'%s': '%s' == '%s'", (_STR), output, (_EXPECT) ); \
+		} else { \
+			fail( "process_macros_r returns ERROR for " _STR ); \
+		} \
+	} while(0)
+
+/*****************************************************************************/
+/*                             Tests                                         */
+/*****************************************************************************/
+
+void test_escaping(nagios_macros *mac) {
+	char *output;
+
+	/* Nothing should be changed... options == 0 */
+	RUN_MACRO_TEST( "$HOSTNAME$ '&%", "name'&% '&%", 0);
+
+	/* ' and & should be stripped from the macro, according to
+	 * init_environment(), but not from the initial string
+	 */
+	RUN_MACRO_TEST( "$HOSTNAME$ '&%", "name% '&%", STRIP_ILLEGAL_MACRO_CHARS);
+
+	/* ESCAPE_MACRO_CHARS doesn't seem to do anything... exist always in pair
+	 * with STRIP_ILLEGAL_MACRO_CHARS
+	 */
+	RUN_MACRO_TEST( "$HOSTNAME$ '&%", "name'&% '&%", ESCAPE_MACRO_CHARS);
+	RUN_MACRO_TEST( "$HOSTNAME$ '&%", "name% '&%",
+			STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS);
+
+	/* $HOSTNAME$ should be url-encoded, but not the tailing chars */
+	RUN_MACRO_TEST( "$HOSTNAME$ '&%", "name%27%26%25 '&%",
+			URL_ENCODE_MACRO_CHARS);
+
+	/* The notes in the notesurl should be url-encoded, no more encoding should
+	 * exist
+	 */
+	RUN_MACRO_TEST( "$HOSTNOTESURL$ '&%",
+			"notes_url'&%(notes%27%26%25%28action_url%27%26%25%29) '&%", 0);
+
+	/* '& in the source string should be removed, as in the url. the macros
+	 * included in the string should be url-encoded, and therefore not contain &
+	 * and '
+	 */
+	RUN_MACRO_TEST( "$HOSTNOTESURL$ '&%",
+			"notes_url%(notes%27%26%25%28action_url%27%26%25%29) '&%",
+			STRIP_ILLEGAL_MACRO_CHARS);
+
+	/* This should double-encode some chars ($HOSTNOTESURL$ should contain
+	 * url-encoded chars, and should itself be url-encoded
+	 */
+	RUN_MACRO_TEST( "$HOSTNOTESURL$ '&%",
+			"notes_url%27%26%25%28notes%2527%2526%2525%2528action_url%2527%2526%2525%2529%29 '&%",
+			URL_ENCODE_MACRO_CHARS);
+}
+
+/*****************************************************************************/
+/*                             Main function                                 */
+/*****************************************************************************/
+
+int main(void) {
+	nagios_macros *mac;
+
+	reset_variables();
+	init_environment();
+	init_macros();
+
+	mac = setup_macro_object();
+
+	test_escaping(mac);
+
+	cleanup();
+	free(mac);
+	return exit_status();
+}
-- 
1.7.1


------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service 
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list