Skip to content

Commit 25e1ef3

Browse files
author
rstam
committed
CSHARP-670: ObjectId constructor and Pack method should throw ArgumentOutOfRangeException if machine or increment values are invalid.
1 parent 20c0c3b commit 25e1ef3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

MongoDB.Bson/ObjectModel/ObjectId.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public ObjectId(DateTime timestamp, int machine, short pid, int increment)
9595
/// <param name="increment">The increment.</param>
9696
public ObjectId(int timestamp, int machine, short pid, int increment)
9797
{
98+
if ((machine & 0xff000000) != 0)
99+
{
100+
throw new ArgumentOutOfRangeException("machine", "The machine value must be between 0 and 16777215 (it must fit in 3 bytes).");
101+
}
102+
if ((increment & 0xff000000) != 0)
103+
{
104+
throw new ArgumentOutOfRangeException("increment", "The increment value must be between 0 and 16777215 (it must fit in 3 bytes).");
105+
}
106+
98107
_timestamp = timestamp;
99108
_machine = machine;
100109
_pid = pid;
@@ -272,6 +281,15 @@ public static ObjectId GenerateNewId(int timestamp)
272281
/// <returns>A byte array.</returns>
273282
public static byte[] Pack(int timestamp, int machine, short pid, int increment)
274283
{
284+
if ((machine & 0xff000000) != 0)
285+
{
286+
throw new ArgumentOutOfRangeException("machine", "The machine value must be between 0 and 16777215 (it must fit in 3 bytes).");
287+
}
288+
if ((increment & 0xff000000) != 0)
289+
{
290+
throw new ArgumentOutOfRangeException("increment", "The increment value must be between 0 and 16777215 (it must fit in 3 bytes).");
291+
}
292+
275293
byte[] bytes = new byte[12];
276294
bytes[0] = (byte)(timestamp >> 24);
277295
bytes[1] = (byte)(timestamp >> 16);

MongoDB.BsonUnitTests/ObjectModel/ObjectIdTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ public void TestIntIntShortIntConstructor()
5757
Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray()));
5858
}
5959

60+
[Test]
61+
public void TestIntIntShortIntConstructorWithInvalidIncrement()
62+
{
63+
var objectId = new ObjectId(0, 0, 0, 0x00ffffff);
64+
Assert.AreEqual(0x00ffffff, objectId.Increment);
65+
Assert.Throws<ArgumentOutOfRangeException>(() => { var invalidId = new ObjectId(0, 0, 0, 0x01000000); });
66+
}
67+
68+
[Test]
69+
public void TestIntIntShortIntConstructorWithInvalidMachine()
70+
{
71+
var objectId = new ObjectId(0, 0x00ffffff, 0, 0);
72+
Assert.AreEqual(0x00ffffff, objectId.Machine);
73+
Assert.Throws<ArgumentOutOfRangeException>(() => { var invalidId = new ObjectId(0, 0x01000000, 0, 0); });
74+
}
75+
76+
[Test]
77+
public void TestPackWithInvalidIncrement()
78+
{
79+
var objectId = new ObjectId(ObjectId.Pack(0, 0, 0, 0x00ffffff));
80+
Assert.AreEqual(0x00ffffff, objectId.Increment);
81+
Assert.Throws<ArgumentOutOfRangeException>(() => { var invalidId = new ObjectId(ObjectId.Pack(0, 0, 0, 0x01000000)); });
82+
}
83+
84+
[Test]
85+
public void TestPackWithInvalidMachine()
86+
{
87+
var objectId = new ObjectId(ObjectId.Pack(0, 0x00ffffff, 0, 0));
88+
Assert.AreEqual(0x00ffffff, objectId.Machine);
89+
Assert.Throws<ArgumentOutOfRangeException>(() => { var invalidId = new ObjectId(ObjectId.Pack(0, 0x01000000, 0, 0)); });
90+
}
91+
6092
[Test]
6193
public void TestDateTimeConstructor()
6294
{

0 commit comments

Comments
 (0)