Skip to content

Commit

Permalink
add unit test for Filesystem BlobKey and ContainerName validator
Browse files Browse the repository at this point in the history
  • Loading branch information
jixinchi authored and gaul committed May 6, 2024
1 parent 03aeccf commit 6670c55
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 ../");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}


Expand Down

0 comments on commit 6670c55

Please sign in to comment.