From: Maros Zatko <mzatko@redhat.com> --- fish/test/Makefile.am | 39 ++++++++++++++++ fish/test/testquoting.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 fish/test/Makefile.am create mode 100644 fish/test/testquoting.c diff --git a/fish/test/Makefile.am b/fish/test/Makefile.am new file mode 100644 index 0000000..d108083 --- /dev/null +++ b/fish/test/Makefile.am @@ -0,0 +1,39 @@ +# libguestfs +# Copyright (C) 2009-2014 Red Hat Inc. +# +# This program 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 2 of the License, or +# (at your option) any later version. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +include $(top_srcdir)/subdir-rules.mk + +check_PROGRAMS = testquoting + +testquoting_SOURCES = \ + testquoting.c \ + $(top_srcdir)/fish/rl.c + +testquoting_CPPFLAGS = \ + -I$(top_srcdir)/src -I$(top_builddir)/src \ + -I$(top_srcdir)/fish -I$(top_builddir)/fish \ + -I$(top_srcdir)/gnulib/lib -I$(top_builddir)/gnulib/lib + +#testquoting_LDADD = $(top_builddir)/fish/rl.o + + +TESTS_ENVIRONMENT = $(top_builddir)/run --test + +TESTS = \ + testquoting + +# testquoting_CFLAGS = -I$(top_srcdir) diff --git a/fish/test/testquoting.c b/fish/test/testquoting.c new file mode 100644 index 0000000..b0b525b --- /dev/null +++ b/fish/test/testquoting.c @@ -0,0 +1,120 @@ +#include <config.h> + +#include <stdlib.h> +#include <stdio.h> +#include <memory.h> + +#include <guestfs.h> +#include <guestfs-internal-frontend.h> +#include <rl.h> + +int +eq_bracket (char *(*fn)(char*), char * in, char * out) +{ + char * q = fn(in); + return (q != NULL) && STREQ(q, out); +} + +int +test_empty_escape (void) +{ + return eq_bracket(bs_escape_filename, "", ""); +} + +int +test_empty_unescape (void) +{ + return eq_bracket(bs_unescape_filename, "", ""); +} + +int +test_singlespace_escape (void) +{ + return eq_bracket(bs_escape_filename, " ", "\\ "); +} + +int +test_singlespace_unescape (void) +{ + return eq_bracket(bs_unescape_filename, "\\ ", " "); +} + +int +test_singleword_escape (void) +{ + return eq_bracket(bs_escape_filename, "singleword", "singleword"); +} + +int +test_singleword_unescape (void) +{ + return eq_bracket(bs_unescape_filename, "singleword", "singleword"); +} + +int +test_multiword_escape (void) +{ + return eq_bracket(bs_escape_filename, "more than one word\n", "more\\ than\\ one\\ word\\n"); +} + +int +test_nonprinting_escape (void) +{ + return eq_bracket(bs_escape_filename, "\xac\xec\x8", "\\xac\\xec\\b"); +} + +int +test_multiword_unescape (void) +{ + return eq_bracket(bs_unescape_filename, "more\\ than\\ one\\ word", "more than one word"); +} + +int +test_nonprinting_unescape (void) +{ + return eq_bracket(bs_unescape_filename, "\\xac\\xec\\b", "\xac\xec\b"); +} + + + +struct test_t { + char *name; + int (*fn)(void); + int expect; +}; + +struct test_t tests[] = { + { .name = "test empty escape", .fn = test_empty_escape, .expect = 1}, + { .name = "test empty unescape", .fn = test_empty_unescape, .expect = 1}, + { .name = "test single space escape", .fn = test_singlespace_escape, .expect = 1}, + { .name = "test single space unescape", .fn = test_singlespace_unescape, .expect = 1}, + { .name = "test single word escape", .fn = test_singleword_escape, .expect = 1}, + { .name = "test single word unescape", .fn = test_singleword_unescape, .expect = 1}, + { .name = "test multi word escape", .fn = test_multiword_escape, .expect = 1}, + { .name = "test nonprinting escape", .fn = test_nonprinting_escape, .expect = 1}, + { .name = "test multi word unescape", .fn = test_multiword_unescape, .expect = 1}, + { .name = "test nonprinting unescape", .fn = test_nonprinting_unescape, .expect = 1}, +}; + +size_t nr_tests = sizeof(tests) / sizeof(*tests); + +int +main (int argc, char *argv[]) +{ + int nr_failed = 0; // failed test count + setbuf(stdout, NULL); + + for (int i = 0; i < nr_tests; i++) { + fprintf(stdout, "%s: ", tests[i].name); + int r = tests[i].fn() == tests[i].expect; + fprintf(stdout, "%s\n", r ? "PASS" : "FAIL"); + nr_failed += !r; + } + + if (nr_failed > 0) { + printf ("***** %zu / %zu tests FAILED *****\n", nr_failed, nr_tests); + } + + return nr_failed > 0 ? EXIT_FAILURE : EXIT_SUCCESS; +} + -- 1.9.3