File tree Expand file tree Collapse file tree 2 files changed +11
-2
lines changed Expand file tree Collapse file tree 2 files changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,9 @@ class ObjectId
41
41
# the remaining bytes will consist of the standard machine id, pid, and counter. If
42
42
# you need a zeroed timestamp, used ObjectId.from_time.
43
43
def initialize ( data = nil , time = nil )
44
+ if data && ( !data . is_a? ( Array ) || data . size != 12 )
45
+ raise InvalidObjectId , 'ObjectId requires 12 byte array'
46
+ end
44
47
@data = data || generate ( time )
45
48
end
46
49
Original file line number Diff line number Diff line change @@ -26,12 +26,18 @@ def test_hashcode
26
26
end
27
27
28
28
def test_array_uniq_for_equilavent_ids
29
- a = ObjectId . new ( '123' )
30
- b = ObjectId . new ( '123' )
29
+ a = ObjectId . new ( '123456789101' . unpack ( 'C*' ) )
30
+ b = ObjectId . new ( '123456789101' . unpack ( 'C*' ) )
31
31
assert_equal a , b
32
32
assert_equal 1 , [ a , b ] . uniq . size
33
33
end
34
34
35
+ def test_initialization_with_bad_data
36
+ assert_raise InvalidObjectId do
37
+ ObjectId . new ( '\xff' )
38
+ end
39
+ end
40
+
35
41
def test_create_pk_method
36
42
doc = { :name => 'Mongo' }
37
43
doc = ObjectId . create_pk ( doc )
You can’t perform that action at this time.
0 commit comments