Skip to content

Commit

Permalink
Update operations should check for empty documents
Browse files Browse the repository at this point in the history
  • Loading branch information
rozza committed May 5, 2015
1 parent 49654cf commit 7b5f965
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ protected UpdateCommandMessage writeTheWrites(final BsonOutput bsonOutput, final
writer.writeName("q");
getCodec(update.getFilter()).encode(writer, update.getFilter(), EncoderContext.builder().build());
writer.writeName("u");

int bufferPosition = bsonOutput.getPosition();
getCodec(update.getUpdate()).encode(writer, update.getUpdate(), EncoderContext.builder().build());

if (update.getType() == WriteRequest.Type.UPDATE && bsonOutput.getPosition() == bufferPosition + 8) {
throw new IllegalArgumentException("Invalid BSON document for an update");
}

if (update.isMulti()) {
writer.writeBoolean("multi", update.isMulti());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ protected RequestMessage encodeMessageBody(final BsonOutput bsonOutput, final in
if (updateRequest.getType() == REPLACE) {
addCollectibleDocument(updateRequest.getUpdate(), bsonOutput, new CollectibleDocumentFieldNameValidator());
} else {
int bufferPosition = bsonOutput.getPosition();
addDocument(updateRequest.getUpdate(), bsonOutput, new UpdateFieldNameValidator());
if (bsonOutput.getPosition() == bufferPosition + 5) {
throw new IllegalArgumentException("Invalid BSON document for an update");
}
}

if (updates.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,44 @@ class MixedBulkWriteOperationAsyncSpecification extends OperationFunctionalSpeci
ordered << [true, false]
}


def 'when updating with an empty document, update should throw IllegalArgumentException'() {
given:
def id = new ObjectId()
def op = new MixedBulkWriteOperation(getNamespace(),
[new UpdateRequest(new BsonDocument('_id', new BsonObjectId(id)), new BsonDocument(), UPDATE)],
true, ACKNOWLEDGED)

when:
executeAsync(op)

then:
def ex = thrown(MongoException)
ex.getCause() instanceof IllegalArgumentException

where:
ordered << [true, false]
}

def 'when updating with an invalid document, update should throw IllegalArgumentException'() {
given:
def id = new ObjectId()
def op = new MixedBulkWriteOperation(getNamespace(),
[new UpdateRequest(new BsonDocument('_id', new BsonObjectId(id)), new BsonDocument('a', new BsonInt32(1)), UPDATE)],
true, ACKNOWLEDGED)

when:
executeAsync(op)

then:
def ex = thrown(MongoException)
ex.getCause() instanceof IllegalArgumentException

where:
ordered << [true, false]
}


def 'when a document contains a key with an illegal character, replacing a document with it should throw IllegalArgumentException'() {
given:
def id = new ObjectId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package com.mongodb.operation

import category.Slow
import com.mongodb.ClusterFixture
import com.mongodb.MongoBulkWriteException
import com.mongodb.OperationFunctionalSpecification
import com.mongodb.ReadPreference
import com.mongodb.WriteConcern
import com.mongodb.binding.SingleConnectionBinding
import com.mongodb.MongoBulkWriteException
import com.mongodb.bulk.BulkWriteResult
import com.mongodb.bulk.BulkWriteUpsert
import com.mongodb.bulk.DeleteRequest
Expand Down Expand Up @@ -252,6 +252,40 @@ class MixedBulkWriteOperationSpecification extends OperationFunctionalSpecificat
ordered << [true, false]
}

def 'when updating with an empty document, update should throw IllegalArgumentException'() {
given:
def id = new ObjectId()
def op = new MixedBulkWriteOperation(getNamespace(),
[new UpdateRequest(new BsonDocument('_id', new BsonObjectId(id)), new BsonDocument(), UPDATE)],
true, ACKNOWLEDGED)

when:
op.execute(getBinding())

then:
thrown(IllegalArgumentException)

where:
ordered << [true, false]
}

def 'when updating with an invalid document, update should throw IllegalArgumentException'() {
given:
def id = new ObjectId()
def op = new MixedBulkWriteOperation(getNamespace(),
[new UpdateRequest(new BsonDocument('_id', new BsonObjectId(id)), new BsonDocument('a', new BsonInt32(1)), UPDATE)],
true, ACKNOWLEDGED)

when:
op.execute(getBinding())

then:
thrown(IllegalArgumentException)

where:
ordered << [true, false]
}

def 'when a document contains a key with an illegal character, replacing a document with it should throw IllegalArgumentException'() {
given:
def id = new ObjectId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,19 @@ class UpdateOperationSpecification extends OperationFunctionalSpecification {
ordered << [true, false]
}


def 'when an update document is empty, update should throw IllegalArgumentException'() {
when:
new UpdateOperation(getNamespace(), ordered, ACKNOWLEDGED,
[new UpdateRequest(new BsonDocument(),
new BsonDocument(), WriteRequest.Type.UPDATE)])
.execute(getBinding())

then:
thrown(IllegalArgumentException)

where:
ordered << [true, false]
}

}

0 comments on commit 7b5f965

Please sign in to comment.