Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Take advantage of TAP test driver #135

Merged
merged 6 commits into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Makefile
Makefile.in
missing
stamp-h1
tap-driver.sh
temp.txt
test-driver

Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ after_failure:
- docker exec $CONTAINER su - user sh -c "cd $BUILDDIR && cat test-suite.log"

after_success:
- if test x"$COVERAGE" = xyes; then docker exec $CONTAINER pip install cpp-coveralls; docker exec -e TRAVIS_JOB_ID="$TRAVIS_JOB_ID" -e TRAVIS_BRANCH="$TRAVIS_BRANCH" $CONTAINER sh -c "cd $BUILDDIR && coveralls -b $BUILDDIR -E '(^|.*/)(frob|mock|test)-.*|(^|.*/)(virtual-fixed\.c|print-messages\.c)' --gcov-options '\-lp'"; fi
- if test x"$COVERAGE" = xyes; then docker exec $CONTAINER pip install cpp-coveralls; docker exec -e TRAVIS_JOB_ID="$TRAVIS_JOB_ID" -e TRAVIS_BRANCH="$TRAVIS_BRANCH" $CONTAINER sh -c "cd $BUILDDIR && coveralls -b $BUILDDIR -E '(^|.*/)(frob|mock|test)-.*|(^|.*/)(virtual-fixed\.c)' --gcov-options '\-lp'"; fi
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ AM_TESTS_ENVIRONMENT = \
export abs_top_builddir;
AM_TESTS_FD_REDIRECT = 9>&2;

LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \
$(top_srcdir)/build/litter/tap-driver.sh
LOG_DRIVER_FLAGS = --comments --ignore-exit

MEMCHECK_ENV = $(TEST_RUNNER) valgrind --error-exitcode=80 --quiet

LEAKCHECK_ENV = $(TEST_RUNNER) valgrind --error-exitcode=81 --quiet --leak-check=yes
Expand Down
121 changes: 106 additions & 15 deletions common/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,42 @@ struct {
jmp_buf jump;
} gl = { NULL, NULL, 0, };

static void
print_diagnostics (const char *filename,
int line,
const char *function,
char *output)
{
const char *pos;
char *from;
char *next;

for (from = output; from != NULL; ) {
next = strchr (from, '\n');
if (next) {
next[0] = '\0';
next += 1;
}

printf ("# %s\n", from);
from = next;
}

pos = strrchr (filename, '/');
if (pos != NULL && pos[1] != '\0')
filename = pos + 1;

printf ("# in %s() at %s:%d\n", function, filename, line);
}

void
p11_test_fail (const char *filename,
int line,
const char *function,
const char *message,
...)
{
const char *pos;
char *output;
char *from;
char *next;
va_list va;

assert (gl.last != NULL);
Expand All @@ -113,23 +138,89 @@ p11_test_fail (const char *filename,
assert (0 && "vasprintf() failed");
va_end (va);

for (from = output; from != NULL; ) {
next = strchr (from, '\n');
if (next) {
next[0] = '\0';
next += 1;
}
print_diagnostics (filename, line, function, output);
free (output);

printf ("# %s\n", from);
from = next;
/* Let coverity know we're not supposed to return from here */
#ifdef __COVERITY__
abort();
#endif

longjmp (gl.jump, 1);
}

void
p11_test_skip (const char *filename,
int line,
const char *function,
const char *message,
...)
{
char *output;
char *pos;
va_list va;

assert (gl.last != NULL);
assert (gl.last->type == TEST);
gl.last->x.test.failed = 1;

printf ("ok %d %s", gl.number, gl.last->x.test.name);

va_start (va, message);
if (vasprintf (&output, message, va) < 0)
assert (0 && "vasprintf() failed");
va_end (va);

pos = strchr (output, '\n');
if (pos) {
*pos = '\0';
pos++;
}
printf (" # SKIP %s\n", output);

pos = strrchr (filename, '/');
if (pos != NULL && pos[1] != '\0')
filename = pos + 1;
if (pos)
print_diagnostics (filename, line, function, pos);
free (output);

printf ("# in %s() at %s:%d\n", function, filename, line);
/* Let coverity know we're not supposed to return from here */
#ifdef __COVERITY__
abort();
#endif

longjmp (gl.jump, 1);
}

void
p11_test_todo (const char *filename,
int line,
const char *function,
const char *message,
...)
{
char *output;
char *pos;
va_list va;

assert (gl.last != NULL);
assert (gl.last->type == TEST);
gl.last->x.test.failed = 1;

printf ("not ok %d %s", gl.number, gl.last->x.test.name);

va_start (va, message);
if (vasprintf (&output, message, va) < 0)
assert (0 && "vasprintf() failed");
va_end (va);

pos = strchr (output, '\n');
if (pos) {
*pos = '\0';
pos++;
}
printf (" # TODO %s\n", output);

if (pos)
print_diagnostics (filename, line, function, pos);
free (output);

/* Let coverity know we're not supposed to return from here */
Expand Down
20 changes: 20 additions & 0 deletions common/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
do { const char *__s = (detail); \
p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "%s%s%s", (msg), __s ? ": ": "", __s ? __s : ""); \
} while (0)
#define assert_skip(msg, detail) \
do { const char *__s = (detail); \
p11_test_skip (__FILE__, __LINE__, __FUNCTION__, "%s%s%s", (msg), __s ? ": ": "", __s ? __s : ""); \
} while (0)
#define assert_todo(msg, detail) \
do { const char *__s = (detail); \
p11_test_todo (__FILE__, __LINE__, __FUNCTION__, "%s%s%s", (msg), __s ? ": ": "", __s ? __s : ""); \
} while (0)
#define assert_not_reached(msg) \
do { \
p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "code should not be reached"); \
Expand Down Expand Up @@ -113,6 +121,18 @@ void p11_test_fail (const char *filename,
const char *message,
...) GNUC_PRINTF(4, 5) CLANG_ANALYZER_NORETURN;

void p11_test_skip (const char *filename,
int line,
const char *function,
const char *message,
...) GNUC_PRINTF(4, 5) CLANG_ANALYZER_NORETURN;

void p11_test_todo (const char *filename,
int line,
const char *function,
const char *message,
...) GNUC_PRINTF(4, 5) CLANG_ANALYZER_NORETURN;

void p11_test (void (* function) (void),
const char *name,
...) GNUC_PRINTF(2, 3);
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ P11KIT_AGE=3
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([build/m4])
AC_CONFIG_AUX_DIR([build/litter])
AC_REQUIRE_AUX_FILE([tap-driver.sh])
AM_INIT_AUTOMAKE([1.12 foreign subdir-objects])
AM_SANITY_CHECK
AM_MAINTAINER_MODE([enable])
Expand Down
9 changes: 8 additions & 1 deletion p11-kit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ check_PROGRAMS += \
print_messages_SOURCES = p11-kit/print-messages.c
print_messages_LDADD = $(p11_kit_LIBS)

sh_tests += p11-kit/test-messages.sh

frob_setuid_SOURCES = p11-kit/frob-setuid.c
frob_setuid_LDADD = $(p11_kit_LIBS)

Expand Down Expand Up @@ -324,7 +326,8 @@ check_LTLIBRARIES += \
mock-two.la \
mock-three.la \
mock-four.la \
mock-five.la
mock-five.la \
mock-seven.la

mock_one_la_SOURCES = p11-kit/mock-module-ep.c
mock_one_la_LIBADD = libp11-test.la libp11-common.la
Expand Down Expand Up @@ -355,6 +358,10 @@ mock_six_la_LDFLAGS = $(mock_one_la_LDFLAGS)
mock_six_la_LIBADD = $(mock_one_la_LIBADD)
endif

mock_seven_la_SOURCES = p11-kit/mock-module-ep5.c
mock_seven_la_LDFLAGS = $(mock_one_la_LDFLAGS)
mock_seven_la_LIBADD = $(mock_one_la_LIBADD)

EXTRA_DIST += \
p11-kit/fixtures \
p11-kit/test-mock.c \
Expand Down
4 changes: 4 additions & 0 deletions p11-kit/fixtures/system-modules/seven.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

module: mock-seven.so
critical: yes
enable-in: test-modules
80 changes: 80 additions & 0 deletions p11-kit/mock-module-ep5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2012 Stefan Walter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be red hat here.

* Copyright (c) 2018 Red Hat, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * 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.
* * The names of contributors to this software may not be
* used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
*
* Author: Stef Walter <stef@thewalter.net>, Daiki Ueno
*/

#include "config.h"

#define CRYPTOKI_EXPORTS 1
#include "pkcs11.h"

#include "mock.h"
#include "test.h"

static bool initialized = false;

static CK_RV
override_initialize (CK_VOID_PTR init_args)
{
CK_RV rv;

if (initialized)
return CKR_CRYPTOKI_ALREADY_INITIALIZED;
rv = mock_C_Initialize (init_args);
if (rv == CKR_OK)
initialized = true;
return rv;
}

static CK_RV
override_finalize (CK_VOID_PTR reserved)
{
initialized = false;
return mock_C_Finalize (reserved);
}

#ifdef OS_WIN32
__declspec(dllexport)
#endif
CK_RV
C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list)
{
mock_module_init ();
mock_module.C_GetFunctionList = C_GetFunctionList;
if (list == NULL)
return CKR_ARGUMENTS_BAD;
mock_module.C_Initialize = override_initialize;
mock_module.C_Finalize = override_finalize;
*list = &mock_module;
return CKR_OK;
}
Loading