diff --git a/ext/cbson/cbson.c b/ext/cbson/cbson.c index c89868f..9791d7a 100644 --- a/ext/cbson/cbson.c +++ b/ext/cbson/cbson.c @@ -231,12 +231,12 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) { int i; if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') { buffer_free(buffer); - rb_raise(InvalidKeyName, "key must not start with '$'"); + rb_raise(InvalidKeyName, "%s - key must not start with '$'", RSTRING_PTR(key)); } for (i = 0; i < RSTRING_LEN(key); i++) { if (RSTRING_PTR(key)[i] == '.') { buffer_free(buffer); - rb_raise(InvalidKeyName, "key must not contain '.'"); + rb_raise(InvalidKeyName, "%s - key must not contain '.'", RSTRING_PTR(key)); } } } diff --git a/test/bson/bson_test.rb b/test/bson/bson_test.rb index 2b3a7a6..bf652d7 100644 --- a/test/bson/bson_test.rb +++ b/test/bson/bson_test.rb @@ -562,4 +562,47 @@ class BSONTest < Test::Unit::TestCase end end + def test_invalid_key_names + assert @encoder.serialize({"hello" => "world"}, true) + assert @encoder.serialize({"hello" => {"hello" => "world"}}, true) + + assert @encoder.serialize({"he$llo" => "world"}, true) + assert @encoder.serialize({"hello" => {"hell$o" => "world"}}, true) + + assert_raise BSON::InvalidDocument do + @encoder.serialize({"he\0llo" => "world"}, true) + end + + assert_raise_error BSON::InvalidKeyName, "$hello" do + @encoder.serialize({"$hello" => "world"}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello" => {"$hello" => "world"}}, true) + end + + assert_raise_error BSON::InvalidKeyName, ".hello" do + @encoder.serialize({".hello" => "world"}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello" => {".hello" => "world"}}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello." => "world"}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello" => {"hello." => "world"}}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hel.lo" => "world"}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello" => {"hel.lo" => "world"}}, true) + end + end end diff --git a/test/db_api_test.rb b/test/db_api_test.rb index ecb5fb5..d1688a3 100644 --- a/test/db_api_test.rb +++ b/test/db_api_test.rb @@ -621,43 +621,6 @@ class DBAPITest < Test::Unit::TestCase assert_equal("mike", @@coll.find_one()["hello"]) end - def test_invalid_key_names - @@coll.remove - - @@coll.insert({"hello" => "world"}) - @@coll.insert({"hello" => {"hello" => "world"}}) - - assert_raise BSON::InvalidKeyName do - @@coll.insert({"$hello" => "world"}) - end - - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello" => {"$hello" => "world"}}) - end - - @@coll.insert({"he$llo" => "world"}) - @@coll.insert({"hello" => {"hell$o" => "world"}}) - - assert_raise BSON::InvalidKeyName do - @@coll.insert({".hello" => "world"}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello" => {".hello" => "world"}}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello." => "world"}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello" => {"hello." => "world"}}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hel.lo" => "world"}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello" => {"hel.lo" => "world"}}) - end - end - def test_collection_names assert_raise TypeError do @@db.collection(5)