Package org.chiefly.sunlamp

Source Code of org.chiefly.sunlamp.LiveSNMPTest

/**
* Copyright (C) 2011 Matt Doyle (chief@chiefly.org)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.chiefly.sunlamp;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.chiefly.sunlamp.util.ResponseEvents;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.snmp4j.PDUv1;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Counter64;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.TreeEvent;

@Ignore
public final class LiveSNMPTest {

  /** A non-SNMPv1 binding to test with. */
  private static final VariableBinding NON_V1_BINDING = new VariableBinding(
      new OID("1.3.6.1.2.1.1.1"), new Counter64());

  /** sysOrid {@link OID}. */
  private static final OID SYS_ORID = new OID("1.3.6.1.2.1.1.9.1.3");

  /** SNMP v1/v2 community string. */
  private static final OctetString V1V2COMMUNITY = new OctetString(
      "c0mmun1ty");

  /** SNMP v3 authentication passphrase. */
  private static final OctetString V3AUTHPASS = null;

  /** SNMP v3 authentication protocol. */
  private static final OID V3AUTHPROTO = null;

  /** SNMP v3 privacy passphrase. */
  private static final OctetString V3PRIVPASS = null;

  /** SNMP v3 privacy protocol. */
  private static final OID V3PRIVPROTO = null;

  /** SNMP v3 security name. */
  private static final OctetString V3SECURITYNAME = new OctetString("admin");

  /** The {@link UdpAddress} to test against. */
  private final UdpAddress address;

  /** All {@link SNMP}s to test. */
  private final List<SNMP<UdpAddress>> allClients = new ArrayList<SNMP<UdpAddress>>();

  /** {@link SNMPv1} to test. */
  private final SNMPv1<UdpAddress> v1Client;

  /** {{@link SNMPv2} to test. */
  private final SNMPv2<UdpAddress> v2Client;

  /** {@link SNMPv2}s to test. */
  private final List<SNMPv2<UdpAddress>> v2Clients = new ArrayList<SNMPv2<UdpAddress>>();

  /** {{@link SNMPv3} to test. */
  private final SNMPv3<UdpAddress> v3Client;

  public LiveSNMPTest() throws IOException {

    final InetAddress ip = InetAddress.getByName("192.168.0.1");
    this.address = new UdpAddress(ip,
        SnmpConstants.DEFAULT_COMMAND_RESPONDER_PORT);

    final UsmUser usmUser = new UsmUser(V3SECURITYNAME, V3AUTHPROTO,
        V3AUTHPASS, V3PRIVPROTO, V3PRIVPASS);
    this.v1Client = new StandardSNMPv1<UdpAddress>(V1V2COMMUNITY,
        new DefaultUdpTransportMapping());
    this.v2Client = new StandardSNMPv2<UdpAddress>(V1V2COMMUNITY,
        new DefaultUdpTransportMapping());
    this.v3Client = new StandardSNMPv3<UdpAddress>(usmUser,
        new OctetString(), new DefaultUdpTransportMapping());

    this.allClients.add(this.v1Client);
    this.allClients.add(this.v2Client);
    // this.allClients.add(this.v3Client);

    this.v2Clients.add(this.v2Client);
    this.v2Clients.add(this.v3Client);
  }

  @Test
  public void testGetAsync() throws IOException {

    for (final SNMP<UdpAddress> sender : this.allClients) {

      final TestResponseListener testListener = new TestResponseListener();

      sender.getAsync(this.address, testListener, "handle",
          SnmpConstants.sysDescr);
      testListener.waitForCallback();
      Assert.assertNotNull(testListener.getResponse());
      Assert.assertTrue(ResponseEvents.extractBindings(
          testListener.getResponse()).containsKey(
          SnmpConstants.sysDescr));
      testListener.reset();
    }
  }

  @Test
  public void testGetBulkAsync() throws IOException {

    for (final SNMPv2<UdpAddress> v2Client : this.v2Clients) {

      final TestResponseListener testListener = new TestResponseListener();

      final OID system = new OID("1.3.6.1.2.1.1");
      final OID ifTableRow1 = new OID("1.3.6.1.2.1.2.2.1.1");
      final OID ifTableRow2 = new OID("1.3.6.1.2.1.2.2.1.2");
      v2Client.getBulkAsync(this.address, 2, 1, testListener, "handle",
          system, ifTableRow1, ifTableRow2);
      testListener.waitForCallback();
      final Map<OID, Variable> bindings = ResponseEvents
          .extractBindings(testListener.getResponse());
      Assert.assertEquals(5, bindings.size());
      Assert.assertTrue(bindings.containsKey(new OID(system.toString()
          + ".1.0")));
      Assert.assertTrue(bindings.containsKey(new OID(ifTableRow1
          .toString() + ".1")));
      Assert.assertTrue(bindings.containsKey(new OID(ifTableRow1
          .toString() + ".2")));
      Assert.assertTrue(bindings.containsKey(new OID(ifTableRow2
          .toString() + ".1")));
      Assert.assertTrue(bindings.containsKey(new OID(ifTableRow2
          .toString() + ".2")));
      testListener.reset();
    }
  }

  @Test
  public void testGetBulkSync() throws IOException {

    for (final SNMPv2<UdpAddress> v2Client : this.v2Clients) {

      final OID system = new OID("1.3.6.1.2.1.1");
      final OID ifTableRow1 = new OID("1.3.6.1.2.1.2.2.1.1");
      final OID ifTableRow2 = new OID("1.3.6.1.2.1.2.2.1.2");
      final ResponseEvent response = v2Client.getBulkSync(this.address,
          2, 1, system, ifTableRow1, ifTableRow2);
      final Map<OID, Variable> bindings = ResponseEvents
          .extractBindings(response);
      Assert.assertEquals(5, bindings.size());
      Assert.assertTrue(bindings.containsKey(new OID(system.toString()
          + ".1.0")));
      Assert.assertTrue(bindings.containsKey(new OID(ifTableRow1
          .toString() + ".1")));
      Assert.assertTrue(bindings.containsKey(new OID(ifTableRow1
          .toString() + ".2")));
      Assert.assertTrue(bindings.containsKey(new OID(ifTableRow2
          .toString() + ".1")));
      Assert.assertTrue(bindings.containsKey(new OID(ifTableRow2
          .toString() + ".2")));
    }
  }

  @Test
  public void testGetNextAsync() throws IOException {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final TestResponseListener testListener = new TestResponseListener();

      client.getNextAsync(this.address, testListener, "handle", new OID(
          "1.3.6.1.2.1.1.1"));
      testListener.waitForCallback();
      Assert.assertNotNull(testListener.getResponse());
      Assert.assertTrue(ResponseEvents.extractBindings(
          testListener.getResponse()).containsKey(
          SnmpConstants.sysDescr));
      testListener.reset();
    }
  }

  @Test
  public void testGetNextSync() throws IOException {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final ResponseEvent response = client.getNextSync(this.address,
          new OID("1.3.6.1.2.1.1.1"));
      Assert.assertNotNull(response);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysDescr));
    }
  }

  @Test
  public void testGetSync() throws IOException {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final ResponseEvent response = client.getSync(this.address,
          SnmpConstants.sysDescr);
      Assert.assertNotNull(response);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysDescr));
    }
  }

  @Test
  public void testMultiGetNextSync() throws IOException {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final OID beforeSysDescr = new OID("1.3.6.1.2.1.1.1");
      final OID beforeSysLocation = new OID("1.3.6.1.2.1.1.6");
      final ResponseEvent response = client.getNextSync(this.address,
          beforeSysDescr, beforeSysLocation);
      Assert.assertNotNull(response);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysDescr));
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysLocation));
    }
  }

  @Test
  public void testMultiGetSync() throws IOException {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final ResponseEvent response = client.getSync(this.address,
          SnmpConstants.sysDescr, SnmpConstants.sysLocation);
      Assert.assertNotNull(response);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysDescr));
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysLocation));
    }
  }

  @Test
  public void testMultiRequest() throws IOException {

    for (final SNMP<UdpAddress> client : this.allClients) {

      /* Do a get()... */
      ResponseEvent response = client.getSync(this.address,
          SnmpConstants.sysDescr);
      Assert.assertNotNull(response);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysDescr));

      /* And a getNext()... */
      response = client.getNextSync(this.address, new OID(
          "1.3.6.1.2.1.1.1"));
      Assert.assertNotNull(response);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysDescr));

      /* And another get()... */
      response = client.getSync(this.address, SnmpConstants.sysDescr);
      Assert.assertNotNull(response);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysDescr));
    }
  }

  @Test
  public void testMultiSetSync() throws IOException {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final String random = Integer.toString(new Random().nextInt(100));
      final OctetString location = new OctetString("SECRET SITE #"
          + random);
      final OctetString descr = new OctetString("SECRET DEVICE " + random);
      final VariableBinding binding1 = new VariableBinding(
          SnmpConstants.sysLocation, location);
      final VariableBinding binding2 = new VariableBinding(
          SnmpConstants.sysDescr, descr);
      ResponseEvent response = client.setSync(this.address, binding1,
          binding2);
      Assert.assertNotNull(response);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysLocation));
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysDescr));
      response = client.getSync(this.address, SnmpConstants.sysLocation,
          SnmpConstants.sysDescr);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysLocation));
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysDescr));
    }
  }

  @Test
  public void testMultiUserV3() throws IOException {

    /* Test a valid security name */
    ResponseEvent response = this.v3Client.getSync(this.address,
        SnmpConstants.sysDescr);
    Assert.assertNotNull(response);
    Assert.assertTrue(ResponseEvents.extractBindings(response).containsKey(
        SnmpConstants.sysDescr));

    /* Test an invalid security name */
    final UsmUser invalidUser = new UsmUser(new OctetString("invalidName"),
        V3AUTHPROTO, V3AUTHPASS, V3PRIVPROTO, V3PRIVPASS);
    final SNMPv3<UdpAddress> invalidV3Client = new StandardSNMPv3<UdpAddress>(
        invalidUser, new OctetString(),
        new DefaultUdpTransportMapping());
    response = invalidV3Client
        .getSync(this.address, SnmpConstants.sysDescr);
    Assert.assertNotNull(response);
    Assert.assertFalse(ResponseEvents.extractBindings(response)
        .containsKey(SnmpConstants.sysDescr));

    /* Test the valid security name again */
    response = this.v3Client.getSync(this.address, SnmpConstants.sysDescr);
    Assert.assertNotNull(response);
    Assert.assertTrue(ResponseEvents.extractBindings(response).containsKey(
        SnmpConstants.sysDescr));
  }

  @Test
  public void testNotify() throws IOException {
    for (final SNMPv2<UdpAddress> v2Client : this.v2Clients) {
      v2Client.notify(this.address, new VariableBinding(
          SnmpConstants.sysDescr));
    }
  }

  @Test
  public void testSetAsync() throws IOException {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final TestResponseListener testListener = new TestResponseListener();

      final OctetString location = new OctetString("SECRET SITE #"
          + Integer.toString(new Random().nextInt(100)));
      final VariableBinding binding = new VariableBinding(
          SnmpConstants.sysLocation, location);
      client.setAsync(this.address, testListener, "handle", binding);
      testListener.waitForCallback();
      Assert.assertNotNull(testListener.getResponse());
      Assert.assertTrue(ResponseEvents.extractBindings(
          testListener.getResponse()).containsKey(
          SnmpConstants.sysLocation));
      testListener.reset();
    }
  }

  @Test(expected = IllegalArgumentException.class)
  public void testSetAsyncNonV1() throws IOException {
    this.v1Client.setAsync(this.address, new TestResponseListener(),
        "handle", NON_V1_BINDING);
  }

  @Test
  public void testSetSync() throws IOException {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final VariableBinding binding = new VariableBinding(
          SnmpConstants.sysLocation, new OctetString("SECRET SITE #"
              + Integer.toString(new Random().nextInt(100))));
      final ResponseEvent response = client
          .setSync(this.address, binding);
      Assert.assertNotNull(response);
      Assert.assertTrue(ResponseEvents.extractBindings(response)
          .containsKey(SnmpConstants.sysLocation));
    }
  }

  @Test(expected = IllegalArgumentException.class)
  public void testSetSyncNonV1() throws IOException {
    this.v1Client.setSync(this.address, NON_V1_BINDING);
  }

  @Test
  public void testTrap() throws IOException {
    this.v1Client.trap(this.address, new OID(new int[] { 1, 3, 6, 1, 6, 3,
        1, 1, 5 }), PDUv1.WARMSTART, 0, new VariableBinding(new OID(
        new int[] { 1, 3, 6, 1, 6, 3, 1, 1, 5, 3 }), new Integer32(7)));
  }

  @Test(expected = IllegalArgumentException.class)
  public void testTrapNonV1() throws IOException {
    this.v1Client.trap(this.address, new OID(new int[] { 1, 3, 6, 1, 6, 3,
        1, 1, 5 }), PDUv1.WARMSTART, 0, NON_V1_BINDING);
  }

  @Test
  public void testWalkAsync() {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final TestTreeListener testListener = new TestTreeListener();

      client.walkAsync(this.address, testListener, "handle", SYS_ORID);
      testListener.waitUntilFinished();
      Assert.assertNotNull(testListener.getEvents());
      Assert.assertTrue(testListener.getEvents().size() > 0);
      testListener.reset();
    }
  }

  @Test
  public void testWalkSync() {

    for (final SNMP<UdpAddress> client : this.allClients) {

      final List<TreeEvent> events = client.walkSync(this.address,
          SYS_ORID);
      Assert.assertNotNull(events);
      Assert.assertTrue(events.size() > 0);
    }
  }
}
TOP

Related Classes of org.chiefly.sunlamp.LiveSNMPTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.