001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.satsa.pki;
028:
029: import com.sun.midp.i3test.TestCase;
030:
031: import javax.microedition.pki.*;
032: import javax.microedition.securityservice.*;
033:
034: import com.sun.cardreader.CardDeviceException;
035: import com.sun.cardreader.SlotFactory;
036:
037: /**
038: * This test case tests PKIManager class.
039: */
040: public class TestPKI extends TestCase {
041:
042: /**
043: * Tests CER generation.
044: */
045: private void testOne() throws java.io.IOException,
046: CardDeviceException {
047: // Parameters for certificate request message.
048: String nameInfo = null;
049: String algorithm = UserCredentialManager.ALGORITHM_RSA;
050: int keyUsage = UserCredentialManager.KEY_USAGE_AUTHENTICATION;
051: int keyLength = 512;
052: String securityElementID = null;
053: String securityElementPrompt = "Please insert PKI security element";
054: boolean forceKeyGen = true;
055: boolean stub_flag = false;
056:
057: try {
058: SlotFactory.init();
059: } catch (CardDeviceException e) {
060: if (e.getMessage().equals("stub")) {
061: stub_flag = true;
062: } else {
063: throw e;
064: }
065: }
066:
067: if (!stub_flag) {
068: byte[] enrollmentRequest = null;
069:
070: // Obtain a certificate enrollment request message.
071: try {
072: enrollmentRequest = UserCredentialManager.generateCSR(
073: nameInfo, algorithm, keyLength, keyUsage,
074: securityElementID, securityElementPrompt,
075: forceKeyGen);
076: assertTrue(true);
077: } catch (UserCredentialManagerException e1) {
078: assertTrue(
079: e1.toString() + " Reason: " + e1.getReason(),
080: false);
081: } catch (CMSMessageSignatureServiceException e2) {
082: assertTrue(
083: e2.toString() + " Reason: " + e2.getReason(),
084: false);
085: } catch (IllegalArgumentException e3) {
086: assertTrue(e3.toString(), false);
087: }
088: } else {
089: assertTrue(true);
090: }
091: }
092:
093: /**
094: * Tests signing.
095: */
096: private void testTwo() throws java.io.IOException,
097: CardDeviceException {
098: // perform test case
099: byte[] authSignature = null;
100: String dataToSign = "JSR 177 Approved";
101: String[] caNames = null;
102: String securityElementPrompt = "Please insert PKI security element";
103: int options = 0;
104: boolean stub_flag = false;
105:
106: try {
107: SlotFactory.init();
108: } catch (CardDeviceException e) {
109: if (e.getMessage().equals("stub")) {
110: stub_flag = true;
111: } else {
112: throw e;
113: }
114: }
115:
116: if (!stub_flag) {
117: try {
118: authSignature = CMSMessageSignatureService.sign(
119: dataToSign, options, caNames,
120: securityElementPrompt + " for sign testing");
121: } catch (CMSMessageSignatureServiceException e1) {
122: assertTrue(e1.toString(), false);
123: } catch (UserCredentialManagerException e2) {
124: assertTrue(e2.toString(), false);
125: }
126: if (authSignature == null) {
127: assertTrue("sign returns null", false);
128: }
129: assertTrue(true);
130: } else {
131: assertTrue(true);
132: }
133: }
134:
135: /**
136: * Run tests.
137: */
138: public void runTests() {
139: try {
140: declare("testOne");
141: testOne();
142:
143: declare("testTwo");
144: testTwo();
145: } catch (Throwable t) {
146: fail("" + t);
147: }
148: }
149:
150: }
|