SignerTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19* @author Aleksei Y. Semenov 20*/ 21 22package org.apache.harmony.security.tests.java.security; 23import java.security.*; 24import org.apache.harmony.security.tests.support.PrivateKeyStub; 25import org.apache.harmony.security.tests.support.PublicKeyStub; 26import org.apache.harmony.security.tests.support.SignerStub; 27 28import junit.framework.TestCase; 29 30 31 32/** 33 * tests for class Signer 34 * 35 */ 36 37public class SignerTest extends TestCase { 38 39 public static class MySecurityManager extends SecurityManager { 40 public Permissions denied = new Permissions(); 41 public void checkPermission(Permission permission){ 42 if (denied!=null && denied.implies(permission)) throw new SecurityException(); 43 } 44 } 45 46 public static void main(String[] args) { 47 junit.textui.TestRunner.run(SignerTest.class); 48 } 49 50 /** 51 * Constructor for SignerTest. 52 * @param arg0 53 */ 54 public SignerTest(String arg0) { 55 super(arg0); 56 } 57 58 /** 59 * @tests java.security.Signer#toString() 60 */ 61 public void test_toString() throws Exception { 62 Signer s1 = new SignerStub("testToString1"); 63 assertEquals("[Signer]testToString1", s1.toString()); 64 65 Signer s2 = new SignerStub("testToString2", IdentityScope.getSystemScope()); 66 s2.toString(); 67 68 KeyPair kp = new KeyPair(new PublicKeyStub("public", "SignerTest.testToString", null), new PrivateKeyStub("private", "SignerTest.testToString", null)); 69 s1.setKeyPair(kp); 70 s1.toString(); 71 72 s2.setKeyPair(kp); 73 s2.toString(); 74 } 75 76 /** 77 * verify Signer() creates instance 78 */ 79 public void testSigner() { 80 Signer s = new SignerStub(); 81 assertNotNull(s); 82 //assertNull(s.getName(), s.getName()); 83 assertNull(s.getPrivateKey()); 84 } 85 86 /** 87 * verify Signer(String) creates instance 88 */ 89 public void testSignerString() throws Exception { 90 Signer s = new SignerStub("sss3"); 91 assertNotNull(s); 92 assertEquals("sss3", s.getName()); 93 assertNull(s.getPrivateKey()); 94 } 95 96 /** 97 * verify Signer(String, IdentityScope) creates instance 98 */ 99 public void testSignerStringIdentityScope() throws Exception { 100 Signer s = new SignerStub("sss4", IdentityScope.getSystemScope()); 101 assertNotNull(s); 102 assertEquals("sss4", s.getName()); 103 assertSame(IdentityScope.getSystemScope(), s.getScope()); 104 assertNull(s.getPrivateKey()); 105 } 106 107 /** 108 * verify Signer.getPrivateKey() returns null or private key 109 */ 110 public void testGetPrivateKey() throws Exception { 111 byte [] privateKeyData = { 1, 2, 3, 4, 5}; 112 PrivateKeyStub privateKey = new PrivateKeyStub("private", "fff", privateKeyData); 113 PublicKeyStub publicKey = new PublicKeyStub("public", "fff", null); 114 KeyPair kp = new KeyPair(publicKey, privateKey); 115 116 Signer s = new SignerStub("sss5"); 117 118 assertNull(s.getPrivateKey()); 119 120 s.setKeyPair(kp); 121 assertSame(privateKey, s.getPrivateKey()); 122 } 123 124 /** 125 * verify Signer.getPrivateKey() throws SecurityException if permission is denied 126 */ 127 public void testGetPrivateKey_denied() throws Exception { 128 MySecurityManager sm = new MySecurityManager(); 129 sm.denied.add(new SecurityPermission("getSignerPrivateKey")); 130 System.setSecurityManager(sm); 131 try { 132 Signer s = new SignerStub("sss6"); 133 s.setKeyPair(new KeyPair(new PublicKeyStub("public", "fff", null), new PrivateKeyStub("private", "fff", null))); 134 try { 135 s.getPrivateKey(); 136 fail("SecurityException should be thrown"); 137 } catch (SecurityException ok) {} 138 } finally { 139 System.setSecurityManager(null); 140 } 141 142 } 143 144 /** 145 * @tests java.security.Signer#setKeyPair(java.security.KeyPair) 146 */ 147 public void test_setKeyPairLjava_security_KeyPair() throws Exception { 148 149 // Regression for HARMONY-2408 150 // test: NullPointerException if pair is null 151 try { 152 new SignerStub("name").setKeyPair(null); 153 fail("No expected NullPointerException"); 154 } catch (NullPointerException e) { 155 } 156 157 // test: SecurityException if permission is denied 158 SecurityManager oldSm = System.getSecurityManager(); 159 MySecurityManager sm = new MySecurityManager(); 160 sm.denied.add(new SecurityPermission("setSignerKeyPair")); 161 System.setSecurityManager(sm); 162 try { 163 Signer s = new SignerStub("sss7"); 164 try { 165 s.setKeyPair(new KeyPair(new PublicKeyStub("public", "fff", 166 null), new PrivateKeyStub("private", "fff", null))); 167 fail("SecurityException should be thrown"); 168 } catch (SecurityException ok) { 169 } 170 } finally { 171 System.setSecurityManager(oldSm); 172 } 173 } 174 175} 176