FileDocCategorySizeDatePackage
TestPKI.javaAPI DocphoneME MR2 API (J2ME)4995Wed May 02 18:00:38 BST 2007com.sun.satsa.pki

TestPKI

public class TestPKI extends com.sun.midp.i3test.TestCase
This test case tests PKIManager class.

Fields Summary
Constructors Summary
Methods Summary
public voidrunTests()
Run tests.

        try {
            declare("testOne");
            testOne();
            
            declare("testTwo");
            testTwo();
        }
        catch (Throwable t) {
            fail("" + t);
        }
    
private voidtestOne()
Tests CER generation.

        // Parameters for certificate request message.
        String nameInfo = null;
        String algorithm = UserCredentialManager.ALGORITHM_RSA;
        int keyUsage = UserCredentialManager.KEY_USAGE_AUTHENTICATION;
        int keyLength = 512;
        String securityElementID = null;
        String securityElementPrompt = "Please insert PKI security element";
        boolean forceKeyGen = true;
        boolean stub_flag = false;
        
        try {
            SlotFactory.init();
        } 
        catch (CardDeviceException e) {
            if (e.getMessage().equals("stub")) {
                stub_flag = true;
            } else {
                throw e;
            }
        }

        if (!stub_flag) {
            byte[] enrollmentRequest = null;
    
            // Obtain a certificate enrollment request message.
            try {
                enrollmentRequest = UserCredentialManager.generateCSR(nameInfo,
                                        algorithm,
                                        keyLength,
                                        keyUsage,
                                        securityElementID,
                                        securityElementPrompt,
                                        forceKeyGen);
                assertTrue(true);
            } catch (UserCredentialManagerException e1) {
                assertTrue(e1.toString() + " Reason: " + e1.getReason(), false);
            } catch (CMSMessageSignatureServiceException e2) {
                assertTrue(e2.toString() + " Reason: " + e2.getReason(), false);
            } catch (IllegalArgumentException e3) {
                assertTrue(e3.toString(), false);
            }
        } else {
            assertTrue(true);
        }
    
private voidtestTwo()
Tests signing.

        // perform test case
        byte[] authSignature = null;
        String dataToSign = "JSR 177 Approved";
        String[] caNames = null;
        String securityElementPrompt = "Please insert PKI security element";
        int options = 0;
        boolean stub_flag = false;
        
        try {
            SlotFactory.init();
        } 
        catch (CardDeviceException e) {
            if (e.getMessage().equals("stub")) {
                stub_flag = true;
            } else {
                throw e;
            }
        }

        if (!stub_flag) {
            try {
                authSignature = CMSMessageSignatureService.sign
                                    (dataToSign,
                                     options,
                                     caNames,
                                 securityElementPrompt + " for sign testing");
            } catch (CMSMessageSignatureServiceException e1) {
                assertTrue(e1.toString(), false);
            } catch (UserCredentialManagerException e2) {
                assertTrue(e2.toString(), false);
            }
            if (authSignature == null) {
                assertTrue("sign returns null", false);
            }
            assertTrue(true);
        } else {
            assertTrue(true);
        }