Source Code Cross Referenced for BerInputStreamTest.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.ASN1Constants;
031:        import org.apache.harmony.security.asn1.ASN1Exception;
032:        import org.apache.harmony.security.asn1.BerInputStream;
033:
034:        /**
035:         * Tests BerInputStream implementation
036:         * 
037:         * @see http://asn1.elibel.tm.fr/en/standards/index.htm
038:         */
039:
040:        public class BerInputStreamTest extends TestCase {
041:
042:            public static void main(String[] args) {
043:                junit.textui.TestRunner.run(BerInputStreamTest.class);
044:            }
045:
046:            /**
047:             * @tests org.apache.harmony.security.asn1.BerInputStream#BerInputStream(
048:             *        java.io.ByteArrayInputStream)
049:             */
050:            public void test_CtorLjava_io_ByteArrayInputStream()
051:                    throws IOException {
052:
053:                //
054:                // tests for decoding initial length of encodings
055:                //
056:                Object[][] testcase = {
057:                // length = 0x01
058:                        { new byte[] { 0x30, (byte) 0x81, 0x01 },
059:                                BigInteger.valueOf(1) },
060:                        // length = 0xFF
061:                        { new byte[] { 0x30, (byte) 0x81, (byte) 0xFF },
062:                                BigInteger.valueOf(0xFF) },
063:                        // length = 0x0101
064:                        { new byte[] { 0x30, (byte) 0x82, 0x01, 0x01 },
065:                                BigInteger.valueOf(0x0101) },
066:                        // length = 0xFFFF
067:                        {
068:                                new byte[] { 0x30, (byte) 0x82, (byte) 0xFF,
069:                                        (byte) 0xFF },
070:                                BigInteger.valueOf(0xFFFF) },
071:                        // length = 0x0FFFFF
072:                        {
073:                                new byte[] { 0x30, (byte) 0x83, 0x0F,
074:                                        (byte) 0xFF, (byte) 0xFF },
075:                                BigInteger.valueOf(0x0FFFFF) },
076:                        // length = 0xFFFFFF
077:                        {
078:                                new byte[] { 0x30, (byte) 0x83, (byte) 0xFF,
079:                                        (byte) 0xFF, (byte) 0xFF },
080:                                BigInteger.valueOf(0xFFFFFF) },
081:                        // length = 0xFFFFFF (encoded length has extra byte)
082:                        {
083:                                new byte[] { 0x30, (byte) 0x84, 0x00,
084:                                        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF },
085:                                BigInteger.valueOf(0xFFFFFF) }, };
086:
087:                // positive testcases
088:                for (int i = 0; i < testcase.length; i++) {
089:                    try {
090:                        BerInputStream in = new BerInputStream(
091:                                new ByteArrayInputStream(
092:                                        (byte[]) testcase[i][0]));
093:
094:                        int expected = ((BigInteger) testcase[i][1]).intValue();
095:
096:                        assertEquals(expected, in.getLength());
097:                    } catch (IOException e) {
098:                        e.printStackTrace();
099:                        fail("Testcase: " + i + "\nUnexpected exception." + e);
100:                    }
101:                }
102:
103:                // negative testcase
104:                try {
105:                    new BerInputStream(new ByteArrayInputStream(new byte[] {
106:                            0x30, (byte) 0x84, 0x01, 0x01, 0x01, 0x01 }));
107:                    fail("No expected ASN1Exception");
108:                } catch (ASN1Exception e) {
109:                    assertTrue(e.getMessage().startsWith("Too long"));
110:                }
111:
112:                //
113:                // Test for correct internal array reallocation
114:                // Regression for HARMONY-5054
115:                //
116:
117:                // must be greater then buffer initial size (16K)
118:                int arrayLength = 17000;
119:
120:                // 1 byte for tag and 3 for length
121:                byte[] encoding = new byte[arrayLength + 4];
122:
123:                // fill tag and length bytes
124:                encoding[0] = ASN1Constants.TAG_OCTETSTRING;
125:                encoding[1] = (byte) 0x82; // length is encoded in two bytes
126:                encoding[2] = (byte) (arrayLength >> 8);
127:                encoding[3] = (byte) (arrayLength & 0xFF);
128:
129:                BerInputStream in = new BerInputStream(
130:                        new ByteArrayInputStream(encoding));
131:                assertEquals(encoding.length, in.getBuffer().length);
132:            }
133:
134:            /**
135:             * @tests org.apache.harmony.security.asn1.BerInputStream#BerInputStream(byte[],
136:             *        int,int)
137:             */
138:            public void test_Ctor$LbyteLintLint() throws IOException {
139:
140:                //
141:                // tests for 'expectedLength' parameter
142:                //
143:                byte[] encoded = new byte[] { 0x01, 0x01, 0x03, // boolean bytes
144:                        0x06, 0x02, 0x01, 0x03, // oid bytes
145:                        0x01, 0x00 // just random bytes
146:                };
147:
148:                // pass boolean encoding
149:                BerInputStream in = new BerInputStream(encoded, 0, 3);
150:                assertEquals("boolean", 1, in.getLength());
151:
152:                // pass oid encoding
153:                in = new BerInputStream(encoded, 3, 4);
154:                assertEquals("boolean", 2, in.getLength());
155:
156:                // pass random encoding (equals to ANY)
157:                in = new BerInputStream(encoded, 7, 2);
158:                assertEquals("any", 0, in.getLength());
159:
160:                // extra bytes for oid
161:                try {
162:                    new BerInputStream(encoded, 3, 5);
163:                    fail("No expected ASN1Exception");
164:                } catch (ASN1Exception e) {
165:                    assertEquals("Wrong content length", e.getMessage());
166:                }
167:
168:                // less bytes for oid
169:                try {
170:                    new BerInputStream(encoded, 3, 3);
171:                    fail("No expected ASN1Exception");
172:                } catch (ASN1Exception e) {
173:                    assertEquals("Wrong content length", e.getMessage());
174:                }
175:            }
176:
177:            /**
178:             * @tests org.apache.harmony.security.asn1.BerInputStream#readContent()
179:             */
180:            public void test_readContent() throws IOException {
181:
182:                byte[] encoding = { ASN1Constants.TAG_OCTETSTRING, 0x0F, 0x01,
183:                        0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
184:                        0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
185:
186:                // a custom input stream that doesn't return all data at once
187:                ByteArrayInputStream in = new ByteArrayInputStream(encoding) {
188:                    public int read(byte[] b, int off, int len) {
189:                        if (len < 2) {
190:                            return super .read(b, off, len);
191:                        } else {
192:                            return super .read(b, off, 4);
193:                        }
194:
195:                    }
196:                };
197:
198:                BerInputStream berIn = new BerInputStream(in);
199:                berIn.readContent();
200:
201:                assertTrue(Arrays.equals(encoding, berIn.getEncoded()));
202:
203:                //
204:                // negative test case: the stream returns only 4 bytes of content
205:                //
206:                in = new ByteArrayInputStream(encoding) {
207:
208:                    int i = 0;
209:
210:                    public int read(byte[] b, int off, int len) {
211:                        if (i == 0) {
212:                            i++;
213:                            return super .read(b, off, 4);
214:                        } else {
215:                            return 0;
216:                        }
217:
218:                    }
219:                };
220:                berIn = new BerInputStream(in);
221:                try {
222:                    berIn.readContent();
223:                    fail("No expected ASN1Exception");
224:                } catch (ASN1Exception e) {
225:                }
226:            }
227:        }
w_w___w__.___j__av__a__2s.__c_o___m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.