Source Code Cross Referenced for DigestInputStream2Test.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » security » tests » java » security » 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.java.security 
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.java.security;
019:
020:        import java.io.ByteArrayInputStream;
021:        import java.io.IOException;
022:        import java.security.DigestInputStream;
023:        import java.security.MessageDigest;
024:        import java.security.NoSuchAlgorithmException;
025:
026:        public class DigestInputStream2Test extends junit.framework.TestCase {
027:
028:            ByteArrayInputStream inStream;
029:
030:            ByteArrayInputStream inStream1;
031:
032:            MessageDigest digest;
033:
034:            /**
035:             * @tests java.security.DigestInputStream#DigestInputStream(java.io.InputStream,
036:             *        java.security.MessageDigest)
037:             */
038:            public void test_ConstructorLjava_io_InputStreamLjava_security_MessageDigest() {
039:                // Test for method java.security.DigestInputStream(java.io.InputStream,
040:                // java.security.MessageDigest)
041:                DigestInputStream dis = new DigestInputStream(inStream, digest);
042:                assertNotNull("Constructor returned null instance", dis);
043:            }
044:
045:            /**
046:             * @tests java.security.DigestInputStream#getMessageDigest()
047:             */
048:            public void test_getMessageDigest() {
049:                // Test for method java.security.MessageDigest
050:                // java.security.DigestInputStream.getMessageDigest()
051:                DigestInputStream dis = new DigestInputStream(inStream, digest);
052:                assertEquals("getMessageDigest returned a bogus result",
053:                        digest, dis.getMessageDigest());
054:            }
055:
056:            /**
057:             * @tests java.security.DigestInputStream#on(boolean)
058:             */
059:            public void test_onZ() throws Exception {
060:                // Test for method void java.security.DigestInputStream.on(boolean)
061:                MessageDigest originalDigest = (MessageDigest) (digest.clone());
062:                MessageDigest noChangeDigest = (MessageDigest) (digest.clone());
063:                DigestInputStream dis = new DigestInputStream(inStream,
064:                        noChangeDigest);
065:                // turn off processing
066:                dis.on(false);
067:                // read some data
068:                int c = dis.read();
069:                assertEquals('T', c);
070:
071:                // make sure the digest for the part where it was off has not
072:                // changed
073:                assertTrue(
074:                        "MessageDigest changed even though processing was off",
075:                        MessageDigest.isEqual(noChangeDigest.digest(),
076:                                originalDigest.digest()));
077:                MessageDigest changeDigest = (MessageDigest) (digest.clone());
078:                dis = new DigestInputStream(inStream, digest);
079:
080:                // turn on processing
081:                dis.on(true);
082:                c = dis.read();
083:                assertEquals('h', c);
084:
085:                // make sure the digest has changed
086:                assertTrue("MessageDigest did not change with processing on",
087:                        !MessageDigest.isEqual(digest.digest(), changeDigest
088:                                .digest()));
089:            }
090:
091:            /**
092:             * @tests java.security.DigestInputStream#read()
093:             */
094:            public void test_read() throws IOException {
095:                // Test for method int java.security.DigestInputStream.read()
096:                DigestInputStream dis = new DigestInputStream(inStream, digest);
097:
098:                // read and compare the data that the inStream has
099:                int c;
100:                while ((c = dis.read()) > -1) {
101:                    int d = inStream1.read();
102:                    assertEquals(d, c);
103:                }// end while
104:            }
105:
106:            /**
107:             * @tests java.security.DigestInputStream#read(byte[], int, int)
108:             */
109:            public void test_read$BII() throws IOException {
110:                // Test for method int java.security.DigestInputStream.read(byte [],
111:                // int, int)
112:                DigestInputStream dis = new DigestInputStream(inStream, digest);
113:                int bytesToRead = inStream.available();
114:                byte buf1[] = new byte[bytesToRead + 5];
115:                byte buf2[] = new byte[bytesToRead + 5];
116:                // make sure we're actually reading some data
117:                assertTrue("No data to read for this test", bytesToRead > 0);
118:
119:                // read and compare the data that the inStream has
120:                int bytesRead1 = dis.read(buf1, 5, bytesToRead);
121:                int bytesRead2 = inStream1.read(buf2, 5, bytesToRead);
122:                assertEquals("Didn't read the same from each stream",
123:                        bytesRead1, bytesRead2);
124:                assertEquals("Didn't read the entire", bytesRead1, bytesToRead);
125:                // compare the arrays
126:                boolean same = true;
127:                for (int i = 0; i < bytesToRead + 5; i++) {
128:                    if (buf1[i] != buf2[i]) {
129:                        same = false;
130:                    }
131:                }// end for 
132:                assertTrue("Didn't get the same data", same);
133:            }
134:
135:            /**
136:             * @tests java.security.DigestInputStream#setMessageDigest(java.security.MessageDigest)
137:             */
138:            public void test_setMessageDigestLjava_security_MessageDigest() {
139:                // Test for method void
140:                // java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)
141:                DigestInputStream dis = new DigestInputStream(inStream, null);
142:
143:                // make sure the digest is null when it's not been set
144:                assertNull(
145:                        "Uninitialised MessageDigest should have been returned as null",
146:                        dis.getMessageDigest());
147:                dis.setMessageDigest(digest);
148:                assertEquals("Wrong MessageDigest was returned.", digest, dis
149:                        .getMessageDigest());
150:            }
151:
152:            /**
153:             * Sets up the fixture, for example, open a network connection. This method
154:             * is called before a test is executed.
155:             */
156:            protected void setUp() {
157:                // create a ByteArrayInputStream to perform digesting on
158:                inStream = new ByteArrayInputStream(
159:                        "This is a test string for digesting".getBytes());
160:                inStream1 = new ByteArrayInputStream(
161:                        "This is a test string for digesting".getBytes());
162:                try {
163:                    digest = MessageDigest.getInstance("SHA-1");
164:                } catch (NoSuchAlgorithmException e) {
165:                    fail("Unable to find SHA-1 algorithm");
166:                }
167:            }
168:        }
www.___j_a__v__a___2_s.__co__m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.