Home | History | Annotate | Download | only in der
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 /**
     19  * @author Vladimir N. Molotkov
     20  */
     21 
     22 package org.apache.harmony.security.tests.asn1.der;
     23 
     24 import java.io.ByteArrayInputStream;
     25 import java.io.IOException;
     26 import java.text.ParseException;
     27 import java.text.SimpleDateFormat;
     28 import java.util.Date;
     29 import java.util.TimeZone;
     30 
     31 import org.apache.harmony.security.asn1.ASN1GeneralizedTime;
     32 import org.apache.harmony.security.asn1.DerInputStream;
     33 import org.apache.harmony.security.asn1.DerOutputStream;
     34 
     35 import junit.framework.TestCase;
     36 
     37 
     38 /**
     39  * ASN.1 DER test for GeneralizedTime type
     40  *
     41  * @see http://asn1.elibel.tm.fr/en/standards/index.htm
     42  */
     43 public class DerGeneralizedTimeEDTest extends TestCase {
     44 
     45     private ASN1GeneralizedTime gTime = ASN1GeneralizedTime.getInstance();
     46 
     47     /**
     48      * GENERALIZED TIME DER Encoder test
     49      *
     50      * @throws ParseException
     51      */
     52     public final void testGeneralizedEncoder() throws Exception {
     53         // full fractional seconds
     54         Date myDate = getGmtDate(1101980374187L);
     55         byte[] encoded =
     56                 new DerOutputStream(gTime, myDate).encoded;
     57         String rep = new String(encoded, 2, encoded[1] & 0xff, "UTF-8");
     58         assertEquals("full", "20041202093934.187Z", rep);
     59 
     60         // 2 digit fractional seconds (last 0 must be trimmed out)
     61         myDate = getGmtDate(1101980374180L);
     62         encoded =
     63                 new DerOutputStream(gTime, myDate).encoded;
     64         rep = new String(encoded, 2, encoded[1] & 0xff, "UTF-8");
     65         assertEquals("2 fraction", "20041202093934.18Z", rep);
     66 
     67         // 1 digit fractional seconds (last 2 0s must be trimmed out)
     68         myDate = getGmtDate(1101980374100L);
     69         encoded =
     70                 new DerOutputStream(gTime, myDate).encoded;
     71         rep = new String(encoded, 2, encoded[1] & 0xff, "UTF-8");
     72         assertEquals("1 fraction", "20041202093934.1Z", rep);
     73 
     74         // no fractional seconds (last 3 0s and "." must be trimmed out)
     75         myDate = getGmtDate(1101980374000L);
     76         encoded =
     77                 new DerOutputStream(gTime, myDate).encoded;
     78         rep = new String(encoded, 2, encoded[1] & 0xff, "UTF-8");
     79         assertEquals("no fraction", "20041202093934Z", rep);
     80 
     81         // midnight
     82         SimpleDateFormat sdf =
     83                 new SimpleDateFormat("dd.MM.yyyy HH:mm");
     84         sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
     85         myDate = sdf.parse("06.06.2004 00:00");
     86         encoded =
     87                 new DerOutputStream(gTime, myDate).encoded;
     88         rep = new String(encoded, 2, encoded[1] & 0xff, "UTF-8");
     89         assertEquals("midnight", "20040606000000Z", rep);
     90     }
     91 
     92     /**
     93      * GENERALIZED TIME DER Encoder/Decoder test
     94      * (byte array case)
     95      *
     96      * @throws ParseException
     97      * @throws IOException
     98      */
     99     public final void testGeneralizedEncoderDecoder01()
    100             throws ParseException,
    101             IOException {
    102         runTest(false);
    103     }
    104 
    105     /**
    106      * GENERALIZED TIME DER Encoder/Decoder test
    107      * (InputStream case)
    108      *
    109      * @throws ParseException
    110      * @throws IOException
    111      */
    112     public final void testGeneralizedEncoderDecoder02()
    113             throws ParseException,
    114             IOException {
    115         runTest(true);
    116     }
    117 
    118     private final void runTest(boolean useInputStream)
    119             throws IOException, ParseException {
    120         // full fractional seconds
    121         Date myDate = new Date(1101980374187L);
    122         byte[] encoded =
    123                 new DerOutputStream(gTime, myDate).encoded;
    124         DerInputStream dis = useInputStream
    125                 ? new DerInputStream(new ByteArrayInputStream(encoded))
    126                 : new DerInputStream(encoded);
    127         assertEquals("full", myDate, gTime.decode(dis));
    128 
    129         // 2 digit fractional seconds (last 0 must be trimmed out)
    130         myDate = new Date(1101980374180L);
    131         encoded =
    132                 new DerOutputStream(gTime, myDate).encoded;
    133         dis = useInputStream
    134                 ? new DerInputStream(new ByteArrayInputStream(encoded))
    135                 : new DerInputStream(encoded);
    136         assertEquals("2 fraction", myDate, gTime.decode(dis));
    137 
    138         // 1 digit fractional seconds (last 2 0s must be trimmed out)
    139         myDate = new Date(1101980374100L);
    140         encoded =
    141                 new DerOutputStream(gTime, myDate).encoded;
    142         dis = useInputStream
    143                 ? new DerInputStream(new ByteArrayInputStream(encoded))
    144                 : new DerInputStream(encoded);
    145         assertEquals("1 fraction", myDate, gTime.decode(dis));
    146 
    147         // no fractional seconds (last 3 0s and "." must be trimmed out)
    148         myDate = new Date(1101980374000L);
    149         encoded =
    150                 new DerOutputStream(gTime, myDate).encoded;
    151         dis = useInputStream
    152                 ? new DerInputStream(new ByteArrayInputStream(encoded))
    153                 : new DerInputStream(encoded);
    154         assertEquals("no fraction", myDate, gTime.decode(dis));
    155 
    156         // midnight
    157         myDate = new SimpleDateFormat("MM.dd.yyyy HH:mm").
    158                 parse("06.06.2004 00:00");
    159         encoded =
    160                 new DerOutputStream(gTime, myDate).encoded;
    161         dis = useInputStream
    162                 ? new DerInputStream(new ByteArrayInputStream(encoded))
    163                 : new DerInputStream(encoded);
    164         assertEquals("midnight", myDate, gTime.decode(dis));
    165 
    166         // date 100 ms
    167         myDate = new Date(100L);
    168         encoded =
    169                 new DerOutputStream(gTime, myDate).encoded;
    170         dis = useInputStream
    171                 ? new DerInputStream(new ByteArrayInputStream(encoded))
    172                 : new DerInputStream(encoded);
    173         assertEquals("100ms", myDate, gTime.decode(dis));
    174     }
    175 
    176     private static Date getGmtDate(long mills) {
    177         return new Date(mills);
    178     }
    179 }
    180