Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SqlConnectionEncryptOption string parser public #1771

Merged
merged 5 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -12,6 +12,31 @@ Implicit conversions have been added to maintain backwards compatibility with bo
]]></format>
</remarks>
</SqlConnectionEncryptOption>
<Parse>
<summary>
Converts the specified string representation of a logical value to its <see cref="T:Microsoft.Data.SqlClient.SqlConnectionEncryptOption"/> equivalent.
</summary>
<param name="value">A string containing the value to convert.</param>
<returns>
An object that is equivalent to <see cref="T:Microsoft.Data.SqlClient.SqlConnectionEncryptOption"/> contained in <paramref name="value"/>.
</returns>
<exception cref="T:System.ArgumentException">
Throws exception if provided <paramref name="value"/> is not convertible to <see cref="T:Microsoft.Data.SqlClient.SqlConnectionEncryptOption"/> type.
</exception>
</Parse>
<TryParse>
<summary>
Converts the specified string representation of a logical value to its <see cref="T:Microsoft.Data.SqlClient.SqlConnectionEncryptOption"/> equivalent and returns a value that indicates whether the conversion succeeded.
</summary>
<param name="value">A string containing the value to convert.</param>
<param name="result">
An object that is equivalent to <see cref="T:Microsoft.Data.SqlClient.SqlConnectionEncryptOption"/> contained in <paramref name="value"/>. <see langword="null" /> if conversion fails.
</param>
<returns>
<see langword="true" /> if the <paramref name="value"/> parameter was converted successfully; otherwise, <see langword="false" />.
</returns>
<remarks>This method does not throw an exception if conversion fails.</remarks>
</TryParse>
<Optional>
<summary>Specifies that TLS encryption is optional when connecting to the server. If the server requires encryption, encryption will be negotiated.</summary>
</Optional>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@ public enum SqlConnectionIPAddressPreference
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/SqlConnectionEncryptOption/*'/>
public sealed class SqlConnectionEncryptOption
{
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/Parse/*' />
public static SqlConnectionEncryptOption Parse(string value) => null;
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/TryParse/*' />
public static bool TryParse(string value, out SqlConnectionEncryptOption result) => throw null;
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/Optional/*' />
public static SqlConnectionEncryptOption Optional => throw null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,12 @@ public enum SqlConnectionIPAddressPreference
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/SqlConnectionEncryptOption/*'/>
public sealed class SqlConnectionEncryptOption
{
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/Parse/*' />
public static SqlConnectionEncryptOption Parse(string value) => null;

/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/TryParse/*' />
public static bool TryParse(string value, out SqlConnectionEncryptOption result) => throw null;

/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/Optional/*' />
public static SqlConnectionEncryptOption Optional => throw null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.Data.Common;

namespace Microsoft.Data.SqlClient
Expand Down Expand Up @@ -29,28 +30,46 @@ private SqlConnectionEncryptOption(string value)
_value = value;
}

internal static SqlConnectionEncryptOption Parse(string value)
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/Parse/*' />
public static SqlConnectionEncryptOption Parse(string value)
{
switch (value.ToLower())
if (TryParse(value, out var result))
{
return result;
}
else
{
throw ADP.InvalidConnectionOptionValue(SqlConnectionString.KEY.Encrypt);
}
}

/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/TryParse/*' />
public static bool TryParse(string value, out SqlConnectionEncryptOption result)
{
switch (value?.ToLower())
{
case TRUE_LOWER:
case YES_LOWER:
case MANDATORY_LOWER:
{
return Mandatory;
result = Mandatory;
return true;
}
case FALSE_LOWER:
case NO_LOWER:
case OPTIONAL_LOWER:
{
return Optional;
result = Optional;
return true;
}
case STRICT_LOWER:
{
return Strict;
result = Strict;
return true;
}
default:
throw ADP.InvalidConnectionOptionValue(SqlConnectionString.KEY.Encrypt);
result = null;
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,47 @@ public void ConnectionBuilderEncryptBackwardsCompatibility()
Assert.True(builder.Encrypt);
}

[Theory]
[InlineData("true", "True")]
[InlineData("mandatory", "True")]
[InlineData("yes", "True")]
[InlineData("false", "False")]
[InlineData("optional", "False")]
[InlineData("no", "False")]
[InlineData("strict", "Strict")]
public void EncryptParserValidValuesParsesSuccessfully(string value, string expectedValue)
=> Assert.Equal(expectedValue, SqlConnectionEncryptOption.Parse(value).ToString());

[Theory]
[InlineData("something")]
[InlineData("")]
[InlineData(null)]
[InlineData(" true ")]
public void EncryptParserInvalidValuesThrowsException(string value)
=> Assert.Throws<ArgumentException>(() => SqlConnectionEncryptOption.Parse(value));

[Theory]
[InlineData("true", "True")]
[InlineData("mandatory", "True")]
[InlineData("yes", "True")]
[InlineData("false", "False")]
[InlineData("optional", "False")]
[InlineData("no", "False")]
[InlineData("strict", "Strict")]
public void EncryptTryParseValidValuesReturnsTrue(string value, string expectedValue)
{
Assert.True(SqlConnectionEncryptOption.TryParse(value, out var result));
Assert.Equal(expectedValue, result.ToString());
}

[Theory]
[InlineData("something")]
[InlineData("")]
[InlineData(null)]
[InlineData(" true ")]
public void EncryptTryParseInvalidValuesReturnsFalse(string value)
=> Assert.False(SqlConnectionEncryptOption.TryParse(value, out _));

internal void ExecuteConnectionStringTests(string connectionString)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
Expand Down