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) ...@@ -229,8 +229,8 @@ exp_runcmd (char **ret, const char *str, size_t len, char **argv, void *closure)
229 229
230 if (strcmp (argv[0], SHELL_CMD) == 0) 230 if (strcmp (argv[0], SHELL_CMD) == 0)
231 { 231 {
232 len -= sizeof SHELL_CMD; 232 len -= sizeof SHELL_CMD - 1;
233 str += sizeof SHELL_CMD; 233 str += sizeof SHELL_CMD - 1;
234 while (len > 0 && mu_isspace (*str)) 234 while (len > 0 && mu_isspace (*str))
235 { 235 {
236 len--; 236 len--;
...@@ -282,8 +282,7 @@ int ...@@ -282,8 +282,7 @@ int
282 mu_str_expand (char **output, char const *input, mu_assoc_t assoc) 282 mu_str_expand (char **output, char const *input, mu_assoc_t assoc)
283 { 283 {
284 struct mu_wordsplit ws; 284 struct mu_wordsplit ws;
285 size_t wordc; 285 int rc = 0;
286 char **wordv;
287 286
288 ws.ws_getvar = exp_getvar; 287 ws.ws_getvar = exp_getvar;
289 ws.ws_command = exp_runcmd; 288 ws.ws_command = exp_runcmd;
...@@ -294,15 +293,36 @@ mu_str_expand (char **output, char const *input, mu_assoc_t assoc) ...@@ -294,15 +293,36 @@ mu_str_expand (char **output, char const *input, mu_assoc_t assoc)
294 MU_WRDSF_NOSPLIT | MU_WRDSF_GETVAR | MU_WRDSF_CLOSURE 293 MU_WRDSF_NOSPLIT | MU_WRDSF_GETVAR | MU_WRDSF_CLOSURE
295 | MU_WRDSF_OPTIONS)) 294 | MU_WRDSF_OPTIONS))
296 { 295 {
296 if (ws.ws_errno == MU_WRDSE_NOSPACE)
297 rc = ENOMEM;
298 else
299 {
297 char *p = strdup (mu_wordsplit_strerror (&ws)); 300 char *p = strdup (mu_wordsplit_strerror (&ws));
298 if (p) 301 if (!p)
302 rc = ENOMEM;
303 else
304 {
299 *output = p; 305 *output = p;
300 return MU_ERR_FAILURE; 306 rc = MU_ERR_FAILURE;
307 }
301 } 308 }
309 }
310 else if (ws.ws_wordc == 0)
311 {
312 *output = strdup ("");
313 if (!*output)
314 rc = ENOMEM;
315 }
316 else
317 {
318 size_t wordc;
319 char **wordv;
320
302 mu_wordsplit_get_words (&ws, &wordc, &wordv); 321 mu_wordsplit_get_words (&ws, &wordc, &wordv);
303 *output = wordv[0]; 322 *output = wordv[0];
323 }
304 mu_wordsplit_free (&ws); 324 mu_wordsplit_free (&ws);
305 return 0; 325 return rc;
306 } 326 }
307 327
308 int 328 int
......
...@@ -11,6 +11,7 @@ argcv ...@@ -11,6 +11,7 @@ argcv
11 debugspec 11 debugspec
12 decode2047 12 decode2047
13 encode2047 13 encode2047
14 exp
14 fltst 15 fltst
15 fsaf 16 fsaf
16 fsaftomod 17 fsaftomod
...@@ -34,6 +35,7 @@ tcli ...@@ -34,6 +35,7 @@ tcli
34 tempfile 35 tempfile
35 url-comp 36 url-comp
36 url-parse 37 url-parse
38 vexp
37 wicket 39 wicket
38 wordwrap 40 wordwrap
39 wsp 41 wsp
......
...@@ -70,6 +70,7 @@ noinst_PROGRAMS = \ ...@@ -70,6 +70,7 @@ noinst_PROGRAMS = \
70 tcli\ 70 tcli\
71 url-comp\ 71 url-comp\
72 url-parse\ 72 url-parse\
73 vexp\
73 wicket\ 74 wicket\
74 wordwrap\ 75 wordwrap\
75 wsp 76 wsp
...@@ -89,6 +90,7 @@ TESTSUITE_AT = \ ...@@ -89,6 +90,7 @@ TESTSUITE_AT = \
89 debugspec.at\ 90 debugspec.at\
90 decode2047.at\ 91 decode2047.at\
91 encode2047.at\ 92 encode2047.at\
93 exp.at\
92 fromflt.at\ 94 fromflt.at\
93 fsaf.at\ 95 fsaf.at\
94 fsaftomod.at\ 96 fsaftomod.at\
......
1 # This file is part of GNU Mailutils. -*- Autotest -*-
2 # Copyright (C) 2007-2012, 2014-2017 Free Software Foundation, Inc.
3 #
4 # GNU Mailutils is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3, or (at
7 # your option) any later version.
8 #
9 # GNU Mailutils is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
16
17 AT_BANNER(Variable and command expansion)
18
19 dnl ------------------------------------------------------------
20 dnl TESTEXP([NAME], [KW = `'], [INPUT], [STDOUT = `'],
21 dnl [STDERR = `'])
22 dnl
23 m4_pushdef([TESTEXP],[
24 m4_pushdef([MU_TEST_GROUP],[Expand])
25 m4_pushdef([MU_TEST_KEYWORDS],[expand])
26 MU_GENERIC_TEST_CMD([$1],[$2],[$3],[],[$4
27 ],[$5])
28 m4_popdef([MU_TEST_KEYWORDS])
29 m4_popdef([MU_TEST_GROUP])
30 ])
31
32 m4_pushdef([MU_TEST_COMMAND],[exp])
33 TESTEXP([variable expansion],[],[x=foo y=bar -- 'test $x $y $z end'],
34 [test foo bar end])
35
36 TESTEXP([domainpart],[],[-- '$(domainpart "foo@example.com")'],
37 [example.com])
38
39 TESTEXP([localpart],[],[-- '$(localpart "foo@example.com")'],
40 [foo])
41
42 TESTEXP([shell],[],[-- '$(shell echo "test string" | tr " " -)'],
43 [test-string])
44
45 m4_popdef([MU_TEST_COMMAND])
46
47 m4_pushdef([MU_TEST_COMMAND],[vexp])
48 TESTEXP([mu_str_vexpand],[],['certain $user on $host running $package'],
49 [certain somebody on localhost running mailutils])
50
51 m4_popdef([MU_TEST_COMMAND])
52 m4_popdef([TESTEXP])
...\ No newline at end of file ...\ No newline at end of file
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2017 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
1 #include <mailutils/mailutils.h> 17 #include <mailutils/mailutils.h>
2 18
3 int 19 int
......
...@@ -51,6 +51,28 @@ MU_TEST_COMMAND < input],[$4],[$5],[$6],[$7],[$8]) ...@@ -51,6 +51,28 @@ MU_TEST_COMMAND < input],[$4],[$5],[$6],[$7],[$8])
51 AT_CLEANUP 51 AT_CLEANUP
52 ]) 52 ])
53 53
54 dnl ------------------------------------------------------------
55 dnl MU_GENERIC_TEST_CMD([NAME], [KW = `'], [ARGS],
56 dnl [CODE], [STDOUT = `'], [STDERR = `'],
57 dnl [RUN-IF-FAIL], [RUN-IF-PASS])
58 dnl
59 dnl NAME $1
60 dnl KW $2
61 dnl ARGS $3
62 dnl CODE $4
63 dnl STDOUT $5
64 dnl STDERR $6
65 dnl RUN-IF-FAIL $7
66 dnl RUN-IF-PASS $8
67 dnl
68 m4_define([MU_GENERIC_TEST_CMD],[
69 AT_SETUP([m4_if(MU_TEST_GROUP,[],,MU_TEST_GROUP: )m4_if([$1],[],[mu_firstline([$3])],[$1])])
70 AT_KEYWORDS([MU_TEST_KEYWORDS $2])
71 AT_CHECK([
72 MU_TEST_COMMAND $3],[$4],[$5],[$6],[$7],[$8])
73 AT_CLEANUP
74 ])
75
54 AT_INIT 76 AT_INIT
55 77
56 AT_BANNER([Conversions]) 78 AT_BANNER([Conversions])
...@@ -131,6 +153,7 @@ m4_include([strerr.at]) ...@@ -131,6 +153,7 @@ m4_include([strerr.at])
131 m4_include([list.at]) 153 m4_include([list.at])
132 m4_include([address.at]) 154 m4_include([address.at])
133 m4_include([wordsplit.at]) 155 m4_include([wordsplit.at])
156 m4_include([exp.at])
134 m4_include([url.at]) 157 m4_include([url.at])
135 m4_include([url-comp.at]) 158 m4_include([url-comp.at])
136 m4_include([mailcap.at]) 159 m4_include([mailcap.at])
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2017 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <mailutils/mailutils.h>
19 #include <assert.h>
20
21 int
22 main (int argc, char **argv)
23 {
24 int rc;
25 char *p;
26
27 assert (argc == 2);
28
29 rc = mu_str_vexpand (&p, argv[1],
30 "user", "somebody",
31 "host", "localhost",
32 "name", "tests",
33 "package", PACKAGE,
34 NULL);
35 switch (rc)
36 {
37 case 0:
38 printf ("%s\n", p);
39 free (p);
40 break;
41
42 case MU_ERR_FAILURE:
43 mu_error ("%s", p);
44 free (p);
45 break;
46
47 default:
48 mu_error ("%s", mu_strerror (rc));
49 return 1;
50 }
51 return 0;
52 }