Fix CommandMessageBinaryEncoderTests failures on Big Endian systems

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Done
    • Priority: Unknown
    • 3.5.0
    • Affects Version/s: None
    • Component/s: Testing
    • None
    • Dotnet Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Description:

      Observed 12 test failures in MongoDB.Driver.Tests, specifically within the test class:
      `MongoDB.Driver.Core.WireProtocol.Messages.Encoders.BinaryEncoders.CommandMessageBinaryEncoderTests`

      These tests were failing on Big Endian architectures (e.g., s390x) due to mismatches in how 32-bit integers were being interpreted during encoding and decoding.

      At first glance, it appeared that the failures stem from incorrect serialization/deserialization logic in the source. However, a deeper debugging of the implementation suggests otherwise.

      The CommandMessageBinaryEncoder writes and reads integer values using a BsonStream, which in this context is implemented by BsonStreamAdapter. This adapter wraps a standard Stream and overrides the relevant methods to ensure wire protocol compliance.
      WriteInt32, ReadInt32

       

      Root Cause — Platform-Dependent Test Assertions:

      The issue lies in the test code, which uses:
       
      BitConverter.ToInt32(...)
      BitConverter.GetBytes(...)
       
      These APIs are inherently platform-endian, consequently, the test expectations did not hold when run on s390x.
      we can use  BinaryPrimitives.ReadInt32LittleEndian and BinaryPrimitives.WriteInt32LittleEndian instead

       
      It appears that the tests may be making assumptions about host endianness, whereas the source code explicitly writes in Little Endian as per wire protocol—so my understanding is that adjusting the tests, rather than the production code, seems like the right direction here.
       
      Tests Affected:

      • WriteMessage_should_write_messageLength
      • WriteMessage_should_write_flags
      • WriteMessage_should_write_responseTo
      • WriteMessage_should_write_requestId
      • WriteMessage_should_write_expected_opcode
      • ReadMessage_should_throw_when_flags_is_invalid

            Assignee:
            Boris Dogadov
            Reporter:
            Medha Tiwari
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: