diff options
Diffstat (limited to 'SoftHSMv2/src/lib/session_mgr/test')
4 files changed, 450 insertions, 0 deletions
diff --git a/SoftHSMv2/src/lib/session_mgr/test/Makefile.am b/SoftHSMv2/src/lib/session_mgr/test/Makefile.am new file mode 100644 index 0000000..6395038 --- /dev/null +++ b/SoftHSMv2/src/lib/session_mgr/test/Makefile.am @@ -0,0 +1,26 @@ +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +AM_CPPFLAGS = -I$(srcdir)/.. \ + -I$(srcdir)/../.. \ + -I$(srcdir)/../../common \ + -I$(srcdir)/../../crypto \ + -I$(srcdir)/../../data_mgr \ + -I$(srcdir)/../../object_store \ + -I$(srcdir)/../../pkcs11 \ + -I$(srcdir)/../../session_mgr \ + -I$(srcdir)/../../slot_mgr \ + @CPPUNIT_CFLAGS@ \ + @CRYPTO_INCLUDES@ + +check_PROGRAMS = sessionmgrtest + +sessionmgrtest_SOURCES = sessionmgrtest.cpp \ + SessionManagerTests.cpp + +sessionmgrtest_LDADD = ../../libsofthsm_convarch.la + +sessionmgrtest_LDFLAGS = @CRYPTO_LIBS@ @CPPUNIT_LIBS@ -no-install -pthread + +TESTS = sessionmgrtest + +EXTRA_DIST = $(srcdir)/*.h diff --git a/SoftHSMv2/src/lib/session_mgr/test/SessionManagerTests.cpp b/SoftHSMv2/src/lib/session_mgr/test/SessionManagerTests.cpp new file mode 100644 index 0000000..2c2e51a --- /dev/null +++ b/SoftHSMv2/src/lib/session_mgr/test/SessionManagerTests.cpp @@ -0,0 +1,260 @@ +/* + * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) + * 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. + */ + +/***************************************************************************** + SessionManagerTests.cpp + + Contains test cases for SessionManager + *****************************************************************************/ + +#include <stdlib.h> +#include <string.h> +#include <cppunit/extensions/HelperMacros.h> +#include "SessionManagerTests.h" +#include "ObjectStore.h" +#include "SessionManager.h" +#include "Session.h" +#include "SlotManager.h" +#include "cryptoki.h" + +CPPUNIT_TEST_SUITE_REGISTRATION(SessionManagerTests); + +void SessionManagerTests::setUp() +{ + CPPUNIT_ASSERT(!system("mkdir testdir")); +} + +void SessionManagerTests::tearDown() +{ +#ifndef _WIN32 + CPPUNIT_ASSERT(!system("rm -rf testdir")); +#else + CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul")); +#endif +} + +void SessionManagerTests::testOpenClose() +{ + // Create an empty object store +#ifndef _WIN32 + ObjectStore store("./testdir"); +#else + ObjectStore store(".\\testdir"); +#endif + + // Create the managers + SlotManager slotManager(&store); + SessionManager sessionManager; + + // Get a slot + CK_SLOT_ID slotID = 0; + Slot* slot = slotManager.getSlot(slotID); + + // Use some bad data + CK_SESSION_HANDLE hSession; + CK_RV rv = sessionManager.openSession(NULL, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_SLOT_ID_INVALID); + rv = sessionManager.openSession(slot, 0, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_SESSION_PARALLEL_NOT_SUPPORTED); + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, NULL_PTR); + CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); + + // Try open a slot with an uninitialized token + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_TOKEN_NOT_RECOGNIZED); + + // Initialize the token + ByteString soPIN((unsigned char*)"1234", 4); + CK_UTF8CHAR label[33] = "My test token "; + CPPUNIT_ASSERT(slot->initToken(soPIN, label) == CKR_OK); + + // Open a session + bool haveSession = sessionManager.haveSession(slotID); + CPPUNIT_ASSERT(haveSession == false); + bool haveROSession = sessionManager.haveROSession(slotID); + CPPUNIT_ASSERT(haveROSession == false); + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + haveSession = sessionManager.haveSession(slotID); + CPPUNIT_ASSERT(haveSession == true); + haveROSession = sessionManager.haveROSession(slotID); + CPPUNIT_ASSERT(haveROSession == true); + + // Close session + rv = sessionManager.closeSession(CK_INVALID_HANDLE); + CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); + rv = sessionManager.closeSession(hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + rv = sessionManager.closeSession(hSession); + CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); + haveSession = sessionManager.haveSession(slotID); + CPPUNIT_ASSERT(haveSession == false); + haveROSession = sessionManager.haveROSession(slotID); + CPPUNIT_ASSERT(haveROSession == false); + + // Try open a Read-Only session when in SO mode + rv = slot->getToken()->loginSO(soPIN); + CPPUNIT_ASSERT(rv == CKR_OK); + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_SESSION_READ_WRITE_SO_EXISTS); + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + haveSession = sessionManager.haveSession(slotID); + CPPUNIT_ASSERT(haveSession == true); + haveROSession = sessionManager.haveROSession(slotID); + CPPUNIT_ASSERT(haveROSession == false); + + // Close session and check that we are logged out + bool isLoggedIn = slot->getToken()->isSOLoggedIn(); + CPPUNIT_ASSERT(isLoggedIn == true); + rv = sessionManager.closeSession(hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + isLoggedIn = slot->getToken()->isSOLoggedIn(); + CPPUNIT_ASSERT(isLoggedIn == false); + haveSession = sessionManager.haveSession(slotID); + CPPUNIT_ASSERT(haveSession == false); + haveROSession = sessionManager.haveROSession(slotID); + CPPUNIT_ASSERT(haveROSession == false); + + // Open a new logged in session + rv = slot->getToken()->loginSO(soPIN); + CPPUNIT_ASSERT(rv == CKR_OK); + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + + // Close all sessions and check that we are logged out + isLoggedIn = slot->getToken()->isSOLoggedIn(); + CPPUNIT_ASSERT(isLoggedIn == true); + rv = sessionManager.closeAllSessions(NULL); + CPPUNIT_ASSERT(rv == CKR_SLOT_ID_INVALID); + rv = sessionManager.closeAllSessions(slot); + CPPUNIT_ASSERT(rv == CKR_OK); + isLoggedIn = slot->getToken()->isSOLoggedIn(); + CPPUNIT_ASSERT(isLoggedIn == false); +} + +void SessionManagerTests::testSessionInfo() +{ + // Create an empty object store +#ifndef _WIN32 + ObjectStore store("./testdir"); +#else + ObjectStore store(".\\testdir"); +#endif + + // Create the managers + SlotManager slotManager(&store); + SessionManager sessionManager; + + // Get a slot + CK_SLOT_ID slotID = 0; + Slot* slot = slotManager.getSlot(slotID); + + // Initialize the token + ByteString soPIN((unsigned char*)"1234", 4); + ByteString userPIN((unsigned char*)"1234", 4); + CK_UTF8CHAR label[33] = "My test token "; + CPPUNIT_ASSERT(slot->initToken(soPIN, label) == CKR_OK); + CPPUNIT_ASSERT(slot->getToken()->loginSO(soPIN) == CKR_OK); + CPPUNIT_ASSERT(slot->getToken()->initUserPIN(userPIN) == CKR_OK); + slot->getToken()->logout(); + + // Get a session + CK_SESSION_HANDLE hSession; + CK_RV rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + + // Get session info + CK_SESSION_INFO info; + rv = sessionManager.getSessionInfo(CK_INVALID_HANDLE, &info); + CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); + rv = sessionManager.getSessionInfo(hSession, NULL_PTR); + CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); + rv = sessionManager.getSessionInfo(hSession, &info); + CPPUNIT_ASSERT(rv == CKR_OK); + + // Public RO session info + CPPUNIT_ASSERT(info.slotID == slotID); + CPPUNIT_ASSERT(info.state == CKS_RO_PUBLIC_SESSION); + CPPUNIT_ASSERT(info.flags == CKF_SERIAL_SESSION); + + rv = sessionManager.closeSession(hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + + // Public RW session info + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + Session* session = sessionManager.getSession(CK_INVALID_HANDLE); + CPPUNIT_ASSERT(session == NULL); + session = sessionManager.getSession(hSession); + CPPUNIT_ASSERT(session != NULL); + rv = session->getInfo(&info); + CPPUNIT_ASSERT(rv == CKR_OK); + CPPUNIT_ASSERT(info.state == CKS_RW_PUBLIC_SESSION); + CPPUNIT_ASSERT(info.flags == (CKF_SERIAL_SESSION | CKF_RW_SESSION)); + + rv = sessionManager.closeSession(hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + + // User RO session info + rv = slot->getToken()->loginUser(userPIN); + CPPUNIT_ASSERT(rv == CKR_OK); + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + rv = sessionManager.getSessionInfo(hSession, &info); + CPPUNIT_ASSERT(rv == CKR_OK); + CPPUNIT_ASSERT(info.state == CKS_RO_USER_FUNCTIONS); + CPPUNIT_ASSERT(info.flags == CKF_SERIAL_SESSION); + + rv = sessionManager.closeSession(hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + + // User RW session info + rv = slot->getToken()->loginUser(userPIN); + CPPUNIT_ASSERT(rv == CKR_OK); + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + rv = sessionManager.getSessionInfo(hSession, &info); + CPPUNIT_ASSERT(rv == CKR_OK); + CPPUNIT_ASSERT(info.state == CKS_RW_USER_FUNCTIONS); + CPPUNIT_ASSERT(info.flags == (CKF_SERIAL_SESSION | CKF_RW_SESSION)); + + rv = sessionManager.closeSession(hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + + // SO RW session info + rv = slot->getToken()->loginSO(soPIN); + CPPUNIT_ASSERT(rv == CKR_OK); + rv = sessionManager.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession); + CPPUNIT_ASSERT(rv == CKR_OK); + rv = sessionManager.getSessionInfo(hSession, &info); + CPPUNIT_ASSERT(rv == CKR_OK); + CPPUNIT_ASSERT(info.state == CKS_RW_SO_FUNCTIONS); + CPPUNIT_ASSERT(info.flags == (CKF_SERIAL_SESSION | CKF_RW_SESSION)); + + rv = sessionManager.closeSession(hSession); + CPPUNIT_ASSERT(rv == CKR_OK); +} diff --git a/SoftHSMv2/src/lib/session_mgr/test/SessionManagerTests.h b/SoftHSMv2/src/lib/session_mgr/test/SessionManagerTests.h new file mode 100644 index 0000000..23f9d8c --- /dev/null +++ b/SoftHSMv2/src/lib/session_mgr/test/SessionManagerTests.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) + * 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. + */ + +/***************************************************************************** + SessionManagerTests.h + + Contains test cases for SessionManager + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_SESSIONMANAGERTESTS_H +#define _SOFTHSM_V2_SESSIONMANAGERTESTS_H + +#include <cppunit/extensions/HelperMacros.h> + +class SessionManagerTests : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(SessionManagerTests); + CPPUNIT_TEST(testOpenClose); + CPPUNIT_TEST(testSessionInfo); + CPPUNIT_TEST_SUITE_END(); + +public: + void testOpenClose(); + void testSessionInfo(); + + void setUp(); + void tearDown(); +}; + +#endif // !_SOFTHSM_V2_SESSIONMANAGERTESTS_H diff --git a/SoftHSMv2/src/lib/session_mgr/test/sessionmgrtest.cpp b/SoftHSMv2/src/lib/session_mgr/test/sessionmgrtest.cpp new file mode 100644 index 0000000..28eeee8 --- /dev/null +++ b/SoftHSMv2/src/lib/session_mgr/test/sessionmgrtest.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2010 SURFnet bv + * 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. + */ + +/***************************************************************************** + sessionmgrtest.cpp + + The main test executor for tests on the session manager in SoftHSM v2 + *****************************************************************************/ + +#include <cppunit/extensions/TestFactoryRegistry.h> +#include <cppunit/ui/text/TestRunner.h> +#include <cppunit/TestResult.h> +#include <cppunit/TestFailure.h> +#include <cppunit/TestResultCollector.h> +#include <cppunit/SourceLine.h> +#include <cppunit/Message.h> +#include <cppunit/Exception.h> +#include <cppunit/XmlOutputter.h> +#include <fstream> +#include <stdlib.h> +#include <iostream> + +#include "config.h" +#include "MutexFactory.h" +#include "SecureMemoryRegistry.h" + +#if defined(WITH_OPENSSL) +#include "OSSLCryptoFactory.h" +#else +#include "BotanCryptoFactory.h" +#endif + +// Initialise the one-and-only instance +#ifdef HAVE_CXX11 + +std::unique_ptr<MutexFactory> MutexFactory::instance(nullptr); +std::unique_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(nullptr); +#if defined(WITH_OPENSSL) +std::unique_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(nullptr); +#else +std::unique_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(nullptr); +#endif + +#else + +std::auto_ptr<MutexFactory> MutexFactory::instance(NULL); +std::auto_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(NULL); +#if defined(WITH_OPENSSL) +std::auto_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(NULL); +#else +std::auto_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(NULL); +#endif + +#endif + +class MyListener : public CPPUNIT_NS::TestListener { + virtual void startTest( CPPUNIT_NS::Test*const pTest ) { + std::cout << std::endl << pTest->getName() << ' ' << pTest->countTestCases() << std::endl << std::endl; + } + virtual void addFailure( const CPPUNIT_NS::TestFailure & failure ) { + const CPPUNIT_NS::SourceLine solurceLine( failure.sourceLine() ); + CPPUNIT_NS::Message message( failure.thrownException()->message() ); + std::cout << solurceLine.fileName() << ' ' << solurceLine.lineNumber() << ' ' << message.shortDescription() << std::endl; + std::cout << message.details() << std::endl << std::endl; + } +}; + +int main(int /*argc*/, char** /*argv*/) +{ + CppUnit::TestResult controller; + CppUnit::TestResultCollector result; + CppUnit::TextUi::TestRunner runner; + controller.addListener(&result); + MyListener progress; + controller.addListener(&progress); + CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry(); + + runner.addTest(registry.makeTest()); + runner.run(controller); + + std::ofstream xmlFileOut("test-results.xml"); + CppUnit::XmlOutputter xmlOut(&result, xmlFileOut); + xmlOut.write(); + + CryptoFactory::reset(); + + return result.wasSuccessful() ? 0 : 1; +} |