From 7cc1a446e717362f029e44e5b06ca9cac89552be Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 26 Apr 2014 18:53:15 +0100 Subject: [PATCH] tests: Add a test of running 'ls --version' and matching the output. --- .gitignore | 1 + Makefile.am | 8 ++++- test-ls-version.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests.h | 16 +++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 test-ls-version.c diff --git a/.gitignore b/.gitignore index e8814ae..959fb48 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,5 @@ /missing /stamp-h1 /test-driver +/test-ls-version /test-spawn diff --git a/Makefile.am b/Makefile.am index 1f649ac..b2ac4b3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -39,12 +39,18 @@ example_sshpass_LDADD = libminiexpect.la # Tests. TESTS = $(check_PROGRAMS) -check_PROGRAMS = test-spawn +check_PROGRAMS = \ + test-spawn \ + test-ls-version test_spawn_SOURCES = test-spawn.c tests.h miniexpect.h test_spawn_CFLAGS = $(PCRE_CFLAGS) -Wall test_spawn_LDADD = libminiexpect.la +test_ls_version_SOURCES = test-ls-version.c tests.h miniexpect.h +test_ls_version_CFLAGS = $(PCRE_CFLAGS) -Wall +test_ls_version_LDADD = libminiexpect.la + # Clean. CLEANFILES = *~ diff --git a/test-ls-version.c b/test-ls-version.c new file mode 100644 index 0000000..3b7bbc6 --- /dev/null +++ b/test-ls-version.c @@ -0,0 +1,100 @@ +/* miniexpect test suite + * Copyright (C) 2014 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* Note this test depends on the output of 'ls --version'. You may + * have to extend the regular expressions below if running on a + * non-Linux OS. + */ + +#include + +#include +#include +#include +#include + +#include + +#include "miniexpect.h" +#include "tests.h" + +int +main (int argc, char *argv[]) +{ + mexp_h *h; + int status, r; + pcre *ls_coreutils_re; + pcre *ls_busybox_re; + const int ovecsize = 12; + int ovector[ovecsize]; + const char *version; + + ls_coreutils_re = test_compile_re ("^ls.* ([.\\d]+)"); + /* Busybox doesn't actually recognize the --version option, but + * it prints out the version string in its error message. + */ + ls_busybox_re = test_compile_re ("^BusyBox v([.\\d+]) "); + + h = mexp_spawnl ("ls", "ls", "--version", NULL); + assert (h != NULL); + + switch (mexp_expect (h, + (mexp_regexp[]) { + { 100, ls_coreutils_re }, + { 101, ls_busybox_re }, + { 0 }, + }, ovector, ovecsize)) { + case 100: + case 101: + /* Get the matched version number. */ + r = pcre_get_substring (h->buffer, ovector, h->pcre_error, 1, &version); + if (r < 0) { + fprintf (stderr, "error: PCRE error reading version substring: %d\n", + r); + exit (EXIT_FAILURE); + } + printf ("ls version = %s\n", version); + pcre_free_substring (version); + break; + case MEXP_EOF: + fprintf (stderr, "error: EOF before matching version string\n"); + exit (EXIT_FAILURE); + case MEXP_TIMEOUT: + fprintf (stderr, "error: timeout before matching version string\n"); + exit (EXIT_FAILURE); + case MEXP_ERROR: + perror ("mexp_expect"); + exit (EXIT_FAILURE); + case MEXP_PCRE_ERROR: + fprintf (stderr, "error: PCRE error: %d\n", h->pcre_error); + exit (EXIT_FAILURE); + } + + status = mexp_close (h); + if (status != 0 && !test_is_sighup (status)) { + fprintf (stderr, "%s: non-zero exit status from subcommand: ", argv[0]); + test_diagnose (status); + fprintf (stderr, "\n"); + exit (EXIT_FAILURE); + } + + pcre_free (ls_coreutils_re); + pcre_free (ls_busybox_re); + + exit (EXIT_SUCCESS); +} diff --git a/tests.h b/tests.h index e258782..e9b19f3 100644 --- a/tests.h +++ b/tests.h @@ -39,4 +39,20 @@ test_diagnose (int status) fprintf (stderr, "stopped by signal %d", WSTOPSIG (status)); } +static pcre * +test_compile_re (const char *rex) +{ + const char *errptr; + int erroffset; + pcre *ret; + + ret = pcre_compile (rex, 0, &errptr, &erroffset, NULL); + if (ret == NULL) { + fprintf (stderr, "error: failed to compile regular expression '%s': %s at offset %d\n", + rex, errptr, erroffset); + exit (EXIT_FAILURE); + } + return ret; +} + #endif /* TESTS_H_ */ -- 1.8.3.1