001package org.eclipse.aether.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.eclipse.aether.internal.test.util.TestFileUtils.*; 023import static org.junit.Assert.*; 024 025import java.io.File; 026import java.io.IOException; 027import java.nio.charset.StandardCharsets; 028import java.util.Arrays; 029import java.util.HashMap; 030import java.util.Map; 031import java.util.Map.Entry; 032 033import org.junit.Before; 034import org.junit.BeforeClass; 035import org.junit.Test; 036 037public class ChecksumUtilTest 038{ 039 private static final String EMPTY = "EMPTY"; 040 private static final String PATTERN = "PATTERN"; 041 private static final String TEXT = "TEXT"; 042 043 private Map<String, File> files = new HashMap<>(3); 044 045 private Map<String, byte[]> bytes = new HashMap<>(3); 046 047 private static Map<String, String> emptyChecksums = new HashMap<>(); 048 049 private static Map<String, String> patternChecksums = new HashMap<>(); 050 051 private static Map<String, String> textChecksums = new HashMap<>(); 052 053 private Map<String, Map<String, String>> sums = new HashMap<>(); 054 055 @BeforeClass 056 public static void beforeClass() 057 { 058 emptyChecksums.put( "MD5", "d41d8cd98f00b204e9800998ecf8427e" ); 059 emptyChecksums.put( "SHA-1", "da39a3ee5e6b4b0d3255bfef95601890afd80709" ); 060 emptyChecksums.put( "SHA-256", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ); 061 emptyChecksums.put( "SHA-512", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" ); 062 patternChecksums.put( "MD5", "14f01d6c7de7d4cf0a4887baa3528b5a" ); 063 patternChecksums.put( "SHA-1", "feeeda19f626f9b0ef6cbf5948c1ec9531694295" ); 064 patternChecksums.put( "SHA-256", "81d480a714840ab206dc8de62ca6119036f65499ad9e2e227c2465551bed684d" ); 065 patternChecksums.put( "SHA-512", "931aa34118d9a85b9514e0224046d736a5bd7e2b2f366505fe1ad07ed85e1a4ac0cbc18e9b9a7fe36ce835be2a18cb571202a4975d182553faff336970eb0b7e" ); 066 textChecksums.put( "MD5", "12582d1a662cefe3385f2113998e43ed" ); 067 textChecksums.put( "SHA-1", "a8ae272db549850eef2ff54376f8cac2770745ee" ); 068 textChecksums.put( "SHA-256", "35829adced2979761ba521dc2bb7d166e92ebed7342319d041398e509d481a46" ); 069 textChecksums.put( "SHA-512", "2d6d19570b26080fa88101af2256ce3dae63512b06864cd36a05371c81d6dbd0ec226dd75f22e8d46a9582e1fc40ee6e7a02d43c852f3c92255982b835db6e7c" ); 070 } 071 072 @Before 073 public void before() 074 throws IOException 075 { 076 sums.clear(); 077 078 byte[] emptyBytes = new byte[0]; 079 bytes.put( EMPTY, emptyBytes ); 080 files.put( EMPTY, createTempFile( emptyBytes, 0 ) ); 081 sums.put( EMPTY, emptyChecksums ); 082 083 byte[] patternBytes = writeBytes( new byte[] { 0, 1, 2, 4, 8, 16, 32, 64, 127, -1, -2, -4, -8, -16, -32, -64, -127 }, 1000 ); 084 bytes.put( PATTERN, patternBytes ); 085 files.put( PATTERN, createTempFile( patternBytes, 1 ) ); 086 sums.put( PATTERN, patternChecksums ); 087 088 byte[] textBytes = writeBytes( "the quick brown fox jumps over the lazy dog\n".getBytes( StandardCharsets.UTF_8 ), 500 ); 089 bytes.put( TEXT, textBytes ); 090 files.put( TEXT, createTempFile( textBytes, 1 ) ); 091 sums.put( TEXT, textChecksums ); 092 093 } 094 095 @Test 096 public void testEquality() 097 throws Throwable 098 { 099 Map<String, Object> checksums = null; 100 101 for ( Map.Entry<String,File> fileEntry : files.entrySet() ) 102 { 103 104 checksums = ChecksumUtils.calc( fileEntry.getValue(), Arrays.asList( "SHA-512", "SHA-256", "SHA-1", "MD5" ) ); 105 106 for ( Entry<String, Object> entry : checksums.entrySet() ) 107 { 108 if ( entry.getValue() instanceof Throwable ) 109 { 110 throw (Throwable) entry.getValue(); 111 } 112 String actual = entry.getValue().toString(); 113 String expected = sums.get( fileEntry.getKey() ).get( entry.getKey() ); 114 assertEquals( String.format( "checksums do not match for '%s', algorithm '%s'", fileEntry.getValue().getName(), 115 entry.getKey() ), expected, actual ); 116 } 117 assertTrue( "Could not delete file", fileEntry.getValue().delete() ); 118 } 119 } 120 121 @Test 122 public void testFileHandleLeakage() 123 throws IOException 124 { 125 for ( File file : files.values() ) 126 { 127 for ( int i = 0; i < 150; i++ ) 128 { 129 ChecksumUtils.calc( file, Arrays.asList( "SHA-512", "SHA-256", "SHA-1", "MD5" ) ); 130 } 131 assertTrue( "Could not delete file", file.delete() ); 132 } 133 134 } 135 136 @Test 137 public void testRead() 138 throws IOException 139 { 140 for ( Map<String, String> checksums : sums.values() ) 141 { 142 String sha512 = checksums.get( "SHA-512" ); 143 String sha256 = checksums.get( "SHA-256" ); 144 String sha1 = checksums.get( "SHA-1" ); 145 String md5 = checksums.get( "MD5" ); 146 147 File sha512File = createTempFile( sha512 ); 148 File sha256File = createTempFile( sha256 ); 149 File sha1File = createTempFile( sha1 ); 150 File md5File = createTempFile( md5 ); 151 152 assertEquals( sha512, ChecksumUtils.read( sha512File ) ); 153 assertEquals( sha256, ChecksumUtils.read( sha256File ) ); 154 assertEquals( sha1, ChecksumUtils.read( sha1File ) ); 155 assertEquals( md5, ChecksumUtils.read( md5File ) ); 156 157 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha512)", sha512File.delete() ); 158 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha256)", sha256File.delete() ); 159 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha1)", sha1File.delete() ); 160 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.md5)", md5File.delete() ); 161 } 162 } 163 164 @Test 165 public void testReadSpaces() 166 throws IOException 167 { 168 for ( Map<String, String> checksums : sums.values() ) 169 { 170 String sha512 = checksums.get( "SHA-512" ); 171 String sha256 = checksums.get( "SHA-256" ); 172 String sha1 = checksums.get( "SHA-1" ); 173 String md5 = checksums.get( "MD5" ); 174 175 File sha512File = createTempFile( "sha512-checksum = " + sha512 ); 176 File sha256File = createTempFile( "sha256-checksum = " + sha256 ); 177 File sha1File = createTempFile( "sha1-checksum = " + sha1 ); 178 File md5File = createTempFile( md5 + " test" ); 179 180 assertEquals( sha512, ChecksumUtils.read( sha512File ) ); 181 assertEquals( sha256, ChecksumUtils.read( sha256File ) ); 182 assertEquals( sha1, ChecksumUtils.read( sha1File ) ); 183 assertEquals( md5, ChecksumUtils.read( md5File ) ); 184 185 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha512)", sha512File.delete() ); 186 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha256)", sha256File.delete() ); 187 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha1)", sha1File.delete() ); 188 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.md5)", md5File.delete() ); 189 } 190 } 191 192 @Test 193 public void testReadEmptyFile() 194 throws IOException 195 { 196 File file = createTempFile( "" ); 197 198 assertEquals( "", ChecksumUtils.read( file ) ); 199 200 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksum.empty)", file.delete() ); 201 } 202 203 @Test 204 public void testToHexString() 205 { 206 assertNull( ChecksumUtils.toHexString( null ) ); 207 assertEquals( "", ChecksumUtils.toHexString( new byte[] {} ) ); 208 assertEquals( "00", ChecksumUtils.toHexString( new byte[] { 0 } ) ); 209 assertEquals( "ff", ChecksumUtils.toHexString( new byte[] { -1 } ) ); 210 assertEquals( "00017f", ChecksumUtils.toHexString( new byte[] { 0, 1, 127 } ) ); 211 } 212 213 @Test 214 public void testFromHexString() 215 { 216 assertNull( ChecksumUtils.toHexString( null ) ); 217 assertArrayEquals( new byte[] {}, ChecksumUtils.fromHexString( "" ) ); 218 assertArrayEquals( new byte[] { 0 }, ChecksumUtils.fromHexString( "00" ) ); 219 assertArrayEquals( new byte[] { -1 } , ChecksumUtils.fromHexString( "ff" ) ); 220 assertArrayEquals( new byte[] { 0, 1, 127 }, ChecksumUtils.fromHexString( "00017f" ) ); 221 } 222 223 @Test 224 public void testCalcWithByteArray() throws Throwable 225 { 226 Map<String, Object> checksums = null; 227 228 for ( Map.Entry<String, byte[]> bytesEntry : bytes.entrySet() ) 229 { 230 checksums = ChecksumUtils.calc( bytesEntry.getValue(), Arrays.asList( "SHA-512", "SHA-256", "SHA-1", "MD5" ) ); 231 232 for ( Entry<String, Object> entry : checksums.entrySet() ) 233 { 234 if ( entry.getValue() instanceof Throwable ) 235 { 236 throw (Throwable) entry.getValue(); 237 } 238 String actual = entry.getValue().toString(); 239 String expected = sums.get( bytesEntry.getKey() ).get( entry.getKey() ); 240 assertEquals( String.format( "checksums do not match for '%s', algorithm '%s'", bytesEntry.getKey(), 241 entry.getKey() ), expected, actual ); 242 } 243 } 244 } 245 246 private byte[] writeBytes( byte[] pattern, int repeat ) 247 { 248 byte[] result = new byte[pattern.length * repeat]; 249 for ( int i = 0; i < repeat; i++ ) 250 { 251 System.arraycopy( pattern, 0, result, i * pattern.length, pattern.length ); 252 } 253 return result; 254 } 255} 256