Skip to content

Commit

Permalink
test-proxy: Add test for slot ID reuse
Browse files Browse the repository at this point in the history
The test covers a case where duplicate slots IDs were assigned to two
different slots in the mapping.

Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
  • Loading branch information
ansasaki committed Feb 18, 2020
1 parent fc0c563 commit 54167b5
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 2 deletions.
7 changes: 6 additions & 1 deletion p11-kit/Makefile.am
Expand Up @@ -432,7 +432,8 @@ check_LTLIBRARIES += \
mock-five.la \
mock-seven.la \
mock-eight.la \
mock-nine.la
mock-nine.la \
mock-ten.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 @@ -475,6 +476,10 @@ mock_nine_la_SOURCES = p11-kit/mock-module-ep7.c
mock_nine_la_LDFLAGS = $(mock_one_la_LDFLAGS)
mock_nine_la_LIBADD = $(mock_one_la_LIBADD)

mock_ten_la_SOURCES = p11-kit/mock-module-ep8.c
mock_ten_la_LDFLAGS = $(mock_one_la_LDFLAGS)
mock_ten_la_LIBADD = $(mock_one_la_LIBADD)

EXTRA_DIST += \
p11-kit/fixtures \
p11-kit/test-mock.c \
Expand Down
3 changes: 2 additions & 1 deletion p11-kit/meson.build
Expand Up @@ -275,7 +275,8 @@ mock_sources = {
'mock-five': ['mock-module-ep3.c'],
'mock-seven': ['mock-module-ep5.c'],
'mock-eight': ['mock-module-ep6.c'],
'mock-nine': ['mock-module-ep7.c']
'mock-nine': ['mock-module-ep7.c'],
'mock-ten': ['mock-module-ep8.c']
}

if host_system != 'windows'
Expand Down
110 changes: 110 additions & 0 deletions p11-kit/mock-module-ep8.c
@@ -0,0 +1,110 @@
/*
* Copyright (c) 2012 Stefan Walter
* Copyright (c) 2020 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, Anderson Sasaki
*/

#include "config.h"

#define CRYPTOKI_EXPORTS 1
#include "pkcs11.h"

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

#define MOCK_SLOT_THREE_ID 792

/* Update mock-module.h URIs when updating this */

static int called = 0;
static CK_SLOT_ID last_id = 1;

static CK_RV
override_get_slot_list (CK_BBOOL token_present,
CK_SLOT_ID_PTR slot_list,
CK_ULONG_PTR count)
{
if (count == NULL)
return CKR_ARGUMENTS_BAD;

/* For odd numbered calls, the module will return 1 slot with a slot ID
* returned previously.
*
* For even numbered calls, the module will return 2 slots, being the new
* slot put first in the list */
if (called % 2) {
if (slot_list == NULL) {
*count = 1;
return CKR_OK;
}
if (*count < 1) {
return CKR_BUFFER_TOO_SMALL;
}

slot_list[0] = last_id;
*count = 1;
} else {
if (slot_list == NULL) {
*count = 2;
return CKR_OK;
}

if (*count < 2) {
return CKR_BUFFER_TOO_SMALL;
}

slot_list[1] = last_id;
slot_list[0] = ++last_id;

*count = 2;
}

++called;

return CKR_OK;
}

#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_GetSlotList= override_get_slot_list;
*list = &mock_module;
return CKR_OK;
}

39 changes: 39 additions & 0 deletions p11-kit/test-proxy.c
Expand Up @@ -236,6 +236,7 @@ teardown (void *unused)
#define ENABLED_PREFIX "enable-in: test-proxy-suffix, p11-kit-proxy-suffix, test-proxy, p11-kit-proxy\n"
#define EIGHT_MODULE "module: mock-eight" SHLEXT "\n"
#define NINE_MODULE "module: mock-nine" SHLEXT "\n"
#define TEN_MODULE "module: mock-ten" SHLEXT "\n"

static CK_ULONG
load_modules_and_count_slots (void)
Expand Down Expand Up @@ -393,6 +394,43 @@ test_slot_event (void)
p11_proxy_module_cleanup ();
}

static void
test_reuse_slots (void)
{
CK_FUNCTION_LIST_PTR proxy;
CK_SLOT_ID slots[32];
CK_ULONG count = 32;
CK_RV rv;

p11_test_file_write (test.directory, "ten.module", TEN_MODULE, strlen (TEN_MODULE));

rv = C_GetFunctionList (&proxy);
assert (rv == CKR_OK);

assert (p11_proxy_module_check (proxy));

rv = proxy->C_Initialize (NULL);
assert (rv == CKR_OK);

rv = proxy->C_GetSlotList (CK_FALSE, slots, &count);
assert (rv == CKR_OK);
assert_num_eq (count, 1);

count = 32;

rv = proxy->C_GetSlotList (CK_FALSE, slots, &count);
assert (rv == CKR_OK);
assert_num_eq (count, 2);

/* Make sure the assigned slot IDs are different */
assert_num_cmp (slots[0], !=, slots[1]);

rv = proxy->C_Finalize (NULL);
assert_num_eq (rv, CKR_OK);

p11_proxy_module_cleanup ();
}

static CK_FUNCTION_LIST_PTR
setup_mock_module (CK_SESSION_HANDLE *session)
{
Expand Down Expand Up @@ -481,6 +519,7 @@ main (int argc,
p11_test (test_no_slot, "/proxy/no-slot");
p11_test (test_slot_appear, "/proxy/slot-appear");
p11_test (test_slot_event, "/proxy/slot-event");
p11_test (test_reuse_slots, "/proxy/reuse-slots");

test_mock_add_tests ("/proxy");

Expand Down

0 comments on commit 54167b5

Please sign in to comment.