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 <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@dementia.org>
This commit is contained in:
Simon Wilkinson 2011-04-23 11:42:54 -04:00 committed by Derrick Brashear
parent 0631fb7567
commit 376e24b2ee
7 changed files with 163 additions and 9 deletions

View File

@ -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 ; \

View File

@ -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,

View File

@ -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

View File

@ -4,4 +4,5 @@ util/queues
auth/keys
auth/superuser
auth/authcon
cmd/commands
ptserver/pt_util

1
tests/cmd/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/command-t

25
tests/cmd/Makefile.in Normal file
View File

@ -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

127
tests/cmd/command-t.c Normal file
View File

@ -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 <afsconfig.h>
#include <afs/param.h>
#include <roken.h>
#include <afs/cmd.h>
#include <tap/basic.h>
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;
}