public void _01_BasicGetInfoTest() { using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.UseOsLocking)) { LibraryInfo libraryInfo = pkcs11.GetInfo(); // Do something interesting with library information Assert.IsFalse(String.IsNullOrEmpty(libraryInfo.ManufacturerId)); } }
/// <summary> /// Obtains a list of all PKCS#11 URI matching slots /// </summary> /// <param name="pkcs11Uri">PKCS#11 URI</param> /// <param name="pkcs11">High level PKCS#11 wrapper</param> /// <param name="tokenPresent">Flag indicating whether the list obtained includes only those slots with a token present (true), or all slots (false)</param> /// <returns>List of slots matching PKCS#11 URI</returns> public static List <Slot> GetMatchingSlotList(Pkcs11Uri pkcs11Uri, Pkcs11 pkcs11, bool tokenPresent) { if (pkcs11Uri == null) { throw new ArgumentNullException("pkcs11Uri"); } if (pkcs11 == null) { throw new ArgumentNullException("pkcs11"); } List <Slot> matchingSlots = new List <Slot>(); LibraryInfo libraryInfo = pkcs11.GetInfo(); if (!Matches(pkcs11Uri, libraryInfo)) { return(matchingSlots); } List <Slot> slots = pkcs11.GetSlotList(false); if ((slots == null) || (slots.Count == 0)) { return(slots); } foreach (Slot slot in slots) { SlotInfo slotInfo = slot.GetSlotInfo(); if (Matches(pkcs11Uri, slotInfo)) { if (slotInfo.SlotFlags.TokenPresent) { TokenInfo tokenInfo = slot.GetTokenInfo(); if (Matches(pkcs11Uri, tokenInfo)) { matchingSlots.Add(slot); } } else { if (!tokenPresent && Pkcs11UriSharedUtils.Matches(pkcs11Uri, null, null, null, null)) { matchingSlots.Add(slot); } } } } return(matchingSlots); }
public void LibraryInfoMatchesHLA() { using (HLA.Pkcs11 pkcs11 = new HLA.Pkcs11(Settings.Pkcs11LibraryPath, false)) { HLA.LibraryInfo libraryInfo = pkcs11.GetInfo(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:vendor=foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // All attributes matching Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryManufacturer nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = "foobar"; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryDescription nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = "foobar"; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryVersion nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = "0"; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); } }
public void GetMatchingSlotListHLA() { using (HLA.Pkcs11 pkcs11 = new HLA.Pkcs11(Settings.Pkcs11LibraryPath, false)) { // Get all slots List<HLA.Slot> allSlots = pkcs11.GetSlotList(true); Assert.IsTrue(allSlots != null && allSlots.Count > 0); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); List<HLA.Slot> matchedSlots = Pkcs11UriUtils.GetMatchingSlotList(pkcs11uri, pkcs11, true); Assert.IsTrue(matchedSlots.Count == allSlots.Count); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:vendor=foobar"); matchedSlots = Pkcs11UriUtils.GetMatchingSlotList(pkcs11uri, pkcs11, true); Assert.IsTrue(matchedSlots.Count == 0); // All attributes matching one slot HLA.LibraryInfo libraryInfo = pkcs11.GetInfo(); HLA.SlotInfo slotInfo = allSlots[0].GetSlotInfo(); HLA.TokenInfo tokenInfo = allSlots[0].GetTokenInfo(); Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11UriBuilder.SlotManufacturer = slotInfo.ManufacturerId; pkcs11UriBuilder.SlotDescription = slotInfo.SlotDescription; pkcs11UriBuilder.SlotId = slotInfo.SlotId; pkcs11UriBuilder.Token = tokenInfo.Label; pkcs11UriBuilder.Manufacturer = tokenInfo.ManufacturerId; pkcs11UriBuilder.Serial = tokenInfo.SerialNumber; pkcs11UriBuilder.Model = tokenInfo.Model; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); matchedSlots = Pkcs11UriUtils.GetMatchingSlotList(pkcs11uri, pkcs11, true); Assert.IsTrue(matchedSlots.Count == 1); // One attribute nonmatching pkcs11UriBuilder.Serial = "foobar"; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); matchedSlots = Pkcs11UriUtils.GetMatchingSlotList(pkcs11uri, pkcs11, true); Assert.IsTrue(matchedSlots.Count == 0); } }
/// <summary> /// Obtains a list of all PKCS#11 URI matching slots /// </summary> /// <param name="pkcs11Uri">PKCS#11 URI</param> /// <param name="pkcs11">High level PKCS#11 wrapper</param> /// <param name="tokenPresent">Flag indicating whether the list obtained includes only those slots with a token present (true), or all slots (false)</param> /// <returns>List of slots matching PKCS#11 URI</returns> public static List<Slot> GetMatchingSlotList(Pkcs11Uri pkcs11Uri, Pkcs11 pkcs11, bool tokenPresent) { if (pkcs11Uri == null) throw new ArgumentNullException("pkcs11Uri"); if (pkcs11 == null) throw new ArgumentNullException("pkcs11"); List<Slot> matchingSlots = new List<Slot>(); LibraryInfo libraryInfo = pkcs11.GetInfo(); if (!Matches(pkcs11Uri, libraryInfo)) return matchingSlots; List<Slot> slots = pkcs11.GetSlotList(false); if ((slots == null) || (slots.Count == 0)) return slots; foreach (Slot slot in slots) { SlotInfo slotInfo = slot.GetSlotInfo(); if (Matches(pkcs11Uri, slotInfo)) { if (slotInfo.SlotFlags.TokenPresent) { TokenInfo tokenInfo = slot.GetTokenInfo(); if (Matches(pkcs11Uri, tokenInfo)) matchingSlots.Add(slot); } else { if (!tokenPresent && Pkcs11UriSharedUtils.Matches(pkcs11Uri, null, null, null, null)) matchingSlots.Add(slot); } } } return matchingSlots; }
public void DisableLogFileTest() { DeleteEnvironmentVariables(); uint flags = 0; // Delete log file if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); // Log to Pkcs11LoggerLogPath1 with disabled log file System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); flags = flags | PKCS11_LOGGER_FLAG_DISABLE_LOG_FILE; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // Check whether Pkcs11LoggerLogPath1 exists if (File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " exists"); // Log to Pkcs11LoggerLogPath1 with enabled log file flags = flags & ~PKCS11_LOGGER_FLAG_DISABLE_LOG_FILE; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // Check whether Pkcs11LoggerLogPath1 exists if (!File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " does not exist"); }
public void LogFilePathTest() { DeleteEnvironmentVariables(); // Delete log files if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); if (File.Exists(Settings.Pkcs11LoggerLogPath2)) File.Delete(Settings.Pkcs11LoggerLogPath2); // Log to Pkcs11LoggerLogPath1 System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // Check whether Pkcs11LoggerLogPath1 exists if (!File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " does not exist"); if (File.Exists(Settings.Pkcs11LoggerLogPath2)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath2 + " exists"); // Delete log files if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); if (File.Exists(Settings.Pkcs11LoggerLogPath2)) File.Delete(Settings.Pkcs11LoggerLogPath2); // Log to Pkcs11LoggerLogPath2 System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath2); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // Check whether Pkcs11LoggerLogPath2 exists if (File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " exists"); if (!File.Exists(Settings.Pkcs11LoggerLogPath2)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath2 + " does not exist"); // Delete log files if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); if (File.Exists(Settings.Pkcs11LoggerLogPath2)) File.Delete(Settings.Pkcs11LoggerLogPath2); // Settings.LogFilePath may also be null System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, null); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // Check whether Pkcs11LoggerLogPath1 exists if (File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " exists"); if (File.Exists(Settings.Pkcs11LoggerLogPath2)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath2 + " exists"); }
public void LibraryPathTest() { DeleteEnvironmentVariables(); // Delete log file if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); // Existing PKCS#11 library path should work System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // Check whether Pkcs11LoggerLogPath1 exists if (!File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " does not exist"); // Non-existing PKCS#11 library path should not work try { System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, @"NonExistingLibrary.dll"); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11Exception); Assert.IsTrue(((Pkcs11Exception)ex).RV == CKR.CKR_GENERAL_ERROR); } // Unspecified PKCS#11 library should not work try { System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, null); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11Exception); Assert.IsTrue(((Pkcs11Exception)ex).RV == CKR.CKR_GENERAL_ERROR); } }
public void EnvironmentVariableProcessingTest() { DeleteEnvironmentVariables(); // PKCS11_LOGGER_LIBRARY_PATH is required try { System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, null); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, null); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, null); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11Exception); Assert.IsTrue(((Pkcs11Exception)ex).RV == CKR.CKR_GENERAL_ERROR); } // PKCS11_LOGGER_LOG_FILE_PATH and PKCS11_LOGGER_FLAGS are optional System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, null); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, null); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // PKCS11_LOGGER_LIBRARY_PATH has to be provided without enclosing quotes try { System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, "\"" + Settings.Pkcs11LibraryPath + "\""); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, null); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, null); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11Exception); Assert.IsTrue(((Pkcs11Exception)ex).RV == CKR.CKR_GENERAL_ERROR); } // PKCS11_LOGGER_LOG_FILE_PATH has to be provided without enclosing quotes try { System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, "\"" + Settings.Pkcs11LoggerLogPath1 + "\""); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, null); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11Exception); Assert.IsTrue(((Pkcs11Exception)ex).RV == CKR.CKR_GENERAL_ERROR); } // PKCS11_LOGGER_FLAGS must contain a number try { System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, "InvalidValue"); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11Exception); Assert.IsTrue(((Pkcs11Exception)ex).RV == CKR.CKR_GENERAL_ERROR); } }
public void EnableStdOutTest() { DeleteEnvironmentVariables(); uint flags = 0; // Test result needs to be verified visually in NUnit console. // To see stdout NUnit GUI needs to be executed with console switch: "nunit-x86.exe /console" System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); flags = flags | PKCS11_LOGGER_FLAG_ENABLE_STDOUT; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); }
public void EnableFcloseTest() { DeleteEnvironmentVariables(); // Tested file locking behavior is valid only on Windows platform if (!Platform.IsWindows) Assert.Inconclusive("Test cannot be executed on this platform"); uint flags = 0; // Delete log file if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); // Log to Pkcs11LoggerLogPath1 with fclose disabled System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); flags = flags & ~PKCS11_LOGGER_FLAG_ENABLE_FCLOSE; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) { pkcs11.GetInfo(); // Check whether log file exists if (!File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " does not exist"); try { // It should not be possible to delete log file File.Delete(Settings.Pkcs11LoggerLogPath1); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is IOException); } // Check whether log file was not deleted if (!File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " does not exist"); } // Delete log file if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); // Log to Pkcs11LoggerLogPath1 with fclose enabled System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); flags = flags | PKCS11_LOGGER_FLAG_ENABLE_FCLOSE; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) { pkcs11.GetInfo(); // Check whether log file exists if (!File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " does not exist"); // It should be possible to delete log file File.Delete(Settings.Pkcs11LoggerLogPath1); // Check whether log file was deleted if (File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " exists"); pkcs11.GetInfo(); // Check whether log file exists if (!File.Exists(Settings.Pkcs11LoggerLogPath1)) Assert.Fail("File " + Settings.Pkcs11LoggerLogPath1 + " does not exist"); } // Delete log file if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); }
public void EnableFclosePerformanceTest() { DeleteEnvironmentVariables(); uint flags = 0; int fcloseDisabledTicks = 0; int fcloseEnabledTicks = 0; // Delete log file if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); // Log to Pkcs11LoggerLogPath1 with fclose disabled System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); flags = flags & ~PKCS11_LOGGER_FLAG_ENABLE_FCLOSE; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) { int tickCountStart = Environment.TickCount; for (int i = 0; i < 10; i++) { pkcs11.GetInfo(); foreach (Slot slot in pkcs11.GetSlotList(true)) { slot.GetTokenInfo(); foreach (CKM mechanism in slot.GetMechanismList()) { slot.GetMechanismInfo(mechanism); } } } int tickCountStop = Environment.TickCount; fcloseDisabledTicks = tickCountStop - tickCountStart; } // Delete log file if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); // Log to Pkcs11LoggerLogPath1 with fclose enabled System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); flags = flags | PKCS11_LOGGER_FLAG_ENABLE_FCLOSE; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) { int tickCountStart = Environment.TickCount; for (int i = 0; i < 10; i++) { pkcs11.GetInfo(); foreach (Slot slot in pkcs11.GetSlotList(true)) { slot.GetTokenInfo(); foreach (CKM mechanism in slot.GetMechanismList()) { slot.GetMechanismInfo(mechanism); } } } int tickCountStop = Environment.TickCount; fcloseEnabledTicks = tickCountStop - tickCountStart; } // Delete log file if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); // PKCS11_LOGGER_FLAG_ENABLE_FCLOSE decreases performance Assert.IsTrue(fcloseEnabledTicks > fcloseDisabledTicks); }
public void DisableProcessIdAndThreadIdTest() { DeleteEnvironmentVariables(); uint flags = 0; // Delete log file if (File.Exists(Settings.Pkcs11LoggerLogPath1)) File.Delete(Settings.Pkcs11LoggerLogPath1); // Log to Pkcs11LoggerLogPath1 with both IDs enabled System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LIBRARY_PATH, Settings.Pkcs11LibraryPath); System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_LOG_FILE_PATH, Settings.Pkcs11LoggerLogPath1); flags = flags & ~PKCS11_LOGGER_FLAG_DISABLE_PROCESS_ID; flags = flags & ~PKCS11_LOGGER_FLAG_DISABLE_THREAD_ID; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // Read first line from the log file string line = null; using (StreamReader reader = new StreamReader(Settings.Pkcs11LoggerLogPath1)) line = reader.ReadLine(); // Get ProcessID and ThreadID string[] parts = line.Split(new string[] { " : " }, StringSplitOptions.RemoveEmptyEntries); Assert.IsTrue(parts != null && parts.Length == 3); string processId = parts[0]; string threadId = parts[1]; Assert.IsTrue(processId.StartsWith("0x") && threadId.StartsWith("0x")); // Delete log file File.Delete(Settings.Pkcs11LoggerLogPath1); // Log to Pkcs11LoggerLogPath1 with ProcessID disabled flags = flags | PKCS11_LOGGER_FLAG_DISABLE_PROCESS_ID; flags = flags & ~PKCS11_LOGGER_FLAG_DISABLE_THREAD_ID; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // Check if the first line starts with ThreadID using (StreamReader reader = new StreamReader(Settings.Pkcs11LoggerLogPath1)) line = reader.ReadLine(); Assert.IsTrue(line.StartsWith(threadId)); // Delete log file File.Delete(Settings.Pkcs11LoggerLogPath1); // Log to Pkcs11LoggerLogPath1 with both IDs disabled flags = flags | PKCS11_LOGGER_FLAG_DISABLE_PROCESS_ID; flags = flags | PKCS11_LOGGER_FLAG_DISABLE_THREAD_ID; System.Environment.SetEnvironmentVariable(PKCS11_LOGGER_FLAGS, Convert.ToString(flags)); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LoggerLibraryPath, true)) pkcs11.GetInfo(); // Check if both IDs are missing using (StreamReader reader = new StreamReader(Settings.Pkcs11LoggerLogPath1)) line = reader.ReadLine(); Assert.IsTrue(!line.StartsWith(processId) && !line.StartsWith(threadId)); }