[Asterisk-code-review] test: Add test coverage for capture child process output (asterisk[16])

Friendly Automation asteriskteam at digium.com
Mon Sep 12 12:15:25 CDT 2022


Friendly Automation has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/19105 )

Change subject: test: Add test coverage for capture child process output
......................................................................

test: Add test coverage for capture child process output

ASTERISK-30037 #close

Change-Id: I0273e85eeeb6b8e46703f24cd74d84f3daf0a69a
---
M configure
M configure.ac
M makeopts.in
M tests/Makefile
A tests/test_capture.c
5 files changed, 417 insertions(+), 0 deletions(-)

Approvals:
  George Joseph: Looks good to me, approved
  Friendly Automation: Approved for Submit




diff --git a/configure b/configure
index a2f59e01..9093130 100755
--- a/configure
+++ b/configure
@@ -714,6 +714,7 @@
 AST_NATIVE_ARCH
 AST_SHADOW_WARNINGS
 AST_NO_STRINGOP_TRUNCATION
+AST_NO_FORMAT_Y2K
 AST_NO_FORMAT_TRUNCATION
 AST_NO_STRICT_OVERFLOW
 AST_FORTIFY_SOURCE
@@ -21240,6 +21241,19 @@
 fi
 
 
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for -Wno-format-y2k" >&5
+printf %s "checking for -Wno-format-y2k... " >&6; }
+if $(${CC} -Wno-format-y2k -Werror -S -o /dev/null -xc /dev/null > /dev/null 2>&1); then
+	{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+printf "%s\n" "yes" >&6; }
+	AST_NO_FORMAT_Y2K=-Wno-format-y2k
+else
+	{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+	AST_NO_FORMAT_Y2K=
+fi
+
+
 { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for -Wno-stringop-truncation" >&5
 printf %s "checking for -Wno-stringop-truncation... " >&6; }
 if $(${CC} -Wno-stringop-truncation -Werror -S -o /dev/null -xc /dev/null > /dev/null 2>&1); then
diff --git a/configure.ac b/configure.ac
index a1a5fcb..03cb493 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1403,6 +1403,16 @@
 fi
 AC_SUBST(AST_NO_FORMAT_TRUNCATION)
 
+AC_MSG_CHECKING(for -Wno-format-y2k)
+if $(${CC} -Wno-format-y2k -Werror -S -o /dev/null -xc /dev/null > /dev/null 2>&1); then
+	AC_MSG_RESULT(yes)
+	AST_NO_FORMAT_Y2K=-Wno-format-y2k
+else
+	AC_MSG_RESULT(no)
+	AST_NO_FORMAT_Y2K=
+fi
+AC_SUBST(AST_NO_FORMAT_Y2K)
+
 AC_MSG_CHECKING(for -Wno-stringop-truncation)
 if $(${CC} -Wno-stringop-truncation -Werror -S -o /dev/null -xc /dev/null > /dev/null 2>&1); then
 	AC_MSG_RESULT(yes)
diff --git a/makeopts.in b/makeopts.in
index 35b6353..e4b8711 100644
--- a/makeopts.in
+++ b/makeopts.in
@@ -120,6 +120,7 @@
 AST_TRAMPOLINES=@AST_TRAMPOLINES@
 AST_NO_STRICT_OVERFLOW=@AST_NO_STRICT_OVERFLOW@
 AST_NO_FORMAT_TRUNCATION=@AST_NO_FORMAT_TRUNCATION@
+AST_NO_FORMAT_Y2K=@AST_NO_FORMAT_Y2K@
 AST_NO_STRINGOP_TRUNCATION=@AST_NO_STRINGOP_TRUNCATION@
 AST_SHADOW_WARNINGS=@AST_SHADOW_WARNINGS@
 AST_NESTED_FUNCTIONS=@AST_NESTED_FUNCTIONS@
diff --git a/tests/Makefile b/tests/Makefile
index 715c3f8..a3fc9dc 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -20,5 +20,7 @@
 include $(ASTTOPDIR)/Makefile.moddir_rules
 
 test_astobj2.o: _ASTCFLAGS+=$(call get_menuselect_cflags,AO2_DEBUG)
+# can't use '%y' in strftime() without warnings since it's not y2k compliant
+test_capture.o: _ASTCFLAGS+=$(AST_NO_FORMAT_Y2K)
 test_strings.o: _ASTCFLAGS+=$(AST_NO_FORMAT_TRUNCATION) $(AST_NO_STRINGOP_TRUNCATION)
 test_voicemail_api.o: _ASTCFLAGS+=$(AST_NO_FORMAT_TRUNCATION)
diff --git a/tests/test_capture.c b/tests/test_capture.c
new file mode 100644
index 0000000..33809df
--- /dev/null
+++ b/tests/test_capture.c
@@ -0,0 +1,379 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2022, Philip Prindeville
+ *
+ * Philip Prindeville <philipp at redfish-solutions.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 Make basic use of capture capability in test framework.
+ *
+ * \author\verbatim Philip Prindeville <philipp at redfish-solutions.com> \endverbatim
+ *
+ * Exercise the capture capabilities built into the test framework so
+ * that external commands might be used to generate validating results
+ * used on corroborating tests.
+ * \ingroup tests
+ */
+
+/*** MODULEINFO
+	<depend>TEST_FRAMEWORK</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/utils.h"
+#include "asterisk/module.h"
+#include "asterisk/test.h"
+
+AST_TEST_DEFINE(test_capture_true)
+{
+	int status = AST_TEST_FAIL;
+	struct ast_test_capture cap;
+	const char *command = "true";
+	char *const args[] = { "true", NULL };
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "test_capture_true";
+		info->category = "/main/test_capture/";
+		info->summary = "capture true exit unit test";
+		info->description =
+			"Capture exit code from true command.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	ast_test_status_update(test, "Executing true exit test...\n");
+
+	if (!ast_check_command_in_path(command)) {
+		ast_test_status_update(test, "couldn't find %s\n", command);
+		return status;
+	}
+
+	if (ast_test_capture_command(&cap, command, args, NULL, 0) != 1) {
+		ast_test_status_update(test, "ast_test_capture_command() failed\n");
+		return status;
+	}
+
+	if (cap.outlen != 0) {
+		ast_test_status_update(test, "unexpected value for stdout\n");
+		goto cleanup;
+	}
+
+	if (cap.errlen != 0) {
+		ast_test_status_update(test, "unexpected value for stderr\n");
+		goto cleanup;
+	}
+
+	if (cap.pid == -1) {
+		ast_test_status_update(test, "invalid process id\n");
+		goto cleanup;
+	}
+
+	if (cap.exitcode != 0) {
+		ast_test_status_update(test, "child exited %d\n", cap.exitcode);
+		goto cleanup;
+	}
+
+	status = AST_TEST_PASS;
+
+cleanup:
+	ast_test_capture_free(&cap);
+
+	return status;
+}
+
+AST_TEST_DEFINE(test_capture_false)
+{
+	int status = AST_TEST_FAIL;
+	struct ast_test_capture cap;
+	const char *command = "false";
+	char *const args[] = { "false", NULL };
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "test_capture_false";
+		info->category = "/main/test_capture/";
+		info->summary = "capture false exit unit test";
+		info->description =
+			"Capture exit code from false command.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	ast_test_status_update(test, "Executing false exit test...\n");
+
+	if (!ast_check_command_in_path(command)) {
+		ast_test_status_update(test, "couldn't find %s\n", command);
+		return status;
+	}
+
+	if (ast_test_capture_command(&cap, command, args, NULL, 0) != 1) {
+		ast_test_status_update(test, "ast_test_capture_command() failed\n");
+		return status;
+	}
+
+	if (cap.outlen != 0) {
+		ast_test_status_update(test, "unexpected value for stdout\n");
+		goto cleanup;
+	}
+
+	if (cap.errlen != 0) {
+		ast_test_status_update(test, "unexpected value for stderr\n");
+		goto cleanup;
+	}
+
+	if (cap.pid == -1) {
+		ast_test_status_update(test, "invalid process id\n");
+		goto cleanup;
+	}
+
+	if (cap.exitcode != 1) {
+		ast_test_status_update(test, "child exited %d\n", cap.exitcode);
+		goto cleanup;
+	}
+
+	status = AST_TEST_PASS;
+
+cleanup:
+	ast_test_capture_free(&cap);
+
+	return status;
+}
+
+AST_TEST_DEFINE(test_capture_with_stdin)
+{
+	int status = AST_TEST_FAIL;
+	struct ast_test_capture cap;
+	const char *command = "base64";
+	char *const args[] = { "base64", NULL };
+	const char data[] = "Mary had a little lamb.";
+	const unsigned datalen = sizeof(data) - 1;
+	const char output[] = "TWFyeSBoYWQgYSBsaXR0bGUgbGFtYi4=\n";
+	const unsigned outputlen = sizeof(output) - 1;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "test_capture_with_stdin";
+		info->category = "/main/test_capture/";
+		info->summary = "capture with stdin unit test";
+		info->description =
+			"Capture output from stdin transformation command.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	ast_test_status_update(test, "Executing stdin test...\n");
+
+	if (!ast_check_command_in_path(command)) {
+		ast_test_status_update(test, "couldn't find %s\n", command);
+		return status;
+	}
+
+	if (ast_test_capture_command(&cap, command, args, data, datalen) != 1) {
+		ast_test_status_update(test, "ast_test_capture_command() failed\n");
+		return status;
+	}
+
+	if (cap.outlen != outputlen || memcmp(cap.outbuf, output, cap.outlen)) {
+		ast_test_status_update(test, "unexpected value for stdout\n");
+		goto cleanup;
+	}
+
+	if (cap.errlen != 0) {
+		ast_test_status_update(test, "unexpected value for stderr\n");
+		goto cleanup;
+	}
+
+	if (cap.pid == -1) {
+		ast_test_status_update(test, "invalid process id\n");
+		goto cleanup;
+	}
+
+	if (cap.exitcode != 0) {
+		ast_test_status_update(test, "child exited %d\n", cap.exitcode);
+		goto cleanup;
+	}
+
+	status = AST_TEST_PASS;
+
+cleanup:
+	ast_test_capture_free(&cap);
+
+	return status;
+}
+
+AST_TEST_DEFINE(test_capture_with_dynamic)
+{
+	int status = AST_TEST_FAIL;
+	struct ast_test_capture cap;
+	const char *command = "date";
+	char *args[] = { "date", "DATE", "FORMAT", NULL };
+	char date[40];
+	const char format[] = "+%a, %d %b %y %T %z";
+	const char format2[] = "%a, %d %b %y %T %z\n";
+	char myresult[64];
+	unsigned myresultlen;
+	time_t now;
+	struct tm *tm;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "test_capture_with_dynamic";
+		info->category = "/main/test_capture/";
+		info->summary = "capture with dynamic argument unit test";
+		info->description =
+			"Capture output from dynamic transformation command.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	ast_test_status_update(test, "Executing dynamic argument test...\n");
+
+	if (!ast_check_command_in_path(command)) {
+		ast_test_status_update(test, "couldn't find %s\n", command);
+		return status;
+	}
+
+	time(&now);
+	snprintf(date, sizeof(date), "--date=@%lu", now);
+
+	tm = localtime(&now);
+	strftime(myresult, sizeof(myresult), format2, tm);
+	myresultlen = strlen(myresult);
+
+	args[1] = date;
+	args[2] = (char *)format;
+
+	if (ast_test_capture_command(&cap, command, args, NULL, 0) != 1) {
+		ast_test_status_update(test, "ast_test_capture_command() failed\n");
+		return status;
+	}
+
+	if (cap.outlen != myresultlen || memcmp(cap.outbuf, myresult, cap.outlen)) {
+		ast_test_status_update(test, "unexpected value for stdout\n");
+		goto cleanup;
+	}
+
+	if (cap.errlen != 0) {
+		ast_test_status_update(test, "unexpected value for stderr\n");
+		goto cleanup;
+	}
+
+	if (cap.pid == -1) {
+		ast_test_status_update(test, "invalid process id\n");
+		goto cleanup;
+	}
+
+	if (cap.exitcode != 0) {
+		ast_test_status_update(test, "child exited %d\n", cap.exitcode);
+		goto cleanup;
+	}
+
+	status = AST_TEST_PASS;
+
+cleanup:
+	ast_test_capture_free(&cap);
+
+	return status;
+}
+
+AST_TEST_DEFINE(test_capture_stdout_stderr)
+{
+	int status = AST_TEST_FAIL;
+	struct ast_test_capture cap;
+	const char *command = "sh";
+	char *const args[] = { "sh", "-c", "echo -n 'foo' >&2 ; echo -n 'zzz' >&1 ; echo -n 'bar' >&2", NULL };
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "test_capture_stdout_stderr";
+		info->category = "/main/test_capture/";
+		info->summary = "capture stdout & stderr unit test";
+		info->description =
+			"Capture both stdout and stderr from shell.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	ast_test_status_update(test, "Executing stdout/stderr test...\n");
+
+	if (!ast_check_command_in_path(command)) {
+		ast_test_status_update(test, "couldn't find %s\n", command);
+		return status;
+	}
+
+	if (ast_test_capture_command(&cap, command, args, NULL, 0) != 1) {
+		ast_test_status_update(test, "ast_test_capture_command() failed\n");
+		return status;
+	}
+
+	if (cap.outlen != 3 || memcmp(cap.outbuf, "zzz", 3)) {
+		ast_test_status_update(test, "unexpected value for stdout\n");
+		goto cleanup;
+	}
+
+	if (cap.errlen != 6 || memcmp(cap.errbuf, "foobar", 6)) {
+		ast_test_status_update(test, "unexpected value for stderr\n");
+		goto cleanup;
+	}
+
+	if (cap.pid == -1) {
+		ast_test_status_update(test, "invalid process id\n");
+		goto cleanup;
+	}
+
+	if (cap.exitcode != 0) {
+		ast_test_status_update(test, "child exited %d\n", cap.exitcode);
+		goto cleanup;
+	}
+
+	status = AST_TEST_PASS;
+
+cleanup:
+	ast_test_capture_free(&cap);
+
+	return status;
+}
+
+static int unload_module(void)
+{
+	AST_TEST_UNREGISTER(test_capture_with_stdin);
+	AST_TEST_UNREGISTER(test_capture_with_dynamic);
+	AST_TEST_UNREGISTER(test_capture_stdout_stderr);
+	AST_TEST_UNREGISTER(test_capture_true);
+	AST_TEST_UNREGISTER(test_capture_false);
+	return 0;
+}
+
+static int load_module(void)
+{
+	AST_TEST_REGISTER(test_capture_with_stdin);
+	AST_TEST_REGISTER(test_capture_with_dynamic);
+	AST_TEST_REGISTER(test_capture_stdout_stderr);
+	AST_TEST_REGISTER(test_capture_true);
+	AST_TEST_REGISTER(test_capture_false);
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Capture support test");
+

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

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: I0273e85eeeb6b8e46703f24cd74d84f3daf0a69a
Gerrit-Change-Number: 19105
Gerrit-PatchSet: 2
Gerrit-Owner: Philip Prindeville <philipp at redfish-solutions.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220912/f4d3e7be/attachment-0001.html>


More information about the asterisk-code-review mailing list