Skip to content

Commit 49a67a3

Browse files
committed
move to integrated auth tests
1 parent 8b44bc9 commit 49a67a3

File tree

2 files changed

+53
-51
lines changed

2 files changed

+53
-51
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/IntegratedAuthenticationTest/IntegratedAuthenticationTest.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
6+
using System.Buffers;
57
using Xunit;
68

79
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
@@ -56,12 +58,63 @@ public static void IntegratedAuthenticationTest_ServerSPN()
5658
TryOpenConnectionWithIntegratedAuthentication(builder.ConnectionString);
5759
}
5860

61+
[ConditionalFact(nameof(IsIntegratedSecurityEnvironmentSet), nameof(AreConnectionStringsSetup))]
62+
public static void CustomSspiContextGeneratorTest()
63+
{
64+
SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString);
65+
builder.IntegratedSecurity = true;
66+
Assert.True(DataTestUtility.ParseDataSource(builder.DataSource, out string hostname, out int port, out string instanceName));
67+
// Build the SPN for the server we are connecting to
68+
builder.ServerSPN = $"MSSQLSvc/{DataTestUtility.GetMachineFQDN(hostname)}";
69+
if (!string.IsNullOrWhiteSpace(instanceName))
70+
{
71+
builder.ServerSPN += ":" + instanceName;
72+
}
73+
74+
using SqlConnection conn = new(builder.ConnectionString)
75+
{
76+
SspiContextProvider = new TestSspiContextProvider(),
77+
};
78+
79+
try
80+
{
81+
conn.Open();
82+
83+
Assert.Fail("Expected to use custom SSPI context provider");
84+
}
85+
catch (SspiTestException sspi)
86+
{
87+
Assert.Equal(sspi.AuthParams.ServerName, builder.DataSource);
88+
Assert.Equal(sspi.AuthParams.DatabaseName, builder.InitialCatalog);
89+
Assert.Equal(sspi.AuthParams.UserId, builder.UserID);
90+
Assert.Equal(sspi.AuthParams.Password, builder.Password);
91+
}
92+
}
93+
5994
private static void TryOpenConnectionWithIntegratedAuthentication(string connectionString)
6095
{
6196
using (SqlConnection connection = new SqlConnection(connectionString))
6297
{
6398
connection.Open();
6499
}
65100
}
101+
102+
private sealed class TestSspiContextProvider : SspiContextProvider
103+
{
104+
protected override bool GenerateContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SspiAuthenticationParameters authParams)
105+
{
106+
throw new SspiTestException(authParams);
107+
}
108+
}
109+
110+
private sealed class SspiTestException : Exception
111+
{
112+
public SspiTestException(SspiAuthenticationParameters authParams)
113+
{
114+
AuthParams = authParams;
115+
}
116+
117+
public SspiAuthenticationParameters AuthParams { get; }
118+
}
66119
}
67120
}

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/KerberosTests/KerberosTest.cs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Buffers;
75
using System.Collections;
86
using System.Collections.Generic;
97
using Xunit;
@@ -26,55 +24,6 @@ public void IsKerBerosSetupTestAsync(string connectionStr)
2624
Assert.True(reader.Read(), "Expected to receive one row data");
2725
Assert.Equal("KERBEROS", reader.GetString(0));
2826
}
29-
30-
[PlatformSpecific(TestPlatforms.AnyUnix)]
31-
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsKerberosTest))]
32-
[ClassData(typeof(ConnectionStringsProvider))]
33-
public void CustomSspiContextGeneratorTest(string connectionStr)
34-
{
35-
KerberosTicketManagemnt.Init(DataTestUtility.KerberosDomainUser, DataTestUtility.KerberosDomainPassword);
36-
37-
using SqlConnection conn = new(connectionStr)
38-
{
39-
SspiContextProvider = new TestSspiContextProvider(),
40-
};
41-
42-
try
43-
{
44-
conn.Open();
45-
using SqlCommand command = new("SELECT auth_scheme from sys.dm_exec_connections where session_id = @@spid", conn);
46-
using SqlDataReader reader = command.ExecuteReader();
47-
48-
Assert.Fail("Expected to use custom SSPI context provider");
49-
}
50-
catch (SspiTestException sspi)
51-
{
52-
var builder = new SqlConnectionStringBuilder(connectionStr);
53-
54-
Assert.Equal(sspi.AuthParams.ServerName, builder.DataSource);
55-
Assert.Equal(sspi.AuthParams.DatabaseName, builder.InitialCatalog);
56-
Assert.Equal(sspi.AuthParams.UserId, builder.UserID);
57-
Assert.Equal(sspi.AuthParams.Password, builder.Password);
58-
}
59-
}
60-
61-
private sealed class TestSspiContextProvider : SspiContextProvider
62-
{
63-
protected override bool GenerateContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SspiAuthenticationParameters authParams)
64-
{
65-
throw new SspiTestException(authParams);
66-
}
67-
}
68-
69-
private sealed class SspiTestException : Exception
70-
{
71-
public SspiTestException(SspiAuthenticationParameters authParams)
72-
{
73-
AuthParams = authParams;
74-
}
75-
76-
public SspiAuthenticationParameters AuthParams { get; }
77-
}
7827
}
7928

8029
public class ConnectionStringsProvider : IEnumerable<object[]>

0 commit comments

Comments
 (0)