using System.Collections.Generic;
using NUnit.Framework;
namespace Net.Pkcs11Interop.Tests.HighLevelAPI
{
[TestFixture()]
public class _16_GetAndSetAttributeValueTest
{
[Test()]
public void _01_GetAttributeValueTest()
{
using (IPkcs11Library pkcs11Library = Settings.Factories.Pkcs11LibraryFactory.LoadPkcs11Library(Settings.Factories, Settings.Pkcs11LibraryPath, Settings.AppType))
{
ISlot slot = Helpers.GetUsableSlot(pkcs11Library);
using (ISession session = slot.OpenSession(
SessionType.ReadWrite))
{
session.Login(
CKU.CKU_USER, Settings.NormalUserPin);
IObjectHandle objectHandle = Helpers.CreateDataObject(session);
List<CKA> attributes = new List<CKA>();
attributes.Add(
CKA.CKA_LABEL);
attributes.Add(
CKA.CKA_VALUE);
List<IObjectAttribute> objectAttributes = session.GetAttributeValue(objectHandle, attributes);
Assert.IsTrue(objectAttributes[0].GetValueAsString() == Settings.ApplicationName);
session.DestroyObject(objectHandle);
session.Logout();
}
}
}
[Test()]
public void _02_GetInvalidAttributeValueTest()
{
using (IPkcs11Library pkcs11Library = Settings.Factories.Pkcs11LibraryFactory.LoadPkcs11Library(Settings.Factories, Settings.Pkcs11LibraryPath, Settings.AppType))
{
ISlot slot = Helpers.GetUsableSlot(pkcs11Library);
using (ISession session = slot.OpenSession(
SessionType.ReadWrite))
{
session.Login(
CKU.CKU_USER, Settings.NormalUserPin);
IObjectHandle publicKey = null;
IObjectHandle privateKey = null;
Helpers.GenerateKeyPair(session, out publicKey, out privateKey);
List<CKA> attributes = new List<CKA>();
attributes.Add(
CKA.CKA_LABEL);
attributes.Add(
CKA.CKA_VALUE);
List<IObjectAttribute> objectAttributes = session.GetAttributeValue(privateKey, attributes);
Assert.IsTrue(objectAttributes[0].GetValueAsString() == Settings.ApplicationName);
Assert.IsTrue(objectAttributes[1].CannotBeRead == true);
session.DestroyObject(privateKey);
session.DestroyObject(publicKey);
session.Logout();
}
}
}
[Test()]
public void _03_SetAttributeValueTest()
{
using (IPkcs11Library pkcs11Library = Settings.Factories.Pkcs11LibraryFactory.LoadPkcs11Library(Settings.Factories, Settings.Pkcs11LibraryPath, Settings.AppType))
{
ISlot slot = Helpers.GetUsableSlot(pkcs11Library);
using (ISession session = slot.OpenSession(
SessionType.ReadWrite))
{
session.Login(
CKU.CKU_USER, Settings.NormalUserPin);
IObjectHandle objectHandle = Helpers.CreateDataObject(session);
List<IObjectAttribute> objectAttributes = new List<IObjectAttribute>();
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(
CKA.CKA_LABEL, Settings.ApplicationName +
"_2"));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(
CKA.CKA_VALUE,
"New data object content"));
session.SetAttributeValue(objectHandle, objectAttributes);
session.DestroyObject(objectHandle);
session.Logout();
}
}
}
}
}