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.util.Arrays; 028import java.util.HashMap; 029import java.util.Map; 030import java.util.Map.Entry; 031 032import org.eclipse.aether.util.ChecksumUtils; 033import org.junit.Before; 034import org.junit.BeforeClass; 035import org.junit.Test; 036 037public class ChecksumUtilTest 038{ 039 private File emptyFile; 040 041 private File patternFile; 042 043 private File textFile; 044 045 private static Map<String, String> emptyFileChecksums = new HashMap<String, String>(); 046 047 private static Map<String, String> patternFileChecksums = new HashMap<String, String>(); 048 049 private static Map<String, String> textFileChecksums = new HashMap<String, String>(); 050 051 private Map<File, Map<String, String>> sums = new HashMap<File, Map<String, String>>(); 052 053 @BeforeClass 054 public static void beforeClass() 055 throws IOException 056 { 057 emptyFileChecksums.put( "MD5", "d41d8cd98f00b204e9800998ecf8427e" ); 058 emptyFileChecksums.put( "SHA-1", "da39a3ee5e6b4b0d3255bfef95601890afd80709" ); 059 patternFileChecksums.put( "MD5", "14f01d6c7de7d4cf0a4887baa3528b5a" ); 060 patternFileChecksums.put( "SHA-1", "feeeda19f626f9b0ef6cbf5948c1ec9531694295" ); 061 textFileChecksums.put( "MD5", "12582d1a662cefe3385f2113998e43ed" ); 062 textFileChecksums.put( "SHA-1", "a8ae272db549850eef2ff54376f8cac2770745ee" ); 063 } 064 065 @Before 066 public void before() 067 throws IOException 068 { 069 sums.clear(); 070 071 emptyFile = createTempFile( new byte[] {}, 0 ); 072 sums.put( emptyFile, emptyFileChecksums ); 073 074 patternFile = 075 createTempFile( new byte[] { 0, 1, 2, 4, 8, 16, 32, 64, 127, -1, -2, -4, -8, -16, -32, -64, -127 }, 1000 ); 076 sums.put( patternFile, patternFileChecksums ); 077 078 textFile = createTempFile( "the quick brown fox jumps over the lazy dog\n".getBytes( "UTF-8" ), 500 ); 079 sums.put( textFile, textFileChecksums ); 080 081 } 082 083 @Test 084 public void testEquality() 085 throws Throwable 086 { 087 Map<String, Object> checksums = null; 088 089 for ( File file : new File[] { emptyFile, patternFile, textFile } ) 090 { 091 092 checksums = ChecksumUtils.calc( file, Arrays.asList( "SHA-1", "MD5" ) ); 093 094 for ( Entry<String, Object> entry : checksums.entrySet() ) 095 { 096 if ( entry.getValue() instanceof Throwable ) 097 { 098 throw (Throwable) entry.getValue(); 099 } 100 String actual = entry.getValue().toString(); 101 String expected = sums.get( file ).get( entry.getKey() ); 102 assertEquals( String.format( "checksums do not match for '%s', algorithm '%s'", file.getName(), 103 entry.getKey() ), expected, actual ); 104 } 105 assertTrue( "Could not delete file", file.delete() ); 106 } 107 } 108 109 @Test 110 public void testFileHandleLeakage() 111 throws IOException 112 { 113 for ( File file : new File[] { emptyFile, patternFile, textFile } ) 114 { 115 for ( int i = 0; i < 150; i++ ) 116 { 117 ChecksumUtils.calc( file, Arrays.asList( "SHA-1", "MD5" ) ); 118 } 119 assertTrue( "Could not delete file", file.delete() ); 120 } 121 122 } 123 124 @Test 125 public void testRead() 126 throws IOException 127 { 128 for ( Map<String, String> checksums : sums.values() ) 129 { 130 String sha1 = checksums.get( "SHA-1" ); 131 String md5 = checksums.get( "MD5" ); 132 133 File sha1File = createTempFile( sha1 ); 134 File md5File = createTempFile( md5 ); 135 136 assertEquals( sha1, ChecksumUtils.read( sha1File ) ); 137 assertEquals( md5, ChecksumUtils.read( md5File ) ); 138 139 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha1)", sha1File.delete() ); 140 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.md5)", md5File.delete() ); 141 } 142 } 143 144 @Test 145 public void testReadSpaces() 146 throws IOException 147 { 148 for ( Map<String, String> checksums : sums.values() ) 149 { 150 String sha1 = checksums.get( "SHA-1" ); 151 String md5 = checksums.get( "MD5" ); 152 153 File sha1File = createTempFile( "sha1-checksum = " + sha1 ); 154 File md5File = createTempFile( md5 + " test" ); 155 156 assertEquals( sha1, ChecksumUtils.read( sha1File ) ); 157 assertEquals( md5, ChecksumUtils.read( md5File ) ); 158 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 testReadEmptyFile() 166 throws IOException 167 { 168 File file = createTempFile( "" ); 169 170 assertEquals( "", ChecksumUtils.read( file ) ); 171 172 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksum.empty)", file.delete() ); 173 } 174 175 @Test 176 public void testToHexString() 177 { 178 assertEquals( null, ChecksumUtils.toHexString( null ) ); 179 assertEquals( "", ChecksumUtils.toHexString( new byte[] {} ) ); 180 assertEquals( "00", ChecksumUtils.toHexString( new byte[] { 0 } ) ); 181 assertEquals( "ff", ChecksumUtils.toHexString( new byte[] { -1 } ) ); 182 assertEquals( "00017f", ChecksumUtils.toHexString( new byte[] { 0, 1, 127 } ) ); 183 } 184 185}