Skip to content

Commit

Permalink
Improved Peppol Process ID checks
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Nov 22, 2023
1 parent f85b17d commit 7dae211
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public boolean isParticipantIdentifierSchemeValid (@Nullable final String sSchem
{
if (!PeppolIdentifierHelper.isValidIdentifierScheme (sScheme))
return false;

return RegExHelper.stringMatchesPattern (PeppolIdentifierHelper.PARTICIPANT_IDENTIFIER_SCHEME_REGEX,
sScheme.toLowerCase (Locale.US));
}
Expand All @@ -202,19 +203,30 @@ public boolean isParticipantIdentifierValueValid (@Nullable final String sValue)
return false;

final int nLength = sValue.length ();
// > 0 and <= 50 characters
if (nLength == 0 || nLength > PeppolIdentifierHelper.MAX_PARTICIPANT_VALUE_LENGTH)

// POLICY 1
// At least one characters
if (nLength == 0)
return false;

// <= 50 characters
if (nLength > PeppolIdentifierHelper.MAX_PARTICIPANT_VALUE_LENGTH)
return false;

// Check if the separator between identifier issuing agency and value is
// present - can only be done if the default scheme is present
// Also does not work in certain representations when the numeric scheme is
// extracted into an attribute (see e.g. POLICY 14)
if (false)
if (sValue.indexOf (':') < 0)
return false;

// Check if the value is US ASCII encoded
return PeppolIdentifierHelper.areCharsetChecksDisabled () ||
StandardCharsets.US_ASCII.newEncoder ().canEncode (sValue);
// Check if the value is ISO-8859-1 encoded
if (!PeppolIdentifierHelper.areCharsetChecksDisabled ())
if (!StandardCharsets.ISO_8859_1.newEncoder ().canEncode (sValue))
return false;

return true;
}

@Nullable
Expand Down Expand Up @@ -249,13 +261,27 @@ public boolean isProcessIdentifierSchemeValid (@Nullable final String sScheme)
public boolean isProcessIdentifierValueValid (@Nullable final String sValue)
{
final int nLength = StringHelper.getLength (sValue);
// > 0 or <= 200 chars
if (nLength == 0 || nLength > PeppolIdentifierHelper.MAX_PROCESS_VALUE_LENGTH)

// POLICY 1
// At least one char
if (nLength == 0)
return false;

// <= 200 chars
if (nLength > PeppolIdentifierHelper.MAX_PROCESS_VALUE_LENGTH)
return false;

// Check if the value is ISO-8859-1 encoded
return PeppolIdentifierHelper.areCharsetChecksDisabled () ||
StandardCharsets.ISO_8859_1.newEncoder ().canEncode (sValue);
if (!PeppolIdentifierHelper.areCharsetChecksDisabled ())
if (!StandardCharsets.ISO_8859_1.newEncoder ().canEncode (sValue))
return false;

// POLICY 25
for (final char c : sValue.toCharArray ())
if (Character.isWhitespace (c))
return false;

return true;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public void testIsValidProcessIdentifierValue ()
assertFalse (PeppolIdentifierFactory.INSTANCE.isProcessIdentifierValueValid (""));

assertTrue (PeppolIdentifierFactory.INSTANCE.isProcessIdentifierValueValid ("proc1"));
assertTrue (PeppolIdentifierFactory.INSTANCE.isProcessIdentifierValueValid ("proc2 "));
assertTrue (PeppolIdentifierFactory.INSTANCE.isProcessIdentifierValueValid ("proc2"));
assertFalse (PeppolIdentifierFactory.INSTANCE.isProcessIdentifierValueValid ("proc2 "));

assertTrue (PeppolIdentifierFactory.INSTANCE.isProcessIdentifierValueValid (StringHelper.getRepeated ('a',
PeppolIdentifierHelper.MAX_PROCESS_VALUE_LENGTH)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ public void testURIStuff ()
assertNull (aIF.parseProcessIdentifier (""));

assertNotNull (aIF.parseProcessIdentifier ("process::proc1"));
assertNotNull (aIF.parseProcessIdentifier ("process::proc2 "));
assertNotNull (aIF.parseProcessIdentifier ("process::proc2"));
assertNull (aIF.parseProcessIdentifier ("process::proc2 "));

assertNull (aIF.parseProcessIdentifier ("processany-actorid-dummythatiswaytoolongforwhatisexpected::proc2"));
assertNull (aIF.parseProcessIdentifier ("process::" +
StringHelper.getRepeated ('a', PeppolIdentifierHelper.MAX_PROCESS_VALUE_LENGTH + 1)));
StringHelper.getRepeated ('a',
PeppolIdentifierHelper.MAX_PROCESS_VALUE_LENGTH +
1)));
assertNull (aIF.parseProcessIdentifier ("process:proc2"));
assertNull (aIF.parseProcessIdentifier ("processproc2"));
}
Expand Down Expand Up @@ -164,7 +167,8 @@ public void testConstraints ()
{
// Scheme too long
new PeppolProcessIdentifier (PeppolIdentifierHelper.DEFAULT_PROCESS_SCHEME +
StringHelper.getRepeated ('a', PeppolIdentifierHelper.MAX_IDENTIFIER_SCHEME_LENGTH + 1),
StringHelper.getRepeated ('a',
PeppolIdentifierHelper.MAX_IDENTIFIER_SCHEME_LENGTH + 1),
"abc");
fail ();
}
Expand All @@ -186,7 +190,8 @@ public void testConstraints ()
public void testHasDefaultProcessIdentifierScheme ()
{
final IIdentifierFactory aIF = PeppolIdentifierFactory.INSTANCE;
assertTrue (aIF.createProcessIdentifierWithDefaultScheme ("abc").hasScheme (PeppolIdentifierHelper.DEFAULT_PROCESS_SCHEME));
assertTrue (aIF.createProcessIdentifierWithDefaultScheme ("abc")
.hasScheme (PeppolIdentifierHelper.DEFAULT_PROCESS_SCHEME));
assertFalse (new PeppolProcessIdentifier ("proctype", "abc").hasDefaultScheme ());
}
}

0 comments on commit 7dae211

Please sign in to comment.