1 /*
2 * Copyright (c) 2022, 2025, 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 import jdk.internal.jimage.ImageReader;
25 import jdk.internal.jimage.PreviewMode;
26
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Set;
33
34 /**
35 * @test
36 * @bug 8247407
37 * @summary Test that ImageReader doesn't create a Directory node with duplicate children
38 * @modules java.base/jdk.internal.jimage
39 * @run main ImageReaderDuplicateChildNodesTest
40 */
41 public class ImageReaderDuplicateChildNodesTest {
42
43 /**
44 * Uses the ImageReader to open and read the JAVA_HOME/lib/modules image. Tests that the
45 * {@link ImageReader#findNode(String)} corresponding to a directory doesn't return a node
46 * with duplicate children in it.
47 */
48 public static void main(final String[] args) throws Exception {
49 final Path imagePath = Paths.get(System.getProperty("java.home"), "lib", "modules");
50 if (!Files.exists(imagePath)) {
51 // skip the testing in the absence of the image file
52 System.err.println("Skipping test since " + imagePath + " is absent");
53 return;
54 }
55 System.out.println("Running test against image " + imagePath);
56 final String integersParentResource = "/modules/java.base/java/lang";
57 final String integerResource = integersParentResource + "/Integer.class";
58 try (final ImageReader reader = ImageReader.open(imagePath, PreviewMode.DISABLED)) {
59 // find the child node/resource first
60 final ImageReader.Node integerNode = reader.findNode(integerResource);
61 if (integerNode == null) {
62 throw new RuntimeException("ImageReader could not locate " + integerResource
63 + " in " + imagePath);
64 }
65 // now find the parent node (which will represent a directory)
66 final ImageReader.Node parent = reader.findNode(integersParentResource);
67 if (parent == null) {
68 throw new RuntimeException("ImageReader could not locate " + integersParentResource
69 + " in " + imagePath);
70 }
71 // now verify that the parent node which is a directory, doesn't have duplicate children
72 final List<String> childNames = parent.getChildNames().toList();
73 if (childNames.isEmpty()) {
74 throw new RuntimeException("ImageReader did not return any child resources under "
75 + integersParentResource + " in " + imagePath);
76 }
77 final Set<ImageReader.Node> uniqueChildren = new HashSet<>();
78 for (final String childName : childNames) {
79 final boolean unique = uniqueChildren.add(reader.findNode(childName));
80 if (!unique) {
81 throw new RuntimeException("ImageReader returned duplicate child resource "
82 + childName + " under " + parent + " from image " + imagePath);
83 }
84 }
85 }
86 }
87 }