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: package org.apache.harmony.jndi.provider.ldap;
019:
020: import java.util.Hashtable;
021:
022: import javax.naming.Context;
023: import javax.naming.PartialResultException;
024: import javax.naming.ReferralException;
025: import javax.naming.directory.DirContext;
026: import javax.naming.directory.InitialDirContext;
027: import javax.naming.ldap.Control;
028: import javax.naming.ldap.InitialLdapContext;
029: import javax.naming.ldap.LdapContext;
030: import javax.naming.ldap.SortControl;
031:
032: import junit.framework.TestCase;
033:
034: import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
035: import org.apache.harmony.jndi.provider.ldap.mock.BindResponse;
036: import org.apache.harmony.jndi.provider.ldap.mock.EncodableLdapResult;
037: import org.apache.harmony.jndi.provider.ldap.mock.MockLdapServer;
038:
039: public class LdapContextServerMockedTest extends TestCase {
040: private MockLdapServer server;
041:
042: private Hashtable<Object, Object> env = new Hashtable<Object, Object>();
043:
044: @Override
045: public void setUp() {
046: server = new MockLdapServer();
047: server.start();
048: env
049: .put(Context.INITIAL_CONTEXT_FACTORY,
050: "org.apache.harmony.jndi.provider.ldap.LdapContextFactory");
051: env.put(Context.PROVIDER_URL, server.getURL());
052: env.put(Context.SECURITY_AUTHENTICATION, "simple");
053: env.put(Context.SECURITY_PRINCIPAL, "");
054: env.put(Context.SECURITY_CREDENTIALS, "");
055: }
056:
057: @Override
058: public void tearDown() {
059: server.stop();
060: }
061:
062: public void testRequestControls() throws Exception {
063:
064: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
065: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
066: null) });
067:
068: InitialLdapContext initialContext = new InitialLdapContext(env,
069: null);
070:
071: Control[] reqCtls = initialContext.getRequestControls();
072: assertEquals(1, reqCtls.length);
073: assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
074: assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
075:
076: initialContext
077: .setRequestControls(new Control[] { new SortControl("",
078: Control.NONCRITICAL) });
079:
080: reqCtls = initialContext.getRequestControls();
081: assertEquals(2, reqCtls.length);
082: Control control = reqCtls[0];
083: if (control instanceof SortControl) {
084: assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
085: assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
086: assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
087: } else {
088: assertEquals("2.16.840.1.113730.3.4.2", control.getID());
089: assertEquals(Control.NONCRITICAL, control.isCritical());
090: assertTrue(reqCtls[1] instanceof SortControl);
091: assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
092: }
093:
094: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
095: LdapASN1Constant.OP_SEARCH_RESULT_DONE,
096: new EncodableLdapResult(), null) });
097:
098: LdapContext context = (LdapContext) initialContext.lookup("");
099: // request controls are not inherited
100: reqCtls = context.getRequestControls();
101: assertEquals(1, reqCtls.length);
102: assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
103: assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
104:
105: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
106: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
107: null) });
108: context = context.newInstance(new Control[] { new SortControl(
109: "", Control.NONCRITICAL) });
110: reqCtls = context.getRequestControls();
111:
112: assertEquals(2, reqCtls.length);
113: control = reqCtls[0];
114: if (control instanceof SortControl) {
115: assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
116: assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
117: assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
118: } else {
119: assertEquals("2.16.840.1.113730.3.4.2", control.getID());
120: assertEquals(Control.NONCRITICAL, control.isCritical());
121: assertTrue(reqCtls[1] instanceof SortControl);
122: assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
123: }
124: }
125:
126: public void testConnectControls() throws Exception {
127:
128: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
129: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
130: null) });
131:
132: InitialDirContext initialDirContext = new InitialDirContext(env);
133:
134: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
135: LdapASN1Constant.OP_SEARCH_RESULT_DONE,
136: new EncodableLdapResult(), null) });
137: LdapContext context = (LdapContext) initialDirContext
138: .lookup("");
139:
140: assertNull(context.getConnectControls());
141:
142: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
143: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
144: null) });
145: context.reconnect(new Control[] { new SortControl("",
146: Control.NONCRITICAL) });
147:
148: Control[] controls = context.getConnectControls();
149: assertNotNull(controls);
150: assertEquals(1, controls.length);
151: Control c = controls[0];
152: assertTrue(c instanceof SortControl);
153: assertEquals(Control.NONCRITICAL, c.isCritical());
154:
155: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
156: LdapASN1Constant.OP_SEARCH_RESULT_DONE,
157: new EncodableLdapResult(), null) });
158: context = (LdapContext) context.lookup("");
159:
160: // connect controls are inherited
161: controls = context.getConnectControls();
162: assertNotNull(controls);
163: assertEquals(1, controls.length);
164: c = controls[0];
165: assertTrue(c instanceof SortControl);
166: assertEquals(Control.NONCRITICAL, c.isCritical());
167:
168: }
169:
170: public void testConnectControls2() throws Exception {
171: // set connect controls by property "java.naming.ldap.control.connect"
172: env
173: .put("java.naming.ldap.control.connect",
174: new Control[] { new SortControl("",
175: Control.NONCRITICAL) });
176:
177: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
178: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
179: null) });
180:
181: InitialDirContext initialDirContext = new InitialDirContext(env);
182:
183: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
184: LdapASN1Constant.OP_SEARCH_RESULT_DONE,
185: new EncodableLdapResult(), null) });
186: LdapContext context = (LdapContext) initialDirContext
187: .lookup("");
188:
189: Control[] controls = context.getConnectControls();
190: assertNotNull(controls);
191: assertEquals(1, controls.length);
192: Control c = controls[0];
193: assertTrue(c instanceof SortControl);
194: assertEquals(Control.NONCRITICAL, c.isCritical());
195: }
196:
197: public void testConnectControls3() throws Exception {
198: // set connect controls by InitialLdapContext
199: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
200: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
201: null) });
202: InitialLdapContext initialDirContext = new InitialLdapContext(
203: env, new Control[] { new SortControl("",
204: Control.NONCRITICAL) });
205:
206: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
207: LdapASN1Constant.OP_SEARCH_RESULT_DONE,
208: new EncodableLdapResult(), null) });
209: LdapContext context = (LdapContext) initialDirContext
210: .lookup("");
211:
212: Control[] controls = context.getConnectControls();
213: assertNotNull(controls);
214: assertEquals(1, controls.length);
215: Control c = controls[0];
216: assertTrue(c instanceof SortControl);
217: assertEquals(Control.NONCRITICAL, c.isCritical());
218:
219: }
220:
221: public void testnewInstance() throws Exception {
222: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
223: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
224: null) });
225: InitialLdapContext initialDirContext = new InitialLdapContext(
226: env, null);
227: Control[] reqCtls = initialDirContext.getRequestControls();
228: assertEquals(1, reqCtls.length);
229: assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
230: assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
231:
232: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
233: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
234: null) });
235: LdapContext context = initialDirContext
236: .newInstance(new Control[] { new SortControl("",
237: Control.NONCRITICAL) });
238:
239: assertNotSame(initialDirContext, context);
240: reqCtls = context.getRequestControls();
241: assertEquals(2, reqCtls.length);
242: Control control = reqCtls[0];
243: if (control instanceof SortControl) {
244: assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
245: assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
246: assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
247: } else {
248: assertEquals("2.16.840.1.113730.3.4.2", control.getID());
249: assertEquals(Control.NONCRITICAL, control.isCritical());
250: assertTrue(reqCtls[1] instanceof SortControl);
251: assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
252: }
253:
254: }
255:
256: public void testRerralIgnore() throws Exception {
257: env.put(Context.REFERRAL, "ignore");
258:
259: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
260: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
261: null) });
262: DirContext context = new InitialDirContext(env);
263:
264: EncodableLdapResult result = new EncodableLdapResult();
265: result = new EncodableLdapResult(LdapResult.REFERRAL, "", "",
266: new String[] { "ldap://localhost" });
267:
268: server
269: .setResponseSeq(new LdapMessage[] { new LdapMessage(
270: LdapASN1Constant.OP_SEARCH_RESULT_DONE, result,
271: null) });
272: try {
273: context.getAttributes("cn=test");
274: fail("Should throw PartialResultException");
275: } catch (PartialResultException e) {
276: // expected
277: }
278:
279: result = new EncodableLdapResult(LdapResult.REFERRAL, null,
280: null, null);
281:
282: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
283: LdapASN1Constant.OP_DEL_RESPONSE, result, null) });
284: try {
285: context.destroySubcontext("cn=test");
286: fail("Should throw PartialResultException");
287: } catch (PartialResultException e) {
288: // expected
289: }
290: }
291:
292: public void testReferralThrow() throws Exception {
293: env.put(Context.REFERRAL, "throw");
294: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
295: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
296: null) });
297: DirContext context = new InitialDirContext(env);
298:
299: EncodableLdapResult result = new EncodableLdapResult(
300: LdapResult.REFERRAL, "", "",
301: new String[] { "ldap://localhost" });
302:
303: server
304: .setResponseSeq(new LdapMessage[] { new LdapMessage(
305: LdapASN1Constant.OP_SEARCH_RESULT_DONE, result,
306: null) });
307: try {
308: context.getAttributes("cn=test");
309: fail("Should throw ReferralException");
310: } catch (ReferralException e) {
311: assertEquals("ldap://localhost", e.getReferralInfo());
312: }
313:
314: result = new EncodableLdapResult(LdapResult.REFERRAL, "", "",
315: new String[] { "ldap://localhost" });
316:
317: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
318: LdapASN1Constant.OP_DEL_RESPONSE, result, null) });
319:
320: try {
321: context.destroySubcontext("");
322: fail("Should throw ReferralException");
323: } catch (ReferralException e) {
324: assertEquals("ldap://localhost", e.getReferralInfo());
325: }
326: }
327:
328: public void testReferralFollow() throws Exception {
329: env.put(Context.REFERRAL, "follow");
330: server.setResponseSeq(new LdapMessage[] { new LdapMessage(
331: LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(),
332: null) });
333: DirContext context = new InitialDirContext(env);
334:
335: MockLdapServer referralServer = new MockLdapServer();
336: referralServer.start();
337:
338: EncodableLdapResult result = new EncodableLdapResult(
339: LdapResult.REFERRAL, "", "",
340: new String[] { referralServer.getURL() });
341:
342: server
343: .setResponseSeq(new LdapMessage[] { new LdapMessage(
344: LdapASN1Constant.OP_SEARCH_RESULT_DONE, result,
345: null) });
346:
347: referralServer.setResponseSeq(new LdapMessage[] {
348: new LdapMessage(LdapASN1Constant.OP_BIND_RESPONSE,
349: new BindResponse(), null),
350: new LdapMessage(LdapASN1Constant.OP_SEARCH_RESULT_DONE,
351: new EncodableLdapResult(), null) });
352:
353: context.getAttributes("cn=test");
354:
355: referralServer.stop();
356: }
357:
358: }
|