Source Code Cross Referenced for GeneralizedTimeTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » security » tests » asn1 » der » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.security.tests.asn1.der 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.security.tests.asn1.der;
019:
020:        import java.io.ByteArrayInputStream;
021:        import java.io.IOException;
022:        import java.text.ParseException;
023:        import java.text.SimpleDateFormat;
024:        import java.util.Arrays;
025:        import java.util.Date;
026:        import java.util.Locale;
027:        import java.util.TimeZone;
028:
029:        import junit.framework.TestCase;
030:
031:        import org.apache.harmony.security.asn1.ASN1GeneralizedTime;
032:        import org.apache.harmony.security.asn1.DerInputStream;
033:        import org.apache.harmony.security.asn1.DerOutputStream;
034:
035:        /**
036:         * ASN.1 DER test for GeneralizedTime type
037:         * 
038:         * @see http://asn1.elibel.tm.fr/en/standards/index.htm
039:         */
040:
041:        public class GeneralizedTimeTest extends TestCase {
042:
043:            // decoder/encoder for testing
044:            private static final ASN1GeneralizedTime gtime = ASN1GeneralizedTime
045:                    .getInstance();
046:
047:            // data for testing with format: date string/DER encoding/Date object
048:            private static final Object[][] validGeneralizedTimes;
049:
050:            static {
051:                SimpleDateFormat sdf = new SimpleDateFormat(
052:                        "dd MMM yyyy HH:mm:ss", Locale.US);
053:                sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
054:
055:                validGeneralizedTimes = new Object[][] {
056:                // YYYYMMDD-HHMMSS = "19000101000000Z"
057:                        {
058:                                "1 Jan 1900 00:00:00",
059:                                new byte[] { 0x18, 0x0F, 0x31, 0x39, 0x30,
060:                                        0x30, 0x30, 0x31, 0x30, 0x31, 0x30,
061:                                        0x30, 0x30, 0x30, 0x30, 0x30, 0x5A },
062:                                null },
063:                        // YYYYMMDD-HHMMSS = "19490203040506Z"
064:                        {
065:                                "3 Feb 1949 04:05:06",
066:                                new byte[] { 0x18, 0x0F, 0x31, 0x39, 0x34,
067:                                        0x39, 0x30, 0x32, 0x30, 0x33, 0x30,
068:                                        0x34, 0x30, 0x35, 0x30, 0x36, 0x5A },
069:                                null },
070:                        // YYYYMMDD-HHMMSS = "2000708091011Z"
071:                        {
072:                                "8 Jul 2000 09:10:11",
073:                                new byte[] { 0x18, 0x0F, 0x32, 0x30, 0x30,
074:                                        0x30, 0x30, 0x37, 0x30, 0x38, 0x30,
075:                                        0x39, 0x31, 0x30, 0x31, 0x31, 0x5A },
076:                                null },
077:                        // YYYYMMDD-HHMMSS = "20501213141516Z"
078:                        {
079:                                "13 Dec 2050 14:15:16",
080:                                new byte[] { 0x18, 0x0F, 0x32, 0x30, 0x35,
081:                                        0x30, 0x31, 0x32, 0x31, 0x33, 0x31,
082:                                        0x34, 0x31, 0x35, 0x31, 0x36, 0x5A },
083:                                null },
084:                        // YYYYMMDD-HHMMSS = "20501213141516Z"
085:                        {
086:                                "29 Mar 2332 06:56:40",
087:                                new byte[] { 0x18, 0x0F, 0x32, 0x33, 0x33,
088:                                        0x32, 0x30, 0x33, 0x32, 0x39, 0x30,
089:                                        0x36, 0x35, 0x36, 0x34, 0x30, 0x5A },
090:                                null }, };
091:
092:                try {
093:                    // fill values for Date objects by parsing date string
094:                    for (int i = 0; i < validGeneralizedTimes.length; i++) {
095:                        validGeneralizedTimes[i][2] = sdf
096:                                .parseObject((String) validGeneralizedTimes[i][0]);
097:                    }
098:                } catch (ParseException e) {
099:                    throw new RuntimeException(e);
100:                }
101:            }
102:
103:            /**
104:             * Verifies DER decoding/encoding ASN.1 GeneralizedTime.
105:             * GeneralizedTime expresses Greenwich Mean Time by 
106:             * the following pattern: YYYYMMDDHHMMSS'Z' 
107:             */
108:            public void test_Decode_Encode() throws Exception {
109:
110:                // decoding byte array
111:                for (int i = 0; i < validGeneralizedTimes.length; i++) {
112:                    DerInputStream in = new DerInputStream(
113:                            (byte[]) validGeneralizedTimes[i][1]);
114:                    assertEquals("Decoding array for "
115:                            + validGeneralizedTimes[i][0],
116:                            validGeneralizedTimes[i][2], //expected
117:                            gtime.decode(in)); //decoded
118:                }
119:
120:                // decoding input stream
121:                for (int i = 0; i < validGeneralizedTimes.length; i++) {
122:                    DerInputStream in = new DerInputStream(
123:                            new ByteArrayInputStream(
124:                                    (byte[]) validGeneralizedTimes[i][1]));
125:                    assertEquals("Decoding stream for "
126:                            + validGeneralizedTimes[i][0],
127:                            validGeneralizedTimes[i][2], //expected
128:                            gtime.decode(in)); //decoded
129:                }
130:
131:                // encoding date object
132:                for (int i = 0; i < validGeneralizedTimes.length; i++) {
133:                    DerOutputStream out = new DerOutputStream(gtime,
134:                            validGeneralizedTimes[i][2]);
135:                    assertTrue("Encoding date for "
136:                            + validGeneralizedTimes[i][0], Arrays.equals(
137:                            (byte[]) validGeneralizedTimes[i][1], // expected
138:                            out.encoded)); //encoded
139:                }
140:            }
141:
142:            /**
143:             * Tests milliseconds result of encoding/decoding on the date after 2050.
144:             */
145:            public void test_Milliseconds() throws IOException {
146:                // Regression test for HARMONY-1252
147:                long old_date = 11431151800000L;
148:                long new_date = ((Date) gtime.decode(gtime.encode(new Date(
149:                        old_date)))).getTime();
150:                assertEquals(old_date, new_date);
151:            }
152:
153:            public void test_EncodeMilliseconds() throws IOException {
154:                //cRegression for HARMONY-2302
155:                long old_date = 1164358741071L;
156:                long new_date = ((Date) gtime.decode(gtime.encode(new Date(
157:                        old_date)))).getTime();
158:                assertEquals(old_date, new_date);
159:            }
160:
161:            public static void main(String[] args) {
162:                junit.textui.TestRunner.run(GeneralizedTimeTest.class);
163:            }
164:        }
w___w_w_.___j___ava2_s___.__co___m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.