Skip to content

Commit 7d6f87f

Browse files
committed
Add relevant test conditions
1 parent 4285e59 commit 7d6f87f

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public static class DataTestUtility
9898
private static bool? s_isVectorSupported;
9999
private static bool? s_isVectorFloat16Supported;
100100

101+
// Login permissions
102+
private static bool? s_isSysAdmin;
103+
private static bool? s_isSecurityAdmin;
104+
101105
// Azure Synapse EngineEditionId == 6
102106
// More could be read at https://learn.microsoft.com/en-us/sql/t-sql/functions/serverproperty-transact-sql?view=sql-server-ver16#propertyname
103107
public static bool IsAzureSynapse
@@ -231,6 +235,20 @@ private static bool CheckVectorFloat16Supported()
231235
}
232236
}
233237

238+
public static bool IsSysAdmin =>
239+
s_isSysAdmin ??= IsTCPConnStringSetup() &&
240+
IsServerRoleMember("sysadmin");
241+
242+
public static bool IsSecurityAdmin =>
243+
s_isSecurityAdmin ??= IsTCPConnStringSetup() &&
244+
IsServerRoleMember("securityadmin");
245+
246+
public static bool CanCreateLogins =>
247+
IsSysAdmin || IsSecurityAdmin;
248+
249+
public static bool CanUseSqlAuthentication =>
250+
IsSysAdmin && GetAuthenticationMode() == 2;
251+
234252
static DataTestUtility()
235253
{
236254
Config c = Config.Load();
@@ -531,6 +549,30 @@ public static bool IsTypePresent(string typeName)
531549
return (int)command.ExecuteScalar() > 0;
532550
}
533551

552+
public static bool IsServerRoleMember(string roleName)
553+
{
554+
using SqlConnection connection = new(TCPConnectionString);
555+
using SqlCommand command = new("SELECT IS_SRVROLEMEMBER(@role)", connection);
556+
557+
connection.Open();
558+
command.Parameters.AddWithValue("@role", roleName);
559+
560+
// IS_SRVROLEMEMBER returns 1 if the caller is a member of the specified server role, 0 if not, and DBNull.Value if the role is not valid.
561+
return command.ExecuteScalar() is int result && result == 1;
562+
}
563+
564+
public static int GetAuthenticationMode()
565+
{
566+
using SqlConnection connection = new(TCPConnectionString);
567+
568+
connection.Open();
569+
using SqlCommand command = new("EXEC xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\\Microsoft\\MSSQLServer\\MSSQLServer', N'LoginMode'", connection);
570+
using SqlDataReader reader = command.ExecuteReader();
571+
572+
reader.Read();
573+
return reader.GetInt32(1);
574+
}
575+
534576
public static bool IsAdmin
535577
{
536578
get

0 commit comments

Comments
 (0)