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: package org.apache.harmony.luni.tests.java.net;
019:
020: import java.net.InetAddress;
021: import java.net.SocketPermission;
022: import java.net.UnknownHostException;
023: import java.security.PermissionCollection;
024:
025: import tests.support.Support_Configuration;
026:
027: public class SocketPermissionTest extends junit.framework.TestCase {
028:
029: String starName = "*." + Support_Configuration.DomainAddress;
030:
031: String wwwName = Support_Configuration.HomeAddress;
032:
033: SocketPermission star_Resolve = new SocketPermission(starName,
034: "resolve");
035:
036: SocketPermission star_All = new SocketPermission(starName,
037: "listen,accept,connect");
038:
039: SocketPermission www_All = new SocketPermission(wwwName,
040: "connect,listen,accept");
041:
042: SocketPermission copyOfWww_All = new SocketPermission(wwwName,
043: "connect,listen,accept");
044:
045: /**
046: * @tests java.net.SocketPermission#SocketPermission(java.lang.String,
047: * java.lang.String)
048: */
049: public void test_ConstructorLjava_lang_StringLjava_lang_String() {
050: assertEquals("Incorrect name", starName, star_Resolve.getName());
051: assertEquals("Incorrect actions", "resolve", star_Resolve
052: .getActions());
053:
054: SocketPermission sp1 = new SocketPermission("", "connect");
055: assertEquals("Wrong name1", "localhost", sp1.getName());
056: SocketPermission sp2 = new SocketPermission(":80", "connect");
057: assertEquals("Wrong name2", ":80", sp2.getName());
058:
059: // Regression for HARMONY-1462
060: SocketPermission sp3 = new SocketPermission("localhost:*",
061: "listen");
062: assertEquals("Wrong name3", "localhost:*", sp3.getName());
063: // For all ports
064: SocketPermission spAllPorts = new SocketPermission(
065: "localhost:0-65535", "listen");
066: assertTrue("Port range error", sp3.implies(spAllPorts));
067: assertTrue("Port range error", spAllPorts.implies(sp3));
068: }
069:
070: /**
071: * @tests java.net.SocketPermission#equals(java.lang.Object)
072: */
073: public void test_equalsLjava_lang_Object() {
074: assertTrue("Different names but returned equal", !star_All
075: .equals(www_All));
076: assertTrue("Different actions but returned equal",
077: !star_Resolve.equals(star_All));
078: assertTrue("Same but returned unequal", www_All
079: .equals(copyOfWww_All));
080: assertTrue("Returned true when compared to a String", !www_All
081: .equals(www_All.toString()));
082:
083: SocketPermission sp1 = new SocketPermission("TEST1.com",
084: "resolve,connect");
085: SocketPermission sp2 = new SocketPermission("test1.com",
086: "resolve,connect");
087: assertTrue("Different cases should be equal", sp1.equals(sp2));
088:
089: // Regression for HARMONY-1524
090: assertFalse(sp1.equals(null));
091:
092: // Regression for HARMONY-3333
093: sp1 = new SocketPermission("TEST1.com:333", "resolve");
094: sp2 = new SocketPermission("test1.com:444", "resolve");
095: assertTrue("Different cases should be equal", sp1.equals(sp2));
096:
097: sp1 = new SocketPermission(
098: Support_Configuration.InetTestAddress,
099: "resolve,connect");
100: sp2 = new SocketPermission(Support_Configuration.InetTestIP,
101: "resolve,connect");
102: assertEquals("Same IP address should be equal", sp1, sp2);
103: }
104:
105: /**
106: * @tests java.net.SocketPermission#getActions()
107: */
108: public void test_getActions() {
109: assertEquals("Incorrect actions", "resolve", star_Resolve
110: .getActions());
111: assertEquals("Incorrect actions/not in canonical form",
112: "connect,listen,accept,resolve", star_All.getActions());
113: }
114:
115: /**
116: * @tests java.net.SocketPermission#implies(java.security.Permission)
117: */
118: public void test_impliesLjava_security_Permission() {
119: assertTrue("All should imply resolve", star_All
120: .implies(star_Resolve));
121:
122: // Regression for HARMONY-1200
123: assertFalse("Null should not be implied", star_All
124: .implies((SocketPermission) null));
125:
126: assertTrue("Equals should imply eachother", www_All
127: .implies(copyOfWww_All));
128: assertTrue("Wild should imply normal", star_All
129: .implies(www_All));
130: assertTrue("Normal shouldn't imply wildcard", !www_All
131: .implies(star_Resolve));
132: assertTrue("Resolve shouldn't imply all", !star_Resolve
133: .implies(star_All));
134: SocketPermission p1 = new SocketPermission(wwwName + ":80-81",
135: "connect");
136: SocketPermission p2 = new SocketPermission(wwwName + ":80",
137: "connect");
138: assertTrue("Port 80 is implied by 80-81", p1.implies(p2));
139: p1 = new SocketPermission(wwwName + ":79-80", "connect");
140: assertTrue("Port 80 is implied by 79-80", p1.implies(p2));
141: p1 = new SocketPermission(wwwName + ":79-81", "connect");
142: assertTrue("Port 80 is implied by 79-81", p1.implies(p2));
143: p2 = new SocketPermission(wwwName + ":79-80", "connect");
144: assertTrue("Port 79-80 is implied by 79-81", p1.implies(p2));
145: p2 = new SocketPermission(wwwName, "resolve");
146: assertTrue(
147: "Any identical host should imply resolve regardless of the ports",
148: p1.implies(p2));
149:
150: SocketPermission sp1 = new SocketPermission("www.Ibm.com",
151: "resolve");
152: SocketPermission sp2 = new SocketPermission("www.IBM.com",
153: "resolve");
154: assertTrue("SocketPermission is case sensitive", sp1
155: .implies(sp2));
156:
157: SocketPermission sp3 = new SocketPermission("*.ibm.com",
158: "resolve");
159: assertTrue("SocketPermission wildcard is case sensitive", sp3
160: .implies(sp2));
161:
162: InetAddress host = null;
163: try {
164: host = InetAddress
165: .getByName(Support_Configuration.UnresolvedIP);
166: } catch (UnknownHostException e) {
167: }
168:
169: SocketPermission perm1 = new SocketPermission(
170: Support_Configuration.UnresolvedIP, "connect");
171: SocketPermission perm2 = new SocketPermission(
172: Support_Configuration.UnresolvedIP + ":80", "connect");
173: assertTrue("should imply port 80", perm1.implies(perm2));
174: PermissionCollection col = perm1.newPermissionCollection();
175: col.add(perm1);
176: assertTrue("collection should imply port 80", col
177: .implies(perm2));
178: }
179:
180: /**
181: * @tests java.net.SocketPermission#newPermissionCollection()
182: */
183: public void test_newPermissionCollection() {
184: java.security.PermissionCollection pc = star_Resolve
185: .newPermissionCollection();
186: pc.add(star_Resolve);
187: pc.add(www_All);
188: assertTrue("Should imply all on " + wwwName, pc
189: .implies(www_All));
190: assertTrue("Should imply resolve on " + starName, pc
191: .implies(star_Resolve));
192: assertTrue("Should not imply all on " + starName, !pc
193: .implies(star_All));
194:
195: // wipe out pc
196: pc = star_Resolve.newPermissionCollection();
197: pc.add(star_All);
198: assertTrue("Should imply resolve on " + starName, pc
199: .implies(star_Resolve));
200: assertTrue("Should imply all on " + wwwName, pc
201: .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: }
|