SignerTest.java revision e98fbf8686c5289bf03fe5c3de7ff82d3a77104d
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 /** 47 * Constructor for SignerTest. 48 * @param arg0 49 */ 50 public SignerTest(String arg0) { 51 super(arg0); 52 } 53 54 /** 55 * @tests java.security.Signer#toString() 56 */ 57 public void test_toString() throws Exception { 58 Signer s1 = new SignerStub("testToString1"); 59 assertEquals("[Signer]testToString1", s1.toString()); 60 61 Signer s2 = new SignerStub("testToString2", IdentityScope.getSystemScope()); 62 s2.toString(); 63 64 KeyPair kp = new KeyPair(new PublicKeyStub("public", "SignerTest.testToString", null), new PrivateKeyStub("private", "SignerTest.testToString", null)); 65 s1.setKeyPair(kp); 66 s1.toString(); 67 68 s2.setKeyPair(kp); 69 s2.toString(); 70 } 71 72 /** 73 * verify Signer() creates instance 74 */ 75 public void testSigner() { 76 Signer s = new SignerStub(); 77 assertNotNull(s); 78 //assertNull(s.getName(), s.getName()); 79 assertNull(s.getPrivateKey()); 80 } 81 82 /** 83 * verify Signer(String) creates instance 84 */ 85 public void testSignerString() throws Exception { 86 Signer s = new SignerStub("sss3"); 87 assertNotNull(s); 88 assertEquals("sss3", s.getName()); 89 assertNull(s.getPrivateKey()); 90 } 91 92 /** 93 * verify Signer(String, IdentityScope) creates instance 94 */ 95 public void testSignerStringIdentityScope() throws Exception { 96 Signer s = new SignerStub("sss4", IdentityScope.getSystemScope()); 97 assertNotNull(s); 98 assertEquals("sss4", s.getName()); 99 assertSame(IdentityScope.getSystemScope(), s.getScope()); 100 assertNull(s.getPrivateKey()); 101 } 102 103 /** 104 * verify Signer.getPrivateKey() returns null or private key 105 */ 106 public void testGetPrivateKey() throws Exception { 107 byte [] privateKeyData = { 1, 2, 3, 4, 5}; 108 PrivateKeyStub privateKey = new PrivateKeyStub("private", "fff", privateKeyData); 109 PublicKeyStub publicKey = new PublicKeyStub("public", "fff", null); 110 KeyPair kp = new KeyPair(publicKey, privateKey); 111 112 Signer s = new SignerStub("sss5"); 113 114 assertNull(s.getPrivateKey()); 115 116 s.setKeyPair(kp); 117 assertSame(privateKey, s.getPrivateKey()); 118 } 119 120 /** 121 * verify Signer.getPrivateKey() throws SecurityException if permission is denied 122 */ 123 public void testGetPrivateKey_denied() throws Exception { 124 MySecurityManager sm = new MySecurityManager(); 125 sm.denied.add(new SecurityPermission("getSignerPrivateKey")); 126 System.setSecurityManager(sm); 127 try { 128 Signer s = new SignerStub("sss6"); 129 s.setKeyPair(new KeyPair(new PublicKeyStub("public", "fff", null), new PrivateKeyStub("private", "fff", null))); 130 try { 131 s.getPrivateKey(); 132 fail("SecurityException should be thrown"); 133 } catch (SecurityException ok) {} 134 } finally { 135 System.setSecurityManager(null); 136 } 137 138 } 139 140 /** 141 * @tests java.security.Signer#setKeyPair(java.security.KeyPair) 142 */ 143 public void test_setKeyPairLjava_security_KeyPair() throws Exception { 144 145 // Regression for HARMONY-2408 146 // test: NullPointerException if pair is null 147 try { 148 new SignerStub("name").setKeyPair(null); 149 fail("No expected NullPointerException"); 150 } catch (NullPointerException e) { 151 } 152 153 // test: SecurityException if permission is denied 154 SecurityManager oldSm = System.getSecurityManager(); 155 MySecurityManager sm = new MySecurityManager(); 156 sm.denied.add(new SecurityPermission("setSignerKeyPair")); 157 System.setSecurityManager(sm); 158 try { 159 Signer s = new SignerStub("sss7"); 160 try { 161 s.setKeyPair(new KeyPair(new PublicKeyStub("public", "fff", 162 null), new PrivateKeyStub("private", "fff", null))); 163 fail("SecurityException should be thrown"); 164 } catch (SecurityException ok) { 165 } 166 } finally { 167 System.setSecurityManager(oldSm); 168 } 169 } 170 171} 172