Source Code Cross Referenced for KSPrivateKeyEntryTest.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.security.*;
024:        import java.security.cert.Certificate;
025:
026:        import org.apache.harmony.security.tests.support.cert.MyCertificate;
027:
028:        import junit.framework.TestCase;
029:
030:        import junit.framework.Test;
031:        import junit.framework.TestSuite;
032:
033:        /**
034:         * Tests for <code>KeyStore.PrivateKeyEntry</code>  class constructor and methods 
035:         * 
036:         */
037:
038:        public class KSPrivateKeyEntryTest extends TestCase {
039:
040:            /**
041:             * Constructor for KSPrivateKeyEntryTest.
042:             * @param arg0
043:             */
044:            public KSPrivateKeyEntryTest(String arg0) {
045:                super (arg0);
046:            }
047:
048:            private PrivateKey testPrivateKey;
049:            private Certificate[] testChain;
050:
051:            private void createParams(boolean diffCerts, boolean diffKeys) {
052:                byte[] encoded = { (byte) 0, (byte) 1, (byte) 2, (byte) 3 };
053:                testChain = new Certificate[5];
054:                for (int i = 0; i < testChain.length; i++) {
055:                    String s = (diffCerts ? Integer.toString(i) : "NEW");
056:                    testChain[i] = new MyCertificate("MY_TEST_CERTIFICATE_"
057:                            .concat(s), encoded);
058:                }
059:                testPrivateKey = (diffKeys ? (PrivateKey) new tmpPrivateKey()
060:                        : (PrivateKey) new tmpPrivateKey(testChain[0]
061:                                .getPublicKey().getAlgorithm()));
062:            }
063:
064:            /**
065:             * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
066:             * constructor
067:             * Assertion: throws NullPointerException when privateKey is null
068:             */
069:            public void testPrivateKeyEntry01() {
070:                Certificate[] certs = new MyCertificate[1];//new Certificate[1];
071:                PrivateKey pk = null;
072:                try {
073:                    new KeyStore.PrivateKeyEntry(pk, certs);
074:                    fail("NullPointerException must be thrown when privateKey is null");
075:                } catch (NullPointerException e) {
076:                }
077:            }
078:
079:            /**
080:             * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
081:             * constructor
082:             * Assertion: throws NullPointerException when chain is null
083:             * and throws IllegalArgumentException when chain length is 0
084:             */
085:            public void testPrivateKeyEntry02() {
086:                Certificate[] chain = null;
087:                PrivateKey pk = new tmpPrivateKey();
088:                try {
089:                    new KeyStore.PrivateKeyEntry(pk, chain);
090:                    fail("NullPointerException must be thrown when chain is null");
091:                } catch (NullPointerException e) {
092:                }
093:                try {
094:                    chain = new Certificate[0];
095:                    new KeyStore.PrivateKeyEntry(pk, chain);
096:                    fail("IllegalArgumentException must be thrown when chain length is 0");
097:                } catch (IllegalArgumentException e) {
098:                }
099:            }
100:
101:            /**
102:             * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
103:             * constructor
104:             * Assertion: throws IllegalArgumentException when chain contains certificates 
105:             * of different types
106:             */
107:            public void testPrivateKeyEntry03() {
108:                createParams(true, false);
109:                try {
110:                    new KeyStore.PrivateKeyEntry(testPrivateKey, testChain);
111:                    fail("IllegalArgumentException must be thrown when chain contains certificates of different types");
112:                } catch (IllegalArgumentException e) {
113:                }
114:            }
115:
116:            /**
117:             * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
118:             * constructor
119:             * Assertion: throws IllegalArgumentException when algorithm of privateKey 
120:             * does not match the algorithm of PublicKey in the end certificate (with 0 index)
121:             */
122:            public void testPrivateKeyEntry04() {
123:                createParams(false, true);
124:                try {
125:                    new KeyStore.PrivateKeyEntry(testPrivateKey, testChain);
126:                    fail("IllegalArgumentException must be thrown when key algorithms do not match");
127:                } catch (IllegalArgumentException e) {
128:                }
129:            }
130:
131:            /**
132:             * Test for <code>getPrivateKey()</code> method
133:             * Assertion: returns PrivateKey object
134:             */
135:            public void testGetPrivateKey() {
136:                createParams(false, false);
137:                KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
138:                        testPrivateKey, testChain);
139:                assertEquals("Incorrect PrivateKey", testPrivateKey, ksPKE
140:                        .getPrivateKey());
141:            }
142:
143:            /**
144:             * Test for <code>getCertificateChain()</code> method Assertion: returns
145:             * array of the Certificates corresponding to chain
146:             */
147:            public void testGetCertificateChain() {
148:                createParams(false, false);
149:                KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
150:                        testPrivateKey, testChain);
151:                Certificate[] res = ksPKE.getCertificateChain();
152:                assertEquals("Incorrect chain length", testChain.length,
153:                        res.length);
154:                for (int i = 0; i < res.length; i++) {
155:                    assertEquals("Incorrect chain element: ".concat(Integer
156:                            .toString(i)), testChain[i], res[i]);
157:                }
158:            }
159:
160:            /**
161:             * Test for <code>getCertificate()</code> method
162:             * Assertion: returns end Certificate (with 0 index in chain)
163:             */
164:            public void testGetCertificate() {
165:                createParams(false, false);
166:                KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
167:                        testPrivateKey, testChain);
168:                Certificate res = ksPKE.getCertificate();
169:                assertEquals("Incorrect end certificate (number 0)",
170:                        testChain[0], res);
171:            }
172:
173:            /**
174:             * Test for <code>toString()</code> method
175:             * Assertion: returns non null String
176:             */
177:            public void testToString() {
178:                createParams(false, false);
179:                KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
180:                        testPrivateKey, testChain);
181:                String res = ksPKE.toString();
182:                assertNotNull("toString() returns null", res);
183:            }
184:
185:            public static Test suite() {
186:                return new TestSuite(KSPrivateKeyEntryTest.class);
187:            }
188:
189:            public static void main(String args[]) {
190:                junit.textui.TestRunner.run(suite());
191:            }
192:
193:            private static class tmpPrivateKey implements  PrivateKey {
194:                private String alg = "My algorithm";
195:
196:                public String getAlgorithm() {
197:                    return alg;
198:                }
199:
200:                public String getFormat() {
201:                    return "My Format";
202:                }
203:
204:                public byte[] getEncoded() {
205:                    return new byte[1];
206:                }
207:
208:                public tmpPrivateKey() {
209:                }
210:
211:                public tmpPrivateKey(String algorithm) {
212:                    super();
213:                    alg = algorithm;
214:                }
215:            }
216:        }
w__ww_.j___av___a2__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.