[asterisk-commits] tilghman: branch tilghman/str_substitution r180787 - in /team/tilghman/str_su...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Mar 9 19:16:29 CDT 2009


Author: tilghman
Date: Mon Mar  9 19:16:25 2009
New Revision: 180787

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=180787
Log:
Test module, as suggested by Russell, along with several associated fixes

Added:
    team/tilghman/str_substitution/tests/test_substitution.c   (with props)
Modified:
    team/tilghman/str_substitution/main/pbx.c
    team/tilghman/str_substitution/main/strings.c
    team/tilghman/str_substitution/res/res_phoneprov.c

Modified: team/tilghman/str_substitution/main/pbx.c
URL: http://svn.digium.com/svn-view/asterisk/team/tilghman/str_substitution/main/pbx.c?view=diff&rev=180787&r1=180786&r2=180787
==============================================================================
--- team/tilghman/str_substitution/main/pbx.c (original)
+++ team/tilghman/str_substitution/main/pbx.c Mon Mar  9 19:16:25 2009
@@ -2991,14 +2991,17 @@
 			ast_rwlock_unlock(&globalslock);
 	}
 	if (s == &not_found || s == NULL) {
+		ast_debug(5, "Result of '%s' is NULL\n", var);
 		ret = NULL;
 	} else {
+		ast_debug(5, "Result of '%s' is '%s'\n", var, s);
 		if (s != ast_str_buffer(*str)) {
 			ast_str_set(str, maxlen, "%s", s);
 		}
 		ret = ast_str_buffer(*str);
 		if (need_substring) {
 			ret = ast_str_substring(*str, offset, length);
+			ast_debug(2, "Final result of '%s' is '%s'\n", var, ret);
 		}
 	}
 
@@ -3489,8 +3492,9 @@
 	const char *tmp, *whereweare;
 	int orig_size = 0;
 	int offset, offset2, isfunction;
-	char *nextvar, *nextexp, *nextthing;
-	char *vars, *vare;
+	const char *nextvar, *nextexp, *nextthing;
+	const char *vars, *vare;
+	char *finalvars;
 	int pos, brackets, needsub, len;
 	struct ast_str *substr1 = ast_str_create(16), *substr2 = NULL, *substr3 = ast_str_create(16);
 
@@ -3554,6 +3558,7 @@
 
 			/* Store variable name (and truncate) */
 			ast_str_set_substr(&substr1, 0, vars, len);
+			ast_debug(5, "Evaluating '%s' (from '%s' len %d)\n", ast_str_buffer(substr1), vars, len);
 
 			/* Substitute if necessary */
 			if (needsub) {
@@ -3563,23 +3568,23 @@
 				}
 
 				ast_str_substitute_variables_full(&substr2, 0, c, headp, ast_str_buffer(substr1), &used);
-				vars = ast_str_buffer(substr2);
+				finalvars = ast_str_buffer(substr2);
 			} else {
-				vars = ast_str_buffer(substr1);
-			}
-
-			parse_variable_name(vars, &offset, &offset2, &isfunction);
+				finalvars = ast_str_buffer(substr1);
+			}
+
+			parse_variable_name(finalvars, &offset, &offset2, &isfunction);
 			if (isfunction) {
 				/* Evaluate function */
 				if (c || !headp) {
-					cp4 = ast_func_read2(c, vars, &substr3, 0) ? NULL : ast_str_buffer(substr3);
+					cp4 = ast_func_read2(c, finalvars, &substr3, 0) ? NULL : ast_str_buffer(substr3);
 				} else {
 					struct varshead old;
 					struct ast_channel *bogus = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Bogus/%p", vars);
 					if (bogus) {
 						memcpy(&old, &bogus->varshead, sizeof(old));
 						memcpy(&bogus->varshead, headp, sizeof(bogus->varshead));
-						cp4 = ast_func_read2(c, vars, &substr3, 0) ? NULL : ast_str_buffer(substr3);
+						cp4 = ast_func_read2(c, finalvars, &substr3, 0) ? NULL : ast_str_buffer(substr3);
 						/* Don't deallocate the varshead that was passed in */
 						memcpy(&bogus->varshead, &old, sizeof(bogus->varshead));
 						ast_channel_free(bogus);
@@ -3590,7 +3595,7 @@
 				ast_debug(2, "Function result is '%s'\n", cp4 ? cp4 : "(null)");
 			} else {
 				/* Retrieve variable value */
-				ast_str_retrieve_variable(&substr3, 0, c, headp, vars);
+				ast_str_retrieve_variable(&substr3, 0, c, headp, finalvars);
 				cp4 = ast_str_buffer(substr3);
 			}
 			if (cp4) {
@@ -3639,13 +3644,13 @@
 				}
 
 				ast_str_substitute_variables_full(&substr2, 0, c, headp, ast_str_buffer(substr1), &used);
-				vars = ast_str_buffer(substr2);
+				finalvars = ast_str_buffer(substr2);
 			} else {
-				vars = ast_str_buffer(substr1);
-			}
-
-			if (ast_str_expr(&substr3, 0, c, vars)) {
-				ast_debug(1, "Expression result is '%s'\n", ast_str_buffer(substr3));
+				finalvars = ast_str_buffer(substr1);
+			}
+
+			if (ast_str_expr(&substr3, 0, c, finalvars)) {
+				ast_debug(2, "Expression result is '%s'\n", ast_str_buffer(substr3));
 			}
 			ast_str_append(buf, maxlen, "%s", ast_str_buffer(substr3));
 		}

Modified: team/tilghman/str_substitution/main/strings.c
URL: http://svn.digium.com/svn-view/asterisk/team/tilghman/str_substitution/main/strings.c?view=diff&rev=180787&r1=180786&r2=180787
==============================================================================
--- team/tilghman/str_substitution/main/strings.c (original)
+++ team/tilghman/str_substitution/main/strings.c Mon Mar  9 19:16:25 2009
@@ -134,7 +134,7 @@
 			ptr += (*buf)->__AST_STR_STR - oldbase;
 		}
 	}
-	if (__builtin_expect(!(maxsrc && maxlen), 0)) {
+	if (__builtin_expect(!maxlen, 0)) {
 		ptr--;
 	}
 	*ptr = '\0';

Modified: team/tilghman/str_substitution/res/res_phoneprov.c
URL: http://svn.digium.com/svn-view/asterisk/team/tilghman/str_substitution/res/res_phoneprov.c?view=diff&rev=180787&r1=180786&r2=180787
==============================================================================
--- team/tilghman/str_substitution/res/res_phoneprov.c (original)
+++ team/tilghman/str_substitution/res/res_phoneprov.c Mon Mar  9 19:16:25 2009
@@ -512,7 +512,7 @@
 
 		ast_str_append(&result, 0,
 			"Content-Type: %s\r\n"
-			"Content-length: %d\r\n"
+			"Content-length: %zd\r\n"
 			"\r\n"
 			"%s", route->file->mime_type, ast_str_strlen(tmp), ast_str_buffer(tmp));
 
@@ -1111,7 +1111,8 @@
 		}
 		ast_str_substitute_variables_varshead(&str, len, AST_LIST_FIRST(&user->extensions)->headp, args.string);
 		if (buf) {
-			ast_build_string(&buf, (size_t *) &len, "%s", ast_str_buffer(str));
+			size_t slen = len;
+			ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
 		} else {
 			ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
 		}
@@ -1193,7 +1194,8 @@
 	AST_LIST_TRAVERSE(&user->extensions, exten, entry) {
 		ast_str_substitute_variables_varshead(&str, 0, exten->headp, file);
 		if (buf) {
-			ast_build_string(&buf, (size_t *) &len, "%s", ast_str_buffer(str));
+			size_t slen = len;
+			ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
 		} else {
 			ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
 		}

Added: team/tilghman/str_substitution/tests/test_substitution.c
URL: http://svn.digium.com/svn-view/asterisk/team/tilghman/str_substitution/tests/test_substitution.c?view=auto&rev=180787
==============================================================================
--- team/tilghman/str_substitution/tests/test_substitution.c (added)
+++ team/tilghman/str_substitution/tests/test_substitution.c Mon Mar  9 19:16:25 2009
@@ -1,0 +1,202 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Tilghman Lesher <tlesher AT digium DOT com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Substitution Test
+ *
+ * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
+ * 
+ * \ingroup tests
+ */
+
+/*** MODULEINFO
+	<defaultenabled>no</defaultenabled>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/file.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/lock.h"
+#include "asterisk/app.h"
+#include "asterisk/strings.h"
+#include "asterisk/stringfields.h"
+#include "asterisk/threadstorage.h"
+#include "asterisk/cli.h"
+
+AST_THREADSTORAGE(buf_buf);
+AST_THREADSTORAGE(var_buf);
+
+static void test_chan_integer(int fd, struct ast_channel *c, int *ifield, const char *expression)
+{
+	int i, okay = 1, value1 = -1, value2 = -1;
+	char workspace[4096];
+	struct ast_str *str = ast_str_thread_get(&buf_buf, 16);
+
+	for (i = 0; i < 256; i++) {
+		*ifield = i;
+		ast_str_substitute_variables(&str, 0, c, expression);
+		pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
+		if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) {
+			ast_cli(fd, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i);
+			okay = 0;
+			break;
+		}
+	}
+	ast_cli(fd, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
+}
+
+static void test_chan_string(int fd, struct ast_channel *c, char *cfield, size_t cfieldsize, const char *expression)
+{
+	const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
+	int i, okay = 1;
+	char workspace[4096];
+	struct ast_str *str = ast_str_thread_get(&buf_buf, 16);
+
+	for (i = 0; i < ARRAY_LEN(values); i++) {
+		ast_copy_string(cfield, values[i], cfieldsize);
+		ast_str_substitute_variables(&str, 0, c, expression);
+		pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
+		if (strcmp(cfield, ast_str_buffer(str)) != 0 || strcmp(cfield, workspace) != 0) {
+			ast_cli(fd, "%s != %s != %s\n", cfield, ast_str_buffer(str), workspace);
+			okay = 0;
+			break;
+		}
+	}
+	ast_cli(fd, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
+}
+
+static void test_chan_variable(int fd, struct ast_channel *c, const char *varname)
+{
+	const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
+	int i, okay = 1;
+	char workspace[4096];
+	struct ast_str *str = ast_str_thread_get(&buf_buf, 16);
+	struct ast_str *var = ast_str_thread_get(&var_buf, 16);
+
+	ast_str_set(&var, 0, "${%s}", varname);
+	for (i = 0; i < ARRAY_LEN(values); i++) {
+		pbx_builtin_setvar_helper(c, varname, values[i]);
+		ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var));
+		pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace));
+		if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) {
+			ast_cli(fd, "%s != %s != %s\n", values[i], ast_str_buffer(str), workspace);
+			okay = 0;
+			break;
+		}
+	}
+	ast_cli(fd, "Testing '%s' . . . . . %s\n", ast_str_buffer(var), okay ? "passed" : "FAILED");
+}
+
+static void test_chan_function(int fd, struct ast_channel *c, const char *expression)
+{
+	int okay = 1;
+	char workspace[4096];
+	struct ast_str *str = ast_str_thread_get(&buf_buf, 16);
+
+	ast_str_substitute_variables(&str, 0, c, expression);
+	pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
+	if (strcmp(workspace, ast_str_buffer(str)) != 0) {
+		ast_cli(fd, "%s != %s\n", ast_str_buffer(str), workspace);
+		okay = 0;
+	}
+	ast_cli(fd, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
+}
+
+static char *handle_cli_test_substitution(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct ast_channel *c;
+	int i;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "test substitution";
+		e->usage = ""
+			"Usage: test substitution\n"
+			"   Test variable and function substitution.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != e->args) {
+		return CLI_SHOWUSAGE;
+	}
+
+	ast_cli(a->fd, "Testing variable substitution ...\n");
+	c = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Test/substitution");
+
+	test_chan_integer(a->fd, c, &c->cid.cid_pres, "${CALLINGPRES}");
+	test_chan_integer(a->fd, c, &c->cid.cid_ani2, "${CALLINGANI2}");
+	test_chan_integer(a->fd, c, &c->cid.cid_ton, "${CALLINGTON}");
+	test_chan_integer(a->fd, c, &c->cid.cid_tns, "${CALLINGTNS}");
+	test_chan_integer(a->fd, c, &c->hangupcause, "${HANGUPCAUSE}");
+	test_chan_integer(a->fd, c, &c->priority, "${PRIORITY}");
+	test_chan_string(a->fd, c, c->context, sizeof(c->context), "${CONTEXT}");
+	test_chan_string(a->fd, c, c->exten, sizeof(c->exten), "${EXTEN}");
+	test_chan_variable(a->fd, c, "CHANNEL(language)");
+	test_chan_variable(a->fd, c, "CHANNEL(musicclass)");
+	test_chan_variable(a->fd, c, "CHANNEL(parkinglot)");
+	test_chan_variable(a->fd, c, "CALLERID(name)");
+
+	/* For testing dialplan functions */
+	for (i = 0; ; i++) {
+		char *cmd = ast_cli_generator("core show function", "", i);
+		if (cmd == NULL) {
+			break;
+		}
+		if (strcmp(cmd, "CHANNEL") && strcmp(cmd, "CALLERID")) {
+			struct ast_custom_function *acf = ast_custom_function_find(cmd);
+			if (acf->read && acf->read2) {
+				char expression[80];
+				snprintf(expression, sizeof(expression), "${%s(foo)}", cmd);
+				test_chan_function(a->fd, c, expression);
+			} else {
+				ast_cli(a->fd, "No test defined for function '%s'\n", cmd);
+			}
+		}
+		ast_free(cmd);
+	}
+
+	ast_hangup(c);
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_substitution[] = {
+	AST_CLI_DEFINE(handle_cli_test_substitution, "Test variable substitution"),
+};
+
+static int unload_module(void)
+{
+	ast_cli_unregister_multiple(cli_substitution, ARRAY_LEN(cli_substitution));
+	return 0;
+}
+
+static int load_module(void)
+{
+	ast_cli_register_multiple(cli_substitution, ARRAY_LEN(cli_substitution));
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Substitution tests");

Propchange: team/tilghman/str_substitution/tests/test_substitution.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/tilghman/str_substitution/tests/test_substitution.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/tilghman/str_substitution/tests/test_substitution.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list