Skip to content

Commit

Permalink
Check for size of item on subdoc (fixes #42)
Browse files Browse the repository at this point in the history
Change-Id: Ibfbe88979929079b7f479e5f76f9db327191b03f
Reviewed-on: http://review.couchbase.org/102419
Reviewed-by: Michael Nitschinger <[email protected]>
Tested-by: Sergey Avseyev <[email protected]>
  • Loading branch information
avsej committed Dec 4, 2018
1 parent 65d4f07 commit 8fc46dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.couchbase.mock.memcached;

import com.couchbase.mock.Info;
import com.couchbase.mock.memcached.protocol.BinaryCommand;
import com.couchbase.mock.memcached.protocol.BinaryResponse;
import com.couchbase.mock.memcached.protocol.BinarySubdocCommand;
Expand Down Expand Up @@ -191,6 +192,9 @@ public BinaryResponse execute(BinaryCommand cmd, MemcachedServer server, Memcach
xattr = existing.getXattr();
body = rci.getNewDocString().getBytes();
}
if (body.length > Info.itemSizeMax()) {
return new BinaryResponse(cmd, ErrorCode.E2BIG);
}
Item newItm = new Item(
existing.getKeySpec(), existing.getFlags(), subdocInput.getExpiryTime(),
body, xattr, subdocInput.getCas(), Datatype.RAW.value());
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/couchbase/mock/clientv2/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.StringDocument;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import com.couchbase.client.java.error.CASMismatchException;
import com.couchbase.client.java.error.RequestTooBigException;
import com.couchbase.mock.Bucket;
import com.couchbase.mock.BucketConfiguration;
import com.couchbase.mock.CouchbaseMock;
import com.couchbase.mock.Info;
import com.couchbase.mock.client.MockClient;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -180,4 +184,21 @@ public void testFailWithInvalidCASOnPrepend() {
StringDocument stored = bucket.upsert(StringDocument.create("prependCasMismatch", "foo"));
bucket.prepend(StringDocument.from(stored, stored.cas() + 1));
}

@Test(expected = RequestTooBigException.class)
public void testSubdocTooBigException() {
String testSubKey = "tooBigBodyForSubdoc";
char[] chars = new char[Info.itemSizeMax() - 15];
Arrays.fill(chars, 'x');
try {
bucket.upsert(JsonDocument.create(testSubKey,
JsonObject.create().put("value",
JsonArray.create().add(new String(chars)))));
} catch (RequestTooBigException ex) {
throw new RuntimeException("not expected TooBig here", ex);
}
bucket.mutateIn(testSubKey)
.arrayAppend("value", "123456789012345")
.execute();
}
}

0 comments on commit 8fc46dc

Please sign in to comment.