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

Chris Ridd
07.09.2012 3c3ca54bfde39ba7df13bd7409b47580cfa0bd39
Fix OPENDJ-559 UTCTimeSyntax parses 32... as 1932
2 files modified
145 ■■■■■ changed files
opends/src/server/org/opends/server/schema/UTCTimeSyntax.java 38 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/schema/UTCTimeSyntaxTest.java 107 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/schema/UTCTimeSyntax.java
@@ -30,6 +30,7 @@
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
@@ -86,6 +87,11 @@
   */
  private static SimpleDateFormat dateFormat;
  /**
   * The date formatter needs help converting 2-digit years.
   */
  private static Date datum1900;
  private static Date datum2000;
  // The default equality matching rule for this syntax.
@@ -109,6 +115,15 @@
    dateFormat.setLenient(false);
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    cal.clear();
    cal.set(1900, 0, 1);
    datum1900 = cal.getTime();
    cal.clear();
    cal.set(2000, 0, 1);
    datum2000 = cal.getTime();
    dateFormatLock = new Object();
  }
@@ -865,10 +880,33 @@
         throws DirectoryException
  {
    String valueString = normalizedValue.toString();
    try
    {
      synchronized (dateFormatLock)
      {
        // 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
            dateFormat.set2DigitYearStart(datum2000);
            break;
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
          default:
            // 50-99
            dateFormat.set2DigitYearStart(datum1900);
            break;
        }
        return dateFormat.parse(valueString);
      }
    }
opends/tests/unit-tests-testng/src/server/org/opends/server/schema/UTCTimeSyntaxTest.java
@@ -23,12 +23,18 @@
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Portions copyright 2012 ForgeRock AS
 *
 */
package org.opends.server.schema;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.opends.server.api.AttributeSyntax;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.ByteString;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -129,4 +135,105 @@
    // sure that the decoded value is within 1000 milliseconds.
    assertTrue(Math.abs(d.getTime() - decodedDate.getTime()) < 1000);
  }
  /**
   * 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 = UTCTimeSyntax.decodeUTCTimeValue(ByteString.valueOf(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 = UTCTimeSyntax.decodeUTCTimeValue(ByteString.valueOf(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 = UTCTimeSyntax.createUTCTimeValue(date).toString();
      String expectedString = String.format("%02d0819120000Z", new Integer(yy));
      assertEquals(expectedString, createdString);
    }
  }
  /**
   * Tests the {@code createUTCTimeValue} method converts
   * 2000-2049 to 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 = UTCTimeSyntax.createUTCTimeValue(date).toString();
      String expectedString = String.format("%02d0819120000Z", new Integer(yy));
      assertEquals(expectedString, createdString);
    }
  }
}