Skip to content

Commit

Permalink
Possible fix for MAC OS properties which contain spaces #33
Browse files Browse the repository at this point in the history
  • Loading branch information
sfuhrm committed Jan 5, 2024
1 parent da3f720 commit 705bf65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions openssl4j/src/main/java/de/sfuhrm/openssl4j/ObjectTransfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,26 @@ final class ObjectTransfer {

/** Gets a system property, enforcing that the value string is alphanumeric.
* @param property the name of the property to get.
* @return the value of the property if it is alphanumeric.
* @throws IllegalStateException if there is a non alphanumeric character in the string.
* @return the value of the property consisting of its alphanumeric parts
* and the non-alphanumeric parts being replaced with an
* underscore character ('_').
* @throws NullPointerException if property is null or not set.
* */
static String getSystemPropertyAlnum(String property) {
Objects.requireNonNull(property);

final String value = System.getProperty(property);
Objects.requireNonNull(value, "System property " + property + " is null");
StringBuilder result = new StringBuilder(value.length());
for (int i = 0; i < value.length(); i++) {
final int c = value.charAt(i);
if ((!Character.isLetterOrDigit(c))) {
throw new IllegalStateException("os property '" + property + "' is containing non-alphanumeric values");
final char c = value.charAt(i);
if (Character.isLetterOrDigit(c)) {
result.append(c);
} else {
result.append('_');
}
}
return value;
return result.toString();
}

private static String getOsName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public void enforceAlnumWithAlnum() {
@Test
public void enforceAlnumWithSpace() {
System.setProperty("foo", "foobar 123");
Assertions.assertThrows(IllegalStateException.class, () -> ObjectTransfer.getSystemPropertyAlnum("foo"));
Assertions.assertEquals("foobar_123", ObjectTransfer.getSystemPropertyAlnum("foo"));
}

@Test
public void enforceAlnumWithBackslash() {
System.setProperty("foo", "foobar\\123");
Assertions.assertThrows(IllegalStateException.class, () -> ObjectTransfer.getSystemPropertyAlnum("foo"));
Assertions.assertEquals("foobar_123", ObjectTransfer.getSystemPropertyAlnum("foo"));
}

@Test
Expand Down

0 comments on commit 705bf65

Please sign in to comment.