Skip to content

CSHARP-5620: Fix CommandMessageBinaryEncoderTests failures on Big Endian systems #1714

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
merged 1 commit into from
Jun 25, 2025
Merged
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using MongoDB.TestHelpers.XunitExtensions;
using MongoDB.Driver.Core.Misc;
using Xunit;
using System.Buffers.Binary;

namespace MongoDB.Driver.Core.WireProtocol.Messages.Encoders.BinaryEncoders
{
Expand Down Expand Up @@ -141,7 +142,7 @@ public void ReadMessage_should_throw_when_flags_is_invalid(
[Values(-1, 1, 4)] int flags)
{
var bytes = CreateMessageBytes();
BitConverter.GetBytes(flags).CopyTo(bytes, 16);
BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(16, 4), flags);
var subject = CreateSubject(bytes);
var expectedMessage = flags == 1 ? "Command message CheckSumPresent flag not supported." : "Command message has invalid flags";

Expand Down Expand Up @@ -271,7 +272,7 @@ public void WriteMessage_should_write_messageLength(int[] sectionTypes)
var result = stream.ToArray();

result.Length.Should().Be(expectedMessageLength);
var writtenMessageLength = BitConverter.ToInt32(result, 0);
var writtenMessageLength = BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(0, 4));
writtenMessageLength.Should().Be(expectedMessageLength);
}

Expand All @@ -287,7 +288,7 @@ public void WriteMessage_should_write_requestId(
subject.WriteMessage(message);
var result = stream.ToArray();

var resultRequestId = BitConverter.ToInt32(result, 4);
var resultRequestId = BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(4, 4));
resultRequestId.Should().Be(requestId);
}

Expand All @@ -303,7 +304,7 @@ public void WriteMessage_should_write_responseTo(
subject.WriteMessage(message);
var result = stream.ToArray();

var resultResponseTo = BitConverter.ToInt32(result, 8);
var resultResponseTo = BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(8, 4));
resultResponseTo.Should().Be(responseTo);
}

Expand All @@ -317,7 +318,7 @@ public void WriteMessage_should_write_expected_opcode()
subject.WriteMessage(message);
var result = stream.ToArray();

var opcode = BitConverter.ToInt32(result, 12);
var opcode = BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(12, 4));
opcode.Should().Be((int)Opcode.OpMsg);
}

Expand All @@ -334,7 +335,7 @@ public void WriteMessage_should_write_flags(
subject.WriteMessage(message);

var result = stream.ToArray();
var flags = (OpMsgFlags)BitConverter.ToInt32(result, 16);
var flags = (OpMsgFlags)BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(16, 4));
flags.HasFlag(OpMsgFlags.MoreToCome).Should().Be(moreToCome);
flags.HasFlag(OpMsgFlags.ExhaustAllowed).Should().Be(exhaustAllowed);
}
Expand Down
Loading