Skip to content

Commit 4072cf4

Browse files
apage43Michael Nitschinger
authored andcommitted
SPY-63 Keys can be anything over binary protocol
Change-Id: I8fcd92bac797f7bb610fd6bc42a8b78d27700785 Reviewed-on: http://review.couchbase.org/21323 Reviewed-by: Matt Ingenthron <[email protected]> Tested-by: Matt Ingenthron <[email protected]> Reviewed-by: Michael Nitschinger <[email protected]>
1 parent 4f8f0df commit 4072cf4

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

src/main/java/net/spy/memcached/MemcachedClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import net.spy.memcached.ops.StoreOperation;
7070
import net.spy.memcached.ops.StoreType;
7171
import net.spy.memcached.ops.TimedOutOperationStatus;
72+
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
7273
import net.spy.memcached.transcoders.TranscodeService;
7374
import net.spy.memcached.transcoders.Transcoder;
7475
import net.spy.memcached.util.StringUtils;
@@ -1061,7 +1062,7 @@ public <T> BulkFuture<Map<String, T>> asyncGetBulk(Iterator<String> keyIter,
10611062
while (keyIter.hasNext() && tcIter.hasNext()) {
10621063
String key = keyIter.next();
10631064
tcMap.put(key, tcIter.next());
1064-
StringUtils.validateKey(key);
1065+
StringUtils.validateKey(key, opFact instanceof BinaryOperationFactory);
10651066
final MemcachedNode primaryNode = locator.getPrimary(key);
10661067
MemcachedNode node = null;
10671068
if (primaryNode.isActive()) {

src/main/java/net/spy/memcached/MemcachedConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import net.spy.memcached.ops.OperationState;
5757
import net.spy.memcached.ops.TapOperation;
5858
import net.spy.memcached.ops.VBucketAware;
59+
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
5960
import net.spy.memcached.protocol.binary.TapAckOperationImpl;
6061
import net.spy.memcached.util.StringUtils;
6162

@@ -636,7 +637,7 @@ public NodeLocator getLocator() {
636637
}
637638

638639
public void enqueueOperation(String key, Operation o) {
639-
StringUtils.validateKey(key);
640+
StringUtils.validateKey(key, opFact instanceof BinaryOperationFactory);
640641
checkState();
641642
addOperation(key, o);
642643
}

src/main/java/net/spy/memcached/util/StringUtils.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static boolean isJsonObject(String s) {
6363
}
6464
}
6565

66-
public static void validateKey(String key) {
66+
public static void validateKey(String key, boolean binary) {
6767
byte[] keyBytes = KeyUtil.getKeyBytes(key);
6868
if (keyBytes.length > MemcachedClientIF.MAX_KEY_LENGTH) {
6969
throw new IllegalArgumentException("Key is too long (maxlen = "
@@ -73,11 +73,13 @@ public static void validateKey(String key) {
7373
throw new IllegalArgumentException(
7474
"Key must contain at least one character.");
7575
}
76-
// Validate the key
77-
for (byte b : keyBytes) {
78-
if (b == ' ' || b == '\n' || b == '\r' || b == 0) {
79-
throw new IllegalArgumentException(
80-
"Key contains invalid characters: ``" + key + "''");
76+
if(!binary) {
77+
// Validate the key
78+
for (byte b : keyBytes) {
79+
if (b == ' ' || b == '\n' || b == '\r' || b == 0) {
80+
throw new IllegalArgumentException(
81+
"Key contains invalid characters: ``" + key + "''");
82+
}
8183
}
8284
}
8385
}

src/test/java/net/spy/memcached/BinaryClientTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package net.spy.memcached;
2525

2626
import java.net.InetSocketAddress;
27+
import java.util.Map;
2728
import java.util.concurrent.ExecutionException;
2829
import net.spy.memcached.internal.OperationFuture;
2930

@@ -103,6 +104,42 @@ public void testAsyncCASResponse() {
103104
assertNotNull(casRes.getCas());
104105
}
105106

107+
@Override
108+
public void testKeyWithSpaces() throws Exception {
109+
String key = "key with spaces";
110+
client.set(key, 0, "");
111+
assertNotNull("Couldn't get the key with spaces in it.", client.get(key));
112+
}
113+
114+
@Override
115+
public void testKeyWithNewline() throws Exception {
116+
String key = "Key\n";
117+
client.set(key, 0, "");
118+
assertNotNull(client.get(key));
119+
}
120+
121+
@Override
122+
public void testKeyWithReturn() throws Exception {
123+
String key = "Key\r";
124+
client.set(key, 0, "");
125+
assertNotNull(client.get(key));
126+
}
127+
128+
@Override
129+
public void testKeyWithASCIINull() throws Exception {
130+
String key = "Key\0";
131+
client.set(key, 0, "");
132+
assertNotNull(client.get(key));
133+
}
134+
135+
@Override
136+
public void testGetBulkKeyWSpaces() throws Exception {
137+
String key = "Key key2";
138+
client.set(key, 0, "");
139+
Map<String, Object> bulkReturn = client.getBulk(key);
140+
assertTrue(bulkReturn.size() >= 1);
141+
}
142+
106143
@Override
107144
protected void syncGetTimeoutsInitClient() throws Exception {
108145
initClient(new BinaryConnectionFactory() {

src/test/java/net/spy/memcached/ProtocolBaseCase.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void testExtendedUTF8Key() throws Exception {
178178
assertEquals("test1value", client.get(key));
179179
}
180180

181-
public void testInvalidKey1() throws Exception {
181+
public void testKeyWithSpaces() throws Exception {
182182
try {
183183
client.get("key with spaces");
184184
fail("Expected IllegalArgumentException getting key with spaces");
@@ -187,7 +187,7 @@ public void testInvalidKey1() throws Exception {
187187
}
188188
}
189189

190-
public void testInvalidKey2() throws Exception {
190+
public void testKeyLongerThan250() throws Exception {
191191
try {
192192
StringBuilder longKey = new StringBuilder();
193193
for (int i = 0; i < 251; i++) {
@@ -200,7 +200,7 @@ public void testInvalidKey2() throws Exception {
200200
}
201201
}
202202

203-
public void testInvalidKey3() throws Exception {
203+
public void testKeyWithNewline() throws Exception {
204204
try {
205205
Object val = client.get("Key\n");
206206
fail("Expected IllegalArgumentException, got " + val);
@@ -209,7 +209,7 @@ public void testInvalidKey3() throws Exception {
209209
}
210210
}
211211

212-
public void testInvalidKey4() throws Exception {
212+
public void testKeyWithReturn() throws Exception {
213213
try {
214214
Object val = client.get("Key\r");
215215
fail("Expected IllegalArgumentException, got " + val);
@@ -218,7 +218,7 @@ public void testInvalidKey4() throws Exception {
218218
}
219219
}
220220

221-
public void testInvalidKey5() throws Exception {
221+
public void testKeyWithASCIINull() throws Exception {
222222
try {
223223
Object val = client.get("Key\0");
224224
fail("Expected IllegalArgumentException, got " + val);
@@ -236,7 +236,7 @@ public void testInvalidKeyBlank() throws Exception {
236236
}
237237
}
238238

239-
public void testInvalidKeyBulk() throws Exception {
239+
public void testGetBulkKeyWSpaces() throws Exception {
240240
try {
241241
Object val = client.getBulk("Key key2");
242242
fail("Expected IllegalArgumentException, got " + val);

0 commit comments

Comments
 (0)