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

Fix | Adding non-string support to SqlConnectionStringBuilder property indexer #2018

Merged
merged 5 commits into from
Apr 28, 2023
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 @@ -816,10 +816,18 @@ internal static SqlConnectionEncryptOption ConvertToSqlConnectionEncryptOption(s
{
return DbConnectionStringDefaults.Encrypt;
}
else if(value is SqlConnectionEncryptOption eValue)
{
return eValue;
}
else if (value is string sValue)
{
return SqlConnectionEncryptOption.Parse(sValue);
}
else if(value is bool bValue)
{
return SqlConnectionEncryptOption.Parse(bValue);
}

throw ADP.InvalidConnectionOptionValue(keyword);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public static SqlConnectionEncryptOption Parse(string value)
}
}

internal static SqlConnectionEncryptOption Parse(bool value)
{
return value ? Mandatory : Optional;
}

/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/TryParse/*' />
public static bool TryParse(string value, out SqlConnectionEncryptOption result)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void SetInvalidPacketSize_Throws(int invalid)
}

[Theory]
[InlineData("AttachDBFilename","somefile.db")]
[InlineData("AttachDBFilename", "somefile.db")]
public void SetKeyword(string keyword, string value)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
Expand Down Expand Up @@ -380,7 +380,7 @@ public void ConnectionBuilderEncryptBackwardsCompatibility()
builder.Encrypt = false;
Assert.Equal("Encrypt=False", builder.ConnectionString);
Assert.False(builder.Encrypt);

builder.Encrypt = true;
Assert.Equal("Encrypt=True", builder.ConnectionString);
Assert.True(builder.Encrypt);
Expand All @@ -402,6 +402,18 @@ public void ConnectionBuilderEncryptBackwardsCompatibility()
Assert.True(builder.Encrypt);
}

[Fact]
public void EncryptParserValidValuesPropertyIndexerForEncryptionOption()
{
SqlConnectionStringBuilder builder = new();
builder["Encrypt"] = SqlConnectionEncryptOption.Strict;
David-Engel marked this conversation as resolved.
Show resolved Hide resolved
CheckEncryptType(builder, SqlConnectionEncryptOption.Strict);
builder["Encrypt"] = SqlConnectionEncryptOption.Optional;
CheckEncryptType(builder, SqlConnectionEncryptOption.Optional);
builder["Encrypt"] = SqlConnectionEncryptOption.Mandatory;
CheckEncryptType(builder, SqlConnectionEncryptOption.Mandatory);
}

[Theory]
[InlineData("true", "True")]
[InlineData("mandatory", "True")]
Expand All @@ -413,6 +425,16 @@ public void ConnectionBuilderEncryptBackwardsCompatibility()
public void EncryptParserValidValuesParsesSuccessfully(string value, string expectedValue)
=> Assert.Equal(expectedValue, SqlConnectionEncryptOption.Parse(value).ToString());

[Theory]
[InlineData(true)]
[InlineData(false)]
public void EncryptParserValidValuesPropertyIndexerForBoolean(bool value)
{
SqlConnectionStringBuilder builder = new();
builder["Encrypt"] = value;
CheckEncryptType(builder, value ? SqlConnectionEncryptOption.Mandatory : SqlConnectionEncryptOption.Optional);
}

[Theory]
[InlineData("something")]
[InlineData("")]
Expand Down Expand Up @@ -467,5 +489,11 @@ internal void ExecuteConnectionStringTests(string connectionString)
Assert.NotNull(connection);
}
}

internal static void CheckEncryptType(SqlConnectionStringBuilder builder, SqlConnectionEncryptOption expectedValue)
{
Assert.IsType<SqlConnectionEncryptOption>(builder.Encrypt);
Assert.Equal(expectedValue, builder.Encrypt);
}
}
}