HBASE-13050 Empty Namespace validation

Signed-off-by: Matteo Bertozzi <matteo.bertozzi@cloudera.com>
This commit is contained in:
Abhishek Kumar 2015-02-17 18:09:27 +05:30 committed by Matteo Bertozzi
parent f64d63d24e
commit 7b045d143a
2 changed files with 19 additions and 12 deletions

View File

@ -208,21 +208,21 @@ public final class TableName implements Comparable<TableName> {
/** /**
* Valid namespace characters are [a-zA-Z_0-9] * Valid namespace characters are [a-zA-Z_0-9]
*/ */
public static void isLegalNamespaceName(byte[] namespaceName, int offset, int length) { public static void isLegalNamespaceName(final byte[] namespaceName,
for (int i = offset; i < length; i++) { final int start,
final int end) {
if(end - start < 1) {
throw new IllegalArgumentException("Namespace name must not be empty");
}
for (int i = start; i < end; i++) {
if (Character.isLetterOrDigit(namespaceName[i])|| namespaceName[i] == '_') { if (Character.isLetterOrDigit(namespaceName[i])|| namespaceName[i] == '_') {
continue; continue;
} }
throw new IllegalArgumentException("Illegal character <" + namespaceName[i] + throw new IllegalArgumentException("Illegal character <" + namespaceName[i] +
"> at " + i + ". Namespaces can only contain " + "> at " + i + ". Namespaces can only contain " +
"'alphanumeric characters': i.e. [a-zA-Z_0-9]: " + Bytes.toString(namespaceName, "'alphanumeric characters': i.e. [a-zA-Z_0-9]: " + Bytes.toString(namespaceName,
offset, length)); start, end));
} }
if(offset == length)
throw new IllegalArgumentException("Illegal character <" + namespaceName[offset] +
"> at " + offset + ". Namespaces can only contain " +
"'alphanumeric characters': i.e. [a-zA-Z_0-9]: " + Bytes.toString(namespaceName,
offset, length));
} }
public byte[] getName() { public byte[] getName() {

View File

@ -23,7 +23,6 @@ import java.util.Map;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -53,8 +52,8 @@ public class TestTableName extends TestWatcher {
public TableName getTableName() { public TableName getTableName() {
return tableName; return tableName;
} }
String emptyTableNames[] ={"", " "}; String emptyNames[] ={"", " "};
String invalidNamespace[] = {":a", "%:a"}; String invalidNamespace[] = {":a", "%:a"};
String legalTableNames[] = { "foo", "with-dash_under.dot", "_under_start_ok", String legalTableNames[] = { "foo", "with-dash_under.dot", "_under_start_ok",
"with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02" "with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02"
@ -73,9 +72,17 @@ public class TestTableName extends TestWatcher {
} }
} }
@Test(expected = IllegalArgumentException.class)
public void testEmptyNamespaceName() {
for (String nn : emptyNames) {
TableName.isLegalNamespaceName(Bytes.toBytes(nn));
fail("invalid Namespace name " + nn + " should have failed with IllegalArgumentException");
}
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testEmptyTableName() { public void testEmptyTableName() {
for (String tn : emptyTableNames) { for (String tn : emptyNames) {
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn)); TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
fail("invalid tablename " + tn + " should have failed with IllegalArgumentException"); fail("invalid tablename " + tn + " should have failed with IllegalArgumentException");
} }