Skip to content

Commit

Permalink
Fix | Dropping reference to Microsoft.Win32.Registry in netcore. (dot…
Browse files Browse the repository at this point in the history
  • Loading branch information
JRahnama authored and kant2002 committed Jun 29, 2023
1 parent 02c1472 commit 91d3a54
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 68 deletions.
Expand Up @@ -939,9 +939,6 @@
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Managed.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true' and '$(IsUAPAssembly)' != 'true'">
<Reference Include="Microsoft.Win32.Registry" />
</ItemGroup>
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND '$(IsUAPAssembly)' == 'true'">
<Reference Include="System.Collections.NonGeneric" />
<Reference Include="System.Memory" />
Expand Down
Expand Up @@ -60,6 +60,7 @@ public static class DataTestUtility
public static readonly bool IsDNSCachingSupportedCR = false; // this is for the control ring
public static readonly bool IsDNSCachingSupportedTR = false; // this is for the tenant ring
public static readonly string UserManagedIdentityClientId = null;
public static readonly string AliasName = null;


public static readonly string EnclaveAzureDatabaseConnString = null;
Expand Down Expand Up @@ -117,6 +118,7 @@ static DataTestUtility()
KerberosDomainUser = c.KerberosDomainUser;
ManagedIdentitySupported = c.ManagedIdentitySupported;
IsManagedInstance = c.IsManagedInstance;
AliasName = c.AliasName;

System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;

Expand Down Expand Up @@ -300,6 +302,10 @@ public static bool AreConnStringsSetup()
return !string.IsNullOrEmpty(NPConnectionString) && !string.IsNullOrEmpty(TCPConnectionString);
}

public static bool IsSQLAliasSetup()
{
return !string.IsNullOrEmpty(AliasName);
}
public static bool IsTCPConnStringSetup()
{
return !string.IsNullOrEmpty(TCPConnectionString);
Expand Down
Expand Up @@ -4,11 +4,9 @@

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;
using Microsoft.Win32;
using Xunit;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
Expand Down Expand Up @@ -369,7 +367,7 @@ public static void ConnectionOpenDisableRetry()
{
InitialCatalog = "DoesNotExist0982532435423",
Pooling = false,
ConnectTimeout=15
ConnectTimeout = 15
};
using SqlConnection sqlConnection = new(connectionStringBuilder.ConnectionString);
Stopwatch timer = new();
Expand All @@ -387,74 +385,24 @@ public static void ConnectionOpenDisableRetry()
Assert.True(duration.Seconds > 5, $"Connection Open() with retries took less time than expected. Expect > 5 sec with transient fault handling. Took {duration.Seconds} sec."); // sqlConnection.Open();
}

private const string ConnectToPath = "SOFTWARE\\Microsoft\\MSSQLServer\\Client\\ConnectTo";
private static bool CanCreateAliases()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
!DataTestUtility.IsTCPConnStringSetup())
{
return false;
}

using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new(identity);
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
return false;
}
}

using RegistryKey key = Registry.LocalMachine.OpenSubKey(ConnectToPath, true);
if (key == null)
{
// Registry not writable
return false;
}

SqlConnectionStringBuilder b = new(DataTestUtility.TCPConnectionString);
if (!DataTestUtility.ParseDataSource(b.DataSource, out string hostname, out int port, out string instanceName) ||
!string.IsNullOrEmpty(instanceName))
{
return false;
}

return true;
}

[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(nameof(CanCreateAliases))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSQLAliasSetup))]
public static void ConnectionAliasTest()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw new Exception("Alias test only valid on Windows");
}

if (!CanCreateAliases())
{
throw new Exception("Unable to create aliases in this environment. Windows + Admin + non-instance data source required.");
}

SqlConnectionStringBuilder b = new(DataTestUtility.TCPConnectionString);
if (!DataTestUtility.ParseDataSource(b.DataSource, out string hostname, out int port, out string instanceName) ||
!string.IsNullOrEmpty(instanceName))
SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString)
{
// Only works with connection strings that parse successfully and don't include an instance name
throw new Exception("Unable to create aliases in this configuration. Parsable data source without instance required.");
}

b.DataSource = "TESTALIAS-" + Guid.NewGuid().ToString().Replace("-", "");
using RegistryKey key = Registry.LocalMachine.OpenSubKey(ConnectToPath, true);
key.SetValue(b.DataSource, "DBMSSOCN," + hostname + "," + (port == -1 ? 1433 : port));
DataSource = DataTestUtility.AliasName
};
using SqlConnection sqlConnection = new(builder.ConnectionString);
Assert.Equal(DataTestUtility.AliasName, builder.DataSource);
try
{
using SqlConnection sqlConnection = new(b.ConnectionString);
sqlConnection.Open();
Assert.Equal(ConnectionState.Open, sqlConnection.State);
}
finally
catch (SqlException ex)
{
key.DeleteValue(b.DataSource);
Assert.Fail(ex.Message);
}
}

Expand Down
Expand Up @@ -44,7 +44,7 @@ public class Config
public string KerberosDomainPassword = null;
public string KerberosDomainUser = null;
public bool IsManagedInstance = false;

public string AliasName = null;
public static Config Load(string configPath = @"config.json")
{
try
Expand Down
Expand Up @@ -30,5 +30,6 @@
"EnclaveAzureDatabaseConnString": "",
"ManagedIdentitySupported": true,
"UserManagedIdentityClientId": "",
"PowerShellPath": ""
"PowerShellPath": "",
"AliasName": ""
}

0 comments on commit 91d3a54

Please sign in to comment.