[asterisk-commits] dvossel: branch dvossel/sip_string_parse_testing r244061 - in /team/dvossel/s...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Jan 30 12:42:12 CST 2010


Author: dvossel
Date: Sat Jan 30 12:42:08 2010
New Revision: 244061

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=244061
Log:
move get_calleridname and unit test into request-parser.c

Modified:
    team/dvossel/sip_string_parse_testing/channels/chan_sip.c
    team/dvossel/sip_string_parse_testing/channels/sip/request-parser.c
    team/dvossel/sip_string_parse_testing/channels/sip/sip.h

Modified: team/dvossel/sip_string_parse_testing/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/sip_string_parse_testing/channels/chan_sip.c?view=diff&rev=244061&r1=244060&r2=244061
==============================================================================
--- team/dvossel/sip_string_parse_testing/channels/chan_sip.c (original)
+++ team/dvossel/sip_string_parse_testing/channels/chan_sip.c Sat Jan 30 12:42:08 2010
@@ -12904,188 +12904,6 @@
 	}
 }
 
-/*! \brief  Get caller id name from SIP headers, copy into output buffer
- *
- *  \retval input string pointer placed after display-name field if possible
- */
-static const char *get_calleridname(const char *input, char *output, size_t outputsize)
-{
-	/* From RFC3261:
-	 * 
-	 * From           =  ( "From" / "f" ) HCOLON from-spec
-	 * from-spec      =  ( name-addr / addr-spec ) *( SEMI from-param )
-	 * name-addr      =  [ display-name ] LAQUOT addr-spec RAQUOT
-	 * display-name   =  *(token LWS)/ quoted-string
-	 * token          =  1*(alphanum / "-" / "." / "!" / "%" / "*"
-	 *                     / "_" / "+" / "`" / "'" / "~" )
-	 * quoted-string  =  SWS DQUOTE *(qdtext / quoted-pair ) DQUOTE
-	 * qdtext         =  LWS / %x21 / %x23-5B / %x5D-7E
-	 *                     / UTF8-NONASCII
-	 * quoted-pair    =  "\" (%x00-09 / %x0B-0C / %x0E-7F)
-	 *
-	 * HCOLON         = *WSP ":" SWS
-	 * SWS            = [LWS]
-	 * LWS            = *[*WSP CRLF] 1*WSP
-	 * WSP            = (SP / HTAB)
-	 *
-	 * Deviations from it:
-	 * - following CRLF's in LWS is not done (here at least)
-	 * - ascii NUL is never legal as it terminates the C-string
-	 * - utf8-nonascii is not checked for validity
-	 */
-	char *orig_output = output;
-	const char *orig_input = input;
-
-	/* clear any empty characters in the beginning */
-	input = ast_skip_blanks(input);
-
-	/* no data at all or no storage room? */
-	if (!input || *input == '<' || !outputsize || !output) {
-		return orig_input;
-	}
-
-	/* make sure the output buffer is initilized */
-	*orig_output = '\0';
-
-	/* make room for '\0' at the end of the output buffer */
-	outputsize--;
-
-	/* quoted-string rules */
-	if (input[0] == '"') {
-		input++; /* skip the first " */
-
-		for (;((outputsize > 0) && *input); input++) {
-			if (*input == '"') {  /* end of quoted-string */
-				break;
-			} else if (*input == 0x5c) { /* quoted-pair = "\" (%x00-09 / %x0B-0C / %x0E-7F) */
-				input++;
-				if (!*input || (unsigned char)*input > 0x7f || *input == 0xa || *input == 0xd) {
-					continue;  /* not a valid quoted-pair, so skip it */
-				}
-			} else if (((*input != 0x9) && ((unsigned char) *input < 0x20)) ||
-			            (*input == 0x7f)) {
-				continue; /* skip this invalid character. */
-			}
-
-			*output++ = *input;
-			outputsize--;
-		}
-
-		/* if this is successful, input should be at the ending quote */
-		if (!input || *input != '"') {
-			ast_log(LOG_WARNING, "No ending quote for display-name was found\n");
-			*orig_output = '\0';
-			return orig_input;
-		}
-
-		/* make sure input is past the last quote */
-		input++;
-
-		/* terminate outbuf */
-		*output = '\0';
-	} else {  /* either an addr-spec or tokenLWS-combo */
-		for (;((outputsize > 0) && *input); input++) {
-			/* token or WSP (without LWS) */
-			if ((*input >= '0' && *input <= '9') || (*input >= 'A' && *input <= 'Z')
-				|| (*input >= 'a' && *input <= 'z') || *input == '-' || *input == '.'
-				|| *input == '!' || *input == '%' || *input == '*' || *input == '_'
-				|| *input == '+' || *input == '`' || *input == '\'' || *input == '~'
-				|| *input == 0x9 || *input == ' ') {
-				*output++ = *input;
-				outputsize -= 1;
-			} else if (*input == '<') {   /* end of tokenLWS-combo */
-				/* we could assert that the previous char is LWS, but we don't care */
-				break;
-			} else if (*input == ':') {
-				/* This invalid character which indicates this is addr-spec rather than display-name. */
-				*orig_output = '\0';
-				return orig_input;
-			} else {         /* else, invalid character we can skip. */
-				continue;    /* skip this character */
-			}
-		}
-
-		/* set NULL while trimming trailing whitespace */
-		do {
-			*output-- = '\0';
-		} while (*output == 0x9 || *output == ' '); /* we won't go past orig_output as first was a non-space */
-	}
-
-	return input;
-}
-
-AST_TEST_DEFINE(get_calleridname_test)
-{
-	int res = AST_TEST_PASS;
-	const char *in1 = "\" quoted-text internal \\\" quote \"<stuff>";
-	const char *in2 = " token text with no quotes <stuff>";
-	const char *overflow1 = " \"quoted-text overflow 1234567890123456789012345678901234567890\" <stuff>";
-	const char *noendquote = " \"quoted-text no end <stuff>";
-	const char *addrspec = " \"sip:blah at blah <stuff>";
-	const char *after_dname;
-	char dname[40];
-
-	switch (cmd) {
-	case TEST_INIT:
-		info->name = "sip_get_calleridname_test";
-		info->category = "channels/chan_sip/";
-		info->summary = "decodes callerid name from sip header";
-		info->description = "Decodes display-name field of sip header.  Checks for valid output and expected failure cases.";
-		return AST_TEST_NOT_RUN;
-	case TEST_EXECUTE:
-		break;
-	}
-
-	/* quoted-text with backslash escaped quote */
-	after_dname = get_calleridname(in1, dname, sizeof(dname));
-	ast_test_status_update(&args->status_update, "display-name1: %s\nafter: %s\n", dname, after_dname);
-	if (strcmp(dname, " quoted-text internal \" quote ")) {
-		ast_test_status_update(&args->status_update, "display-name1 test failed\n");
-		ast_str_append(&args->ast_test_error_str, 0, "quoted-text with internal backslash decode failed. \n");
-		res = AST_TEST_FAIL;
-	}
-
-	/* token text */
-	after_dname = get_calleridname(in2, dname, sizeof(dname));
-	ast_test_status_update(&args->status_update, "display-name2: %s\nafter: %s\n", dname, after_dname);
-	if (strcmp(dname, "token text with no quotes")) {
-		ast_test_status_update(&args->status_update, "display-name2 test failed\n");
-		ast_str_append(&args->ast_test_error_str, 0, "token text with decode failed. \n");
-		res = AST_TEST_FAIL;
-	}
-
-	/* quoted-text buffer overflow */
-	after_dname = get_calleridname(overflow1, dname, sizeof(dname));
-	ast_test_status_update(&args->status_update, "overflow display-name1: %s\nafter: %s\n", dname, after_dname);
-	if (*dname != '\0' && after_dname != overflow1) {
-		ast_test_status_update(&args->status_update, "overflow display-name1 test failed\n");
-		ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer overflow check failed. \n");
-		res = AST_TEST_FAIL;
-	}
-
-	/* quoted-text buffer with no terminating end quote */
-	after_dname = get_calleridname(noendquote, dname, sizeof(dname));
-	ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
-	if (*dname != '\0' && after_dname != noendquote) {
-		ast_test_status_update(&args->status_update, "no end quote for quoted-text display-name failed\n");
-		ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer check no terminating end quote failed. \n");
-		res = AST_TEST_FAIL;
-	}
-
-	/* addr-spec rather than display-name. */
-	after_dname = get_calleridname(addrspec, dname, sizeof(dname));
-	ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
-	if (*dname != '\0' && after_dname != addrspec) {
-		ast_test_status_update(&args->status_update, "detection of addr-spec failed\n");
-		ast_str_append(&args->ast_test_error_str, 0, "detection of addr-spec failed. \n");
-		res = AST_TEST_FAIL;
-	}
-
-
-	return res;
-}
-
-
 /*! \brief Validate device authentication */
 static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of,
 	struct sip_request *req, int sipmethod, struct sockaddr_in *sin,
@@ -25458,7 +25276,6 @@
 /*! \brief SIP test registration */
 static void sip_register_tests(void)
 {
-	AST_TEST_REGISTER(get_calleridname_test);
 	sip_config_parser_register_tests();
 	sip_request_parser_register_tests();
 }
@@ -25466,7 +25283,6 @@
 /*! \brief SIP test registration */
 static void sip_unregister_tests(void)
 {
-	AST_TEST_UNREGISTER(get_calleridname_test);
 	sip_config_parser_unregister_tests();
 	sip_request_parser_unregister_tests();
 }

Modified: team/dvossel/sip_string_parse_testing/channels/sip/request-parser.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/sip_string_parse_testing/channels/sip/request-parser.c?view=diff&rev=244061&r1=244060&r2=244061
==============================================================================
--- team/dvossel/sip_string_parse_testing/channels/sip/request-parser.c (original)
+++ team/dvossel/sip_string_parse_testing/channels/sip/request-parser.c Sat Jan 30 12:42:08 2010
@@ -190,11 +190,194 @@
 	return res;
 }
 
+/*! \brief  Get caller id name from SIP headers, copy into output buffer
+ *
+ *  \retval input string pointer placed after display-name field if possible
+ */
+const char *get_calleridname(const char *input, char *output, size_t outputsize)
+{
+	/* From RFC3261:
+	 * 
+	 * From           =  ( "From" / "f" ) HCOLON from-spec
+	 * from-spec      =  ( name-addr / addr-spec ) *( SEMI from-param )
+	 * name-addr      =  [ display-name ] LAQUOT addr-spec RAQUOT
+	 * display-name   =  *(token LWS)/ quoted-string
+	 * token          =  1*(alphanum / "-" / "." / "!" / "%" / "*"
+	 *                     / "_" / "+" / "`" / "'" / "~" )
+	 * quoted-string  =  SWS DQUOTE *(qdtext / quoted-pair ) DQUOTE
+	 * qdtext         =  LWS / %x21 / %x23-5B / %x5D-7E
+	 *                     / UTF8-NONASCII
+	 * quoted-pair    =  "\" (%x00-09 / %x0B-0C / %x0E-7F)
+	 *
+	 * HCOLON         = *WSP ":" SWS
+	 * SWS            = [LWS]
+	 * LWS            = *[*WSP CRLF] 1*WSP
+	 * WSP            = (SP / HTAB)
+	 *
+	 * Deviations from it:
+	 * - following CRLF's in LWS is not done (here at least)
+	 * - ascii NUL is never legal as it terminates the C-string
+	 * - utf8-nonascii is not checked for validity
+	 */
+	char *orig_output = output;
+	const char *orig_input = input;
+
+	/* clear any empty characters in the beginning */
+	input = ast_skip_blanks(input);
+
+	/* no data at all or no storage room? */
+	if (!input || *input == '<' || !outputsize || !output) {
+		return orig_input;
+	}
+
+	/* make sure the output buffer is initilized */
+	*orig_output = '\0';
+
+	/* make room for '\0' at the end of the output buffer */
+	outputsize--;
+
+	/* quoted-string rules */
+	if (input[0] == '"') {
+		input++; /* skip the first " */
+
+		for (;((outputsize > 0) && *input); input++) {
+			if (*input == '"') {  /* end of quoted-string */
+				break;
+			} else if (*input == 0x5c) { /* quoted-pair = "\" (%x00-09 / %x0B-0C / %x0E-7F) */
+				input++;
+				if (!*input || (unsigned char)*input > 0x7f || *input == 0xa || *input == 0xd) {
+					continue;  /* not a valid quoted-pair, so skip it */
+				}
+			} else if (((*input != 0x9) && ((unsigned char) *input < 0x20)) ||
+			            (*input == 0x7f)) {
+				continue; /* skip this invalid character. */
+			}
+
+			*output++ = *input;
+			outputsize--;
+		}
+
+		/* if this is successful, input should be at the ending quote */
+		if (!input || *input != '"') {
+			ast_log(LOG_WARNING, "No ending quote for display-name was found\n");
+			*orig_output = '\0';
+			return orig_input;
+		}
+
+		/* make sure input is past the last quote */
+		input++;
+
+		/* terminate outbuf */
+		*output = '\0';
+	} else {  /* either an addr-spec or tokenLWS-combo */
+		for (;((outputsize > 0) && *input); input++) {
+			/* token or WSP (without LWS) */
+			if ((*input >= '0' && *input <= '9') || (*input >= 'A' && *input <= 'Z')
+				|| (*input >= 'a' && *input <= 'z') || *input == '-' || *input == '.'
+				|| *input == '!' || *input == '%' || *input == '*' || *input == '_'
+				|| *input == '+' || *input == '`' || *input == '\'' || *input == '~'
+				|| *input == 0x9 || *input == ' ') {
+				*output++ = *input;
+				outputsize -= 1;
+			} else if (*input == '<') {   /* end of tokenLWS-combo */
+				/* we could assert that the previous char is LWS, but we don't care */
+				break;
+			} else if (*input == ':') {
+				/* This invalid character which indicates this is addr-spec rather than display-name. */
+				*orig_output = '\0';
+				return orig_input;
+			} else {         /* else, invalid character we can skip. */
+				continue;    /* skip this character */
+			}
+		}
+
+		/* set NULL while trimming trailing whitespace */
+		do {
+			*output-- = '\0';
+		} while (*output == 0x9 || *output == ' '); /* we won't go past orig_output as first was a non-space */
+	}
+
+	return input;
+}
+
+AST_TEST_DEFINE(get_calleridname_test)
+{
+	int res = AST_TEST_PASS;
+	const char *in1 = "\" quoted-text internal \\\" quote \"<stuff>";
+	const char *in2 = " token text with no quotes <stuff>";
+	const char *overflow1 = " \"quoted-text overflow 1234567890123456789012345678901234567890\" <stuff>";
+	const char *noendquote = " \"quoted-text no end <stuff>";
+	const char *addrspec = " \"sip:blah at blah <stuff>";
+	const char *after_dname;
+	char dname[40];
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "sip_get_calleridname_test";
+		info->category = "channels/chan_sip/";
+		info->summary = "decodes callerid name from sip header";
+		info->description = "Decodes display-name field of sip header.  Checks for valid output and expected failure cases.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	/* quoted-text with backslash escaped quote */
+	after_dname = get_calleridname(in1, dname, sizeof(dname));
+	ast_test_status_update(&args->status_update, "display-name1: %s\nafter: %s\n", dname, after_dname);
+	if (strcmp(dname, " quoted-text internal \" quote ")) {
+		ast_test_status_update(&args->status_update, "display-name1 test failed\n");
+		ast_str_append(&args->ast_test_error_str, 0, "quoted-text with internal backslash decode failed. \n");
+		res = AST_TEST_FAIL;
+	}
+
+	/* token text */
+	after_dname = get_calleridname(in2, dname, sizeof(dname));
+	ast_test_status_update(&args->status_update, "display-name2: %s\nafter: %s\n", dname, after_dname);
+	if (strcmp(dname, "token text with no quotes")) {
+		ast_test_status_update(&args->status_update, "display-name2 test failed\n");
+		ast_str_append(&args->ast_test_error_str, 0, "token text with decode failed. \n");
+		res = AST_TEST_FAIL;
+	}
+
+	/* quoted-text buffer overflow */
+	after_dname = get_calleridname(overflow1, dname, sizeof(dname));
+	ast_test_status_update(&args->status_update, "overflow display-name1: %s\nafter: %s\n", dname, after_dname);
+	if (*dname != '\0' && after_dname != overflow1) {
+		ast_test_status_update(&args->status_update, "overflow display-name1 test failed\n");
+		ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer overflow check failed. \n");
+		res = AST_TEST_FAIL;
+	}
+
+	/* quoted-text buffer with no terminating end quote */
+	after_dname = get_calleridname(noendquote, dname, sizeof(dname));
+	ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
+	if (*dname != '\0' && after_dname != noendquote) {
+		ast_test_status_update(&args->status_update, "no end quote for quoted-text display-name failed\n");
+		ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer check no terminating end quote failed. \n");
+		res = AST_TEST_FAIL;
+	}
+
+	/* addr-spec rather than display-name. */
+	after_dname = get_calleridname(addrspec, dname, sizeof(dname));
+	ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
+	if (*dname != '\0' && after_dname != addrspec) {
+		ast_test_status_update(&args->status_update, "detection of addr-spec failed\n");
+		ast_str_append(&args->ast_test_error_str, 0, "detection of addr-spec failed. \n");
+		res = AST_TEST_FAIL;
+	}
+
+	return res;
+}
+
+
 void sip_request_parser_register_tests(void)
 {
+	AST_TEST_REGISTER(get_calleridname_test);
 	AST_TEST_REGISTER(sip_parse_uri_test);
 }
 void sip_request_parser_unregister_tests(void)
 {
 	AST_TEST_UNREGISTER(sip_parse_uri_test);
-}
+	AST_TEST_UNREGISTER(get_calleridname_test);
+}

Modified: team/dvossel/sip_string_parse_testing/channels/sip/sip.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/sip_string_parse_testing/channels/sip/sip.h?view=diff&rev=244061&r1=244060&r2=244061
==============================================================================
--- team/dvossel/sip_string_parse_testing/channels/sip/sip.h (original)
+++ team/dvossel/sip_string_parse_testing/channels/sip/sip.h Sat Jan 30 12:42:08 2010
@@ -1318,6 +1318,12 @@
  */
 int parse_uri(char *uri, const char *scheme, char **ret_name, char **pass, char **domain, char **port, char **transport);
 
+/*! \brief  Get caller id name from SIP headers, copy into output buffer
+ *
+ *  \retval input string pointer placed after display-name field if possible
+ */
+const char *get_calleridname(const char *input, char *output, size_t outputsize);
+
 /*! \brief register request parsing tests */
 void sip_request_parser_register_tests(void);
 




More information about the asterisk-commits mailing list