kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 6d3e2a121092cab38b8b03042aea799cf0b55694ba73db4d47b4853edb26e62e |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-08-25 17:57 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-08-25 18:07 +02:00 |
| Parents | c6839ceb058a |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +6 −1
| @@ -193,10 +193,15 @@ | ||
| 193 | 193 | |
| 194 | 194 | // Utility method for collections |
| 195 | 195 | private static ByteBuffer pack(List<ByteBuffer> buffers, int elements, int size) { |
| 196 | + if (elements > 65535) | |
| 197 | + throw new IllegalArgumentException("Native protocol version 2 supports up to 65535 elements in any collection - but collection contains " + elements + " elements"); | |
| 196 | 198 | ByteBuffer result = ByteBuffer.allocate(2 + size); |
| 197 | 199 | result.putShort((short)elements); |
| 198 | 200 | for (ByteBuffer bb : buffers) { |
| 199 | - result.putShort((short)bb.remaining()); | |
| 201 | + int elemSize = bb.remaining(); | |
| 202 | + if (elemSize > 65535) | |
| 203 | + throw new IllegalArgumentException("Native protocol version 2 supports only elements with size up to 65535 bytes - but element size is " + elemSize + " bytes"); | |
| 204 | + result.putShort((short)elemSize); | |
| 200 | 205 | result.put(bb.duplicate()); |
| 201 | 206 | } |
| 202 | 207 | return (ByteBuffer)result.flip(); |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTest.java +22 −0
| @@ -15,6 +15,12 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.util.Collections; | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +import org.testng.collections.Lists; | |
| 22 | + | |
| 23 | +import com.google.common.base.Strings; | |
| 18 | 24 | import org.testng.Assert; |
| 19 | 25 | import org.testng.annotations.Test; |
| 20 | 26 | |
| @@ -47,4 +53,20 @@ | ||
| 47 | 53 | TypeCodec mapType = TypeCodec.mapOf(text(), CUSTOM_FOO); |
| 48 | 54 | Assert.assertNotNull(mapType); |
| 49 | 55 | } |
| 56 | + | |
| 57 | + @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }) | |
| 58 | + public void collectionTooLargeTest() throws Exception { | |
| 59 | + TypeCodec<List<Integer>> listType = TypeCodec.listOf(DataType.cint()); | |
| 60 | + List<Integer> list = Collections.nCopies(65536, 1); | |
| 61 | + | |
| 62 | + listType.serialize(list); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }) | |
| 66 | + public void collectionElementTooLargeTest() throws Exception { | |
| 67 | + TypeCodec<List<String>> listType = TypeCodec.listOf(DataType.text()); | |
| 68 | + List<String> list = Lists.newArrayList(Strings.repeat("a", 65536)); | |
| 69 | + | |
| 70 | + listType.serialize(list); | |
| 71 | + } | |
| 50 | 72 | } |