CookieHandlerTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package tests.api.java.net;
18
19import java.io.IOException;
20import java.net.CookieHandler;
21import java.net.NetPermission;
22import java.net.URI;
23import java.security.Permission;
24import java.util.Map;
25
26import junit.framework.TestCase;
27
28public class CookieHandlerTest extends TestCase {
29
30    /**
31     * @tests java.net.CookieHandler#getDefault()
32     */
33    public void test_GetDefault() {
34        assertNull(CookieHandler.getDefault());
35    }
36
37    /**
38     * @tests java.net.CookieHandler#setDefault(CookieHandler)
39     */
40    public void test_SetDefault_java_net_cookieHandler() {
41        MockCookieHandler rc1 = new MockCookieHandler();
42        MockCookieHandler rc2 = new MockCookieHandler();
43        CookieHandler.setDefault(rc1);
44        assertSame(CookieHandler.getDefault(), rc1);
45        CookieHandler.setDefault(rc2);
46        assertSame(CookieHandler.getDefault(), rc2);
47        CookieHandler.setDefault(null);
48        assertNull(CookieHandler.getDefault());
49    }
50
51    /**
52     * @tests java.net.CookieHandler#getDefault()
53     */
54    public void testGetDefault_Security() {
55        SecurityManager old = System.getSecurityManager();
56        try {
57            System.setSecurityManager(new MockSM());
58        } catch (SecurityException e) {
59            System.err.println("Unable to reset securityManager,test ignored");
60            return;
61        }
62        try {
63            CookieHandler.getDefault();
64            fail("should throw SecurityException");
65        } catch (SecurityException e) {
66            // correct
67        } finally {
68            System.setSecurityManager(old);
69        }
70    }
71
72    /**
73     * @tests java.net.CookieHandler#setDefault(CookieHandler)
74     */
75    public void testSetDefault_Security() {
76        CookieHandler rc = new MockCookieHandler();
77        SecurityManager old = System.getSecurityManager();
78        try {
79            System.setSecurityManager(new MockSM());
80        } catch (SecurityException e) {
81            System.err.println("Unable to reset securityManager,test ignored");
82            return;
83        }
84
85        try {
86            CookieHandler.setDefault(rc);
87            fail("should throw SecurityException");
88        } catch (SecurityException e) {
89            // correct
90        } finally {
91            System.setSecurityManager(old);
92        }
93    }
94
95    class MockCookieHandler extends CookieHandler {
96
97        public Map get(URI uri, Map requestHeaders) throws IOException {
98            return null;
99        }
100
101        public void put(URI uri, Map responseHeaders) throws IOException {
102            // empty
103        }
104
105    }
106
107    class MockSM extends SecurityManager {
108        public void checkPermission(Permission permission) {
109            if (permission instanceof NetPermission) {
110                if ("setCookieHandler".equals(permission.getName())) {
111                    throw new SecurityException();
112                }
113            }
114
115            if (permission instanceof NetPermission) {
116                if ("getCookieHandler".equals(permission.getName())) {
117                    throw new SecurityException();
118                }
119            }
120
121            if (permission instanceof RuntimePermission) {
122                if ("setSecurityManager".equals(permission.getName())) {
123                    return;
124                }
125            }
126        }
127    }
128}
129