Commit af06135f af06135f2e383ed76107e4e4aa8c866e515c40e4 by Sergey Poznyakoff

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.
1 parent 9ba835fb
......@@ -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))
{
if (ws.ws_errno == MU_WRDSE_NOSPACE)
rc = ENOMEM;
else
{
char *p = strdup (mu_wordsplit_strerror (&ws));
if (p)
if (!p)
rc = ENOMEM;
else
{
*output = p;
return MU_ERR_FAILURE;
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_free (&ws);
return 0;
return rc;
}
int
......
......@@ -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
......
......@@ -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\
......
# 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
/* 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
......
......@@ -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])
......
/* 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;
}