Skip to content

Commit c6a288d

Browse files
author
Lance Andersen
committed
8305945: (zipfs) Opening a directory to get input stream produces incorrect exception message
Reviewed-by: naoto, cstein
1 parent 73018b3 commit c6a288d

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ InputStream newInputStream(byte[] path) throws IOException {
860860
if (e == null)
861861
throw new NoSuchFileException(getString(path));
862862
if (e.isDir())
863-
throw new FileSystemException(getString(path), "is a directory", null);
863+
throw new FileSystemException(getString(path), null, "is a directory");
864864
return getInputStream(e);
865865
} finally {
866866
endRead();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
30+
import java.io.IOException;
31+
import java.nio.file.*;
32+
import java.util.zip.ZipEntry;
33+
import java.util.zip.ZipOutputStream;
34+
35+
import static org.junit.jupiter.api.Assertions.*;
36+
37+
/**
38+
* @test
39+
* @bug 8305945
40+
* @summary Validate that Zip FS provides the correct exception message when an
41+
* attempt is made to obtain an InputStream from a directory entry
42+
* @modules jdk.zipfs
43+
* @run junit ZipFSDirectoryExceptionMessageTest
44+
*/
45+
public class ZipFSDirectoryExceptionMessageTest {
46+
/**
47+
* Name of Directory created within the Zip file
48+
*/
49+
public static final String DIRECTORY_NAME = "folder/";
50+
/**
51+
* The expected error message
52+
*/
53+
public static final String DIR_EXCEPTION_MESSAGE = "/folder: is a directory";
54+
/**
55+
* Zip file to create
56+
*/
57+
public static final Path ZIP_FILE = Path.of("directoryExceptionTest.zip");
58+
59+
/**
60+
* Create a Zip file which contains a single directory entry
61+
* @throws IOException if an error occurs
62+
*/
63+
@BeforeEach
64+
public void setup() throws IOException {
65+
var ze = new ZipEntry(DIRECTORY_NAME);
66+
ze.setMethod(ZipEntry.STORED);
67+
ze.setCompressedSize(0);
68+
ze.setSize(0);
69+
ze.setCrc(0);
70+
try (ZipOutputStream zos = new ZipOutputStream(
71+
Files.newOutputStream(ZIP_FILE))) {
72+
zos.putNextEntry(ze);
73+
}
74+
}
75+
76+
/**
77+
* Delete the Zip file used by the test
78+
* @throws IOException If an error occurs
79+
*/
80+
@AfterEach
81+
public void cleanup() throws IOException {
82+
Files.deleteIfExists(ZIP_FILE);
83+
}
84+
85+
/**
86+
* Validate that Zip FS returns the correct Exception message when
87+
* attempting to obtain an InputStream from a path representing a directory
88+
* and the FileSystemException::getOtherfile returns null
89+
*
90+
* @throws IOException If an error occurs
91+
*/
92+
@Test
93+
public void testException() throws IOException {
94+
try (FileSystem zipfs = FileSystems.newFileSystem(ZIP_FILE)) {
95+
var file = zipfs.getPath(DIRECTORY_NAME);
96+
var x = assertThrows(FileSystemException.class, () -> Files.newInputStream(file));
97+
// validate that other file should be null
98+
assertNull(x.getOtherFile());
99+
assertEquals(DIR_EXCEPTION_MESSAGE, x.getMessage());
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)