jdk/test/javax/imageio/CachePremissionsTest/CachePermissionsTest.java
author prr
Thu, 18 Dec 2014 10:45:45 -0800
changeset 29908 83e2c403fefd
parent 7668 d4a77089c587
permissions -rw-r--r--
8067050: Better font consistency checking Reviewed-by: bae, srl, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3014
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6481
diff changeset
     2
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3014
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
     4
 *
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
     7
 * published by the Free Software Foundation.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
     8
 *
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    13
 * accompanied this code).
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    14
 *
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3014
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3014
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3014
diff changeset
    21
 * questions.
3014
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    22
 */
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    23
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    24
/**
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    25
 * @test
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    26
 * @bug     6684104
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    27
 * @summary Test verifies that ImageIO checks all permissions required for
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    28
 *           the file cache usage:
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    29
 *
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    30
 *          no policy file: No security restrictions.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    31
 *             Expected result: ImageIO creates file-cached stream.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    32
 *
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    33
 *          w.policy: the case when we have read and write permissions
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    34
 *              for java.io.temp directory but have only write permission
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    35
 *              for a temp file.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    36
 *             Expected result: ImageIO create a memory-cached stream
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    37
 *              image output stream.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    38
 *
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    39
 *          rw.policy: the case when we  have read and write permissions
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    40
 *              for java.io.temp directory but have only read and write
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    41
 *              permission for a temp cache file.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    42
 *             Expected result: ImageIO creates a memory-cached stream
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    43
 *              because temporary cache file can not be deleted.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    44
 *
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    45
 *          rwd.policy: the case when we  have read and write permissions
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    46
 *              for java.io.temp directory and have all required permissions
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    47
 *             (read, write, and delete) for a temporary cache file.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    48
 *             Expected result: ImageIO creates file-cached stream.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    49
 *
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    50
 *           -Djava.security.debug=access can be used to verify file permissions.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    51
 *
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    52
 * @run     main CachePermissionsTest true
6481
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    53
 * @run     main/othervm CachePermissionsTest false w.policy
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    54
 * @run     main/othervm CachePermissionsTest false rw.policy
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    55
 * @run     main/othervm CachePermissionsTest true rwd.policy
3014
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    56
 */
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    57
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    58
import java.io.File;
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    59
import java.io.IOException;
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    60
import java.io.ByteArrayOutputStream;
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    61
import javax.imageio.stream.ImageOutputStream;
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    62
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    63
import javax.imageio.ImageIO;
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    64
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    65
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    66
public class CachePermissionsTest {
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    67
    public static void main(String[] args) {
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    68
        boolean isFileCacheExpected =
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    69
            Boolean.valueOf(args[0]).booleanValue();
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    70
        System.out.println("Is file cache expected: " + isFileCacheExpected);
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    71
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    72
        ImageIO.setUseCache(true);
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    73
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    74
        System.out.println("java.io.tmpdir is " + System.getProperty("java.io.tmpdir"));
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    75
6481
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    76
        if (args.length > 1) {
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    77
            String testsrc = System.getProperty("test.src", ".");
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    78
            String policy = testsrc + File.separator + args[1];
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    79
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    80
            System.out.println("Policy file: " + policy);
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    81
            System.setProperty("java.security.policy", policy);
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    82
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    83
            System.out.println("Install security manager...");
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    84
            System.setSecurityManager(new SecurityManager());
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    85
        }
78d56f33c3a7 6972495: javax/imageio/CachePremissionsTest/CachePermissionsTest.java failed
bae
parents: 5506
diff changeset
    86
3014
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    87
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    88
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    89
        try {
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    90
            ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    91
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    92
            boolean isFileCache = ios.isCachedFile();
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    93
            System.out.println("Is file cache used: " + isFileCache);
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    94
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    95
            if (isFileCache !=isFileCacheExpected) {
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    96
                System.out.println("WARNING: file chace usage is not as expected!");
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    97
            }
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    98
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
    99
            System.out.println("Verify data writing...");
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   100
            for (int i = 0; i < 8192; i++) {
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   101
                ios.writeInt(i);
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   102
            }
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   103
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   104
            System.out.println("Verify data reading...");
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   105
            ios.seek(0L);
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   106
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   107
            for (int i = 0; i < 8192; i++) {
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   108
                int j = ios.readInt();
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   109
                if (i != j) {
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   110
                    throw new RuntimeException("Wrong data in the stream " + j + " instead of " + i);
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   111
                }
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   112
            }
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   113
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   114
            System.out.println("Verify stream closing...");
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   115
            ios.close();
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   116
        } catch (IOException e) {
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   117
            /*
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   118
             * Something went wrong?
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   119
             */
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   120
            throw new RuntimeException("Test FAILED.", e);
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   121
        } catch (SecurityException e) {
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   122
            /*
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   123
             * We do not expect security execptions here:
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   124
             * we there are any security restrition, ImageIO
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   125
             * should swith to memory-cached streams, instead
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   126
             * of using file cache.
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   127
             */
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   128
            throw new RuntimeException("Test FAILED.", e);
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   129
        }
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   130
    }
85cbc1221cc0 6684104: Applets fails to launch using ImageIO if .java.policy with File permissions present on the system
bae
parents:
diff changeset
   131
}