4
4
5
5
using System ;
6
6
using System . Collections . Generic ;
7
+ using System . Data ;
7
8
using System . Diagnostics ;
8
- using System . Runtime . InteropServices ;
9
- using System . Security . Principal ;
10
9
using System . Threading ;
11
- using Microsoft . Win32 ;
12
10
using Xunit ;
13
11
14
12
namespace Microsoft . Data . SqlClient . ManualTesting . Tests
@@ -369,7 +367,7 @@ public static void ConnectionOpenDisableRetry()
369
367
{
370
368
InitialCatalog = "DoesNotExist0982532435423" ,
371
369
Pooling = false ,
372
- ConnectTimeout = 15
370
+ ConnectTimeout = 15
373
371
} ;
374
372
using SqlConnection sqlConnection = new ( connectionStringBuilder . ConnectionString ) ;
375
373
Stopwatch timer = new ( ) ;
@@ -387,74 +385,24 @@ public static void ConnectionOpenDisableRetry()
387
385
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();
388
386
}
389
387
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
-
425
388
[ PlatformSpecific ( TestPlatforms . Windows ) ]
426
- [ ConditionalFact ( nameof ( CanCreateAliases ) ) ]
389
+ [ ConditionalFact ( typeof ( DataTestUtility ) , nameof ( DataTestUtility . AreConnStringsSetup ) , nameof ( DataTestUtility . IsSQLAliasSetup ) ) ]
427
390
public static void ConnectionAliasTest ( )
428
391
{
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 )
442
393
{
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 ) ;
450
398
try
451
399
{
452
- using SqlConnection sqlConnection = new ( b . ConnectionString ) ;
453
400
sqlConnection . Open ( ) ;
401
+ Assert . Equal ( ConnectionState . Open , sqlConnection . State ) ;
454
402
}
455
- finally
403
+ catch ( SqlException ex )
456
404
{
457
- key . DeleteValue ( b . DataSource ) ;
405
+ Assert . Fail ( ex . Message ) ;
458
406
}
459
407
}
460
408
0 commit comments