From e9c272c0afd193f3f6ef2cb1f33b3778bd4adcf7 Mon Sep 17 00:00:00 2001 From: Jose Perez Rodriguez Date: Wed, 20 May 2020 09:09:15 -0700 Subject: [PATCH] Adding OSX support for System.DirectoryServices.Protocols (#36669) * Adding OSX support for System.DirectoryServices.Protocols * Addressing PR Feedback * Fixing issue in netfx builds where local member is assigned but never used --- .../Common/src/Interop/OSX/Interop.Libraries.cs | 1 + .../src/System.DirectoryServices.Protocols.csproj | 17 ++++++++++---- .../tests/DirectoryServicesTestHelpers.cs | 27 ++++++++++------------ ...System.DirectoryServices.Protocols.Tests.csproj | 2 +- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs b/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs index 6c2a0ab..9ba6608 100644 --- a/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs +++ b/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs @@ -14,6 +14,7 @@ internal static partial class Interop internal const string LibSystemCommonCrypto = "/usr/lib/system/libcommonCrypto"; internal const string LibSystemKernel = "/usr/lib/system/libsystem_kernel"; internal const string Odbc32 = "libodbc.2.dylib"; + internal const string OpenLdap = "libldap"; internal const string SystemConfigurationLibrary = "/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration"; internal const string AppleCryptoNative = "System.Security.Cryptography.Native.Apple"; internal const string MsQuic = "msquic"; diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System.DirectoryServices.Protocols.csproj b/src/libraries/System.DirectoryServices.Protocols/src/System.DirectoryServices.Protocols.csproj index 2ffb1d9..03d3102 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System.DirectoryServices.Protocols.csproj +++ b/src/libraries/System.DirectoryServices.Protocols/src/System.DirectoryServices.Protocols.csproj @@ -3,7 +3,7 @@ true $(NoWarn);0649;CA1810 true - $(NetCoreAppCurrent)-Linux;netcoreapp2.0-Linux;$(NetCoreAppCurrent)-Windows_NT;netstandard2.0;netcoreapp2.0-Windows_NT;_$(NetFrameworkCurrent) + $(NetCoreAppCurrent)-Windows_NT;netcoreapp2.0-Windows_NT;$(NetCoreAppCurrent)-OSX;netcoreapp2.0-OSX;$(NetCoreAppCurrent)-Linux;netcoreapp2.0-Linux;netstandard2.0;_$(NetFrameworkCurrent) true @@ -57,16 +57,13 @@ Common\Interop\Windows\Wldap32\Interop.Ber.cs - + - - Common\Interop\Linux\Interop.Libraries.cs - Common\Interop\Linux\OpenLdap\Interop.Ldap.cs @@ -74,6 +71,16 @@ Common\Interop\Linux\OpenLdap\Interop.Ber.cs + + + Common\Interop\Linux\Interop.Libraries.cs + + + + + Common\Interop\OSX\Interop.Libraries.cs + + diff --git a/src/libraries/System.DirectoryServices.Protocols/tests/DirectoryServicesTestHelpers.cs b/src/libraries/System.DirectoryServices.Protocols/tests/DirectoryServicesTestHelpers.cs index 80ac7ec..98d4545 100644 --- a/src/libraries/System.DirectoryServices.Protocols/tests/DirectoryServicesTestHelpers.cs +++ b/src/libraries/System.DirectoryServices.Protocols/tests/DirectoryServicesTestHelpers.cs @@ -16,34 +16,31 @@ namespace System.DirectoryServices.Protocols.Tests // Cache the check once we have performed it once private static bool? _isLibLdapInstalled = null; + /// + /// Returns true if able to PInvoke into Linux or OSX, false otherwise + /// public static bool IsLibLdapInstalled { get { +#if NETCOREAPP if (!_isLibLdapInstalled.HasValue) { - try + if (PlatformDetection.IsOSX) { - // Attempt PInvoking into libldap - IntPtr handle = ber_alloc(1); - ber_free(handle, 1); - _isLibLdapInstalled = true; + _isLibLdapInstalled = NativeLibrary.TryLoad("libldap.dylib", out _); } - catch (Exception) + else { - _isLibLdapInstalled = false; + _isLibLdapInstalled = NativeLibrary.TryLoad("libldap-2.4.so.2", out _); } } return _isLibLdapInstalled.Value; +#else + _isLibLdapInstalled = true; // In .NET Framework ldap is always installed. + return _isLibLdapInstalled.Value; +#endif } } - - internal const string OpenLdap = "libldap-2.4.so.2"; - - [DllImport(OpenLdap, EntryPoint = "ber_alloc_t", CharSet = CharSet.Ansi)] - internal static extern IntPtr ber_alloc(int option); - - [DllImport(OpenLdap, EntryPoint = "ber_free", CharSet = CharSet.Ansi)] - public static extern IntPtr ber_free([In] IntPtr berelement, int option); } } diff --git a/src/libraries/System.DirectoryServices.Protocols/tests/System.DirectoryServices.Protocols.Tests.csproj b/src/libraries/System.DirectoryServices.Protocols/tests/System.DirectoryServices.Protocols.Tests.csproj index 1643277..4c621fb 100644 --- a/src/libraries/System.DirectoryServices.Protocols/tests/System.DirectoryServices.Protocols.Tests.csproj +++ b/src/libraries/System.DirectoryServices.Protocols/tests/System.DirectoryServices.Protocols.Tests.csproj @@ -1,6 +1,6 @@ - $(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-Windows_NT;$(NetFrameworkCurrent) + $(NetCoreAppCurrent);$(NetFrameworkCurrent) -- 2.7.4