1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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.security.permissions; 18 19import java.io.File; 20import java.io.IOException; 21import java.security.Permission; 22import java.util.zip.ZipException; 23import java.util.zip.ZipFile; 24 25import junit.framework.TestCase; 26import dalvik.annotation.TestLevel; 27import dalvik.annotation.TestTargetClass; 28import dalvik.annotation.TestTargetNew; 29/* 30 * This class tests the security permissions which are documented in 31 * http://java.sun.com/j2se/1.5.0/docs/guide/security/permissions.html#PermsAndMethods 32 * for class java.util.zip.ZipFile 33 */ 34@TestTargetClass(java.util.zip.ZipFile.class) 35public class JavaUtilZipZipFile extends TestCase { 36 37 SecurityManager old; 38 39 @Override 40 protected void setUp() throws Exception { 41 old = System.getSecurityManager(); 42 super.setUp(); 43 } 44 45 @Override 46 protected void tearDown() throws Exception { 47 System.setSecurityManager(old); 48 super.tearDown(); 49 } 50 51 @TestTargetNew( 52 level = TestLevel.PARTIAL_COMPLETE, 53 notes = "Verifies that the constructor java.util.zip.ZipFile() calls checkRead on the security manager.", 54 method = "ZipFile", 55 args = {java.lang.String.class} 56 ) 57 public void test_ZipFile() throws IOException { 58 class TestSecurityManager extends SecurityManager { 59 private boolean called = false; 60 private String name = null; 61 void reset(){ 62 called = false; 63 name = null; 64 } 65 String getName(){return name;} 66 @Override 67 public void checkRead(String name) { 68 called = true; 69 this.name = name; 70 super.checkRead(name); 71 } 72 73 @Override 74 public void checkPermission(Permission permission) { 75 } 76 } 77 78 File file = File.createTempFile("foo", "zip"); 79 String filename = file.getAbsolutePath(); 80 81 TestSecurityManager s = new TestSecurityManager(); 82 System.setSecurityManager(s); 83 84 s.reset(); 85 try { 86 new ZipFile(filename); 87 } catch (ZipException ex) { 88 // Ignore. 89 } 90 assertTrue("java.util.zip.ZipFile() construcor must call checkRead on security permissions", s.called); 91 assertEquals("Argument of checkPermission is not correct", filename, s.getName()); 92 } 93} 94