Index: modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/ValidityTest.java =================================================================== --- modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/ValidityTest.java (revision 0) +++ modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/ValidityTest.java (revision 0) @@ -0,0 +1,57 @@ +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed 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.harmony.security.tests.x509; + +import java.util.Date; + +import org.apache.harmony.security.x509.Validity; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Validity test + */ +public class ValidityTest extends TestCase { + + /** + * Tests the result of encoding/decoding work on the datea + * before and after 2050. + */ + public void test_Encoding_Decoding() throws Exception { + long dateBefore2050 = 1143115180000L; + long dateAfter2050 = 11431151800000L; + Date before = new Date(dateBefore2050); + Date after = new Date(dateAfter2050); + Validity validity = new Validity(before, after); + Validity val = (Validity) Validity.ASN1.decode(validity.getEncoded()); + assertEquals("Date differs from expected.", + dateBefore2050, val.getNotBefore().getTime()); + assertEquals("Date differs from expected.", + dateAfter2050, val.getNotAfter().getTime()); + } + + public static Test suite() { + return new TestSuite(ValidityTest.class); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } +} + Index: modules/security/src/main/java/common/org/apache/harmony/security/x509/Time.java =================================================================== --- modules/security/src/main/java/common/org/apache/harmony/security/x509/Time.java (revision 421196) +++ modules/security/src/main/java/common/org/apache/harmony/security/x509/Time.java (working copy) @@ -46,9 +46,12 @@ ASN1GeneralizedTime.getInstance(), ASN1UTCTime.getInstance() }) { public int getIndex(java.lang.Object object) { - return 1; // always code as ASN1UTCTime () - // FIXME: But it is correct only if the Date to encode - // is before 2050. See rfc 3280 p.22 + // choose encoding method (see RFC 3280 p. 22) + if (((java.util.Date) object).getTime() < 2524608000000L) { + return 1; // it is before 2005, so encode as UTCTime + } else { + return 0; // it is after 2005, encode as GeneralizedTime + } } public Object getObjectToEncode(Object object) {