001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.ldap.sampler;
020:
021: import javax.naming.NamingException;
022: import javax.naming.directory.Attribute;
023: import javax.naming.directory.BasicAttribute;
024: import javax.naming.directory.BasicAttributes;
025: import javax.naming.directory.DirContext;
026: import javax.naming.directory.ModificationItem;
027:
028: import org.apache.jmeter.config.Argument;
029: import org.apache.jmeter.config.Arguments;
030: import org.apache.jmeter.config.ConfigTestElement;
031: import org.apache.jmeter.samplers.AbstractSampler;
032: import org.apache.jmeter.samplers.Entry;
033: import org.apache.jmeter.samplers.SampleResult;
034: import org.apache.jmeter.testelement.property.BooleanProperty;
035: import org.apache.jmeter.testelement.property.PropertyIterator;
036: import org.apache.jmeter.testelement.property.StringProperty;
037: import org.apache.jmeter.testelement.property.TestElementProperty;
038: import org.apache.jorphan.logging.LoggingManager;
039: import org.apache.log.Logger;
040:
041: /**
042: * Ldap Sampler class is main class for the LDAP test. This will control all the
043: * test available in the LDAP Test.
044: *
045: * @author T.Elanjchezhiyan(chezhiyan@siptech.co.in) - Sip Technologies and
046: * Exports Ltd. Created Apr 29 2003 11:00 AM
047: */
048: public class LDAPSampler extends AbstractSampler {
049: transient private static Logger log = LoggingManager
050: .getLoggerForClass();
051:
052: public final static String SERVERNAME = "servername"; //$NON-NLS-1$
053:
054: public final static String PORT = "port"; //$NON-NLS-1$
055:
056: public final static String ROOTDN = "rootdn"; //$NON-NLS-1$
057:
058: public final static String TEST = "test"; //$NON-NLS-1$
059:
060: public final static String ADD = "add"; //$NON-NLS-1$
061:
062: public final static String MODIFY = "modify"; //$NON-NLS-1$
063:
064: public final static String DELETE = "delete"; //$NON-NLS-1$
065:
066: public final static String SEARCHBASE = "search"; //$NON-NLS-1$
067:
068: public final static String SEARCHFILTER = "searchfilter"; //$NON-NLS-1$
069:
070: public final static String USER_DEFINED = "user_defined"; //$NON-NLS-1$
071:
072: public final static String ARGUMENTS = "arguments"; //$NON-NLS-1$
073:
074: public final static String BASE_ENTRY_DN = "base_entry_dn"; //$NON-NLS-1$
075:
076: // For In build test case using this counter
077: // create the new entry in the server
078: private static int counter = 0;
079:
080: private boolean searchFoundEntries;// TODO turn into parameter?
081:
082: public LDAPSampler() {
083: }
084:
085: /**
086: * Gets the username attribute of the LDAP object.
087: *
088: * @return the username
089: */
090: public String getUsername() {
091: return getPropertyAsString(ConfigTestElement.USERNAME);
092: }
093:
094: /**
095: * Gets the password attribute of the LDAP object.
096: *
097: * @return the password
098: */
099: public String getPassword() {
100: return getPropertyAsString(ConfigTestElement.PASSWORD);
101: }
102:
103: /**
104: * Sets the Servername attribute of the ServerConfig object.
105: *
106: * @param servername
107: * the new servername value
108: */
109: public void setServername(String servername) {
110: setProperty(new StringProperty(SERVERNAME, servername));
111: }
112:
113: /**
114: * Sets the Port attribute of the ServerConfig object.
115: *
116: * @param port
117: * the new Port value
118: */
119: public void setPort(String port) {
120: setProperty(new StringProperty(PORT, port));
121: }
122:
123: /**
124: * Gets the servername attribute of the LDAPSampler object.
125: *
126: * @return the Servername value
127: */
128: public String getServername() {
129: return getPropertyAsString(SERVERNAME);
130: }
131:
132: /**
133: * Gets the Port attribute of the LDAPSampler object.
134: *
135: * @return the Port value
136: */
137: public String getPort() {
138: return getPropertyAsString(PORT);
139: }
140:
141: /**
142: * Sets the Rootdn attribute of the LDAPSampler object.
143: *
144: * @param newRootdn
145: * the new rootdn value
146: */
147: public void setRootdn(String newRootdn) {
148: this .setProperty(ROOTDN, newRootdn);
149: }
150:
151: /**
152: * Gets the Rootdn attribute of the LDAPSampler object.
153: *
154: * @return the Rootdn value
155: */
156: public String getRootdn() {
157: return getPropertyAsString(ROOTDN);
158: }
159:
160: /**
161: * Sets the Test attribute of the LdapConfig object.
162: *
163: * @param newTest
164: * the new test value(Add,Modify,Delete and search)
165: */
166: public void setTest(String newTest) {
167: this .setProperty(TEST, newTest);
168: }
169:
170: /**
171: * Gets the test attribute of the LDAPSampler object.
172: *
173: * @return the test value (Add, Modify, Delete and search)
174: */
175: public String getTest() {
176: return getPropertyAsString(TEST);
177: }
178:
179: /**
180: * Sets the UserDefinedTest attribute of the LDAPSampler object.
181: *
182: * @param value
183: * the new UserDefinedTest value
184: */
185: public void setUserDefinedTest(boolean value) {
186: setProperty(new BooleanProperty(USER_DEFINED, value));
187: }
188:
189: /**
190: * Gets the UserDefinedTest attribute of the LDAPSampler object.
191: *
192: * @return the test value true or false. If true it will do the
193: * UserDefinedTest else our own inbuild test case.
194: */
195: public boolean getUserDefinedTest() {
196: return getPropertyAsBoolean(USER_DEFINED);
197: }
198:
199: /**
200: * Sets the Base Entry DN attribute of the LDAPSampler object.
201: *
202: * @param newbaseentry
203: * the new Base entry DN value
204: */
205: public void setBaseEntryDN(String newbaseentry) {
206: setProperty(new StringProperty(BASE_ENTRY_DN, newbaseentry));
207: }
208:
209: /**
210: * Gets the BaseEntryDN attribute of the LDAPSampler object.
211: *
212: * @return the Base entry DN value
213: */
214: public String getBaseEntryDN() {
215: return getPropertyAsString(BASE_ENTRY_DN);
216: }
217:
218: /**
219: * Sets the Arguments attribute of the LdapConfig object. This will collect
220: * values from the table for user defined test case.
221: *
222: * @param value
223: * the arguments
224: */
225: public void setArguments(Arguments value) {
226: setProperty(new TestElementProperty(ARGUMENTS, value));
227: }
228:
229: /**
230: * Gets the Arguments attribute of the LdapConfig object.
231: *
232: * @return the arguments. User defined test case.
233: */
234: public Arguments getArguments() {
235: return (Arguments) getProperty(ARGUMENTS).getObjectValue();
236: }
237:
238: /**
239: * Collect all the value from the table (Arguments), using this create the
240: * basicAttributes. This will create the Basic Attributes for the User
241: * defined TestCase for Add Test.
242: *
243: * @return the BasicAttributes
244: */
245: private BasicAttributes getUserAttributes() {
246: BasicAttribute basicattribute = new BasicAttribute(
247: "objectclass"); //$NON-NLS-1$
248: basicattribute.add("top"); //$NON-NLS-1$
249: basicattribute.add("person"); //$NON-NLS-1$
250: basicattribute.add("organizationalPerson"); //$NON-NLS-1$
251: basicattribute.add("inetOrgPerson"); //$NON-NLS-1$
252: BasicAttributes attrs = new BasicAttributes(true);
253: attrs.put(basicattribute);
254: BasicAttribute attr;
255: PropertyIterator iter = getArguments().iterator();
256:
257: while (iter.hasNext()) {
258: Argument item = (Argument) iter.next().getObjectValue();
259: attr = getBasicAttribute(item.getName(), item.getValue());
260: attrs.put(attr);
261: }
262: return attrs;
263: }
264:
265: /**
266: * Collect all the value from the table (Arguments), using this create the
267: * basicAttributes. This will create the Basic Attributes for the User
268: * defined TestCase for Modify test.
269: *
270: * @return the BasicAttributes
271: */
272: private ModificationItem[] getUserModAttributes() {
273: ModificationItem[] mods = new ModificationItem[getArguments()
274: .getArguments().size()];
275: BasicAttribute attr;
276: PropertyIterator iter = getArguments().iterator();
277: int count = 0;
278: while (iter.hasNext()) {
279: Argument item = (Argument) iter.next().getObjectValue();
280: attr = getBasicAttribute(item.getName(), item.getValue());
281: mods[count] = new ModificationItem(
282: DirContext.REPLACE_ATTRIBUTE, attr);
283: count = +1;
284: }
285: return mods;
286: }
287:
288: /**
289: * This will create the Basic Attributes for the Inbuilt TestCase for Modify
290: * test.
291: *
292: * @return the BasicAttributes
293: */
294: private ModificationItem[] getModificationItem() {
295: ModificationItem[] mods = new ModificationItem[2];
296: // replace (update) attribute
297: Attribute mod0 = new BasicAttribute("userpassword", "secret"); //$NON-NLS-1$ //$NON-NLS-2$
298: // add mobile phone number attribute
299: Attribute mod1 = new BasicAttribute("mobile", "123-456-1234"); //$NON-NLS-1$ //$NON-NLS-2$
300:
301: mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
302: mod0);
303: mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);
304:
305: return mods;
306: }
307:
308: /**
309: * This will create the Basic Attributes for the In build TestCase for Add
310: * Test.
311: *
312: * @return the BasicAttributes
313: */
314: private BasicAttributes getBasicAttributes() {
315: BasicAttributes basicattributes = new BasicAttributes();
316: BasicAttribute basicattribute = new BasicAttribute(
317: "objectclass"); //$NON-NLS-1$
318: basicattribute.add("top"); //$NON-NLS-1$
319: basicattribute.add("person"); //$NON-NLS-1$
320: basicattribute.add("organizationalPerson"); //$NON-NLS-1$
321: basicattribute.add("inetOrgPerson"); //$NON-NLS-1$
322: basicattributes.put(basicattribute);
323: String s1 = "User"; //$NON-NLS-1$
324: String s3 = "Test"; //$NON-NLS-1$
325: String s5 = "user"; //$NON-NLS-1$
326: String s6 = "test"; //$NON-NLS-1$
327: counter += 1;
328: basicattributes.put(new BasicAttribute("givenname", s1)); //$NON-NLS-1$
329: basicattributes.put(new BasicAttribute("sn", s3)); //$NON-NLS-1$
330: basicattributes.put(new BasicAttribute(
331: "cn", "TestUser" + counter)); //$NON-NLS-1$ //$NON-NLS-2$
332: basicattributes.put(new BasicAttribute("uid", s5)); //$NON-NLS-1$
333: basicattributes.put(new BasicAttribute("userpassword", s6)); //$NON-NLS-1$
334: setProperty(new StringProperty(ADD, "cn=TestUser" + counter)); //$NON-NLS-1$
335: return basicattributes;
336: }
337:
338: /**
339: * This will create the Basic Attribute for the given name value pair.
340: *
341: * @return the BasicAttribute
342: */
343: private BasicAttribute getBasicAttribute(String name, String value) {
344: BasicAttribute attr = new BasicAttribute(name, value);
345: return attr;
346: }
347:
348: /**
349: * Returns a formatted string label describing this sampler
350: *
351: * @return a formatted string label describing this sampler
352: */
353: public String getLabel() {
354: return ("ldap://" + this .getServername() + ":" + getPort()
355: + "/" + this .getRootdn());
356: }
357:
358: /**
359: * This will do the add test for the User defined TestCase as well as
360: * inbuilt test case.
361: *
362: */
363: private void addTest(LdapClient ldap, SampleResult res)
364: throws NamingException {
365: if (getPropertyAsBoolean(USER_DEFINED)) {
366: res.sampleStart();
367: ldap.createTest(getUserAttributes(),
368: getPropertyAsString(BASE_ENTRY_DN));
369: res.sampleEnd();
370: } else {
371: res.sampleStart();
372: ldap.createTest(getBasicAttributes(),
373: getPropertyAsString(ADD));
374: res.sampleEnd();
375: ldap.deleteTest(getPropertyAsString(ADD));
376: }
377: }
378:
379: /**
380: * This will do the delete test for the User defined TestCase as well as
381: * inbuilt test case.
382: *
383: */
384: private void deleteTest(LdapClient ldap, SampleResult res)
385: throws NamingException {
386: if (!getPropertyAsBoolean(USER_DEFINED)) {
387: ldap.createTest(getBasicAttributes(),
388: getPropertyAsString(ADD));
389: setProperty(new StringProperty(DELETE,
390: getPropertyAsString(ADD)));
391: }
392: res.sampleStart();
393: ldap.deleteTest(getPropertyAsString(DELETE));
394: res.sampleEnd();
395: }
396:
397: /**
398: * This will do the search test for the User defined TestCase as well as
399: * inbuilt test case.
400: *
401: */
402: private void searchTest(LdapClient ldap, SampleResult res)
403: throws NamingException {
404: if (!getPropertyAsBoolean(USER_DEFINED)) {
405: ldap.createTest(getBasicAttributes(),
406: getPropertyAsString(ADD));
407: setProperty(new StringProperty(SEARCHBASE,
408: getPropertyAsString(ADD)));
409: setProperty(new StringProperty(SEARCHFILTER,
410: getPropertyAsString(ADD)));
411: }
412: res.sampleStart();
413: searchFoundEntries = ldap.searchTest(
414: getPropertyAsString(SEARCHBASE),
415: getPropertyAsString(SEARCHFILTER));
416: res.sampleEnd();
417: if (!getPropertyAsBoolean(USER_DEFINED)) {
418: ldap.deleteTest(getPropertyAsString(ADD));
419: }
420: }
421:
422: /**
423: * This will do the search test for the User defined TestCase as well as
424: * inbuilt test case.
425: *
426: */
427: private void modifyTest(LdapClient ldap, SampleResult res)
428: throws NamingException {
429: if (getPropertyAsBoolean(USER_DEFINED)) {
430: res.sampleStart();
431: ldap.modifyTest(getUserModAttributes(),
432: getPropertyAsString(BASE_ENTRY_DN));
433: res.sampleEnd();
434: } else {
435: ldap.createTest(getBasicAttributes(),
436: getPropertyAsString(ADD));
437: setProperty(new StringProperty(MODIFY,
438: getPropertyAsString(ADD)));
439: res.sampleStart();
440: ldap.modifyTest(getModificationItem(),
441: getPropertyAsString(MODIFY));
442: res.sampleEnd();
443: ldap.deleteTest(getPropertyAsString(ADD));
444: }
445: }
446:
447: public SampleResult sample(Entry e) {
448: SampleResult res = new SampleResult();
449: boolean isSuccessful = false;
450: res.setSampleLabel(getName());
451: res.setSamplerData(getPropertyAsString(TEST));// TODO improve this
452: LdapClient ldap = new LdapClient();
453:
454: try {
455: ldap.connect(getServername(), getPort(), getRootdn(),
456: getUsername(), getPassword());
457:
458: if (getPropertyAsString(TEST).equals(ADD)) {
459: addTest(ldap, res);
460: } else if (getPropertyAsString(TEST).equals(DELETE)) {
461: deleteTest(ldap, res);
462: } else if (getPropertyAsString(TEST).equals(MODIFY)) {
463: modifyTest(ldap, res);
464: } else if (getPropertyAsString(TEST).equals(SEARCHBASE)) {
465: searchTest(ldap, res);
466: }
467:
468: // TODO - needs more work ...
469: if (getPropertyAsString(TEST).equals(SEARCHBASE)
470: && !searchFoundEntries) {
471: res.setResponseCode("201");// TODO is this a sensible number? //$NON-NLS-1$
472: res.setResponseMessage("OK - no results");
473: res.setResponseData("successful - no results"
474: .getBytes());
475: } else {
476: res.setResponseCodeOK();
477: res.setResponseMessage("OK"); //$NON-NLS-1$
478: res.setResponseData("successful".getBytes());
479: }
480: res.setDataType(SampleResult.TEXT);
481: isSuccessful = true;
482: } catch (Exception ex) {
483: log.error("Ldap client - ", ex);
484: // Could time this
485: // res.sampleEnd();
486: // if sampleEnd() is not called, elapsed time will remain zero
487: res.setResponseCode("500");// TODO distinguish errors better //$NON-NLS-1$
488: res.setResponseMessage(ex.toString());
489: isSuccessful = false;
490: } finally {
491: ldap.disconnect();
492: }
493:
494: // Set if we were successful or not
495: res.setSuccessful(isSuccessful);
496: return res;
497: }
498: }
|