-
Notifications
You must be signed in to change notification settings - Fork 311
Add Feature Extension for User Agent #3451
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
Merged
+214
−1
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
27a8795
Add Feature Extension for User Agent
samsharma2700 035d6b2
resolving merge conflicts
samsharma2700 a27be5d
resolve conflicts
samsharma2700 8cf667f
Merge remote-tracking branch 'origin/main' into sam_feat_uagent
samsharma2700 9509553
Add summary and review changes
samsharma2700 ff70662
Merge branch 'main' of https://github.com/dotnet/SqlClient into sam_f…
samsharma2700 c3bacb9
Add unit tests for WriteUserAgentFeatureRequest
samsharma2700 56949db
Update tests to avoid reflection
samsharma2700 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
src/Microsoft.Data.SqlClient/tests/UnitTests/TdsParserInternalsTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using Xunit; | ||
|
||
#nullable enable | ||
|
||
namespace Microsoft.Data.SqlClient.UnitTests | ||
{ | ||
public class TdsParserInternalsTest | ||
{ | ||
// Selects and returns the first non-public instance constructor for TdsParser | ||
private static TdsParser CreateParserInstance() | ||
{ | ||
Type parserType = typeof(TdsParser); | ||
|
||
ConstructorInfo ctor = parserType | ||
.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic) | ||
.First(); | ||
|
||
// build default args for each parameter | ||
object?[] ctorArgs = ctor.GetParameters() | ||
.Select(p => p.ParameterType.IsValueType | ||
? Activator.CreateInstance(p.ParameterType) | ||
: null) | ||
.ToArray(); | ||
|
||
return (TdsParser)ctor.Invoke(ctorArgs); | ||
} | ||
|
||
// Helper function to extract private _physicalStateObj fields raw buffer and no. of bytes written so far | ||
private static (byte[] buffer, int count) ExtractOutputBuffer(TdsParser parser) | ||
samsharma2700 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
FieldInfo stateField = typeof(TdsParser) | ||
.GetField("_physicalStateObj", BindingFlags.Instance | BindingFlags.NonPublic) | ||
?? throw new InvalidOperationException("_physicalStateObj not found"); | ||
|
||
object stateObj = stateField.GetValue(parser) | ||
?? throw new InvalidOperationException("physical state object is null"); | ||
|
||
Type stateType = stateObj.GetType(); | ||
|
||
FieldInfo buffField = stateType | ||
.GetField("_outBuff", BindingFlags.Instance | BindingFlags.NonPublic) | ||
?? throw new InvalidOperationException("_outBuff not found"); | ||
|
||
byte[] buffer = (byte[])buffField.GetValue(stateObj)!; | ||
|
||
FieldInfo usedField = stateType | ||
.GetField("_outBytesUsed", BindingFlags.Instance | BindingFlags.NonPublic) | ||
?? throw new InvalidOperationException("_outBytesUsed not found"); | ||
|
||
int count = (int)usedField.GetValue(stateObj)!; | ||
|
||
return (buffer, count); | ||
} | ||
|
||
[Fact] | ||
public void WriteUserAgentFeatureRequest_WriteFalse_LengthOnlyReturn() | ||
{ | ||
byte[] payload = Encoding.UTF8.GetBytes("{\"kel\":\"sier\"}"); | ||
var parser = CreateParserInstance(); | ||
samsharma2700 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
int lengthOnly = parser.WriteUserAgentFeatureRequest(payload, write: false); | ||
|
||
// assert: total = 1 (feat-ID) + 4 (len field) + [1 (version) + payload.Length] | ||
int expectedDataLen = 1 + payload.Length; | ||
int expectedTotalLen = 1 + 4 + expectedDataLen; | ||
Assert.Equal(expectedTotalLen, lengthOnly); | ||
samsharma2700 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
[Fact] | ||
public void WriteUserAgentFeatureRequest_WriteTrue_AppendsOnlyExtensionBytes() | ||
{ | ||
byte[] payload = Encoding.UTF8.GetBytes("{\"kel\":\"sier\"}"); | ||
var parser = CreateParserInstance(); | ||
|
||
var (bufferBefore, countBefore) = ExtractOutputBuffer(parser); | ||
|
||
int returnedLength = parser.WriteUserAgentFeatureRequest(payload, write: true); | ||
|
||
var (bufferAfter, countAfter) = ExtractOutputBuffer(parser); | ||
samsharma2700 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
int appended = countAfter - countBefore; | ||
Assert.Equal(returnedLength, appended); | ||
|
||
int start = countBefore; | ||
|
||
Assert.Equal( | ||
TdsEnums.FEATUREEXT_USERAGENT, | ||
bufferAfter[start]); | ||
|
||
int dataLenFromStream = BitConverter.ToInt32(bufferAfter, start + 1); | ||
int expectedDataLen = 1 + payload.Length; | ||
Assert.Equal(expectedDataLen, dataLenFromStream); | ||
|
||
Assert.Equal( | ||
TdsEnums.SUPPORTED_USER_AGENT_VERSION, | ||
bufferAfter[start + 5]); | ||
|
||
byte[] writtenPayload = bufferAfter | ||
samsharma2700 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.Skip(start + 6) | ||
.Take(payload.Length) | ||
.ToArray(); | ||
Assert.Equal(payload, writtenPayload); | ||
|
||
Assert.Equal(returnedLength, appended); | ||
samsharma2700 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.