From b6882078d7187bc94d018281cd05ea463d50e947 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 7 Oct 2019 16:10:21 -0700 Subject: [PATCH] Ignore ERROR_NOT_FOUND in File.Encrypt test (dotnet/corefx#41604) Makes the test pass when EFS (Encrypted File System) is not available. I have also fixed the interop definition for Encrypt/DecryptFile P/Invokes while I was on it. Fixes dotnet/corefx#39211 Commit migrated from https://github.com/dotnet/corefx/commit/8c8bf07d52389d8a2948460d4f7e0dba112c20a0 --- .../Windows/{Kernel32 => Advapi32}/Interop.EncryptDecrypt.cs | 6 +++--- .../Microsoft.IO.Redist/src/Microsoft.IO.Redist.csproj | 2 +- .../System.IO.FileSystem/src/System.IO.FileSystem.csproj | 2 +- .../System.IO.FileSystem/src/System/IO/FileSystem.Win32.cs | 4 ++-- .../System.IO.FileSystem/tests/File/EncryptDecrypt.cs | 11 ++++++++++- 5 files changed, 17 insertions(+), 8 deletions(-) rename src/libraries/Common/src/Interop/Windows/{Kernel32 => Advapi32}/Interop.EncryptDecrypt.cs (86%) diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.EncryptDecrypt.cs b/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.EncryptDecrypt.cs similarity index 86% rename from src/libraries/Common/src/Interop/Windows/Kernel32/Interop.EncryptDecrypt.cs rename to src/libraries/Common/src/Interop/Windows/Advapi32/Interop.EncryptDecrypt.cs index 4064a27..db4149d 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.EncryptDecrypt.cs +++ b/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.EncryptDecrypt.cs @@ -7,12 +7,12 @@ using System.Runtime.InteropServices; internal partial class Interop { - internal partial class Kernel32 + internal partial class Advapi32 { /// /// WARNING: This method does not implicitly handle long paths. Use EncryptFile. /// - [DllImport(Libraries.Advapi32, EntryPoint = "EncryptFileW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)] + [DllImport(Libraries.Advapi32, EntryPoint = "EncryptFileW", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool EncryptFilePrivate(string lpFileName); internal static bool EncryptFile(string path) @@ -24,7 +24,7 @@ internal partial class Interop /// /// WARNING: This method does not implicitly handle long paths. Use DecryptFile. /// - [DllImport(Libraries.Advapi32, EntryPoint = "DecryptFileW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)] + [DllImport(Libraries.Advapi32, EntryPoint = "DecryptFileW", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool DecryptFileFilePrivate(string lpFileName, int dwReserved); internal static bool DecryptFile(string path) diff --git a/src/libraries/Microsoft.IO.Redist/src/Microsoft.IO.Redist.csproj b/src/libraries/Microsoft.IO.Redist/src/Microsoft.IO.Redist.csproj index 4311245..cea8c95 100644 --- a/src/libraries/Microsoft.IO.Redist/src/Microsoft.IO.Redist.csproj +++ b/src/libraries/Microsoft.IO.Redist/src/Microsoft.IO.Redist.csproj @@ -159,7 +159,7 @@ Common\Interop\Windows\Interop.GenericOperations.cs - + Common\Interop\Windows\Interop.EncryptDecrypt.cs diff --git a/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj b/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj index 6f3752d..5b56cec 100644 --- a/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj +++ b/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj @@ -184,7 +184,7 @@ Common\Interop\Windows\Interop.CreateFile.cs - + Common\Interop\Windows\Interop.EncryptDecrypt.cs diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Win32.cs b/src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Win32.cs index 37cd344..95828b8 100644 --- a/src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Win32.cs +++ b/src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Win32.cs @@ -19,7 +19,7 @@ namespace System.IO { string fullPath = Path.GetFullPath(path); - if (!Interop.Kernel32.EncryptFile(fullPath)) + if (!Interop.Advapi32.EncryptFile(fullPath)) { ThrowExceptionEncryptDecryptFail(fullPath); } @@ -29,7 +29,7 @@ namespace System.IO { string fullPath = Path.GetFullPath(path); - if (!Interop.Kernel32.DecryptFile(fullPath)) + if (!Interop.Advapi32.DecryptFile(fullPath)) { ThrowExceptionEncryptDecryptFail(fullPath); } diff --git a/src/libraries/System.IO.FileSystem/tests/File/EncryptDecrypt.cs b/src/libraries/System.IO.FileSystem/tests/File/EncryptDecrypt.cs index 9e70a1f..ea32fe0 100644 --- a/src/libraries/System.IO.FileSystem/tests/File/EncryptDecrypt.cs +++ b/src/libraries/System.IO.FileSystem/tests/File/EncryptDecrypt.cs @@ -41,7 +41,16 @@ namespace System.IO.Tests string fileContentRead = File.ReadAllText(tmpFileName); Assert.Equal(textContentToEncrypt, fileContentRead); - File.Encrypt(tmpFileName); + try + { + File.Encrypt(tmpFileName); + } + catch (IOException e) when (e.HResult == unchecked((int)0x80070490)) + { + // Ignore ERROR_NOT_FOUND 1168 (0x490). It is reported when EFS is disabled by domain policy. + return; + } + Assert.Equal(fileContentRead, File.ReadAllText(tmpFileName)); Assert.Equal(FileAttributes.Encrypted, (FileAttributes.Encrypted & File.GetAttributes(tmpFileName))); -- 2.7.4