Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ internal static class LocalAppContextSwitches
private const string EnableMultiSubnetFailoverByDefaultString =
"Switch.Microsoft.Data.SqlClient.EnableMultiSubnetFailoverByDefault";

/// <summary>
/// The name of the app context switch that controls whether
/// the user agent feature is enabled.
/// </summary>
private const string EnableUserAgentString =
"Switch.Microsoft.Data.SqlClient.EnableUserAgent";

#if NET
/// <summary>
/// The name of the app context switch that controls whether
Expand Down Expand Up @@ -177,11 +170,6 @@ private enum SwitchValue : byte
/// </summary>
private static SwitchValue s_enableMultiSubnetFailoverByDefault = SwitchValue.None;

/// <summary>
/// The cached value of the EnableUserAgent switch.
/// </summary>
private static SwitchValue s_enableUserAgent = SwitchValue.None;

#if NET
/// <summary>
/// The cached value of the GlobalizationInvariantMode switch.
Expand Down Expand Up @@ -319,18 +307,6 @@ static LocalAppContextSwitches()
defaultValue: false,
ref s_enableMultiSubnetFailoverByDefault);

/// <summary>
/// When set to true, the user agent feature is enabled and the driver will
/// send the user agent string to the server.
///
/// The default value of this switch is false.
/// </summary>
public static bool EnableUserAgent =>
AcquireAndReturn(
EnableUserAgentString,
defaultValue: false,
ref s_enableUserAgent);

#if NET
/// <summary>
/// .NET Core 2.0 and up supports Globalization Invariant mode, which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9663,7 +9663,7 @@ private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
checked
{
// NOTE: As part of TDS spec UserAgent feature extension should be the first feature extension in the list.
if (LocalAppContextSwitches.EnableUserAgent && ((requestedFeatures & TdsEnums.FeatureExtension.UserAgent) != 0))
if ((requestedFeatures & TdsEnums.FeatureExtension.UserAgent) != 0)
{
length += WriteUserAgentFeatureRequest(userAgent, write);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public sealed class LocalAppContextSwitchesHelper : IDisposable
private readonly bool? _disableTnirByDefaultOriginal;
#endif
private readonly bool? _enableMultiSubnetFailoverByDefaultOriginal;
private readonly bool? _enableUserAgentOriginal;
#if NET
private readonly bool? _globalizationInvariantModeOriginal;
#endif
Expand Down Expand Up @@ -93,8 +92,6 @@ public LocalAppContextSwitchesHelper()
#endif
_enableMultiSubnetFailoverByDefaultOriginal =
GetSwitchValue("s_enableMultiSubnetFailoverByDefault");
_enableUserAgentOriginal =
GetSwitchValue("s_enableUserAgent");
#if NET
_globalizationInvariantModeOriginal =
GetSwitchValue("s_globalizationInvariantMode");
Expand Down Expand Up @@ -149,9 +146,6 @@ public void Dispose()
SetSwitchValue(
"s_enableMultiSubnetFailoverByDefault",
_enableMultiSubnetFailoverByDefaultOriginal);
SetSwitchValue(
"s_enableUserAgent",
_enableUserAgentOriginal);
#if NET
SetSwitchValue(
"s_globalizationInvariantMode",
Expand Down Expand Up @@ -228,15 +222,6 @@ public bool? EnableMultiSubnetFailoverByDefault
set => SetSwitchValue("s_enableMultiSubnetFailoverByDefault", value);
}

/// <summary>
/// Get or set the EnableUserAgent switch value.
/// </summary>
public bool? EnableUserAgent
{
get => GetSwitchValue("s_enableUserAgent");
set => SetSwitchValue("s_enableUserAgent", value);
}

#if NET
/// <summary>
/// Get or set the GlobalizationInvariantMode switch value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public void TestDefaultAppContextSwitchValues()
Assert.False(LocalAppContextSwitches.UseConnectionPoolV2);
Assert.False(LocalAppContextSwitches.TruncateScaledDecimal);
Assert.False(LocalAppContextSwitches.IgnoreServerProvidedFailoverPartner);
Assert.False(LocalAppContextSwitches.EnableUserAgent);
Assert.False(LocalAppContextSwitches.EnableMultiSubnetFailoverByDefault);
#if NET
Assert.False(LocalAppContextSwitches.GlobalizationInvariantMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,10 +841,6 @@ public void TestConnWithVectorFeatExtVersionNegotiation(bool expectedConnectionR
[InlineData(false)]
public void TestConnWithUserAgentFeatureExtension(bool sendAck)
{
// Enable sending the UserAgent if desired.
using LocalAppContextSwitchesHelper switchesHelper = new();
switchesHelper.EnableUserAgent = true;

// Start the test server.
using TdsServer server = new();
server.Start();
Expand Down Expand Up @@ -905,55 +901,5 @@ public void TestConnWithUserAgentFeatureExtension(bool sendAck)

// TODO: Confirm the server sent an Ack by reading log message from SqlInternalConnectionTds
}

/// <summary>
/// Test to verify no UserAgent relevant information is sent when EnableUserAgentField switch is disabled.
/// </summary>
[Fact]
public void TestConnWithoutUserAgentFeatureExtension()
{
// Disable the client-side UserAgent field entirely
using LocalAppContextSwitchesHelper switchesHelper = new();
switchesHelper.EnableUserAgent = false;

using var server = new TdsServer();
server.Start();

// Do not advertise or force the UserAgent feature on the server
server.ServerSupportedUserAgentFeatureExtVersion = 0x00; // no support
server.EnableUserAgentFeatureExt = false; // no forced ACK

bool loginValidated = false;
bool userAgentFeatureSeen = false;

// Inspect the LOGIN7 packet captured by the test server
server.OnLogin7Validated = loginToken =>
{
var featureExtTokens = loginToken.FeatureExt
.OfType<TDSLogin7GenericOptionToken>()
.ToArray();

// Ensure there is no UserAgentSupport token at all
var uaToken = featureExtTokens.FirstOrDefault(t => t.FeatureID == TDSFeatureID.UserAgentSupport);
userAgentFeatureSeen = uaToken is not null;

loginValidated = true;
};

// Connect to the test TDS server with a basic connection string
var connStr = new SqlConnectionStringBuilder
{
DataSource = $"localhost,{server.EndPoint.Port}",
Encrypt = SqlConnectionEncryptOption.Optional,
}.ConnectionString;

using var connection = new SqlConnection(connStr);
connection.Open();

// Verify that the connection succeeded and no UserAgent data was sent
Assert.Equal(ConnectionState.Open, connection.State);
Assert.True(loginValidated, "Expected LOGIN7 to be validated by the test server");
Assert.False(userAgentFeatureSeen, "Did not expect a UserAgentSupport feature token in LOGIN7");
}
}
}
Loading