Source Code Cross Referenced for IntegerTest.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:        /**
019:         * @author Stepan M. Mishura
020:         * @version $Revision$
021:         */package org.apache.harmony.security.tests.asn1.der;
022:
023:        import java.io.ByteArrayInputStream;
024:        import java.io.IOException;
025:        import java.math.BigInteger;
026:        import java.util.Arrays;
027:
028:        import junit.framework.TestCase;
029:
030:        import org.apache.harmony.security.asn1.ASN1Exception;
031:        import org.apache.harmony.security.asn1.ASN1Integer;
032:        import org.apache.harmony.security.asn1.DerInputStream;
033:        import org.apache.harmony.security.asn1.DerOutputStream;
034:
035:        /**
036:         * ASN.1 DER test for INTEGER type
037:         * 
038:         * @see http://asn1.elibel.tm.fr/en/standards/index.htm
039:         */
040:
041:        public class IntegerTest extends TestCase {
042:
043:            public static void main(String[] args) {
044:                junit.textui.TestRunner.run(IntegerTest.class);
045:            }
046:
047:            public static final Object[][] validTestcase = {
048:                    // BigInteger / two's-complement representation / encoding
049:                    { new BigInteger("0"), null, null },
050:                    { new BigInteger("1"), null, null },
051:                    { new BigInteger("-1"), null, null },
052:                    { new BigInteger("127"), null, null },
053:                    { new BigInteger("-127"), null, null },
054:                    { new BigInteger("128"), null, null },
055:                    { new BigInteger("-128"), null, null },
056:                    { new BigInteger("32767"), null, null },
057:                    { new BigInteger("-32767"), null, null },
058:                    { new BigInteger("32768"), null, null },
059:                    { new BigInteger("-32768"), null, null },
060:                    { new BigInteger("1234567890"), null, null },
061:                    { new BigInteger("-1234567890"), null, null }, { // 20 octets
062:                            new BigInteger(new byte[] { 0x7F, 0x00, 0x00, 0x00,
063:                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
064:                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
065:                                    0x00, 0x00 }), null, null }, };
066:
067:            static {
068:                for (int i = 0; i < validTestcase.length; i++) {
069:                    // get two's-complement representation
070:                    byte[] array = ((BigInteger) validTestcase[i][0])
071:                            .toByteArray();
072:
073:                    // create encoding
074:                    byte[] encoded = new byte[array.length + 2];
075:                    encoded[0] = 0x02;
076:                    encoded[1] = (byte) (encoded.length - 2);
077:                    System.arraycopy(array, 0, encoded, 2, encoded[1]);
078:
079:                    // assign is to testcases
080:                    validTestcase[i][1] = array;
081:                    validTestcase[i][2] = encoded;
082:                }
083:            }
084:
085:            /**
086:             * Tests decoding/encoding integers to/from byte array
087:             */
088:            public void testDecode_Encode() throws IOException {
089:
090:                // oid decoder/encoder for testing
091:                ASN1Integer asn1 = ASN1Integer.getInstance();
092:
093:                // decode from byte array
094:                for (int i = 0; i < validTestcase.length; i++) {
095:                    DerInputStream in = new DerInputStream(
096:                            (byte[]) validTestcase[i][2]);
097:                    assertTrue((validTestcase[i][0]).toString(), // message
098:                            Arrays.equals((byte[]) validTestcase[i][1], // expected
099:                                    (byte[]) asn1.decode(in))); // returned
100:                }
101:
102:                // decode from input stream
103:                for (int i = 0; i < validTestcase.length; i++) {
104:                    DerInputStream in = new DerInputStream(
105:                            new ByteArrayInputStream(
106:                                    (byte[]) validTestcase[i][2]));
107:                    assertTrue((validTestcase[i][0]).toString(), //message
108:                            Arrays.equals((byte[]) validTestcase[i][1], //expected
109:                                    (byte[]) asn1.decode(in))); //returned
110:                }
111:
112:                // encoding
113:                for (int i = 0; i < validTestcase.length; i++) {
114:                    DerOutputStream out = new DerOutputStream(asn1,
115:                            validTestcase[i][1]);
116:                    assertTrue((validTestcase[i][0]).toString(), //message
117:                            Arrays.equals((byte[]) validTestcase[i][2], //expected
118:                                    out.encoded));//returned
119:                }
120:            }
121:
122:            /**
123:             * Tests invalid ASN.1 integer encodings
124:             */
125:            public void testDecode_Invalid() throws IOException {
126:                byte[][] invalid = new byte[][] {
127:                // wrong tag: tag is not 0x02
128:                        new byte[] { 0x01, 0x01, 0x00 },
129:                        // wrong length: length is 0
130:                        new byte[] { 0x02, 0x00 },
131:                        // wrong content: is not encoded in minimum number of octets 
132:                        new byte[] { 0x02, 0x02, 0x00, 0x00 },
133:                        // wrong content: is not encoded in minimum number of octets 
134:                        new byte[] { 0x02, 0x02, (byte) 0xFF, (byte) 0x80 } };
135:
136:                for (int i = 0; i < invalid.length; i++) {
137:                    try {
138:                        DerInputStream in = new DerInputStream(invalid[i]);
139:                        ASN1Integer.getInstance().decode(in);
140:                        fail("No expected ASN1Exception for:" + i);
141:                    } catch (ASN1Exception e) {
142:                    }
143:                }
144:            }
145:
146:            public void testConversion() {
147:                int[] testcase = new int[] { 0, 1, -1, 127, -127, 128, -128,
148:                        32767, -32768, Integer.MAX_VALUE, Integer.MIN_VALUE };
149:
150:                for (int i = 0; i < testcase.length; i++) {
151:                    assertEquals("Testcase: " + i, testcase[i], ASN1Integer
152:                            .toIntValue(ASN1Integer.fromIntValue(testcase[i])));
153:                }
154:            }
155:        }
w_w___w.__ja_v__a__2___s__.c___om___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.