summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2017-01-19 13:11:27 +0200
committerSergey Poznyakoff <gray@gnu.org>2017-01-19 13:16:18 +0200
commitaf06135f2e383ed76107e4e4aa8c866e515c40e4 (patch)
treedd254b966691708dd5801b02b860475fce8119c3
parent9ba835fbb5a937735f51925c2170bbc748820fcf (diff)
downloadmailutils-af06135f2e383ed76107e4e4aa8c866e515c40e4.tar.gz
mailutils-af06135f2e383ed76107e4e4aa8c866e515c40e4.tar.bz2
Add tests for mu_str_expand and mu_str_vexpand functions
* libmailutils/string/expvar.c (exp_shell): Fix out-of-buffer read when used without arguments. (mu_str_expand): Improve error handling. * libmailutils/tests/testsuite.at (MU_GENERIC_TEST_CMD): New macro. Include exp.at * libmailutils/tests/vexp.c: New file. * libmailutils/tests/Makefile.am: Build vexp. Add exp.at * libmailutils/tests/exp.at: New file.
-rw-r--r--libmailutils/string/expvar.c44
-rw-r--r--libmailutils/tests/.gitignore2
-rw-r--r--libmailutils/tests/Makefile.am2
-rw-r--r--libmailutils/tests/exp.at52
-rw-r--r--libmailutils/tests/exp.c16
-rw-r--r--libmailutils/tests/testsuite.at23
-rw-r--r--libmailutils/tests/vexp.c52
7 files changed, 179 insertions, 12 deletions
diff --git a/libmailutils/string/expvar.c b/libmailutils/string/expvar.c
index a9361ef85..04e45421a 100644
--- a/libmailutils/string/expvar.c
+++ b/libmailutils/string/expvar.c
@@ -173,7 +173,7 @@ exp_shell (char **ret, char const *str, size_t len, void *closure)
--buflen;
buffer[buflen] = 0;
}
-
+
pclose (fp);
free (cmd);
@@ -229,8 +229,8 @@ exp_runcmd (char **ret, const char *str, size_t len, char **argv, void *closure)
if (strcmp (argv[0], SHELL_CMD) == 0)
{
- len -= sizeof SHELL_CMD;
- str += sizeof SHELL_CMD;
+ len -= sizeof SHELL_CMD - 1;
+ str += sizeof SHELL_CMD - 1;
while (len > 0 && mu_isspace (*str))
{
len--;
@@ -282,8 +282,7 @@ int
mu_str_expand (char **output, char const *input, mu_assoc_t assoc)
{
struct mu_wordsplit ws;
- size_t wordc;
- char **wordv;
+ int rc = 0;
ws.ws_getvar = exp_getvar;
ws.ws_command = exp_runcmd;
@@ -294,15 +293,36 @@ mu_str_expand (char **output, char const *input, mu_assoc_t assoc)
MU_WRDSF_NOSPLIT | MU_WRDSF_GETVAR | MU_WRDSF_CLOSURE
| MU_WRDSF_OPTIONS))
{
- char *p = strdup (mu_wordsplit_strerror (&ws));
- if (p)
- *output = p;
- return MU_ERR_FAILURE;
+ if (ws.ws_errno == MU_WRDSE_NOSPACE)
+ rc = ENOMEM;
+ else
+ {
+ char *p = strdup (mu_wordsplit_strerror (&ws));
+ if (!p)
+ rc = ENOMEM;
+ else
+ {
+ *output = p;
+ rc = MU_ERR_FAILURE;
+ }
+ }
+ }
+ else if (ws.ws_wordc == 0)
+ {
+ *output = strdup ("");
+ if (!*output)
+ rc = ENOMEM;
+ }
+ else
+ {
+ size_t wordc;
+ char **wordv;
+
+ mu_wordsplit_get_words (&ws, &wordc, &wordv);
+ *output = wordv[0];
}
- mu_wordsplit_get_words (&ws, &wordc, &wordv);
- *output = wordv[0];
mu_wordsplit_free (&ws);
- return 0;
+ return rc;
}
int
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index 10ebd32f5..45351de18 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -11,6 +11,7 @@ argcv
debugspec
decode2047
encode2047
+exp
fltst
fsaf
fsaftomod
@@ -34,6 +35,7 @@ tcli
tempfile
url-comp
url-parse
+vexp
wicket
wordwrap
wsp
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index b31b5ac79..4c6b2f3e5 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -70,6 +70,7 @@ noinst_PROGRAMS = \
tcli\
url-comp\
url-parse\
+ vexp\
wicket\
wordwrap\
wsp
@@ -89,6 +90,7 @@ TESTSUITE_AT = \
debugspec.at\
decode2047.at\
encode2047.at\
+ exp.at\
fromflt.at\
fsaf.at\
fsaftomod.at\
diff --git a/libmailutils/tests/exp.at b/libmailutils/tests/exp.at
new file mode 100644
index 000000000..d97c252f1
--- /dev/null
+++ b/libmailutils/tests/exp.at
@@ -0,0 +1,52 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2007-2012, 2014-2017 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
+
+AT_BANNER(Variable and command expansion)
+
+dnl ------------------------------------------------------------
+dnl TESTEXP([NAME], [KW = `'], [INPUT], [STDOUT = `'],
+dnl [STDERR = `'])
+dnl
+m4_pushdef([TESTEXP],[
+m4_pushdef([MU_TEST_GROUP],[Expand])
+m4_pushdef([MU_TEST_KEYWORDS],[expand])
+MU_GENERIC_TEST_CMD([$1],[$2],[$3],[],[$4
+],[$5])
+m4_popdef([MU_TEST_KEYWORDS])
+m4_popdef([MU_TEST_GROUP])
+])
+
+m4_pushdef([MU_TEST_COMMAND],[exp])
+TESTEXP([variable expansion],[],[x=foo y=bar -- 'test $x $y $z end'],
+[test foo bar end])
+
+TESTEXP([domainpart],[],[-- '$(domainpart "foo@example.com")'],
+[example.com])
+
+TESTEXP([localpart],[],[-- '$(localpart "foo@example.com")'],
+[foo])
+
+TESTEXP([shell],[],[-- '$(shell echo "test string" | tr " " -)'],
+[test-string])
+
+m4_popdef([MU_TEST_COMMAND])
+
+m4_pushdef([MU_TEST_COMMAND],[vexp])
+TESTEXP([mu_str_vexpand],[],['certain $user on $host running $package'],
+[certain somebody on localhost running mailutils])
+
+m4_popdef([MU_TEST_COMMAND])
+m4_popdef([TESTEXP]) \ No newline at end of file
diff --git a/libmailutils/tests/exp.c b/libmailutils/tests/exp.c
index 35a52d00b..f126f8820 100644
--- a/libmailutils/tests/exp.c
+++ b/libmailutils/tests/exp.c
@@ -1,3 +1,19 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+ Copyright (C) 2017 Free Software Foundation, Inc.
+
+ GNU Mailutils is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Mailutils is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
+
#include <mailutils/mailutils.h>
int
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index 0c6204071..11e12c808 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -51,6 +51,28 @@ MU_TEST_COMMAND < input],[$4],[$5],[$6],[$7],[$8])
AT_CLEANUP
])
+dnl ------------------------------------------------------------
+dnl MU_GENERIC_TEST_CMD([NAME], [KW = `'], [ARGS],
+dnl [CODE], [STDOUT = `'], [STDERR = `'],
+dnl [RUN-IF-FAIL], [RUN-IF-PASS])
+dnl
+dnl NAME $1
+dnl KW $2
+dnl ARGS $3
+dnl CODE $4
+dnl STDOUT $5
+dnl STDERR $6
+dnl RUN-IF-FAIL $7
+dnl RUN-IF-PASS $8
+dnl
+m4_define([MU_GENERIC_TEST_CMD],[
+AT_SETUP([m4_if(MU_TEST_GROUP,[],,MU_TEST_GROUP: )m4_if([$1],[],[mu_firstline([$3])],[$1])])
+AT_KEYWORDS([MU_TEST_KEYWORDS $2])
+AT_CHECK([
+MU_TEST_COMMAND $3],[$4],[$5],[$6],[$7],[$8])
+AT_CLEANUP
+])
+
AT_INIT
AT_BANNER([Conversions])
@@ -131,6 +153,7 @@ m4_include([strerr.at])
m4_include([list.at])
m4_include([address.at])
m4_include([wordsplit.at])
+m4_include([exp.at])
m4_include([url.at])
m4_include([url-comp.at])
m4_include([mailcap.at])
diff --git a/libmailutils/tests/vexp.c b/libmailutils/tests/vexp.c
new file mode 100644
index 000000000..7c381743d
--- /dev/null
+++ b/libmailutils/tests/vexp.c
@@ -0,0 +1,52 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+ Copyright (C) 2017 Free Software Foundation, Inc.
+
+ GNU Mailutils is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Mailutils is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <mailutils/mailutils.h>
+#include <assert.h>
+
+int
+main (int argc, char **argv)
+{
+ int rc;
+ char *p;
+
+ assert (argc == 2);
+
+ rc = mu_str_vexpand (&p, argv[1],
+ "user", "somebody",
+ "host", "localhost",
+ "name", "tests",
+ "package", PACKAGE,
+ NULL);
+ switch (rc)
+ {
+ case 0:
+ printf ("%s\n", p);
+ free (p);
+ break;
+
+ case MU_ERR_FAILURE:
+ mu_error ("%s", p);
+ free (p);
+ break;
+
+ default:
+ mu_error ("%s", mu_strerror (rc));
+ return 1;
+ }
+ return 0;
+}

Return to:

Send suggestions and report system problems to the System administrator.