From 6670c556d7637d2d3301ca3cbe8ba60fdd08d67c Mon Sep 17 00:00:00 2001 From: jixinchi Date: Mon, 15 Apr 2024 11:27:51 +0800 Subject: [PATCH] add unit test for Filesystem BlobKey and ContainerName validator --- .../internal/FilesystemBlobKeyValidatorImpl.java | 5 ++++- .../internal/FilesystemBlobKeyValidatorTest.java | 6 ++++++ .../internal/FilesystemContainerNameValidatorTest.java | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemBlobKeyValidatorImpl.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemBlobKeyValidatorImpl.java index 563968edcc1..f53cb110219 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemBlobKeyValidatorImpl.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemBlobKeyValidatorImpl.java @@ -20,6 +20,9 @@ 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 void validate(String name) throws IllegalArgumentException { //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 ../"); } diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemBlobKeyValidatorTest.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemBlobKeyValidatorTest.java index b06254f5426..e3e1436edb9 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemBlobKeyValidatorTest.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemBlobKeyValidatorTest.java @@ -36,6 +36,7 @@ public void testNamesValidity() { 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 void testInvalidNames() { 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) {} } diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemContainerNameValidatorTest.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemContainerNameValidatorTest.java index c8bdd61de6e..668dc6870c8 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemContainerNameValidatorTest.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/predicates/validators/internal/FilesystemContainerNameValidatorTest.java @@ -60,6 +60,16 @@ public void testInvalidNames() { 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) {} }