From 2273e99cefae2839c1da157ed1aeb0489ab8e5c5 Mon Sep 17 00:00:00 2001 From: Sebb Date: Fri, 3 Jan 2025 15:10:28 +0000 Subject: [PATCH] Add check that pattern agrees with length --- .../commons/validator/routines/IBANValidatorTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java index 8a87654ac..168dd3dbe 100644 --- a/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java @@ -228,19 +228,21 @@ public class IBANValidatorTest { ); // @formatter:on - private static String fmtRE(final String ibanPat) { + private static String fmtRE(final String ibanPat, final int ibanLength) { final Matcher m = IBAN_PAT.matcher(ibanPat); if (!m.matches()) { throw new IllegalArgumentException("Unexpected IBAN pattern " + ibanPat); } final StringBuilder sb = new StringBuilder(); int len = Integer.parseInt(m.group(1)); // length of part + int totLen = len; String curType = m.group(2); // part type for (int i = 3; i <= m.groupCount(); i += 2) { if (m.group(i) == null) { // reached an optional group break; } final int count = Integer.parseInt(m.group(i)); + totLen += count; final String type = m.group(i + 1); if (type.equals(curType)) { // more of the same type len += count; @@ -251,6 +253,7 @@ private static String fmtRE(final String ibanPat) { } } sb.append(formatToRE(curType, len)); + assertEquals(ibanLength, totLen, "Wrong length for " + ibanPat); return sb.toString(); } @@ -514,7 +517,7 @@ public void testValidatorShouldExistWithProperConfiguration(final String country final List allPatterns = Arrays.stream(validator.getRegexValidator().getPatterns()).map(Pattern::pattern).collect(Collectors.toList()); - final String re = fmtRE(structure.substring(2)); + final String re = fmtRE(structure.substring(2), ibanLength - 2); //allow for prefix assertTrue(allPatterns.remove(countryCode + re), "No pattern " + countryCode + re + " found for " + countryInfo); for (final String ac : acountyCode) { assertTrue(allPatterns.remove(ac + re), "No additional country code " + ac + " found for " + countryInfo);