-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
adam
committed
Mar 20, 2024
1 parent
85948ac
commit 24a54c6
Showing
5 changed files
with
59 additions
and
21 deletions.
There are no files selected for viewing
13 changes: 0 additions & 13 deletions
13
common/src/main/java/com/adamcalculator/dynamicpack/IDValidator.java
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
common/src/main/java/com/adamcalculator/dynamicpack/InputValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.adamcalculator.dynamicpack; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class InputValidator { | ||
private static final Pattern CONTENT_ID_PATTERN = Pattern.compile("^[a-z0-9_:]{2,128}$"); | ||
|
||
public static boolean isContentIdValid(String input) { | ||
if (input == null) { | ||
return false; | ||
} | ||
Matcher matcher = CONTENT_ID_PATTERN.matcher(input); | ||
return matcher.matches(); | ||
} | ||
|
||
public static boolean isPackNameValid(String input) { | ||
if (input == null) { | ||
return false; | ||
} | ||
|
||
return input.trim().length() < 64 && !input.trim().isEmpty() && !input.contains("\n") && !input.contains("\r") && !input.contains("\b"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tests; | ||
|
||
import com.adamcalculator.dynamicpack.InputValidator; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class InputValidatorTest { | ||
|
||
@Test | ||
public void testContentId() { | ||
Assertions.assertFalse(InputValidator.isContentIdValid("")); | ||
Assertions.assertFalse(InputValidator.isContentIdValid(" ")); | ||
Assertions.assertFalse(InputValidator.isContentIdValid(" ")); | ||
Assertions.assertFalse(InputValidator.isContentIdValid(" 32")); | ||
Assertions.assertFalse(InputValidator.isContentIdValid("test\ntest")); | ||
|
||
Assertions.assertTrue(InputValidator.isContentIdValid("__")); | ||
Assertions.assertTrue(InputValidator.isContentIdValid("pack:megapack")); | ||
Assertions.assertTrue(InputValidator.isContentIdValid("1234567890")); | ||
Assertions.assertTrue(InputValidator.isContentIdValid("01")); | ||
Assertions.assertTrue(InputValidator.isContentIdValid("test_pack")); | ||
Assertions.assertTrue(InputValidator.isContentIdValid("super:mega_puper:")); | ||
} | ||
|
||
@Test | ||
public void testRemoteName() { | ||
Assertions.assertFalse(InputValidator.isPackNameValid("\n")); | ||
Assertions.assertTrue(InputValidator.isPackNameValid("__")); | ||
} | ||
} |