44
55using System ;
66using System . Collections . Generic ;
7+ using System . Data ;
78using System . Diagnostics ;
8- using System . Runtime . InteropServices ;
9- using System . Security . Principal ;
109using System . Threading ;
11- using Microsoft . Win32 ;
1210using Xunit ;
1311
1412namespace Microsoft . Data . SqlClient . ManualTesting . Tests
@@ -369,7 +367,7 @@ public static void ConnectionOpenDisableRetry()
369367 {
370368 InitialCatalog = "DoesNotExist0982532435423" ,
371369 Pooling = false ,
372- ConnectTimeout = 15
370+ ConnectTimeout = 15
373371 } ;
374372 using SqlConnection sqlConnection = new ( connectionStringBuilder . ConnectionString ) ;
375373 Stopwatch timer = new ( ) ;
@@ -387,74 +385,24 @@ public static void ConnectionOpenDisableRetry()
387385 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();
388386 }
389387
390- private const string ConnectToPath = "SOFTWARE\\ Microsoft\\ MSSQLServer\\ Client\\ ConnectTo" ;
391- private static bool CanCreateAliases ( )
392- {
393- if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ||
394- ! DataTestUtility . IsTCPConnStringSetup ( ) )
395- {
396- return false ;
397- }
398-
399- using ( WindowsIdentity identity = WindowsIdentity . GetCurrent ( ) )
400- {
401- WindowsPrincipal principal = new ( identity ) ;
402- if ( ! principal . IsInRole ( WindowsBuiltInRole . Administrator ) )
403- {
404- return false ;
405- }
406- }
407-
408- using RegistryKey key = Registry . LocalMachine . OpenSubKey ( ConnectToPath , true ) ;
409- if ( key == null )
410- {
411- // Registry not writable
412- return false ;
413- }
414-
415- SqlConnectionStringBuilder b = new ( DataTestUtility . TCPConnectionString ) ;
416- if ( ! DataTestUtility . ParseDataSource ( b . DataSource , out string hostname , out int port , out string instanceName ) ||
417- ! string . IsNullOrEmpty ( instanceName ) )
418- {
419- return false ;
420- }
421-
422- return true ;
423- }
424-
425388 [ PlatformSpecific ( TestPlatforms . Windows ) ]
426- [ ConditionalFact ( nameof ( CanCreateAliases ) ) ]
389+ [ ConditionalFact ( typeof ( DataTestUtility ) , nameof ( DataTestUtility . AreConnStringsSetup ) , nameof ( DataTestUtility . IsSQLAliasSetup ) ) ]
427390 public static void ConnectionAliasTest ( )
428391 {
429- if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
430- {
431- throw new Exception ( "Alias test only valid on Windows" ) ;
432- }
433-
434- if ( ! CanCreateAliases ( ) )
435- {
436- throw new Exception ( "Unable to create aliases in this environment. Windows + Admin + non-instance data source required." ) ;
437- }
438-
439- SqlConnectionStringBuilder b = new ( DataTestUtility . TCPConnectionString ) ;
440- if ( ! DataTestUtility . ParseDataSource ( b . DataSource , out string hostname , out int port , out string instanceName ) ||
441- ! string . IsNullOrEmpty ( instanceName ) )
392+ SqlConnectionStringBuilder builder = new ( DataTestUtility . TCPConnectionString )
442393 {
443- // Only works with connection strings that parse successfully and don't include an instance name
444- throw new Exception ( "Unable to create aliases in this configuration. Parsable data source without instance required." ) ;
445- }
446-
447- b . DataSource = "TESTALIAS-" + Guid . NewGuid ( ) . ToString ( ) . Replace ( "-" , "" ) ;
448- using RegistryKey key = Registry . LocalMachine . OpenSubKey ( ConnectToPath , true ) ;
449- key . SetValue ( b . DataSource , "DBMSSOCN," + hostname + "," + ( port == - 1 ? 1433 : port ) ) ;
394+ DataSource = DataTestUtility . AliasName
395+ } ;
396+ using SqlConnection sqlConnection = new ( builder . ConnectionString ) ;
397+ Assert . Equal ( DataTestUtility . AliasName , builder . DataSource ) ;
450398 try
451399 {
452- using SqlConnection sqlConnection = new ( b . ConnectionString ) ;
453400 sqlConnection . Open ( ) ;
401+ Assert . Equal ( ConnectionState . Open , sqlConnection . State ) ;
454402 }
455- finally
403+ catch ( SqlException ex )
456404 {
457- key . DeleteValue ( b . DataSource ) ;
405+ Assert . Fail ( ex . Message ) ;
458406 }
459407 }
460408
0 commit comments