Skip to content

Commit

Permalink
Refactoring test certificate handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
WHQ_NT_DOMAIN\rm028363 committed Feb 24, 2018
1 parent 5c92a69 commit 2597590
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/Crypt32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 2597590

Please sign in to comment.