Source Code Cross Referenced for KeyStore_Impl3Test.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.ByteArrayInputStream;
024:        import java.io.ByteArrayOutputStream;
025:        import java.io.IOException;
026:        import java.security.KeyStore;
027:        import java.security.PrivateKey;
028:        import java.security.Provider;
029:        import java.security.Security;
030:        import java.security.cert.Certificate;
031:
032:        import org.apache.harmony.security.tests.support.KeyStoreTestSupport;
033:        import org.apache.harmony.security.tests.support.SpiEngUtils;
034:
035:        import junit.framework.TestCase;
036:
037:        /**
038:         * Tests for <code>KeyStore</code> constructor and methods
039:         * 
040:         */
041:
042:        public class KeyStore_Impl3Test extends TestCase {
043:
044:            private static final String KeyStoreProviderClass = "org.apache.harmony.security.tests.support.MyKeyStore";
045:
046:            private static final String defaultType = "KeyStore";
047:
048:            public static boolean KSSupported = false;
049:
050:            public static String defaultProviderName = null;
051:
052:            public static Provider defaultProvider = null;
053:
054:            private static String NotSupportMsg = "Default KeyStore type is not supported";
055:
056:            Provider mProv;
057:
058:            public KeyStore[] createKS() throws Exception {
059:                assertTrue(NotSupportMsg, KSSupported);
060:                KeyStore[] kpg = new KeyStore[3];
061:
062:                kpg[0] = KeyStore.getInstance(defaultType);
063:                kpg[1] = KeyStore.getInstance(defaultType, defaultProvider);
064:                kpg[2] = KeyStore.getInstance(defaultType, defaultProviderName);
065:                return kpg;
066:            }
067:
068:            protected void setUp() throws Exception {
069:                super .setUp();
070:                mProv = (new SpiEngUtils()).new MyProvider("MyKSProvider",
071:                        "Testing provider", KeyStoreTestSupport.srvKeyStore
072:                                .concat(".").concat(defaultType),
073:                        KeyStoreProviderClass);
074:                Security.insertProviderAt(mProv, 2);
075:                defaultProvider = SpiEngUtils.isSupport(defaultType,
076:                        KeyStoreTestSupport.srvKeyStore);
077:                KSSupported = (defaultProvider != null);
078:                defaultProviderName = (KSSupported ? defaultProvider.getName()
079:                        : null);
080:            }
081:
082:            /*
083:             * @see TestCase#tearDown()
084:             */
085:            protected void tearDown() throws Exception {
086:                super .tearDown();
087:                Security.removeProvider(mProv.getName());
088:            }
089:
090:            /**
091:             * Test for <code>load(InputStream stream, char[] password)</code> 
092:             * <code>store(InputStream stream, char[] password)</code> 
093:             * <code>size()</code>
094:             * <code>getCreationDate(String alias)</code>
095:             * methods 
096:             * Assertions: store(...) throws NullPointerException when stream or
097:             * password is null; 
098:             * getCreationDate(...) throws NullPointerException when alias is null;
099:             * stores KeyStore and then loads it;
100:             * @throws Exception 
101:             */
102:
103:            public void testLoadStore01() throws Exception {
104:                assertTrue(NotSupportMsg, KSSupported);
105:
106:                String tType = "TestType";
107:                KeyStore.TrustedCertificateEntry tCert = new KeyStore.TrustedCertificateEntry(
108:                        new KeyStoreTestSupport.MCertificate("type",
109:                                new byte[0]));
110:
111:                Certificate certs[] = {
112:                        new KeyStoreTestSupport.MCertificate(tType,
113:                                new byte[10]),
114:                        new KeyStoreTestSupport.MCertificate(tType,
115:                                new byte[20]) };
116:                PrivateKey pk = new KeyStoreTestSupport.MyPrivateKey(tType, "",
117:                        new byte[10]);
118:                KeyStore.PrivateKeyEntry pKey = new KeyStore.PrivateKeyEntry(
119:                        pk, certs);
120:                char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
121:                KeyStore.PasswordProtection pPath = new KeyStore.PasswordProtection(
122:                        pwd);
123:                String[] aliases = { "Alias1", "Alias2", "Alias3", "Alias4",
124:                        "Alias5" };
125:
126:                KeyStore[] kss = createKS();
127:                KeyStore[] kss1 = new KeyStore[kss.length];
128:                assertNotNull("KeyStore objects were not created", kss);
129:
130:                for (int i = 0; i < kss.length; i++) {
131:                    kss1[i] = kss[i];
132:                    kss[i].load(null, null);
133:                    kss[i].setEntry(aliases[0], tCert, null);
134:                    kss[i].setEntry(aliases[1], pKey, pPath);
135:                    kss[i].setEntry(aliases[2], pKey, pPath);
136:                    try {
137:                        kss[i].getCreationDate(null);
138:                        fail("NullPointerException should be thrown when alias is null");
139:                    } catch (NullPointerException e) {
140:                    }
141:
142:                    kss[i].setKeyEntry(aliases[3], pk, pwd, certs);
143:                    kss[i].setCertificateEntry(aliases[4], certs[0]);
144:                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
145:                    try {
146:                        kss[i].store(null, pwd);
147:                        fail("IOException or NullPointerException should be thrown when stream is null");
148:                    } catch (IOException e) {
149:                    } catch (NullPointerException e) {
150:                    }
151:
152:                    //RI does not throw exception while password is null.
153:                    kss[i].store(bos, null);
154:
155:                    kss[i].store(bos, pwd);
156:                    ByteArrayInputStream bis = new ByteArrayInputStream(bos
157:                            .toByteArray());
158:                    kss1[i].load(bis, pwd);
159:                    assertEquals("Incorrect size", kss1[i].size(), kss[i]
160:                            .size());
161:                    KeyStore.Entry en, en1;
162:                    for (int j = 0; j < 3; j++) {
163:                        en = kss[i].getEntry(aliases[j],
164:                                (j == 0 ? null : pPath));
165:                        en1 = kss1[i].getEntry(aliases[j], (j == 0 ? null
166:                                : pPath));
167:                        if (en instanceof  KeyStore.TrustedCertificateEntry) {
168:                            assertTrue(
169:                                    "Incorrect entry 1",
170:                                    en1 instanceof  KeyStore.TrustedCertificateEntry);
171:                            assertEquals("Incorrect Certificate",
172:                                    ((KeyStore.TrustedCertificateEntry) en)
173:                                            .getTrustedCertificate(),
174:                                    ((KeyStore.TrustedCertificateEntry) en1)
175:                                            .getTrustedCertificate());
176:                        } else {
177:                            if (en instanceof  KeyStore.PrivateKeyEntry) {
178:                                assertTrue("Incorrect entry 2",
179:                                        en1 instanceof  KeyStore.PrivateKeyEntry);
180:                                assertEquals("Incorrect Certificate",
181:                                        ((KeyStore.PrivateKeyEntry) en)
182:                                                .getPrivateKey(),
183:                                        ((KeyStore.PrivateKeyEntry) en1)
184:                                                .getPrivateKey());
185:                            } else {
186:                                if (en instanceof  KeyStore.SecretKeyEntry) {
187:                                    assertTrue(
188:                                            "Incorrect entry 3",
189:                                            en1 instanceof  KeyStore.SecretKeyEntry);
190:                                    assertEquals("Incorrect Certificate",
191:                                            ((KeyStore.SecretKeyEntry) en)
192:                                                    .getSecretKey(),
193:                                            ((KeyStore.SecretKeyEntry) en1)
194:                                                    .getSecretKey());
195:                                }
196:                            }
197:                        }
198:
199:                        assertEquals("Incorrect date", kss[i]
200:                                .getCreationDate(aliases[j]), kss1[i]
201:                                .getCreationDate(aliases[j]));
202:                    }
203:                    assertEquals("Incorrect entry", kss[i].getKey(aliases[3],
204:                            pwd), kss1[i].getKey(aliases[3], pwd));
205:                    assertEquals("Incorrect date", kss[i]
206:                            .getCreationDate(aliases[3]), kss1[i]
207:                            .getCreationDate(aliases[3]));
208:                    assertEquals("Incorrect entry", kss[i]
209:                            .getCertificate(aliases[4]), kss1[i]
210:                            .getCertificate(aliases[4]));
211:                    assertEquals("Incorrect date", kss[i]
212:                            .getCreationDate(aliases[4]), kss1[i]
213:                            .getCreationDate(aliases[4]));
214:                }
215:            }
216:        }
w___w___w._j___a_v__a_2___s_._c___o___m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.