public void Deserialize_binary_data_should_return_expected_result_when_guid_representation_is_specified( [ClassValues(typeof(GuidModeValues))] GuidMode mode, [Values(-1, GuidRepresentation.Unspecified)] GuidRepresentation readerGuidRepresentation, [Values(GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy, GuidRepresentation.Standard)] GuidRepresentation guidRepresentation) { #pragma warning disable 618 mode.Set(); var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); var subject = new ObjectSerializer(discriminatorConvention, guidRepresentation); var bytes = new byte[] { 29, 0, 0, 0, 5, 120, 0, 16, 0, 0, 0, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0 }; var subType = GuidConverter.GetSubType(guidRepresentation); bytes[11] = (byte)subType; var readerSettings = new BsonBinaryReaderSettings(); if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { readerSettings.GuidRepresentation = readerGuidRepresentation == (GuidRepresentation)(-1) ? guidRepresentation : GuidRepresentation.Unspecified; } using (var memoryStream = new MemoryStream(bytes)) using (var reader = new BsonBinaryReader(memoryStream, readerSettings)) { var context = BsonDeserializationContext.CreateRoot(reader); reader.ReadStartDocument(); reader.ReadName("x"); var result = subject.Deserialize <object>(context); var guidBytes = bytes.Skip(12).Take(16).ToArray(); var expectedResult = GuidConverter.FromBytes(guidBytes, guidRepresentation); result.Should().Be(expectedResult); } #pragma warning restore 618 }
// public methods /// <summary> /// Deserializes a value. /// </summary> /// <param name="context">The deserialization context.</param> /// <param name="args">The deserialization args.</param> /// <returns>A deserialized value.</returns> public override Guid Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { var bsonReader = context.Reader; string message; var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Binary: #pragma warning disable 618 BsonBinaryData binaryData; if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && _guidRepresentation == GuidRepresentation.Unspecified) { binaryData = bsonReader.ReadBinaryData(); } else { binaryData = bsonReader.ReadBinaryDataWithGuidRepresentationUnspecified(); } var bytes = binaryData.Bytes; var subType = binaryData.SubType; var guidRepresentation = _guidRepresentation; if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && guidRepresentation == GuidRepresentation.Unspecified) { guidRepresentation = binaryData.GuidRepresentation; } if (bytes.Length != 16) { message = string.Format("Expected length to be 16, not {0}.", bytes.Length); throw new FormatException(message); } if (subType != BsonBinarySubType.UuidStandard && subType != BsonBinarySubType.UuidLegacy) { message = string.Format("Expected binary sub type to be UuidStandard or UuidLegacy, not {0}.", subType); throw new FormatException(message); } if (guidRepresentation == GuidRepresentation.Unspecified) { throw new BsonSerializationException("GuidSerializer cannot deserialize a Guid when GuidRepresentation is Unspecified."); } if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V3 || _guidRepresentation != GuidRepresentation.Unspecified) { var expectedSubType = GuidConverter.GetSubType(guidRepresentation); if (subType != expectedSubType) { throw new FormatException($"GuidSerializer cannot deserialize a Guid when GuidRepresentation is {guidRepresentation} and binary sub type is {subType}."); } } return(GuidConverter.FromBytes(bytes, guidRepresentation)); #pragma warning restore 618 case BsonType.String: return(new Guid(bsonReader.ReadString())); default: throw CreateCannotDeserializeFromBsonTypeException(bsonType); } }
public void Deserializer_should_return_expected_result_when_representation_is_binary( GuidRepresentationMode defaultGuidRepresentationMode, GuidRepresentation defaultGuidRepresentation, GuidRepresentation serializerGuidRepresentation, GuidRepresentation readerGuidRepresentation, GuidRepresentation expectedGuidRepresentation) { GuidMode.Set(defaultGuidRepresentationMode, defaultGuidRepresentation); var subject = new GuidSerializer(serializerGuidRepresentation); var documentBytes = new byte[] { 29, 0, 0, 0, 5, 120, 0, 16, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0 }; var documentSubType = GuidConverter.GetSubType(expectedGuidRepresentation); documentBytes[11] = (byte)documentSubType; var readerSettings = new BsonBinaryReaderSettings(); if (defaultGuidRepresentationMode == GuidRepresentationMode.V2) { #pragma warning disable 618 readerSettings.GuidRepresentation = readerGuidRepresentation; #pragma warning restore 618 } var reader = new BsonBinaryReader(new MemoryStream(documentBytes), readerSettings); reader.ReadStartDocument(); reader.ReadName("x"); var context = BsonDeserializationContext.CreateRoot(reader); var args = new BsonDeserializationArgs(); var result = subject.Deserialize(context, args); var guidBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; var expectedGuid = GuidConverter.FromBytes(guidBytes, expectedGuidRepresentation); result.Should().Be(expectedGuid); }
public static IEnumerable <object[]> Deserialize_should_return_expected_result_when_representation_is_binary_MemberData() { var data = new TheoryData <GuidRepresentationMode, GuidRepresentation, GuidRepresentation, GuidRepresentation, GuidRepresentation>(); foreach (var defaultGuidRepresentationMode in EnumHelper.GetValues <GuidRepresentationMode>()) { foreach (var defaultGuidRepresentation in EnumHelper.GetValues <GuidRepresentation>()) { if (defaultGuidRepresentationMode == GuidRepresentationMode.V3 && defaultGuidRepresentation != GuidRepresentation.Unspecified) { continue; } foreach (var serializerGuidRepresentation in EnumHelper.GetValues <GuidRepresentation>()) { if (defaultGuidRepresentationMode == GuidRepresentationMode.V3 && serializerGuidRepresentation == GuidRepresentation.Unspecified) { continue; } foreach (var readerGuidRepresentation in EnumHelper.GetValues <GuidRepresentation>()) { if (defaultGuidRepresentationMode == GuidRepresentationMode.V2 && serializerGuidRepresentation != GuidRepresentation.Unspecified && readerGuidRepresentation != GuidRepresentation.Unspecified && GuidConverter.GetSubType(serializerGuidRepresentation) != GuidConverter.GetSubType(readerGuidRepresentation)) { continue; } if (defaultGuidRepresentationMode == GuidRepresentationMode.V3 && readerGuidRepresentation != GuidRepresentation.Unspecified) { continue; } var expectedGuidRepresentation = serializerGuidRepresentation; if (defaultGuidRepresentationMode == GuidRepresentationMode.V2 && expectedGuidRepresentation == GuidRepresentation.Unspecified) { expectedGuidRepresentation = readerGuidRepresentation; } if (expectedGuidRepresentation == GuidRepresentation.Unspecified) { continue; } data.Add(defaultGuidRepresentationMode, defaultGuidRepresentation, serializerGuidRepresentation, readerGuidRepresentation, expectedGuidRepresentation); } } } } return(data); }
/// <summary> /// Serializes a value. /// </summary> /// <param name="context">The serialization context.</param> /// <param name="args">The serialization args.</param> /// <param name="value">The object.</param> public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Guid value) { var bsonWriter = context.Writer; switch (_representation) { #pragma warning disable 618 case BsonType.Binary: var guidRepresentation = _guidRepresentation; if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && guidRepresentation == GuidRepresentation.Unspecified) { guidRepresentation = bsonWriter.Settings.GuidRepresentation; } if (guidRepresentation == GuidRepresentation.Unspecified) { throw new BsonSerializationException("GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified."); } var bytes = GuidConverter.ToBytes(value, guidRepresentation); var subType = GuidConverter.GetSubType(guidRepresentation); if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { var binaryData = new BsonBinaryData(bytes, subType, guidRepresentation); bsonWriter.PushSettings(s => s.GuidRepresentation = GuidRepresentation.Unspecified); try { bsonWriter.WriteBinaryData(binaryData); } finally { bsonWriter.PopSettings(); } } else { var binaryData = new BsonBinaryData(bytes, subType); bsonWriter.WriteBinaryData(binaryData); } break; #pragma warning restore 618 case BsonType.String: bsonWriter.WriteString(value.ToString()); break; default: var message = string.Format("'{0}' is not a valid Guid representation.", _representation); throw new BsonSerializationException(message); } }
public static IEnumerable <object[]> Serialize_should_write_expected_bytes_MemberData() { var data = new TheoryData <GuidRepresentationMode, GuidRepresentation, GuidRepresentation, GuidRepresentation?, BsonBinarySubType, GuidRepresentation>(); foreach (var defaultGuidRepresentationMode in EnumHelper.GetValues <GuidRepresentationMode>()) { foreach (var defaultGuidRepresentation in EnumHelper.GetValues <GuidRepresentation>()) { if (defaultGuidRepresentationMode == GuidRepresentationMode.V3 && defaultGuidRepresentation != GuidRepresentation.Unspecified) { continue; } foreach (var serializerGuidRepresentation in EnumHelper.GetValues <GuidRepresentation>()) { foreach (var writerGuidRepresentation in EnumHelper.GetValuesAndNull <GuidRepresentation>()) { var effectiveGuidRepresentation = serializerGuidRepresentation; if (defaultGuidRepresentationMode == GuidRepresentationMode.V2 && serializerGuidRepresentation == GuidRepresentation.Unspecified) { effectiveGuidRepresentation = writerGuidRepresentation ?? defaultGuidRepresentation; } if (effectiveGuidRepresentation == GuidRepresentation.Unspecified) { continue; } if (defaultGuidRepresentationMode == GuidRepresentationMode.V2) { var effectiveWriterGuidRepresentation = writerGuidRepresentation ?? defaultGuidRepresentation; if (effectiveWriterGuidRepresentation != GuidRepresentation.Unspecified && effectiveGuidRepresentation != effectiveWriterGuidRepresentation) { continue; } } var expectedSubType = GuidConverter.GetSubType(effectiveGuidRepresentation); data.Add(defaultGuidRepresentationMode, defaultGuidRepresentation, serializerGuidRepresentation, writerGuidRepresentation, expectedSubType, effectiveGuidRepresentation); } } } } return(data); }
public static IEnumerable <object[]> Deserialize_should_throw_when_representation_is_binary_and_sub_type_does_not_match_MemberData() { var data = new TheoryData <GuidRepresentationMode, GuidRepresentation, GuidRepresentation, GuidRepresentation?, BsonBinarySubType>(); foreach (var defaultGuidRepresentationMode in EnumHelper.GetValues <GuidRepresentationMode>()) { if (defaultGuidRepresentationMode == GuidRepresentationMode.V2) { continue; // for backward compatibility GuidSerializer only enforces this constraint in V3 mode } foreach (var defaultGuidRepresentation in EnumHelper.GetValues <GuidRepresentation>()) { if (defaultGuidRepresentationMode == GuidRepresentationMode.V3 && defaultGuidRepresentation != GuidRepresentation.Unspecified) { continue; } foreach (var serializerGuidRepresentation in EnumHelper.GetValues <GuidRepresentation>()) { foreach (var readerGuidRepresentation in EnumHelper.GetValuesAndNull <GuidRepresentation>()) { var effectiveGuidRepresentation = serializerGuidRepresentation; #pragma warning disable 618 if (defaultGuidRepresentationMode == GuidRepresentationMode.V2 && serializerGuidRepresentation == GuidRepresentation.Unspecified) { effectiveGuidRepresentation = readerGuidRepresentation ?? defaultGuidRepresentation; } #pragma warning restore 618 if (effectiveGuidRepresentation == GuidRepresentation.Unspecified) { continue; } var expectedSubType = GuidConverter.GetSubType(effectiveGuidRepresentation); data.Add(defaultGuidRepresentationMode, defaultGuidRepresentation, serializerGuidRepresentation, readerGuidRepresentation, expectedSubType); } } } } return(data); }
public void Serialize_guid_should_have_expected_result_when_guid_representation_is_unspecified_and_mode_is_v2( [Values(GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy, GuidRepresentation.Standard, GuidRepresentation.Unspecified)] GuidRepresentation defaultGuidRepresentation, [Values(GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy, GuidRepresentation.Standard, GuidRepresentation.Unspecified)] GuidRepresentation writerGuidRepresentation) { #pragma warning disable 618 var expectedGuidRepresentation = writerGuidRepresentation != GuidRepresentation.Unspecified ? writerGuidRepresentation : defaultGuidRepresentation; if (expectedGuidRepresentation == GuidRepresentation.Unspecified) { throw new SkipException("Test skipped because expectedGuidRepresentation is Unspecified."); } BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V2; BsonDefaults.GuidRepresentation = defaultGuidRepresentation; var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); var subject = new ObjectSerializer(discriminatorConvention, GuidRepresentation.Unspecified); var writerSettings = new BsonBinaryWriterSettings(); if (writerGuidRepresentation != GuidRepresentation.Unspecified) { writerSettings.GuidRepresentation = writerGuidRepresentation; } using (var memoryStream = new MemoryStream()) using (var writer = new BsonBinaryWriter(memoryStream, writerSettings)) { var context = BsonSerializationContext.CreateRoot(writer); var guid = Guid.Parse("01020304-0506-0708-090a-0b0c0d0e0f10"); writer.WriteStartDocument(); writer.WriteName("x"); subject.Serialize(context, guid); writer.WriteEndDocument(); var bytes = memoryStream.ToArray(); var expectedBytes = new byte[] { 29, 0, 0, 0, 5, 120, 0, 16, 0, 0, 0, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0 }; var expectedSubType = GuidConverter.GetSubType(expectedGuidRepresentation); var expectedGuidBytes = GuidConverter.ToBytes(guid, expectedGuidRepresentation); expectedBytes[11] = (byte)expectedSubType; Array.Copy(expectedGuidBytes, 0, expectedBytes, 12, 16); bytes.Should().Equal(expectedBytes); } #pragma warning restore 618 }
/// <summary> /// Serializes a value. /// </summary> /// <param name="context">The serialization context.</param> /// <param name="args">The serialization args.</param> /// <param name="value">The object.</param> protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, BsonBinaryData value) { var bsonWriter = context.Writer; #pragma warning disable 618 if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { var subType = value.SubType; if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { var writerGuidRepresentation = bsonWriter.Settings.GuidRepresentation; if (writerGuidRepresentation != GuidRepresentation.Unspecified) { var bytes = value.Bytes; var guidRepresentation = value.GuidRepresentation; if (guidRepresentation == GuidRepresentation.Unspecified) { var message = string.Format( "Cannot serialize BsonBinaryData with GuidRepresentation Unspecified to destination with GuidRepresentation {0}.", writerGuidRepresentation); throw new BsonSerializationException(message); } if (guidRepresentation != writerGuidRepresentation) { var guid = GuidConverter.FromBytes(bytes, guidRepresentation); bytes = GuidConverter.ToBytes(guid, writerGuidRepresentation); subType = GuidConverter.GetSubType(writerGuidRepresentation); guidRepresentation = writerGuidRepresentation; value = new BsonBinaryData(bytes, subType, guidRepresentation); } } } } #pragma warning restore 618 bsonWriter.WriteBinaryData(value); }
public void TestMapGuid( [ClassValues(typeof(GuidModeValues))] GuidMode mode) { mode.Set(); #pragma warning disable 618 var value = Guid.NewGuid(); if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && BsonDefaults.GuidRepresentation != GuidRepresentation.Unspecified) { var bsonValue = (BsonBinaryData)BsonTypeMapper.MapToBsonValue(value); byte[] expectedBytes; BsonBinarySubType expectedSubType; var guidRepresentation = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? BsonDefaults.GuidRepresentation : GuidRepresentation.Unspecified; if (guidRepresentation == GuidRepresentation.Unspecified) { expectedBytes = GuidConverter.ToBytes(value, GuidRepresentation.Standard); expectedSubType = BsonBinarySubType.UuidStandard; } else { expectedBytes = GuidConverter.ToBytes(value, guidRepresentation); expectedSubType = GuidConverter.GetSubType(guidRepresentation); } Assert.True(expectedBytes.SequenceEqual(bsonValue.Bytes)); Assert.Equal(expectedSubType, bsonValue.SubType); var bsonBinary = (BsonBinaryData)BsonTypeMapper.MapToBsonValue(value, BsonType.Binary); Assert.True(expectedBytes.SequenceEqual(bsonBinary.Bytes)); Assert.Equal(expectedSubType, bsonBinary.SubType); } else { var exception = Record.Exception(() => (BsonBinaryData)BsonTypeMapper.MapToBsonValue(value)); exception.Should().BeOfType <InvalidOperationException>(); exception = Record.Exception(() => (BsonBinaryData)BsonTypeMapper.MapToBsonValue(value, BsonType.Binary)); exception.Should().BeOfType <InvalidOperationException>(); } #pragma warning restore 618 }
public void TestImplicitConversionFromNullableGuid( [ClassValues(typeof(GuidModeValues))] GuidMode mode) { mode.Set(); #pragma warning disable 618 var guid = Guid.NewGuid(); if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && BsonDefaults.GuidRepresentation != GuidRepresentation.Unspecified) { BsonValue v = (Guid?)guid; BsonValue n = (Guid?)null; byte[] expectedBytes; BsonBinarySubType expectedSubType; var guidRepresentation = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? BsonDefaults.GuidRepresentation : GuidRepresentation.Unspecified; if (guidRepresentation == GuidRepresentation.Unspecified) { expectedBytes = GuidConverter.ToBytes(guid, GuidRepresentation.Standard); expectedSubType = BsonBinarySubType.UuidStandard; } else { expectedBytes = GuidConverter.ToBytes(guid, guidRepresentation); expectedSubType = GuidConverter.GetSubType(guidRepresentation); } Assert.IsType <BsonBinaryData>(v); Assert.IsType <BsonNull>(n); var b = (BsonBinaryData)v; Assert.True(expectedBytes.SequenceEqual(b.AsByteArray)); Assert.Equal(expectedSubType, b.SubType); } else { var exception = Record.Exception(() => { BsonValue v = (Guid?)guid; }); exception.Should().BeOfType <InvalidOperationException>(); } #pragma warning restore 618 }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="binaryData">The binary data.</param> public override void WriteBinaryData(BsonBinaryData binaryData) { if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (State != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } var bytes = binaryData.Bytes; var subType = binaryData.SubType; switch (subType) { case BsonBinarySubType.OldBinary: if (Settings.FixOldBinarySubTypeOnOutput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } break; case BsonBinarySubType.UuidLegacy: case BsonBinarySubType.UuidStandard: if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { if (Settings.GuidRepresentation != GuidRepresentation.Unspecified) { var expectedSubType = GuidConverter.GetSubType(Settings.GuidRepresentation); if (subType != expectedSubType) { var message = string.Format( "The GuidRepresentation for the writer is {0}, which requires the subType argument to be {1}, not {2}.", Settings.GuidRepresentation, expectedSubType, subType); throw new BsonSerializationException(message); } var guidRepresentation = binaryData.GuidRepresentation; if (guidRepresentation != Settings.GuidRepresentation) { var message = string.Format( "The GuidRepresentation for the writer is {0}, which requires the the guidRepresentation argument to also be {0}, not {1}.", Settings.GuidRepresentation, guidRepresentation); throw new BsonSerializationException(message); } } } break; } _bsonStream.WriteBsonType(BsonType.Binary); WriteNameHelper(); if (subType == BsonBinarySubType.OldBinary) { // sub type OldBinary has two sizes (for historical reasons) _bsonStream.WriteInt32(bytes.Length + 4); _bsonStream.WriteBinarySubType(subType); _bsonStream.WriteInt32(bytes.Length); } else { _bsonStream.WriteInt32(bytes.Length); _bsonStream.WriteBinarySubType(subType); } _bsonStream.WriteBytes(bytes, 0, bytes.Length); State = GetNextState(); }
public void SerializeValue_should_throw_when_value_representation_is_unspecified( [ClassValues(typeof(GuidModeValues))] GuidMode mode, [Values( GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy, GuidRepresentation.Standard, GuidRepresentation.Unspecified)] GuidRepresentation writerGuidRepresentation, [Values( GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy, GuidRepresentation.Standard, GuidRepresentation.Unspecified)] GuidRepresentation valueGuidRepresentation) { mode.Set(); #pragma warning disable 618 var subject = new BsonBinaryDataSerializer(); var mockWriter = new Mock <IBsonWriter>(); var writerSettings = new BsonBinaryWriterSettings(); if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { writerSettings.GuidRepresentation = writerGuidRepresentation; } mockWriter.SetupGet(m => m.Settings).Returns(writerSettings); var context = BsonSerializationContext.CreateRoot(mockWriter.Object); var args = new BsonSerializationArgs(); var subType = valueGuidRepresentation == GuidRepresentation.Unspecified ? BsonBinarySubType.UuidLegacy : GuidConverter.GetSubType(valueGuidRepresentation); var value = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? new BsonBinaryData(new byte[16], subType, valueGuidRepresentation) : new BsonBinaryData(new byte[16], subType); var exception = Record.Exception(() => subject.Serialize(context, args, value)); var isExceptionExpected = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && writerGuidRepresentation != GuidRepresentation.Unspecified && valueGuidRepresentation == GuidRepresentation.Unspecified; if (isExceptionExpected) { var e = exception.Should().BeOfType <BsonSerializationException>().Subject; e.Message.Should().Contain("Cannot serialize BsonBinaryData with GuidRepresentation Unspecified"); } else { exception.Should().BeNull(); } #pragma warning restore 618 }
public void SerializeValue_should_convert_representation_when_required( [ClassValues(typeof(GuidModeValues))] GuidMode mode, [Values( GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy, GuidRepresentation.Standard, GuidRepresentation.Unspecified)] GuidRepresentation writerGuidRepresentation, [Values( GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy, GuidRepresentation.Standard, GuidRepresentation.Unspecified)] GuidRepresentation valueGuidRepresentation) { mode.Set(); #pragma warning disable 618 var subject = new BsonBinaryDataSerializer(); var mockWriter = new Mock <IBsonWriter>(); var writerSettings = new BsonBinaryWriterSettings(); if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { writerSettings.GuidRepresentation = writerGuidRepresentation; } mockWriter.SetupGet(m => m.Settings).Returns(writerSettings); var context = BsonSerializationContext.CreateRoot(mockWriter.Object); var args = new BsonSerializationArgs(); var bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; var subType = valueGuidRepresentation == GuidRepresentation.Unspecified ? BsonBinarySubType.UuidLegacy : GuidConverter.GetSubType(valueGuidRepresentation); var value = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? new BsonBinaryData(bytes, subType, valueGuidRepresentation) : new BsonBinaryData(bytes, subType); var isExceptionExpected = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && writerGuidRepresentation != GuidRepresentation.Unspecified && valueGuidRepresentation == GuidRepresentation.Unspecified; if (!isExceptionExpected) { subject.Serialize(context, args, value); var shouldConvertRepresentation = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && writerGuidRepresentation != GuidRepresentation.Unspecified && valueGuidRepresentation != GuidRepresentation.Unspecified && valueGuidRepresentation != writerGuidRepresentation; var writtenValue = value; if (shouldConvertRepresentation) { var guid = GuidConverter.FromBytes(bytes, valueGuidRepresentation); var convertedBytes = GuidConverter.ToBytes(guid, writerGuidRepresentation); var convertedSubType = GuidConverter.GetSubType(writerGuidRepresentation); writtenValue = new BsonBinaryData(convertedBytes, convertedSubType, writerGuidRepresentation); } mockWriter.Verify(m => m.WriteBinaryData(writtenValue), Times.Once); } #pragma warning restore 618 }