Skip to content

Commit a406d3d

Browse files
committed
debugging
1 parent 1f7063a commit a406d3d

File tree

68 files changed

+17381
-17360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+17381
-17360
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,24 @@ internal bool TryReadChars(char[] chars, int charsOffset, int charsCount, out in
440440
{
441441
charsCopied = 0;
442442
int count = 0;
443+
StringBuilder build = new StringBuilder();
444+
build.AppendLine($"TryReadChars({chars.Length}, {charsOffset}, {charsCount}, out int charsCopied)");
443445
while (charsCopied < charsCount)
444446
{
445447
// check if the current buffer contains some bytes we need to copy and copy them
446448
// in a block
449+
int bytesAvailable = _inBytesRead - _inBytesUsed;
447450
int bytesToRead = Math.Min(
448451
(charsCount - charsCopied) * 2,
449-
unchecked((_inBytesRead - _inBytesUsed) & (int)0xFFFFFFFE) // if the result is odd take off the 0 to make it even
452+
unchecked(bytesAvailable & (int)0xFFFFFFFE) // if the result is odd take off the 0 to make it even
450453
);
454+
451455

452456
if (bytesToRead > 0)
453457
{
458+
build.AppendLine($" ({count} bulk) bytesToRead {bytesToRead} = Math.Min(({charsCount} - {charsCopied}) * 2, unchecked({bytesAvailable} & (int)0xFFFFFFFE)");
459+
build.AppendLine($" ({count} before)(bytesAvailable {bytesAvailable}, _inBytesPacket {_inBytesPacket}, _inBytesRead {_inBytesRead}, _inBytesUsed {_inBytesUsed} ");
460+
454461
Buffer.BlockCopy(
455462
_inBuff,
456463
_inBytesUsed,
@@ -459,15 +466,19 @@ internal bool TryReadChars(char[] chars, int charsOffset, int charsCount, out in
459466
bytesToRead
460467
);
461468

462-
if ((_inBytesPacket - bytesToRead) < 0)
463-
{
464-
throw new Exception($"TryReadChars() {count} _inBytesPacket {_inBytesPacket}\n" +
465-
$" bytesToRead = Math.Min(({charsCount} - {charsCopied}) * 2, unchecked(({_inBytesRead} - {_inBytesUsed}) & (int)0xFFFFFFFE)");
466-
}
467-
468-
charsCopied = bytesToRead / 2;
469+
charsCopied += bytesToRead / 2;
469470
_inBytesUsed += bytesToRead;
470471
_inBytesPacket -= bytesToRead;
472+
473+
bytesAvailable = _inBytesRead - _inBytesUsed;
474+
build.AppendLine($" ({count} after )(bytesAvailable {bytesAvailable}, _inBytesPacket {_inBytesPacket}, _inBytesRead {_inBytesRead}, _inBytesUsed {_inBytesUsed} ");
475+
476+
if (_inBytesPacket < 0)
477+
{
478+
//throw new Exception($"TryReadChars() {count} _inBytesPacket {_inBytesPacket}\n" +
479+
//$" bytesToRead = Math.Min(({charsCount} - {charsCopied}) * 2, unchecked(({_inBytesRead} - {_inBytesUsed}) & (int)0xFFFFFFFE)");
480+
throw new Exception("not enough bytes available to read" + Environment.NewLine + build.ToString());
481+
}
471482
}
472483

473484
// if the number of chars requested is lower than the number copied then we need
@@ -476,6 +487,10 @@ internal bool TryReadChars(char[] chars, int charsOffset, int charsCount, out in
476487

477488
if (charsCopied < charsCount)
478489
{
490+
bytesAvailable = _inBytesRead - _inBytesUsed;
491+
build.AppendLine($" ({count} char) read 1 char and new packet");
492+
build.AppendLine($" ({count} before)(bytesAvailable {bytesAvailable}, _inBytesPacket {_inBytesPacket}, _inBytesRead {_inBytesRead}, _inBytesUsed {_inBytesUsed} ");
493+
479494
bool result = TryReadChar(out chars[charsOffset + charsCopied]);
480495
if (result)
481496
{
@@ -485,7 +500,13 @@ internal bool TryReadChars(char[] chars, int charsOffset, int charsCount, out in
485500
{
486501
return false;
487502
}
503+
504+
bytesAvailable = _inBytesRead - _inBytesUsed;
505+
build.AppendLine($" ({count} after )(bytesAvailable {bytesAvailable}, _inBytesPacket {_inBytesPacket}, _inBytesRead {_inBytesRead}, _inBytesUsed {_inBytesUsed} ");
488506
}
507+
508+
build.AppendLine($" ({count} end) charsCopied {charsCopied}");
509+
489510
count += 1;
490511
}
491512
return true;
Lines changed: 124 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,124 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
2-
// The .NET Foundation licenses this file to you under the MIT license.
3-
// See the LICENSE file in the project root for more information.
4-
5-
using System;
6-
using System.Collections.Generic;
7-
using System.Linq;
8-
using Azure.Identity;
9-
using Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider;
10-
using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup;
11-
using Xunit;
12-
13-
namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted
14-
{
15-
public class AKVTest : IClassFixture<SQLSetupStrategyAzureKeyVault>
16-
{
17-
private readonly SQLSetupStrategyAzureKeyVault _fixture;
18-
private readonly string _akvTableName;
19-
20-
public AKVTest(SQLSetupStrategyAzureKeyVault fixture)
21-
{
22-
_fixture = fixture;
23-
_akvTableName = fixture.AKVTestTable.Name;
24-
25-
// Disable the cache to avoid false failures.
26-
SqlConnection.ColumnEncryptionQueryMetadataCacheEnabled = false;
27-
}
28-
29-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE), nameof(DataTestUtility.IsAKVSetupAvailable))]
30-
public void TestEncryptDecryptWithAKV()
31-
{
32-
SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionStringHGSVBS)
33-
{
34-
ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled,
35-
AttestationProtocol = SqlConnectionAttestationProtocol.NotSpecified,
36-
EnclaveAttestationUrl = ""
37-
};
38-
using SqlConnection sqlConnection = new (builder.ConnectionString);
39-
40-
sqlConnection.Open();
41-
Customer customer = new(45, "Microsoft", "Corporation");
42-
43-
// Start a transaction and either commit or rollback based on the test variation.
44-
using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction())
45-
{
46-
DatabaseHelper.InsertCustomerData(sqlConnection, sqlTransaction, _akvTableName, customer);
47-
sqlTransaction.Commit();
48-
}
49-
50-
// Test INPUT parameter on an encrypted parameter
51-
using SqlCommand sqlCommand = new ($"SELECT CustomerId, FirstName, LastName FROM [{_akvTableName}] WHERE FirstName = @firstName",
52-
sqlConnection);
53-
SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
54-
customerFirstParam.Direction = System.Data.ParameterDirection.Input;
55-
customerFirstParam.ForceColumnEncryption = true;
56-
57-
using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
58-
DatabaseHelper.ValidateResultSet(sqlDataReader);
59-
}
60-
61-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAKVSetupAvailable))]
62-
[PlatformSpecific(TestPlatforms.Windows)]
63-
public void TestRoundTripWithAKVAndCertStoreProvider()
64-
{
65-
using SQLSetupStrategyCertStoreProvider certStoreFixture = new ();
66-
byte[] plainTextColumnEncryptionKey = ColumnEncryptionKey.GenerateRandomBytes(ColumnEncryptionKey.KeySizeInBytes);
67-
byte[] encryptedColumnEncryptionKeyUsingAKV = _fixture.AkvStoreProvider.EncryptColumnEncryptionKey(DataTestUtility.AKVUrl, @"RSA_OAEP", plainTextColumnEncryptionKey);
68-
byte[] columnEncryptionKeyReturnedAKV2Cert = certStoreFixture.CertStoreProvider.DecryptColumnEncryptionKey(certStoreFixture.CspColumnMasterKey.KeyPath, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingAKV);
69-
Assert.True(plainTextColumnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedAKV2Cert), @"Roundtrip failed");
70-
71-
// Try the opposite.
72-
byte[] encryptedColumnEncryptionKeyUsingCert = certStoreFixture.CertStoreProvider.EncryptColumnEncryptionKey(certStoreFixture.CspColumnMasterKey.KeyPath, @"RSA_OAEP", plainTextColumnEncryptionKey);
73-
byte[] columnEncryptionKeyReturnedCert2AKV = _fixture.AkvStoreProvider.DecryptColumnEncryptionKey(DataTestUtility.AKVUrl, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingCert);
74-
Assert.True(plainTextColumnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedCert2AKV), @"Roundtrip failed");
75-
}
76-
77-
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE), nameof(DataTestUtility.IsAKVSetupAvailable))]
78-
public void TestLocalCekCacheIsScopedToProvider()
79-
{
80-
SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionStringHGSVBS)
81-
{
82-
ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled,
83-
AttestationProtocol = SqlConnectionAttestationProtocol.NotSpecified,
84-
EnclaveAttestationUrl = ""
85-
};
86-
87-
using SqlConnection sqlConnection = new(builder.ConnectionString);
88-
89-
sqlConnection.Open();
90-
91-
// Test INPUT parameter on an encrypted parameter
92-
using SqlCommand sqlCommand = new($"SELECT CustomerId, FirstName, LastName FROM [{_akvTableName}] WHERE FirstName = @firstName",
93-
sqlConnection);
94-
SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
95-
customerFirstParam.Direction = System.Data.ParameterDirection.Input;
96-
customerFirstParam.ForceColumnEncryption = true;
97-
98-
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
99-
sqlDataReader.Close();
100-
101-
SqlColumnEncryptionAzureKeyVaultProvider sqlColumnEncryptionAzureKeyVaultProvider =
102-
new(new SqlClientCustomTokenCredential());
103-
104-
Dictionary<string, SqlColumnEncryptionKeyStoreProvider> customProvider = new()
105-
{
106-
{ SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, sqlColumnEncryptionAzureKeyVaultProvider }
107-
};
108-
109-
// execute a query using provider from command-level cache. this will cache the cek in the local cek cache
110-
sqlCommand.RegisterColumnEncryptionKeyStoreProvidersOnCommand(customProvider);
111-
SqlDataReader sqlDataReader2 = sqlCommand.ExecuteReader();
112-
sqlDataReader2.Close();
113-
114-
// global cek cache and local cek cache are populated above
115-
// when using a new per-command provider, it will only use its local cek cache
116-
// the following query should fail due to an empty cek cache and invalid credentials
117-
customProvider[SqlColumnEncryptionAzureKeyVaultProvider.ProviderName] =
118-
new SqlColumnEncryptionAzureKeyVaultProvider(new ClientSecretCredential("tenant", "client", "secret"));
119-
sqlCommand.RegisterColumnEncryptionKeyStoreProvidersOnCommand(customProvider);
120-
Exception ex = Assert.Throws<SqlException>(() => sqlCommand.ExecuteReader());
121-
Assert.StartsWith("The current credential is not configured to acquire tokens for tenant", ex.InnerException.Message);
122-
}
123-
}
124-
}
1+
//// Licensed to the .NET Foundation under one or more agreements.
2+
//// The .NET Foundation licenses this file to you under the MIT license.
3+
//// See the LICENSE file in the project root for more information.
4+
5+
//using System;
6+
//using System.Collections.Generic;
7+
//using System.Linq;
8+
//using Azure.Identity;
9+
//using Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider;
10+
//using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup;
11+
//using Xunit;
12+
13+
//namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted
14+
//{
15+
// public class AKVTest : IClassFixture<SQLSetupStrategyAzureKeyVault>
16+
// {
17+
// private readonly SQLSetupStrategyAzureKeyVault _fixture;
18+
// private readonly string _akvTableName;
19+
20+
// public AKVTest(SQLSetupStrategyAzureKeyVault fixture)
21+
// {
22+
// _fixture = fixture;
23+
// _akvTableName = fixture.AKVTestTable.Name;
24+
25+
// // Disable the cache to avoid false failures.
26+
// SqlConnection.ColumnEncryptionQueryMetadataCacheEnabled = false;
27+
// }
28+
29+
// [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE), nameof(DataTestUtility.IsAKVSetupAvailable))]
30+
// public void TestEncryptDecryptWithAKV()
31+
// {
32+
// SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionStringHGSVBS)
33+
// {
34+
// ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled,
35+
// AttestationProtocol = SqlConnectionAttestationProtocol.NotSpecified,
36+
// EnclaveAttestationUrl = ""
37+
// };
38+
// using SqlConnection sqlConnection = new (builder.ConnectionString);
39+
40+
// sqlConnection.Open();
41+
// Customer customer = new(45, "Microsoft", "Corporation");
42+
43+
// // Start a transaction and either commit or rollback based on the test variation.
44+
// using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction())
45+
// {
46+
// DatabaseHelper.InsertCustomerData(sqlConnection, sqlTransaction, _akvTableName, customer);
47+
// sqlTransaction.Commit();
48+
// }
49+
50+
// // Test INPUT parameter on an encrypted parameter
51+
// using SqlCommand sqlCommand = new ($"SELECT CustomerId, FirstName, LastName FROM [{_akvTableName}] WHERE FirstName = @firstName",
52+
// sqlConnection);
53+
// SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
54+
// customerFirstParam.Direction = System.Data.ParameterDirection.Input;
55+
// customerFirstParam.ForceColumnEncryption = true;
56+
57+
// using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
58+
// DatabaseHelper.ValidateResultSet(sqlDataReader);
59+
// }
60+
61+
// [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAKVSetupAvailable))]
62+
// [PlatformSpecific(TestPlatforms.Windows)]
63+
// public void TestRoundTripWithAKVAndCertStoreProvider()
64+
// {
65+
// using SQLSetupStrategyCertStoreProvider certStoreFixture = new ();
66+
// byte[] plainTextColumnEncryptionKey = ColumnEncryptionKey.GenerateRandomBytes(ColumnEncryptionKey.KeySizeInBytes);
67+
// byte[] encryptedColumnEncryptionKeyUsingAKV = _fixture.AkvStoreProvider.EncryptColumnEncryptionKey(DataTestUtility.AKVUrl, @"RSA_OAEP", plainTextColumnEncryptionKey);
68+
// byte[] columnEncryptionKeyReturnedAKV2Cert = certStoreFixture.CertStoreProvider.DecryptColumnEncryptionKey(certStoreFixture.CspColumnMasterKey.KeyPath, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingAKV);
69+
// Assert.True(plainTextColumnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedAKV2Cert), @"Roundtrip failed");
70+
71+
// // Try the opposite.
72+
// byte[] encryptedColumnEncryptionKeyUsingCert = certStoreFixture.CertStoreProvider.EncryptColumnEncryptionKey(certStoreFixture.CspColumnMasterKey.KeyPath, @"RSA_OAEP", plainTextColumnEncryptionKey);
73+
// byte[] columnEncryptionKeyReturnedCert2AKV = _fixture.AkvStoreProvider.DecryptColumnEncryptionKey(DataTestUtility.AKVUrl, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingCert);
74+
// Assert.True(plainTextColumnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedCert2AKV), @"Roundtrip failed");
75+
// }
76+
77+
// [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE), nameof(DataTestUtility.IsAKVSetupAvailable))]
78+
// public void TestLocalCekCacheIsScopedToProvider()
79+
// {
80+
// SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionStringHGSVBS)
81+
// {
82+
// ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled,
83+
// AttestationProtocol = SqlConnectionAttestationProtocol.NotSpecified,
84+
// EnclaveAttestationUrl = ""
85+
// };
86+
87+
// using SqlConnection sqlConnection = new(builder.ConnectionString);
88+
89+
// sqlConnection.Open();
90+
91+
// // Test INPUT parameter on an encrypted parameter
92+
// using SqlCommand sqlCommand = new($"SELECT CustomerId, FirstName, LastName FROM [{_akvTableName}] WHERE FirstName = @firstName",
93+
// sqlConnection);
94+
// SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
95+
// customerFirstParam.Direction = System.Data.ParameterDirection.Input;
96+
// customerFirstParam.ForceColumnEncryption = true;
97+
98+
// SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
99+
// sqlDataReader.Close();
100+
101+
// SqlColumnEncryptionAzureKeyVaultProvider sqlColumnEncryptionAzureKeyVaultProvider =
102+
// new(new SqlClientCustomTokenCredential());
103+
104+
// Dictionary<string, SqlColumnEncryptionKeyStoreProvider> customProvider = new()
105+
// {
106+
// { SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, sqlColumnEncryptionAzureKeyVaultProvider }
107+
// };
108+
109+
// // execute a query using provider from command-level cache. this will cache the cek in the local cek cache
110+
// sqlCommand.RegisterColumnEncryptionKeyStoreProvidersOnCommand(customProvider);
111+
// SqlDataReader sqlDataReader2 = sqlCommand.ExecuteReader();
112+
// sqlDataReader2.Close();
113+
114+
// // global cek cache and local cek cache are populated above
115+
// // when using a new per-command provider, it will only use its local cek cache
116+
// // the following query should fail due to an empty cek cache and invalid credentials
117+
// customProvider[SqlColumnEncryptionAzureKeyVaultProvider.ProviderName] =
118+
// new SqlColumnEncryptionAzureKeyVaultProvider(new ClientSecretCredential("tenant", "client", "secret"));
119+
// sqlCommand.RegisterColumnEncryptionKeyStoreProvidersOnCommand(customProvider);
120+
// Exception ex = Assert.Throws<SqlException>(() => sqlCommand.ExecuteReader());
121+
// Assert.StartsWith("The current credential is not configured to acquire tokens for tenant", ex.InnerException.Message);
122+
// }
123+
// }
124+
//}

0 commit comments

Comments
 (0)