add collection name validation

This commit is contained in:
Mike Dirolf 2009-08-04 14:24:18 -04:00
parent 91d6ab4caa
commit fe7d7745c7
2 changed files with 39 additions and 0 deletions

View File

@ -26,6 +26,24 @@ module XGen
attr_reader :db, :name, :hint attr_reader :db, :name, :hint
def initialize(db, name) def initialize(db, name)
case name
when Symbol, String
else
raise RuntimeError, "new_name must be a string or symbol"
end
name = name.to_s
if name.empty? or name.include? ".."
raise RuntimeError, "collection names cannot be empty"
end
if name.include? "$" and not name.match(/^\$cmd/)
raise RuntimeError, "collection names must not contain '$'"
end
if name.match(/^\./) or name.match(/\.$/)
raise RuntimeError, "collection names must not start or end with '.'"
end
@db, @name = db, name @db, @name = db, name
@hint = nil @hint = nil
end end

View File

@ -713,6 +713,27 @@ class DBAPITest < Test::Unit::TestCase
@@coll.modify({"hello" => "world"}, {"$inc" => "hello"}) @@coll.modify({"hello" => "world"}, {"$inc" => "hello"})
end end
def test_collection_names
assert_raise RuntimeError do
@@db.collection(5)
end
assert_raise RuntimeError do
@@db.collection("")
end
assert_raise RuntimeError do
@@db.collection("te$t")
end
assert_raise RuntimeError do
@@db.collection(".test")
end
assert_raise RuntimeError do
@@db.collection("test.")
end
assert_raise RuntimeError do
@@db.collection("tes..t")
end
end
def test_rename_collection def test_rename_collection
@@db.drop_collection("foo") @@db.drop_collection("foo")
@@db.drop_collection("bar") @@db.drop_collection("bar")