SocketPermissionTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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 18package tests.api.java.net; 19 20import java.net.InetAddress; 21import java.net.SocketPermission; 22import java.net.UnknownHostException; 23import java.security.PermissionCollection; 24 25import tests.support.Support_Configuration; 26 27public class SocketPermissionTest extends junit.framework.TestCase { 28 29 String starName = "*." + Support_Configuration.DomainAddress; 30 31 String wwwName = Support_Configuration.HomeAddress; 32 33 SocketPermission star_Resolve = new SocketPermission(starName, "resolve"); 34 35 SocketPermission star_All = new SocketPermission(starName, 36 "listen,accept,connect"); 37 38 SocketPermission www_All = new SocketPermission(wwwName, 39 "connect,listen,accept"); 40 41 SocketPermission copyOfWww_All = new SocketPermission(wwwName, 42 "connect,listen,accept"); 43 44 /** 45 * @tests java.net.SocketPermission#SocketPermission(java.lang.String, 46 * java.lang.String) 47 */ 48 public void test_ConstructorLjava_lang_StringLjava_lang_String() { 49 // Test for method java.net.SocketPermission(java.lang.String, 50 // java.lang.String) 51 assertTrue("Incorrect name", star_Resolve.getName().equals(starName)); 52 assertEquals("Incorrect actions", 53 "resolve", star_Resolve.getActions()); 54 55 SocketPermission sp1 = new SocketPermission("", "connect"); 56 assertEquals("Wrong name1", "localhost", sp1.getName()); 57 SocketPermission sp2 = new SocketPermission(":80", "connect"); 58 assertEquals("Wrong name2", ":80", sp2.getName()); 59 60 // regression for HARMONY-1462 61 SocketPermission sp3 = new SocketPermission("localhost:*", "listen"); 62 assertEquals("Wrong name3", "localhost:*", sp3.getName()); 63 // for all ports 64 SocketPermission spAllPorts = new SocketPermission("localhost:0-65535", 65 "listen"); 66 assertTrue("Port range error", sp3.implies(spAllPorts)); 67 assertTrue("Port range error", spAllPorts.implies(sp3)); 68 } 69 70 /** 71 * @tests java.net.SocketPermission#equals(java.lang.Object) 72 */ 73 public void test_equalsLjava_lang_Object() { 74 // Test for method boolean 75 // java.net.SocketPermission.equals(java.lang.Object) 76 assertTrue("Different names but returned equal", !star_All 77 .equals(www_All)); 78 assertTrue("Different actions but returned equal", !star_Resolve 79 .equals(star_All)); 80 assertTrue("Same but returned unequal", www_All.equals(copyOfWww_All)); 81 assertTrue("Returned true when compared to a String", !www_All 82 .equals(www_All.toString())); 83 84 SocketPermission sp1 = new SocketPermission("TEST1.com", 85 "resolve,connect"); 86 SocketPermission sp2 = new SocketPermission("test1.com", 87 "resolve,connect"); 88 assertTrue("Different cases should be equal", sp1.equals(sp2)); 89 90 // Regression for HARMONY-1524 91 assertFalse(sp1.equals(null)); 92 93 // Regression for HARMONY-3333 94 sp1 = new SocketPermission("TEST1.com:333", "resolve"); 95 sp2 = new SocketPermission("test1.com:444", "resolve"); 96 assertTrue("Different cases should be equal", sp1.equals(sp2)); 97 } 98 99 /** 100 * @tests java.net.SocketPermission#equals(java.lang.Object) 101 */ 102 public void test_equalsLjava_lang_Object_subtest0() { 103 SocketPermission sp1 = new SocketPermission( 104 Support_Configuration.InetTestAddress, "resolve,connect"); 105 SocketPermission sp2 = new SocketPermission( 106 Support_Configuration.InetTestIP, "resolve,connect"); 107 assertTrue("Same IP address should be equal", sp1.equals(sp2)); 108 109 } 110 111 /** 112 * @tests java.net.SocketPermission#getActions() 113 */ 114 public void test_getActions() { 115 // Test for method java.lang.String 116 // java.net.SocketPermission.getActions() 117 assertEquals("Incorrect actions", 118 "resolve", star_Resolve.getActions()); 119 assertEquals("Incorrect actions/not in canonical form", "connect,listen,accept,resolve", star_All 120 .getActions()); 121 } 122 123 /** 124 * @tests java.net.SocketPermission#implies(java.security.Permission) 125 */ 126 public void test_impliesLjava_security_Permission() { 127 // Test for method boolean 128 // java.net.SocketPermission.implies(java.security.Permission) 129 assertTrue("All should imply resolve", star_All.implies(star_Resolve)); 130 131 // regression for HARMONY-1200 132 assertFalse("Null should not be implied", star_All.implies((SocketPermission)null)); 133 134 assertTrue("Equals should imply eachother", www_All 135 .implies(copyOfWww_All)); 136 assertTrue("Wild should imply normal", star_All.implies(www_All)); 137 assertTrue("Normal shouldn't imply wildcard", !www_All 138 .implies(star_Resolve)); 139 assertTrue("Resolve shouldn't imply all", !star_Resolve 140 .implies(star_All)); 141 SocketPermission p1 = new SocketPermission(wwwName + ":80-81", 142 "connect"); 143 SocketPermission p2 = new SocketPermission(wwwName + ":80", "connect"); 144 assertTrue("Port 80 is implied by 80-81", p1.implies(p2)); 145 p1 = new SocketPermission(wwwName + ":79-80", "connect"); 146 assertTrue("Port 80 is implied by 79-80", p1.implies(p2)); 147 p1 = new SocketPermission(wwwName + ":79-81", "connect"); 148 assertTrue("Port 80 is implied by 79-81", p1.implies(p2)); 149 p2 = new SocketPermission(wwwName + ":79-80", "connect"); 150 assertTrue("Port 79-80 is implied by 79-81", p1.implies(p2)); 151 p2 = new SocketPermission(wwwName, "resolve"); 152 assertTrue( 153 "Any identical host should imply resolve regardless of the ports", 154 p1.implies(p2)); 155 156 SocketPermission sp1 = new SocketPermission("www.Ibm.com", "resolve"); 157 SocketPermission sp2 = new SocketPermission("www.IBM.com", "resolve"); 158 assertTrue("SocketPermission is case sensitive", sp1.implies(sp2)); 159 160 SocketPermission sp3 = new SocketPermission("*.ibm.com", "resolve"); 161 assertTrue("SocketPermission wildcard is case sensitive", sp3 162 .implies(sp2)); 163 164 InetAddress host = null; 165 try { 166 host = InetAddress.getByName(Support_Configuration.UnresolvedIP); 167 } catch (UnknownHostException e) { 168 } 169 170 SocketPermission perm1 = new SocketPermission( 171 Support_Configuration.UnresolvedIP, "connect"); 172 SocketPermission perm2 = new SocketPermission( 173 Support_Configuration.UnresolvedIP + ":80", "connect"); 174 assertTrue("should imply port 80", perm1.implies(perm2)); 175 PermissionCollection col = perm1.newPermissionCollection(); 176 col.add(perm1); 177 assertTrue("collection should imply port 80", col.implies(perm2)); 178 179 } 180 181 /** 182 * @tests java.net.SocketPermission#newPermissionCollection() 183 */ 184 public void test_newPermissionCollection() { 185 // Test for method java.security.PermissionCollection 186 // java.net.SocketPermission.newPermissionCollection() 187 java.security.PermissionCollection pc = star_Resolve 188 .newPermissionCollection(); 189 pc.add(star_Resolve); 190 pc.add(www_All); 191 assertTrue("Should imply all on " + wwwName, pc.implies(www_All)); 192 assertTrue("Should imply resolve on " + starName, pc 193 .implies(star_Resolve)); 194 assertTrue("Should not imply all on " + starName, !pc.implies(star_All)); 195 196 // wipe out pc 197 pc = star_Resolve.newPermissionCollection(); 198 pc.add(star_All); 199 assertTrue("Should imply resolve on " + starName, pc 200 .implies(star_Resolve)); 201 assertTrue("Should imply all on " + wwwName, pc.implies(www_All)); 202 203 pc = star_Resolve.newPermissionCollection(); 204 SocketPermission p1 = new SocketPermission(wwwName + ":79-80", 205 "connect"); 206 pc.add(p1); 207 SocketPermission p2 = new SocketPermission(wwwName, "resolve"); 208 assertTrue( 209 "Any identical host should imply resolve regardless of the ports", 210 pc.implies(p2)); 211 assertTrue("A different host should not imply resolve", !pc 212 .implies(star_Resolve)); 213 } 214 215 /** 216 * Sets up the fixture, for example, open a network connection. This method 217 * is called before a test is executed. 218 */ 219 protected void setUp() { 220 } 221 222 /** 223 * Tears down the fixture, for example, close a network connection. This 224 * method is called after a test is executed. 225 */ 226 protected void tearDown() { 227 } 228} 229