Path: blob/master/test/jdk/javax/imageio/CachePremissionsTest/CachePermissionsTest.java
41152 views
/*1* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 668410426* @summary Test verifies that ImageIO checks all permissions required for27* the file cache usage:28*29* no policy file: No security restrictions.30* Expected result: ImageIO creates file-cached stream.31*32* w.policy: the case when we have read and write permissions33* for java.io.temp directory but have only write permission34* for a temp file.35* Expected result: ImageIO create a memory-cached stream36* image output stream.37*38* rw.policy: the case when we have read and write permissions39* for java.io.temp directory but have only read and write40* permission for a temp cache file.41* Expected result: ImageIO creates a memory-cached stream42* because temporary cache file can not be deleted.43*44* rwd.policy: the case when we have read and write permissions45* for java.io.temp directory and have all required permissions46* (read, write, and delete) for a temporary cache file.47* Expected result: ImageIO creates file-cached stream.48*49* -Djava.security.debug=access can be used to verify file permissions.50*51* @run main CachePermissionsTest true52* @run main/othervm -Djava.security.manager=allow CachePermissionsTest false w.policy53* @run main/othervm -Djava.security.manager=allow CachePermissionsTest false rw.policy54* @run main/othervm -Djava.security.manager=allow CachePermissionsTest true rwd.policy55*/5657import java.io.File;58import java.io.IOException;59import java.io.ByteArrayOutputStream;60import javax.imageio.stream.ImageOutputStream;6162import javax.imageio.ImageIO;636465public class CachePermissionsTest {66public static void main(String[] args) {67boolean isFileCacheExpected =68Boolean.valueOf(args[0]).booleanValue();69System.out.println("Is file cache expected: " + isFileCacheExpected);7071ImageIO.setUseCache(true);7273System.out.println("java.io.tmpdir is " + System.getProperty("java.io.tmpdir"));7475if (args.length > 1) {76String testsrc = System.getProperty("test.src", ".");77String policy = testsrc + File.separator + args[1];7879System.out.println("Policy file: " + policy);80System.setProperty("java.security.policy", policy);8182System.out.println("Install security manager...");83System.setSecurityManager(new SecurityManager());84}8586ByteArrayOutputStream baos = new ByteArrayOutputStream();8788try {89ImageOutputStream ios = ImageIO.createImageOutputStream(baos);9091boolean isFileCache = ios.isCachedFile();92System.out.println("Is file cache used: " + isFileCache);9394if (isFileCache !=isFileCacheExpected) {95System.out.println("WARNING: file chace usage is not as expected!");96}9798System.out.println("Verify data writing...");99for (int i = 0; i < 8192; i++) {100ios.writeInt(i);101}102103System.out.println("Verify data reading...");104ios.seek(0L);105106for (int i = 0; i < 8192; i++) {107int j = ios.readInt();108if (i != j) {109throw new RuntimeException("Wrong data in the stream " + j + " instead of " + i);110}111}112113System.out.println("Verify stream closing...");114ios.close();115} catch (IOException e) {116/*117* Something went wrong?118*/119throw new RuntimeException("Test FAILED.", e);120} catch (SecurityException e) {121/*122* We do not expect security execptions here:123* we there are any security restrition, ImageIO124* should swith to memory-cached streams, instead125* of using file cache.126*/127throw new RuntimeException("Test FAILED.", e);128}129}130}131132133