From 376e24b2ee910af99f65aba472e9553dc509aa5d Mon Sep 17 00:00:00 2001 From: Simon Wilkinson Date: Sat, 23 Apr 2011 11:42:54 -0400 Subject: [PATCH 1/1] cmd: Add some tests to the test suite Add some tests for the command library to the integrated test suite in tests. These are far from complete, and are mainly there to ensure that we don't break any of this functionality when modifying the library. Change-Id: Ib6fbdca114c005c32c5ba8c41f9e350ca67e1fb8 Reviewed-on: http://gerrit.openafs.org/4538 Tested-by: BuildBot Reviewed-by: Derrick Brashear --- Makefile.in | 3 ++ configure.ac | 1 + tests/Makefile.in | 14 ++---- tests/TESTS | 1 + tests/cmd/.gitignore | 1 + tests/cmd/Makefile.in | 25 ++++++++++ tests/cmd/command-t.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 tests/cmd/.gitignore create mode 100644 tests/cmd/Makefile.in create mode 100644 tests/cmd/command-t.c diff --git a/Makefile.in b/Makefile.in index 2fa9b20..48fa738 100644 --- a/Makefile.in +++ b/Makefile.in @@ -953,6 +953,9 @@ distclean: clean tests/Makefile \ tests/rpctestlib/Makefile \ tests/tap/Makefile \ + tests/auth/Makefile \ + tests/cmd/Makefile \ + tests/util/Makefile \ src/helper-splint.sh if test -d doc/man-pages ; then \ rm -f doc/man-pages/Makefile doc/man-pages/install-man ; \ diff --git a/configure.ac b/configure.ac index e93d18f..0df22bc 100644 --- a/configure.ac +++ b/configure.ac @@ -244,6 +244,7 @@ src/xstat/Makefile \ src/helper-splint.sh \ tests/Makefile \ tests/auth/Makefile \ +tests/cmd/Makefile \ tests/rpctestlib/Makefile \ tests/tap/Makefile \ tests/util/Makefile, diff --git a/tests/Makefile.in b/tests/Makefile.in index 76057f6..5d5583b 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -9,24 +9,20 @@ include @TOP_OBJDIR@/src/config/Makefile.pthread MODULE_CFLAGS = -DSOURCE='"$(abs_top_srcdir)/tests"' \ -DBUILD='"$(abs_top_builddir)/tests"' +SUBDIRS = tap auth util cmd + all: runtests - cd tap && $(MAKE) $@ - cd auth && $(MAKE) $@ - cd util && $(MAKE) $@ + @for A in $(SUBDIRS); do cd $$A && $(MAKE) $@ && cd .. || exit 1; done runtests: runtests.o $(AFS_LDRULE) runtests.o check test tests: runtests - cd tap && $(MAKE) $@ - cd auth && $(MAKE) $@ - cd util && $(MAKE) $@ + @for A in $(SUBDIRS); do cd $$A && $(MAKE) $@ && cd .. || exit 1; done ./runtests $(abs_top_srcdir)/tests/TESTS install: clean distclean: - cd tap && $(MAKE) $@ - cd auth && $(MAKE) $@ - cd util && $(MAKE) $@ + @for A in $(SUBDIRS); do cd $$A && $(MAKE) $@ && cd .. || exit 1; done $(RM) -f *.o core runtests diff --git a/tests/TESTS b/tests/TESTS index 93a0787..4b17741 100644 --- a/tests/TESTS +++ b/tests/TESTS @@ -4,4 +4,5 @@ util/queues auth/keys auth/superuser auth/authcon +cmd/commands ptserver/pt_util diff --git a/tests/cmd/.gitignore b/tests/cmd/.gitignore new file mode 100644 index 0000000..58a83d4 --- /dev/null +++ b/tests/cmd/.gitignore @@ -0,0 +1 @@ +/command-t diff --git a/tests/cmd/Makefile.in b/tests/cmd/Makefile.in new file mode 100644 index 0000000..ff4beee --- /dev/null +++ b/tests/cmd/Makefile.in @@ -0,0 +1,25 @@ +# Build rules for the OpenAFS cmd test suite. + +srcdir=@srcdir@ +abs_top_builddir=@abs_top_builddir@ +include @TOP_OBJDIR@/src/config/Makefile.config +include @TOP_OBJDIR@/src/config/Makefile.pthread + +MODULE_CFLAGS = -I$(srcdir)/.. + +LIBS = ../tap/libtap.a \ + $(abs_top_builddir)/lib/libcmd.a \ + $(abs_top_builddir)/lib/libcom_err.a \ + $(abs_top_builddir)/lib/util.a + +tests = command-t + +all check test tests: $(tests) + +command-t: command-t.o $(LIBS) + $(AFS_LDRULE) command-t.o $(LIBS) $(XLIBS) + +install: + +clean distclean: + $(RM) -f $(tests) *.o core diff --git a/tests/cmd/command-t.c b/tests/cmd/command-t.c new file mode 100644 index 0000000..bbd2997 --- /dev/null +++ b/tests/cmd/command-t.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2010 Your File System Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Test the command line parsing library + */ + +#include +#include + +#include + +#include + +#include + +static int +testproc(struct cmd_syndesc *as, void *rock) +{ + is_string("foo", as->parms[0].items->data, + "first option matches"); + is_string("bar", as->parms[1].items->data, + "second option matches"); + ok(as->parms[2].items != NULL, + "flag is set"); + + return 0; +} + +int +main(int argc, char **argv) +{ + char *tv[100]; + struct cmd_syndesc *opts; + int code; + int tc; + + plan(25); + + initialize_CMD_error_table(); + + opts = cmd_CreateSyntax(NULL, testproc, NULL, NULL); + cmd_AddParm(opts, "-first", CMD_SINGLE, CMD_REQUIRED, "first option"); + cmd_AddParm(opts, "-second", CMD_LIST, CMD_OPTIONAL, "second option"); + cmd_AddParm(opts, "-flag", CMD_FLAG, CMD_OPTIONAL, "a flag"); + + /* A simple command line */ + code = cmd_ParseLine("-first foo -second bar -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(0, code, "dispatching simple comamnd line succeeds"); + cmd_FreeArgv(tv); + + /* unknown switch */ + code = cmd_ParseLine("-first foo -second bar -third -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(CMD_UNKNOWNSWITCH, code, "invalid options fail as expected"); + cmd_FreeArgv(tv); + + /* missing parameter */ + code = cmd_ParseLine("-first foo -second -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(CMD_TOOFEW, code, "missing parameters fail as expected"); + cmd_FreeArgv(tv); + + /* missing option */ + code = cmd_ParseLine("-second bar -third -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(CMD_UNKNOWNSWITCH, code, "missing options fail as expected"); + cmd_FreeArgv(tv); + + code = cmd_ParseLine("-first foo baz -second bar -third -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(CMD_NOTLIST, code, "too many parameters fails as expected"); + cmd_FreeArgv(tv); + + + + + /* Positional parameters */ + code = cmd_ParseLine("foo bar -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(0, code, "dispatching positional parameters succeeds"); + cmd_FreeArgv(tv); + + /* Abbreviations */ + code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(0, code, "dispatching abbreviations succeeds"); + cmd_FreeArgv(tv); + + /* Ambiguous */ + code = cmd_ParseLine("-f foo -s bar -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(CMD_UNKNOWNSWITCH, code, "ambiguous abbreviations correctly fail"); + cmd_FreeArgv(tv); + + return 0; +} -- 1.9.4