Source Code Cross Referenced for KeyStoreTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » security » tests » java » security » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.security.tests.java.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        /**
019:         * @author Vera Y. Petrashkova
020:         * @version $Revision$
021:         */package org.apache.harmony.security.tests.java.security;
022:
023:        import java.io.IOException;
024:        import java.security.KeyStore;
025:        import java.security.KeyStoreException;
026:        import java.security.Provider;
027:        import java.security.Security;
028:        import java.security.SignatureException;
029:        import java.security.Principal;
030:        import java.security.PublicKey;
031:        import java.security.InvalidKeyException;
032:        import java.security.NoSuchProviderException;
033:        import java.security.NoSuchAlgorithmException;
034:        import java.security.cert.Certificate;
035:        import java.security.cert.CertificateException;
036:        import java.security.cert.CertificateEncodingException;
037:        import java.security.cert.CertificateExpiredException;
038:        import java.security.cert.CertificateNotYetValidException;
039:        import java.security.cert.X509Certificate;
040:        import java.util.Date;
041:        import java.util.Set;
042:        import java.math.BigInteger;
043:
044:        import javax.crypto.KeyGenerator;
045:        import javax.crypto.SecretKey;
046:
047:        import org.apache.harmony.security.tests.support.KeyStoreTestSupport;
048:        import org.apache.harmony.security.tests.support.MyLoadStoreParams;
049:        import org.apache.harmony.security.tests.support.SpiEngUtils;
050:
051:        import junit.framework.TestCase;
052:
053:        /**
054:         * Tests for <code>KeyStore</code> constructor and methods
055:         * 
056:         */
057:
058:        public class KeyStoreTest extends TestCase {
059:
060:            private static final String KeyStoreProviderClass = "org.apache.harmony.security.tests.support.MyKeyStore";
061:
062:            private static final String defaultType = "KeyStore";
063:
064:            public static boolean KSSupported = false;
065:
066:            public static String defaultProviderName = null;
067:
068:            public static Provider defaultProvider = null;
069:
070:            private static String NotSupportMsg = "Default KeyStore type is not supported";
071:
072:            Provider mProv;
073:
074:            public KeyStore[] createKS() throws Exception {
075:                assertTrue(NotSupportMsg, KSSupported);
076:                KeyStore[] kpg = new KeyStore[3];
077:
078:                kpg[0] = KeyStore.getInstance(defaultType);
079:                kpg[1] = KeyStore.getInstance(defaultType, defaultProvider);
080:                kpg[2] = KeyStore.getInstance(defaultType, defaultProviderName);
081:                return kpg;
082:            }
083:
084:            protected void setUp() throws Exception {
085:                super .setUp();
086:                mProv = (new SpiEngUtils()).new MyProvider("MyKSProvider",
087:                        "Testing provider", KeyStoreTestSupport.srvKeyStore
088:                                .concat(".").concat(defaultType),
089:                        KeyStoreProviderClass);
090:                Security.insertProviderAt(mProv, 2);
091:                defaultProvider = SpiEngUtils.isSupport(defaultType,
092:                        KeyStoreTestSupport.srvKeyStore);
093:                KSSupported = (defaultProvider != null);
094:                defaultProviderName = (KSSupported ? defaultProvider.getName()
095:                        : null);
096:            }
097:
098:            /*
099:             * @see TestCase#tearDown()
100:             */
101:            protected void tearDown() throws Exception {
102:                super .tearDown();
103:                Security.removeProvider(mProv.getName());
104:            }
105:
106:            /**
107:             * Test for <code>load(LoadStoreParameter param)</code> 
108:             * <code>store(LoadStoreParameter param)</code>
109:             * methods 
110:             * Assertions: throw IllegalArgumentException if param is null;
111:             */
112:            public void testLoadStore02() throws Exception {
113:                assertTrue(NotSupportMsg, KSSupported);
114:
115:                KeyStore[] kss = createKS();
116:                assertNotNull("KeyStore objects were not created", kss);
117:
118:                for (int i = 0; i < kss.length; i++) {
119:                    try {
120:                        kss[i].load(null);
121:                        fail("IOException or IllegalArgumentException should be thrown for null parameter");
122:                    } catch (IOException e) {
123:                    } catch (IllegalArgumentException e) {
124:                    }
125:                    kss[i].load(null, null);
126:                    try {
127:                        kss[i].store(null);
128:                        fail("IOException or IllegalArgumentException should be thrown for null parameter");
129:                    } catch (IOException e) {
130:                    } catch (IllegalArgumentException e) {
131:                    }
132:                }
133:                KeyStore.LoadStoreParameter lParam = new MyLoadStoreParams(
134:                        new KeyStore.PasswordProtection(new char[0]));
135:                for (int i = 0; i < kss.length; i++) {
136:                    kss[i].load(lParam);
137:                    assertEquals("Incorrect result", kss[i].size(), 0);
138:                    kss[i].store(lParam);
139:                }
140:            }
141:
142:            /**
143:             * Test for <code>setKeyEntry(String alias, byte[] key, Certificate[] chain)</code> 
144:             * method 
145:             * Assertion: stores KeyEntry.
146:             */
147:            public void testSetKeyEntry() throws Exception {
148:                assertTrue(NotSupportMsg, KSSupported);
149:
150:                KeyStore[] kss = createKS();
151:                assertNotNull("KeyStore objects were not created", kss);
152:                byte[] kk = { (byte) 1, (byte) 2, (byte) 127, (byte) 77 };
153:                String alias = "keyEntry";
154:                char[] pwd = new char[0];
155:                byte[] res;
156:                Certificate certs[] = {
157:                        new KeyStoreTestSupport.MCertificate(alias, kk),
158:                        new KeyStoreTestSupport.MCertificate(alias, kk) };
159:                for (int i = 0; i < kss.length; i++) {
160:                    kss[i].load(null, null);
161:                    try {
162:                        kss[i].setKeyEntry("proba", null, null);
163:                        fail("KeyStoreException must be thrown");
164:                    } catch (KeyStoreException e) {
165:                    }
166:                    kss[i].setKeyEntry(alias, kk, certs);
167:                    res = kss[i].getKey(alias, pwd).getEncoded();
168:                    assertEquals(kk.length, res.length);
169:                    for (int j = 0; j < res.length; j++) {
170:                        assertEquals(res[j], kk[j]);
171:                    }
172:                    assertEquals(kss[i].getCertificateChain(alias).length,
173:                            certs.length);
174:                    kss[i].setKeyEntry(alias, kk, null);
175:                    res = kss[i].getKey(alias, pwd).getEncoded();
176:                    assertEquals(kk.length, res.length);
177:                    for (int j = 0; j < res.length; j++) {
178:                        assertEquals(res[j], kk[j]);
179:                    }
180:                    assertNull(kss[i].getCertificateChain(alias));
181:                }
182:            }
183:
184:            /**
185:             * Test for <code>getDefaultType()</code> method Assertion: returns
186:             * default security key store type or "jks" string
187:             */
188:            public void testKeyStore01() {
189:                String propName = "keystore.type";
190:                String defKSType = Security.getProperty(propName);
191:                String dType = KeyStore.getDefaultType();
192:                String resType = defKSType;
193:                if (resType == null) {
194:                    resType = defaultType;
195:                }
196:                assertNotNull("Default type have not be null", dType);
197:                assertEquals("Incorrect default type", dType, resType);
198:
199:                if (defKSType == null) {
200:                    Security.setProperty(propName, defaultType);
201:                    dType = KeyStore.getDefaultType();
202:                    resType = Security.getProperty(propName);
203:                    assertNotNull("Incorrect default type", resType);
204:                    assertNotNull("Default type have not be null", dType);
205:                    assertEquals("Incorrect default type", dType, resType);
206:                }
207:            }
208:
209:            /**
210:             * Test for <code>getInstance(String type)</code> method 
211:             * Assertion: 
212:             * throws NullPointerException when type is null 
213:             * throws KeyStoreException when type is not available
214:             * 
215:             */
216:            public void testKeyStore02() throws KeyStoreException {
217:                String[] invalidValues = SpiEngUtils.invalidValues;
218:                try {
219:                    KeyStore.getInstance(null);
220:                    fail("NullPointerException must be thrown when type is null");
221:                } catch (NullPointerException e) {
222:                }
223:                for (int i = 0; i < invalidValues.length; i++) {
224:                    try {
225:                        KeyStore.getInstance(invalidValues[i]);
226:                        fail("KeyStoreException must be thrown (type: ".concat(
227:                                invalidValues[i]).concat(" )"));
228:                    } catch (KeyStoreException e) {
229:                    }
230:                }
231:            }
232:
233:            /**
234:             * @test java.security.KeyStore.PasswordProtection.getPassword()
235:             */
236:            public void testKeyStorePPGetPassword() {
237:                // Regression for HARMONY-1539
238:                // no exception expected
239:                assertNull(new KeyStore.PasswordProtection(null).getPassword());
240:                char[] password = new char[] { 'a', 'b', 'c' };
241:                KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(
242:                        password);
243:                assertNotSame(pp.getPassword(), password);
244:                assertSame(pp.getPassword(), pp.getPassword());
245:
246:            }
247:
248:            /*
249:             * @tests java.security.KeyStoreSpi.engineEntryInstanceOf(String, Class<? extends Entry>)
250:             */
251:            public void testEngineEntryInstanceOf() throws Exception {
252:                //Regression for HARMONY-615
253:
254:                // create a KeyStore 
255:                KeyStore keyStore = KeyStore.getInstance(KeyStore
256:                        .getDefaultType());
257:                keyStore.load(null, "pwd".toCharArray());
258:
259:                // generate a key 
260:                KeyGenerator keyGen = KeyGenerator.getInstance("DES");
261:                keyGen.init(56);
262:                SecretKey secretKey = keyGen.generateKey();
263:
264:                // put the key into keystore 
265:                String alias = "alias";
266:                keyStore.setKeyEntry(alias, secretKey, "pwd".toCharArray(),
267:                        null);
268:
269:                // check if it is a secret key 
270:                assertTrue(keyStore.entryInstanceOf(alias,
271:                        KeyStore.SecretKeyEntry.class));
272:
273:                // check if it is NOT a private key 
274:                assertFalse(keyStore.entryInstanceOf(alias,
275:                        KeyStore.PrivateKeyEntry.class));
276:            }
277:
278:            /**
279:             * @tests java.security.KeyStore.TrustedCertificateEntry.toString()
280:             */
281:            public void testKeyStoreTCToString() {
282:                // Regression for HARMONY-1542
283:                // no exception expected
284:                class TestX509Certificate extends X509Certificate {
285:                    private static final long serialVersionUID = 1L;
286:
287:                    public void checkValidity()
288:                            throws CertificateExpiredException,
289:                            CertificateNotYetValidException {
290:                    }
291:
292:                    public void checkValidity(Date p)
293:                            throws CertificateExpiredException,
294:                            CertificateNotYetValidException {
295:                    }
296:
297:                    public int getVersion() {
298:                        return 0;
299:                    }
300:
301:                    public BigInteger getSerialNumber() {
302:                        return null;
303:                    }
304:
305:                    public Principal getIssuerDN() {
306:                        return null;
307:                    }
308:
309:                    public Principal getSubjectDN() {
310:                        return null;
311:                    }
312:
313:                    public Date getNotBefore() {
314:                        return null;
315:                    }
316:
317:                    public Date getNotAfter() {
318:                        return null;
319:                    }
320:
321:                    public byte[] getTBSCertificate()
322:                            throws CertificateEncodingException {
323:                        return null;
324:                    }
325:
326:                    public byte[] getSignature() {
327:                        return null;
328:                    }
329:
330:                    public String getSigAlgName() {
331:                        return null;
332:                    }
333:
334:                    public String getSigAlgOID() {
335:                        return null;
336:                    }
337:
338:                    public byte[] getSigAlgParams() {
339:                        return null;
340:                    }
341:
342:                    public boolean[] getIssuerUniqueID() {
343:                        return null;
344:                    }
345:
346:                    public boolean[] getSubjectUniqueID() {
347:                        return null;
348:                    }
349:
350:                    public boolean[] getKeyUsage() {
351:                        return null;
352:                    }
353:
354:                    public int getBasicConstraints() {
355:                        return 0;
356:                    }
357:
358:                    public byte[] getEncoded()
359:                            throws CertificateEncodingException {
360:                        return null;
361:                    }
362:
363:                    public void verify(PublicKey p)
364:                            throws CertificateException,
365:                            NoSuchAlgorithmException, InvalidKeyException,
366:                            NoSuchProviderException, SignatureException {
367:                    }
368:
369:                    public void verify(PublicKey p0, String p1)
370:                            throws CertificateException,
371:                            NoSuchAlgorithmException, InvalidKeyException,
372:                            NoSuchProviderException, SignatureException {
373:                    }
374:
375:                    public String toString() {
376:                        return null;
377:                    }
378:
379:                    public PublicKey getPublicKey() {
380:                        return null;
381:                    }
382:
383:                    public boolean hasUnsupportedCriticalExtension() {
384:                        return false;
385:                    }
386:
387:                    public Set getCriticalExtensionOIDs() {
388:                        return null;
389:                    }
390:
391:                    public Set getNonCriticalExtensionOIDs() {
392:                        return null;
393:                    }
394:
395:                    public byte[] getExtensionValue(String p) {
396:                        return null;
397:                    }
398:                }
399:                assertNotNull(new KeyStore.TrustedCertificateEntry(
400:                        new TestX509Certificate()).toString());
401:            }
402:        }
www_._j__a___v_a_2s.c_o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.