Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
Test for multi varbinds
Browse files Browse the repository at this point in the history
Support for multi line strings
  • Loading branch information
sfuhrm committed Jan 8, 2024
1 parent f22065d commit 86bc184
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,29 @@ static Map<OID, Variable> readVariableBindings(final File walk, final BufferedRe
}

// if we have a continuation line for a Hex-STRING, append to it
if (lastType != null && lastOid != null && lastType.equals("Hex-STRING")) {
if (!match && lastType != null && lastOid != null && lastType.equals("STRING")) {
OctetString octetStringToExtend = (OctetString) bindings.get(lastOid);
if (octetStringToExtend != null) {
match = true;
String oldString = octetStringToExtend.toString();
String newString;
if (line.endsWith("\"")) {
newString = line.substring(0, line.length() - 1);
} else {
newString = line;
}
String combined = oldString + "\n" + newString;
bindings.put(lastOid, new OctetString(combined));
} else {
log.warn("Could not find the previous octet string of OID {} in walk file {}", lastOid);
}
}

// if we have a continuation line for a Hex-STRING, append to it
if (!match && lastType != null && lastOid != null && lastType.equals("Hex-STRING")) {
matcher = HEX_STRING_PATTERN.matcher(line);
if (matcher.matches()) {
match = true;
OctetString octetStringToExtend = (OctetString) bindings.get(lastOid);
if (octetStringToExtend != null) {
byte[] oldBytes = octetStringToExtend.getValue();
Expand Down Expand Up @@ -145,14 +165,17 @@ private static Variable getVariable(final String type, final String value) {
switch (type) {
// TODO add "BITS" support
case "STRING":
if (value.startsWith("\"") && value.endsWith("\"")) {
if (value.length() == 2) {
return new OctetString();
}
return new OctetString(value.substring(1, value.length() - 1));
} else {
return new OctetString(value);
String use = value;
if (use.startsWith("\"")) {
use = use.substring(1);
}
if (use.endsWith("\"")) {
use = use.substring(0, use.length() - 1);
}
if (use.length() == 0) {
return new OctetString();
}
return new OctetString(use);
case "OID":
return new OID(value);
case "Gauge32":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.oneandone.snmpman.configuration.modifier.Counter32Modifier;
import com.oneandone.snmpman.configuration.modifier.Modifier;
import com.oneandone.snmpman.configuration.type.ModifierProperties;
import org.snmp4j.smi.Counter64;
import org.snmp4j.smi.Gauge32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
Expand All @@ -15,6 +17,7 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.testng.Assert.assertEquals;
Expand Down Expand Up @@ -50,6 +53,23 @@ public void readWalkWithStringLine() throws IOException {
new OctetString("GigabitEthernet0/1")));
}

@Test
public void readWalkWithFourStringLine() throws IOException {
Files.write(tmpFile, Arrays.asList(
".1.0.8802.1.1.2.1.3.4.0 = STRING: \"Cisco IOS Software, C3560 Software (C3560-IPBASEK9-M), Version 12.2(50)SE3, RELEASE SOFTWARE (fc1)",
"Technical Support: http://www.cisco.com/techsupport",
"Copyright (c) 1986-2009 by Cisco Systems, Inc.",
"Compiled Wed 22-Jul-09 06:41 by prod_rel_team\""));
Map<OID, Variable> walk = Walks.readWalk(tmpFile.toFile());
String expected = "Cisco IOS Software, C3560 Software (C3560-IPBASEK9-M), Version 12.2(50)SE3, RELEASE SOFTWARE (fc1)\n" +
"Technical Support: http://www.cisco.com/techsupport\n" +
"Copyright (c) 1986-2009 by Cisco Systems, Inc.\n" +
"Compiled Wed 22-Jul-09 06:41 by prod_rel_team";
assertEquals(walk, Collections.singletonMap(
new OID(".1.0.8802.1.1.2.1.3.4.0"),
new OctetString(expected)));
}

@Test
public void readWalkWithHexStringLine() throws IOException {
Files.write(tmpFile, Collections.singletonList(".1.3.6.1.4.1.9.9.683.1.5.0 = Hex-STRING: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f"));
Expand Down Expand Up @@ -78,6 +98,32 @@ public void readWalkWithTwoHexStringLine() throws IOException {
new OctetString(expected)));
}

@Test
public void readWalkWithMultipleKeys() throws IOException {
Files.write(tmpFile, Arrays.asList(
".1.3.6.1.2.1.2.2.1.2.10101 = STRING: \"GigabitEthernet0/1\"",
".1.3.6.1.2.1.31.1.1.1.1.10101 = STRING: \"Gi0/1\"",
".1.3.6.1.2.1.31.1.1.1.10.10101 = Counter64: 48648257581",
".1.3.6.1.2.1.31.1.1.1.11.10101 = Counter64: 32038868",
".1.3.6.1.2.1.31.1.1.1.12.10101 = Counter64: 141915228",
".1.3.6.1.2.1.31.1.1.1.13.10101 = Counter64: 44011328",
".1.3.6.1.2.1.31.1.1.1.15.10101 = Gauge32: 1000",
".1.3.6.1.2.1.2.2.1.5.10101 = Gauge32: 1000000000"
));
Map<OID, Variable> walk = Walks.readWalk(tmpFile.toFile());
Map<OID, Variable> expected = new HashMap<>();
expected.put(new OID(".1.3.6.1.2.1.2.2.1.2.10101"), new OctetString("GigabitEthernet0/1"));
expected.put(new OID(".1.3.6.1.2.1.31.1.1.1.1.10101"), new OctetString("Gi0/1"));
expected.put(new OID(".1.3.6.1.2.1.31.1.1.1.10.10101"), new Counter64(48648257581L));
expected.put(new OID(".1.3.6.1.2.1.31.1.1.1.11.10101"), new Counter64(32038868L));
expected.put(new OID(".1.3.6.1.2.1.31.1.1.1.12.10101"), new Counter64(141915228L));
expected.put(new OID(".1.3.6.1.2.1.31.1.1.1.13.10101"), new Counter64(44011328));
expected.put(new OID(".1.3.6.1.2.1.31.1.1.1.15.10101"), new Gauge32(1000));
expected.put(new OID(".1.3.6.1.2.1.2.2.1.5.10101"), new Gauge32(1000000000));

assertEquals(walk, expected);
}

@Test
public void readWalkWithInvalidLine() throws IOException {
Files.write(tmpFile, Collections.singletonList("this is just an example"));
Expand Down

0 comments on commit 86bc184

Please sign in to comment.