harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvj...@apache.org
Subject svn commit: r658557 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/
Date Wed, 21 May 2008 05:20:52 GMT
Author: lvjing
Date: Tue May 20 22:20:52 2008
New Revision: 658557

URL: http://svn.apache.org/viewvc?rev=658557&view=rev
Log:
Apply patch for HARMONY-5832, [classlib][jndi][ldap] - LdapContextImpl.bind(Name, Object,
Attributes) generate incorrect ldap request message

Added:
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ExtendedOpTest.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java?rev=658557&r1=658556&r2=658557&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
Tue May 20 22:20:52 2008
@@ -416,8 +416,11 @@
                 if (element.getID().equalsIgnoreCase("objectClass")) {
                     element = mergeAttribute(resultAttributes
                             .get("objectClass"), element);
-                    if (resultAttributes.get("objectClass") != null) {
-                        element.remove("javaContainer");
+                    Attribute oc = resultAttributes.get("objectClass");
+                    if (oc != null) {
+                        if (!oc.contains("javaContainer") && oc.size() > 0) {
+                            element.remove("javaContainer");
+                        }
                     }
                     resultAttributes.put(element);
                 } else if (resultAttributes.get(element.getID()) == null) {

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ExtendedOpTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ExtendedOpTest.java?rev=658557&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ExtendedOpTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ExtendedOpTest.java
Tue May 20 22:20:52 2008
@@ -0,0 +1,95 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You under the Apache License, Version 2.0 
+ *  (the "License"); you may not use this file except in compliance with 
+ *  the License.  You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ *  Unless required by applicable law or agreed to in writing, software 
+ *  distributed under the License is distributed on an "AS IS" BASIS, 
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ *  See the License for the specific language governing permissions and 
+ *  limitations under the License. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap;
+
+import javax.naming.NamingException;
+import javax.naming.ldap.ExtendedRequest;
+import javax.naming.ldap.ExtendedResponse;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1TestUtils;
+import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
+import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
+import org.apache.harmony.security.asn1.ASN1Integer;
+
+public class ExtendedOpTest extends TestCase {
+    public void test_encodeValues_$LObject() {
+        ExtendedOp op = new ExtendedOp(new MockExtendedRequest());
+
+        ASN1TestUtils.checkEncode(op, LdapASN1Constant.ExtendedRequest);
+
+        assertEquals(LdapASN1Constant.OP_EXTENDED_REQUEST, op.getRequestId());
+        assertEquals(LdapASN1Constant.OP_EXTENDED_RESPONSE, op.getResponseId());
+        assertSame(op, op.getRequest());
+        assertSame(op, op.getResponse());
+    }
+
+    public void test_getExtendedResponse() throws Exception {
+        MockExtendedRequest mockRequest = new MockExtendedRequest();
+        ExtendedOp op = new ExtendedOp(mockRequest);
+
+        assertSame(mockRequest, op.getExtendedRequest());
+        assertNull(op.getExtendedResponse());
+
+        Object[] values = new Object[] { ASN1Integer.fromIntValue(0),
+                Utils.getBytes(""), Utils.getBytes(""), null,
+                Utils.getBytes("hello"), Utils.getBytes("world") };
+
+        op.decodeValues(values);
+
+        LdapResult result = op.getResult();
+        assertNotNull(result);
+        assertEquals(0, result.getResultCode());
+        assertEquals("", result.getMachedDN());
+        assertEquals("", result.getErrorMessage());
+        assertNull(result.getReferrals());
+
+        ExtendedResponse response = op.getExtendedResponse();
+        assertTrue(response instanceof MockExtendedResponse);
+    }
+
+    public class MockExtendedRequest implements ExtendedRequest {
+
+        public ExtendedResponse createExtendedResponse(String s, byte[] value,
+                int offset, int length) throws NamingException {
+            return new MockExtendedResponse();
+        }
+
+        public byte[] getEncodedValue() {
+            return new byte[0];
+        }
+
+        public String getID() {
+            return getClass().getName();
+        }
+
+    }
+
+    public class MockExtendedResponse implements ExtendedResponse {
+
+        public byte[] getEncodedValue() {
+            return new byte[0];
+        }
+
+        public String getID() {
+            return getClass().getName();
+        }
+
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ExtendedOpTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java?rev=658557&r1=658556&r2=658557&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
Tue May 20 22:20:52 2008
@@ -674,6 +674,74 @@
         assertEquals("bind", attribute.get(0));
     }
 
+    public void test_bind_LAttributes() throws Exception {
+        MockLdapClient client = new MockLdapClient();
+        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+
+        context = new LdapContextImpl(client, env, "cn=test");
+        
+        Attributes attrs = new BasicAttributes();
+        attrs.put("objectClass", "cn");
+        attrs.put("cn", "testBind");
+        context.bind("cn=testBind", "object", attrs);
+        
+        AddOp op = (AddOp) client.getRequest();
+        assertEquals("cn=testBind,cn=test", op.getEntry());
+        List<LdapAttribute> attrList = op.getAttributeList();
+        // has attribute: objectClass, javaClassNames, javaClassName,
+        // javaSerializedData, cn
+        assertEquals(5, attrList.size());
+        Map<String, LdapAttribute> map = new HashMap<String, LdapAttribute>();
+        for (Iterator iter = attrList.iterator(); iter.hasNext();) {
+            LdapAttribute attr = (LdapAttribute) iter.next();
+            map.put(attr.getID(), attr);
+        }
+
+        assertTrue(map.containsKey("objectClass"));
+        Attribute attribute = map.get("objectClass");
+        NamingEnumeration<?> enu = attribute.getAll();
+        HashSet<Object> valueSet = new HashSet<Object>();
+        while (enu.hasMore()) {
+            valueSet.add(enu.next());
+        }
+        // objectClass has values: top, cn, javaObject, 
+        // javaSerializedObject
+        assertEquals(4, valueSet.size());
+        assertTrue(valueSet.contains("top"));
+        assertTrue(valueSet.contains("cn"));
+        assertTrue(valueSet.contains("javaObject"));
+        assertTrue(valueSet.contains("javaSerializedObject"));
+
+        assertTrue(map.containsKey("javaClassNames"));
+        attribute = map.get("javaClassNames");
+        enu = attribute.getAll();
+        valueSet = new HashSet<Object>();
+        while (enu.hasMore()) {
+            valueSet.add(enu.next());
+        }
+
+        assertEquals(5, valueSet.size());
+        assertTrue(valueSet.contains("java.io.Serializable"));
+        assertTrue(valueSet.contains("java.lang.CharSequence"));
+        assertTrue(valueSet.contains("java.lang.Comparable"));
+        assertTrue(valueSet.contains("java.lang.Object"));
+        assertTrue(valueSet.contains("java.lang.String"));
+
+        assertTrue(map.containsKey("javaClassName"));
+        attribute = map.get("javaClassName");
+        assertEquals(1, attribute.size());
+        assertEquals(String.class.getName(), attribute.get(0));
+
+        assertTrue(map.containsKey("javaSerializedData"));
+        assertEquals(1, attribute.size());
+        attribute = map.get("javaSerializedData");
+
+        assertTrue(map.containsKey("cn"));
+        attribute = map.get("cn");
+        assertEquals(1, attribute.size());
+        assertEquals("testBind", attribute.get(0));
+    }
+    
     public void test_lookup() throws Exception {
         MockLdapClient client = new MockLdapClient();
         Hashtable<Object, Object> env = new Hashtable<Object, Object>();

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java?rev=658557&r1=658556&r2=658557&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
Tue May 20 22:20:52 2008
@@ -26,6 +26,7 @@
 import javax.naming.Context;
 import javax.naming.InvalidNameException;
 import javax.naming.Name;
+import javax.naming.NamingException;
 import javax.naming.PartialResultException;
 import javax.naming.RefAddr;
 import javax.naming.Reference;
@@ -36,6 +37,8 @@
 import javax.naming.event.EventDirContext;
 import javax.naming.event.NamingExceptionEvent;
 import javax.naming.ldap.Control;
+import javax.naming.ldap.ExtendedRequest;
+import javax.naming.ldap.ExtendedResponse;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
 import javax.naming.ldap.PagedResultsControl;
@@ -54,7 +57,6 @@
 import org.apache.harmony.jndi.provider.ldap.mock.EncodableLdapResult;
 import org.apache.harmony.jndi.provider.ldap.mock.MockLdapMessage;
 import org.apache.harmony.jndi.provider.ldap.mock.MockLdapServer;
-import org.apache.harmony.security.x509.IssuingDistributionPoint;
 
 public class LdapContextServerMockedTest extends TestCase {
     private MockLdapServer server;
@@ -645,7 +647,7 @@
         eventContext.addNamingListener("", "(objectclass=cn)", new Object[0],
                 new SearchControls(), listener);
         server.disconnectNotify();
-        Thread.sleep(5000);
+        Thread.sleep(500);
         assertNull(listener.exceptionEvent);
         assertNotNull(listener.unsolicatedEvent);
         assertTrue(listener.unsolicatedEvent.getSource() instanceof LdapContext);
@@ -659,6 +661,99 @@
         assertNull(notification.getEncodedValue());
     }
 
+    public void testExtendedOperation() throws Exception {
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        LdapContext context = new InitialLdapContext(env, null);
+
+        ASN1Encodable encodableResponse = new ASN1Encodable() {
+
+            public void encodeValues(Object[] values) {
+                new EncodableLdapResult().encodeValues(values);
+                values[4] = Utils.getBytes("It's my id");
+                values[5] = new byte[] { 0, 1, 2, 3 };
+            }
+
+        };
+        server
+                .setResponseSeq(new LdapMessage[] { new LdapMessage(
+                        LdapASN1Constant.OP_EXTENDED_RESPONSE,
+                        encodableResponse, null) });
+
+        ExtendedResponse response = context
+                .extendedOperation(new MockExtendedRequest());
+        assertTrue(response instanceof MockExtendedResponse);
+        assertEquals("It's my id", response.getID());
+        assertEquals(4, response.getEncodedValue().length);
+        assertEquals(0, response.getEncodedValue()[0]);
+        assertEquals(1, response.getEncodedValue()[1]);
+        assertEquals(2, response.getEncodedValue()[2]);
+        assertEquals(3, response.getEncodedValue()[3]);
+
+        // test exception
+        encodableResponse = new ASN1Encodable() {
+            public void encodeValues(Object[] values) {
+                new EncodableLdapResult().encodeValues(values);
+                values[4] = Utils.getBytes("exception");
+                values[5] = new byte[] { 0, 1, 2, 3 };
+            }
+
+        };
+        server
+                .setResponseSeq(new LdapMessage[] { new LdapMessage(
+                        LdapASN1Constant.OP_EXTENDED_RESPONSE,
+                        encodableResponse, null) });
+
+        try {
+            context.extendedOperation(new MockExtendedRequest());
+            fail("Should throw NamingException");
+        } catch (NamingException e) {
+            // expected
+            assertEquals("exception", e.getMessage());
+        }
+    }
+
+    public class MockExtendedRequest implements ExtendedRequest {
+
+        public ExtendedResponse createExtendedResponse(String s, byte[] value,
+                int offset, int length) throws NamingException {
+            if (s.equalsIgnoreCase("exception")) {
+                throw new NamingException("exception");
+            }
+            return new MockExtendedResponse(s, value);
+        }
+
+        public byte[] getEncodedValue() {
+            return new byte[0];
+        }
+
+        public String getID() {
+            return getClass().getName();
+        }
+
+    }
+
+    public class MockExtendedResponse implements ExtendedResponse {
+
+        private String id;
+
+        private byte[] values;
+
+        public MockExtendedResponse(String id, byte[] values) {
+            this.id = id;
+            this.values = values;
+        }
+
+        public byte[] getEncodedValue() {
+            return values;
+        }
+
+        public String getID() {
+            return id;
+        }
+
+    }
+
     public class MockUnsolicitedNotificationListener implements
             UnsolicitedNotificationListener {
 

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java?rev=658557&r1=658556&r2=658557&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/ReferralExceptionTest.java
Tue May 20 22:20:52 2008
@@ -20,6 +20,9 @@
 import java.util.Hashtable;
 
 import javax.naming.Context;
+import javax.naming.ldap.Control;
+import javax.naming.ldap.PagedResultsControl;
+import javax.naming.ldap.SortControl;
 
 import junit.framework.TestCase;
 
@@ -124,4 +127,49 @@
 
 		assertNull(ex.getReferralInfo());
 	}
+
+    public void testGetReferralContext3() throws Exception {
+        Hashtable<Object, Object> initialEnv = new Hashtable<Object, Object>();
+
+        initialEnv.put(Context.REFERRAL, "throw");
+        initialEnv.put("test.getReferralContext", "GetReferralContext");
+
+        String[] referrals = new String[] { server.getURL() };
+        ReferralExceptionImpl ex = new ReferralExceptionImpl("cn=dn",
+                referrals, initialEnv);
+
+        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+
+        env.put(Context.REFERRAL, "follow");
+        env.put("test.getReferralContext", "changed");
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        assertEquals(referrals[0], ex.getReferralInfo());
+        Context refContext = ex.getReferralContext(env, new Control[] {
+                new PagedResultsControl(1, true),
+                new SortControl("hello", true) });
+
+        Hashtable<Object, Object> refEnv = (Hashtable<Object, Object>) refContext
+                .getEnvironment();
+
+        assertEquals(env.get(Context.REFERRAL), refEnv.get(Context.REFERRAL));
+        assertEquals(env.get("test.getReferralContext"), refEnv
+                .get("test.getReferralContext"));
+        Control[] cs = (Control[]) refEnv
+                .get("java.naming.ldap.control.connect");
+        
+        assertNotNull(cs);
+        assertEquals(2, cs.length);
+        assertTrue(cs[0] instanceof PagedResultsControl);
+        assertTrue(cs[1] instanceof SortControl);
+
+        assertFalse(ex.skipReferral());
+
+        assertNull(ex.getReferralInfo());
+        
+        // do nothing
+        ex.retryReferral();
+    }
 }



Mime
View raw message