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
4141 # the remaining bytes will consist of the standard machine id, pid, and counter. If
4242 # you need a zeroed timestamp, used ObjectId.from_time.
4343 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
4447 @data = data || generate ( time )
4548 end
4649
Original file line number Diff line number Diff line change @@ -26,12 +26,18 @@ def test_hashcode
2626 end
2727
2828 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*' ) )
3131 assert_equal a , b
3232 assert_equal 1 , [ a , b ] . uniq . size
3333 end
3434
35+ def test_initialization_with_bad_data
36+ assert_raise InvalidObjectId do
37+ ObjectId . new ( '\xff' )
38+ end
39+ end
40+
3541 def test_create_pk_method
3642 doc = { :name => 'Mongo' }
3743 doc = ObjectId . create_pk ( doc )
You can’t perform that action at this time.
0 commit comments