Source Code Cross Referenced for CipherInputStreamTest.java in  » Apache-Harmony-Java-SE » javax-package » javax » crypto » 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 » javax package » javax.crypto 
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 Alexander Y. Kleymenov
020:         * @version $Revision$
021:         */package javax.crypto;
022:
023:        import java.io.ByteArrayInputStream;
024:        import javax.crypto.NullCipher;
025:
026:        import junit.framework.TestCase;
027:
028:        /**
029:         */
030:
031:        public class CipherInputStreamTest extends TestCase {
032:
033:            private static class TestInputStream extends ByteArrayInputStream {
034:                private boolean closed = false;
035:
036:                public TestInputStream(byte[] data) {
037:                    super (data);
038:                }
039:
040:                public void close() {
041:                    closed = true;
042:                }
043:
044:                public boolean wasClosed() {
045:                    return closed;
046:                }
047:            }
048:
049:            /**
050:             * CipherInputStream(InputStream is) method testing. Tests that
051:             * CipherInputStream uses NullCipher if Cipher is not specified
052:             * in the constructor.
053:             */
054:            public void testCipherInputStream() throws Exception {
055:                byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
056:                        50, 127 };
057:                TestInputStream tis = new TestInputStream(data);
058:                CipherInputStream cis = new CipherInputStream(tis);
059:
060:                for (int i = 0; i < data.length; i++) {
061:                    if ((byte) cis.read() != data[i]) {
062:                        fail("NullCipher should be used "
063:                                + "if Cipher is not specified.");
064:                    }
065:                }
066:                if (cis.read() != -1) {
067:                    fail("NullCipher should be used if Cipher is not specified.");
068:                }
069:            }
070:
071:            /**
072:             * read() method testing. Tests that method returns the correct value
073:             * (related to the InputStream) and that it returns -1 at the end of stream.
074:             */
075:            public void testRead1() throws Exception {
076:                byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
077:                        50, 127 };
078:                TestInputStream tis = new TestInputStream(data);
079:                CipherInputStream cis = new CipherInputStream(tis,
080:                        new NullCipher());
081:                byte res;
082:                for (int i = 0; i < data.length; i++) {
083:                    if ((res = (byte) cis.read()) != data[i]) {
084:                        fail("read() returned the incorrect value. "
085:                                + "Expected: " + data[i] + ", Got: " + res
086:                                + ".");
087:                    }
088:                }
089:                if (cis.read() != -1) {
090:                    fail("read() should return -1 at the end of the stream.");
091:                }
092:            }
093:
094:            /**
095:             * read(byte[] b) method testing. Tests that method returns the correct
096:             * value (related to the InputStream) and that it returns -1 at the end of
097:             * stream.
098:             */
099:            public void testRead2() throws Exception {
100:                byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
101:                        50, 127 };
102:                TestInputStream tis = new TestInputStream(data);
103:                CipherInputStream cis = new CipherInputStream(tis,
104:                        new NullCipher());
105:
106:                int expected = data.length;
107:                byte[] result = new byte[expected];
108:
109:                int ind = 0; // index into the data array (to check the got data)
110:                int got = cis.read(result); // the number of got bytes
111:                while (true) {
112:                    for (int j = 0; j < got - ind; j++) {
113:                        if (result[j] != data[ind + j]) {
114:                            fail("read(byte[] b) returned incorrect data.");
115:                        }
116:                    }
117:                    if (got == expected) {
118:                        break;
119:                    } else if (got > expected) {
120:                        fail("The data returned by read(byte[] b) "
121:                                + "is larger than expected.");
122:                    } else {
123:                        ind = got;
124:                        got += cis.read(result);
125:                    }
126:                }
127:                if (cis.read(result) != -1) {
128:                    fail("read(byte[] b) should return -1 "
129:                            + "at the end of the stream.");
130:                }
131:            }
132:
133:            /**
134:             * read(byte[] b, int off, int len) method testing. Tests that method
135:             * returns the correct value (related to the InputStream), that it discards
136:             * bytes in the case of null buffer, and that it returns -1 at the end of
137:             * stream.
138:             */
139:            public void testRead3() throws Exception {
140:                byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
141:                        50, 127 };
142:                TestInputStream tis = new TestInputStream(data);
143:                CipherInputStream cis = new CipherInputStream(tis,
144:                        new NullCipher());
145:
146:                int expected = data.length;
147:                byte[] result = new byte[expected];
148:
149:                int skip = 2;
150:                int ind = skip; // index into the data array (to check the got data)
151:                // should read and discard bytes;
152:                cis.read(null, 0, skip);
153:                int got = skip + cis.read(result, 0, 1); // the number of got bytes
154:                while (true) {
155:                    for (int j = 0; j < got - ind; j++) {
156:                        assertEquals("read(byte[] b, int off, int len) "
157:                                + "returned incorrect data.", result[j],
158:                                data[ind + j]);
159:                    }
160:                    if (got == expected) {
161:                        break;
162:                    } else if (got > expected) {
163:                        fail("The data returned by "
164:                                + "read(byte[] b, int off, int len) "
165:                                + "is larger than expected.");
166:                    } else {
167:                        ind = got;
168:                        got += cis.read(result, 0, 3);
169:                    }
170:                }
171:                if (cis.read(result, 0, 1) != -1) {
172:                    fail("read() should return -1 at the end of the stream.");
173:                }
174:            }
175:
176:            /**
177:             * skip(long n) method testing. Tests that the method correctly skips the
178:             * bytes.
179:             */
180:            public void testSkip() throws Exception {
181:                byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
182:                        50, 127 };
183:                TestInputStream tis = new TestInputStream(data);
184:                CipherInputStream cis = new CipherInputStream(tis,
185:                        new NullCipher());
186:                int expected = data.length;
187:                byte[] result = new byte[expected];
188:
189:                int skipped = (int) cis.skip(2);
190:                int ind = skipped;
191:                int got = skipped + cis.read(result, 0, 1); // the number of got bytes
192:                while (true) {
193:                    for (int j = 0; j < got - ind; j++) {
194:                        if (result[j] != data[ind + j]) {
195:                            fail("read(byte[] b, int off, int len) "
196:                                    + "returned incorrect data: Expected "
197:                                    + data[ind + j] + ", got: " + result[j]);
198:                        }
199:                    }
200:                    if (got == expected) {
201:                        break;
202:                    } else if (got > expected) {
203:                        fail("The data returned by "
204:                                + "read(byte[] b, int off, int len) "
205:                                + "is larger than expected.");
206:                    } else {
207:                        ind = got;
208:                        got += cis.read(result, 0, 1);
209:                    }
210:                }
211:                if ((got = cis.read(result, 0, 1)) != -1) {
212:                    fail("read() should return -1 at the end of the stream. "
213:                            + "Output is: " + got + ".");
214:                }
215:            }
216:
217:            /**
218:             * available() method testing. Tests that the method always return 0.
219:             */
220:            public void testAvailable() throws Exception {
221:                byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
222:                        50, 127 };
223:                TestInputStream tis = new TestInputStream(data);
224:                CipherInputStream cis = new CipherInputStream(tis,
225:                        new NullCipher());
226:                assertEquals("The returned by available() method value "
227:                        + "should be 0.", cis.available(), 0);
228:            }
229:
230:            /**
231:             * close() method testing. Tests that the method calls the close()
232:             * method of the underlying input stream.
233:             */
234:            public void testClose() throws Exception {
235:                byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
236:                        50, 127 };
237:                TestInputStream tis = new TestInputStream(data);
238:                CipherInputStream cis = new CipherInputStream(tis,
239:                        new NullCipher());
240:                cis.close();
241:                assertTrue("The close() method should call the close() method "
242:                        + "of its underlying input stream.", tis.wasClosed());
243:            }
244:
245:            /**
246:             * markSupported() method testing. Tests that mark is not supported.
247:             */
248:            public void testMarkSupported() {
249:                byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
250:                        50, 127 };
251:                TestInputStream tis = new TestInputStream(data);
252:                CipherInputStream cis = new CipherInputStream(tis,
253:                        new NullCipher());
254:                assertFalse("The returned by markSupported() method value "
255:                        + "should be false.", cis.markSupported());
256:            }
257:
258:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.