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