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.entry;
21  
22  
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.directory.server.schema.bootstrap.ApacheSchema;
29  import org.apache.directory.server.schema.bootstrap.ApachemetaSchema;
30  import org.apache.directory.server.schema.bootstrap.BootstrapSchemaLoader;
31  import org.apache.directory.server.schema.bootstrap.CoreSchema;
32  import org.apache.directory.server.schema.bootstrap.CosineSchema;
33  import org.apache.directory.server.schema.bootstrap.InetorgpersonSchema;
34  import org.apache.directory.server.schema.bootstrap.Schema;
35  import org.apache.directory.server.schema.bootstrap.SystemSchema;
36  import org.apache.directory.server.schema.registries.DefaultOidRegistry;
37  import org.apache.directory.server.schema.registries.DefaultRegistries;
38  import org.apache.directory.server.schema.registries.OidRegistry;
39  import org.apache.directory.server.schema.registries.Registries;
40  import org.apache.directory.shared.ldap.entry.EntryAttribute;
41  import org.apache.directory.shared.ldap.name.LdapDN;
42  import org.apache.directory.shared.ldap.schema.DeepTrimToLowerNormalizer;
43  import org.apache.directory.shared.ldap.schema.OidNormalizer;
44  import org.apache.directory.shared.ldap.util.StringTools;
45  
46  import static org.junit.Assert.assertEquals;
47  import org.junit.BeforeClass;
48  import org.junit.Test;
49  
50  
51  /**
52   * Test the ServerEntry serialization/deserialization class
53   *
54   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
55   * @version $Rev$, $Date$
56   */
57  public class ServerEntrySerializerTest
58  {
59      private static BootstrapSchemaLoader loader;
60      private static Registries registries;
61      private static OidRegistry oidRegistry;
62      private static Map<String, OidNormalizer> oids;
63      private static Map<String, OidNormalizer> oidOids;
64  
65      /**
66       * Initialize the registries once for the whole test suite
67       */
68      @BeforeClass
69      public static void setup() throws Exception
70      {
71          loader = new BootstrapSchemaLoader();
72          oidRegistry = new DefaultOidRegistry();
73          registries = new DefaultRegistries( "bootstrap", loader, oidRegistry );
74          
75          // load essential bootstrap schemas 
76          Set<Schema> bootstrapSchemas = new HashSet<Schema>();
77          bootstrapSchemas.add( new ApachemetaSchema() );
78          bootstrapSchemas.add( new ApacheSchema() );
79          bootstrapSchemas.add( new CoreSchema() );
80          bootstrapSchemas.add( new SystemSchema() );
81          bootstrapSchemas.add( new InetorgpersonSchema() );
82          bootstrapSchemas.add( new CosineSchema() );
83          loader.loadWithDependencies( bootstrapSchemas, registries );
84          
85          oids = new HashMap<String, OidNormalizer>();
86  
87          oids.put( "dc", new OidNormalizer( "dc", new DeepTrimToLowerNormalizer() ) );
88          oids.put( "domaincomponent", new OidNormalizer( "dc", new DeepTrimToLowerNormalizer() ) );
89          oids.put( "0.9.2342.19200300.100.1.25", new OidNormalizer( "dc", new DeepTrimToLowerNormalizer() ) );
90          oids.put( "ou", new OidNormalizer( "ou", new DeepTrimToLowerNormalizer() ) );
91          oids.put( "organizationalUnitName", new OidNormalizer( "ou", new DeepTrimToLowerNormalizer() ) );
92          oids.put( "2.5.4.11", new OidNormalizer( "ou", new DeepTrimToLowerNormalizer() ) );
93      
94      
95          // Another map where we store OIDs instead of names.
96          oidOids = new HashMap<String, OidNormalizer>();
97  
98          oidOids.put( "dc", new OidNormalizer( "0.9.2342.19200300.100.1.25", new DeepTrimToLowerNormalizer() ) );
99          oidOids.put( "domaincomponent", new OidNormalizer( "0.9.2342.19200300.100.1.25", new DeepTrimToLowerNormalizer() ) );
100         oidOids.put( "0.9.2342.19200300.100.1.25", 
101             new OidNormalizer( "0.9.2342.19200300.100.1.25", new DeepTrimToLowerNormalizer() ) );
102         oidOids.put( "ou", new OidNormalizer( "2.5.4.11", new DeepTrimToLowerNormalizer() ) );
103         oidOids.put( "organizationalUnitName", new OidNormalizer( "2.5.4.11", new DeepTrimToLowerNormalizer() ) );
104         oidOids.put( "2.5.4.11", new OidNormalizer( "2.5.4.11", new DeepTrimToLowerNormalizer() ) );
105     }
106 
107     
108     @Test public void testSerializeEmtpyServerEntry() throws Exception
109     {
110         LdapDN dn = LdapDN.EMPTY_LDAPDN;
111         ServerEntry entry = new DefaultServerEntry( registries, dn );
112 
113         ServerEntrySerializer ses = new ServerEntrySerializer( registries );
114         
115         byte[] data = ses.serialize( entry );
116         
117         ServerEntry result = (ServerEntry)ses.deserialize( data );
118         
119         assertEquals( entry, result );
120     }
121 
122 
123     @Test public void testSerializeDNServerEntry() throws Exception
124     {
125         LdapDN dn = new LdapDN( "cn=text, dc=example, dc=com" );
126         dn.normalize( oids );
127         
128         ServerEntry entry = new DefaultServerEntry( registries, dn );
129 
130         ServerEntrySerializer ses = new ServerEntrySerializer( registries );
131         
132         byte[] data = ses.serialize( entry );
133         
134         ServerEntry result = (ServerEntry)ses.deserialize( data );
135         
136         assertEquals( entry, result );
137     }
138 
139 
140     @Test public void testSerializeServerEntryOC() throws Exception
141     {
142         LdapDN dn = new LdapDN( "cn=text, dc=example, dc=com" );
143         dn.normalize( oids );
144         
145         ServerEntry entry = new DefaultServerEntry( registries, dn );
146         entry.add( "objectClass", "top", "person", "inetOrgPerson", "organizationalPerson" );
147 
148         ServerEntrySerializer ses = new ServerEntrySerializer( registries );
149 
150         byte[] data = ses.serialize( entry );
151         
152         ServerEntry result = (ServerEntry)ses.deserialize( data );
153         
154         assertEquals( entry, result );
155     }
156 
157 
158     @Test public void testSerializeServerEntry() throws Exception
159     {
160         LdapDN dn = new LdapDN( "cn=text, dc=example, dc=com" );
161         dn.normalize( oids );
162         
163         ServerEntry entry = new DefaultServerEntry( registries, dn );
164         entry.add( "objectClass", "top", "person", "inetOrgPerson", "organizationalPerson" );
165         entry.add( "cn", "text", "test" );
166         entry.add( "SN", (String)null );
167         entry.add( "userPassword", StringTools.getBytesUtf8( "password" ) );
168 
169         ServerEntrySerializer ses = new ServerEntrySerializer( registries );
170         
171         byte[] data = ses.serialize( entry );
172         
173         ServerEntry result = (ServerEntry)ses.deserialize( data );
174         
175         assertEquals( entry, result );
176     }
177 
178 
179     @Test public void testSerializeServerEntryWithEmptyDN() throws Exception
180     {
181         LdapDN dn = new LdapDN( "" );
182         dn.normalize( oids );
183         
184         ServerEntry entry = new DefaultServerEntry( registries, dn );
185         entry.add( "objectClass", "top", "person", "inetOrgPerson", "organizationalPerson" );
186         entry.add( "cn", "text", "test" );
187         entry.add( "SN", (String)null );
188         entry.add( "userPassword", StringTools.getBytesUtf8( "password" ) );
189 
190         ServerEntrySerializer ses = new ServerEntrySerializer( registries );
191         
192         byte[] data = ses.serialize( entry );
193         
194         ServerEntry result = (ServerEntry)ses.deserialize( data );
195         
196         assertEquals( entry, result );
197     }
198 
199     
200     @Test public void testSerializeServerEntryWithNoAttributes() throws Exception
201     {
202         LdapDN dn = new LdapDN( "" );
203         dn.normalize( oids );
204         
205         ServerEntry entry = new DefaultServerEntry( registries, dn );
206 
207         ServerEntrySerializer ses = new ServerEntrySerializer( registries );
208         
209         byte[] data = ses.serialize( entry );
210         
211         ServerEntry result = (ServerEntry)ses.deserialize( data );
212         
213         assertEquals( entry, result );
214     }
215     
216     
217     @Test public void testSerializeServerEntryWithAttributeNoValue() throws Exception
218     {
219         LdapDN dn = new LdapDN( "" );
220         dn.normalize( oids );
221         
222         ServerEntry entry = new DefaultServerEntry( registries, dn );
223 
224         ServerEntrySerializer ses = new ServerEntrySerializer( registries );
225         EntryAttribute oc = new DefaultServerAttribute( "ObjectClass", registries.getAttributeTypeRegistry().lookup( "objectclass" ) );
226         entry.add( oc );
227         
228         byte[] data = ses.serialize( entry );
229         
230         ServerEntry result = (ServerEntry)ses.deserialize( data );
231         
232         assertEquals( entry, result );
233     }
234 
235 
236     @Test public void testSerializeServerEntryWithAttributeStringValue() throws Exception
237     {
238         LdapDN dn = new LdapDN( "" );
239         dn.normalize( oids );
240         
241         ServerEntry entry = new DefaultServerEntry( registries, dn );
242 
243         ServerEntrySerializer ses = new ServerEntrySerializer( registries );
244         entry.add( "ObjectClass", "top", "person" );
245         
246         byte[] data = ses.serialize( entry );
247         
248         ServerEntry result = (ServerEntry)ses.deserialize( data );
249         
250         assertEquals( entry, result );
251     }
252 
253 
254     @Test public void testSerializeServerEntryWithAttributeBinaryValue() throws Exception
255     {
256         LdapDN dn = new LdapDN( "" );
257         dn.normalize( oids );
258         
259         ServerEntry entry = new DefaultServerEntry( registries, dn );
260 
261         ServerEntrySerializer ses = new ServerEntrySerializer( registries );
262         entry.add( "userPassword", StringTools.getBytesUtf8( "secret" ) );
263         
264         byte[] data = ses.serialize( entry );
265         
266         ServerEntry result = (ServerEntry)ses.deserialize( data );
267         
268         assertEquals( entry, result );
269     }
270 }