Skip to content

Commit c4cc5e3

Browse files
authored
CSHARP-5620 Fix CommandMessageBinaryEncoderTests failures on Big Endian systems (#1714)
1 parent 72e05dd commit c4cc5e3

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

tests/MongoDB.Driver.Tests/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandMessageBinaryEncoderTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using MongoDB.TestHelpers.XunitExtensions;
2626
using MongoDB.Driver.Core.Misc;
2727
using Xunit;
28+
using System.Buffers.Binary;
2829

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

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

273274
result.Length.Should().Be(expectedMessageLength);
274-
var writtenMessageLength = BitConverter.ToInt32(result, 0);
275+
var writtenMessageLength = BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(0, 4));
275276
writtenMessageLength.Should().Be(expectedMessageLength);
276277
}
277278

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

290-
var resultRequestId = BitConverter.ToInt32(result, 4);
291+
var resultRequestId = BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(4, 4));
291292
resultRequestId.Should().Be(requestId);
292293
}
293294

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

306-
var resultResponseTo = BitConverter.ToInt32(result, 8);
307+
var resultResponseTo = BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(8, 4));
307308
resultResponseTo.Should().Be(responseTo);
308309
}
309310

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

320-
var opcode = BitConverter.ToInt32(result, 12);
321+
var opcode = BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(12, 4));
321322
opcode.Should().Be((int)Opcode.OpMsg);
322323
}
323324

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

336337
var result = stream.ToArray();
337-
var flags = (OpMsgFlags)BitConverter.ToInt32(result, 16);
338+
var flags = (OpMsgFlags)BinaryPrimitives.ReadInt32LittleEndian(result.AsSpan(16, 4));
338339
flags.HasFlag(OpMsgFlags.MoreToCome).Should().Be(moreToCome);
339340
flags.HasFlag(OpMsgFlags.ExhaustAllowed).Should().Be(exhaustAllowed);
340341
}

0 commit comments

Comments
 (0)