From 259759007428992d8d86495b8f30227c6f90aaf3 Mon Sep 17 00:00:00 2001 From: "WHQ_NT_DOMAIN\\rm028363" Date: Sat, 24 Feb 2018 14:35:59 -0600 Subject: [PATCH] Refactoring test certificate handling. --- .../sun/jna/platform/win32/Crypt32Test.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/contrib/platform/test/com/sun/jna/platform/win32/Crypt32Test.java b/contrib/platform/test/com/sun/jna/platform/win32/Crypt32Test.java index be4240e8e8..9e261745f9 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/Crypt32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Crypt32Test.java @@ -37,10 +37,36 @@ * @author dblock[at]dblock[dot]org */ public class Crypt32Test extends TestCase { + + /** + * Track if the test certificate was created during the test. + */ + private boolean createdCertificate = false; public static void main(String[] args) { junit.textui.TestRunner.run(Crypt32Test.class); } + + protected void setUp() { + HCERTSTORE hCertStore = Crypt32.INSTANCE.CertOpenSystemStore(Pointer.NULL, "MY"); + WString myString = new WString("cryptsigntest"); + Pointer pvFindPara = new Memory((myString.length() + 1) * 2); + pvFindPara.setString(0, myString); + + CERT_CONTEXT.ByReference pc = Crypt32.INSTANCE.CertFindCertificateInStore(hCertStore, + (WinCrypt.PKCS_7_ASN_ENCODING | WinCrypt.X509_ASN_ENCODING), 0, WinCrypt.CERT_FIND_SUBJECT_STR, + pvFindPara, null); + + if (pc == null) { + createdCertificate = createTestCertificate(); + } + } + + protected void tearDown() { + if(createdCertificate) { + removeTestCertificate(); + } + } public void testCryptProtectUnprotectData() { DATA_BLOB pDataIn = new DATA_BLOB("hello world"); @@ -271,4 +297,17 @@ private boolean createTestCertificate() { return true; } + + private boolean removeTestCertificate() { + try { + KeyStore keyStore = KeyStore.getInstance("Windows-MY", "SunMSCAPI"); + keyStore.load(null, null); + keyStore.deleteEntry("cryptsigntest"); + } catch (Exception e) { + System.out.println("Test certificate deletion failed."); + return false; + } + + return true; + } } \ No newline at end of file