Skip to content
Permalink
Browse files
Applied patch for DIRSERVER-1847
  • Loading branch information
elecharny committed Jun 14, 2019
1 parent c7524e0 commit 2a10bd45ae8b5cd0836948f360642ad6a802a74f
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 21 deletions.
@@ -37,6 +37,7 @@
import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants;
import org.apache.directory.api.util.Base64;
import org.apache.directory.api.util.DateUtils;
import org.apache.directory.api.util.TimeProvider;
import org.apache.directory.api.util.Strings;

/**
@@ -607,7 +608,7 @@ private static void merge( byte[] all, byte[] left, byte[] right )
* @param pwdMaxAgeSec the max age value in seconds
* @return true if expired, false otherwise
*/
public static boolean isPwdExpired( String pwdChangedZtime, int pwdMaxAgeSec )
public static boolean isPwdExpired( String pwdChangedZtime, int pwdMaxAgeSec, TimeProvider timeProvider )
{
Date pwdChangeDate = DateUtils.getDate( pwdChangedZtime );

@@ -616,7 +617,7 @@ public static boolean isPwdExpired( String pwdChangedZtime, int pwdMaxAgeSec )
time += pwdChangeDate.getTime();

Date expiryDate = DateUtils.getDate( DateUtils.getGeneralizedTime( time ) );
Date now = DateUtils.getDate( DateUtils.getGeneralizedTime() );
Date now = DateUtils.getDate( DateUtils.getGeneralizedTime( timeProvider ) );

boolean expired = false;

@@ -33,6 +33,7 @@
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.directory.api.ldap.model.schema.registries.Schema;
import org.apache.directory.api.util.DateUtils;
import org.apache.directory.api.util.TimeProvider;


/**
@@ -43,6 +44,8 @@
*/
public class AttributesFactory
{
private TimeProvider timeProvider = TimeProvider.DEFAULT;

/**
* Get a SchemaObject as an Entry
*
@@ -106,7 +109,7 @@ public Entry convert( Schema schema, SchemaManager schemaManager ) throws LdapEx
entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SCHEMA_OC );
entry.put( SchemaConstants.CN_AT, schema.getSchemaName() );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

if ( schema.isDisabled() )
{
@@ -148,7 +151,7 @@ public Entry convert( SyntaxChecker syntaxChecker, Schema schema, SchemaManager
entry.put( MetaSchemaConstants.M_OID_AT, syntaxChecker.getOid() );
entry.put( MetaSchemaConstants.M_FQCN_AT, syntaxChecker.getClass().getName() );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

return entry;
}
@@ -169,7 +172,7 @@ public Entry convert( LdapSyntax syntax, Schema schema, SchemaManager schemaMana

entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SYNTAX_OC );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
injectCommon( syntax, entry, schemaManager );

return entry;
@@ -193,7 +196,7 @@ public Entry convert( String oid, Normalizer normalizer, Schema schema, SchemaMa
entry.put( MetaSchemaConstants.M_OID_AT, oid );
entry.put( MetaSchemaConstants.M_FQCN_AT, normalizer.getClass().getName() );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

return entry;
}
@@ -216,7 +219,7 @@ public Entry convert( String oid, LdapComparator<? super Object> comparator, Sch
entry.put( MetaSchemaConstants.M_OID_AT, oid );
entry.put( MetaSchemaConstants.M_FQCN_AT, comparator.getClass().getName() );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

return entry;
}
@@ -239,7 +242,7 @@ public Entry convert( MatchingRule matchingRule, Schema schema, SchemaManager sc
entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_MATCHING_RULE_OC );
entry.put( MetaSchemaConstants.M_SYNTAX_AT, matchingRule.getSyntaxOid() );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
injectCommon( matchingRule, entry, schemaManager );

return entry;
@@ -260,7 +263,7 @@ public Entry convert( MatchingRuleUse matchingRuleUse, Schema schema, SchemaMana

entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

return entry;
}
@@ -280,7 +283,7 @@ public Entry convert( DitStructureRule ditStructureRule, Schema schema, SchemaMa

entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

return entry;
}
@@ -300,7 +303,7 @@ public Entry convert( DitContentRule dITContentRule, Schema schema, SchemaManage

entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

return entry;
}
@@ -321,7 +324,7 @@ public Entry convert( NameForm nameForm, Schema schema, SchemaManager schemaMana

entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

return entry;
}
@@ -357,7 +360,7 @@ public Entry convert( AttributeType attributeType, Schema schema, SchemaManager
entry.put( MetaSchemaConstants.M_SINGLE_VALUE_AT, getBoolean( attributeType.isSingleValued() ) );
entry.put( MetaSchemaConstants.M_USAGE_AT, attributeType.getUsage().toString() );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

injectCommon( attributeType, entry, schemaManager );

@@ -421,7 +424,7 @@ public Entry convert( ObjectClass objectClass, Schema schema, SchemaManager sche
entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_OBJECT_CLASS_OC );
entry.put( MetaSchemaConstants.M_TYPE_OBJECT_CLASS_AT, objectClass.getType().toString() );
entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );

injectCommon( objectClass, entry, schemaManager );
Attribute attr;
@@ -33,7 +33,6 @@
*/
public final class DateUtils
{

/**
* Private constructor.
*/
@@ -43,10 +42,9 @@ private DateUtils()


/**
* Return a Date instance from a String
*
* @param zuluTime The String to convert
* @return The Date instance
* Converts a generalized time string to a {@link Date} object.
* @param zuluTime the generalized time string
* @return the converted data
*/
public static Date getDate( String zuluTime )
{
@@ -66,9 +64,9 @@ public static Date getDate( String zuluTime )
*
* @return the generalizedTime right now
*/
public static String getGeneralizedTime()
public static String getGeneralizedTime( TimeProvider timeProvider )
{
return new GeneralizedTime( new Date() ).toGeneralizedTime();
return new GeneralizedTime( timeProvider.currentIimeMillis() ).toGeneralizedTime();
}


@@ -207,6 +207,22 @@ public GeneralizedTime( Calendar calendar )
{
setUp( calendar );
}


/**
* Creates a new instance of GeneralizedTime by setting the time to an instance of Calendar.
* @see #GeneralizedTime(Calendar)
*
* @param timeInMillis the time in milliseconds
*/
public GeneralizedTime( long timeInMillis )
{
Locale locale = Locale.getDefault();
TimeZone timeZone = TimeZone.getTimeZone( "GMT" );
calendar = Calendar.getInstance( timeZone, locale );
calendar.setTimeInMillis( timeInMillis );
setUp( calendar );
}


/**
@@ -0,0 +1,61 @@
/*
* 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.directory.api.util;


/**
* Implementation of {@link TimeProvider} that always returns a fixed time.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
public class MockTimeProvider implements TimeProvider
{
private long timeInMillis;


public MockTimeProvider()
{
this.timeInMillis = System.currentTimeMillis();
}


public long currentIimeMillis()
{
return timeInMillis;
}


public void setTimeInMillis( long timeInMillis )
{
this.timeInMillis = timeInMillis;
}


public void addMillis( long millis )
{
this.timeInMillis += millis;
}


public void substractMillis( long millis )
{
this.timeInMillis -= millis;
}
}
@@ -0,0 +1,51 @@
/*
* 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.directory.api.util;


/**
* Provides the current time, i.e. <code>System.currentTimeMillis()</code>.
* This abstraction is done to be able to switch the implementation for time dependent tests.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
public interface TimeProvider
{
/**
* The default time provider that always returns the system time.
*/
TimeProvider DEFAULT = new TimeProvider()
{
//@Override
public long currentIimeMillis()
{
return System.currentTimeMillis();
}
};


/**
* Gets the current time in milliseconds sind 1970-01-01 UTC.
*
* @return the current time in milliseconds sind 1970-01-01 UTC
*/
long currentIimeMillis();

}
@@ -20,8 +20,13 @@

package org.apache.directory.api.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.text.ParseException;
import java.util.Date;

import org.junit.jupiter.api.Test;

/**
@@ -31,6 +36,36 @@
*/
public class DateUtilTest
{
@Test
public void testGetGeneralizedTimeWithDefaultTimeProvider() throws ParseException
{
long t1 = System.currentTimeMillis();
String gt = DateUtils.getGeneralizedTime( TimeProvider.DEFAULT );
long now = new GeneralizedTime( gt ).getTime();
long t2 = System.currentTimeMillis();
assertTrue( t1 <= now );
assertTrue( now <= t2 );
}


@Test
public void testGetGeneralizedTimeWithMockTimeProvider() throws ParseException
{
MockTimeProvider mockTimeProvider = new MockTimeProvider();
mockTimeProvider.setTimeInMillis( 1234567890L );

String gt = DateUtils.getGeneralizedTime( mockTimeProvider );
long t1 = new GeneralizedTime( gt ).getTime();
assertEquals( 1234567890L, t1 );
}


@Test
public void testGetDate()
{
Date date = DateUtils.getDate( "19700101000000.000Z" );
assertEquals( 0, date.getTime() );
}

@Test
public void testInfinite()

0 comments on commit 2a10bd4

Please sign in to comment.