Skip to content

Commit 1a74751

Browse files
committed
CSHARP-2218: Fix BsonBinaryData constructor exception messages
1 parent 64bce88 commit 1a74751

File tree

3 files changed

+123
-14
lines changed

3 files changed

+123
-14
lines changed

src/MongoDB.Bson/ObjectModel/BsonBinaryData.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public BsonBinaryData(byte[] bytes, BsonBinarySubType subType, GuidRepresentatio
6161
{
6262
if (bytes == null)
6363
{
64-
throw new ArgumentNullException("bytes");
64+
throw new ArgumentNullException(nameof(bytes));
6565
}
6666
if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
6767
{
@@ -70,25 +70,39 @@ public BsonBinaryData(byte[] bytes, BsonBinarySubType subType, GuidRepresentatio
7070
var message = string.Format(
7171
"Length must be 16, not {0}, when subType is {1}.",
7272
bytes.Length, subType);
73-
throw new ArgumentException(message);
73+
throw new ArgumentException(message, nameof(bytes));
7474
}
75-
var expectedSubType = (guidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
75+
BsonBinarySubType expectedSubType;
76+
switch (guidRepresentation)
77+
{
78+
case GuidRepresentation.CSharpLegacy:
79+
case GuidRepresentation.JavaLegacy:
80+
case GuidRepresentation.PythonLegacy:
81+
case GuidRepresentation.Unspecified:
82+
expectedSubType = BsonBinarySubType.UuidLegacy;
83+
break;
84+
85+
case GuidRepresentation.Standard:
86+
expectedSubType = BsonBinarySubType.UuidStandard;
87+
break;
88+
89+
default:
90+
throw new ArgumentException($"Invalid guidRepresentation: {guidRepresentation}.", nameof(guidRepresentation));
91+
}
92+
7693
if (subType != expectedSubType)
7794
{
78-
var message = string.Format(
79-
"SubType must be {0}, not {1}, when GuidRepresentation is {2}.",
80-
expectedSubType, subType, GuidRepresentation);
81-
throw new ArgumentException(message);
95+
throw new ArgumentException($"GuidRepresentation {guidRepresentation} is only valid with subType {expectedSubType}, not with subType {subType}.", nameof(guidRepresentation));
8296
}
8397
}
8498
else
8599
{
86100
if (guidRepresentation != GuidRepresentation.Unspecified)
87101
{
88102
var message = string.Format(
89-
"GuidRepresentation must be Unspecified, not {0}, when SubType is not UuidStandard or UuidLegacy.",
103+
"GuidRepresentation must be Unspecified, not {0}, when subType is not UuidStandard or UuidLegacy.",
90104
guidRepresentation);
91-
throw new ArgumentException(message);
105+
throw new ArgumentException(message, nameof(guidRepresentation));
92106
}
93107
}
94108
_bytes = bytes;

src/MongoDB.Bson/ObjectModel/GuidConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public static Guid FromBytes(byte[] bytes, GuidRepresentation representation)
7777
/// Converts a Guid to a byte array.
7878
/// </summary>
7979
/// <param name="guid">The Guid.</param>
80-
/// <param name="representation">The representation of the Guid in the byte array.</param>
80+
/// <param name="guidRepresentation">The representation of the Guid in the byte array.</param>
8181
/// <returns>A byte array.</returns>
82-
public static byte[] ToBytes(Guid guid, GuidRepresentation representation)
82+
public static byte[] ToBytes(Guid guid, GuidRepresentation guidRepresentation)
8383
{
8484
var bytes = (byte[])guid.ToByteArray().Clone();
85-
switch (representation)
85+
switch (guidRepresentation)
8686
{
8787
case GuidRepresentation.CSharpLegacy:
8888
if (!BitConverter.IsLittleEndian)
@@ -114,7 +114,7 @@ public static byte[] ToBytes(Guid guid, GuidRepresentation representation)
114114
case GuidRepresentation.Unspecified:
115115
throw new InvalidOperationException("Unable to convert Guid to byte array because GuidRepresentation is Unspecified.");
116116
default:
117-
throw new BsonInternalException("Unexpected GuidRepresentation.");
117+
throw new ArgumentException($"Invalid guidRepresentation: {guidRepresentation}.", nameof(guidRepresentation));
118118
}
119119
return bytes;
120120
}

tests/MongoDB.Bson.Tests/ObjectModel/BsonBinaryDataTests.cs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,108 @@
1515

1616
using System;
1717
using System.Linq;
18-
using MongoDB.Bson;
18+
using FluentAssertions;
19+
using MongoDB.Bson.TestHelpers.XunitExtensions;
1920
using Xunit;
2021

2122
namespace MongoDB.Bson.Tests
2223
{
2324
public class BsonBinaryDataTests
2425
{
26+
[Theory]
27+
[ParameterAttributeData]
28+
public void constructor_should_throw_when_bytes_is_null(
29+
[Range(1, 3)] int overload)
30+
{
31+
var bytes = (byte[])null;
32+
33+
Exception exception = null;
34+
switch (overload)
35+
{
36+
case 1: exception = Record.Exception(() => new BsonBinaryData(bytes)); break;
37+
case 2: exception = Record.Exception(() => new BsonBinaryData(bytes, BsonBinarySubType.Binary)); break;
38+
case 3: exception = Record.Exception(() => new BsonBinaryData(bytes, BsonBinarySubType.Binary, GuidRepresentation.Unspecified)); break;
39+
}
40+
41+
var e = exception.Should().BeOfType<ArgumentNullException>().Subject;
42+
e.ParamName.Should().Be("bytes");
43+
}
44+
45+
[Theory]
46+
[ParameterAttributeData]
47+
public void constructor_should_throw_when_bytes_length_is_not_16_and_sub_type_is_uuid(
48+
[Values(BsonBinarySubType.UuidLegacy, BsonBinarySubType.UuidStandard)] BsonBinarySubType subType,
49+
[Values(0, 15, 17)] int length,
50+
[Range(1, 2)] int overload)
51+
{
52+
var bytes = new byte[length];
53+
var guidRepresentation = subType == BsonBinarySubType.UuidLegacy ? GuidRepresentation.CSharpLegacy : GuidRepresentation.Standard;
54+
55+
Exception exception = null;
56+
switch (overload)
57+
{
58+
case 1: exception = Record.Exception(() => new BsonBinaryData(bytes, subType)); break;
59+
case 2: exception = Record.Exception(() => new BsonBinaryData(bytes, subType, guidRepresentation)); break;
60+
}
61+
62+
var e = exception.Should().BeOfType<ArgumentException>().Subject;
63+
e.Message.Should().StartWith($"Length must be 16, not {length}, when subType is {subType}.");
64+
e.ParamName.Should().Be("bytes");
65+
}
66+
67+
[Theory]
68+
[ParameterAttributeData]
69+
public void constructor_should_throw_when_sub_type_is_uuid_and_guid_representation_is_invalid(
70+
[Values(5)] GuidRepresentation guidRepresentation,
71+
[Range(1, 2)] int overload)
72+
{
73+
var bytes = new byte[16];
74+
var guid = Guid.Empty;
75+
76+
Exception exception = null;
77+
switch (overload)
78+
{
79+
case 1: exception = Record.Exception(() => new BsonBinaryData(bytes, BsonBinarySubType.UuidLegacy, guidRepresentation)); break;
80+
case 2: exception = Record.Exception(() => new BsonBinaryData(guid, guidRepresentation)); break;
81+
}
82+
83+
var e = exception.Should().BeOfType<ArgumentException>().Subject;
84+
e.Message.Should().StartWith($"Invalid guidRepresentation: 5.");
85+
e.ParamName.Should().Be("guidRepresentation");
86+
}
87+
88+
[Theory]
89+
[InlineData(BsonBinarySubType.UuidLegacy, GuidRepresentation.Standard, "GuidRepresentation Standard is only valid with subType UuidStandard, not with subType UuidLegacy.")]
90+
[InlineData(BsonBinarySubType.UuidStandard, GuidRepresentation.CSharpLegacy, "GuidRepresentation CSharpLegacy is only valid with subType UuidLegacy, not with subType UuidStandard.")]
91+
[InlineData(BsonBinarySubType.UuidStandard, GuidRepresentation.JavaLegacy, "GuidRepresentation JavaLegacy is only valid with subType UuidLegacy, not with subType UuidStandard.")]
92+
[InlineData(BsonBinarySubType.UuidStandard, GuidRepresentation.PythonLegacy, "GuidRepresentation PythonLegacy is only valid with subType UuidLegacy, not with subType UuidStandard.")]
93+
[InlineData(BsonBinarySubType.UuidStandard, GuidRepresentation.Unspecified, "GuidRepresentation Unspecified is only valid with subType UuidLegacy, not with subType UuidStandard.")]
94+
public void constructor_should_throw_when_sub_type_is_uuid_and_guid_representation_is_invalid_with_sub_type(BsonBinarySubType subType, GuidRepresentation guidRepresentation, string expectedMessage)
95+
{
96+
var bytes = new byte[16];
97+
98+
var exception = Record.Exception(() => new BsonBinaryData(bytes, subType, guidRepresentation));
99+
100+
var e = exception.Should().BeOfType<ArgumentException>().Subject;
101+
e.Message.Should().StartWith(expectedMessage);
102+
e.ParamName.Should().Be("guidRepresentation");
103+
}
104+
105+
[Theory]
106+
[ParameterAttributeData]
107+
public void constructor_should_throw_when_sub_type_is_not_uuid_and_guid_representation_is_not_unspecified(
108+
[Values(BsonBinarySubType.Binary)] BsonBinarySubType subType,
109+
[Values(GuidRepresentation.CSharpLegacy, GuidRepresentation.Standard)] GuidRepresentation guidRepresentation)
110+
{
111+
var bytes = new byte[0];
112+
113+
var exception = Record.Exception(() => new BsonBinaryData(bytes, subType, guidRepresentation));
114+
115+
var e = exception.Should().BeOfType<ArgumentException>().Subject;
116+
e.Message.Should().StartWith($"GuidRepresentation must be Unspecified, not {guidRepresentation}, when subType is not UuidStandard or UuidLegacy.");
117+
e.ParamName.Should().Be("guidRepresentation");
118+
}
119+
25120
[Fact]
26121
public void TestCreateNull()
27122
{

0 commit comments

Comments
 (0)