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