Skip to content

Commit

Permalink
Merge pull request #1421 from daebo01:enum-serialize-fix-pr
Browse files Browse the repository at this point in the history
Fix packet corruption issue when enum was not 4 bytes.

Conflicts:
	src/MySqlConnector/MySqlParameter.cs
  • Loading branch information
bgrainger committed Dec 18, 2023
2 parents 2d44a00 + 9352d48 commit bd53d6e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
50 changes: 49 additions & 1 deletion tests/MySqlConnector.Tests/DummyEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,52 @@ internal enum DummyEnum
{
FirstValue,
SecondValue
}
}

internal enum DummyByteEnum : byte
{
FirstValue,
SecondValue = 0x11,
}

internal enum DummySByteEnum : sbyte
{
FirstValue,
SecondValue = 0x11,
}

internal enum DummyShortEnum : short
{
FirstValue,
SecondValue = 0x1122,
}

internal enum DummyUShortEnum : ushort
{
FirstValue,
SecondValue = 0x1122,
}

internal enum DummyIntEnum : int
{
FirstValue,
SecondValue = 0x11223344,
}

internal enum DummyUIntEnum : uint
{
FirstValue,
SecondValue = 0x11223344,
}

internal enum DummyLongEnum : long
{
FirstValue,
SecondValue = 0x11223344_55667788,
}

internal enum DummyULongEnum : ulong
{
FirstValue,
SecondValue = 0x11223344_55667788,
}
2 changes: 1 addition & 1 deletion tests/MySqlConnector.Tests/MySqlConnector.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

<ItemGroup Condition=" '$(Configuration)' == 'MySqlData' ">
<PackageReference Include="MySql.Data" />
<Compile Remove="ByteBufferWriterTests.cs;CachedProcedureTests.cs;CancellationTests.cs;ColumnCountPayloadTests.cs;ColumnReaderTests.cs;ConnectionTests.cs;FakeMySqlServer.cs;FakeMySqlServerConnection.cs;LoadBalancerTests.cs;MySqlDecimalTests.cs;MySqlExceptionTests.cs;MySqlParameterCollectionNameToIndexTests.cs;NormalizeTests.cs;ServerVersionTests.cs;StatementPreparerTests.cs;TypeMapperTests.cs;UtilityTests.cs" />
<Compile Remove="ByteBufferWriterTests.cs;CachedProcedureTests.cs;CancellationTests.cs;ColumnCountPayloadTests.cs;ColumnReaderTests.cs;ConnectionTests.cs;FakeMySqlServer.cs;FakeMySqlServerConnection.cs;LoadBalancerTests.cs;MySqlDecimalTests.cs;MySqlExceptionTests.cs;MySqlParameterAppendBinaryTests.cs;MySqlParameterCollectionNameToIndexTests.cs;NormalizeTests.cs;ServerVersionTests.cs;StatementPreparerTests.cs;TypeMapperTests.cs;UtilityTests.cs" />
<Compile Remove="Metrics\*.cs" />
<Using Include="MySql.Data.MySqlClient" />
<Using Include="MySql.Data.Types" />
Expand Down
27 changes: 27 additions & 0 deletions tests/MySqlConnector.Tests/MySqlParameterAppendBinaryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using MySqlConnector.Protocol.Serialization;

namespace MySqlConnector.Tests;

public class MySqlParameterAppendBinaryTests
{
[Theory]
[InlineData(DummySByteEnum.SecondValue, MySqlDbType.Byte, new byte[] { 0x11 })]
[InlineData(DummyByteEnum.SecondValue, MySqlDbType.UByte, new byte[] { 0x11 })]
[InlineData(DummyShortEnum.SecondValue, MySqlDbType.Int16, new byte[] { 0x22, 0x11 })]
[InlineData(DummyUShortEnum.SecondValue, MySqlDbType.UInt16, new byte[] { 0x22, 0x11 })]
[InlineData(DummyIntEnum.SecondValue, MySqlDbType.Int32, new byte[] { 0x44, 0x33, 0x22, 0x11 })]
[InlineData(DummyUIntEnum.SecondValue, MySqlDbType.UInt32, new byte[] { 0x44, 0x33, 0x22, 0x11 })]
[InlineData(DummyEnum.SecondValue, MySqlDbType.Int32, new byte[] { 0x01, 0x00, 0x00, 0x00 })]
[InlineData(DummyLongEnum.SecondValue, MySqlDbType.Int64, new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 })]
[InlineData(DummyULongEnum.SecondValue, MySqlDbType.UInt64, new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 })]
public void WriteBinaryEnumType(object value, MySqlDbType expectedMySqlDbType, byte[] expectedBinary)
{
var parameter = new MySqlParameter { Value = value };
var writer = new ByteBufferWriter();
parameter.AppendBinary(writer, StatementPreparerOptions.None);

Assert.Equal(parameter.MySqlDbType, expectedMySqlDbType);
Assert.Equal(writer.Position, expectedBinary.Length);
Assert.Equal(writer.ArraySegment, expectedBinary);
}
}

0 comments on commit bd53d6e

Please sign in to comment.