You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChecksumTest.java 4.0KB

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