From 8fd0072e133fe5947278c7a86f5048eb0f540d5c Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Sun, 5 May 2024 16:26:19 +0200 Subject: [PATCH 1/5] Remove UWP (uap) references and special cased code Some boy scouting on DataTestUtility --- .../src/Microsoft.Data.SqlClient.csproj | 41 +++---- .../Data/SqlClient/LocalDBAPI.uap.cs | 14 --- .../Data/SqlClient/SNI/LocalDB.uap.cs | 14 --- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 4 +- .../src/Microsoft/Data/SqlClient/TdsEnums.cs | 41 ------- .../ManualTests/DataCommon/DataTestUtility.cs | 102 ------------------ .../ExceptionTest/ConnectionExceptionTest.cs | 19 ---- 7 files changed, 13 insertions(+), 222 deletions(-) delete mode 100644 src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.uap.cs delete mode 100644 src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.uap.cs diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 0a0f573333..57497fd2b0 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -726,6 +726,9 @@ Common\Interop\Windows\Kernel32\Interop.IoControlTransferType.cs + + Common\Interop\Windows\kernel32\Interop.LoadLibraryEx.cs + Common\Interop\Windows\NtDll\Interop.FILE_FULL_EA_INFORMATION.cs @@ -826,16 +829,25 @@ Microsoft\Data\Sql\SqlDataSourceEnumerator.Windows.cs + + Microsoft\Data\SqlClient\SSPI\NativeSSPIContextProvider.cs + Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs + + + + + + @@ -894,31 +906,6 @@ - - - - - - - - - - - - - - Common\Interop\Windows\kernel32\Interop.LoadLibraryEx.cs - - - Microsoft\Data\SqlClient\SSPI\NativeSSPIContextProvider.cs - - - - - - - - @@ -947,10 +934,6 @@ - - - - diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.uap.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.uap.cs deleted file mode 100644 index 9ed10f0fcc..0000000000 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.uap.cs +++ /dev/null @@ -1,14 +0,0 @@ -// 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; - -namespace System.Data -{ - internal static partial class LocalDBAPI - { - private static IntPtr LoadProcAddress() => - throw new PlatformNotSupportedException(Strings.LocalDBNotSupported); // No Registry support on UAP - } -} diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.uap.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.uap.cs deleted file mode 100644 index e764375802..0000000000 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.uap.cs +++ /dev/null @@ -1,14 +0,0 @@ -// 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. - -namespace Microsoft.Data.SqlClient.SNI -{ - internal class LocalDB - { - internal static string GetLocalDBConnectionString(string localDbInstance) - { - throw new PlatformNotSupportedException(Strings.LocalDBNotSupported); // No Registry support on UAP - } - } -} diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index 0d3c0a01b5..f415f342b3 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -1509,9 +1509,7 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj) SqlClientEventSource.Log.TryAdvancedTraceEvent(" Empty error message received from SNI. Error Message = {0}", details.errorMessage); } - string sniContextEnumName = TdsEnums.GetSniContextEnumName(stateObj.SniContext); - - string sqlContextInfo = StringsHelper.GetResourceString(sniContextEnumName); + string sqlContextInfo = StringsHelper.GetResourceString(nameof(stateObj.SniContext)); string providerRid = string.Format("SNI_PN{0}", details.provider); string providerName = StringsHelper.GetResourceString(providerRid); Debug.Assert(!string.IsNullOrEmpty(providerName), $"invalid providerResourceId '{providerRid}'"); diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs index c483d8a0f8..5029c4c151 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs @@ -998,47 +998,6 @@ internal enum FedAuthInfoId : byte internal const byte DATA_CLASSIFICATION_VERSION_WITHOUT_RANK_SUPPORT = 0x01; internal const byte DATA_CLASSIFICATION_VERSION_MAX_SUPPORTED = 0x02; - // Needed for UapAot, since we cannot use Enum.GetName() on SniContext. - // Enum.GetName() uses reflection, which is blocked on UapAot for internal types - // like SniContext. - internal static string GetSniContextEnumName(SniContext sniContext) - { - switch (sniContext) - { - case SniContext.Undefined: - return "Undefined"; - case SniContext.Snix_Connect: - return "Snix_Connect"; - case SniContext.Snix_PreLoginBeforeSuccessfulWrite: - return "Snix_PreLoginBeforeSuccessfulWrite"; - case SniContext.Snix_PreLogin: - return "Snix_PreLogin"; - case SniContext.Snix_LoginSspi: - return "Snix_LoginSspi"; - case SniContext.Snix_ProcessSspi: - return "Snix_ProcessSspi"; - case SniContext.Snix_Login: - return "Snix_Login"; - case SniContext.Snix_EnableMars: - return "Snix_EnableMars"; - case SniContext.Snix_AutoEnlist: - return "Snix_AutoEnlist"; - case SniContext.Snix_GetMarsSession: - return "Snix_GetMarsSession"; - case SniContext.Snix_Execute: - return "Snix_Execute"; - case SniContext.Snix_Read: - return "Snix_Read"; - case SniContext.Snix_Close: - return "Snix_Close"; - case SniContext.Snix_SendRows: - return "Snix_SendRows"; - default: - Debug.Fail($"Received unknown SniContext enum. Value: {sniContext}"); - return null; - } - } - // TCE Related constants internal const byte MAX_SUPPORTED_TCE_VERSION = 0x03; // max version internal const byte MIN_TCE_VERSION_WITH_ENCLAVE_SUPPORT = 0x02; // min version with enclave support diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs index 06209e1ad2..7628d8028f 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs @@ -21,7 +21,6 @@ using System.Net.NetworkInformation; using System.Text; using System.Security.Principal; -using System.Runtime.InteropServices; using Azure.Identity; using Azure.Core; @@ -78,10 +77,6 @@ public static class DataTestUtility public const string AKVEventSourceName = "Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider.EventSource"; private const string ManagedNetworkingAppContextSwitch = "Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows"; - // uap constant - const long APPMODEL_ERROR_NO_PACKAGE = 15700L; - public static readonly bool IsRunningAsUWPApp = RunningAsUWPApp(); - private static Dictionary AvailableDatabases; private static BaseEventListener TraceListener; @@ -330,8 +325,6 @@ public static bool GetSQLServerStatusOnTDS8(string connectionString) return isTDS8Supported; } - public static bool IsNotX86Architecture => RuntimeInformation.ProcessArchitecture != Architecture.X86; - public static bool IsDatabasePresent(string name) { AvailableDatabases = AvailableDatabases ?? new Dictionary(); @@ -683,10 +676,6 @@ public static string GetUserIdentityAccessToken() public static bool IsAccessTokenSetup() => !string.IsNullOrEmpty(GetAccessToken()); - public static bool IsSystemIdentityTokenSetup() => !string.IsNullOrEmpty(GetSystemIdentityAccessToken()); - - public static bool IsUserIdentityTokenSetup() => !string.IsNullOrEmpty(GetUserIdentityAccessToken()); - public static bool IsFileStreamSetup() => !string.IsNullOrEmpty(FileStreamDirectory) && IsNotAzureServer() && IsNotAzureSynapse(); private static bool CheckException(Exception ex, string exceptionMessage, bool innerExceptionMustBeNull) where TException : Exception @@ -787,58 +776,6 @@ public static TException ExpectFailure(Action actionThatFails, strin } } - public static TException ExpectFailure(Action actionThatFails, string exceptionMessage = null, string innerExceptionMessage = null, bool innerInnerExceptionMustBeNull = false) where TException : Exception where TInnerException : Exception - { - try - { - actionThatFails(); - Assert.Fail("ERROR: Did not get expected exception"); - return null; - } - catch (Exception ex) - { - if ((CheckException(ex, exceptionMessage, false)) && (CheckException(ex.InnerException, innerExceptionMessage, innerInnerExceptionMustBeNull))) - { - return (ex as TException); - } - else - { - throw; - } - } - } - - public static TException ExpectFailure(Action actionThatFails, string exceptionMessage = null, string innerExceptionMessage = null, string innerInnerExceptionMessage = null, bool innerInnerInnerExceptionMustBeNull = false) where TException : Exception where TInnerException : Exception where TInnerInnerException : Exception - { - try - { - actionThatFails(); - Assert.Fail("ERROR: Did not get expected exception"); - return null; - } - catch (Exception ex) - { - if ((CheckException(ex, exceptionMessage, false)) && (CheckException(ex.InnerException, innerExceptionMessage, false)) && (CheckException(ex.InnerException.InnerException, innerInnerExceptionMessage, innerInnerInnerExceptionMustBeNull))) - { - return (ex as TException); - } - else - { - throw; - } - } - } - - public static void ExpectAsyncFailure(Func actionThatFails, string exceptionMessage = null, bool innerExceptionMustBeNull = false) where TException : Exception - { - ExpectFailure(() => actionThatFails().Wait(), null, exceptionMessage, innerExceptionMustBeNull); - } - - public static void ExpectAsyncFailure(Func actionThatFails, string exceptionMessage = null, string innerExceptionMessage = null, bool innerInnerExceptionMustBeNull = false) where TException : Exception where TInnerException : Exception - { - ExpectFailure(() => actionThatFails().Wait(), null, exceptionMessage, innerExceptionMessage, innerInnerExceptionMustBeNull); - } - public static string GenerateObjectName() { return string.Format("TEST_{0}{1}{2}", Environment.GetEnvironmentVariable("ComputerName"), Environment.TickCount, Guid.NewGuid()).Replace('-', '_'); @@ -1100,44 +1037,5 @@ public static string GetMachineFQDN(string hostname) } return fqdn.ToString(); } - - public static bool IsNotLocalhost() - { - // get the tcp connection string - SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString); - - string hostname = ""; - - // parse the datasource - ParseDataSource(builder.DataSource, out hostname, out _, out _); - - // hostname must not be localhost, ., 127.0.0.1 nor ::1 - return !(new string[] { "localhost", ".", "127.0.0.1", "::1" }).Contains(hostname.ToLowerInvariant()); - - } - - private static bool RunningAsUWPApp() - { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return false; - } - else - { - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName); - - { - int length = 0; - StringBuilder sb = new(0); - _ = GetCurrentPackageFullName(ref length, sb); - - sb = new StringBuilder(length); - int result = GetCurrentPackageFullName(ref length, sb); - - return result != APPMODEL_ERROR_NO_PACKAGE; - } - } - } } } diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ExceptionTest/ConnectionExceptionTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ExceptionTest/ConnectionExceptionTest.cs index 9b8e7476e4..b1ba557562 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ExceptionTest/ConnectionExceptionTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ExceptionTest/ConnectionExceptionTest.cs @@ -176,25 +176,6 @@ public void NamedPipeInvalidConnStringTest() OpenBadConnection(builder.ConnectionString, invalidConnStringError); } - [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsRunningAsUWPApp))] - public static void LocalDBNotSupportedOnUapTest() - { - SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(@$"server=(localdb)\{DataTestUtility.LocalDbAppName}") - { - IntegratedSecurity = true, - ConnectTimeout = 2 - }; - - Assert.Throws(() => - { - using (SqlConnection conn = new SqlConnection(builder.ConnectionString)) - { - conn.Open(); - } - }); - } - - private void GenerateConnectionException(string connectionString) { using (SqlConnection sqlConnection = new SqlConnection(connectionString)) From 599fccb3218446097aefc1016cba0cb18429e19d Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Mon, 6 May 2024 07:28:11 +0200 Subject: [PATCH 2/5] Use ToString() --- .../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index f415f342b3..84b3978bdf 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -1509,7 +1509,7 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj) SqlClientEventSource.Log.TryAdvancedTraceEvent(" Empty error message received from SNI. Error Message = {0}", details.errorMessage); } - string sqlContextInfo = StringsHelper.GetResourceString(nameof(stateObj.SniContext)); + string sqlContextInfo = StringsHelper.GetResourceString(stateObj.SniContext.ToString()); string providerRid = string.Format("SNI_PN{0}", details.provider); string providerName = StringsHelper.GetResourceString(providerRid); Debug.Assert(!string.IsNullOrEmpty(providerName), $"invalid providerResourceId '{providerRid}'"); From 1bc08b5731ebbdfe33eb45e98295ef46fe77772e Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Mon, 6 May 2024 07:52:21 +0200 Subject: [PATCH 3/5] Fix tests --- .../tests/ManualTests/DataCommon/DataTestUtility.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs index 7628d8028f..e5cb4c33f4 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs @@ -23,6 +23,7 @@ using System.Security.Principal; using Azure.Identity; using Azure.Core; +using System.Runtime.InteropServices; namespace Microsoft.Data.SqlClient.ManualTesting.Tests { @@ -133,6 +134,8 @@ public static bool IsTDS8Supported } } + public static bool IsNotX86Architecture => RuntimeInformation.ProcessArchitecture != Architecture.X86; + static DataTestUtility() { Config c = Config.Load(); From 02d1f708b3b3c6d230aae3b0c98728b3a2bb240d Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Mon, 6 May 2024 08:35:03 +0200 Subject: [PATCH 4/5] Improve revert and sort usings --- .../ManualTests/DataCommon/DataTestUtility.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs index e5cb4c33f4..a45e62c21a 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs @@ -11,19 +11,19 @@ using System.IO; using System.Linq; using System.Net; +using System.Net.NetworkInformation; using System.Net.Sockets; +using System.Runtime.InteropServices; using System.Security; +using System.Security.Principal; +using System.Text; using System.Threading; using System.Threading.Tasks; +using Azure.Core; +using Azure.Identity; using Microsoft.Data.SqlClient.TestUtilities; using Microsoft.Identity.Client; using Xunit; -using System.Net.NetworkInformation; -using System.Text; -using System.Security.Principal; -using Azure.Identity; -using Azure.Core; -using System.Runtime.InteropServices; namespace Microsoft.Data.SqlClient.ManualTesting.Tests { @@ -134,8 +134,6 @@ public static bool IsTDS8Supported } } - public static bool IsNotX86Architecture => RuntimeInformation.ProcessArchitecture != Architecture.X86; - static DataTestUtility() { Config c = Config.Load(); @@ -328,6 +326,8 @@ public static bool GetSQLServerStatusOnTDS8(string connectionString) return isTDS8Supported; } + public static bool IsNotX86Architecture => RuntimeInformation.ProcessArchitecture != Architecture.X86; + public static bool IsDatabasePresent(string name) { AvailableDatabases = AvailableDatabases ?? new Dictionary(); From 554a88f155cb90a97646f9786c8ce28820623eab Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Tue, 7 May 2024 08:56:04 +0200 Subject: [PATCH 5/5] Fix usings --- .../src/Microsoft/Data/SqlClient/TdsEnums.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs index 5029c4c151..8c7119a695 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs @@ -3,11 +3,7 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Data; -using System.Diagnostics; -using Microsoft.Data.Common; namespace Microsoft.Data.SqlClient {