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  package org.apache.directory.server.core.changelog;
20  
21  
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import javax.naming.NamingException;
32  
33  import junit.framework.TestCase;
34  import org.apache.directory.server.core.authn.LdapPrincipal;
35  import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
36  import org.apache.directory.shared.ldap.constants.SchemaConstants;
37  import org.apache.directory.shared.ldap.ldif.ChangeType;
38  import org.apache.directory.shared.ldap.ldif.LdifEntry;
39  import org.apache.directory.shared.ldap.ldif.LdifUtils;
40  import org.apache.directory.shared.ldap.name.LdapDN;
41  import org.apache.directory.shared.ldap.schema.NoOpNormalizer;
42  import org.apache.directory.shared.ldap.schema.OidNormalizer;
43  import org.apache.directory.shared.ldap.util.DateUtils;
44  import org.apache.directory.shared.ldap.util.StringTools;
45  import org.junit.After;
46  import org.junit.Before;
47  import org.junit.Test;
48  
49  
50  /**
51   * Tests the MemoryChangeLogStore.
52   *
53   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
54   * @version $Rev$, $Date$
55   */
56  public class MemoryChangeLogStoreTest extends TestCase
57  {
58      MemoryChangeLogStore store;
59  
60      Map<String, OidNormalizer> oidsMap = new HashMap<String, OidNormalizer>();
61      
62      
63      @Before
64      public void setUp() throws Exception
65      {
66          super.setUp();
67          store = new MemoryChangeLogStore();
68  
69          oidsMap.put( SchemaConstants.UID_AT, new OidNormalizer( SchemaConstants.UID_AT_OID, new NoOpNormalizer() ) );
70          oidsMap.put( SchemaConstants.USER_ID_AT, new OidNormalizer( SchemaConstants.UID_AT_OID, new NoOpNormalizer() ) );
71          oidsMap.put( SchemaConstants.UID_AT_OID, new OidNormalizer( SchemaConstants.UID_AT_OID, new NoOpNormalizer() ) );
72          
73          oidsMap.put( SchemaConstants.OU_AT, new OidNormalizer( SchemaConstants.OU_AT_OID, new NoOpNormalizer()  ) );
74          oidsMap.put( SchemaConstants.ORGANIZATIONAL_UNIT_NAME_AT, new OidNormalizer( SchemaConstants.OU_AT_OID, new NoOpNormalizer()  ) );
75          oidsMap.put( SchemaConstants.OU_AT_OID, new OidNormalizer( SchemaConstants.OU_AT_OID, new NoOpNormalizer()  ) );
76      }
77  
78  
79      @After
80      public void tearDown() throws Exception
81      {
82          super.tearDown();
83          store = null;
84      }
85  
86  
87      @Test
88      public void testLogCheckRevision() throws Exception
89      {
90          assertEquals( "first revision is always 0", 0, store.getCurrentRevision() );
91  
92          LdifEntry forward = new LdifEntry();
93          forward.setDn( "ou=system" );
94          forward.setChangeType( ChangeType.Add );
95          forward.putAttribute( "objectClass", "organizationalUnit" );
96          forward.putAttribute( "ou", "system" );
97  
98          LdifEntry reverse = LdifUtils.reverseAdd( new LdapDN( forward.getDn() ) );
99          assertEquals( 1, store.log( new LdapPrincipal(), forward, reverse ).getRevision() );
100         assertEquals( 1, store.getCurrentRevision() );
101     }
102     
103     
104     @Test
105     public void testChangeLogSerialization() throws NamingException, IOException, ClassNotFoundException
106     {
107         LdapDN systemDn = new LdapDN( "ou=system" );
108         systemDn.normalize( oidsMap );
109         
110         LdapDN adminDn = new LdapDN( "uid=admin, ou=system" );
111         adminDn.normalize( oidsMap );
112 
113         LdifEntry forward = new LdifEntry();
114         forward.setDn( systemDn );
115         forward.setChangeType( ChangeType.Add );
116         forward.putAttribute( "objectClass", "organizationalUnit" );
117         forward.putAttribute( "ou", "system" );
118         
119         LdapDN reverseDn = new LdapDN( forward.getDn() );
120         reverseDn.normalize( oidsMap );
121 
122         LdifEntry reverse = LdifUtils.reverseAdd( reverseDn );
123 
124         String zuluTime = DateUtils.getGeneralizedTime();
125         long revision = 1L;
126         
127         LdapPrincipal principal = new LdapPrincipal( adminDn, AuthenticationLevel.SIMPLE, StringTools.getBytesUtf8( "secret"  ) );
128         ChangeLogEvent event = new ChangeLogEvent( revision, zuluTime, principal, forward, reverse );
129         
130         ByteArrayOutputStream baos = new ByteArrayOutputStream();
131         ObjectOutputStream out = new ObjectOutputStream( baos );
132 
133         out.writeObject( event );
134         
135         byte[] data = baos.toByteArray();
136         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
137         
138         ChangeLogEvent read = (ChangeLogEvent)in.readObject(); 
139         
140         // The read event should not be equal to the written event, as
141         // the principal's password has not been stored
142         assertNotSame( event, read );
143         
144         LdapPrincipal readPrincipal = read.getCommitterPrincipal();
145         
146         assertEquals( principal.getAuthenticationLevel(), readPrincipal.getAuthenticationLevel() );
147         assertEquals( principal.getName(), readPrincipal.getName() );
148         assertEquals( principal.getJndiName(), readPrincipal.getJndiName() );
149         assertNull( readPrincipal.getUserPassword() );
150         
151         assertEquals( zuluTime, read.getZuluTime() );
152         assertEquals( revision, read.getRevision() );
153         assertEquals( forward, read.getForwardLdif() );
154         assertEquals( reverse, read.getReverseLdifs().get( 0 ) );
155     }
156 }