Skip to content

Commit

Permalink
add _ and : for id of content
Browse files Browse the repository at this point in the history
  • Loading branch information
adam committed Mar 20, 2024
1 parent 85948ac commit 24a54c6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.

This file was deleted.

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");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adamcalculator.dynamicpack.pack;

import com.adamcalculator.dynamicpack.DynamicPackModBase;
import com.adamcalculator.dynamicpack.InputValidator;
import com.adamcalculator.dynamicpack.Mod;
import com.adamcalculator.dynamicpack.PackUtil;
import com.adamcalculator.dynamicpack.sync.PackSyncProgress;
Expand All @@ -12,7 +13,6 @@

import java.io.IOException;
import java.nio.file.Path;
import java.rmi.RemoteException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.function.LongConsumer;
Expand Down Expand Up @@ -105,11 +105,8 @@ public void onUpdate(FileDownloadConsumer it) {
}

String remoteName = repoJson.getString("name");
if (remoteName.isBlank()) {
throw new RuntimeException("Name of remote pack can't be blank");
}
if (remoteName.trim().length() > 50) {
throw new RuntimeException("Length of name pack can't > 50");
if (!InputValidator.isPackNameValid(remoteName)) {
throw new RuntimeException("Remote name of pack not valid.");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.adamcalculator.dynamicpack.pack;

import com.adamcalculator.dynamicpack.DynamicPackModBase;
import com.adamcalculator.dynamicpack.IDValidator;
import com.adamcalculator.dynamicpack.InputValidator;
import com.adamcalculator.dynamicpack.Mod;
import com.adamcalculator.dynamicpack.PackUtil;
import com.adamcalculator.dynamicpack.sync.PackSyncProgress;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void close() throws IOException {

private void processContent(JSONObject jsonContent) throws IOException {
String id = jsonContent.getString("id");
if (!IDValidator.isValid(id)) {
if (!InputValidator.isContentIdValid(id)) {
throw new RuntimeException("Id of content is not valid.");
}

Expand Down
30 changes: 30 additions & 0 deletions common/src/test/java/tests/InputValidatorTest.java
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("__"));
}
}

0 comments on commit 24a54c6

Please sign in to comment.