1 package org.codehaus.plexus.digest;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.codehaus.plexus.PlexusTestCase;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.security.NoSuchAlgorithmException;
24
25
26
27
28
29
30 public class DigesterTest
31 extends PlexusTestCase
32 {
33 private static final String MD5 = "adbc688ce77fa2aece4bb72cad9f98ba";
34
35 private static final String SHA1 = "2a7b459938e12a2dc35d1bf6cff35e9c2b592fa9";
36
37 private static final String WRONG_SHA1 = "4d8703779816556cdb8be7f6bb5c954f4b5730e2";
38
39 private Digester sha1Digest;
40
41 private Digester md5Digest;
42
43 protected void setUp()
44 throws Exception
45 {
46 super.setUp();
47
48 sha1Digest = (Digester) lookup( Digester.ROLE, "sha1" );
49 md5Digest = (Digester) lookup( Digester.ROLE, "md5" );
50 }
51
52 public void testAlgorithm()
53 {
54 assertEquals( "SHA-1", sha1Digest.getAlgorithm() );
55 assertEquals( "MD5", md5Digest.getAlgorithm() );
56 }
57
58 public void testBareDigestFormat()
59 throws DigesterException, IOException
60 {
61 File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
62
63 try
64 {
65 md5Digest.verify( file, MD5 );
66 }
67 catch ( DigesterException e )
68 {
69 fail( "Bare format MD5 must not throw exception" );
70 }
71
72 try
73 {
74 sha1Digest.verify( file, SHA1 );
75 }
76 catch ( DigesterException e )
77 {
78 fail( "Bare format SHA1 must not throw exception" );
79 }
80
81 try
82 {
83 sha1Digest.verify( file, WRONG_SHA1 );
84 fail( "wrong checksum must throw an exception" );
85 }
86 catch ( DigesterException e )
87 {
88
89 }
90 }
91
92 public void testOpensslDigestFormat()
93 throws IOException, DigesterException
94 {
95 File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
96
97 try
98 {
99 md5Digest.verify( file, "MD5(test-file.txt)= " + MD5 );
100 }
101 catch ( DigesterException e )
102 {
103 fail( "OpenSSL MD5 format must not cause exception" );
104 }
105
106 try
107 {
108 md5Digest.verify( file, "MD5 (test-file.txt) = " + MD5 );
109 }
110 catch ( DigesterException e )
111 {
112 fail( "FreeBSD MD5 format must not cause exception" );
113 }
114
115 try
116 {
117 sha1Digest.verify( file, "SHA1(test-file.txt)= " + SHA1 );
118 }
119 catch ( DigesterException e )
120 {
121 fail( "OpenSSL SHA1 format must not cause exception" );
122 }
123
124 try
125 {
126 sha1Digest.verify( file, "SHA1 (test-file.txt) = " + SHA1 );
127 }
128 catch ( DigesterException e )
129 {
130 fail( "FreeBSD SHA1 format must not cause exception" );
131 }
132
133 try
134 {
135 sha1Digest.verify( file, "SHA1 (FOO) = " + SHA1 );
136 fail( "Wrong filename should cause an exception" );
137 }
138 catch ( DigesterException e )
139 {
140
141 }
142
143 try
144 {
145 sha1Digest.verify( file, "SHA1 (test-file.txt) = " + WRONG_SHA1 );
146 fail( "Wrong sha1 should cause an exception" );
147 }
148 catch ( DigesterException e )
149 {
150
151 }
152 }
153
154 public void testGnuDigestFormat()
155 throws NoSuchAlgorithmException, IOException, DigesterException
156 {
157 File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
158
159 try
160 {
161 md5Digest.verify( file, MD5 + " *test-file.txt" );
162 }
163 catch ( DigesterException e )
164 {
165 fail( "GNU format MD5 must not cause exception" );
166 }
167
168 try
169 {
170 md5Digest.verify( file, MD5 + " test-file.txt" );
171 }
172 catch ( DigesterException e )
173 {
174 fail( "GNU text format MD5 must not cause exception" );
175 }
176
177 try
178 {
179 sha1Digest.verify( file, SHA1 + " *test-file.txt" );
180 }
181 catch ( DigesterException e )
182 {
183 fail( "GNU format SHA1 must not cause exception" );
184 }
185
186 try
187 {
188 sha1Digest.verify( file, SHA1 + " test-file.txt" );
189 }
190 catch ( DigesterException e )
191 {
192 fail( "GNU text format SHA1 must not cause exception" );
193 }
194
195 try
196 {
197 sha1Digest.verify( file, SHA1 + " FOO" );
198 fail( "Wrong filename cause an exception" );
199 }
200 catch ( DigesterException e )
201 {
202
203 }
204
205 try
206 {
207 sha1Digest.verify( file, WRONG_SHA1 + " test-file.txt" );
208 fail( "Wrong SHA1 cause an exception" );
209 }
210 catch ( DigesterException e )
211 {
212
213 }
214 }
215
216 public void testUntrimmedContent()
217 throws NoSuchAlgorithmException, IOException
218 {
219 File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
220 try
221 {
222 sha1Digest.verify( file, SHA1 + " *test-file.txt \n" );
223 }
224 catch ( DigesterException e )
225 {
226 fail( "GNU untrimmed SHA1 must not cause exception" );
227 }
228 }
229 }