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: }
|