Active Directory (AD) is Microsoft's proprietary directory service, today most of the organization's use AD services for user management and administration.
For Performance testing active directory application's, we should know about the protocol used, Active directory uses LDAP protocol versions 2 and 3, LDAP stands for lightweight directory access protocol, is a communications protocol that defines the methods in which a directory service can access and modify centrally stored information over a network.
LDAP adheres to the usual client/server paradigm. A typical interaction between the client and the server goes like this,
- LDAP Client application connect to the LDAP server, in technical terms this process is called as "bind" to a server, based on the access restrictions configured on the server, the LDAP server either accepts or refuses the bind request
- If bind successful, client can perform CRUD(create/read/update/delete) operations on the directory server based on the access permissions
- Once done with the task, LDAP Client application can close the connection to the server, in technical terms this process is called as "un-bind" to a server
In LoadRunner we have multiple options to test LDAP based application, first and recommended option is to use LDAP protocol if you have license for that, which comes with built in methods to perform bind, unbind and all other CRUD operations, and second option is to use template VUser to write custom code.
This blog post shows my preferred approach, write some simple java code to interact with LDAP server, for this post I am considering below flow,
1. Bind to the LDAP server
2. Search and retrieve user info from LDAP server
3. Unbind from LDAP server
In java, we have various libraries to communicate with LDAP server, here I am using built in "javax.naming.directory" package, so no additional libraries needed.
Bind to the LDAP server:: below method binds user to AD server,
public DirContext establishConnection(String url, String user, String password) {
DirContext bindCtx = null;
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, user);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
lr.start_transaction("Bind User");
bindCtx = new InitialDirContext(env);
lr.end_transaction("Bind User",lr.PASS);
lr.output_message("User Authenticated! " + ctx.getEnvironment());
} catch (AuthenticationException ex) {
lr.end_transaction("Bind User",lr.FAIL);
System.out.println(ex.getMessage());
} catch (NamingException e) {
lr.end_transaction("Bind User",lr.FAIL);
e.printStackTrace();
}
return bindCtx;
}
Search User : This method executes an LDAP search query against the server once the bind has been successful and prints attributes . The filter and search object will depend on what you are trying to retrieve,
public String searchUsers(DirContext bindCtx, String searchObj, String user) {
String userDn = null;
String searchFilter = "(samaccountname=" + user + ")";
String[] reqAtt = { "cn", "mail", "sharepointgroup" };
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setReturningAttributes(reqAtt);
NamingEnumeration<SearchResult> users;
SearchResult result = null;
try {
lr.start_transaction("Search User");
users = bindCtx.search(searchObj, searchFilter, controls);
lr.end_transaction("Search User",lr.PASS);
if (users.hasMore()) {
result = (SearchResult) users.next();
userDn = result.getNameInNamespace();
Attributes attr = result.getAttributes();
System.out.println(attr.get("cn"));
System.out.println(attr.get("mail"));
System.out.println(attr.get("sharepointgroup"));
System.out.println(userDn);
}
} catch (NamingException e) {
lr.end_transaction("Search User",lr.FAIL);
e.printStackTrace();
}
return userDn;
}
Un-Bind to the LDAP server:: below method unbinds(disconnects) the binded user using Bind context object.
public void closeConnection(DirContext bindCtx) {
try {
lr.start_transaction("UnBind User");
bindCtx.close();
lr.end_transaction("UnBind User",lr.PASS);
} catch (NamingException e) {
lr.end_transaction("UnBind User",lr.FAIL);
e.printStackTrace();
}
}
For complete Vugen script please refer my GitHub Link