[Asterisk-code-review] app_assert: Add Assert application (asterisk[master])

N A asteriskteam at digium.com
Sun Oct 24 06:39:55 CDT 2021


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/16629 )


Change subject: app_assert: Add Assert application
......................................................................

app_assert: Add Assert application

Adds an assertion application to the dialplan that
can be used to verify constraints or properties
in the dialplan, similar to the assert() function
in C. Useful for codifying and enforcing behavior
and for test cases in the dialplan.

ASTERISK-29701 #close

Change-Id: Ia089c2debf608f42f5f87e6c29d50e8ebcc093e5
---
A apps/app_assert.c
A doc/CHANGES-staging/app_assert.txt
2 files changed, 172 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/29/16629/1

diff --git a/apps/app_assert.c b/apps/app_assert.c
new file mode 100644
index 0000000..556926e
--- /dev/null
+++ b/apps/app_assert.c
@@ -0,0 +1,167 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2021, Naveen Albert
+ *
+ * Naveen Albert <asterisk at phreaknet.org>
+ *
+ * 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 Dialplan assertion application
+ *
+ * \author Naveen Albert <asterisk at phreaknet.org>
+ *
+ * \ingroup applications
+ */
+
+/*** MODULEINFO
+	<support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/file.h"
+#include "asterisk/pbx.h"
+#include "asterisk/channel.h"
+#include "asterisk/dsp.h"
+#include "asterisk/app.h"
+#include "asterisk/module.h"
+#include "asterisk/indications.h"
+#include "asterisk/conversions.h"
+
+/*** DOCUMENTATION
+	<application name="Assert" language="en_US">
+		<synopsis>
+			Asserts that an expression is true.
+		</synopsis>
+		<syntax>
+			<parameter name="expression">
+				<para>The expression to evaluate.</para>
+			</parameter>
+			<parameter name="options">
+				<optionlist>
+					<option name="d">
+						<para>Do not hang up if expression is false.</para>
+					</option>
+				</optionlist>
+			</parameter>
+		</syntax>
+		<description>
+			<para>Evaluates <replaceable>expression</replaceable> and continues dialplan
+			execution if the expression is true and ends the call with a warning
+			if it is false (unless the <replaceable>d</replaceable> option is provided).</para>
+			<para>This application can be used to verify functional correctness
+			of dialplans (e.g. dialplan test cases), similar to the <replaceable>assert</replaceable>
+			function in C. For instance, if a certain property is expected to always hold at some
+			point in your dialplan, this application can be used to enforce that.</para>
+		</description>
+		<see-also>
+			<ref type="application">RaiseException</ref>
+		</see-also>
+	</application>
+ ***/
+
+enum read_option_flags {
+	OPT_NOHANGUP = (1 << 0),
+};
+
+AST_APP_OPTIONS(read_app_options, {
+	AST_APP_OPTION('d', OPT_NOHANGUP),
+});
+
+static const char *app = "Assert";
+
+static int assert_exec(struct ast_channel *chan, const char *data)
+{
+	char *argcopy = NULL;
+	struct ast_flags flags = {0};
+	int value = 0;
+
+	AST_DECLARE_APP_ARGS(arglist,
+		AST_APP_ARG(expression);
+		AST_APP_ARG(options);
+	);
+
+	if (ast_strlen_zero(data)) {
+		ast_log(LOG_WARNING, "%s requires an argument (variable)\n", app);
+		return -1;
+	}
+
+	argcopy = ast_strdupa(data);
+
+	AST_STANDARD_APP_ARGS(arglist, argcopy);
+
+	if (!ast_strlen_zero(arglist.options)) {
+		ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
+	}
+
+	if (ast_strlen_zero(arglist.expression)) {
+		ast_log(LOG_WARNING, "%s requires an argument (variable)\n", app);
+		return -1;
+	}
+
+	value = atoi(arglist.expression); /* already parsed, so it should already be an integer anyways */
+
+	if (!value) {
+		AST_DECLARE_APP_ARGS(extendata,
+			AST_APP_ARG(expr);
+			AST_APP_ARG(rest);
+		);
+		const char *exprparse = NULL;
+		char *datacopy = NULL;
+		char *context, *exten;
+		int priority;
+
+		/* so, what was the expression? Let's find out */
+		struct ast_exten *e;
+		struct pbx_find_info q = { .stacklen = 0 }; /* the rest is set in pbx_find_context */
+
+		ast_channel_lock(chan);
+		context = ast_strdupa(ast_channel_context(chan));
+		exten = ast_strdupa(ast_channel_exten(chan));
+		priority = ast_channel_priority(chan);
+		ast_channel_unlock(chan);
+
+		ast_rdlock_contexts();
+		e = pbx_find_extension(chan, NULL, &q, context, exten, priority, NULL, "", E_MATCH);
+		ast_unlock_contexts();
+		if (!e) {
+			ast_log(LOG_WARNING, "Couldn't find current execution location\n"); /* should never happen */
+			return -1;
+		}
+		exprparse = ast_get_extension_app_data(e);
+		datacopy = ast_strdupa(exprparse);
+		AST_STANDARD_APP_ARGS(extendata, datacopy);
+
+		ast_log(LOG_WARNING, "Assertion failed: %s\n", extendata.expr);
+
+		if (!ast_test_flag(&flags, OPT_NOHANGUP)) {
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
+static int unload_module(void)
+{
+	return ast_unregister_application(app);
+}
+
+static int load_module(void)
+{
+	return ast_register_application_xml(app, assert_exec);
+}
+
+AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Dialplan assertion test application");
diff --git a/doc/CHANGES-staging/app_assert.txt b/doc/CHANGES-staging/app_assert.txt
new file mode 100644
index 0000000..6703b04
--- /dev/null
+++ b/doc/CHANGES-staging/app_assert.txt
@@ -0,0 +1,5 @@
+Subject: app_assert
+
+Adds an Assert application that can be used to verify
+and enforce constraints or conditions in the dialplan,
+similar to the assert() function in C.

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16629
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Ia089c2debf608f42f5f87e6c29d50e8ebcc093e5
Gerrit-Change-Number: 16629
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20211024/804c618c/attachment-0001.html>


More information about the asterisk-code-review mailing list