1 package org.apache.archiva.checksum;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import junit.framework.TestCase;
23 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
37 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
38 public class ChecksumTest
41 private static final String UNSET_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
44 public void testConstructSha1()
46 Checksum checksum = new Checksum( ChecksumAlgorithm.SHA1 );
47 assertEquals( "Checksum.algorithm", checksum.getAlgorithm().getAlgorithm(), ChecksumAlgorithm.SHA1
52 public void testConstructMd5()
54 Checksum checksum = new Checksum( ChecksumAlgorithm.MD5 );
55 assertEquals( "Checksum.algorithm", checksum.getAlgorithm().getAlgorithm(), ChecksumAlgorithm.MD5
60 public void testUpdate()
62 Checksum checksum = new Checksum( ChecksumAlgorithm.SHA1 );
63 byte buf[] = ( "You know, I'm sick of following my dreams, man. "
64 + "I'm just going to ask where they're going and hook up with 'em later. - Mitch Hedberg" ).getBytes();
65 checksum.update( buf, 0, buf.length );
66 assertEquals( "Checksum", "e396119ae0542e85a74759602fd2f81e5d36d762", checksum.getChecksum() );
70 public void testUpdateMany()
73 Checksum checksumSha1 = new Checksum( ChecksumAlgorithm.SHA1 );
74 Checksum checksumMd5 = new Checksum( ChecksumAlgorithm.MD5 );
75 List<Checksum> checksums = new ArrayList<>();
76 checksums.add( checksumSha1 );
77 checksums.add( checksumMd5 );
79 byte buf[] = ( "You know, I'm sick of following my dreams, man. "
80 + "I'm just going to ask where they're going and hook up with 'em later. - Mitch Hedberg" ).getBytes();
82 ByteArrayInputStream stream = new ByteArrayInputStream( buf );
83 Checksum.update( checksums, stream );
85 assertEquals( "Checksum SHA1", "e396119ae0542e85a74759602fd2f81e5d36d762", checksumSha1.getChecksum() );
86 assertEquals( "Checksum MD5", "21c2c5ca87ec018adacb2e2fb3432219", checksumMd5.getChecksum() );
90 public void testUpdateWholeUpdatePartial()
92 Checksum checksum = new Checksum( ChecksumAlgorithm.SHA1 );
93 assertEquals( "Checksum unset", UNSET_SHA1, checksum.getChecksum() );
95 String expected = "066c2cbbc8cdaecb8ff97dcb84502462d6f575f3";
96 byte reesepieces[] = "eatagramovabits".getBytes();
97 checksum.update( reesepieces, 0, reesepieces.length );
98 String actual = checksum.getChecksum();
100 assertEquals( "Expected", expected, actual );
102 // Reset the checksum.
104 assertEquals( "Checksum unset", UNSET_SHA1, checksum.getChecksum() );
106 // Now parse it again in 3 pieces.
107 checksum.update( reesepieces, 0, 5 );
108 checksum.update( reesepieces, 5, 5 );
109 checksum.update( reesepieces, 10, reesepieces.length - 10 );
111 assertEquals( "Expected", expected, actual );