Skip to content

Commit

Permalink
Make SqlConnectionEncryptOption string parser public (#1771)
Browse files Browse the repository at this point in the history
* Make SqlConnectionEncryptOption string parser public

* Address feedback

* Fix non-nullables

* Fix docs

* Apply suggestions from code review

Co-authored-by: DavoudEshtehari <[email protected]>

Co-authored-by: DavoudEshtehari <[email protected]>
  • Loading branch information
cheenamalhotra and DavoudEshtehari authored Sep 27, 2022
1 parent 4dafe91 commit 50ec923
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
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) => throw 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) => throw 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 SqlConnectionEncryptOption 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

0 comments on commit 50ec923

Please sign in to comment.