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 import junit.framework.TestCase;
27 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30
31
32
33
34
35
36 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
37 public class ChecksumTest
38 extends TestCase
39 {
40 private static final String UNSET_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
41
42 @Test
43 public void testConstructSha1()
44 {
45 Checksum checksum = new Checksum( ChecksumAlgorithm.SHA1 );
46 assertEquals( "Checksum.algorithm", checksum.getAlgorithm().getAlgorithm(), ChecksumAlgorithm.SHA1
47 .getAlgorithm() );
48 }
49
50 @Test
51 public void testConstructMd5()
52 {
53 Checksum checksum = new Checksum( ChecksumAlgorithm.MD5 );
54 assertEquals( "Checksum.algorithm", checksum.getAlgorithm().getAlgorithm(), ChecksumAlgorithm.MD5
55 .getAlgorithm() );
56 }
57
58 @Test
59 public void testUpdate()
60 {
61 Checksum checksum = new Checksum( ChecksumAlgorithm.SHA1 );
62 byte buf[] = ( "You know, I'm sick of following my dreams, man. "
63 + "I'm just going to ask where they're going and hook up with 'em later. - Mitch Hedberg" ).getBytes();
64 checksum.update( buf, 0, buf.length );
65 assertEquals( "Checksum", "e396119ae0542e85a74759602fd2f81e5d36d762", checksum.getChecksum() );
66 }
67
68 @Test
69 public void testUpdateMany()
70 throws IOException
71 {
72 Checksum checksumSha1 = new Checksum( ChecksumAlgorithm.SHA1 );
73 Checksum checksumMd5 = new Checksum( ChecksumAlgorithm.MD5 );
74 List<Checksum> checksums = new ArrayList<>();
75 checksums.add( checksumSha1 );
76 checksums.add( checksumMd5 );
77
78 byte buf[] = ( "You know, I'm sick of following my dreams, man. "
79 + "I'm just going to ask where they're going and hook up with 'em later. - Mitch Hedberg" ).getBytes();
80
81 ByteArrayInputStream stream = new ByteArrayInputStream( buf );
82 Checksum.update( checksums, stream );
83
84 assertEquals( "Checksum SHA1", "e396119ae0542e85a74759602fd2f81e5d36d762", checksumSha1.getChecksum() );
85 assertEquals( "Checksum MD5", "21c2c5ca87ec018adacb2e2fb3432219", checksumMd5.getChecksum() );
86 }
87
88 @Test
89 public void testUpdateWholeUpdatePartial()
90 {
91 Checksum checksum = new Checksum( ChecksumAlgorithm.SHA1 );
92 assertEquals( "Checksum unset", UNSET_SHA1, checksum.getChecksum() );
93
94 String expected = "066c2cbbc8cdaecb8ff97dcb84502462d6f575f3";
95 byte reesepieces[] = "eatagramovabits".getBytes();
96 checksum.update( reesepieces, 0, reesepieces.length );
97 String actual = checksum.getChecksum();
98
99 assertEquals( "Expected", expected, actual );
100
101
102 checksum.reset();
103 assertEquals( "Checksum unset", UNSET_SHA1, checksum.getChecksum() );
104
105
106 checksum.update( reesepieces, 0, 5 );
107 checksum.update( reesepieces, 5, 5 );
108 checksum.update( reesepieces, 10, reesepieces.length - 10 );
109
110 assertEquals( "Expected", expected, actual );
111 }
112 }