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 Aleksei Y. Semenov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.security.*;
024: import org.apache.harmony.security.tests.support.PrivateKeyStub;
025: import org.apache.harmony.security.tests.support.PublicKeyStub;
026: import org.apache.harmony.security.tests.support.SignerStub;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * tests for class Signer
032: *
033: */
034:
035: public class SignerTest extends TestCase {
036:
037: public static class MySecurityManager extends SecurityManager {
038: public Permissions denied = new Permissions();
039:
040: public void checkPermission(Permission permission) {
041: if (denied != null && denied.implies(permission))
042: throw new SecurityException();
043: }
044: }
045:
046: public static void main(String[] args) {
047: junit.textui.TestRunner.run(SignerTest.class);
048: }
049:
050: /**
051: * Constructor for SignerTest.
052: * @param arg0
053: */
054: public SignerTest(String arg0) {
055: super (arg0);
056: }
057:
058: /**
059: * @tests java.security.Signer#toString()
060: */
061: public void test_toString() throws Exception {
062: Signer s1 = new SignerStub("testToString1");
063: assertEquals("[Signer]testToString1", s1.toString());
064:
065: Signer s2 = new SignerStub("testToString2", IdentityScope
066: .getSystemScope());
067: s2.toString();
068:
069: KeyPair kp = new KeyPair(new PublicKeyStub("public",
070: "SignerTest.testToString", null), new PrivateKeyStub(
071: "private", "SignerTest.testToString", null));
072: s1.setKeyPair(kp);
073: s1.toString();
074:
075: s2.setKeyPair(kp);
076: s2.toString();
077: }
078:
079: /**
080: * verify Signer() creates instance
081: */
082: public void testSigner() {
083: Signer s = new SignerStub();
084: assertNotNull(s);
085: //assertNull(s.getName(), s.getName());
086: assertNull(s.getPrivateKey());
087: }
088:
089: /**
090: * verify Signer(String) creates instance
091: */
092: public void testSignerString() throws Exception {
093: Signer s = new SignerStub("sss3");
094: assertNotNull(s);
095: assertEquals("sss3", s.getName());
096: assertNull(s.getPrivateKey());
097: }
098:
099: /**
100: * verify Signer(String, IdentityScope) creates instance
101: */
102: public void testSignerStringIdentityScope() throws Exception {
103: Signer s = new SignerStub("sss4", IdentityScope
104: .getSystemScope());
105: assertNotNull(s);
106: assertEquals("sss4", s.getName());
107: assertSame(IdentityScope.getSystemScope(), s.getScope());
108: assertNull(s.getPrivateKey());
109: }
110:
111: /**
112: * verify Signer.getPrivateKey() returns null or private key
113: */
114: public void testGetPrivateKey() throws Exception {
115: byte[] privateKeyData = { 1, 2, 3, 4, 5 };
116: PrivateKeyStub privateKey = new PrivateKeyStub("private",
117: "fff", privateKeyData);
118: PublicKeyStub publicKey = new PublicKeyStub("public", "fff",
119: null);
120: KeyPair kp = new KeyPair(publicKey, privateKey);
121:
122: Signer s = new SignerStub("sss5");
123:
124: assertNull(s.getPrivateKey());
125:
126: s.setKeyPair(kp);
127: assertSame(privateKey, s.getPrivateKey());
128: }
129:
130: /**
131: * verify Signer.getPrivateKey() throws SecurityException if permission is denied
132: */
133: public void testGetPrivateKey_denied() throws Exception {
134: MySecurityManager sm = new MySecurityManager();
135: sm.denied.add(new SecurityPermission("getSignerPrivateKey"));
136: System.setSecurityManager(sm);
137: try {
138: Signer s = new SignerStub("sss6");
139: s.setKeyPair(new KeyPair(new PublicKeyStub("public", "fff",
140: null), new PrivateKeyStub("private", "fff", null)));
141: try {
142: s.getPrivateKey();
143: fail("SecurityException should be thrown");
144: } catch (SecurityException ok) {
145: }
146: } finally {
147: System.setSecurityManager(null);
148: }
149:
150: }
151:
152: /**
153: * @tests java.security.Signer#setKeyPair(java.security.KeyPair)
154: */
155: public void test_setKeyPairLjava_security_KeyPair()
156: throws Exception {
157:
158: // Regression for HARMONY-2408
159: // test: NullPointerException if pair is null
160: try {
161: new SignerStub("name").setKeyPair(null);
162: fail("No expected NullPointerException");
163: } catch (NullPointerException e) {
164: }
165:
166: // test: SecurityException if permission is denied
167: SecurityManager oldSm = System.getSecurityManager();
168: MySecurityManager sm = new MySecurityManager();
169: sm.denied.add(new SecurityPermission("setSignerKeyPair"));
170: System.setSecurityManager(sm);
171: try {
172: Signer s = new SignerStub("sss7");
173: try {
174: s.setKeyPair(new KeyPair(new PublicKeyStub("public",
175: "fff", null), new PrivateKeyStub("private",
176: "fff", null)));
177: fail("SecurityException should be thrown");
178: } catch (SecurityException ok) {
179: }
180: } finally {
181: System.setSecurityManager(oldSm);
182: }
183: }
184:
185: }
|