1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.jndi;
21  
22  
23  import org.apache.directory.server.core.DirectoryService;
24  import org.apache.directory.server.core.integ.CiRunner;
25  import static org.apache.directory.server.core.integ.IntegrationUtils.getSystemContext;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertTrue;
29  import org.junit.Test;
30  import org.junit.runner.RunWith;
31  
32  import javax.naming.Context;
33  import javax.naming.NamingEnumeration;
34  import javax.naming.NamingException;
35  import javax.naming.directory.Attribute;
36  import javax.naming.directory.Attributes;
37  import javax.naming.directory.BasicAttribute;
38  import javax.naming.directory.BasicAttributes;
39  import javax.naming.directory.DirContext;
40  import javax.naming.directory.InitialDirContext;
41  import javax.naming.directory.SearchControls;
42  import javax.naming.directory.SearchResult;
43  import javax.naming.ldap.LdapContext;
44  import java.util.Hashtable;
45  
46  
47  /**
48   * Contributed by Luke Taylor to fix DIRSERVER-169.
49   *
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   * @version $Rev$
52   */
53  @RunWith ( CiRunner.class )
54  public class DIRSERVER169IT
55  {
56      public static DirectoryService service;
57  
58  
59      /**
60       * @todo replace this later with an Ldif tag
61       *
62       * @throws NamingException on error
63       */
64      protected void createData() throws Exception
65      {
66          LdapContext sysRoot = getSystemContext( service );
67  
68          Attributes people = new BasicAttributes( true );
69          Attribute attribute = new BasicAttribute( "objectClass" );
70          attribute.add( "top" );
71          attribute.add( "organizationalUnit" );
72          people.put( attribute );
73          people.put( "ou", "people" );
74          sysRoot.createSubcontext( "ou=people", people );
75  
76          Attributes user = new BasicAttributes( "uid", "bob", true );
77          user.put( "cn", "Bob Hamilton" );
78          user.put( "userPassword", "bobspassword" );
79  
80          Attribute objectClass = new BasicAttribute( "objectClass" );
81          user.put( objectClass );
82          objectClass.add( "top" );
83          objectClass.add( "person" );
84          objectClass.add( "organizationalPerson" );
85          objectClass.add( "inetOrgPerson" );
86          user.put( "sn", "Hamilton" );
87  
88          sysRoot.createSubcontext( "uid=bob,ou=people", user );
89      }
90  
91  
92      @Test
93      public void testSearchResultNameIsRelativeToSearchContext() throws Exception
94      {
95          // @todo replace with ldif tags
96          createData();
97  
98          LdapContext sysRoot = getSystemContext( service );
99  
100         Hashtable<String,Object> env = new Hashtable<String,Object>();
101         env.put( DirectoryService.JNDI_KEY, service );
102         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
103         env.put( Context.PROVIDER_URL, "ou=system" );
104 
105         DirContext ctx = new InitialDirContext( env );
106         SearchControls ctls = new SearchControls();
107         String searchBase = "ou=people";
108 
109         NamingEnumeration<SearchResult> results = ctx.search( searchBase, "(uid=bob)", ctls );
110         assertTrue( results.hasMore() );
111         SearchResult searchResult = results.next();
112 
113         StringBuffer userDn = new StringBuffer();
114         userDn.append( searchResult.getName() );
115 
116         // Note that only if it's returned as a relative name do you need to 
117         // add the search base to the returned name value 
118         if ( searchResult.isRelative() )
119         {
120             if ( searchBase.length() > 0 )
121             {
122                 userDn.append( "," );
123                 userDn.append( searchBase );
124             }
125             userDn.append( "," );
126             userDn.append( ctx.getNameInNamespace() );
127         }
128         
129         assertEquals( "uid=bob,ou=people," + sysRoot.getNameInNamespace(), userDn.toString() );
130     }
131 
132 
133     /**
134      * Search over binary attributes now should work via the core JNDI 
135      * provider.
136      *
137      * @throws Exception if there are errors
138      */
139     @Test
140     public void testPasswordComparisonSucceeds() throws Exception
141     {
142         // @todo replace with ldif tags
143         createData();
144 
145         Hashtable<String,Object> env = new Hashtable<String,Object>();
146         env.put( DirectoryService.JNDI_KEY, service );
147         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
148         env.put( Context.PROVIDER_URL, "ou=system" );
149 
150         DirContext ctx = new InitialDirContext( env );
151         SearchControls ctls = new SearchControls();
152         ctls.setReturningAttributes( new String[0] );
153         ctls.setSearchScope( SearchControls.OBJECT_SCOPE );
154 
155         String filter = "(userPassword={0})";
156         NamingEnumeration<SearchResult> results = 
157             ctx.search( "uid=bob,ou=people", filter, new Object[] { "bobspassword" }, ctls );
158 
159         // We should have a match
160         assertTrue( results.hasMore() );
161     }
162 }