Skip to content

Commit

Permalink
8320943: Files/probeContentType/Basic.java fails on latest Windows 11…
Browse files Browse the repository at this point in the history
… - content type mismatch

Reviewed-by: mdoerr
Backport-of: 87516e29dc5015c4cab2c07c5539ad30f2768667
  • Loading branch information
Amos Shi authored and TheRealMDoerr committed Mar 14, 2024
1 parent 6488725 commit 27cf2f4
Showing 1 changed file with 62 additions and 43 deletions.
105 changes: 62 additions & 43 deletions test/jdk/java/nio/file/Files/probeContentType/Basic.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,25 +23,28 @@

/* @test
* @bug 4313887 8129632 8129633 8162624 8146215 8162745 8273655 8274171
* @library /test/lib
* @modules java.base/jdk.internal.util
* @summary Unit test for probeContentType method
* @library ../..
* @build Basic SimpleFileTypeDetector
* @build jdk.test.lib.OSVersion jdk.test.lib.Platform Basic SimpleFileTypeDetector
* @run main/othervm Basic
*/

import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import jdk.test.lib.Platform;
import jdk.test.lib.OSVersion;

/**
* Uses Files.probeContentType to probe html file, custom file type, and minimal
* set of file extension to content type mappings.
*/
public class Basic {
private static final boolean IS_UNIX =
! System.getProperty("os.name").startsWith("Windows");

static Path createHtmlFile() throws IOException {
Path file = Files.createTempFile("foo", ".html");
try (OutputStream out = Files.newOutputStream(file)) {
Expand Down Expand Up @@ -77,7 +80,7 @@ private static int checkContentTypes(String expected, String actual) {
assert actual != null;

if (!expected.equals(actual)) {
if (IS_UNIX) {
if (!Platform.isWindows()) {
Path userMimeTypes =
Paths.get(System.getProperty("user.home"), ".mime.types");
checkMimeTypesFile(userMimeTypes);
Expand All @@ -96,12 +99,12 @@ private static int checkContentTypes(String expected, String actual) {
return 0;
}

static int checkContentTypes(ExType[] exTypes)
static int checkContentTypes(List<ExType> exTypes)
throws IOException {
int failures = 0;
for (int i = 0; i < exTypes.length; i++) {
String extension = exTypes[i].extension();
List<String> expectedTypes = exTypes[i].expectedTypes();
for (ExType exType : exTypes) {
String extension = exType.extension();
List<String> expectedTypes = exType.expectedTypes();
Path file = Files.createTempFile("foo", "." + extension);
try {
String type = Files.probeContentType(file);
Expand Down Expand Up @@ -153,39 +156,55 @@ public static void main(String[] args) throws IOException {
}

// Verify that certain extensions are mapped to the correct type.
var exTypes = new ExType[] {
new ExType("adoc", List.of("text/plain")),
new ExType("bz2", List.of("application/bz2", "application/x-bzip2", "application/x-bzip")),
new ExType("css", List.of("text/css")),
new ExType("csv", List.of("text/csv")),
new ExType("doc", List.of("application/msword")),
new ExType("docx", List.of("application/vnd.openxmlformats-officedocument.wordprocessingml.document")),
new ExType("gz", List.of("application/gzip", "application/x-gzip")),
new ExType("jar", List.of("application/java-archive", "application/x-java-archive", "application/jar")),
new ExType("jpg", List.of("image/jpeg")),
new ExType("js", List.of("text/javascript", "application/javascript")),
new ExType("json", List.of("application/json")),
new ExType("markdown", List.of("text/markdown")),
new ExType("md", List.of("text/markdown", "application/x-genesis-rom")),
new ExType("mp3", List.of("audio/mpeg")),
new ExType("mp4", List.of("video/mp4")),
new ExType("odp", List.of("application/vnd.oasis.opendocument.presentation")),
new ExType("ods", List.of("application/vnd.oasis.opendocument.spreadsheet")),
new ExType("odt", List.of("application/vnd.oasis.opendocument.text")),
new ExType("pdf", List.of("application/pdf")),
new ExType("php", List.of("text/plain", "text/php", "application/x-php")),
new ExType("png", List.of("image/png")),
new ExType("ppt", List.of("application/vnd.ms-powerpoint")),
new ExType("pptx",List.of("application/vnd.openxmlformats-officedocument.presentationml.presentation")),
new ExType("py", List.of("text/plain", "text/x-python", "text/x-python-script")),
new ExType("rar", List.of("application/rar", "application/vnd.rar", "application/x-rar", "application/x-rar-compressed")),
new ExType("rtf", List.of("application/rtf", "text/rtf")),
new ExType("webm", List.of("video/webm")),
new ExType("webp", List.of("image/webp")),
new ExType("xls", List.of("application/vnd.ms-excel")),
new ExType("xlsx", List.of("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")),
new ExType("7z", List.of("application/x-7z-compressed")),
};
List<ExType> exTypes = new ArrayList<ExType>();

// extensions with consistent content type
exTypes.add(new ExType("adoc", List.of("text/plain")));
exTypes.add(new ExType("css", List.of("text/css")));
exTypes.add(new ExType("doc", List.of("application/msword")));
exTypes.add(new ExType("docx", List.of("application/vnd.openxmlformats-officedocument.wordprocessingml.document")));
exTypes.add(new ExType("gz", List.of("application/gzip", "application/x-gzip")));
exTypes.add(new ExType("jar", List.of("application/java-archive", "application/x-java-archive", "application/jar")));
exTypes.add(new ExType("jpg", List.of("image/jpeg")));
exTypes.add(new ExType("js", List.of("text/plain", "text/javascript", "application/javascript")));
exTypes.add(new ExType("json", List.of("application/json")));
exTypes.add(new ExType("markdown", List.of("text/markdown")));
exTypes.add(new ExType("md", List.of("text/markdown", "application/x-genesis-rom")));
exTypes.add(new ExType("mp3", List.of("audio/mpeg")));
exTypes.add(new ExType("mp4", List.of("video/mp4")));
exTypes.add(new ExType("odp", List.of("application/vnd.oasis.opendocument.presentation")));
exTypes.add(new ExType("ods", List.of("application/vnd.oasis.opendocument.spreadsheet")));
exTypes.add(new ExType("odt", List.of("application/vnd.oasis.opendocument.text")));
exTypes.add(new ExType("pdf", List.of("application/pdf")));
exTypes.add(new ExType("php", List.of("text/plain", "text/php", "application/x-php")));
exTypes.add(new ExType("png", List.of("image/png")));
exTypes.add(new ExType("ppt", List.of("application/vnd.ms-powerpoint")));
exTypes.add(new ExType("pptx", List.of("application/vnd.openxmlformats-officedocument.presentationml.presentation")));
exTypes.add(new ExType("py", List.of("text/plain", "text/x-python", "text/x-python-script")));
exTypes.add(new ExType("webm", List.of("video/webm")));
exTypes.add(new ExType("webp", List.of("image/webp")));
exTypes.add(new ExType("xls", List.of("application/vnd.ms-excel")));
exTypes.add(new ExType("xlsx", List.of("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")));
// exTypes.add(new ExType("wasm", List.of("application/wasm"))); Note. "wasm" is added via JDK-8297609 (Java 20) which not exist in current java version yet

// extensions with content type that differs on Windows 11+
if (Platform.isWindows() &&
(System.getProperty("os.name").endsWith("11") ||
new OSVersion(10, 0).compareTo(OSVersion.current()) > 0)) {
System.out.println("Windows 11+ detected: using different types");
exTypes.add(new ExType("bz2", List.of("application/bz2", "application/x-bzip2", "application/x-bzip", "application/x-compressed")));
exTypes.add(new ExType("csv", List.of("text/csv", "application/vnd.ms-excel")));
exTypes.add(new ExType("rar", List.of("application/rar", "application/vnd.rar", "application/x-rar", "application/x-rar-compressed", "application/x-compressed")));
exTypes.add(new ExType("rtf", List.of("application/rtf", "text/rtf", "application/msword")));
exTypes.add(new ExType("7z", List.of("application/x-7z-compressed", "application/x-compressed")));
} else {
exTypes.add(new ExType("bz2", List.of("application/bz2", "application/x-bzip2", "application/x-bzip")));
exTypes.add(new ExType("csv", List.of("text/csv")));
exTypes.add(new ExType("rar", List.of("application/rar", "application/vnd.rar", "application/x-rar", "application/x-rar-compressed")));
exTypes.add(new ExType("rtf", List.of("application/rtf", "text/rtf")));
exTypes.add(new ExType("7z", List.of("application/x-7z-compressed")));
}

failures += checkContentTypes(exTypes);

if (failures > 0) {
Expand Down

1 comment on commit 27cf2f4

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.