Ignore ERROR_NOT_FOUND in File.Encrypt test (dotnet/corefx#41604)
authorJan Kotas <jkotas@microsoft.com>
Mon, 7 Oct 2019 23:10:21 +0000 (16:10 -0700)
committerGitHub <noreply@github.com>
Mon, 7 Oct 2019 23:10:21 +0000 (16:10 -0700)
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

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.EncryptDecrypt.cs [moved from src/libraries/Common/src/Interop/Windows/Kernel32/Interop.EncryptDecrypt.cs with 86% similarity]
src/libraries/Microsoft.IO.Redist/src/Microsoft.IO.Redist.csproj
src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj
src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Win32.cs
src/libraries/System.IO.FileSystem/tests/File/EncryptDecrypt.cs

@@ -7,12 +7,12 @@ using System.Runtime.InteropServices;
 
 internal partial class Interop
 {
-    internal partial class Kernel32
+    internal partial class Advapi32
     {
         /// <summary>
         /// WARNING: This method does not implicitly handle long paths. Use EncryptFile.
         /// </summary>
-        [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
         /// <summary>
         /// WARNING: This method does not implicitly handle long paths. Use DecryptFile.
         /// </summary>
-        [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)
index 4311245..cea8c95 100644 (file)
     <Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.GenericOperations.cs">
       <Link>Common\Interop\Windows\Interop.GenericOperations.cs</Link>
     </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.EncryptDecrypt.cs">
+    <Compile Include="$(CommonPath)\Interop\Windows\Advapi32\Interop.EncryptDecrypt.cs">
       <Link>Common\Interop\Windows\Interop.EncryptDecrypt.cs</Link>
     </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.GetVolumeInformation.cs">
index 6f3752d..5b56cec 100644 (file)
     <Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.CreateFile.cs">
       <Link>Common\Interop\Windows\Interop.CreateFile.cs</Link>
     </Compile>
-    <Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.EncryptDecrypt.cs">
+    <Compile Include="$(CommonPath)\Interop\Windows\Advapi32\Interop.EncryptDecrypt.cs">
       <Link>Common\Interop\Windows\Interop.EncryptDecrypt.cs</Link>
     </Compile>
     <Compile Include="$(CommonPath)\Interop\Windows\Interop.UNICODE_STRING.cs">
index 37cd344..95828b8 100644 (file)
@@ -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);
             }
index 9e70a1f..ea32fe0 100644 (file)
@@ -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)));