Example #1
0
  public BaseTestCase(String schemaType, int schemaVersion) {

    DhcpServerConfiguration.configFilename = configFilename;
    if (config == null) {
      try {
        config = DhcpServerConfiguration.getInstance();

        DhcpServerPolicies.setProperty(Property.DATABASE_SCHEMA_TYTPE, schemaType);
        DhcpServerPolicies.setProperty(
            Property.DATABASE_SCHEMA_VERSION, Integer.toString(schemaVersion));
        if (schemaType.contains("derby")) {
          // start with a fresh database
          System.out.println("Cleaning db/derby...");
          FileUtils.cleanDirectory(new File("db/derby"));
        } else if (schemaType.contains("h2")) {
          // start with a fresh database
          System.out.println("Cleaning db/h2...");
          FileUtils.cleanDirectory(new File("db/h2"));
        } else if (schemaType.contains("sqlite")) {
          // start with a fresh database
          System.out.println("Cleaning db/sqlite...");
          FileUtils.cleanDirectory(new File("db/sqlite"));
        }
        String[] appContext = JagornetDhcpServer.getAppContextFiles(schemaType, schemaVersion);

        ctx = new ClassPathXmlApplicationContext(appContext);

        IaManager iaMgr = (IaManager) ctx.getBean("iaManager");
        iaMgr.init();
        config.setIaMgr(iaMgr);

        V6NaAddrBindingManager v6NaAddrBindingMgr =
            (V6NaAddrBindingManager) ctx.getBean("v6NaAddrBindingManager");
        v6NaAddrBindingMgr.init();
        config.setNaAddrBindingMgr(v6NaAddrBindingMgr);

        V6TaAddrBindingManager v6TaAddrBindingMgr =
            (V6TaAddrBindingManager) ctx.getBean("v6TaAddrBindingManager");
        v6TaAddrBindingMgr.init();
        config.setTaAddrBindingMgr(v6TaAddrBindingMgr);

        V6PrefixBindingManager v6PrefixBindingMgr =
            (V6PrefixBindingManager) ctx.getBean("v6PrefixBindingManager");
        v6PrefixBindingMgr.init();
        config.setPrefixBindingMgr(v6PrefixBindingMgr);

        V4AddrBindingManager v4AddrBindingMgr =
            (V4AddrBindingManager) ctx.getBean("v4AddrBindingManager");
        v4AddrBindingMgr.init();
        config.setV4AddrBindingMgr(v4AddrBindingMgr);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
  }
 /**
  * Gets the next available address in this address pool.
  *
  * @return the next available address
  */
 public InetAddress getNextAvailableAddress() {
   if (freeList != null) {
     BigInteger next = freeList.getNextFree();
     if (next != null) {
       try {
         InetAddress ip = InetAddress.getByAddress(next.toByteArray());
         int pingCheckTimeout =
             DhcpServerPolicies.globalPolicyAsInt(Property.V4_PINGCHECK_TIMEOUT);
         if (pingCheckTimeout > 0) {
           try {
             if (ip.isReachable(pingCheckTimeout)) {
               log.warn("Next free address answered ping check: " + ip.getHostAddress());
               setUsed(ip);
               return getNextAvailableAddress(); // try again
             }
           } catch (IOException ex) {
             log.error("Failed to perform v4 ping check: " + ex);
           }
         }
         return ip;
       } catch (UnknownHostException ex) {
         log.error("Unable to build IPv4 address from next free: " + ex);
       }
     }
   }
   return null;
 }
  /**
   * Test rebind no binding.
   *
   * @throws Exception the exception
   */
  public void testRebindNoBinding() throws Exception {
    // set the default server policy
    DhcpServerPolicies.getProperties().put(Property.VERIFY_UNKNOWN_REBIND.key(), "false");

    DhcpV6Message requestMsg = buildNaRequestMessage(firstPoolAddr);
    requestMsg.setMessageType(DhcpConstants.V6MESSAGE_TYPE_REBIND);

    DhcpV6RebindProcessor processor =
        new DhcpV6RebindProcessor(requestMsg, requestMsg.getRemoteAddress().getAddress());

    DhcpV6Message replyMsg = processor.processMessage();

    assertNull(replyMsg);
  }
  /**
   * Test rebind no binding with verify.
   *
   * @throws Exception the exception
   */
  public void testRebindNoBindingWithVerify() throws Exception {
    // set the default server policy
    DhcpServerPolicies.getProperties().put(Property.VERIFY_UNKNOWN_REBIND.key(), "true");

    DhcpV6Message requestMsg = buildNaRequestMessage(firstPoolAddr);
    requestMsg.setMessageType(DhcpConstants.V6MESSAGE_TYPE_REBIND);
    DhcpV6IaAddrOption dhcpIaAddr = new DhcpV6IaAddrOption();
    dhcpIaAddr.setIpAddress("2001:DB8:2::1");
    requestMsg.getIaNaOptions().iterator().next().getIaAddrOptions().add(dhcpIaAddr);

    DhcpV6RebindProcessor processor =
        new DhcpV6RebindProcessor(requestMsg, requestMsg.getRemoteAddress().getAddress());

    DhcpV6Message replyMsg = processor.processMessage();

    assertNotNull(replyMsg);
    assertEquals(requestMsg.getTransactionId(), replyMsg.getTransactionId());
    assertEquals(DhcpConstants.V6MESSAGE_TYPE_REPLY, replyMsg.getMessageType());

    Collection<DhcpOption> dhcpOptions = replyMsg.getDhcpOptions();
    assertNotNull(dhcpOptions);
    assertEquals(2, dhcpOptions.size());

    DhcpV6ClientIdOption _clientIdOption =
        (DhcpV6ClientIdOption) replyMsg.getDhcpOption(DhcpConstants.V6OPTION_CLIENTID);
    assertNotNull(_clientIdOption);

    DhcpV6ServerIdOption _serverIdOption =
        (DhcpV6ServerIdOption) replyMsg.getDhcpOption(DhcpConstants.V6OPTION_SERVERID);
    assertNotNull(_serverIdOption);

    DhcpV6IaNaOption _iaNaOption = replyMsg.getIaNaOptions().get(0);
    assertNotNull(_iaNaOption);

    DhcpV6IaAddrOption _iaAddrOption = _iaNaOption.getIaAddrOptions().get(0);
    assertNotNull(_iaAddrOption);
    assertNotNull(_iaAddrOption.getInetAddress());
    assertEquals(InetAddress.getByName("2001:DB8:2::1"), _iaAddrOption.getInetAddress());
    assertEquals(0, _iaAddrOption.getPreferredLifetime());
    assertEquals(0, _iaAddrOption.getValidLifetime());
  }
  /**
   * Test zero lifetimes.
   *
   * @throws Exception the exception
   */
  public void testZeroLifetimes() throws Exception {
    DhcpV6Message requestMsg = buildNaRequestMessage(firstPoolAddr);
    requestMsg.setMessageType(DhcpConstants.V6MESSAGE_TYPE_SOLICIT);

    DhcpV6SolicitProcessor sProc =
        new DhcpV6SolicitProcessor(requestMsg, requestMsg.getRemoteAddress().getAddress());

    DhcpV6Message advertiseMsg = sProc.processMessage();

    assertNotNull(advertiseMsg);

    // use the ADVERTISE message to create the REQUEST message
    advertiseMsg.setMessageType(DhcpConstants.V6MESSAGE_TYPE_REQUEST);
    DhcpV6RequestProcessor rProc =
        new DhcpV6RequestProcessor(advertiseMsg, advertiseMsg.getRemoteAddress().getAddress());

    DhcpV6Message replyMsg = rProc.processMessage();

    assertNotNull(replyMsg);

    System.out.println("Sleeping before rebind...");
    Thread.sleep(2000);

    // convert the reply into a rebind request
    replyMsg.setMessageType(DhcpConstants.V6MESSAGE_TYPE_REBIND);
    // null out the ServerId option for a rebind
    replyMsg.getDhcpOptionMap().remove(DhcpConstants.V6OPTION_SERVERID);
    // hack the returned reply to request an off-link address
    replyMsg
        .getIaNaOptions()
        .iterator()
        .next()
        .getIaAddrOptions()
        .iterator()
        .next()
        .setIpAddress("2001:DB8:2::1");

    DhcpV6RebindProcessor nProc =
        new DhcpV6RebindProcessor(replyMsg, replyMsg.getRemoteAddress().getAddress());

    // set the server policy to allow it to verify this unknown rebind
    DhcpServerPolicies.setProperty(Property.VERIFY_UNKNOWN_REBIND, "true");

    replyMsg = nProc.processMessage();

    assertNotNull(replyMsg);
    assertEquals(requestMsg.getTransactionId(), replyMsg.getTransactionId());
    assertEquals(DhcpConstants.V6MESSAGE_TYPE_REPLY, replyMsg.getMessageType());

    //		checkReply(replyMsg, null, null, 0);
    Collection<DhcpOption> dhcpOptions = replyMsg.getDhcpOptions();
    assertNotNull(dhcpOptions);
    assertEquals(2, dhcpOptions.size());

    DhcpV6ClientIdOption _clientIdOption =
        (DhcpV6ClientIdOption) replyMsg.getDhcpOption(DhcpConstants.V6OPTION_CLIENTID);
    assertNotNull(_clientIdOption);

    DhcpV6ServerIdOption _serverIdOption =
        (DhcpV6ServerIdOption) replyMsg.getDhcpOption(DhcpConstants.V6OPTION_SERVERID);
    assertNotNull(_serverIdOption);

    DhcpV6IaNaOption _iaNaOption = replyMsg.getIaNaOptions().get(0);
    assertNotNull(_iaNaOption);

    DhcpV6IaAddrOption _iaAddrOption = _iaNaOption.getIaAddrOptions().get(0);
    assertNotNull(_iaAddrOption);
    assertNotNull(_iaAddrOption.getInetAddress());
    assertEquals(0, _iaAddrOption.getPreferredLifetime());
    assertEquals(0, _iaAddrOption.getValidLifetime());
  }