From f2260f107bd2d71aed96e1199edfa5186b8b308f Mon Sep 17 00:00:00 2001 From: Jonathan Ellis Date: Fri, 8 May 2009 14:53:25 +0000 Subject: [PATCH] add more robust sanity checking of get_column arguments. patch by jbellis; reviewed by Jun Rao for CASSANDRA-151 git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@773016 13f79535-47bb-0310-9956-ffa450edef68 --- .../cassandra/service/CassandraServer.java | 22 ++++++++++++++++--- test/system/test_server.py | 19 +++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/java/org/apache/cassandra/service/CassandraServer.java b/src/java/org/apache/cassandra/service/CassandraServer.java index bfe147e3a169..8f874fe23013 100644 --- a/src/java/org/apache/cassandra/service/CassandraServer.java +++ b/src/java/org/apache/cassandra/service/CassandraServer.java @@ -212,11 +212,27 @@ public column_t get_column(String tablename, String key, String columnFamily_col { logger.debug("get_column"); String[] values = RowMutation.getColumnAndColumnFamily(columnFamily_column); - if (values.length < 2) + if (values.length < 1) { - throw new InvalidRequestException("get_column requires both parts of columnfamily:column"); + throw new InvalidRequestException("get_column requires non-empty columnfamily"); } - ColumnFamily cfamily = readColumnFamily(new ColumnReadCommand(tablename, key, columnFamily_column)); + if (DatabaseDescriptor.getColumnFamilyType(values[0]).equals("Standard")) + { + if (values.length != 2) + { + throw new InvalidRequestException("get_column requires both parts of columnfamily:column for standard CF " + values[0]); + } + } + else + { + if (values.length != 3) + { + throw new InvalidRequestException("get_column requires all parts of columnfamily:supercolumn:subcolumn for super CF " + values[0]); + } + } + + ColumnReadCommand readCommand = new ColumnReadCommand(tablename, key, columnFamily_column); + ColumnFamily cfamily = readColumnFamily(readCommand); if (cfamily == null) { throw new NotFoundException(); diff --git a/test/system/test_server.py b/test/system/test_server.py index 90e9f3b2aabc..c7579b204a24 100644 --- a/test/system/test_server.py +++ b/test/system/test_server.py @@ -3,7 +3,7 @@ from . import client, root, CassandraTester from thrift.Thrift import TApplicationException -from ttypes import batch_mutation_t, batch_mutation_super_t, superColumn_t, column_t, NotFoundException +from ttypes import batch_mutation_t, batch_mutation_super_t, superColumn_t, column_t, NotFoundException, InvalidRequestException _SIMPLE_COLUMNS = [column_t(columnName='c1', value='value1', timestamp=0), column_t(columnName='c2', value='value2', timestamp=0)] @@ -45,13 +45,15 @@ def _verify_super(supercolumn='Super1'): slice = client.get_slice_super('Table1', 'key1', 'Super1', -1, -1) assert slice == _SUPER_COLUMNS, slice -def _expect_missing(fn): +def _expect_exception(fn, type_): try: r = fn() - except NotFoundException: + except type_: pass else: - raise Exception('expected missing result; got %s' % r) + raise Exception('expected %s; got %s' % (type_.__name__, r)) +def _expect_missing(fn): + _expect_exception(fn, NotFoundException) class TestMutations(CassandraTester): @@ -62,7 +64,7 @@ def test_empty_slice_super(self): assert client.get_slice('Table1', 'key1', 'Super1', -1, -1) == [] def test_missing_super(self): - _expect_missing(lambda: client.get_column('Table1', 'key1', 'Super1:sc1')) + _expect_missing(lambda: client.get_column('Table1', 'key1', 'Super1:sc1:c1')) def test_count(self): assert client.get_column_count('Table1', 'key1', 'Standard2') == 0 @@ -89,6 +91,13 @@ def test_batch_insert_blocking(self): _insert_batch(True) _verify_batch() + def test_bad_gets(self): + _expect_exception(lambda: client.get_column('Table1', 'key1', 'Standard1'), InvalidRequestException) + _expect_exception(lambda: client.get_column('Table1', 'key1', 'Standard1:x:y'), InvalidRequestException) + _expect_exception(lambda: client.get_column('Table1', 'key1', 'Super1'), InvalidRequestException) + _expect_exception(lambda: client.get_column('Table1', 'key1', 'Super1:x'), InvalidRequestException) + _expect_exception(lambda: client.get_column('Table1', 'key1', 'Super1:x:y:z'), InvalidRequestException) + def test_batch_insert_super(self): cfmap = {'Super1': _SUPER_COLUMNS, 'Super2': _SUPER_COLUMNS}