public void ConstructorTest()
        {
            // Existing PKCS#11 library
            using (Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(_libraryPath))
                Assert.IsTrue(pkcs11Explorer != null);

            // Non-existing PKCS#11 library
            try
            {
                Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(_incorrectString);
                pkcs11Explorer.Dispose();
                Assert.Fail("Exception expected but not thrown");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is UnmanagedException);
            }

            // Unspecified PKCS#11 library
            try
            {
                Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(null);
                pkcs11Explorer.Dispose();
                Assert.Fail("Exception expected but not thrown");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is ArgumentNullException);
            }
        }
        public void GetTokensTest()
        {
            using (Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(_libraryPath))
            {
                List <Token> tokens = pkcs11Explorer.GetTokens();
                Assert.IsTrue(tokens != null && tokens.Count > 0);

                bool tokenFound = false;

                for (int i = 0; i < tokens.Count; i++)
                {
                    if ((tokens[i].SerialNumber == _tokenSerial) && (tokens[i].Label == _tokenLabel))
                    {
                        tokenFound = true;
                    }

                    Assert.IsFalse(string.IsNullOrEmpty(tokens[i].ManufacturerId));
                    Assert.IsFalse(string.IsNullOrEmpty(tokens[i].Model));
                    Assert.IsFalse(string.IsNullOrEmpty(tokens[i].SerialNumber));
                    Assert.IsFalse(tokens[i].Label == null);
                }

                if (!tokenFound)
                {
                    throw new Exception("Required token not found");
                }
            }
        }
        public void ConstructorTest()
        {
            // Existing PKCS#11 library
            using (Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(_libraryPath))
                Assert.IsTrue(pkcs11Explorer != null);

            // Non-existing PKCS#11 library
            try
            {
                Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(_incorrectString);
                pkcs11Explorer.Dispose();
                Assert.Fail("Exception expected but not thrown");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is UnmanagedException);
            }

            // Unspecified PKCS#11 library
            try
            {
                Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(null);
                pkcs11Explorer.Dispose();
                Assert.Fail("Exception expected but not thrown");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is ArgumentNullException);
            }
        }
        public void GetTokenObjects()
        {
            using (Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(_libraryPath))
            {
                List <Token> tokens = pkcs11Explorer.GetTokens();
                Assert.IsTrue(tokens != null && tokens.Count > 0);

                AsymmetricKeyParameter pubKey = null;
                bool keyFound  = false;
                bool certFound = false;

                foreach (Token token in tokens)
                {
                    List <PrivateKey>  privateKeys  = null;
                    List <Certificate> certificates = null;

                    pkcs11Explorer.GetTokenObjects(token, true, _pin, out privateKeys, out certificates);

                    Assert.IsTrue(privateKeys != null && privateKeys.Count > 0);
                    foreach (PrivateKey privateKey in privateKeys)
                    {
                        if ((privateKey.Id == _ckaId) && (privateKey.Label == _ckaLabel))
                        {
                            pubKey   = privateKey.PublicKey;
                            keyFound = true;
                        }

                        Assert.IsFalse(string.IsNullOrEmpty(privateKey.Id));
                        Assert.IsFalse(string.IsNullOrEmpty(privateKey.Label));
                    }

                    Assert.IsTrue(certificates != null && certificates.Count > 0);
                    foreach (Certificate certificate in certificates)
                    {
                        if ((keyFound == true) && (certificate.PublicKey != null) && (certificate.PublicKey.Equals(pubKey)))
                        {
                            certFound = true;
                        }

                        Assert.IsFalse(string.IsNullOrEmpty(certificate.Id));
                        Assert.IsFalse(string.IsNullOrEmpty(certificate.Label));
                        Assert.IsTrue(certificate.Data != null && certificate.Data.Length > 0);
                    }
                }

                if (!keyFound)
                {
                    throw new Exception("Required private key not found");
                }

                if (!certFound)
                {
                    throw new Exception("Required certificate not found");
                }
            }
        }
        public void GetTokenObjects()
        {
            using (Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(_libraryPath))
            {
                List<Token> tokens = pkcs11Explorer.GetTokens();
                Assert.IsTrue(tokens != null && tokens.Count > 0);

                AsymmetricKeyParameter pubKey = null;
                bool keyFound = false;
                bool certFound = false;

                foreach (Token token in tokens)
                {
                    List<PrivateKey> privateKeys = null;
                    List<Certificate> certificates = null;

                    pkcs11Explorer.GetTokenObjects(token, true, _pin, out privateKeys, out certificates);

                    Assert.IsTrue(privateKeys != null && privateKeys.Count > 0);
                    foreach (PrivateKey privateKey in privateKeys)
                    {
                        if ((privateKey.Id == _ckaId) && (privateKey.Label == _ckaLabel))
                        {
                            pubKey = privateKey.PublicKey;
                            keyFound = true;
                        }

                        Assert.IsFalse(string.IsNullOrEmpty(privateKey.Id));
                        Assert.IsFalse(string.IsNullOrEmpty(privateKey.Label));
                    }

                    Assert.IsTrue(certificates != null && certificates.Count > 0);
                    foreach (Certificate certificate in certificates)
                    {
                        if ((keyFound == true) && (certificate.PublicKey != null) && (certificate.PublicKey.Equals(pubKey)))
                            certFound = true;

                        Assert.IsFalse(string.IsNullOrEmpty(certificate.Id));
                        Assert.IsFalse(string.IsNullOrEmpty(certificate.Label));
                        Assert.IsTrue(certificate.Data != null && certificate.Data.Length > 0);
                    }
                }

                if (!keyFound)
                    throw new Exception("Required private key not found");

                if (!certFound)
                    throw new Exception("Required certificate not found");
            }
        }
        public void GetTokensTest()
        {
            using (Pkcs11Explorer pkcs11Explorer = new Pkcs11Explorer(_libraryPath))
            {
                List<Token> tokens = pkcs11Explorer.GetTokens();
                Assert.IsTrue(tokens != null && tokens.Count > 0);

                bool tokenFound = false;

                for (int i = 0; i < tokens.Count; i++)
                {
                    if ((tokens[i].SerialNumber == _tokenSerial) && (tokens[i].Label == _tokenLabel))
                        tokenFound = true;

                    Assert.IsFalse(string.IsNullOrEmpty(tokens[i].ManufacturerId));
                    Assert.IsFalse(string.IsNullOrEmpty(tokens[i].Model));
                    Assert.IsFalse(string.IsNullOrEmpty(tokens[i].SerialNumber));
                    Assert.IsFalse(string.IsNullOrEmpty(tokens[i].Label));
                }

                if (!tokenFound)
                    throw new Exception("Required token not found");
            }
        }