From f85aa557ce664b2adece39363e1b50c7273ca82f Mon Sep 17 00:00:00 2001 From: Davoud Eshtehari Date: Tue, 17 Oct 2023 03:14:19 +0000 Subject: [PATCH] Merged PR 4029: [5.1.2] Fix | Adding non-string support to SqlConnectionStringBuilder property indexer Ports [#2018](https://github.com/dotnet/SqlClient/pull/2018) --- .../Data/Common/DbConnectionStringCommon.cs | 8 +++++ .../SqlClient/SqlConnectionEncryptOption.cs | 5 +++ .../SqlConnectionStringBuilderTest.cs | 32 +++++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/DbConnectionStringCommon.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/DbConnectionStringCommon.cs index f8a31231c0..7e21441852 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/DbConnectionStringCommon.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/DbConnectionStringCommon.cs @@ -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); } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionEncryptOption.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionEncryptOption.cs index cb5b467298..997833437f 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionEncryptOption.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionEncryptOption.cs @@ -45,6 +45,11 @@ public static SqlConnectionEncryptOption Parse(string value) } } + internal static SqlConnectionEncryptOption Parse(bool value) + { + return value ? Mandatory : Optional; + } + /// public static bool TryParse(string value, out SqlConnectionEncryptOption result) { diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs index 7e65cec996..557833e80c 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs @@ -260,7 +260,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(); @@ -387,7 +387,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); @@ -409,6 +409,18 @@ public void ConnectionBuilderEncryptBackwardsCompatibility() Assert.True(builder.Encrypt); } + [Fact] + public void EncryptParserValidValuesPropertyIndexerForEncryptionOption() + { + SqlConnectionStringBuilder builder = new(); + builder["Encrypt"] = SqlConnectionEncryptOption.Strict; + 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")] @@ -420,6 +432,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("")] @@ -627,5 +649,11 @@ internal void ExecuteConnectionStringTests(string connectionString) Assert.NotNull(connection); } } + + internal static void CheckEncryptType(SqlConnectionStringBuilder builder, SqlConnectionEncryptOption expectedValue) + { + Assert.IsType(builder.Encrypt); + Assert.Equal(expectedValue, builder.Encrypt); + } } }