Source Code Cross Referenced for BitStringTest.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.util.Arrays;
026:
027:        import junit.framework.TestCase;
028:
029:        import org.apache.harmony.security.asn1.ASN1BitString;
030:        import org.apache.harmony.security.asn1.ASN1Exception;
031:        import org.apache.harmony.security.asn1.BitString;
032:        import org.apache.harmony.security.asn1.DerInputStream;
033:        import org.apache.harmony.security.asn1.DerOutputStream;
034:        import org.apache.harmony.security.asn1.ASN1BitString.ASN1NamedBitList;
035:
036:        /**
037:         * ASN.1 DER test for Bitstring type
038:         * 
039:         * @see http://asn1.elibel.tm.fr/en/standards/index.htm
040:         */
041:
042:        public class BitStringTest extends TestCase {
043:
044:            public static void main(String[] args) {
045:                junit.textui.TestRunner.run(BitStringTest.class);
046:            }
047:
048:            private static Object[][] validBitstring = new Object[][] {
049:            //bitstring array format: bitstring object/ byte array
050:                    //
051:                    { new BitString(new byte[] {}, 0), // object 
052:                            new byte[] { 0x03, 0x01, 0x00 } },
053:                    //
054:                    { new BitString(new byte[] { 0x05 }, 0), // object 
055:                            new byte[] { 0x03, 0x02, 0x00, 0x05 } },
056:                    //
057:                    { new BitString(new byte[] { (byte) 0x80 }, 7), // object
058:                            new byte[] { 0x03, 0x02, 0x07, (byte) 0x80 } } };
059:
060:            public void testDecode_Encode() throws IOException {
061:
062:                // decoder/encoder for testing
063:                ASN1BitString asn1 = ASN1BitString.getInstance();
064:
065:                // decode from byte array
066:                for (int i = 0; i < validBitstring.length; i++) {
067:                    DerInputStream in = new DerInputStream(
068:                            (byte[]) validBitstring[i][1]);
069:
070:                    BitString expected = (BitString) validBitstring[i][0];
071:                    BitString decoded = (BitString) asn1.decode(in);
072:
073:                    assertEquals("Testcase: " + i, expected.unusedBits,
074:                            decoded.unusedBits);
075:
076:                    assertTrue("Testcase: " + i, Arrays.equals(expected.bytes,
077:                            decoded.bytes));
078:                }
079:
080:                // decode from input stream
081:                for (int i = 0; i < validBitstring.length; i++) {
082:                    DerInputStream in = new DerInputStream(
083:                            new ByteArrayInputStream(
084:                                    (byte[]) validBitstring[i][1]));
085:
086:                    BitString expected = (BitString) validBitstring[i][0];
087:                    BitString decoded = (BitString) asn1.decode(in);
088:
089:                    assertEquals("Testcase: " + i, expected.unusedBits,
090:                            decoded.unusedBits);
091:
092:                    assertTrue("Testcase: " + i, Arrays.equals(expected.bytes,
093:                            decoded.bytes));
094:                }
095:
096:                // encoding
097:                for (int i = 0; i < validBitstring.length; i++) {
098:                    DerOutputStream out = new DerOutputStream(asn1,
099:                            validBitstring[i][0]);
100:                    assertTrue("Testcase: " + i, Arrays.equals(
101:                            (byte[]) validBitstring[i][1], out.encoded));
102:                }
103:            }
104:
105:            public void testDecode_Invalid() throws IOException {
106:                byte[][] invalid = new byte[][] {
107:                // wrong tag: tag is not 0x03
108:                        new byte[] { 0x02, 0x01, 0x00 },
109:                        // wrong length: length is 0
110:                        new byte[] { 0x03, 0x00 },
111:                        // wrong content: unused bits value > 7
112:                        new byte[] { 0x03, 0x03, 0x09, 0x0F, 0x0F },
113:                        // wrong content: not 0 unused bits for empty string
114:                        new byte[] { 0x03, 0x01, 0x01 },
115:                        // wrong content: unused bits in final octet are not 0
116:                        new byte[] { 0x03, 0x02, 0x01, 0x01 },
117:                        // wrong content: constructed encoding
118:                        new byte[] { 0x23, 0x03, 0x03, 0x01, 0x00 } };
119:
120:                for (int i = 0; i < invalid.length; i++) {
121:                    try {
122:                        DerInputStream in = new DerInputStream(invalid[i]);
123:                        ASN1BitString.getInstance().decode(in);
124:                        fail("No expected ASN1Exception for: " + i);
125:                    } catch (ASN1Exception e) {
126:                    }
127:                }
128:            }
129:
130:            //
131:            //
132:            // Named Bit List
133:            //
134:            //
135:
136:            public void testDecodeNamedBitList() throws IOException {
137:
138:                Object[][] testcaseBoolean = new Object[][] {
139:                // bitstring array format: bitstring object/ byte array
140:                        //
141:                        { new boolean[] {}, // object
142:                                new byte[] { 0x03, 0x01, 0x00 } },
143:                        //
144:                        { new boolean[] { true }, // object
145:                                new byte[] { 0x03, 0x02, 0x07, (byte) 0x80 } },
146:                        // 
147:                        { new boolean[] { true, false, true }, // object
148:                                new byte[] { 0x03, 0x02, 0x05, (byte) 0xA0 } },
149:                        //
150:                        {
151:                                new boolean[] { true, true, true, true, true,
152:                                        true, true, true }, // object
153:                                new byte[] { 0x03, 0x02, 0x00, (byte) 0xFF } },
154:                        //
155:                        {
156:                                new boolean[] { false, false, false, false,
157:                                        false, false, false, false, true }, // object
158:                                new byte[] { 0x03, 0x03, 0x07, 0x00,
159:                                        (byte) 0x80 } } };
160:
161:                ASN1NamedBitList decoder = new ASN1NamedBitList();
162:
163:                for (int i = 0; i < testcaseBoolean.length; i++) {
164:                    DerInputStream in = new DerInputStream(
165:                            (byte[]) testcaseBoolean[i][1]);
166:
167:                    assertTrue("Testcase: " + i, Arrays.equals(
168:                            (boolean[]) testcaseBoolean[i][0],
169:                            (boolean[]) decoder.decode(in)));
170:                }
171:            }
172:
173:            public void testDecodeNamedBitList_SizeConstraints()
174:                    throws IOException {
175:
176:                Object[][] testcaseBoolean = new Object[][] {
177:                //bitstring array format: bitstring object/ byte array
178:                        //
179:                        {
180:                                new boolean[] { false, false, false, false,
181:                                        false, false, false, false }, // object
182:                                new byte[] { 0x03, 0x01, 0x00 } },
183:                        //
184:                        {
185:                                new boolean[] { true, false, false, false,
186:                                        false, false, false, false }, // object
187:                                new byte[] { 0x03, 0x02, 0x07, (byte) 0x80 } },
188:                        // 
189:                        {
190:                                new boolean[] { true, false, true, false,
191:                                        false, false, false, false }, // object
192:                                new byte[] { 0x03, 0x02, 0x05, (byte) 0xA0 } },
193:                        //
194:                        {
195:                                new boolean[] { true, true, true, true, true,
196:                                        true, true, true }, // object
197:                                new byte[] { 0x03, 0x02, 0x00, (byte) 0xFF } },
198:                        //
199:                        {
200:                                new boolean[] { false, false, false, false,
201:                                        false, false, false, false, true }, // object
202:                                new byte[] { 0x03, 0x03, 0x07, 0x00,
203:                                        (byte) 0x80 } } };
204:
205:                ASN1NamedBitList decoder = new ASN1NamedBitList(8);
206:
207:                for (int i = 0; i < testcaseBoolean.length; i++) {
208:                    DerInputStream in = new DerInputStream(
209:                            (byte[]) testcaseBoolean[i][1]);
210:
211:                    assertTrue("Testcase: " + i, Arrays.equals(
212:                            (boolean[]) testcaseBoolean[i][0],
213:                            (boolean[]) decoder.decode(in)));
214:                }
215:            }
216:
217:            public void testEncodeNamedBitList() throws IOException {
218:
219:                Object[][] testcaseBoolean = new Object[][] {
220:                //bitstring array format: bitstring object/ byte array
221:                        //
222:                        { new boolean[] {}, // object
223:                                new byte[] { 0x03, 0x01, 0x00 } },
224:                        //
225:                        { new boolean[] { false }, // object
226:                                new byte[] { 0x03, 0x01, 0x00 } },
227:                        //
228:                        { new boolean[] { true }, // object
229:                                new byte[] { 0x03, 0x02, 0x07, (byte) 0x80 } },
230:                        // 
231:                        { new boolean[] { true, false, true }, // object
232:                                new byte[] { 0x03, 0x02, 0x05, (byte) 0xA0 } },
233:                        //
234:                        {
235:                                new boolean[] { true, true, true, true, true,
236:                                        true, true, true }, // object
237:                                new byte[] { 0x03, 0x02, 0x00, (byte) 0xFF } },
238:                        //
239:                        {
240:                                new boolean[] { false, false, false, false,
241:                                        false, false, false, false, true }, // object
242:                                new byte[] { 0x03, 0x03, 0x07, 0x00,
243:                                        (byte) 0x80 } } };
244:
245:                ASN1NamedBitList encoder = new ASN1NamedBitList();
246:
247:                for (int i = 0; i < testcaseBoolean.length; i++) {
248:                    DerOutputStream out = new DerOutputStream(encoder,
249:                            testcaseBoolean[i][0]);
250:                    assertTrue("Testcase: " + i, Arrays.equals(
251:                            (byte[]) testcaseBoolean[i][1], out.encoded));
252:                }
253:            }
254:        }
w___w__w.jav_a___2__s__.c_o_m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.