001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.luni.tests.java.net;
018:
019: import java.io.IOException;
020: import java.net.CookieHandler;
021: import java.net.NetPermission;
022: import java.net.URI;
023: import java.security.Permission;
024: import java.util.Map;
025:
026: import junit.framework.TestCase;
027:
028: public class CookieHandlerTest extends TestCase {
029:
030: /**
031: * @tests java.net.CookieHandler#getDefault()
032: */
033: public void test_GetDefault() {
034: assertNull(CookieHandler.getDefault());
035: }
036:
037: /**
038: * @tests java.net.CookieHandler#setDefault(CookieHandler)
039: */
040: public void test_SetDefault_java_net_cookieHandler() {
041: MockCookieHandler rc1 = new MockCookieHandler();
042: MockCookieHandler rc2 = new MockCookieHandler();
043: CookieHandler.setDefault(rc1);
044: assertSame(CookieHandler.getDefault(), rc1);
045: CookieHandler.setDefault(rc2);
046: assertSame(CookieHandler.getDefault(), rc2);
047: CookieHandler.setDefault(null);
048: assertNull(CookieHandler.getDefault());
049: }
050:
051: /**
052: * @tests java.net.CookieHandler#getDefault()
053: */
054: public void testGetDefault_Security() {
055: SecurityManager old = System.getSecurityManager();
056: try {
057: System.setSecurityManager(new MockSM());
058: } catch (SecurityException e) {
059: System.err
060: .println("Unable to reset securityManager,test ignored");
061: return;
062: }
063: try {
064: CookieHandler.getDefault();
065: fail("should throw SecurityException");
066: } catch (SecurityException e) {
067: // correct
068: } finally {
069: System.setSecurityManager(old);
070: }
071: }
072:
073: /**
074: * @tests java.net.CookieHandler#setDefault(CookieHandler)
075: */
076: public void testSetDefault_Security() {
077: CookieHandler rc = new MockCookieHandler();
078: SecurityManager old = System.getSecurityManager();
079: try {
080: System.setSecurityManager(new MockSM());
081: } catch (SecurityException e) {
082: System.err
083: .println("Unable to reset securityManager,test ignored");
084: return;
085: }
086:
087: try {
088: CookieHandler.setDefault(rc);
089: fail("should throw SecurityException");
090: } catch (SecurityException e) {
091: // correct
092: } finally {
093: System.setSecurityManager(old);
094: }
095: }
096:
097: class MockCookieHandler extends CookieHandler {
098:
099: public Map get(URI uri, Map requestHeaders) throws IOException {
100: return null;
101: }
102:
103: public void put(URI uri, Map responseHeaders)
104: throws IOException {
105: // empty
106: }
107:
108: }
109:
110: class MockSM extends SecurityManager {
111: public void checkPermission(Permission permission) {
112: if (permission instanceof NetPermission) {
113: if ("setCookieHandler".equals(permission.getName())) {
114: throw new SecurityException();
115: }
116: }
117:
118: if (permission instanceof NetPermission) {
119: if ("getCookieHandler".equals(permission.getName())) {
120: throw new SecurityException();
121: }
122: }
123:
124: if (permission instanceof RuntimePermission) {
125: if ("setSecurityManager".equals(permission.getName())) {
126: return;
127: }
128: }
129: }
130: }
131: }
|