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 be4240e8e..9e261745f 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