From a5bf82af09c18a768a7ec722a22de943746f463e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Silv=C3=A9n?= Date: Thu, 4 Mar 2021 15:53:38 +0100 Subject: [PATCH] Do not refresh object store before fetching object. Before this commit the object store for a file token was always refreshed by reading the file of the token every time an object of the token was fetched. Now the HSM may be configured not to refresh when fetching an object. But the refresh will still be done after an application gets a handle for an object. The reason for this change is that the CPU time consumed by the reading may not be negligible for some HW. It is only the objects store on file that are affected by this feature. If DB is used refreshing will always be done since the is then no difference in CPU usage. --- src/lib/SoftHSM.cpp | 46 +++---- src/lib/SoftHSM.h | 2 + src/lib/common/Configuration.cpp | 1 + src/lib/common/softhsm2.conf.5.in | 25 ++++ src/lib/common/softhsm2.conf.in | 3 + src/lib/object_store/DBObject.cpp | 2 +- src/lib/object_store/DBObject.h | 2 +- src/lib/object_store/DBToken.cpp | 2 +- src/lib/object_store/OSObject.h | 3 +- src/lib/object_store/OSToken.cpp | 15 ++- src/lib/object_store/ObjectFile.cpp | 10 +- src/lib/object_store/ObjectFile.h | 2 +- src/lib/object_store/SessionObject.cpp | 3 +- src/lib/object_store/SessionObject.h | 3 +- src/lib/object_store/SessionObjectStore.cpp | 2 +- src/lib/object_store/test/Makefile.am | 2 + src/lib/object_store/test/ObjectFileTests.cpp | 116 ++++++++---------- src/lib/object_store/test/ObjectFileTests.h | 24 +--- .../test/ObjectFileTestsNoRefresh.cpp | 56 +++++++++ .../test/ObjectFileTestsNoRefresh.h | 65 ++++++++++ .../test/ObjectFileTestsRefresh.cpp | 56 +++++++++ .../test/ObjectFileTestsRefresh.h | 65 ++++++++++ 22 files changed, 381 insertions(+), 124 deletions(-) create mode 100644 src/lib/object_store/test/ObjectFileTestsNoRefresh.cpp create mode 100644 src/lib/object_store/test/ObjectFileTestsNoRefresh.h create mode 100644 src/lib/object_store/test/ObjectFileTestsRefresh.cpp create mode 100644 src/lib/object_store/test/ObjectFileTestsRefresh.h diff --git a/src/lib/SoftHSM.cpp b/src/lib/SoftHSM.cpp index 54f9089f2..6704ec02d 100644 --- a/src/lib/SoftHSM.cpp +++ b/src/lib/SoftHSM.cpp @@ -610,6 +610,8 @@ CK_RV SoftHSM::C_Initialize(CK_VOID_PTR pInitArgs) // Load the handle manager handleManager = new HandleManager(); + doRefresh = Configuration::i()->getBool("objectstore.readrefresh", true); + // Set the state to initialised isInitialised = true; @@ -1608,7 +1610,7 @@ CK_RV SoftHSM::C_CopyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject // Check the object handle. OSObject *object = (OSObject *)handleManager->getObject(hObject); - if (object == NULL_PTR || !object->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (object == NULL_PTR || !object->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL wasOnToken = object->getBooleanValue(CKA_TOKEN, false); CK_BBOOL wasPrivate = object->getBooleanValue(CKA_PRIVATE, true); @@ -1777,7 +1779,7 @@ CK_RV SoftHSM::C_DestroyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObj // Check the object handle. OSObject *object = (OSObject *)handleManager->getObject(hObject); - if (object == NULL_PTR || !object->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (object == NULL_PTR || !object->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = object->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = object->getBooleanValue(CKA_PRIVATE, true); @@ -1825,7 +1827,7 @@ CK_RV SoftHSM::C_GetObjectSize(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObj // Check the object handle. OSObject *object = (OSObject *)handleManager->getObject(hObject); - if (object == NULL_PTR || !object->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (object == NULL_PTR || !object->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; *pulSize = CK_UNAVAILABLE_INFORMATION; @@ -1849,7 +1851,7 @@ CK_RV SoftHSM::C_GetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE // Check the object handle. OSObject *object = (OSObject *)handleManager->getObject(hObject); - if (object == NULL_PTR || !object->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (object == NULL_PTR || !object->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = object->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = object->getBooleanValue(CKA_PRIVATE, true); @@ -1896,7 +1898,7 @@ CK_RV SoftHSM::C_SetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE // Check the object handle. OSObject *object = (OSObject *)handleManager->getObject(hObject); - if (object == NULL_PTR || !object->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (object == NULL_PTR || !object->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = object->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = object->getBooleanValue(CKA_PRIVATE, true); @@ -2166,7 +2168,7 @@ CK_RV SoftHSM::SymEncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hKey); - if (key == NULL_PTR || !key->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -2414,7 +2416,7 @@ CK_RV SoftHSM::AsymEncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMec // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hKey); - if (key == NULL_PTR || !key->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -2895,7 +2897,7 @@ CK_RV SoftHSM::SymDecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hKey); - if (key == NULL_PTR || !key->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -3144,7 +3146,7 @@ CK_RV SoftHSM::AsymDecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMec // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hKey); - if (key == NULL_PTR || !key->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -3795,7 +3797,7 @@ CK_RV SoftHSM::C_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject) // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hObject); - if (key == NULL_PTR || !key->isValid()) return CKR_KEY_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_KEY_HANDLE_INVALID; CK_BBOOL isOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -3946,7 +3948,7 @@ CK_RV SoftHSM::MacSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechani // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hKey); - if (key == NULL_PTR || !key->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -4098,7 +4100,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hKey); - if (key == NULL_PTR || !key->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -4924,7 +4926,7 @@ CK_RV SoftHSM::MacVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMecha // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hKey); - if (key == NULL_PTR || !key->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -5076,7 +5078,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hKey); - if (key == NULL_PTR || !key->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -6512,7 +6514,7 @@ CK_RV SoftHSM::C_WrapKey // Check the wrapping key handle. OSObject *wrapKey = (OSObject *)handleManager->getObject(hWrappingKey); - if (wrapKey == NULL_PTR || !wrapKey->isValid()) return CKR_WRAPPING_KEY_HANDLE_INVALID; + if (wrapKey == NULL_PTR || !wrapKey->isValid(doRefresh)) return CKR_WRAPPING_KEY_HANDLE_INVALID; CK_BBOOL isWrapKeyOnToken = wrapKey->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isWrapKeyPrivate = wrapKey->getBooleanValue(CKA_PRIVATE, true); @@ -6554,7 +6556,7 @@ CK_RV SoftHSM::C_WrapKey // Check the to be wrapped key handle. OSObject *key = (OSObject *)handleManager->getObject(hKey); - if (key == NULL_PTR || !key->isValid()) return CKR_KEY_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_KEY_HANDLE_INVALID; CK_BBOOL isKeyOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isKeyPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -6980,7 +6982,7 @@ CK_RV SoftHSM::C_UnwrapKey // Check the unwrapping key handle. OSObject *unwrapKey = (OSObject *)handleManager->getObject(hUnwrappingKey); - if (unwrapKey == NULL_PTR || !unwrapKey->isValid()) return CKR_UNWRAPPING_KEY_HANDLE_INVALID; + if (unwrapKey == NULL_PTR || !unwrapKey->isValid(doRefresh)) return CKR_UNWRAPPING_KEY_HANDLE_INVALID; CK_BBOOL isUnwrapKeyOnToken = unwrapKey->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isUnwrapKeyPrivate = unwrapKey->getBooleanValue(CKA_PRIVATE, true); @@ -7280,7 +7282,7 @@ CK_RV SoftHSM::C_DeriveKey // Check the key handle. OSObject *key = (OSObject *)handleManager->getObject(hBaseKey); - if (key == NULL_PTR || !key->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (key == NULL_PTR || !key->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; CK_BBOOL isKeyOnToken = key->getBooleanValue(CKA_TOKEN, false); CK_BBOOL isKeyPrivate = key->getBooleanValue(CKA_PRIVATE, true); @@ -10386,7 +10388,7 @@ CK_RV SoftHSM::deriveDH // Get the base key handle OSObject *baseKey = (OSObject *)handleManager->getObject(hBaseKey); - if (baseKey == NULL || !baseKey->isValid()) + if (baseKey == NULL || !baseKey->isValid(doRefresh)) return CKR_KEY_HANDLE_INVALID; // Get the DH algorithm handler @@ -10718,7 +10720,7 @@ CK_RV SoftHSM::deriveECDH // Get the base key handle OSObject *baseKey = (OSObject *)handleManager->getObject(hBaseKey); - if (baseKey == NULL || !baseKey->isValid()) + if (baseKey == NULL || !baseKey->isValid(doRefresh)) return CKR_KEY_HANDLE_INVALID; // Get the ECDH algorithm handler @@ -11072,7 +11074,7 @@ CK_RV SoftHSM::deriveEDDSA // Get the base key handle OSObject *baseKey = (OSObject *)handleManager->getObject(hBaseKey); - if (baseKey == NULL || !baseKey->isValid()) + if (baseKey == NULL || !baseKey->isValid(doRefresh)) return CKR_KEY_HANDLE_INVALID; // Get the EDDSA algorithm handler @@ -11598,7 +11600,7 @@ CK_RV SoftHSM::deriveSymmetric // Check the key handle OSObject *baseKey = (OSObject *)handleManager->getObject(hBaseKey); - if (baseKey == NULL_PTR || !baseKey->isValid()) return CKR_OBJECT_HANDLE_INVALID; + if (baseKey == NULL_PTR || !baseKey->isValid(doRefresh)) return CKR_OBJECT_HANDLE_INVALID; // Get the data ByteString secretValue; diff --git a/src/lib/SoftHSM.h b/src/lib/SoftHSM.h index e92a1772c..aa0ed3894 100644 --- a/src/lib/SoftHSM.h +++ b/src/lib/SoftHSM.h @@ -186,6 +186,8 @@ class SoftHSM // Is the SoftHSM PKCS #11 library initialised? bool isInitialised; bool isRemovable; + // Do refresh of all objects from storage before validating. + bool doRefresh; SessionObjectStore* sessionObjectStore; ObjectStore* objectStore; diff --git a/src/lib/common/Configuration.cpp b/src/lib/common/Configuration.cpp index ab984d036..53755204a 100644 --- a/src/lib/common/Configuration.cpp +++ b/src/lib/common/Configuration.cpp @@ -51,6 +51,7 @@ const struct config Configuration::valid_config[] = { { "slots.removable", CONFIG_TYPE_BOOL }, { "slots.mechanisms", CONFIG_TYPE_STRING }, { "library.reset_on_fork", CONFIG_TYPE_BOOL }, + { "objectstore.readrefresh", CONFIG_TYPE_BOOL }, { "", CONFIG_TYPE_UNSUPPORTED } }; diff --git a/src/lib/common/softhsm2.conf.5.in b/src/lib/common/softhsm2.conf.5.in index cf356e9de..c55fc95e3 100644 --- a/src/lib/common/softhsm2.conf.5.in +++ b/src/lib/common/softhsm2.conf.5.in @@ -102,6 +102,31 @@ library.reset_on_fork = true .fi .RE .LP +.SH OBJECTSTORE.READREFRESH +If set to false and if 'objectstore.backend = file', then this will affect +the refreshing of the object store. +Before using an object that is not changed, no files will be read. +.LP +Depending of what kind of HW that is used setting 'false' may improve the +performance of the HSM. +.LP +But the drawback is that if one processes is using an object handle from a +token for multiple function calls then this process may still use the old +unmodified or deleted object even if it is changed or deleted. Another +process may have called C_DestroyObject or C_SetAttributeValue. But every +time a process gets a new handle for an object the objectstore of this +process is updated for all objects even if this property is false. +.LP +If 'objectstore.backend = db' then the value of this property is ignored. +.LP +Default is true. +.LP +.RS +.nf +objectstore.readrefresh = false +.fi +.RE +.LP .SH ENVIRONMENT .TP SOFTHSM2_CONF diff --git a/src/lib/common/softhsm2.conf.in b/src/lib/common/softhsm2.conf.in index fcc587272..578bc2981 100644 --- a/src/lib/common/softhsm2.conf.in +++ b/src/lib/common/softhsm2.conf.in @@ -15,3 +15,6 @@ slots.mechanisms = ALL # If the library should reset the state on fork library.reset_on_fork = false + +# Set to false if there should be no update of a token objects each time it is used. +objectstore.readrefresh = true diff --git a/src/lib/object_store/DBObject.cpp b/src/lib/object_store/DBObject.cpp index a29417343..f9c7208af 100644 --- a/src/lib/object_store/DBObject.cpp +++ b/src/lib/object_store/DBObject.cpp @@ -1364,7 +1364,7 @@ bool DBObject::deleteAttribute(CK_ATTRIBUTE_TYPE type) } // The validity state of the object -bool DBObject::isValid() +bool DBObject::isValid(const bool doRefresh __attribute__((unused))) { MutexLocker lock(_mutex); diff --git a/src/lib/object_store/DBObject.h b/src/lib/object_store/DBObject.h index 4dc1249f1..c191dc65a 100644 --- a/src/lib/object_store/DBObject.h +++ b/src/lib/object_store/DBObject.h @@ -96,7 +96,7 @@ class DBObject : public OSObject virtual bool deleteAttribute(CK_ATTRIBUTE_TYPE type); // The validity state of the object - virtual bool isValid(); + virtual bool isValid(bool doRefresh); // Start an attribute set transaction; this method is used when - for // example - a key is generated and all its attributes need to be diff --git a/src/lib/object_store/DBToken.cpp b/src/lib/object_store/DBToken.cpp index 53b91f478..b4767c84e 100644 --- a/src/lib/object_store/DBToken.cpp +++ b/src/lib/object_store/DBToken.cpp @@ -684,7 +684,7 @@ OSObject *DBToken::createObject() return NULL; } - if (!newObject->isValid()) + if (!newObject->isValid(true)) { newObject->abortTransaction(); delete newObject; diff --git a/src/lib/object_store/OSObject.h b/src/lib/object_store/OSObject.h index 6efaa6de5..adbbdd8c4 100644 --- a/src/lib/object_store/OSObject.h +++ b/src/lib/object_store/OSObject.h @@ -64,7 +64,8 @@ class OSObject virtual bool deleteAttribute(CK_ATTRIBUTE_TYPE type) = 0; // The validity state of the object - virtual bool isValid() = 0; + // If doRefresh==true then update the object from the storage before validating. + virtual bool isValid(bool doRefresh=true) = 0; // Start an attribute set transaction; this method is used when - for // example - a key is generated and all its attributes need to be diff --git a/src/lib/object_store/OSToken.cpp b/src/lib/object_store/OSToken.cpp index ea9b43c99..40fdde3e8 100644 --- a/src/lib/object_store/OSToken.cpp +++ b/src/lib/object_store/OSToken.cpp @@ -179,7 +179,8 @@ bool OSToken::setSOPIN(const ByteString& soPINBlob) // Get the SO PIN bool OSToken::getSOPIN(ByteString& soPINBlob) { - if (!valid || !tokenObject->isValid()) + // this is done so rarely so will not impact the performance to refresh. + if (!valid || !tokenObject->isValid(true)) { return false; } @@ -223,7 +224,8 @@ bool OSToken::setUserPIN(ByteString userPINBlob) // Get the user PIN bool OSToken::getUserPIN(ByteString& userPINBlob) { - if (!valid || !tokenObject->isValid()) + // this is done so rarely so will not impact the performance to refresh. + if (!valid || !tokenObject->isValid(true)) { return false; } @@ -243,7 +245,8 @@ bool OSToken::getUserPIN(ByteString& userPINBlob) // Retrieve the token label bool OSToken::getTokenLabel(ByteString& label) { - if (!valid || !tokenObject->isValid()) + // this is done so rarely so will not impact the performance to refresh. + if (!valid || !tokenObject->isValid(true)) { return false; } @@ -263,7 +266,8 @@ bool OSToken::getTokenLabel(ByteString& label) // Retrieve the token serial bool OSToken::getTokenSerial(ByteString& serial) { - if (!valid || !tokenObject->isValid()) + // this is done so rarely so will not impact the performance to refresh. + if (!valid || !tokenObject->isValid(true)) { return false; } @@ -283,7 +287,8 @@ bool OSToken::getTokenSerial(ByteString& serial) // Get the token flags bool OSToken::getTokenFlags(CK_ULONG& flags) { - if (!valid || !tokenObject->isValid()) + // this is done so rarely so will not impact the performance to refresh. + if (!valid || !tokenObject->isValid(true)) { return false; } diff --git a/src/lib/object_store/ObjectFile.cpp b/src/lib/object_store/ObjectFile.cpp index ba72e4fe4..9152a1202 100644 --- a/src/lib/object_store/ObjectFile.cpp +++ b/src/lib/object_store/ObjectFile.cpp @@ -262,11 +262,13 @@ bool ObjectFile::deleteAttribute(CK_ATTRIBUTE_TYPE type) return valid; } -// The validity state of the object (refresh from disk as a side effect) -bool ObjectFile::isValid() +// The validity state of the object (may refresh from disk as a side effect) +bool ObjectFile::isValid(const bool doRefresh) { - refresh(); - + if(doRefresh) + { + refresh(); + } return valid; } diff --git a/src/lib/object_store/ObjectFile.h b/src/lib/object_store/ObjectFile.h index b74c9a6a0..3b11e6e2b 100644 --- a/src/lib/object_store/ObjectFile.h +++ b/src/lib/object_store/ObjectFile.h @@ -76,7 +76,7 @@ class ObjectFile : public OSObject virtual bool deleteAttribute(CK_ATTRIBUTE_TYPE type); // The validity state of the object (refresh from disk as a side effect) - virtual bool isValid(); + virtual bool isValid(bool doRefresh); // Invalidate the object file externally; this method is normally // only called by the OSToken class in case an object file has diff --git a/src/lib/object_store/SessionObject.cpp b/src/lib/object_store/SessionObject.cpp index 49dfa7d42..9721df2f1 100644 --- a/src/lib/object_store/SessionObject.cpp +++ b/src/lib/object_store/SessionObject.cpp @@ -217,7 +217,8 @@ bool SessionObject::deleteAttribute(CK_ATTRIBUTE_TYPE type) } // The validity state of the object -bool SessionObject::isValid() +// the doRefresh parameter has no meaning for this implementation since noting is stored on disk. +bool SessionObject::isValid(const bool /*doRefresh*/) { return valid; } diff --git a/src/lib/object_store/SessionObject.h b/src/lib/object_store/SessionObject.h index caadb64eb..83c571783 100644 --- a/src/lib/object_store/SessionObject.h +++ b/src/lib/object_store/SessionObject.h @@ -73,7 +73,8 @@ class SessionObject : public OSObject virtual bool deleteAttribute(CK_ATTRIBUTE_TYPE type); // The validity state of the object - virtual bool isValid(); + // doRefresh has no meaning since this is a session object. + virtual bool isValid(bool doRefresh=true); bool hasSlotID(CK_SLOT_ID inSlotID); diff --git a/src/lib/object_store/SessionObjectStore.cpp b/src/lib/object_store/SessionObjectStore.cpp index d8d8da6f8..af95aecae 100644 --- a/src/lib/object_store/SessionObjectStore.cpp +++ b/src/lib/object_store/SessionObjectStore.cpp @@ -106,7 +106,7 @@ SessionObject* SessionObjectStore::createObject(CK_SLOT_ID slotID, CK_SESSION_HA // Create the new object file SessionObject* newObject = new SessionObject(this, slotID, hSession, isPrivate); - if (!newObject->isValid()) + if (!newObject->isValid(false)) { ERROR_MSG("Failed to create new object"); diff --git a/src/lib/object_store/test/Makefile.am b/src/lib/object_store/test/Makefile.am index 0ba05c136..761cab520 100644 --- a/src/lib/object_store/test/Makefile.am +++ b/src/lib/object_store/test/Makefile.am @@ -17,6 +17,8 @@ objstoretest_SOURCES = objstoretest.cpp \ DirectoryTests.cpp \ UUIDTests.cpp \ FileTests.cpp \ + ObjectFileTestsRefresh.cpp \ + ObjectFileTestsNoRefresh.cpp \ ObjectFileTests.cpp \ OSTokenTests.cpp \ ObjectStoreTests.cpp \ diff --git a/src/lib/object_store/test/ObjectFileTests.cpp b/src/lib/object_store/test/ObjectFileTests.cpp index fc5aabecf..071d43136 100644 --- a/src/lib/object_store/test/ObjectFileTests.cpp +++ b/src/lib/object_store/test/ObjectFileTests.cpp @@ -43,26 +43,10 @@ #include "RNG.h" #include "cryptoki.h" -CPPUNIT_TEST_SUITE_REGISTRATION(ObjectFileTests); - // FIXME: all pathnames in this file are *NIX/BSD specific -void ObjectFileTests::setUp() -{ -#ifndef _WIN32 - CPPUNIT_ASSERT(!system("mkdir testdir")); -#else - system("mkdir testdir 2> nul"); -#endif -} - -void ObjectFileTests::tearDown() +ObjectFileTests::ObjectFileTests(const bool _doRefresh) : doRefresh(_doRefresh) { -#ifndef _WIN32 - CPPUNIT_ASSERT(!system("rm -rf testdir")); -#else - CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul")); -#endif } void ObjectFileTests::testBoolAttr() @@ -75,7 +59,7 @@ void ObjectFileTests::testBoolAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); bool value1 = true; bool value2 = false; @@ -104,7 +88,7 @@ void ObjectFileTests::testBoolAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_TOKEN)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_SENSITIVE)); @@ -145,7 +129,7 @@ void ObjectFileTests::testULongAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); unsigned long value1 = 0x12345678; unsigned long value2 = 0x87654321; @@ -174,7 +158,7 @@ void ObjectFileTests::testULongAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_MODULUS_BITS)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_PRIME_BITS)); @@ -221,7 +205,7 @@ void ObjectFileTests::testByteStrAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); OSAttribute attr1(value1); OSAttribute attr2(value2); @@ -244,7 +228,7 @@ void ObjectFileTests::testByteStrAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_MODULUS)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_COEFFICIENT)); @@ -284,7 +268,7 @@ void ObjectFileTests::testMechTypeSetAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); std::set set; set.insert(CKM_SHA256); @@ -302,7 +286,7 @@ void ObjectFileTests::testMechTypeSetAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_ALLOWED_MECHANISMS)); @@ -333,7 +317,7 @@ void ObjectFileTests::testAttrMapAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); bool value1 = true; unsigned long value2 = 0x87654321; @@ -361,7 +345,7 @@ void ObjectFileTests::testAttrMapAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_WRAP_TEMPLATE)); CPPUNIT_ASSERT(!testObject.attributeExists(CKA_UNWRAP_TEMPLATE)); @@ -399,7 +383,7 @@ void ObjectFileTests::testMixedAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); bool value1 = true; unsigned long value2 = 0x87654321; @@ -423,7 +407,7 @@ void ObjectFileTests::testMixedAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_TOKEN)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_PRIME_BITS)); @@ -462,7 +446,7 @@ void ObjectFileTests::testDoubleAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); bool value1 = true; unsigned long value2 = 0x87654321; @@ -486,7 +470,7 @@ void ObjectFileTests::testDoubleAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_TOKEN)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_PRIME_BITS)); @@ -513,7 +497,7 @@ void ObjectFileTests::testDoubleAttr() OSAttribute attr4(value4a); // Change the attributes - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_TOKEN, attr1)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_PRIME_BITS, attr2)); @@ -540,7 +524,7 @@ void ObjectFileTests::testDoubleAttr() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_TOKEN)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_PRIME_BITS)); @@ -582,7 +566,7 @@ void ObjectFileTests::testRefresh() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); bool value1 = true; unsigned long value2 = 0x87654321; @@ -606,7 +590,7 @@ void ObjectFileTests::testRefresh() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_TOKEN)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_PRIME_BITS)); @@ -633,7 +617,7 @@ void ObjectFileTests::testRefresh() OSAttribute attr4(value4a); // Change the attributes - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_TOKEN, attr1)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_PRIME_BITS, attr2)); @@ -658,7 +642,7 @@ void ObjectFileTests::testRefresh() ObjectFile testObject2(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject2.isValid()); + CPPUNIT_ASSERT(testObject2.isValid(doRefresh)); // Check the attributes on the second instance CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).isBooleanAttribute()); @@ -683,11 +667,11 @@ void ObjectFileTests::testRefresh() CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).isByteStringAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).getByteStringValue() == id); - // Now check that the first instance also knows about it - CPPUNIT_ASSERT(testObject.isValid()); - CPPUNIT_ASSERT(testObject.attributeExists(CKA_ID)); - CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).isByteStringAttribute()); - CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).getByteStringValue() == id); + // Now check that the first instance also knows about it. If doRequest==false the object will no be changed. + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); + CPPUNIT_ASSERT(testObject.attributeExists(CKA_ID) == doRefresh); + CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).isByteStringAttribute() == doRefresh); + CPPUNIT_ASSERT((testObject.getAttribute(CKA_ID).getByteStringValue() == id) == doRefresh); // Now change another attribute unsigned long value2a = 0x89898989; @@ -700,10 +684,10 @@ void ObjectFileTests::testRefresh() CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a); - // Now check that the second instance also knows about the change - CPPUNIT_ASSERT(testObject2.isValid()); + // Now check that the second instance also knows about the change. If doRequest==false the object will no be changed. + CPPUNIT_ASSERT(testObject2.isValid(doRefresh)); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a) == doRefresh); } } @@ -728,7 +712,7 @@ void ObjectFileTests::testCorruptFile() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(!testObject.isValid()); + CPPUNIT_ASSERT(!testObject.isValid(doRefresh)); } void ObjectFileTests::testTransactions() @@ -740,7 +724,7 @@ void ObjectFileTests::testTransactions() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); bool value1 = true; unsigned long value2 = 0x87654321; @@ -766,7 +750,7 @@ void ObjectFileTests::testTransactions() ObjectFile testObject2(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock"); #endif - CPPUNIT_ASSERT(testObject2.isValid()); + CPPUNIT_ASSERT(testObject2.isValid(doRefresh)); // Check that it has the same attributes CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).isBooleanAttribute()); @@ -813,7 +797,7 @@ void ObjectFileTests::testTransactions() CPPUNIT_ASSERT(testObject.getAttribute(CKA_ALLOWED_MECHANISMS).getMechanismTypeSetValue() == value4a); // Verify that they are unchanged on the other instance - CPPUNIT_ASSERT(testObject2.isValid()); + CPPUNIT_ASSERT(testObject2.isValid(doRefresh)); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE).isByteStringAttribute()); @@ -827,17 +811,17 @@ void ObjectFileTests::testTransactions() // Commit the transaction CPPUNIT_ASSERT(testObject.commitTransaction()); - // Verify that they have now changed on the other instance - CPPUNIT_ASSERT(testObject2.isValid()); + // Verify that they have now changed on the other instance. If doRefresh==false then no change will be done. + CPPUNIT_ASSERT(testObject2.isValid(doRefresh)); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE).isByteStringAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ALLOWED_MECHANISMS).isMechanismTypeSetAttribute()); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).getBooleanValue() == value1a); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE).getByteStringValue() == value3a); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ALLOWED_MECHANISMS).getMechanismTypeSetValue() == value4a); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_TOKEN).getBooleanValue() == value1a) == doRefresh); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a) == doRefresh); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_VALUE).getByteStringValue() == value3a) == doRefresh); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_ALLOWED_MECHANISMS).getMechanismTypeSetValue() == value4a) == doRefresh); // Start transaction on object CPPUNIT_ASSERT(testObject.startTransaction(ObjectFile::ReadWrite)); @@ -860,16 +844,16 @@ void ObjectFileTests::testTransactions() CPPUNIT_ASSERT(testObject.getAttribute(CKA_ALLOWED_MECHANISMS).getMechanismTypeSetValue() == value4); // Verify that they are unchanged on the other instance - CPPUNIT_ASSERT(testObject2.isValid()); + CPPUNIT_ASSERT(testObject2.isValid(doRefresh)); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE).isByteStringAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ALLOWED_MECHANISMS).isMechanismTypeSetAttribute()); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).getBooleanValue() == value1a); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE).getByteStringValue() == value3a); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ALLOWED_MECHANISMS).getMechanismTypeSetValue() == value4a); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_TOKEN).getBooleanValue() == value1a) == doRefresh); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a) == doRefresh); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_VALUE).getByteStringValue() == value3a) == doRefresh); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_ALLOWED_MECHANISMS).getMechanismTypeSetValue() == value4a) == doRefresh); // Abort the transaction CPPUNIT_ASSERT(testObject.abortTransaction()); @@ -885,16 +869,16 @@ void ObjectFileTests::testTransactions() CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE).getByteStringValue() == value3a); CPPUNIT_ASSERT(testObject.getAttribute(CKA_ALLOWED_MECHANISMS).getMechanismTypeSetValue() == value4a); - CPPUNIT_ASSERT(testObject2.isValid()); + CPPUNIT_ASSERT(testObject2.isValid(doRefresh)); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE).isByteStringAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ALLOWED_MECHANISMS).isMechanismTypeSetAttribute()); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).getBooleanValue() == value1a); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE).getByteStringValue() == value3a); - CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ALLOWED_MECHANISMS).getMechanismTypeSetValue() == value4a); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_TOKEN).getBooleanValue() == value1a) == doRefresh); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a) == doRefresh); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_VALUE).getByteStringValue() == value3a) == doRefresh); + CPPUNIT_ASSERT((testObject2.getAttribute(CKA_ALLOWED_MECHANISMS).getMechanismTypeSetValue() == value4a) == doRefresh); } void ObjectFileTests::testDestroyObjectFails() @@ -906,7 +890,7 @@ void ObjectFileTests::testDestroyObjectFails() ObjectFile testObject(NULL, "testdir\\test.object", DEFAULT_UMASK, "testdir\\test.lock", true); #endif - CPPUNIT_ASSERT(testObject.isValid()); + CPPUNIT_ASSERT(testObject.isValid(doRefresh)); OSObject* testIF = (OSObject*) &testObject; diff --git a/src/lib/object_store/test/ObjectFileTests.h b/src/lib/object_store/test/ObjectFileTests.h index 8342a64ce..1e8c4cf07 100644 --- a/src/lib/object_store/test/ObjectFileTests.h +++ b/src/lib/object_store/test/ObjectFileTests.h @@ -33,26 +33,12 @@ #ifndef _SOFTHSM_V2_OBJECTFILETESTS_H #define _SOFTHSM_V2_OBJECTFILETESTS_H -#include -class ObjectFileTests : public CppUnit::TestFixture +class ObjectFileTests { - CPPUNIT_TEST_SUITE(ObjectFileTests); - CPPUNIT_TEST(testBoolAttr); - CPPUNIT_TEST(testULongAttr); - CPPUNIT_TEST(testByteStrAttr); - CPPUNIT_TEST(testMechTypeSetAttr); - CPPUNIT_TEST(testAttrMapAttr); - CPPUNIT_TEST(testMixedAttr); - CPPUNIT_TEST(testDoubleAttr); - CPPUNIT_TEST(testRefresh); - CPPUNIT_TEST(testCorruptFile); - CPPUNIT_TEST(testTransactions); - CPPUNIT_TEST(testDestroyObjectFails); - CPPUNIT_TEST_SUITE_END(); - public: - void testBoolAttr(); + ObjectFileTests(bool doRefresh); + void testBoolAttr(); void testULongAttr(); void testByteStrAttr(); void testMechTypeSetAttr(); @@ -64,8 +50,8 @@ class ObjectFileTests : public CppUnit::TestFixture void testTransactions(); void testDestroyObjectFails(); - void setUp(); - void tearDown(); +private: + const bool doRefresh; }; #endif // !_SOFTHSM_V2_OBJECTFILETESTS_H diff --git a/src/lib/object_store/test/ObjectFileTestsNoRefresh.cpp b/src/lib/object_store/test/ObjectFileTestsNoRefresh.cpp new file mode 100644 index 000000000..ba99110c6 --- /dev/null +++ b/src/lib/object_store/test/ObjectFileTestsNoRefresh.cpp @@ -0,0 +1,56 @@ +/* + * 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. + */ + +/***************************************************************************** + ObjectObjectFileTests.cpp + + Contains test cases to test the object file implementation + *****************************************************************************/ + +#include "ObjectFileTestsNoRefresh.h" + +CPPUNIT_TEST_SUITE_REGISTRATION(ObjectFileTestsNoRefresh); + +ObjectFileTestsNoRefresh::ObjectFileTestsNoRefresh() : ObjectFileTests(false) {} + +void ObjectFileTestsNoRefresh::setUp() +{ +#ifndef _WIN32 + CPPUNIT_ASSERT(!system("mkdir testdir")); +#else + system("mkdir testdir 2> nul"); +#endif +} + +void ObjectFileTestsNoRefresh::tearDown() +{ +#ifndef _WIN32 + CPPUNIT_ASSERT(!system("rm -rf testdir")); +#else + CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul")); +#endif +} + diff --git a/src/lib/object_store/test/ObjectFileTestsNoRefresh.h b/src/lib/object_store/test/ObjectFileTestsNoRefresh.h new file mode 100644 index 000000000..a99ae3e46 --- /dev/null +++ b/src/lib/object_store/test/ObjectFileTestsNoRefresh.h @@ -0,0 +1,65 @@ +/* + * 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. + */ + +/***************************************************************************** + ObjectFileTests.h + + Contains test cases to test the object file implementation + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_OBJECTFILETESTSNOREFRESH_H +#define _SOFTHSM_V2_OBJECTFILETESTSNOREFRESH_H + +#include "ObjectFileTests.h" +#include "test/SessionObjectTests.h" + +#include + +class ObjectFileTestsNoRefresh : public ObjectFileTests , public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(ObjectFileTestsNoRefresh); + CPPUNIT_TEST(testBoolAttr); + CPPUNIT_TEST(testULongAttr); + CPPUNIT_TEST(testByteStrAttr); + CPPUNIT_TEST(testMechTypeSetAttr); + CPPUNIT_TEST(testAttrMapAttr); + CPPUNIT_TEST(testMixedAttr); + CPPUNIT_TEST(testDoubleAttr); + CPPUNIT_TEST(testRefresh); + CPPUNIT_TEST(testCorruptFile); + CPPUNIT_TEST(testTransactions); + CPPUNIT_TEST(testDestroyObjectFails); + CPPUNIT_TEST_SUITE_END(); + +public: + ObjectFileTestsNoRefresh(); + + void setUp(); + void tearDown(); +}; + +#endif // !_SOFTHSM_V2_OBJECTFILETESTSNOREFRESH_H + diff --git a/src/lib/object_store/test/ObjectFileTestsRefresh.cpp b/src/lib/object_store/test/ObjectFileTestsRefresh.cpp new file mode 100644 index 000000000..9cc233a04 --- /dev/null +++ b/src/lib/object_store/test/ObjectFileTestsRefresh.cpp @@ -0,0 +1,56 @@ +/* + * 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. + */ + +/***************************************************************************** + ObjectObjectFileTests.cpp + + Contains test cases to test the object file implementation + *****************************************************************************/ + +#include "ObjectFileTestsRefresh.h" + +CPPUNIT_TEST_SUITE_REGISTRATION(ObjectFileTestsRefresh); + +ObjectFileTestsRefresh::ObjectFileTestsRefresh() : ObjectFileTests(true) {} + +void ObjectFileTestsRefresh::setUp() +{ +#ifndef _WIN32 + CPPUNIT_ASSERT(!system("mkdir testdir")); +#else + system("mkdir testdir 2> nul"); +#endif +} + +void ObjectFileTestsRefresh::tearDown() +{ +#ifndef _WIN32 + CPPUNIT_ASSERT(!system("rm -rf testdir")); +#else + CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul")); +#endif +} + diff --git a/src/lib/object_store/test/ObjectFileTestsRefresh.h b/src/lib/object_store/test/ObjectFileTestsRefresh.h new file mode 100644 index 000000000..491a3e1cd --- /dev/null +++ b/src/lib/object_store/test/ObjectFileTestsRefresh.h @@ -0,0 +1,65 @@ +/* + * 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. + */ + +/***************************************************************************** + ObjectFileTests.h + + Contains test cases to test the object file implementation + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_OBJECTFILETESTSREFRESH_H +#define _SOFTHSM_V2_OBJECTFILETESTSREFRESH_H + +#include "ObjectFileTests.h" +#include "test/SessionObjectTests.h" + +#include + +class ObjectFileTestsRefresh : public ObjectFileTests , public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(ObjectFileTestsRefresh); + CPPUNIT_TEST(testBoolAttr); + CPPUNIT_TEST(testULongAttr); + CPPUNIT_TEST(testByteStrAttr); + CPPUNIT_TEST(testMechTypeSetAttr); + CPPUNIT_TEST(testAttrMapAttr); + CPPUNIT_TEST(testMixedAttr); + CPPUNIT_TEST(testDoubleAttr); + CPPUNIT_TEST(testRefresh); + CPPUNIT_TEST(testCorruptFile); + CPPUNIT_TEST(testTransactions); + CPPUNIT_TEST(testDestroyObjectFails); + CPPUNIT_TEST_SUITE_END(); + +public: + ObjectFileTestsRefresh(); + + void setUp(); + void tearDown(); +}; + +#endif // !_SOFTHSM_V2_OBJECTFILETESTSREFRESH_H +