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.eclipse.aether.util.ChecksumUtils; 034import org.junit.Before; 035import org.junit.BeforeClass; 036import org.junit.Test; 037 038public class ChecksumUtilTest 039{ 040 private static final String EMPTY = "EMPTY"; 041 private static final String PATTERN = "PATTERN"; 042 private static final String TEXT = "TEXT"; 043 044 private Map<String, File> files = new HashMap<>(3); 045 046 private Map<String, byte[]> bytes = new HashMap<>(3); 047 048 private static Map<String, String> emptyChecksums = new HashMap<>(); 049 050 private static Map<String, String> patternChecksums = new HashMap<>(); 051 052 private static Map<String, String> textChecksums = new HashMap<>(); 053 054 private Map<String, Map<String, String>> sums = new HashMap<>(); 055 056 @BeforeClass 057 public static void beforeClass() 058 { 059 emptyChecksums.put( "MD5", "d41d8cd98f00b204e9800998ecf8427e" ); 060 emptyChecksums.put( "SHA-1", "da39a3ee5e6b4b0d3255bfef95601890afd80709" ); 061 emptyChecksums.put( "SHA-256", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ); 062 emptyChecksums.put( "SHA-512", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" ); 063 patternChecksums.put( "MD5", "14f01d6c7de7d4cf0a4887baa3528b5a" ); 064 patternChecksums.put( "SHA-1", "feeeda19f626f9b0ef6cbf5948c1ec9531694295" ); 065 patternChecksums.put( "SHA-256", "81d480a714840ab206dc8de62ca6119036f65499ad9e2e227c2465551bed684d" ); 066 patternChecksums.put( "SHA-512", "931aa34118d9a85b9514e0224046d736a5bd7e2b2f366505fe1ad07ed85e1a4ac0cbc18e9b9a7fe36ce835be2a18cb571202a4975d182553faff336970eb0b7e" ); 067 textChecksums.put( "MD5", "12582d1a662cefe3385f2113998e43ed" ); 068 textChecksums.put( "SHA-1", "a8ae272db549850eef2ff54376f8cac2770745ee" ); 069 textChecksums.put( "SHA-256", "35829adced2979761ba521dc2bb7d166e92ebed7342319d041398e509d481a46" ); 070 textChecksums.put( "SHA-512", "2d6d19570b26080fa88101af2256ce3dae63512b06864cd36a05371c81d6dbd0ec226dd75f22e8d46a9582e1fc40ee6e7a02d43c852f3c92255982b835db6e7c" ); 071 } 072 073 @Before 074 public void before() 075 throws IOException 076 { 077 sums.clear(); 078 079 byte[] emptyBytes = new byte[0]; 080 bytes.put( EMPTY, emptyBytes ); 081 files.put( EMPTY, createTempFile( emptyBytes, 0 ) ); 082 sums.put( EMPTY, emptyChecksums ); 083 084 byte[] patternBytes = writeBytes( new byte[] { 0, 1, 2, 4, 8, 16, 32, 64, 127, -1, -2, -4, -8, -16, -32, -64, -127 }, 1000 ); 085 bytes.put( PATTERN, patternBytes ); 086 files.put( PATTERN, createTempFile( patternBytes, 1 ) ); 087 sums.put( PATTERN, patternChecksums ); 088 089 byte[] textBytes = writeBytes( "the quick brown fox jumps over the lazy dog\n".getBytes( StandardCharsets.UTF_8 ), 500 ); 090 bytes.put( TEXT, textBytes ); 091 files.put( TEXT, createTempFile( textBytes, 1 ) ); 092 sums.put( TEXT, textChecksums ); 093 094 } 095 096 @Test 097 public void testEquality() 098 throws Throwable 099 { 100 Map<String, Object> checksums = null; 101 102 for ( Map.Entry<String,File> fileEntry : files.entrySet() ) 103 { 104 105 checksums = ChecksumUtils.calc( fileEntry.getValue(), Arrays.asList( "SHA-512", "SHA-256", "SHA-1", "MD5" ) ); 106 107 for ( Entry<String, Object> entry : checksums.entrySet() ) 108 { 109 if ( entry.getValue() instanceof Throwable ) 110 { 111 throw (Throwable) entry.getValue(); 112 } 113 String actual = entry.getValue().toString(); 114 String expected = sums.get( fileEntry.getKey() ).get( entry.getKey() ); 115 assertEquals( String.format( "checksums do not match for '%s', algorithm '%s'", fileEntry.getValue().getName(), 116 entry.getKey() ), expected, actual ); 117 } 118 assertTrue( "Could not delete file", fileEntry.getValue().delete() ); 119 } 120 } 121 122 @Test 123 public void testFileHandleLeakage() 124 throws IOException 125 { 126 for ( File file : files.values() ) 127 { 128 for ( int i = 0; i < 150; i++ ) 129 { 130 ChecksumUtils.calc( file, Arrays.asList( "SHA-512", "SHA-256", "SHA-1", "MD5" ) ); 131 } 132 assertTrue( "Could not delete file", file.delete() ); 133 } 134 135 } 136 137 @Test 138 public void testRead() 139 throws IOException 140 { 141 for ( Map<String, String> checksums : sums.values() ) 142 { 143 String sha512 = checksums.get( "SHA-512" ); 144 String sha256 = checksums.get( "SHA-256" ); 145 String sha1 = checksums.get( "SHA-1" ); 146 String md5 = checksums.get( "MD5" ); 147 148 File sha512File = createTempFile( sha512 ); 149 File sha256File = createTempFile( sha256 ); 150 File sha1File = createTempFile( sha1 ); 151 File md5File = createTempFile( md5 ); 152 153 assertEquals( sha512, ChecksumUtils.read( sha512File ) ); 154 assertEquals( sha256, ChecksumUtils.read( sha256File ) ); 155 assertEquals( sha1, ChecksumUtils.read( sha1File ) ); 156 assertEquals( md5, ChecksumUtils.read( md5File ) ); 157 158 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha512)", sha512File.delete() ); 159 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha256)", sha256File.delete() ); 160 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha1)", sha1File.delete() ); 161 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.md5)", md5File.delete() ); 162 } 163 } 164 165 @Test 166 public void testReadSpaces() 167 throws IOException 168 { 169 for ( Map<String, String> checksums : sums.values() ) 170 { 171 String sha512 = checksums.get( "SHA-512" ); 172 String sha256 = checksums.get( "SHA-256" ); 173 String sha1 = checksums.get( "SHA-1" ); 174 String md5 = checksums.get( "MD5" ); 175 176 File sha512File = createTempFile( "sha512-checksum = " + sha512 ); 177 File sha256File = createTempFile( "sha256-checksum = " + sha256 ); 178 File sha1File = createTempFile( "sha1-checksum = " + sha1 ); 179 File md5File = createTempFile( md5 + " test" ); 180 181 assertEquals( sha512, ChecksumUtils.read( sha512File ) ); 182 assertEquals( sha256, ChecksumUtils.read( sha256File ) ); 183 assertEquals( sha1, ChecksumUtils.read( sha1File ) ); 184 assertEquals( md5, ChecksumUtils.read( md5File ) ); 185 186 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha512)", sha512File.delete() ); 187 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha256)", sha256File.delete() ); 188 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha1)", sha1File.delete() ); 189 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.md5)", md5File.delete() ); 190 } 191 } 192 193 @Test 194 public void testReadEmptyFile() 195 throws IOException 196 { 197 File file = createTempFile( "" ); 198 199 assertEquals( "", ChecksumUtils.read( file ) ); 200 201 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksum.empty)", file.delete() ); 202 } 203 204 @Test 205 public void testToHexString() 206 { 207 assertEquals( null, ChecksumUtils.toHexString( null ) ); 208 assertEquals( "", ChecksumUtils.toHexString( new byte[] {} ) ); 209 assertEquals( "00", ChecksumUtils.toHexString( new byte[] { 0 } ) ); 210 assertEquals( "ff", ChecksumUtils.toHexString( new byte[] { -1 } ) ); 211 assertEquals( "00017f", ChecksumUtils.toHexString( new byte[] { 0, 1, 127 } ) ); 212 } 213 214 @Test 215 public void testCalcWithByteArray() throws Throwable 216 { 217 Map<String, Object> checksums = null; 218 219 for ( Map.Entry<String, byte[]> bytesEntry : bytes.entrySet() ) 220 { 221 checksums = ChecksumUtils.calc( bytesEntry.getValue(), Arrays.asList( "SHA-512", "SHA-256", "SHA-1", "MD5" ) ); 222 223 for ( Entry<String, Object> entry : checksums.entrySet() ) 224 { 225 if ( entry.getValue() instanceof Throwable ) 226 { 227 throw (Throwable) entry.getValue(); 228 } 229 String actual = entry.getValue().toString(); 230 String expected = sums.get( bytesEntry.getKey() ).get( entry.getKey() ); 231 assertEquals( String.format( "checksums do not match for '%s', algorithm '%s'", bytesEntry.getKey(), 232 entry.getKey() ), expected, actual ); 233 } 234 } 235 } 236 237 private byte[] writeBytes( byte[] pattern, int repeat ) 238 { 239 byte[] result = new byte[pattern.length * repeat]; 240 for ( int i = 0; i < repeat; i++ ) 241 { 242 System.arraycopy( pattern, 0, result, i * pattern.length, pattern.length ); 243 } 244 return result; 245 } 246} 247