|
| 1 | +/* Copyright 2015 MongoDB Inc. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "mongo/client/insert_write_operation.h" |
| 17 | + |
| 18 | +#include "mongo/bson/bsonobj.h" |
| 19 | +#include "mongo/bson/bsonobjbuilder.h" |
| 20 | +#include "mongo/util/assert_util.h" |
| 21 | + |
| 22 | +#include "mongo/unittest/unittest.h" |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | + using namespace mongo; |
| 27 | + |
| 28 | + TEST(InsertWriteOperation, IdFieldCannotContainDollarSignTopLevel) { |
| 29 | + BSONObjBuilder bob; |
| 30 | + bob.append("foo", "bar"); |
| 31 | + BSONObjBuilder subbob(bob.subobjStart("_id")); |
| 32 | + subbob.append("$bad", "nogood"); |
| 33 | + subbob.done(); |
| 34 | + ASSERT_THROWS(InsertWriteOperation w(bob.done()), UserException); |
| 35 | + } |
| 36 | + |
| 37 | + TEST(InsertWriteOperation, IdFieldCannotContainDollarSignNested) { |
| 38 | + BSONObjBuilder bob; |
| 39 | + bob.append("garply", "nnnnoooo"); |
| 40 | + BSONObjBuilder subbob(bob.subobjStart("_id")); |
| 41 | + BSONObjBuilder subsubbob(subbob.subobjStart("foo")); |
| 42 | + BSONObjBuilder subsubsubbob(subbob.subobjStart("baz")); |
| 43 | + subsubsubbob.append("$blah", "borked"); |
| 44 | + subsubsubbob.done(); |
| 45 | + subsubbob.done(); |
| 46 | + subbob.done(); |
| 47 | + ASSERT_THROWS(InsertWriteOperation w(bob.done()), UserException); |
| 48 | + } |
| 49 | + |
| 50 | +} // namespace |
0 commit comments