Pkcs11Interop  4.0.0
HighLevelAPI/_01_InitializeTest.cs
/*
* Copyright 2012-2017 The Pkcs11Interop Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Written for the Pkcs11Interop project by:
* Jaroslav IMRICH <jimrich@jimrich.sk>
*/
using NUnit.Framework;
namespace Net.Pkcs11Interop.Tests.HighLevelAPI
{
/// <summary>
/// Pkcs11 construct, dispose, initialize and finalize tests.
/// </summary>
[TestFixture()]
public class _01_InitializeTest
{
/// <summary>
/// Basic construct and dispose test.
/// </summary>
[Test()]
public void _01_BasicPkcs11DisposeTest()
{
// Unmanaged PKCS#11 library is loaded by the constructor of Pkcs11 class.
// Every PKCS#11 library needs to be initialized with C_Initialize method
// which is also called automatically by the constructor of Pkcs11 class.
Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType);
// Do something interesting
// Unmanaged PKCS#11 library is unloaded by Dispose() method.
// C_Finalize should be the last call made by an application and it
// is also called automatically by Dispose() method.
pkcs11.Dispose();
}
/// <summary>
/// Using statement test.
/// </summary>
[Test()]
public void _02_UsingPkcs11DisposeTest()
{
// Pkcs11 class can be used in using statement which defines a scope
// at the end of which an object will be disposed.
using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType))
{
// Do something interesting
}
}
/// <summary>
/// Example for single-threaded applications.
/// </summary>
[Test()]
public void _03_SingleThreadedInitializeTest()
{
// If an application will not be accessing PKCS#11 library from multiple threads
// simultaneously, it should specify "AppType.SingleThreaded" as a value of "appType" parameter.
using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, AppType.SingleThreaded))
{
// Do something interesting
}
}
/// <summary>
/// Example for multi-threaded applications.
/// </summary>
[Test()]
public void _04_MultiThreadedInitializeTest()
{
// If an application will be accessing PKCS#11 library from multiple threads
// simultaneously, it should specify "AppType.MultiThreaded" as a value of "appType" parameter.
// PKCS#11 library will use the native operation system threading model for locking.
using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, AppType.MultiThreaded))
{
// Do something interesting
}
}
/// <summary>
/// Example for libraries that support C_GetFunctionList()
/// </summary>
[Test()]
public void _05_Pkcs11WithGetFunctionListTest()
{
// Before an application can perform any cryptographic operations with Cryptoki library
// it has to obtain function pointers for all the Cryptoki API routines present in the library.
// This can be done either via C_GetFunctionList() function or via platform specific native
// function - GetProcAddress() on Windows and dlsym() on Unix.
// The most simple constructor of Net.Pkcs11Interop.HighLevelAPI.Pkcs11 class uses
// C_GetFunctionList() approach but Pkcs11Interop also provides an alternative constructor
// that can specify which approach should be used.
using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType, InitType.WithFunctionList))
{
// Do something interesting
}
}
/// <summary>
/// Example for libraries that do not support C_GetFunctionList()
/// </summary>
[Test()]
public void _06_Pkcs11WithoutGetFunctionListTest()
{
// Before an application can perform any cryptographic operations with Cryptoki library
// it has to obtain function pointers for all the Cryptoki API routines present in the library.
// This can be done either via C_GetFunctionList() function or via platform specific native
// function - GetProcAddress() on Windows and dlsym() on Unix.
// The most simple constructor of Net.Pkcs11Interop.HighLevelAPI.Pkcs11 class uses
// C_GetFunctionList() approach but Pkcs11Interop also provides an alternative constructor
// that can specify which approach should be used.
using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType, InitType.WithoutFunctionList))
{
// Do something interesting
}
}
}
}