harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r393443 - /incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/GeneralizedTimeTest.java
Date Wed, 12 Apr 2006 12:04:17 GMT
Author: smishura
Date: Wed Apr 12 05:04:15 2006
New Revision: 393443

URL: http://svn.apache.org/viewcvs?rev=393443&view=rev
Log:
New test for ASN1GeneralizedTime

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/GeneralizedTimeTest.java

Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/GeneralizedTimeTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/GeneralizedTimeTest.java?rev=393443&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/GeneralizedTimeTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/asn1/der/GeneralizedTimeTest.java
Wed Apr 12 05:04:15 2006
@@ -0,0 +1,125 @@
+/*
+ *  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.asn1.der;
+
+import java.io.ByteArrayInputStream;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.TimeZone;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.security.asn1.ASN1GeneralizedTime;
+import org.apache.harmony.security.asn1.DerInputStream;
+import org.apache.harmony.security.asn1.DerOutputStream;
+
+/**
+ * ASN.1 DER test for GeneralizedTime type
+ * 
+ * @see http://asn1.elibel.tm.fr/en/standards/index.htm
+ */
+
+public class GeneralizedTimeTest extends TestCase {
+
+    // decoder/encoder for testing
+    private static final ASN1GeneralizedTime gtime = ASN1GeneralizedTime
+            .getInstance();
+
+    // data for testing with format: date string/DER encoding/Date object
+    private static final Object[][] validGeneralizedTimes;
+
+    static {
+        SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
+        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
+
+        validGeneralizedTimes = new Object[][] {
+                // YYYYMMDD-HHMMSS = "19000101000000Z"
+                {
+                        "1 Jan 1900 00:00:00",
+                        new byte[] { 0x18, 0x0F, 0x31, 0x39, 0x30, 0x30, 0x30,
+                                0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
+                                0x30, 0x5A }, null },
+                // YYYYMMDD-HHMMSS = "19490203040506Z"
+                {
+                        "3 Feb 1949 04:05:06",
+                        new byte[] { 0x18, 0x0F, 0x31, 0x39, 0x34, 0x39, 0x30,
+                                0x32, 0x30, 0x33, 0x30, 0x34, 0x30, 0x35, 0x30,
+                                0x36, 0x5A }, null },
+                // YYYYMMDD-HHMMSS = "2000708091011Z"
+                {
+                        "8 Jul 2000 09:10:11",
+                        new byte[] { 0x18, 0x0F, 0x32, 0x30, 0x30, 0x30, 0x30,
+                                0x37, 0x30, 0x38, 0x30, 0x39, 0x31, 0x30, 0x31,
+                                0x31, 0x5A }, null },
+                // YYYYMMDD-HHMMSS = "20501213141516Z"
+                {
+                        "13 Dec 2050 14:15:16",
+                        new byte[] { 0x18, 0x0F, 0x32, 0x30, 0x35, 0x30, 0x31,
+                                0x32, 0x31, 0x33, 0x31, 0x34, 0x31, 0x35, 0x31,
+                                0x36, 0x5A }, null }, };
+
+        try {
+            // fill values for Date objects by parsing date string
+            for (int i = 0; i < validGeneralizedTimes.length; i++) {
+                validGeneralizedTimes[i][2] = sdf
+                        .parseObject((String) validGeneralizedTimes[i][0]);
+            }
+        } catch (ParseException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Verifies DER decoding/encoding ASN.1 GeneralizedTime.
+     * GeneralizedTime expresses Greenwich Mean Time by 
+     * the following pattern: YYYYMMDDHHMMSS'Z' 
+     */
+    public void test_Decode_Encode() throws Exception {
+
+        // decoding byte array
+        for (int i = 0; i < validGeneralizedTimes.length; i++) {
+            DerInputStream in = new DerInputStream(
+                    (byte[]) validGeneralizedTimes[i][1]);
+            assertEquals("Decoding array for " + validGeneralizedTimes[i][0],
+                    validGeneralizedTimes[i][2], //expected
+                    gtime.decode(in)); //decoded
+        }
+
+        // decoding input stream
+        for (int i = 0; i < validGeneralizedTimes.length; i++) {
+            DerInputStream in = new DerInputStream(new ByteArrayInputStream(
+                    (byte[]) validGeneralizedTimes[i][1]));
+            assertEquals("Decoding stream for " + validGeneralizedTimes[i][0],
+                    validGeneralizedTimes[i][2], //expected
+                    gtime.decode(in)); //decoded
+        }
+
+        // encoding date object
+        for (int i = 0; i < validGeneralizedTimes.length; i++) {
+            DerOutputStream out = new DerOutputStream(gtime,
+                    validGeneralizedTimes[i][2]);
+            assertTrue("Encoding date for " + validGeneralizedTimes[i][0],
+                    Arrays.equals((byte[]) validGeneralizedTimes[i][1], // expected
+                            out.encoded)); //encoded
+        }
+    }
+
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(GeneralizedTimeTest.class);
+    }
+}



Mime
View raw message