mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Chris Ridd
08.44.2012 5142187d26acf1fca4e229e175a4153c6ad378d6
Fix OPENDJ-559 UTCTimeSyntax parses 32... as 1932
2 files modified
131 ■■■■■ changed files
opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/UTCTimeSyntaxImpl.java 38 ●●●●● patch | view | raw | blame | history
opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/UTCTimeSyntaxTest.java 93 ●●●●● patch | view | raw | blame | history
opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/UTCTimeSyntaxImpl.java
@@ -22,6 +22,7 @@
 *
 *
 *      Copyright 2009 Sun Microsystems, Inc.
 *      Portions copyright 2012 ForgeRock AS
 */
package org.forgerock.opendj.ldap.schema;
@@ -30,6 +31,7 @@
import static org.forgerock.opendj.ldap.schema.SchemaConstants.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
@@ -62,6 +64,12 @@
     */
    private final static SimpleDateFormat DATE_FORMAT;
    /**
     * The date formatter needs help converting 2-digit years.
     */
    private static Date datum1900;
    private static Date datum2000;
    /*
     * Create the date formatter that will be used to construct and parse
     * normalized UTC time values.
@@ -71,6 +79,15 @@
        DATE_FORMAT.setLenient(false);
        DATE_FORMAT.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_UTC));
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(TIME_ZONE_UTC));
        cal.clear();
        cal.set(1900, 0, 1);
        datum1900 = cal.getTime();
        cal.clear();
        cal.set(2000, 0, 1);
        datum2000 = cal.getTime();
        DATE_FORMAT_LOCK = new Object();
    }
@@ -104,6 +121,27 @@
    static Date decodeUTCTimeValue(final String valueString) throws DecodeException {
        try {
            synchronized (DATE_FORMAT_LOCK) {
                // RFC 3280 4.1.2.5.1. defines the datum we need to
                // set for the parser.
                switch (valueString.charAt(0)) {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                    // 00-49
                    DATE_FORMAT.set2DigitYearStart(datum2000);
                    break;
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                default:
                    // 50-99
                    DATE_FORMAT.set2DigitYearStart(datum1900);
                    break;
                }
                return DATE_FORMAT.parse(valueString);
            }
        } catch (final Exception e) {
opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/UTCTimeSyntaxTest.java
@@ -22,13 +22,18 @@
 *
 *
 *      Copyright 2009 Sun Microsystems, Inc.
 *      Portions copyright 2012 ForgeRock AS
 *
 */
package org.forgerock.opendj.ldap.schema;
import static org.forgerock.opendj.ldap.schema.SchemaConstants.SYNTAX_UTC_TIME_OID;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertEquals;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -86,6 +91,94 @@
    }
    /**
     * Tests the {@code decodeUTCTimeValue} method decodes
     * 50-99 into 1950-1999. See RFC 3280 4.1.2.5.1.
     *
     * @throws Exception
     *             If an unexpected problem occurs.
     */
    @Test()
    public void testDecode50to99() throws Exception {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        // values from 50 through 99 inclusive shall have 1900 added to it
        for (int yy = 50; yy <= 99; yy++) {
            String utcString = String.format("%02d0819120000Z", new Integer(yy));
            Date decodedDate = UTCTimeSyntaxImpl.decodeUTCTimeValue(utcString);
            cal.clear();
            cal.setTime(decodedDate);
            int year = cal.get(Calendar.YEAR);
            assertEquals(year, yy + 1900);
        }
    }
    /**
     * Tests the {@code decodeUTCTimeValue} method decodes
     * 00-49 into 2000-2049. See RFC 3280 4.1.2.5.1.
     *
     * @throws Exception
     *             If an unexpected problem occurs.
     */
    @Test()
    public void testDecode00to49() throws Exception {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        // values from 00 through 49 inclusive shall have 2000 added to it
        for (int yy = 0; yy <= 49; yy++) {
            String utcString = String.format("%02d0819120000Z", new Integer(yy));
            Date decodedDate = UTCTimeSyntaxImpl.decodeUTCTimeValue(utcString);
            cal.clear();
            cal.setTime(decodedDate);
            int year = cal.get(Calendar.YEAR);
            assertEquals(year, yy + 2000);
        }
    }
    /**
     * Tests the {@code createUTCTimeValue} method converts
     * 1950-1999 into 50-99. See RFC 3280 4.1.2.5.1.
     *
     * @throws Exception
     *             If an unexpected problem occurs.
     */
    @Test()
    public void testCreate50to99() throws Exception {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        // values from 50 through 99 inclusive shall have 1900 added to it
        for (int yy = 50; yy <= 99; yy++) {
            cal.clear();
            cal.set(1900 + yy, 7, 19, 12, 0, 0); // months are 0..11
            Date date = cal.getTime();
            String createdString = UTCTimeSyntaxImpl.createUTCTimeValue(date);
            String expectedString = String.format("%02d0819120000Z", new Integer(yy));
            assertEquals(expectedString, createdString);
        }
    }
    /**
     * Tests the {@code createUTCTimeValue} method converts
     * 2000-2049 into 00-49. See RFC 3280 4.1.2.5.1.
     *
     * @throws Exception
     *             If an unexpected problem occurs.
     */
    @Test()
    public void testCreate00to49() throws Exception {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        // values from 00 through 49 inclusive shall have 2000 added to it
        for (int yy = 0; yy <= 49; yy++) {
            cal.clear();
            cal.set(2000 + yy, 7, 19, 12, 0, 0); // months are 0..11
            Date date = cal.getTime();
            String createdString = UTCTimeSyntaxImpl.createUTCTimeValue(date);
            String expectedString = String.format("%02d0819120000Z", new Integer(yy));
            assertEquals(expectedString, createdString);
        }
    }
    /**
     * {@inheritDoc}
     */
    @Override