Skip to content

Commit

Permalink
NMS-15498: Treat only ASCII characters between 32 and 127 as displayable
Browse files Browse the repository at this point in the history
  • Loading branch information
christianpape authored Apr 18, 2023
1 parent 8570ccf commit ac63cc6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions core/snmp/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@

package org.opennms.netmgt.snmp;

import java.nio.charset.Charset;

import org.apache.commons.lang.StringUtils;

public abstract class AbstractSnmpValue implements SnmpValue {

public static boolean allBytesPlainAscii(final byte[] bytes) {
return StringUtils.isAsciiPrintable(new String(bytes, Charset.defaultCharset()));
}

public static boolean allBytesDisplayable(final byte[] bytes) {
if (allBytesUTF_8(bytes)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package org.opennms.netmgt.snmp;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -110,6 +111,14 @@ public void testHighIso8859CharDisplayable() throws UnsupportedEncodingException
assertTrue(new String(hexStringToBytes(highIso8859Char), "ISO-8859-1"), AbstractSnmpValue.allBytesDisplayable(hexStringToBytes(highIso8859Char)));
}

@Test
public void testPlainAscii() {
for(char i = 0; i < 256; i++) {
final String string = "foo" + i + "bar";
assertEquals(i > 31 && i < 127, AbstractSnmpValue.allBytesPlainAscii(string.getBytes()));
}
}

private static byte[] hexStringToBytes(String hexString) {
assertTrue(hexString.length() % 2 == 0);
byte[] retval = new byte[hexString.length() / 2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public boolean isDisplayable() {
return true;

if (getType() == SnmpValue.SNMP_OCTET_STRING) {
return allBytesDisplayable(getBytes());
return allBytesPlainAscii(getBytes());
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public boolean isDisplayable() {
}

if (getType() == SnmpValue.SNMP_OCTET_STRING) {
return allBytesDisplayable(getBytes());
return allBytesPlainAscii(getBytes());
}

return false;
Expand Down

0 comments on commit ac63cc6

Please sign in to comment.