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]
*/
public static void isLegalNamespaceName(byte[] namespaceName, int offset, int length) {
for (int i = offset; i < length; i++) {
public static void isLegalNamespaceName(final byte[] namespaceName,
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] == '_') {
continue;
}
throw new IllegalArgumentException("Illegal character <" + namespaceName[i] +
"> at " + i + ". Namespaces can only contain " +
"'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() {

View File

@ -23,7 +23,6 @@ import java.util.Map;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
@ -54,7 +53,7 @@ public class TestTableName extends TestWatcher {
return tableName;
}
String emptyTableNames[] ={"", " "};
String emptyNames[] ={"", " "};
String invalidNamespace[] = {":a", "%:a"};
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"
@ -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)
public void testEmptyTableName() {
for (String tn : emptyTableNames) {
for (String tn : emptyNames) {
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
fail("invalid tablename " + tn + " should have failed with IllegalArgumentException");
}