add unit test for Filesystem BlobKey and ContainerName validator

This commit is contained in:
jixinchi 2024-04-15 11:27:51 +08:00 committed by Andrew Gaul
parent 03aeccffdf
commit 6670c556d7
3 changed files with 20 additions and 1 deletions

View File

@ -20,6 +20,9 @@ import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import com.google.inject.Singleton;
import java.io.File;
import java.util.Arrays;
/**
* Validates name for filesystem container blob keys implementation
*
@ -38,7 +41,7 @@ public class FilesystemBlobKeyValidatorImpl extends FilesystemBlobKeyValidator {
//blobkey cannot start with / (or \ in Windows) character
if (name.startsWith("\\") || name.startsWith("/"))
throw new IllegalArgumentException("Blob key '" + name + "' cannot start with \\ or /");
if (name.contains("../"))
if (Arrays.asList(name.split(File.separator.equals("\\") ? "\\\\" : File.separator)).contains(".."))
throw new IllegalArgumentException("Blob key '" + name + "' cannot contain ../");
}

View File

@ -36,6 +36,7 @@ public class FilesystemBlobKeyValidatorTest {
validator.validate("all.img");
validator.validate("all" + File.separator + "is" + File.separator + "" + "ok");
validator.validate("all" + File.separator + "is" + File.separator + ".." + "ok");
}
@Test
@ -51,6 +52,11 @@ public class FilesystemBlobKeyValidatorTest {
validator.validate(File.separator + "is" + File.separator + "" + "ok");
fail("Blob key value incorrect, but was not recognized");
} catch (IllegalArgumentException e) {}
try {
validator.validate("all" + File.separator + ".." + File.separator + "ok");
fail("Blob key value incorrect, but was not recognized");
} catch (IllegalArgumentException e) {}
}

View File

@ -60,6 +60,16 @@ public class FilesystemContainerNameValidatorTest {
validator.validate("all" + File.separator + "is" + File.separator);
fail("Container name value incorrect, but was not recognized");
} catch (IllegalArgumentException e) {}
try {
validator.validate(".");
fail("Container name value incorrect, but was not recognized");
} catch (IllegalArgumentException e) {}
try {
validator.validate("..");
fail("Container name value incorrect, but was not recognized");
} catch (IllegalArgumentException e) {}
}