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