WASM: Fix System.Net.Primitives and tests (#39748)
authorAlexander Köplinger <alex.koeplinger@outlook.com>
Thu, 23 Jul 2020 14:43:07 +0000 (16:43 +0200)
committerGitHub <noreply@github.com>
Thu, 23 Jul 2020 14:43:07 +0000 (16:43 +0200)
Add HostInformationPal and InterfaceInfoPal implementations for Browser.

In CookiePortTest.cs use example.com instead of localhost for the tests since Browser uses `localhost` as the `Environment.MachineName` and that changes the test values.

Allows the tests to run on WebAssembly:
```
System.Net.Primitives.Pal.Tests: Tests run: 60, Errors: 0, Failures: 0, Skipped: 0. Time: 0.15872s
System.Net.Primitives.Functional.Tests: Tests run: 2620, Errors: 0, Failures: 0, Skipped: 1. Time: 3.145804s
```

src/libraries/Common/src/System/Net/NetworkInformation/HostInformationPal.Browser.cs [new file with mode: 0644]
src/libraries/Common/src/System/Net/NetworkInformation/InterfaceInfoPal.Browser.cs [new file with mode: 0644]
src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj
src/libraries/System.Net.Primitives/tests/FunctionalTests/CookieTest/CookiePortTest.cs
src/libraries/System.Net.Primitives/tests/FunctionalTests/SocketAddressTest.cs
src/libraries/System.Net.Primitives/tests/PalTests/System.Net.Primitives.Pal.Tests.csproj
src/libraries/tests.proj

diff --git a/src/libraries/Common/src/System/Net/NetworkInformation/HostInformationPal.Browser.cs b/src/libraries/Common/src/System/Net/NetworkInformation/HostInformationPal.Browser.cs
new file mode 100644 (file)
index 0000000..01c30fd
--- /dev/null
@@ -0,0 +1,18 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System.Net.NetworkInformation
+{
+    internal static class HostInformationPal
+    {
+        public static string GetHostName()
+        {
+            return Environment.MachineName;
+        }
+
+        public static string GetDomainName()
+        {
+            return Environment.UserDomainName;
+        }
+    }
+}
diff --git a/src/libraries/Common/src/System/Net/NetworkInformation/InterfaceInfoPal.Browser.cs b/src/libraries/Common/src/System/Net/NetworkInformation/InterfaceInfoPal.Browser.cs
new file mode 100644 (file)
index 0000000..d5d50a2
--- /dev/null
@@ -0,0 +1,14 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System.Net.NetworkInformation
+{
+    internal static class InterfaceInfoPal
+    {
+        public static uint InterfaceNameToIndex(string interfaceName)
+        {
+            // zero means "unknown"
+            return 0;
+        }
+    }
+}
index 8c5c2d9..cfc1e85 100644 (file)
     <Compile Include="System\Net\SocketException.Unix.cs" />
     <Compile Include="$(CommonPath)System\Net\SocketAddressPal.Unix.cs"
              Link="Common\System\Net\SocketAddressPal.Unix.cs" />
-    <Compile Include="$(CommonPath)System\Net\NetworkInformation\HostInformationPal.Unix.cs"
-             Link="Common\System\Net\NetworkInformation\HostInformationPal.Unix.cs" />
     <Compile Include="$(CommonPath)System\Net\Sockets\SocketErrorPal.Unix.cs"
              Link="Common\System\Net\Sockets\SocketErrorPal.Unix.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs"
              Link="Common\Interop\Unix\Interop.Errors.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
              Link="Common\Interop\Unix\Interop.Libraries.cs" />
+    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.IPAddress.cs"
+             Link="Common\Interop\Unix\System.Native\Interop.IPAddress.cs" />
+    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.SocketAddress.cs"
+             Link="Common\Interop\Unix\System.Native\Interop.SocketAddress.cs" />
+  </ItemGroup>
+  <ItemGroup Condition="'$(TargetsUnix)' == 'true'">
+    <Compile Include="$(CommonPath)System\Net\NetworkInformation\HostInformationPal.Unix.cs"
+             Link="Common\System\Net\NetworkInformation\HostInformationPal.Unix.cs" />
+    <Compile Include="$(CommonPath)System\Net\NetworkInformation\InterfaceInfoPal.Unix.cs"
+             Link="Common\System\Net\NetworkInformation\InterfaceInfoPal.Unix.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetDomainName.cs"
              Link="Common\Interop\Unix\System.Native\Interop.GetDomainName.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetHostName.cs"
              Link="Common\Interop\Unix\System.Native\Interop.GetNameInfo.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.HostEntry.cs"
              Link="Common\Interop\Unix\System.Native\Interop.HostEntry.cs" />
-    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.IPAddress.cs"
-             Link="Common\Interop\Unix\System.Native\Interop.IPAddress.cs" />
-    <Compile Include="$(CommonPath)System\Net\NetworkInformation\InterfaceInfoPal.Unix.cs"
-             Link="Common\System\Net\NetworkInformation\InterfaceInfoPal.Unix.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.InterfaceNameToIndex.cs"
              Link="Common\Interop\Unix\System.Native\Interop.InterfaceNameToIndex.cs" />
-    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.SocketAddress.cs"
-             Link="Common\Interop\Unix\System.Native\Interop.SocketAddress.cs" />
+  </ItemGroup>
+  <ItemGroup Condition="'$(TargetsBrowser)' == 'true'">
+    <Compile Include="$(CommonPath)System\Net\NetworkInformation\HostInformationPal.Browser.cs"
+             Link="Common\System\Net\NetworkInformation\HostInformationPal.Browser.cs" />
+    <Compile Include="$(CommonPath)System\Net\NetworkInformation\InterfaceInfoPal.Browser.cs"
+             Link="Common\System\Net\NetworkInformation\InterfaceInfoPal.Browser.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Microsoft.Win32.Primitives" />
index d7a3428..5ed895d 100644 (file)
@@ -13,65 +13,65 @@ namespace System.Net.Primitives.Functional.Tests
         public CookiePortTest()
         {
             _cc = new CookieContainer();
-            _cookie = new Cookie("name", "value1", "/path", "localhost");
+            _cookie = new Cookie("name", "value1", "/path", "example.com");
             // use both space and comma as delimiter
             _cookie.Port = "\"80 110,1050, 1090 ,1100\"";
-            _cc.Add(new Uri("http://localhost/path"), _cookie);
+            _cc.Add(new Uri("http://example.com/path"), _cookie);
         }
 
         [Fact]
         public void Port_SetMultiplePorts_Port1Set()
         {
-            CookieCollection cookies = _cc.GetCookies(new Uri("http://localhost:80/path"));
+            CookieCollection cookies = _cc.GetCookies(new Uri("http://example.com:80/path"));
             Assert.Equal(1, cookies.Count);
         }
 
         [Fact]
         public void Port_SpaceDelimiter_PortSet()
         {
-            CookieCollection cookies = _cc.GetCookies(new Uri("http://localhost:110/path"));
+            CookieCollection cookies = _cc.GetCookies(new Uri("http://example.com:110/path"));
             Assert.Equal(1, cookies.Count);
         }
 
         [Fact]
         public void Port_CommaDelimiter_PortSet()
         {
-            CookieCollection cookies = _cc.GetCookies(new Uri("http://localhost:1050/path"));
+            CookieCollection cookies = _cc.GetCookies(new Uri("http://example.com:1050/path"));
             Assert.Equal(1, cookies.Count);
         }
 
         [Fact]
         public void Port_CommaSpaceDelimiter_PortSet()
         {
-            CookieCollection cookies = _cc.GetCookies(new Uri("http://localhost:1090/path"));
+            CookieCollection cookies = _cc.GetCookies(new Uri("http://example.com:1090/path"));
             Assert.Equal(1, cookies.Count);
         }
 
         [Fact]
         public void Port_SpaceCommaDelimiter_PortSet()
         {
-            CookieCollection cookies = _cc.GetCookies(new Uri("http://localhost:1100/path"));
+            CookieCollection cookies = _cc.GetCookies(new Uri("http://example.com:1100/path"));
             Assert.Equal(1, cookies.Count);
         }
 
         [Fact]
         public void Port_SetMultiplePorts_NoPortMatch()
         {
-            CookieCollection cookies = _cc.GetCookies(new Uri("http://localhost:1000/path"));
+            CookieCollection cookies = _cc.GetCookies(new Uri("http://example.com:1000/path"));
             Assert.Equal(0, cookies.Count);
         }
 
         [Fact]
         public void Port_SetMultiplePorts_NoPathMatch()
         {
-            CookieCollection cookies = _cc.GetCookies(new Uri("http://localhost:1050"));
+            CookieCollection cookies = _cc.GetCookies(new Uri("http://example.com:1050"));
             Assert.Equal(0, cookies.Count);
         }
 
         [Fact]
         public void Port_NoPortSpecified_ReturnsCookies()
         {
-            CookieCollection cookies = _cc.GetCookies(new Uri("http://localhost/path"));
+            CookieCollection cookies = _cc.GetCookies(new Uri("http://example.com/path"));
             Assert.Equal(1, cookies.Count);
         }
     }
index a4ce20f..af2a89c 100644 (file)
@@ -98,7 +98,7 @@ namespace System.Net.Primitives.Functional.Tests
         [Theory]
         [InlineData(AddressFamily.Packet)]
         [InlineData(AddressFamily.ControllerAreaNetwork)]
-        [PlatformSpecific(~TestPlatforms.Linux)]
+        [PlatformSpecific(~(TestPlatforms.Linux | TestPlatforms.Browser))]
         public static void ToString_UnsupportedFamily_Throws(AddressFamily family)
         {
             Assert.Throws<PlatformNotSupportedException>(() => new SocketAddress(family));
index 38a9e23..212d2de 100644 (file)
@@ -85,8 +85,6 @@
   <ItemGroup Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'">
     <Compile Include="..\..\src\System\Net\SocketException.Unix.cs"
              Link="ProductionCode\System\Net\SocketException.Unix.cs" />
-    <Compile Include="$(CommonPath)System\Net\NetworkInformation\HostInformationPal.Unix.cs"
-             Link="Common\System\Net\NetworkInformation\HostInformationPal.Unix.cs" />
     <Compile Include="$(CommonPath)System\Net\SocketAddressPal.Unix.cs"
              Link="Common\System\Net\SocketAddressPal.Unix.cs" />
     <Compile Include="$(CommonPath)System\Net\Sockets\SocketErrorPal.Unix.cs"
              Link="ProductionCode\Interop\Unix\Interop.Errors.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
              Link="ProductionCode\Common\Interop\Unix\Interop.Libraries.cs" />
-    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetDomainName.cs"
-             Link="ProductionCode\Common\Interop\Unix\System.Native\Interop.GetDomainName.cs" />
-    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetHostName.cs"
-             Link="ProductionCode\Common\Interop\Unix\System.Native\Interop.GetHostName.cs" />
-    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetNameInfo.cs"
-             Link="ProductionCode\Common\Interop\Unix\System.Native\Interop.GetNameInfo.cs" />
-    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.HostEntry.cs"
-             Link="ProductionCode\Common\Interop\Unix\System.Native\Interop.HostEntry.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.IPAddress.cs"
              Link="ProductionCode\Common\Interop\Unix\System.Native\Interop.IPAddress.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.SocketAddress.cs"
              Link="ProductionCode\Common\Interop\Unix\System.Native\Interop.SocketAddress.cs" />
+  </ItemGroup>
+  <ItemGroup Condition="'$(TargetsUnix)' == 'true'">
+    <Compile Include="$(CommonPath)System\Net\NetworkInformation\HostInformationPal.Unix.cs"
+             Link="Common\System\Net\NetworkInformation\HostInformationPal.Unix.cs" />
     <Compile Include="$(CommonPath)System\Net\NetworkInformation\InterfaceInfoPal.Unix.cs"
-             Link="ProductionCode\Common\System\Net\NetworkInformation\InterfaceInfoPal.Unix.cs" />
+             Link="Common\System\Net\NetworkInformation\InterfaceInfoPal.Unix.cs" />
+    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetDomainName.cs"
+             Link="Common\Interop\Unix\System.Native\Interop.GetDomainName.cs" />
+    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetHostName.cs"
+             Link="Common\Interop\Unix\System.Native\Interop.GetHostName.cs" />
+    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetNameInfo.cs"
+             Link="Common\Interop\Unix\System.Native\Interop.GetNameInfo.cs" />
+    <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.HostEntry.cs"
+             Link="Common\Interop\Unix\System.Native\Interop.HostEntry.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.InterfaceNameToIndex.cs"
-             Link="ProductionCode\Common\Interop\Unix\System.Native\Interop.InterfaceNameToIndex.cs" />
+             Link="Common\Interop\Unix\System.Native\Interop.InterfaceNameToIndex.cs" />
+  </ItemGroup>
+  <ItemGroup Condition="'$(TargetsBrowser)' == 'true'">
+    <Compile Include="$(CommonPath)System\Net\NetworkInformation\HostInformationPal.Browser.cs"
+             Link="Common\System\Net\NetworkInformation\HostInformationPal.Browser.cs" />
+    <Compile Include="$(CommonPath)System\Net\NetworkInformation\InterfaceInfoPal.Browser.cs"
+             Link="ProductionCode\Common\System\Net\NetworkInformation\InterfaceInfoPal.Browser.cs" />
   </ItemGroup>
 </Project>
\ No newline at end of file
index 626c4ea..eb67053 100644 (file)
@@ -32,8 +32,6 @@
     <ProjectExclusions Include="$(MSBuildThisFileDirectory)System.IO.FileSystem\tests\System.IO.FileSystem.Tests.csproj" />
     <ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Linq.Parallel\tests\System.Linq.Parallel.Tests.csproj" />
     <ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Net.Http\tests\FunctionalTests\System.Net.Http.Functional.Tests.csproj" />
-    <ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Net.Primitives\tests\FunctionalTests\System.Net.Primitives.Functional.Tests.csproj" />
-    <ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Net.Primitives\tests\PalTests\System.Net.Primitives.Pal.Tests.csproj" />
     <ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Net.WebSockets.Client\tests\System.Net.WebSockets.Client.Tests.csproj" />
     <ProjectExclusions Include="$(MSBuildThisFileDirectory)System.ObjectModel\tests\System.ObjectModel.Tests.csproj" />
     <ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Private.Uri\tests\FunctionalTests\System.Private.Uri.Functional.Tests.csproj" />