1 package org.apache.archiva.checksum;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28
29
30
31
32 public class ChecksumTest
33 extends AbstractChecksumTestCase
34 {
35 private static final String UNSET_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
36
37 public void testConstructSha1()
38 {
39 Checksum checksum = new Checksum( ChecksumAlgorithm.SHA1 );
40 assertEquals( "Checksum.algorithm", checksum.getAlgorithm().getAlgorithm(), ChecksumAlgorithm.SHA1
41 .getAlgorithm() );
42 }
43
44 public void testConstructMd5()
45 {
46 Checksum checksum = new Checksum( ChecksumAlgorithm.MD5 );
47 assertEquals( "Checksum.algorithm", checksum.getAlgorithm().getAlgorithm(), ChecksumAlgorithm.MD5
48 .getAlgorithm() );
49 }
50
51 public void testUpdate()
52 {
53 Checksum checksum = new Checksum( ChecksumAlgorithm.SHA1 );
54 byte buf[] = ( "You know, I'm sick of following my dreams, man. "
55 + "I'm just going to ask where they're going and hook up with 'em later. - Mitch Hedberg" ).getBytes();
56 checksum.update( buf, 0, buf.length );
57 assertEquals( "Checksum", "e396119ae0542e85a74759602fd2f81e5d36d762", checksum.getChecksum() );
58 }
59
60 public void testUpdateMany()
61 throws IOException
62 {
63 Checksum checksumSha1 = new Checksum( ChecksumAlgorithm.SHA1 );
64 Checksum checksumMd5 = new Checksum( ChecksumAlgorithm.MD5 );
65 List<Checksum> checksums = new ArrayList<Checksum>();
66 checksums.add( checksumSha1 );
67 checksums.add( checksumMd5 );
68
69 byte buf[] = ( "You know, I'm sick of following my dreams, man. "
70 + "I'm just going to ask where they're going and hook up with 'em later. - Mitch Hedberg" ).getBytes();
71
72 ByteArrayInputStream stream = new ByteArrayInputStream( buf );
73 Checksum.update( checksums, stream );
74
75 assertEquals( "Checksum SHA1", "e396119ae0542e85a74759602fd2f81e5d36d762", checksumSha1.getChecksum() );
76 assertEquals( "Checksum MD5", "21c2c5ca87ec018adacb2e2fb3432219", checksumMd5.getChecksum() );
77 }
78
79 public void testUpdateWholeUpdatePartial()
80 {
81 Checksum checksum = new Checksum( ChecksumAlgorithm.SHA1 );
82 assertEquals( "Checksum unset", UNSET_SHA1, checksum.getChecksum() );
83
84 String expected = "066c2cbbc8cdaecb8ff97dcb84502462d6f575f3";
85 byte reesepieces[] = "eatagramovabits".getBytes();
86 checksum.update( reesepieces, 0, reesepieces.length );
87 String actual = checksum.getChecksum();
88
89 assertEquals( "Expected", expected, actual );
90
91
92 checksum.reset();
93 assertEquals( "Checksum unset", UNSET_SHA1, checksum.getChecksum() );
94
95
96 checksum.update( reesepieces, 0, 5 );
97 checksum.update( reesepieces, 5, 5 );
98 checksum.update( reesepieces, 10, reesepieces.length - 10 );
99
100 assertEquals( "Expected", expected, actual );
101 }
102 }