Skip to content

Commit 7310277

Browse files
committed
CryptKey connection string handling fix (DNET-794).
1 parent 4f0a53f commit 7310277

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

Provider/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnectionStringBuilder.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -353,23 +353,22 @@ private bool GetBoolean(string keyword, bool defaultValue)
353353

354354
private byte[] GetBytes(string keyword, byte[] defaultValue)
355355
{
356-
if (!TryGetValue(GetKey(keyword), out var value))
357-
return defaultValue;
358-
359-
switch (value)
360-
{
361-
case byte[] bytes:
362-
return bytes;
363-
case string s:
364-
return Convert.FromBase64String(s);
365-
default:
366-
return defaultValue;
367-
}
356+
return TryGetValue(GetKey(keyword), out var value)
357+
? Convert.FromBase64String(value as string)
358+
: defaultValue;
368359
}
369360

370361
private void SetValue<T>(string keyword, T value)
371362
{
372-
this[GetKey(keyword)] = value;
363+
var index = GetKey(keyword);
364+
if (typeof(T) == typeof(byte[]))
365+
{
366+
this[index] = Convert.ToBase64String(value as byte[]);
367+
}
368+
else
369+
{
370+
this[index] = value;
371+
}
373372
}
374373

375374
private string GetKey(string keyword)

Provider/src/FirebirdSql.Data.UnitTests/FbConnectionStringBuilderTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Data;
2222
using System.Globalization;
2323
using System.Reflection;
24+
using System.Text;
2425
using System.Threading;
2526
using FirebirdSql.Data.FirebirdClient;
2627
using NUnit.Framework;
@@ -43,5 +44,20 @@ public void NoValueProvidedReturnsDefault()
4344
var b = new FbConnectionStringBuilder();
4445
Assert.AreEqual(b.MaxPoolSize, FbConnectionString.DefaultValueMaxPoolSize);
4546
}
47+
48+
[Test]
49+
public void CryptKeyValueSetter()
50+
{
51+
var b = new FbConnectionStringBuilder();
52+
b.CryptKey = Encoding.ASCII.GetBytes("test");
53+
Assert.AreEqual("crypt key=\"dGVzdA==\"", b.ToString());
54+
}
55+
56+
[Test]
57+
public void CryptKeyValueGetter()
58+
{
59+
var b = new FbConnectionStringBuilder("CryptKey=dGVzdA==");
60+
Assert.AreEqual("test", Encoding.ASCII.GetString(b.CryptKey));
61+
}
4662
}
4763
}

0 commit comments

Comments
 (0)