Exemplo n.º 1
0
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);
}
Exemplo n.º 2
0
void test_escaping(nagios_macros *mac)
{
	char *output;

	/* Nothing should be changed... options == 0 */
	RUN_MACRO_TEST("$HOSTNAME$ '&%", "name'&% '&%", 0);

	/* Able to escape illegal macro chars in HOSTCHECKCOMMAND */
	RUN_MACRO_TEST("$HOSTCHECKCOMMAND$ '&%", "check_command!3!Some output '&%", STRIP_ILLEGAL_MACRO_CHARS);
	RUN_MACRO_TEST("$HOSTCHECKCOMMAND$ '&%", "check_command!3!\"Some output\" '&%", 0);

	RUN_MACRO_TEST("$HOSTNOTES$", "notes%(action_url%)", STRIP_ILLEGAL_MACRO_CHARS);
	RUN_MACRO_TEST("$HOSTNOTES$", "notes'&%\"(action_url'&%)", 0);

	RUN_MACRO_TEST("$SERVICENOTES$", "notes%(action_url%)", STRIP_ILLEGAL_MACRO_CHARS);
	RUN_MACRO_TEST("$SERVICENOTES$", "notes'&%\"(action_url'&%)", 0);

	RUN_MACRO_TEST("$HOSTGROUPNOTES$", "notes%(action_url%)", STRIP_ILLEGAL_MACRO_CHARS);
	RUN_MACRO_TEST("$HOSTGROUPNOTES$", "notes'&%\"(action_url'&%)", 0);

	RUN_MACRO_TEST("$SERVICEGROUPNOTES$", "notes%(action_url%)", STRIP_ILLEGAL_MACRO_CHARS);
	RUN_MACRO_TEST("$SERVICEGROUPNOTES$", "notes'&%\"(action_url'&%)", 0);

	/* Nothing should be changed... HOSTNAME doesn't accept STRIP_ILLEGAL_MACRO_CHARS */
	RUN_MACRO_TEST("$HOSTNAME$ '&%", "name'&% '&%", STRIP_ILLEGAL_MACRO_CHARS);

	/* ' and & should be stripped from the macro, according to
	 * init_environment(), but not from the initial string
	 */
	RUN_MACRO_TEST("$HOSTOUTPUT$ '&%", "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("$HOSTOUTPUT$ '&%", "name'&% '&%", ESCAPE_MACRO_CHARS);
	RUN_MACRO_TEST("$HOSTOUTPUT$ '&%", "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%22%28action_url%27%26%25%29) '&%", 0);

	/* '& in the source string shouldn't be removed, because HOSTNOTESURL
	 * doesn't accept STRIP_ILLEGAL_MACRO_CHARS, 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%22%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%2522%2528action_url%2527%2526%2525%2529%29 '&%",
	               URL_ENCODE_MACRO_CHARS);

	/* Test for escaped $ value($$) */
	RUN_MACRO_TEST("$$ '&%",
	               "$ '&%",
	               URL_ENCODE_MACRO_CHARS);

	/* Testing for invalid macro */
	RUN_MACRO_TEST("$IDONOTEXIST$ '&%",
	               "$IDONOTEXIST$ '&%",
	               URL_ENCODE_MACRO_CHARS);

	/* Testing for incomplete macro */
	RUN_MACRO_TEST("we have an $ alone",
	               "we have an $ alone",
	               URL_ENCODE_MACRO_CHARS);
}