001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.ext.ldap;
066:
067: import com.jcorporate.expresso.services.test.ExpressoTestCase;
068: import com.jcorporate.expresso.services.test.TestSystemInitializer;
069: import junit.framework.Test;
070: import junit.framework.TestSuite;
071:
072: import javax.naming.NamingEnumeration;
073: import javax.naming.NamingException;
074: import javax.naming.directory.Attribute;
075: import javax.naming.directory.Attributes;
076: import javax.naming.directory.SearchControls;
077: import javax.naming.directory.SearchResult;
078: import java.util.Enumeration;
079:
080: /**
081: * Basic test suite for LDAP based users
082: *
083: * @author Michael Rimov
084: */
085: public class UserLDAPTest extends ExpressoTestCase {
086: public UserLDAPTest(String testName) throws Exception {
087: super (testName);
088: }
089:
090: public static void main(String[] args) throws Exception {
091:
092: //Set the system properties we need
093: junit.textui.TestRunner.run(suite());
094: }
095:
096: /**
097: * Define the suite of tests that verify each function of the cache
098: *
099: * @return
100: */
101: public static Test suite() throws Exception {
102: return new TestSuite(UserLDAPTest.class);
103: } /* suite() */
104:
105: public void testLDAP() throws Exception {
106: UserLDAP user = new UserLDAP();
107: String testUser = "MNash2";
108: SearchControls constraints = new SearchControls();
109: constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
110: System.out.println("LDAP Test: ");
111:
112: try {
113: user.setDBName(TestSystemInitializer.getTestContext());
114:
115: NamingEnumeration results = user.getContext().search(
116: "o=" + user.getRoot(), "(uid=" + testUser + ")",
117: constraints);
118: assertTrue("No results for search of LDAP server",
119: results != null);
120:
121: while (results.hasMore()) {
122: SearchResult sr = (SearchResult) results.next();
123:
124: //result.get ln fn cn mail
125: Attributes a = sr.getAttributes();
126: Attribute attr = a.get("mail");
127:
128: if (attr != null) {
129: System.out.println("Email address:");
130:
131: for (NamingEnumeration vals = attr.getAll(); vals
132: .hasMoreElements(); System.out.println("\t"
133: + vals.nextElement())) {
134: ;
135: }
136: }
137:
138: attr = a.get("sn");
139:
140: if (attr != null) {
141: System.out.println("Surname:");
142:
143: for (NamingEnumeration vals = attr.getAll(); vals
144: .hasMoreElements(); System.out.println("\t"
145: + vals.nextElement())) {
146: ;
147: }
148: }
149:
150: attr = a.get("fn");
151:
152: if (attr != null) {
153: System.out.println("First name:");
154:
155: for (NamingEnumeration vals = attr.getAll(); vals
156: .hasMoreElements(); System.out.println("\t"
157: + vals.nextElement())) {
158: ;
159: }
160: }
161:
162: attr = a.get("cn");
163:
164: if (attr != null) {
165: System.out.println("Full name:");
166:
167: for (NamingEnumeration vals = attr.getAll(); vals
168: .hasMoreElements(); System.out.println("\t"
169: + vals.nextElement())) {
170: ;
171: }
172: }
173: for (NamingEnumeration ns = a.getAll(); ns.hasMore();) {
174: Attribute x = (Attribute) ns.next();
175: System.out.println("Additional Attribute "
176: + x.getID());
177:
178: for (NamingEnumeration vals = x.getAll(); vals
179: .hasMoreElements(); System.out
180: .println("\t\t" + vals.nextElement())) {
181: ;
182: }
183: }
184: }
185:
186: System.out.println("No (more) elements");
187: } catch (NamingException ne) {
188: ne.printStackTrace(System.out);
189: fail("Naming Exception : " + ne.getMessage());
190: }
191:
192: System.out.println("LDAP test 2 begins");
193: user.setUserName(testUser);
194: user.setDBName(TestSystemInitializer.getTestContext());
195: user.retrieve();
196: assertTrue("Proper Password", user.passwordEquals("xyx"));
197: System.out.println("LDAP test 3 begins");
198:
199: try {
200: user.setDBName(TestSystemInitializer.getTestContext());
201:
202: NamingEnumeration results = user.getContext().search(
203: "o=" + user.getRoot(),
204: "(objectclass=groupofuniquenames)", constraints);
205:
206: if (results == null) {
207: fail("No results for search of LDAP server for groups");
208: } else {
209: System.out.println("Got results");
210: }
211: while (results.hasMore()) {
212: SearchResult sr = (SearchResult) results.next();
213: System.out.println("Found " + sr.getName());
214:
215: Attributes a = sr.getAttributes();
216:
217: for (NamingEnumeration ns = a.getAll(); ns.hasMore();) {
218: Attribute x = (Attribute) ns.next();
219: System.out.println("Additional Attribute "
220: + x.getID());
221:
222: for (NamingEnumeration vals = x.getAll(); vals
223: .hasMoreElements(); System.out
224: .println("\t\t" + vals.nextElement())) {
225: ;
226: }
227: }
228: }
229:
230: System.out.println("No (more) groups");
231: } catch (NamingException ne) {
232: ne.printStackTrace();
233: fail("Failed LDAP Test #3 " + ne.getMessage());
234: }
235:
236: System.out.println("LDAP test 4 begins");
237:
238: try {
239: user.setDBName(TestSystemInitializer.getTestContext());
240:
241: /**
242: * @todo: This was name where I've replaced user.getLoginName()
243: * What should it be
244: */
245: NamingEnumeration results = user.getContext().search(
246: "o=" + user.getRoot(),
247: "(uniquemember=" + user.getLoginName() + ", o="
248: + user.getRoot() + ")", constraints);
249:
250: if (results == null) {
251: fail("LDAP Test #3 No results for search of LDAP server for groups");
252: } else {
253: System.out.println("Got results");
254: }
255: while (results.hasMore()) {
256: SearchResult sr = (SearchResult) results.next();
257: System.out.println("Found " + sr.getName());
258:
259: Attributes a = sr.getAttributes();
260:
261: for (NamingEnumeration ns = a.getAll(); ns.hasMore();) {
262: Attribute x = (Attribute) ns.next();
263: System.out.println("Additional Attribute "
264: + x.getID());
265:
266: for (NamingEnumeration vals = x.getAll(); vals
267: .hasMoreElements(); System.out
268: .println("\t\t" + vals.nextElement())) {
269: ;
270: }
271: }
272: }
273:
274: System.out.println("No (more) groups");
275: } catch (NamingException ne) {
276: ne.printStackTrace();
277: fail("caught exception in LDAP Test #4 " + ne.getMessage());
278: }
279:
280: System.out.println("LDAP test 5 begins");
281:
282: for (Enumeration e = user.getGroups().elements(); e
283: .hasMoreElements();) {
284: System.out.println("Test user belong to group '"
285: + (String) e.nextElement() + "'");
286: }
287: }
288: }
|