Path: blob/master/test/jdk/sun/net/www/protocol/file/DirPermissionDenied.java
41159 views
/*1* Copyright (c) 2010, 2019, 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 697785126* @summary NPE from FileURLConnection.connect27* @library /test/lib28* @build DirPermissionDenied jdk.test.lib.process.*29* jdk.test.lib.util.FileUtils30* @run testng DirPermissionDenied31*/3233import java.io.IOException;34import java.net.MalformedURLException;35import java.net.URL;36import java.net.URLConnection;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.Paths;4041import jdk.test.lib.process.ProcessTools;42import jdk.test.lib.util.FileUtils;43import org.testng.annotations.AfterTest;44import org.testng.annotations.Test;45import org.testng.annotations.BeforeTest;46public class DirPermissionDenied {47private static final Path TEST_DIR = Paths.get(48"DirPermissionDeniedDirectory");4950@Test51public void doTest() throws MalformedURLException {52URL url = new URL(TEST_DIR.toUri().toString());53try {54URLConnection uc = url.openConnection();55uc.connect();56} catch (IOException e) {57// OK58} catch (Exception e) {59throw new RuntimeException("Failed " + e);60}6162try {63URLConnection uc = url.openConnection();64uc.getInputStream();65} catch (IOException e) {66// OK67} catch (Exception e) {68throw new RuntimeException("Failed " + e);69}7071try {72URLConnection uc = url.openConnection();73uc.getContentLengthLong();74} catch (IOException e) {75// OK76} catch (Exception e) {77throw new RuntimeException("Failed " + e);78}79}8081@BeforeTest82public void setup() throws Throwable {83// mkdir and chmod "333"84Files.createDirectories(TEST_DIR);85ProcessTools.executeCommand("chmod", "333", TEST_DIR.toString())86.outputTo(System.out)87.errorTo(System.out)88.shouldHaveExitValue(0);89}9091@AfterTest92public void tearDown() throws Throwable {93// add read permission to ensure the dir removable94ProcessTools.executeCommand("chmod", "733", TEST_DIR.toString())95.outputTo(System.out)96.errorTo(System.out)97.shouldHaveExitValue(0);98FileUtils.deleteFileIfExistsWithRetry(TEST_DIR);99}100}101102103