From 86bc184cfdce2dfb844a0401dc99095f36dab1ac Mon Sep 17 00:00:00 2001 From: Stephan Fuhrmann Date: Mon, 8 Jan 2024 16:05:25 +0100 Subject: [PATCH] Test for multi varbinds Support for multi line strings --- .../snmpman/configuration/Walks.java | 39 ++++++++++++---- .../snmpman/configuration/WalksTest.java | 46 +++++++++++++++++++ 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/snmpman/src/main/java/com/oneandone/snmpman/configuration/Walks.java b/snmpman/src/main/java/com/oneandone/snmpman/configuration/Walks.java index 3e79f3c..eff964d 100644 --- a/snmpman/src/main/java/com/oneandone/snmpman/configuration/Walks.java +++ b/snmpman/src/main/java/com/oneandone/snmpman/configuration/Walks.java @@ -109,9 +109,29 @@ static Map 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(); @@ -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": diff --git a/snmpman/src/test/java/com/oneandone/snmpman/configuration/WalksTest.java b/snmpman/src/test/java/com/oneandone/snmpman/configuration/WalksTest.java index c1f4575..622c90f 100644 --- a/snmpman/src/test/java/com/oneandone/snmpman/configuration/WalksTest.java +++ b/snmpman/src/test/java/com/oneandone/snmpman/configuration/WalksTest.java @@ -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; @@ -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; @@ -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 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")); @@ -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 walk = Walks.readWalk(tmpFile.toFile()); + Map 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"));