|
| 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8345358 |
| 27 | + * @run main/othervm CheckWindowsProperty |
| 28 | + * @summary file property check for Windows .exe/.dll |
| 29 | + * @requires os.family == "windows" |
| 30 | + */ |
| 31 | + |
| 32 | +import java.io.BufferedReader; |
| 33 | +import java.io.InputStreamReader; |
| 34 | + |
| 35 | +public class CheckWindowsProperty { |
| 36 | + public static void main(String[] args) throws Exception { |
| 37 | + String targetDir = System.getProperty("test.jdk") + "\\"; |
| 38 | + String psCommand = String.format( |
| 39 | + "Get-ChildItem -Path '%s' -include *.exe,*.dll -Recurse -File | " + |
| 40 | + "ForEach-Object { " + |
| 41 | + "$vi = $_.VersionInfo; " + |
| 42 | + "@($vi.FileName, $vi.CompanyName, $vi.FileDescription, $vi.FileVersion, " + |
| 43 | + "$vi.InternalName, $vi.Language, $vi.LegalCopyright, $vi.OriginalFilename, " + |
| 44 | + "$vi.ProductName, $vi.ProductVersion) -join ';'" + |
| 45 | + "}", |
| 46 | + targetDir |
| 47 | + ); |
| 48 | + |
| 49 | + ProcessBuilder pb = new ProcessBuilder("powershell", "-NoLogo", "-NoProfile", |
| 50 | + "-Command", psCommand).redirectErrorStream(true); |
| 51 | + Process p = pb.start(); |
| 52 | + p.getOutputStream().close(); |
| 53 | + |
| 54 | + try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) { |
| 55 | + for (String line = br.readLine(); line != null; line = br.readLine()) { |
| 56 | + checkFileProperty(line); |
| 57 | + } |
| 58 | + } |
| 59 | + p.waitFor(); |
| 60 | + int exitCode = p.exitValue(); |
| 61 | + if (exitCode != 0) { |
| 62 | + throw new RuntimeException("ExitCode is " + exitCode + ". PowerShell command failed."); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static void checkFileProperty(String line) { |
| 67 | + // line format |
| 68 | + // FileName;CompanyName;FileDescription;FileVersion;InternalName;Language;LegalCopyright; |
| 69 | + // OriginalFilename;ProductName;ProductVersion |
| 70 | + String[] data = line.split(";", -1); |
| 71 | + String filename = data[0].substring(data[0].lastIndexOf(System.getProperty("file.separator")) + 1); |
| 72 | + |
| 73 | + // skip Microsoft redist dll |
| 74 | + if (filename.startsWith("api-ms-win") || |
| 75 | + filename.startsWith("msvcp") || |
| 76 | + filename.startsWith("ucrtbase") || |
| 77 | + filename.startsWith("vcruntime")) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + for (int i = 1; i < data.length; i++) { |
| 82 | + if (data[i] == null || data[i].isEmpty()) { |
| 83 | + if (!filename.equals("freetype.dll") && !filename.equals("sawindbg.dll")) { |
| 84 | + throw new RuntimeException("data[" + i + "] should not be empty for " + filename); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments