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 fde49b3980..ecabdb9f04 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionEncryptOption.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionEncryptOption.cs @@ -43,6 +43,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 0825760e45..2bc842566a 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs @@ -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(); @@ -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); @@ -402,6 +402,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")] @@ -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("")] @@ -467,5 +489,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); + } } }