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 org.apache.harmony.nio_char.tests.java.nio.charset.spi; 18 19import dalvik.annotation.TestTargetClass; 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestTargetNew; 22import dalvik.annotation.TestLevel; 23 24import java.nio.charset.Charset; 25import java.nio.charset.spi.CharsetProvider; 26import java.security.Permission; 27import java.util.Iterator; 28 29import junit.framework.TestCase; 30@TestTargetClass(CharsetProvider.class) 31/** 32 * Test class java.nio.charset.spi.CharsetProvider. 33 */ 34public class CharsetProviderTest extends TestCase { 35 36 /* 37 * Test the security check in the constructor. 38 */ 39 @TestTargetNew( 40 level = TestLevel.COMPLETE, 41 notes = "", 42 method = "CharsetProvider", 43 args = {} 44 ) 45 public void testConstructor() { 46 // with sufficient privilege 47 new MockCharsetProvider(); 48 49 SecurityManager oldMan = System.getSecurityManager(); 50 System.setSecurityManager(new MockSecurityManager()); 51 // set a normal value 52 try { 53 new MockCharsetProvider(); 54 fail("Should throw SecurityException!"); 55 } catch (SecurityException e) { 56 // expected 57 } finally { 58 System.setSecurityManager(oldMan); 59 } 60 } 61 62 /* 63 * Test the signature. 64 */ 65 static class MockCharsetProvider extends CharsetProvider { 66 67 public Charset charsetForName(String charsetName) { 68 return null; 69 } 70 71 public Iterator charsets() { 72 return null; 73 } 74 } 75 76 /* 77 * Used to grant all permissions except charset provider access. 78 */ 79 public static class MockSecurityManager extends SecurityManager { 80 81 public MockSecurityManager() { 82 } 83 84 public void checkPermission(Permission perm) { 85 // grant all permissions except logging control 86 if (perm instanceof RuntimePermission) { 87 RuntimePermission rp = (RuntimePermission) perm; 88 if (rp.getName().equals("charsetProvider")) { 89 throw new SecurityException(); 90 } 91 } 92 } 93 94 public void checkPermission(Permission perm, Object context) { 95 // grant all permissions except logging control 96 if (perm instanceof RuntimePermission) { 97 RuntimePermission rp = (RuntimePermission) perm; 98 if (rp.getName().equals("charsetProvider")) { 99 throw new SecurityException(); 100 } 101 } 102 } 103 } 104 105} 106