test/jdk/java/net/httpclient/security/filePerms/SecurityBeforeFile.java
author chegar
Wed, 07 Mar 2018 14:06:39 +0000
branchhttp-client-branch
changeset 56257 82a9340bdda6
permissions -rw-r--r--
http-client-branch: rework file permissions to use limited doPriv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56257
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
     1
/*
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
     4
 *
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
     8
 *
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    13
 * accompanied this code).
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    14
 *
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    18
 *
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    21
 * questions.
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    22
 */
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    23
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    24
/*
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    25
 * @test
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    26
 * @summary Verifies security checks are performed before existence checks
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    27
 *          in pre-defined body processors APIs
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    28
 * @run testng/othervm SecurityBeforeFile
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    29
 * @run testng/othervm/java.security.policy=nopermissions.policy SecurityBeforeFile
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    30
 */
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    31
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    32
import java.io.FileNotFoundException;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    33
import java.nio.file.Files;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    34
import java.nio.file.OpenOption;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    35
import java.nio.file.Path;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    36
import java.nio.file.Paths;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    37
import java.net.http.HttpRequest.BodyPublishers;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    38
import java.net.http.HttpResponse.BodyHandlers;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    39
import org.testng.annotations.DataProvider;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    40
import org.testng.annotations.Test;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    41
import static java.lang.System.out;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    42
import static java.nio.file.StandardOpenOption.*;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    43
import static org.testng.Assert.*;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    44
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    45
public class SecurityBeforeFile {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    46
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    47
    static final boolean hasSecurityManager = System.getSecurityManager() != null;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    48
    static final boolean hasNoSecurityManager = !hasSecurityManager;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    49
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    50
    @Test
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    51
    public void BodyPublishersOfFile() {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    52
        Path p = Paths.get("doesNotExist.txt");
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    53
        if (hasNoSecurityManager && Files.exists(p))
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    54
            throw new AssertionError("Unexpected " + p);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    55
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    56
        try {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    57
            BodyPublishers.ofFile(p);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    58
            fail("UNEXPECTED, file " + p.toString() + " exists?");
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    59
        } catch (SecurityException se) {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    60
            assertTrue(hasSecurityManager);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    61
            out.println("caught expected security exception: " + se);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    62
        } catch (FileNotFoundException fnfe) {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    63
            assertTrue(hasNoSecurityManager);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    64
            out.println("caught expected file not found exception: " + fnfe);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    65
        }
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    66
    }
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    67
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    68
    @DataProvider(name = "handlerOpenOptions")
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    69
    public Object[][] handlerOpenOptions() {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    70
        return new Object[][] {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    71
                { new OpenOption[] {               } },
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    72
                { new OpenOption[] { CREATE        } },
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    73
                { new OpenOption[] { CREATE, WRITE } },
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    74
        };
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    75
    }
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    76
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    77
    @Test(dataProvider = "handlerOpenOptions")
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    78
    public void BodyHandlersOfFileDownload(OpenOption[] openOptions) {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    79
        Path p = Paths.get("doesNotExistDir");
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    80
        if (hasNoSecurityManager && Files.exists(p))
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    81
            throw new AssertionError("Unexpected " + p);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    82
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    83
        try {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    84
            BodyHandlers.ofFileDownload(p, openOptions);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    85
            fail("UNEXPECTED, file " + p.toString() + " exists?");
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    86
        } catch (SecurityException se) {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    87
            assertTrue(hasSecurityManager);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    88
            out.println("caught expected security exception: " + se);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    89
        } catch (IllegalArgumentException iae) {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    90
            assertTrue(hasNoSecurityManager);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    91
            out.println("caught expected illegal argument exception: " + iae);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    92
        }
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    93
    }
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
diff changeset
    94
}