Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ internal static partial class Interop
{
internal static partial class CoreFoundation
{
internal enum CFNumberType
// CFNumberType is based on CFIndex, which is 64-bit on supported Apple platforms.
internal enum CFNumberType : long
{
kCFNumberIntType = 9,
}

[LibraryImport(Libraries.CoreFoundationLibrary)]
private static unsafe partial int CFNumberGetValue(IntPtr handle, CFNumberType type, int* value);
private static unsafe partial byte CFNumberGetValue(IntPtr handle, CFNumberType type, int* value);
}
}
52 changes: 52 additions & 0 deletions src/libraries/System.Net.Http/tests/UnitTests/CFProxyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using Xunit;

internal static partial class Interop
{
internal static partial class Libraries
{
// The unit-test assembly also includes Windows interop for the proxy fakes.
internal const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
internal const string CFNetworkLibrary = "/System/Library/Frameworks/CFNetwork.framework/CFNetwork";
}
}

namespace System.Net.Http.Tests
{
public partial class CFProxyTest
{
[LibraryImport(Interop.Libraries.CFNetworkLibrary)]
private static partial SafeCFArrayHandle CFNetworkCopyProxiesForAutoConfigurationScript(
SafeCreateHandle script, SafeCreateHandle targetUrl, out SafeCFErrorHandle error);

[Theory]
[InlineData("PROXY 127.0.0.1:1", 1)]
[InlineData("PROXY 127.0.0.1:80", 80)]
[InlineData("PROXY 127.0.0.1:443", 443)]
[InlineData("PROXY 127.0.0.1:8080", 8080)]
[InlineData("PROXY 127.0.0.1:65535", 65535)]
[InlineData("DIRECT", -1)]
public void ProxyAutoConfiguration_ReadsPortNumber(string scriptResult, int expectedPort)
{
using SafeCreateHandle script = Interop.CoreFoundation.CFStringCreateWithCString(
$"function FindProxyForURL(url, host) {{ return '{scriptResult}'; }}");
using SafeCreateHandle url = Interop.CoreFoundation.CFURLCreateWithString("https://example.invalid/");
using SafeCFArrayHandle proxies = CFNetworkCopyProxiesForAutoConfigurationScript(script, url, out SafeCFErrorHandle error);
using (error)
{
Assert.True(error.IsInvalid, Interop.CoreFoundation.GetErrorDescription(error));
Assert.False(proxies.IsInvalid);
Assert.Equal(1, Interop.CoreFoundation.CFArrayGetCount(proxies));

using SafeCFDictionaryHandle dictionary = new SafeCFDictionaryHandle(
Interop.CoreFoundation.CFArrayGetValueAtIndex(proxies, 0), ownsHandle: false);
Interop.CoreFoundation.CFProxy proxy = new Interop.CoreFoundation.CFProxy(dictionary);
Assert.Equal(expectedPort, proxy.PortNumber);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,27 @@
<Compile Include="..\..\..\System.Net.Http.WinHttpHandler\tests\UnitTests\TestServer.cs"
Link="WinHttpHandler\UnitTests\TestServer.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'osx' or '$(TargetPlatformIdentifier)' == 'ios' or '$(TargetPlatformIdentifier)' == 'tvos' or '$(TargetPlatformIdentifier)' == 'maccatalyst'">
<Compile Include="CFProxyTest.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.CoreFoundation.cs"
Link="Common\Interop\OSX\Interop.CoreFoundation.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.CoreFoundation.CFArray.cs"
Link="Common\Interop\OSX\Interop.CoreFoundation.CFArray.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.CoreFoundation.CFData.cs"
Link="Common\Interop\OSX\Interop.CoreFoundation.CFData.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.CoreFoundation.CFDictionary.cs"
Link="Common\Interop\OSX\Interop.CoreFoundation.CFDictionary.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.CoreFoundation.CFError.cs"
Link="Common\Interop\OSX\Interop.CoreFoundation.CFError.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.CoreFoundation.CFNumber.cs"
Link="Common\Interop\OSX\Interop.CoreFoundation.CFNumber.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.CoreFoundation.CFProxy.cs"
Link="Common\Interop\OSX\Interop.CoreFoundation.CFProxy.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.CoreFoundation.CFString.cs"
Link="Common\Interop\OSX\Interop.CoreFoundation.CFString.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.CoreFoundation.CFUrl.cs"
Link="Common\Interop\OSX\Interop.CoreFoundation.CFUrl.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeCreateHandle.OSX.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeCreateHandle.OSX.cs" />
</ItemGroup>
</Project>
Loading