@Test
 public void testInMemoryLDAPSetup() throws Exception {
   // Get a plain connection (unencrypted) to the server's LDAP listener
   LDAPConnection conn = ldapServer.getConnection("LDAP");
   LDAPTestUtils.assertEntryExists(conn, "cn=Barbara Jensen,ou=People,dc=example,dc=com");
   conn.close();
 }
  @Test
  public void testGetFieldValue() throws Exception {
    LDAPConnection conn = ldapServer.getConnection();
    SearchResultEntry item =
        conn.searchForEntry("ou=People,dc=example,dc=com", SearchScope.SUB, "uid=bjensen");
    assertNotNull(item);

    LDAPDataProvider dp = new LDAPDataProvider();
    assertEquals("12345", dp.getFieldValue(item, "postalCode"));

    assertEquals("valid12345", dp.getFieldValue(item, "valid${postalCode}"));

    assertEquals("invalidAttribute", dp.getFieldValue(item, "invalidAttribute"));

    assertEquals("BJ", dp.getFieldValue(item, "${initials}"));
    assertEquals(" BJ ", dp.getFieldValue(item, " ${initials} "));
    assertEquals(" BJ arbitrary string", dp.getFieldValue(item, " ${initials} arbitrary string"));

    assertEquals(
        " BJ Web Developer arbitrary string",
        dp.getFieldValue(item, " ${initials} ${title} arbitrary string"));
    assertEquals(
        " BJWeb Developerarbitrary string",
        dp.getFieldValue(item, " ${initials}${title}arbitrary string"));
  }
  @Test
  public void testFromSearchResultEntry() throws Exception {
    LDAPDataProvider dp = new LDAPDataProvider();
    Configuration cfg = Configuration.getInstance();

    LDAPConnection conn = ldapServer.getConnection();
    SearchResultEntry item =
        conn.searchForEntry(
            cfg.getLDAPConfiguration().getUserBaseDN(), SearchScope.SUB, "uid=bjensen");

    List<Treaty> t =
        new ArrayList<Treaty>() {
          {
            add(Treaty.CMS);
            add(Treaty.AEWA);
          }
        };

    LDAPConfiguration ldap = cfg.getLDAPConfiguration();
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_PREFIX, "personalTitle");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_COUNTRY, "c");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_FIRST_NAME, "givenName");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_LAST_NAME, "sn");
    ldap.setMapping(
        LDAPConfiguration.LDAP_MAPPING_ADDRESS, "Address: ${registeredAddress} ${postalCode}");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_DEPARTMENT, "ou");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_EMAIL, "mail");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_FAX, "facsimileTelephoneNumber");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_INSTITUTION, "o");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_PHONE, "telephoneNumber");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_POSITION, "title");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_UPDATED, "roomNumber");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_TREATIES, "carLicense");
    cfg.save();

    IContact c = dp.fromSearchResultEntry(item);

    assertEquals("Address: 5th Avenue, NY 12345", c.getAddress());
    assertEquals("bjensen", c.getId());
    assertEquals("RO", c.getCountry());
    assertEquals("Software Development", c.getDepartment());
    assertEquals("*****@*****.**", c.getEmail());
    assertEquals("+1 408 555 1992", c.getFax());
    assertEquals("Barbara", c.getFirstName());
    assertEquals("Eau de Web", c.getInstitution());
    assertEquals("Jensen", c.getLastName());
    assertEquals("+1 408 555 1862", c.getPhoneNumber());
    assertEquals("Web Developer", c.getPosition());
    assertEquals("H.E.", c.getPrefix());
    assertEquals(new Short((short) 1), c.getProtocolVersion());
    assertEquals(t, c.getTreaties());

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+2"));
    cal.set(2006, 11, 25, 16, 45, 22);
    cal.set(Calendar.MILLISECOND, 0);
    Date d = cal.getTime();
    Date updated = c.getUpdated();
    assertEquals(updated, d);
  }
 @Test
 public void testOpenResources() {
   LDAPDataProvider dp = new LDAPDataProvider();
   dp.openResources();
   LDAPConnection conn = dp.getConnection();
   assertNotNull(conn);
   assertTrue(conn.isConnected());
   dp.closeResources();
 }
  public boolean connect() throws LDAPException, GeneralSecurityException {

    SSLUtil sslUtil = new SSLUtil(new TrustAllTrustManager());
    LDAPConnectionOptions options = new LDAPConnectionOptions();
    options.setUseKeepAlive(true);
    mConnection = new LDAPConnection(options);
    mConnection.setSocketFactory(createSocketFactory());
    mConnection.connect(HOST, PORT);

    return true;
  }
 public boolean authenticate(String pid, String pwd) throws LDAPException {
   Filter f = Filter.create("(mail=" + pid + ')');
   SearchRequest request = new SearchRequest(BASE_DN, SearchScope.SUB, f);
   SearchResult searchResult = mConnection.search(request);
   if (searchResult.getEntryCount() == 0) return false;
   SearchResultEntry e = searchResult.getSearchEntries().get(0);
   String uid = e.getAttribute("uid").getValue();
   Log.d(LOG_TAG, uid);
   BindRequest bindRequest = new SimpleBindRequest(e.getDN(), pwd);
   BindResult result = mConnection.bind(bindRequest);
   return result.getResultCode() == ResultCode.SUCCESS;
 }
  @Test(expected = InvalidValueException.class)
  public void testFromSearchResultEntryMissingId() throws Exception {
    LDAPDataProvider dp = new LDAPDataProvider();
    Configuration cfg = Configuration.getInstance();

    LDAPConnection conn = ldapServer.getConnection();
    SearchResultEntry item =
        conn.searchForEntry(
            cfg.getLDAPConfiguration().getUserBaseDN(), SearchScope.SUB, "uid=bjensen");
    cfg.getLDAPConfiguration().setMapping(LDAPConfiguration.LDAP_MAPPING_ID, "unknown");
    dp.fromSearchResultEntry(item);
  }
Example #8
0
 /** {@inheritDoc} */
 @Override()
 public synchronized void destroyThread() {
   if (conn != null) {
     conn.close();
     conn = null;
   }
 }
Example #9
0
 /** {@inheritDoc} */
 @Override()
 public void finalizeThread() {
   if (conn != null) {
     conn.close();
     conn = null;
   }
 }
Example #10
0
  /** {@inheritDoc} */
  @Override()
  protected boolean testNonLDAPJobParameters(
      final ParameterList parameters,
      final LDAPConnection connection,
      final ArrayList<String> outputMessages) {
    boolean successful = true;

    // Ensure that the parent DN exists.
    StringParameter parentDNParam = parameters.getStringParameter(parentDNParameter.getName());
    if ((parentDNParam != null) && parentDNParam.hasValue()) {
      try {
        String base = parentDNParam.getStringValue();
        outputMessages.add("Ensuring that parent entry '" + base + "' exists....");
        SearchResultEntry e = connection.getEntry(base);
        if (e == null) {
          outputMessages.add("ERROR:  The parent entry does not exist.");
          successful = false;
        } else {
          outputMessages.add("The parent entry exists.");
        }
      } catch (Exception e) {
        successful = false;
        outputMessages.add("Unable to perform the search:  " + stackTraceToString(e));
      }

      outputMessages.add("");
    }

    return successful;
  }
Example #11
0
 @Override
 public void execute(LDAPConnection connection) throws NetworkErrorException {
   try {
     this.result = new LDAPResult(connection.add(m_dn, m_attributes));
   } catch (LDAPException e) {
     this.result = new LDAPResult(e.toLDAPResult());
     if (isNetworkError(e.getResultCode())) {
       throw new NetworkErrorException(e.getMessage());
     }
   }
 }
Example #12
0
  /** {@inheritDoc} */
  @Override()
  public void runJob() {
    String jobID = getJobID();
    String prefix = rdnAttribute + '=' + rdnValuePrefix;
    String dnSuffix = rdnValueSuffix + ',' + parentDN;
    String newRDNSuffix = rdnValueSuffix + '-' + jobID;

    StringBuilder dnBuffer = new StringBuilder();
    StringBuilder newRDNBuffer = new StringBuilder();

    modDNsCompleted.startTracker();
    modDNTimer.startTracker();
    resultCodes.startTracker();
    modDNsExceedingThreshold.startTracker();

    // Iterate through the entries to rename them to include the job ID.
    while (!shouldStop()) {
      if (rateLimiter != null) {
        if (rateLimiter.await()) {
          continue;
        }
      }

      long opStartTime = System.currentTimeMillis();
      int i = entryNumber.getAndIncrement();
      if (i > upperBound) {
        break;
      }

      dnBuffer.setLength(0);
      dnBuffer.append(prefix);
      dnBuffer.append(i);
      dnBuffer.append(dnSuffix);

      newRDNBuffer.setLength(0);
      newRDNBuffer.append(prefix);
      newRDNBuffer.append(i);
      newRDNBuffer.append(newRDNSuffix);

      modDNTimer.startTimer();

      try {
        LDAPResult modDNResult = conn.modifyDN(dnBuffer.toString(), newRDNBuffer.toString(), true);
        resultCodes.increment(modDNResult.getResultCode().toString());
      } catch (LDAPException le) {
        resultCodes.increment(le.getResultCode().toString());
      } finally {
        modDNTimer.stopTimer();
        modDNsCompleted.increment();

        if ((responseTimeThreshold > 0)
            && (modDNTimer.getLastOperationTime() > responseTimeThreshold)) {
          modDNsExceedingThreshold.increment();
        }
      }

      if (timeBetweenRequests > 0) {
        long elapsedTime = System.currentTimeMillis() - opStartTime;
        long sleepTime = timeBetweenRequests - elapsedTime;
        if (sleepTime > 0) {
          try {
            Thread.sleep(sleepTime);
          } catch (Exception e) {
          }
        }
      }
    }

    // Wait until all of the threads have completed the first modify DN before
    // starting the second.
    int remainingActive = activeThreads.decrementAndGet();
    if (remainingActive == 0) {
      entryNumber.set(lowerBound);
      activeThreads.decrementAndGet();
    } else {
      while (activeThreads.get() >= 0) {
        try {
          Thread.sleep(0, 1);
        } catch (Exception e) {
        }
      }
    }

    // Iterate through the entries to rename them to remove the job ID.
    dnSuffix = rdnValueSuffix + '-' + jobID + ',' + parentDN;
    newRDNSuffix = rdnValueSuffix;
    while (!shouldStop()) {
      if (rateLimiter != null) {
        if (rateLimiter.await()) {
          continue;
        }
      }

      long opStartTime = System.currentTimeMillis();
      int i = entryNumber.getAndIncrement();
      if (i > upperBound) {
        break;
      }

      dnBuffer.setLength(0);
      dnBuffer.append(prefix);
      dnBuffer.append(i);
      dnBuffer.append(dnSuffix);

      newRDNBuffer.setLength(0);
      newRDNBuffer.append(prefix);
      newRDNBuffer.append(i);
      newRDNBuffer.append(newRDNSuffix);

      modDNTimer.startTimer();

      try {
        LDAPResult modDNResult = conn.modifyDN(dnBuffer.toString(), newRDNBuffer.toString(), true);
        resultCodes.increment(modDNResult.getResultCode().toString());
      } catch (LDAPException le) {
        resultCodes.increment(le.getResultCode().toString());
      } finally {
        modDNTimer.stopTimer();
        modDNsCompleted.increment();

        if ((responseTimeThreshold > 0)
            && (modDNTimer.getLastOperationTime() > responseTimeThreshold)) {
          modDNsExceedingThreshold.increment();
        }
      }

      if (timeBetweenRequests > 0) {
        long elapsedTime = System.currentTimeMillis() - opStartTime;
        long sleepTime = timeBetweenRequests - elapsedTime;
        if (sleepTime > 0) {
          try {
            Thread.sleep(sleepTime);
          } catch (Exception e) {
          }
        }
      }
    }

    modDNsCompleted.stopTracker();
    modDNTimer.stopTracker();
    resultCodes.stopTracker();
    modDNsExceedingThreshold.stopTracker();
  }
Example #13
0
 @Override
 com.unboundid.ldap.sdk.LDAPResult doExecute(
     LDAPConnection connection, UpdatableLDAPRequest request) throws LDAPException {
   return connection.add((AddRequest) request);
 }