1292N/A/*
3261N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
1292N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1292N/A *
1292N/A * This code is free software; you can redistribute it and/or modify it
1292N/A * under the terms of the GNU General Public License version 2 only, as
1292N/A * published by the Free Software Foundation.
1292N/A *
1292N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1292N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1292N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1292N/A * version 2 for more details (a copy is included in the LICENSE file that
1292N/A * accompanied this code).
1292N/A *
1292N/A * You should have received a copy of the GNU General Public License version
1292N/A * 2 along with this work; if not, write to the Free Software Foundation,
1292N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1292N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1292N/A */
1292N/A
1292N/A/**
1292N/A * @test
1292N/A * @bug 6684104
1292N/A * @summary Test verifies that ImageIO checks all permissions required for
1292N/A * the file cache usage:
1292N/A *
1292N/A * no policy file: No security restrictions.
1292N/A * Expected result: ImageIO creates file-cached stream.
1292N/A *
1292N/A * w.policy: the case when we have read and write permissions
1292N/A * for java.io.temp directory but have only write permission
1292N/A * for a temp file.
1292N/A * Expected result: ImageIO create a memory-cached stream
1292N/A * image output stream.
1292N/A *
1292N/A * rw.policy: the case when we have read and write permissions
1292N/A * for java.io.temp directory but have only read and write
1292N/A * permission for a temp cache file.
1292N/A * Expected result: ImageIO creates a memory-cached stream
1292N/A * because temporary cache file can not be deleted.
1292N/A *
1292N/A * rwd.policy: the case when we have read and write permissions
1292N/A * for java.io.temp directory and have all required permissions
1292N/A * (read, write, and delete) for a temporary cache file.
1292N/A * Expected result: ImageIO creates file-cached stream.
1292N/A *
1292N/A * -Djava.security.debug=access can be used to verify file permissions.
1292N/A *
1292N/A * @run main CachePermissionsTest true
2692N/A * @run main/othervm CachePermissionsTest false w.policy
2692N/A * @run main/othervm CachePermissionsTest false rw.policy
2692N/A * @run main/othervm CachePermissionsTest true rwd.policy
1292N/A */
1292N/A
1292N/Aimport java.io.File;
1292N/Aimport java.io.IOException;
1292N/Aimport java.io.ByteArrayOutputStream;
1292N/Aimport javax.imageio.stream.ImageOutputStream;
1292N/A
1292N/Aimport javax.imageio.ImageIO;
1292N/A
1292N/A
1292N/Apublic class CachePermissionsTest {
1292N/A public static void main(String[] args) {
1292N/A boolean isFileCacheExpected =
1292N/A Boolean.valueOf(args[0]).booleanValue();
1292N/A System.out.println("Is file cache expected: " + isFileCacheExpected);
1292N/A
1292N/A ImageIO.setUseCache(true);
1292N/A
1292N/A System.out.println("java.io.tmpdir is " + System.getProperty("java.io.tmpdir"));
1292N/A
2692N/A if (args.length > 1) {
2692N/A String testsrc = System.getProperty("test.src", ".");
2692N/A String policy = testsrc + File.separator + args[1];
2692N/A
2692N/A System.out.println("Policy file: " + policy);
2692N/A System.setProperty("java.security.policy", policy);
2692N/A
2692N/A System.out.println("Install security manager...");
2692N/A System.setSecurityManager(new SecurityManager());
2692N/A }
2692N/A
1292N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
1292N/A
1292N/A try {
1292N/A ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
1292N/A
1292N/A boolean isFileCache = ios.isCachedFile();
1292N/A System.out.println("Is file cache used: " + isFileCache);
1292N/A
1292N/A if (isFileCache !=isFileCacheExpected) {
1292N/A System.out.println("WARNING: file chace usage is not as expected!");
1292N/A }
1292N/A
1292N/A System.out.println("Verify data writing...");
1292N/A for (int i = 0; i < 8192; i++) {
1292N/A ios.writeInt(i);
1292N/A }
1292N/A
1292N/A System.out.println("Verify data reading...");
1292N/A ios.seek(0L);
1292N/A
1292N/A for (int i = 0; i < 8192; i++) {
1292N/A int j = ios.readInt();
1292N/A if (i != j) {
1292N/A throw new RuntimeException("Wrong data in the stream " + j + " instead of " + i);
1292N/A }
1292N/A }
1292N/A
1292N/A System.out.println("Verify stream closing...");
1292N/A ios.close();
1292N/A } catch (IOException e) {
1292N/A /*
1292N/A * Something went wrong?
1292N/A */
1292N/A throw new RuntimeException("Test FAILED.", e);
1292N/A } catch (SecurityException e) {
1292N/A /*
1292N/A * We do not expect security execptions here:
1292N/A * we there are any security restrition, ImageIO
1292N/A * should swith to memory-cached streams, instead
1292N/A * of using file cache.
1292N/A */
1292N/A throw new RuntimeException("Test FAILED.", e);
1292N/A }
1292N/A }
1292N/A}