Skip to content

Commit 72ed0c2

Browse files
committed
tests: Run tests using SoftHSM after installation
This makes the unit tests self-contained so all possible code paths are exercised without optional packages installed. The integration tests against SoftHSM can still run after installation, through "make installcheck" or running the scripts manually. Signed-off-by: Daiki Ueno <ueno@gnu.org>
1 parent cdc4720 commit 72ed0c2

File tree

17 files changed

+552
-129
lines changed

17 files changed

+552
-129
lines changed

.github/actions/basic-autotools/action.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ runs:
4242
cd $GITHUB_WORKSPACE/$BUILDDIR
4343
make install
4444
# This is necessary for 'trust/test-extract.sh'
45+
mkdir -p $GITHUB_WORKSPACE/$INSTALLDIR/etc/pki/ca-trust-source
46+
mkdir -p $GITHUB_WORKSPACE/$INSTALLDIR/share/pki/ca-trust-source
4547
mkdir -p $GITHUB_WORKSPACE/$INSTALLDIR/libexec/p11-kit
4648
ln -sf /usr/bin/true $GITHUB_WORKSPACE/$INSTALLDIR/libexec/p11-kit/trust-extract-compat
4749
if [ "$RUNNER_OS" = "macOS" ]; then
48-
DD=gdd
49-
export DD
50+
export DD=gdd
5051
fi
52+
# This is necessary for 'p11-kit/test-softhsm2.sh'
53+
mkdir -p $GITHUB_WORKSPACE/$INSTALLDIR/share/p11-kit/modules
54+
ln -sf /usr/share/p11-kit/modules/softhsm2.module $GITHUB_WORKSPACE/$INSTALLDIR/share/p11-kit/modules || :
55+
export PATH=$GITHUB_WORKSPACE/$INSTALLDIR/bin:$PATH
56+
export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/$INSTALLDIR/lib
57+
export abs_top_builddir=$GITHUB_WORKSPACE/$BUILDDIR
58+
export abs_top_srcdir=$GITHUB_WORKSPACE
5159
make installcheck
5260
shell: bash

.github/actions/basic-meson/action.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ runs:
1010

1111
- name: Setup
1212
run: |
13-
$GITHUB_WORKSPACE/build/run-wrapper.sh meson setup $GITHUB_WORKSPACE/$BUILDDIR -Dstrict=true -Dprefix=$GITHUB_WORKSPACE/$INSTALLDIR -Dlibdir=$GITHUB_WORKSPACE/$INSTALLDIR/lib -Dsysconfdir=$GITHUB_WORKSPACE/$INSTALLDIR/etc -Dtrust_paths=$GITHUB_WORKSPACE/$INSTALLDIR/etc/pki/ca-trust-source:$GITHUB_WORKSPACE/$INSTALLDIR/share/pki/ca-trust-source -Dsystemd=disabled -Dbash_completion=disabled $MESON_BUILD_OPTS
13+
$GITHUB_WORKSPACE/build/run-wrapper.sh meson setup $GITHUB_WORKSPACE/$BUILDDIR -Dstrict=true -Dprefix=$GITHUB_WORKSPACE/$INSTALLDIR -Dlibdir=$GITHUB_WORKSPACE/$INSTALLDIR/lib -Dsysconfdir=$GITHUB_WORKSPACE/$INSTALLDIR/etc -Dtrust_paths=$GITHUB_WORKSPACE/$INSTALLDIR/etc/pki/ca-trust-source:$GITHUB_WORKSPACE/$INSTALLDIR/share/pki/ca-trust-source -Dsystemd=disabled -Dbash_completion=disabled -Dpost_install_test=true $MESON_BUILD_OPTS
1414
shell: bash
1515

1616
- name: Build (scan-build)
@@ -32,6 +32,21 @@ runs:
3232
exit $ret
3333
shell: bash
3434

35+
- name: Prepare for post-install tests
36+
run: |
37+
# This is necessary for 'trust/test-extract.sh'
38+
mkdir -p $GITHUB_WORKSPACE/$INSTALLDIR/etc/pki/ca-trust-source
39+
mkdir -p $GITHUB_WORKSPACE/$INSTALLDIR/share/pki/ca-trust-source
40+
mkdir -p $GITHUB_WORKSPACE/$INSTALLDIR/libexec/p11-kit
41+
ln -sf /usr/bin/true $GITHUB_WORKSPACE/$INSTALLDIR/libexec/p11-kit/trust-extract-compat
42+
if [ "$RUNNER_OS" = "macOS" ]; then
43+
export DD=gdd
44+
fi
45+
# This is necessary for 'p11-kit/test-softhsm2.sh'
46+
mkdir -p $GITHUB_WORKSPACE/$INSTALLDIR/share/p11-kit/modules
47+
ln -sf /usr/share/p11-kit/modules/softhsm2.module $GITHUB_WORKSPACE/$INSTALLDIR/share/p11-kit/modules || :
48+
shell: bash
49+
3550
- name: Install
3651
run: ninja -C $GITHUB_WORKSPACE/$BUILDDIR install
3752
shell: bash

Makefile.am

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ c_tests =
4343
sh_tests =
4444
TESTS = $(c_tests) $(sh_tests)
4545

46+
post_install_tests =
47+
noinst_SCRIPTS = $(post_install_tests)
48+
4649
moduledir = $(p11_module_path)
4750
module_LTLIBRARIES =
4851

@@ -76,6 +79,8 @@ DISTCHECK_CONFIGURE_FLAGS = \
7679
AM_TESTS_ENVIRONMENT = \
7780
abs_top_builddir="$(abs_top_builddir)"; \
7881
export abs_top_builddir; \
82+
abs_top_srcdir="$(abs_top_srcdir)"; \
83+
export abs_top_srcdir; \
7984
P11_MODULE_PATH="$(abs_top_builddir)/.libs"; \
8085
export P11_MODULE_PATH;
8186
AM_TESTS_FD_REDIRECT = 9>&2;
@@ -113,6 +118,15 @@ dist-hook:
113118
echo A git clone is required to generate a ChangeLog >&2; \
114119
fi
115120

121+
noinst_SCRIPTS += $(post_install_tests)
122+
123+
if !OS_WIN32
124+
installcheck-local:
125+
for t in $(post_install_tests); do \
126+
abs_top_builddir="$(abs_top_builddir)" $(SHELL) $(srcdir)/$$t; \
127+
done
128+
endif
129+
116130
if WITH_COVERAGE
117131
coverage:
118132
mkdir -p build/coverage

meson_options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ option('test', type : 'boolean',
7474
value : true,
7575
description : 'Enable building test programs')
7676

77+
option('post_install_test', type : 'boolean',
78+
value : false,
79+
description : 'Enable running post-install test programs')
80+
7781
option('rpc_min', type : 'integer',
7882
min : 0, max : 0, value : 0,
7983
description : 'Minimum RPC protocol version we support')

meson_post_install_test.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
set +x
4+
5+
bindir="$1"
6+
libdir="$2"
7+
shift 2
8+
9+
export PATH="$MESON_INSTALL_DESTDIR_PREFIX/$bindir:$PATH"
10+
export LD_LIBRARY_PATH="$MESON_INSTALL_DESTDIR_PREFIX/$libdir:$LD_LIBRARY_PATH"
11+
export PKG_CONFIG_PATH="$MESON_INSTALL_DESTDIR_PREFIX/$libdir/pkg-config:$PKG_CONFIG_PATH"
12+
export abs_top_builddir="$MESON_BUILD_ROOT"
13+
export abs_top_srcdir="$MESON_SOURCE_ROOT"
14+
15+
exec "$@"

p11-kit/Makefile.am

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ check_LTLIBRARIES += \
545545
mock-ten.la \
546546
mock-eleven.la \
547547
mock-twelve.la \
548-
mock-thirteen.la
548+
mock-thirteen.la \
549+
mock-fourteen.la
549550

550551
mock_one_la_SOURCES = p11-kit/mock-module-ep.c
551552
mock_one_la_LIBADD = libp11-test.la libp11-common.la
@@ -622,6 +623,18 @@ else
622623
mock_thirteen_la_LIBADD = $(mock_one_la_LIBADD)
623624
endif
624625

626+
mock_fourteen_la_SOURCES = p11-kit/mock-module-ep12.c
627+
mock_fourteen_la_LDFLAGS = $(mock_one_la_LDFLAGS)
628+
if WITH_ASN1
629+
mock_fourteen_la_LIBADD = libp11-asn1.la $(mock_one_la_LIBADD) $(LIBTASN1_LIBS)
630+
else
631+
mock_fourteen_la_LIBADD = $(mock_one_la_LIBADD)
632+
endif
633+
634+
if WITH_ASN1
635+
post_install_tests += p11-kit/test-softhsm2.sh
636+
endif
637+
625638
EXTRA_DIST += \
626639
p11-kit/fixtures \
627640
p11-kit/templates \
@@ -637,4 +650,5 @@ EXTRA_DIST += \
637650
p11-kit/test-import-public.sh \
638651
p11-kit/test-list-mechanisms.sh \
639652
p11-kit/test-generate-keypair.sh \
653+
p11-kit/test-softhsm2.sh \
640654
$(NULL)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
module: libsofthsm2.so
2+
module: mock-fourteen.so
33
managed: yes
44
enable-in: p11-kit-testable

p11-kit/meson.build

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ if get_option('test')
386386

387387
p11_kit_tests_env = environment()
388388
p11_kit_tests_env.set('abs_top_builddir', top_build_dir)
389+
p11_kit_tests_env.set('abs_top_srcdir', top_source_dir)
389390
p11_kit_tests_env.set('P11_MODULE_PATH', meson.current_build_dir())
390391

391392
if host_system != 'windows'
@@ -432,6 +433,15 @@ if get_option('test')
432433
env: p11_kit_tests_env)
433434
endif
434435

436+
if get_option('post_install_test') and with_asn1 and host_system != 'windows'
437+
meson.add_install_script(
438+
top_source_dir / 'meson_post_install_test.sh',
439+
bindir,
440+
libdir,
441+
find_program('test-softhsm2.sh'),
442+
)
443+
endif
444+
435445
mock_sources = {
436446
'mock-one': ['mock-module-ep.c'],
437447
'mock-v3-one': ['mock-module-v3-ep.c'],
@@ -447,7 +457,8 @@ if get_option('test')
447457
'mock-ten': ['mock-module-ep8.c'],
448458
'mock-eleven': ['mock-module-ep9.c'],
449459
'mock-twelve': ['mock-module-ep10.c'],
450-
'mock-thirteen': ['mock-module-ep11.c']
460+
'mock-thirteen': ['mock-module-ep11.c'],
461+
'mock-fourteen': ['mock-module-ep12.c']
451462
}
452463

453464
if host_system != 'windows'

p11-kit/mock-module-ep12.c

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
* Copyright (c) 2023, Red Hat Inc.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* * Redistributions of source code must retain the above
9+
* copyright notice, this list of conditions and the
10+
* following disclaimer.
11+
* * Redistributions in binary form must reproduce the
12+
* above copyright notice, this list of conditions and
13+
* the following disclaimer in the documentation and/or
14+
* other materials provided with the distribution.
15+
* * The names of contributors to this software may not be
16+
* used to endorse or promote products derived from this
17+
* software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29+
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30+
* DAMAGE.
31+
*
32+
* Author: Zoltan Fridrich <zfridric@redhat.com>, Daiki Ueno
33+
*/
34+
35+
#include "config.h"
36+
37+
#define CRYPTOKI_EXPORTS 1
38+
#include "pkcs11.h"
39+
40+
#include "attrs.h"
41+
#include "debug.h"
42+
#include "mock.h"
43+
44+
#ifdef WITH_ASN1
45+
#include "persist.h"
46+
#endif
47+
48+
#include <stdio.h>
49+
#include <string.h>
50+
51+
static const CK_TOKEN_INFO MOCK_TOKEN_INFO = {
52+
"PERSIST LABEL ONE ",
53+
"PERSIST MANUFACTURER ",
54+
"PERSIST MODEL ",
55+
"PERSIST SERIAL ",
56+
CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_CLOCK_ON_TOKEN | CKF_TOKEN_INITIALIZED,
57+
1,
58+
2,
59+
3,
60+
4,
61+
5,
62+
6,
63+
7,
64+
8,
65+
9,
66+
10,
67+
{ 75, 175 },
68+
{ 85, 185 },
69+
{ '1', '9', '9', '9', '0', '5', '2', '5', '0', '9', '1', '9', '5', '9', '0', '0' }
70+
};
71+
72+
static CK_RV
73+
override_C_GetTokenInfo (CK_SLOT_ID slot_id,
74+
CK_TOKEN_INFO_PTR info)
75+
{
76+
return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD);
77+
78+
switch (slot_id) {
79+
case MOCK_SLOT_ONE_ID:
80+
memcpy (info, &MOCK_TOKEN_INFO, sizeof (*info));
81+
return CKR_OK;
82+
case MOCK_SLOT_TWO_ID:
83+
return CKR_TOKEN_NOT_PRESENT;
84+
default:
85+
return CKR_SLOT_ID_INVALID;
86+
}
87+
}
88+
89+
#ifdef WITH_ASN1
90+
static CK_RV
91+
override_C_Initialize (CK_VOID_PTR init_args)
92+
{
93+
bool ok;
94+
size_t i, size = 0;
95+
void *data = NULL;
96+
const char *filename = "test-persist.p11-kit";
97+
p11_mmap *map = NULL;
98+
p11_persist *persist = NULL;
99+
p11_array *objects = NULL;
100+
CK_ATTRIBUTE *attrs = NULL;
101+
CK_RV rv;
102+
103+
map = p11_mmap_open (filename, NULL, &data, &size);
104+
if (map == NULL)
105+
return mock_C_Initialize (init_args);
106+
107+
ok = p11_persist_magic (data, size);
108+
return_val_if_fail (ok, CKR_GENERAL_ERROR);
109+
110+
persist = p11_persist_new ();
111+
return_val_if_fail (persist != NULL, CKR_HOST_MEMORY);
112+
113+
objects = p11_array_new (NULL);
114+
return_val_if_fail (objects != NULL, CKR_HOST_MEMORY);
115+
116+
ok = p11_persist_read (persist, filename, (const unsigned char *)data, size, objects);
117+
return_val_if_fail (ok, CKR_GENERAL_ERROR);
118+
119+
rv = mock_C_Initialize (init_args);
120+
for (i = 0; i < objects->num; ++i) {
121+
attrs = p11_attrs_build (objects->elem[i], NULL);
122+
mock_module_add_object (MOCK_SLOT_ONE_ID, attrs);
123+
p11_attrs_free (attrs);
124+
}
125+
126+
p11_array_free (objects);
127+
p11_persist_free (persist);
128+
p11_mmap_close (map);
129+
return rv;
130+
}
131+
132+
struct WriteData {
133+
p11_persist *persist;
134+
p11_buffer buf;
135+
};
136+
137+
static bool
138+
persist_enumerator (CK_OBJECT_HANDLE handle,
139+
CK_ATTRIBUTE *attrs,
140+
void *user_data)
141+
{
142+
struct WriteData *data = user_data;
143+
144+
p11_persist_write (data->persist, attrs, &data->buf);
145+
146+
return true;
147+
}
148+
149+
static CK_RV
150+
override_C_Finalize (CK_VOID_PTR reserved)
151+
{
152+
bool ok;
153+
FILE *f = NULL;
154+
const char *filename = "test-persist.p11-kit";
155+
CK_SESSION_HANDLE session = 0;
156+
struct WriteData data;
157+
CK_RV rv;
158+
159+
ok = p11_buffer_init (&data.buf, 0);
160+
return_val_if_fail (ok, CKR_HOST_MEMORY);
161+
162+
data.persist = p11_persist_new ();
163+
return_val_if_fail (data.persist != NULL, CKR_HOST_MEMORY);
164+
165+
rv = mock_C_OpenSession (MOCK_SLOT_ONE_ID, CKF_SERIAL_SESSION, NULL, NULL, &session);
166+
return_val_if_fail (rv == CKR_OK, CKR_GENERAL_ERROR);
167+
168+
mock_module_enumerate_objects (session,
169+
persist_enumerator,
170+
&data);
171+
172+
rv = mock_C_CloseSession (session);
173+
return_val_if_fail (rv == CKR_OK, CKR_GENERAL_ERROR);
174+
175+
f = fopen (filename, "wb");
176+
return_val_if_fail (f != NULL, CKR_HOST_MEMORY);
177+
fwrite (data.buf.data, 1, data.buf.len, f);
178+
fclose (f);
179+
180+
p11_persist_free (data.persist);
181+
p11_buffer_uninit (&data.buf);
182+
return mock_C_Finalize (reserved);
183+
}
184+
#endif /* WITH_ASN1 */
185+
186+
#ifdef OS_WIN32
187+
__declspec(dllexport)
188+
#endif
189+
CK_RV
190+
C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list)
191+
{
192+
mock_module_init ();
193+
#ifdef WITH_ASN1
194+
mock_module.C_Initialize = override_C_Initialize;
195+
mock_module.C_Finalize = override_C_Finalize;
196+
#endif
197+
mock_module.C_GetFunctionList = C_GetFunctionList;
198+
mock_module.C_GetTokenInfo = override_C_GetTokenInfo;
199+
if (list == NULL)
200+
return CKR_ARGUMENTS_BAD;
201+
*list = &mock_module;
202+
return CKR_OK;
203+
}

0 commit comments

Comments
 (0)