Source Code Cross Referenced for CodeSource_ImplTest.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 Alexander V. Astapchuk
020:         * @version $Revision$
021:         */package org.apache.harmony.security.tests.java.security;
022:
023:        import java.net.MalformedURLException;
024:        import java.net.URL;
025:        import java.security.CodeSigner;
026:        import java.security.CodeSource;
027:        import java.security.Provider;
028:        import java.security.Security;
029:        import java.security.cert.Certificate;
030:        import java.security.cert.CertificateException;
031:        import java.security.cert.CertificateFactory;
032:        import java.security.cert.X509Certificate;
033:        import java.util.ArrayList;
034:
035:        import javax.security.auth.x500.X500Principal;
036:
037:        import org.apache.harmony.security.tests.support.TestCertUtils;
038:
039:        import junit.framework.TestCase;
040:
041:        /**
042:         * Unit test for CodeSource.
043:         * 
044:         */
045:
046:        public class CodeSource_ImplTest extends TestCase {
047:
048:            /* Below are various URLs used during the testing */
049:            private static URL urlSite;
050:
051:            static {
052:                try {
053:                    String siteName = "www.intel.com";
054:                    urlSite = new URL("http://" + siteName + "");
055:                } catch (MalformedURLException ex) {
056:                    throw new Error(ex);
057:                }
058:            }
059:
060:            /**
061:             * Test for equals(Object)<br>
062:             * The signer certificate may contain nulls, and equals() must not fail with NPE 
063:             */
064:            public void testEqualsObject_03() {
065:                Certificate cert0 = new TestCertUtils.TestCertificate();
066:                Certificate[] certs0 = new Certificate[] { cert0, null };
067:                Certificate[] certs1 = new Certificate[] { null, cert0 };
068:                CodeSource thiz = new CodeSource(urlSite, certs0);
069:                CodeSource that = new CodeSource(urlSite, certs1);
070:                assertTrue(thiz.equals(that));
071:            }
072:
073:            /**
074:             * Test for equals(Object)<br>
075:             * Checks that both 'null' and not-null certs are taken into account - properly. 
076:             */
077:            public void testEqualsObject_05() {
078:                Certificate cert0 = new TestCertUtils.TestCertificate("cert0");
079:                Certificate cert1 = new TestCertUtils.TestCertificate("cert1");
080:                Certificate cert2 = new TestCertUtils.TestCertificate("cert2");
081:
082:                Certificate[] smallSet = new Certificate[] { cert0, cert1 };
083:                Certificate[] bigSet = new Certificate[] { cert0, cert1, cert2 };
084:
085:                CodeSource thiz = new CodeSource(urlSite, smallSet);
086:                CodeSource that = new CodeSource(urlSite, (Certificate[]) null);
087:                assertFalse(thiz.equals(that));
088:
089:                that = new CodeSource(urlSite, bigSet);
090:                assertFalse(thiz.equals(that));
091:
092:                thiz = new CodeSource(urlSite, bigSet);
093:                that = new CodeSource(urlSite, smallSet);
094:                assertFalse(thiz.equals(that));
095:
096:                thiz = new CodeSource(urlSite, (Certificate[]) null);
097:                that = new CodeSource(urlSite, /*any set*/smallSet);
098:                assertFalse(thiz.equals(that));
099:                assertFalse(that.equals(thiz));
100:            }
101:
102:            /**
103:             * getCodeSigners() must not take into account non-X509 certificates.
104:             */
105:            public void testGetCodeSigners_01() {
106:                Certificate[] certs = { new TestCertUtils.TestCertificate("00") };
107:                CodeSource cs = new CodeSource(null, certs);
108:                assertNull(cs.getCodeSigners());
109:            }
110:
111:            /**
112:             * getCodeSigners() must return null if no X509 factory available
113:             */
114:            public void testGetCodeSigners_02() {
115:                ArrayList al = new ArrayList();
116:                boolean noMoreFactories = false;
117:                try {
118:                    // remove all providers for x509
119:                    // 'for' loop here for the sake of avoiding endless loop - well, just 
120:                    // in case if something is wrong with add/remove machinery.
121:                    // '100' seems reasonable big to remove all necessary providers
122:                    // ...
123:                    for (int i = 0; i < 100; i++) {
124:                        try {
125:                            CertificateFactory f = CertificateFactory
126:                                    .getInstance("X.509");
127:                            al.add(f.getProvider());
128:                            Security.removeProvider(f.getProvider().getName());
129:                        } catch (CertificateException ex) {
130:                            noMoreFactories = true;
131:                            break;
132:                        }
133:                    }
134:                    if (!noMoreFactories) {
135:                        throw new Error(
136:                                "Unable to setup test: too many providers provide X.509");
137:                    }
138:                    Certificate[] certs = new Certificate[] { TestCertUtils.rootCA };
139:                    CodeSource cs = new CodeSource(null, certs);
140:                    assertNull(cs.getCodeSigners());
141:                } finally {
142:                    // .. and restore providers back - to avoid affecting following tests
143:                    for (int i = 0; i < al.size(); i++) {
144:                        Security.addProvider((Provider) al.get(i));
145:                    }
146:                }
147:
148:            }
149:
150:            /**
151:             * getCodeSigners() must return an array of CodeSigners. Just make sure
152:             * the array looks healthy.
153:             */
154:            public void testGetCodeSigners_03() {
155:                TestCertUtils.install_test_x509_factory();
156:                try {
157:                    X500Principal[] ps = TestCertUtils.UniGen.genX500s(3);
158:
159:                    // 2-certs chain 
160:                    X509Certificate rootCA = TestCertUtils.rootCA;
161:                    X509Certificate c0 = new TestCertUtils.TestX509Certificate(
162:                            ps[0], rootCA.getIssuerX500Principal());
163:                    //
164:                    X509Certificate c1 = new TestCertUtils.TestX509Certificate(
165:                            ps[1], ps[1]);
166:                    X509Certificate c2 = new TestCertUtils.TestX509Certificate(
167:                            ps[2], ps[2]);
168:
169:                    java.security.cert.Certificate[] certs = new java.security.cert.Certificate[] {
170:                            c0, rootCA, c1, c2 };
171:                    CodeSource cs = new CodeSource(null, certs);
172:                    CodeSigner[] signers = cs.getCodeSigners();
173:
174:                    // must get exactly 3 CodeSigner-s: one for the chain, and one 
175:                    // for each of single certs
176:                    assertEquals(3, signers.length);
177:                } finally {
178:                    TestCertUtils.uninstall_test_x509_factory();
179:                }
180:            }
181:
182:            /**
183:             * getCodeSigners(). Make sure, that CertException is handled properly
184:             */
185:            public void testGetCodeSigners_04() {
186:                try {
187:                    TestCertUtils.install_test_x509_factory();
188:                    X500Principal[] ps = TestCertUtils.UniGen.genX500s(1);
189:
190:                    // 2-certs chain 
191:                    X509Certificate rootCA = TestCertUtils.rootCA;
192:                    X509Certificate c0 = new TestCertUtils.TestInvalidX509Certificate(
193:                            ps[0], rootCA.getIssuerX500Principal());
194:                    java.security.cert.Certificate[] certs = new java.security.cert.Certificate[] {
195:                            c0, rootCA };
196:
197:                    CodeSource cs = new CodeSource(null, certs);
198:                    CodeSigner[] signers = cs.getCodeSigners();
199:
200:                    assertNull(signers);
201:
202:                    // Must force a check for 'factory==null' 
203:                    cs.getCodeSigners();
204:                } finally {
205:                    TestCertUtils.uninstall_test_x509_factory();
206:                }
207:            }
208:
209:            /**
210:             * @tests java.security.CodeSource#toString()
211:             */
212:            public void test_toString() throws Exception {
213:                // Test for method java.lang.String java.security.CodeSource.toString()
214:                CodeSource cs = new CodeSource(
215:                        new java.net.URL("file:///test"), (Certificate[]) null);
216:                assertEquals("toString did not return expected value.",
217:                        "CodeSource, url=file:/test, <no certificates>", cs
218:                                .toString());
219:            }
220:
221:        }
w_w__w_.j_a_va___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.