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 org.junit.Test;
24 import java.io.IOException;
25 import java.net.URISyntaxException;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.security.MessageDigest;
29 import java.security.NoSuchAlgorithmException;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.List;
35 import static org.junit.Assert.*;
38 * @author Martin Stockhammer <martin_s@apache.org>
41 public class ChecksumValidatorTest
45 public void isValidChecksum( ) throws URISyntaxException, ChecksumValidationException
47 ChecksumValidator validator = new ChecksumValidator();
48 String fileName = "checksum/checksumTest1.txt";
49 List<String> exts = Arrays.asList( "md5", "sha1", "sha2", "sha3", "sha5" );
50 for(String ext : exts)
52 Path hashFile = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( fileName + "."+ext ).toURI( ) );
53 assertTrue( validator.isValidChecksum( hashFile ) );
55 fileName = "checksum/checksumTest2.txt";
56 for(String ext : exts)
58 Path hashFile = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( fileName + "."+ext ).toURI( ) );
59 assertTrue( validator.isValidChecksum( hashFile ) );
64 public void isInValidChecksum( ) throws URISyntaxException, ChecksumValidationException
66 ChecksumValidator validator = new ChecksumValidator();
67 String fileName = "checksum/checksumTest3.txt";
68 List<String> exts = Arrays.asList( "md5", "sha1", "sha2", "sha3", "sha5" );
69 for(String ext : exts)
71 Path hashFile = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( fileName + "."+ext ).toURI( ) );
72 assertFalse( validator.isValidChecksum( hashFile ) );
77 public void isInvalidExtension( ) throws URISyntaxException, ChecksumValidationException
79 ChecksumValidator validator = new ChecksumValidator();
80 String fileName = "checksum/checksumTest1.txt";
84 Path hashFile = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( fileName + "." + ext ).toURI( ) );
85 validator.isValidChecksum( hashFile );
86 } catch (ChecksumValidationException e) {
87 assertEquals(ChecksumValidationException.ValidationError.INVALID_FORMAT, e.getErrorType());
92 public void computeFileDoesNotExist( ) throws URISyntaxException, ChecksumValidationException
94 ChecksumValidator validator = new ChecksumValidator();
95 String fileName = "checksum/checksumTest4.txt";
99 Path hashFile = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( fileName + "." + ext ).toURI( ) );
100 validator.isValidChecksum( hashFile );
101 } catch (ChecksumValidationException e) {
102 assertEquals(ChecksumValidationException.ValidationError.READ_ERROR, e.getErrorType());
107 public void checksumFileDoesNotExist( ) throws URISyntaxException, ChecksumValidationException
109 ChecksumValidator validator = new ChecksumValidator();
110 String fileName = "checksumTest5.txt";
114 Path sibling = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "checksum/checksumTest1.txt." + ext ).toURI( ) );
115 Path hashFile = sibling.getParent().resolve(fileName);
116 validator.isValidChecksum( hashFile );
117 } catch (ChecksumValidationException e) {
118 assertEquals(ChecksumValidationException.ValidationError.FILE_NOT_FOUND, e.getErrorType());
123 public void computeHash( ) throws URISyntaxException, NoSuchAlgorithmException, IOException, ChecksumValidationException
125 ChecksumValidator validator = new ChecksumValidator();
126 Map<String, String> hashes = new HashMap<>( );
127 hashes.put("md5","079fe13e970ae7311172df6657f36892");
128 hashes.put("sha1", "01e14abba5401e1a63be468f9c3b723167f27dc8");
129 hashes.put("sha2", "ae7278e7bdfd8d7c06f9b1932ddccdddb0061a58a893aec3f00932e53ef9c794");
130 hashes.put("sha3", "a52efc629f256cd2b390f080ab7e23fc706ab9e2c8948cea2bd8504a70894f69f44f48e83c889edc82b40b673b575bad");
131 hashes.put("sha5", "b2340bbf150403725fdf6a6f340a8a33bb9526bad7e0220f1dfea67d5a06217bc1d5c3a773b083ed8c9f5352c94ecc6da2a6d8a33ad0347566f0acc55e042fde");
132 Path hashFile = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "checksum/checksumTest1.txt").toURI( ) );
134 for (String key : hashes.keySet()) {
135 byte[] expectedSum = validator.convertFromHex( hashes.get(key) );
136 byte[] computedSum = validator.computeHash( hashFile, key );
137 assertArrayEquals( expectedSum, computedSum );
142 public void readHashFile( )