Add path tests
authorJeremy Kuhne <jeremy.kuhne@microsoft.com>
Thu, 24 Mar 2016 01:39:16 +0000 (18:39 -0700)
committerStephen Toub <stoub@microsoft.com>
Sun, 3 Jul 2016 14:40:53 +0000 (10:40 -0400)
Add tests for path error mapping

Commit migrated from https://github.com/dotnet/corefx/commit/40a0c3d8f6e9075762075c8de615f9b511ab145e

src/libraries/Common/tests/Common.Tests.csproj
src/libraries/Common/tests/Resources/Strings.resx
src/libraries/Common/tests/Tests/System/IO/Win32Marshal.Tests.cs [new file with mode: 0644]

index a85926d..bc059fc 100644 (file)
     <Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.GetFullPathNameW.cs">
       <Link>Common\Interop\Windows\mincore\Interop.GetFullPathNameW.cs</Link>
     </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.FormatMessage.cs">
+      <Link>Common\Interop\Windows\mincore\Interop.FormatMessage.cs</Link>
+    </Compile>
+    <Compile Include="$(CommonPath)\Interop\Windows\mincore\Interop.Errors.cs">
+      <Link>Common\Interop\Windows\mincore\Interop.Errors.cs</Link>
+    </Compile>
+    <Compile Include="$(CommonPath)\System\IO\Win32Marshal.cs">
+      <Link>Common\System\IO\Win32Marshal.cs</Link>
+    </Compile>
+    <Compile Include="Tests\System\IO\Win32Marshal.Tests.cs" />
   </ItemGroup>
   <ItemGroup Condition="'$(TargetsUnix)'=='true'">
     <Compile Include="..\src\System\IO\PathInternal.Unix.cs">
       <Link>Common\Interop\Unix\Interop.Libraries.cs</Link>
     </Compile>
   </ItemGroup>
+  <ItemGroup>
+    <Folder Include="System\Net\Sockets\" />
+    <Folder Include="System\Net\VirtualNetwork\" />
+  </ItemGroup>
   <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
-</Project>
+</Project>
\ No newline at end of file
index 90023c9..26af9eb 100644 (file)
                <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
        </resheader>
   <data name="Argument_InvalidPathChars" xml:space="preserve">
-    <value>Illegal characters in path.</value>
+    <value>Argument_InvalidPathChars</value>
+  </data>
+  <data name="IO_FileNotFound" xml:space="preserve">
+    <value>IO_FileNotFound</value>
+  </data>
+    <data name="IO_FileNotFound_FileName" xml:space="preserve">
+    <value>IO_FileNotFound_FileName {0}</value>
+  </data>
+  <data name="IO_PathNotFound_NoPathName" xml:space="preserve">
+    <value>IO_PathNotFound_NoPathName</value>
+  </data>
+  <data name="IO_PathNotFound_Path" xml:space="preserve">
+    <value>IO_PathNotFound_Path {0}</value>
+  </data>
+  <data name="UnauthorizedAccess_IODenied_NoPathName" xml:space="preserve">
+    <value>UnauthorizedAccess_IODenied_NoPathName</value>
+  </data>
+  <data name="UnauthorizedAccess_IODenied_Path" xml:space="preserve">
+    <value>UnauthorizedAccess_IODenied_Path {0}</value>
+  </data>
+  <data name="IO_AlreadyExists_Name" xml:space="preserve">
+    <value>IO_AlreadyExists_Name {0}</value>
+  </data>
+  <data name="IO_FileExists_Name" xml:space="preserve">
+    <value>IO_FileExists_Name {0}</value>
+  </data>
+  <data name="IO_PathTooLong" xml:space="preserve">
+    <value>IO_PathTooLong</value>
+  </data>
+  <data name="IO_SharingViolation_NoFileName" xml:space="preserve">
+    <value>IO_SharingViolation_NoFileName</value>
+  </data>
+  <data name="IO_SharingViolation_File" xml:space="preserve">
+    <value>IO_SharingViolation_File {0}</value>
   </data>
 </root>
\ No newline at end of file
diff --git a/src/libraries/Common/tests/Tests/System/IO/Win32Marshal.Tests.cs b/src/libraries/Common/tests/Tests/System/IO/Win32Marshal.Tests.cs
new file mode 100644 (file)
index 0000000..9a30e37
--- /dev/null
@@ -0,0 +1,105 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.IO;
+using System.Text;
+using Xunit;
+
+namespace Tests.System.IO
+{
+    public class Win32MarshalTests
+    {
+        [Theory]
+        [InlineData("", Interop.mincore.Errors.ERROR_ALREADY_EXISTS, "")]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_ALREADY_EXISTS, "IO_AlreadyExists_Name")]
+        [InlineData("", Interop.mincore.Errors.ERROR_INVALID_PARAMETER, "")]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_INVALID_PARAMETER, "")]
+        [InlineData("", Interop.mincore.Errors.ERROR_SHARING_VIOLATION, "IO_SharingViolation_NoFileName")]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_SHARING_VIOLATION, "IO_SharingViolation_File")]
+        [InlineData("", Interop.mincore.Errors.ERROR_FILE_EXISTS, "")]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_FILE_EXISTS, "IO_FileExists_Name")]
+        // This is a random error we don't explicitly check
+        [InlineData("", Interop.mincore.Errors.ERROR_INVALID_SID, "")]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_INVALID_SID, "")]
+        public void IOExceptionErrors(string path, int errorCode, string error)
+        {
+            var exception = Win32Marshal.GetExceptionForWin32Error(errorCode, path);
+            Assert.IsType<IOException>(exception);
+            Assert.Equal(Win32Marshal.MakeHRFromErrorCode(errorCode), exception.HResult);
+            if (!string.IsNullOrEmpty(error))
+            {
+                Assert.StartsWith(error, exception.Message);
+                if (!string.IsNullOrEmpty(path))
+                {
+                    Assert.EndsWith(path, exception.Message);
+                }
+            }
+        }
+
+        [Theory]
+        [InlineData("", Interop.mincore.Errors.ERROR_FILE_NOT_FOUND, "IO_FileNotFound")]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_FILE_NOT_FOUND, "IO_FileNotFound_FileName")]
+        public void FileNotFoundErrors(string path, int errorCode, string error)
+        {
+            var exception = Win32Marshal.GetExceptionForWin32Error(errorCode, path);
+            Assert.IsType<FileNotFoundException>(exception);
+
+            Assert.StartsWith(error, exception.Message);
+            if (!string.IsNullOrEmpty(path))
+            {
+                Assert.EndsWith(path, exception.Message);
+            }
+        }
+
+        [Theory]
+        [InlineData("", Interop.mincore.Errors.ERROR_PATH_NOT_FOUND, "IO_PathNotFound_NoPathName")]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_PATH_NOT_FOUND, "IO_PathNotFound_Path")]
+        public void DirectoryNotFoundErrors(string path, int errorCode, string error)
+        {
+            var exception = Win32Marshal.GetExceptionForWin32Error(errorCode, path);
+            Assert.IsType<DirectoryNotFoundException>(exception);
+
+            Assert.StartsWith(error, exception.Message);
+            if (!string.IsNullOrEmpty(path))
+            {
+                Assert.EndsWith(path, exception.Message);
+            }
+        }
+
+        [Theory]
+        [InlineData("", Interop.mincore.Errors.ERROR_ACCESS_DENIED, "UnauthorizedAccess_IODenied_NoPathName")]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_ACCESS_DENIED, "UnauthorizedAccess_IODenied_Path")]
+        public void UnauthorizedAccessErrors(string path, int errorCode, string error)
+        {
+            var exception = Win32Marshal.GetExceptionForWin32Error(errorCode, path);
+            Assert.IsType<UnauthorizedAccessException>(exception);
+
+            Assert.StartsWith(error, exception.Message);
+            if (!string.IsNullOrEmpty(path))
+            {
+                Assert.EndsWith(path, exception.Message);
+            }
+        }
+
+        [Theory]
+        [InlineData("", Interop.mincore.Errors.ERROR_FILENAME_EXCED_RANGE)]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_FILENAME_EXCED_RANGE)]
+        public void PathTooLongErrors(string path, int errorCode)
+        {
+            var exception = Win32Marshal.GetExceptionForWin32Error(errorCode, path);
+            Assert.IsType<PathTooLongException>(exception);
+            Assert.StartsWith("IO_PathTooLong", exception.Message);
+        }
+
+        [Theory]
+        [InlineData("", Interop.mincore.Errors.ERROR_OPERATION_ABORTED)]
+        [InlineData("foo", Interop.mincore.Errors.ERROR_OPERATION_ABORTED)]
+        public void OperationCancelledErrors(string path, int errorCode)
+        {
+            var exception = Win32Marshal.GetExceptionForWin32Error(errorCode, path);
+            Assert.IsType<OperationCanceledException>(exception);
+        }
+    }
+}