Fix Files.isSameFile for non existent files
- Try to harmonize test results
Change-Id: Icac558b8ef320f8f51fba3d9df90f7d9c3139547
diff --git a/src/library_desugar/java/desugar/sun/nio/fs/DesugarAndroidFileSystemProvider.java b/src/library_desugar/java/desugar/sun/nio/fs/DesugarAndroidFileSystemProvider.java
index 59e79d2..6b4a062 100644
--- a/src/library_desugar/java/desugar/sun/nio/fs/DesugarAndroidFileSystemProvider.java
+++ b/src/library_desugar/java/desugar/sun/nio/fs/DesugarAndroidFileSystemProvider.java
@@ -150,4 +150,17 @@
}
return DesugarChannels.openEmulatedFileChannel(path, options, attrs);
}
+
+ @Override
+ public boolean isSameFile(Path path, Path path2) throws IOException {
+ // If the paths are equals, then it answers true even if they do not exist.
+ if (path.equals(path2)) {
+ return true;
+ }
+ // If the paths are not equal, they could still be equal due to symbolic link and so on, but
+ // in that case accessibility is checked.
+ checkAccess(path);
+ checkAccess(path2);
+ return super.isSameFile(path, path2);
+ }
}
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesAttributes2Test.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesAttributes2Test.java
index 1028b08..8bbd564 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesAttributes2Test.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesAttributes2Test.java
@@ -38,10 +38,10 @@
"fileStore:%s",
"lastModifiedTime:true",
"lastModifiedTime:true",
- "owner:true",
- "owner:true",
- "posix:true",
- "posix:true",
+ "owner:%s",
+ "owner:%s",
+ "posix:%s",
+ "posix:%s",
"dir:false",
"dir:false",
"hidden:false",
@@ -52,15 +52,15 @@
"regular:true",
"same:true",
"same:false",
- "symlink:false",
+ "symlink:%s",
"PRESENT DIR",
"fileStore:%s",
"lastModifiedTime:true",
"lastModifiedTime:true",
- "owner:true",
- "owner:true",
- "posix:true",
- "posix:true",
+ "owner:%s",
+ "owner:%s",
+ "posix:%s",
+ "posix:%s",
"dir:true",
"dir:true",
"hidden:false",
@@ -71,15 +71,15 @@
"regular:false",
"same:true",
"same:false",
- "symlink:false",
+ "symlink:%s",
"ABSENT FILE",
"fileStore:%s",
"lastModifiedTime:class java.nio.file.NoSuchFileException :: notExisting.txt",
"lastModifiedTime:class java.nio.file.NoSuchFileException :: notExisting.txt",
- "owner:class java.nio.file.NoSuchFileException :: notExisting.txt",
- "owner:class java.nio.file.NoSuchFileException :: notExisting.txt",
- "posix:class java.nio.file.NoSuchFileException :: notExisting.txt",
- "posix:class java.nio.file.NoSuchFileException :: notExisting.txt",
+ "owner:%s",
+ "owner:%s",
+ "posix:%s",
+ "posix:%s",
"dir:false",
"dir:false",
"hidden:false",
@@ -91,65 +91,6 @@
"same:true",
"same:class java.nio.file.NoSuchFileException :: notExisting.txt",
"symlink:false");
- private static final String EXPECTED_RESULT_ANDROID_DESUGARING =
- StringUtils.lines(
- "PRESENT FILE",
- "fileStore:class java.lang.SecurityException :: getFileStore",
- "lastModifiedTime:true",
- "lastModifiedTime:true",
- "owner:class java.lang.UnsupportedOperationException :: no-message",
- "owner:class java.lang.UnsupportedOperationException :: no-message",
- "posix:class java.lang.UnsupportedOperationException :: no-message",
- "posix:class java.lang.UnsupportedOperationException :: no-message",
- "dir:false",
- "dir:false",
- "hidden:false",
- "readable:true",
- "writable:true",
- "executable:false",
- "regular:true",
- "regular:true",
- "same:true",
- "same:false",
- "symlink:%s",
- "PRESENT DIR",
- "fileStore:class java.lang.SecurityException :: getFileStore",
- "lastModifiedTime:true",
- "lastModifiedTime:true",
- "owner:class java.lang.UnsupportedOperationException :: no-message",
- "owner:class java.lang.UnsupportedOperationException :: no-message",
- "posix:class java.lang.UnsupportedOperationException :: no-message",
- "posix:class java.lang.UnsupportedOperationException :: no-message",
- "dir:true",
- "dir:true",
- "hidden:false",
- "readable:true",
- "writable:true",
- "executable:true",
- "regular:false",
- "regular:false",
- "same:true",
- "same:false",
- "symlink:%s",
- "ABSENT FILE",
- "fileStore:class java.lang.SecurityException :: getFileStore",
- "lastModifiedTime:class java.nio.file.NoSuchFileException :: notExisting.txt",
- "lastModifiedTime:class java.nio.file.NoSuchFileException :: notExisting.txt",
- "owner:class java.lang.UnsupportedOperationException :: no-message",
- "owner:class java.lang.UnsupportedOperationException :: no-message",
- "posix:class java.lang.UnsupportedOperationException :: no-message",
- "posix:class java.lang.UnsupportedOperationException :: no-message",
- "dir:false",
- "dir:false",
- "hidden:false",
- "readable:false",
- "writable:false",
- "executable:false",
- "regular:false",
- "regular:false",
- "same:true",
- "same:false",
- "symlink:false");
private final TestParameters parameters;
private final LibraryDesugaringSpecification libraryDesugaringSpecification;
@@ -179,25 +120,43 @@
}
private String getExpectedResult() {
- if (parameters.isCfRuntime()) {
- return String.format(
- EXPECTED_RESULT,
- "true",
- "true",
- "class java.nio.file.NoSuchFileException :: notExisting.txt");
- }
- if (libraryDesugaringSpecification.usesPlatformFileSystem(parameters)) {
- return String.format(
- EXPECTED_RESULT,
- "class java.lang.SecurityException :: getFileStore",
- "class java.lang.SecurityException :: getFileStore",
- "class java.lang.SecurityException :: getFileStore");
- }
- if (parameters.getDexRuntimeVersion().isEqualTo(Version.V7_0_0)) {
- // Everything is a symlink on 24.
- return String.format(EXPECTED_RESULT_ANDROID_DESUGARING, true, true);
- }
- return String.format(EXPECTED_RESULT_ANDROID_DESUGARING, false, false);
+ // On Android 24, everything seems to be considered a symlink due to canonicalFile being
+ // invalid. It seems it does not reproduce on real device, so this may be an issue from our
+ // test set-up.
+ boolean invalidSymlink =
+ parameters.isDexRuntime() && parameters.getDexRuntimeVersion().isEqualTo(Version.V7_0_0);
+ // On Dex, the security manager is not working in our test set-up.
+ boolean invalidFileStore = parameters.isDexRuntime();
+ // Posix attributes are not available on desugared nio.
+ boolean invalidPosix =
+ parameters.isDexRuntime()
+ && !libraryDesugaringSpecification.usesPlatformFileSystem(parameters);
+
+ String invalidFileStoreString = "class java.lang.SecurityException :: getFileStore";
+ String invalidPosixString = "class java.lang.UnsupportedOperationException :: no-message";
+ String missingFile = "class java.nio.file.NoSuchFileException :: notExisting.txt";
+ String success = "true";
+
+ Object[] vars = {
+ invalidFileStore ? invalidFileStoreString : success,
+ invalidPosix ? invalidPosixString : success,
+ invalidPosix ? invalidPosixString : success,
+ invalidPosix ? invalidPosixString : success,
+ invalidPosix ? invalidPosixString : success,
+ String.valueOf(invalidSymlink),
+ invalidFileStore ? invalidFileStoreString : success,
+ invalidPosix ? invalidPosixString : success,
+ invalidPosix ? invalidPosixString : success,
+ invalidPosix ? invalidPosixString : success,
+ invalidPosix ? invalidPosixString : success,
+ String.valueOf(invalidSymlink),
+ invalidFileStore ? invalidFileStoreString : missingFile,
+ invalidPosix ? invalidPosixString : missingFile,
+ invalidPosix ? invalidPosixString : missingFile,
+ invalidPosix ? invalidPosixString : missingFile,
+ invalidPosix ? invalidPosixString : missingFile,
+ };
+ return String.format(EXPECTED_RESULT, vars);
}
@Test