Skip to content

Commit

Permalink
Add more unit test to increase core_pkcs11.c code coverage rate (#195)
Browse files Browse the repository at this point in the history
* Add more unit test to increase core_pkcs11.c code coverage rate
  • Loading branch information
chinglee-iot authored May 16, 2024
1 parent 2ad3f80 commit a0170b8
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 62 deletions.
4 changes: 2 additions & 2 deletions docs/doxygen/include/size_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<tr>
<td>core_pkcs11.c</td>
<td><center>0.8K</center></td>
<td><center>0.8K</center></td>
<td><center>0.7K</center></td>
</tr>
<tr>
<td>core_pki_utils.c</td>
Expand All @@ -25,6 +25,6 @@
<tr>
<td><b>Total estimates</b></td>
<td><b><center>10.3K</center></b></td>
<td><b><center>8.6K</center></b></td>
<td><b><center>8.5K</center></b></td>
</tr>
</table>
81 changes: 36 additions & 45 deletions source/core_pkcs11.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,6 @@

/*-----------------------------------------------------------*/

/** @brief Open a PKCS #11 Session.
*
* \param[out] pxSession Pointer to the session handle to be created.
* \param[out] xSlotId Slot ID to be used for the session.
*
* \return CKR_OK or PKCS #11 error code. (PKCS #11 error codes are positive).
*/
static CK_RV prvOpenSession( CK_SESSION_HANDLE * pxSession,
CK_SLOT_ID xSlotId )
{
CK_RV xResult;
CK_FUNCTION_LIST_PTR pxFunctionList;

xResult = C_GetFunctionList( &pxFunctionList );

if( ( xResult == CKR_OK ) && ( pxFunctionList != NULL ) && ( pxFunctionList->C_OpenSession != NULL ) )
{
xResult = pxFunctionList->C_OpenSession( xSlotId,
CKF_SERIAL_SESSION | CKF_RW_SESSION,
NULL, /* Application defined pointer. */
NULL, /* Callback function. */
pxSession );
}

return xResult;
}

/*-----------------------------------------------------------*/

CK_RV xGetSlotList( CK_SLOT_ID ** ppxSlotId,
CK_ULONG * pxSlotCount )
{
Expand Down Expand Up @@ -193,26 +164,27 @@ CK_RV xInitializePkcs11Token( void )
CK_FLAGS xTokenFlags = 0;
CK_TOKEN_INFO_PTR pxTokenInfo = NULL;

xResult = C_GetFunctionList( &pxFunctionList );
xResult = xInitializePKCS11();

if( ( pxFunctionList == NULL ) || ( pxFunctionList->C_GetTokenInfo == NULL ) || ( pxFunctionList->C_InitToken == NULL ) )
if( ( xResult == CKR_OK ) || ( xResult == CKR_CRYPTOKI_ALREADY_INITIALIZED ) )
{
xResult = CKR_FUNCTION_FAILED;
xResult = xGetSlotList( &pxSlotId, &xSlotCount );
}

if( xResult == CKR_OK )
{
xResult = xInitializePKCS11();
}
xResult = C_GetFunctionList( &pxFunctionList );

if( ( xResult == CKR_OK ) || ( xResult == CKR_CRYPTOKI_ALREADY_INITIALIZED ) )
{
xResult = xGetSlotList( &pxSlotId, &xSlotCount );
if( xResult == CKR_OK )
{
if( ( pxFunctionList == NULL ) || ( pxFunctionList->C_GetTokenInfo == NULL ) || ( pxFunctionList->C_InitToken == NULL ) )
{
xResult = CKR_FUNCTION_FAILED;
}
}
}

if( ( xResult == CKR_OK ) &&
( NULL != pxFunctionList->C_GetTokenInfo ) &&
( NULL != pxFunctionList->C_InitToken ) )
if( xResult == CKR_OK )
{
/* Check if the token requires further initialization. */
/* MISRA Ref 11.5.1 [Void pointer assignment] */
Expand Down Expand Up @@ -270,13 +242,21 @@ CK_RV xInitializePkcs11Session( CK_SESSION_HANDLE * pxSession )
CK_FUNCTION_LIST_PTR pxFunctionList = NULL;
CK_ULONG xSlotCount = 0;

xResult = C_GetFunctionList( &pxFunctionList );

if( pxSession == NULL )
{
xResult = CKR_ARGUMENTS_BAD;
}

if( xResult == CKR_OK )
{
xResult = C_GetFunctionList( &pxFunctionList );

if( ( xResult == CKR_OK ) && ( pxFunctionList == NULL ) )
{
xResult = CKR_FUNCTION_FAILED;
}
}

/* Initialize the module. */
if( xResult == CKR_OK )
{
Expand All @@ -295,19 +275,30 @@ CK_RV xInitializePkcs11Session( CK_SESSION_HANDLE * pxSession )
}

/* Open a PKCS #11 session. */
if( ( xResult == CKR_OK ) && ( pxSlotId != NULL ) && ( xSlotCount >= 1UL ) )
if( ( xResult == CKR_OK ) && ( xSlotCount >= 1UL ) )
{
/* We will take the first slot available.
* If your application has multiple slots, insert logic
* for selecting an appropriate slot here.
*/
xResult = prvOpenSession( pxSession, pxSlotId[ 0 ] );
if( pxFunctionList->C_OpenSession != NULL )
{
xResult = pxFunctionList->C_OpenSession( pxSlotId[ 0 ],
CKF_SERIAL_SESSION | CKF_RW_SESSION,
NULL, /* Application defined pointer. */
NULL, /* Callback function. */
pxSession );
}
else
{
xResult = CKR_FUNCTION_FAILED;
}

/* Free the memory allocated by xGetSlotList. */
pkcs11configPKCS11_FREE( pxSlotId );
}

if( ( xResult == CKR_OK ) && ( pxFunctionList != NULL ) && ( pxFunctionList->C_Login != NULL ) )
if( ( xResult == CKR_OK ) && ( pxFunctionList->C_Login != NULL ) )
{
xResult = pxFunctionList->C_Login( *pxSession,
CKU_USER,
Expand Down
Loading

0 comments on commit a0170b8

Please sign in to comment.