Skip to content

Commit

Permalink
NMS-15599: Fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
christianpape authored Apr 20, 2023
1 parent ac63cc6 commit 6d48ed9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,30 @@

import java.nio.charset.Charset;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.CharUtils;

public abstract class AbstractSnmpValue implements SnmpValue {

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

final String str = new String(bytes, Charset.defaultCharset());
final int sz = str.length();

for(int i = 0; i < sz; ++i) {
// check whether character is between 31 and 127
final boolean isDisplayable = CharUtils.isAsciiPrintable(str.charAt(i));
// check for null terminated string
final boolean isNullTerminated = str.charAt(i) == 0 && i == sz-1;

if (!isDisplayable && !isNullTerminated) {
return false;
}
}

return true;
}

public static boolean allBytesDisplayable(final byte[] bytes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public String toHexString() {

@Override
public boolean isDisplayable() {
return allBytesDisplayable(getBytes());
return allBytesPlainAscii(getBytes());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public void testOtherAddressOctetString() {
final SnmpValue value = factory.getOctetString(rawBytes);

assertArrayEquals(className + ": getOctetString bytes should match", rawBytes, value.getBytes());
assertTrue(className + ": getOctetString displayable should be true", value.isDisplayable());
assertFalse(className + ": getOctetString displayable should be false", value.isDisplayable());
assertEquals(className + ": getOctetString to String should return " + stringBytes, stringBytes, value.toString());
assertEquals(className + ": getOctetString to DisplayString should return " + stringBytes, stringBytes, value.toDisplayString());
assertEquals(className + ": getOctetString to HexString should return " + hexString, hexString, value.toHexString());
Expand Down

0 comments on commit 6d48ed9

Please sign in to comment.