Skip to content

Commit

Permalink
Validating the key format before we perform the operation
Browse files Browse the repository at this point in the history
  • Loading branch information
smadappa committed May 23, 2016
1 parent f4a1c11 commit 34887c5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
12 changes: 10 additions & 2 deletions evcache-client/src/main/java/com/netflix/evcache/EVCacheImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import com.netflix.spectator.api.DistributionSummary;

import net.spy.memcached.CachedData;
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
import net.spy.memcached.transcoders.Transcoder;
import net.spy.memcached.util.StringUtils;
import rx.Observable;
import rx.Scheduler;
import rx.Single;
Expand Down Expand Up @@ -102,8 +104,14 @@ final public class EVCacheImpl implements EVCache {
}

private String getCanonicalizedKey(String key) {
if (this._cacheName == null) return key;
return _cacheName + ':' + key;
final String cKey;
if (this._cacheName == null) {
cKey = key;
} else {
cKey = _cacheName + ':' + key;
}
StringUtils.validateKey(cKey, false);
return cKey;
}

private String getKey(String canonicalizedKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import java.util.concurrent.CountDownLatch;

import net.spy.memcached.ops.Operation;
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
import net.spy.memcached.protocol.binary.EVCacheNodeImpl;
import net.spy.memcached.util.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -75,6 +77,13 @@ public void addOperations(Map<MemcachedNode, Operation> ops) {
}
}

@Override
public void enqueueOperation(final String key, final Operation o) {
checkState();
addOperation(key, o);
}


@Override
public CountDownLatch broadcastOperation(BroadcastOpFactory of, Collection<MemcachedNode> nodes) {
for (MemcachedNode node : nodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,44 @@ public void testEVCache() {
this.evCache = getNewBuilder().setAppName("EVCACHE").setCachePrefix("cid").enableRetry().build();
assertNotNull(evCache);
}

@Test(dependsOnMethods = { "testEVCache" })
public void testKeySizeCheck() throws Exception {
final String key = "This is an invalid key";
boolean exceptionThrown = false;
for (int i = 0; i < loops; i++) {
try {
if (log.isDebugEnabled()) log.debug("Check key : " + key );
evCache.<String>get(key);
} catch(Exception e) {
exceptionThrown = true;
if (log.isDebugEnabled()) log.debug("Check key : " + key + ": INVALID");
}
assertTrue(exceptionThrown);
}

final String longKey = "This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.This_is_an_a_very_long_key.";
exceptionThrown = false;
for (int i = 0; i < loops; i++) {
try {
if (log.isDebugEnabled()) log.debug("Check key length : " + longKey );
evCache.<String>get(longKey);
} catch(Exception e) {
exceptionThrown = true;
if (log.isDebugEnabled()) log.debug("Check key length: " + longKey + ": INVALID");
}
assertTrue(exceptionThrown);
}

}

@Test(dependsOnMethods = { "testKeySizeCheck" })
public void testDelete() throws Exception {
for (int i = 0; i < loops; i++) {
delete(i, evCache);
}
}

@Test(dependsOnMethods = { "testDelete" })
public void testAdd() throws Exception {
for (int i = 0; i < loops; i++) {
Expand Down

0 comments on commit 34887c5

Please sign in to comment.