ITeye博客

SNMP4J的测试代码

steeven · 2009-08-05 17:42 · 行业应用 · Markdown

基本功能的测试,Snmp类的封装有些怪异,不看源码会写错。


import java.io.IOException;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SnmpTest {

	private final static Logger LOG = LoggerFactory.getLogger(SnmpTest.class);
	private static Snmp snmp;
	private static CommunityTarget target1;
	private static CommunityTarget target2;

	@BeforeClass
	public static void init() throws IOException {
		// SNMP echo agent at port 50162
		// snmpListen = new Snmp(new DefaultUdpTransportMapping(new
		// UdpAddress(50162), true));
		snmp = new Snmp(new DefaultUdpTransportMapping());
		snmp.listen();
		CommandResponder listener = new CommandResponder() {
			@Override
			public void processPdu(CommandResponderEvent event) {
				LOG.info(event.getTransportMapping().getListenAddress() + " Get request: {}", event.getPDU());
				PDU pdu = new PDU(event.getPDU()); // echo
				pdu.setType(PDU.RESPONSE);
				pdu.setRequestID(event.getPDU().getRequestID());
				CommunityTarget respTarget = new CommunityTarget(event.getPeerAddress(), new OctetString("public"));
				// respTarget.setRetries(2);
				// respTarget.setTimeout(1500);
				// respTarget.setVersion(SnmpConstants.version2c);
				try { // send an empty response PDU
					snmp.send(pdu, respTarget);
				} catch (IOException e) {
					LOG.error(null, e);
				}
			}
		};
		snmp.addNotificationListener(new UdpAddress(50162), listener);
		snmp.addNotificationListener(new UdpAddress(50168), listener);

		// session to send
		target1 = new CommunityTarget(UdpAddress.parse("localhost/50162"), new OctetString("public"));
		// target.setRetries(2);
		// target.setTimeout(1500);
		target1.setVersion(SnmpConstants.version2c); // support getBulk
		target2 = new CommunityTarget(UdpAddress.parse("localhost/50168"), new OctetString("public"));
	}

	@AfterClass
	public static void destroy() throws IOException {
		snmp.close();
	}

	@Test
	public void testSnmpSend() throws InterruptedException, IOException {
		PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 1 })));
		checkResponse(snmp.get(pdu, target1));
		checkResponse(snmp.getNext(pdu, target1));
		checkResponse(snmp.getBulk(pdu, target1));
		checkResponse(snmp.set(pdu, target1));
		checkResponse(snmp.inform(pdu, target1));
		pdu.setType(PDU.TRAP);
		snmp.send(pdu, target1);
	}

	@Test
	public void testSnmpSendAsync() throws Exception {
		// creating PDU
		final PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 1 })));
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 2 })));
		pdu.setType(PDU.GETNEXT);
		// sending request
		final ResponseListener listener = new ResponseListener() {
			public void onResponse(ResponseEvent event) {
				// important!!!
				((Snmp) event.getSource()).cancel(event.getRequest(), this);
				checkResponse(event);
				synchronized (pdu) {
					pdu.notifyAll();
				}
			}
		};
		snmp.getNext(pdu, target2, null, listener);
		synchronized (pdu) {
			pdu.wait(5000);
		}
	}

	private void checkResponse(ResponseEvent event) {
		if (event == null) {
			Assert.fail("response timeout");
		} else if (event.getError() != null) {
			LOG.error("Get response error", event.getError());
			Assert.fail("failed: " + event.getError().getLocalizedMessage());
		} else if (event.getResponse() == null) {
			Assert.fail("response timeout");
		} else
			LOG.info("Get response {}: {}", Thread.currentThread(), event.getResponse());
	}

	@Test
	public void testTable() {
		// TableUtils util = new TableUtils(snmp,new DefaultPDUFactory());
		// OID oidIndex;
		// OID oidStatus;
		// VariableBinding[] values;
		// util.createRow(target1, oidStatus, oidIndex, values);
		// util.destroyRow(target1, oidIndex, oidIndex);
		// OID[] columnOIDs;
		// util.getTable(target1, columnOIDs, null, oidIndex);
	}
}

评论(1)

  1. #1 楼 · steeven · 2009-08-05 23:45

    有点小问题,listener应该用mock来测试 log输出看点: 不同端口的接收 同步异步的thread

    2009-08-05 23:41:08,427 INFO SnmpTest 154 0.0.0.0/50162 Get request: GET[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 163 Get response Thread[main,5,main]: RESPONSE[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 154 0.0.0.0/50162 Get request: GETNEXT[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 163 Get response Thread[main,5,main]: RESPONSE[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 154 0.0.0.0/50162 Get request: GETBULK[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 163 Get response Thread[main,5,main]: RESPONSE[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 154 0.0.0.0/50162 Get request: SET[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 163 Get response Thread[main,5,main]: RESPONSE[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 163 Get response Thread[main,5,main]: RESPONSE[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 154 0.0.0.0/50162 Get request: INFORM[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 154 0.0.0.0/50162 Get request: TRAP[requestid=446902765, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 154 0.0.0.0/50168 Get request: GETNEXT[requestid=446902779, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null; 1.3.6.1.2.1.1.2 = Null]] 2009-08-05 23:41:08,427 INFO SnmpTest 163 Get response Thread[defaultudptransportmapping_10.80.2.184/0,5,main]: RESPONSE[requestid=446902779, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null; 1.3.6.1.2.1.1.2 = Null]]

评论功能维护中,当前仅展示历史评论。