1 package org.eclipse.aether.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.eclipse.aether.internal.test.util.TestFileUtils.*;
23 import static org.junit.Assert.*;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Map.Entry;
32
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36
37 public class ChecksumUtilTest
38 {
39 private static final String EMPTY = "EMPTY";
40 private static final String PATTERN = "PATTERN";
41 private static final String TEXT = "TEXT";
42
43 private Map<String, File> files = new HashMap<>(3);
44
45 private Map<String, byte[]> bytes = new HashMap<>(3);
46
47 private static Map<String, String> emptyChecksums = new HashMap<>();
48
49 private static Map<String, String> patternChecksums = new HashMap<>();
50
51 private static Map<String, String> textChecksums = new HashMap<>();
52
53 private Map<String, Map<String, String>> sums = new HashMap<>();
54
55 @BeforeClass
56 public static void beforeClass()
57 {
58 emptyChecksums.put( "MD5", "d41d8cd98f00b204e9800998ecf8427e" );
59 emptyChecksums.put( "SHA-1", "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
60 emptyChecksums.put( "SHA-256", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" );
61 emptyChecksums.put( "SHA-512", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" );
62 patternChecksums.put( "MD5", "14f01d6c7de7d4cf0a4887baa3528b5a" );
63 patternChecksums.put( "SHA-1", "feeeda19f626f9b0ef6cbf5948c1ec9531694295" );
64 patternChecksums.put( "SHA-256", "81d480a714840ab206dc8de62ca6119036f65499ad9e2e227c2465551bed684d" );
65 patternChecksums.put( "SHA-512", "931aa34118d9a85b9514e0224046d736a5bd7e2b2f366505fe1ad07ed85e1a4ac0cbc18e9b9a7fe36ce835be2a18cb571202a4975d182553faff336970eb0b7e" );
66 textChecksums.put( "MD5", "12582d1a662cefe3385f2113998e43ed" );
67 textChecksums.put( "SHA-1", "a8ae272db549850eef2ff54376f8cac2770745ee" );
68 textChecksums.put( "SHA-256", "35829adced2979761ba521dc2bb7d166e92ebed7342319d041398e509d481a46" );
69 textChecksums.put( "SHA-512", "2d6d19570b26080fa88101af2256ce3dae63512b06864cd36a05371c81d6dbd0ec226dd75f22e8d46a9582e1fc40ee6e7a02d43c852f3c92255982b835db6e7c" );
70 }
71
72 @Before
73 public void before()
74 throws IOException
75 {
76 sums.clear();
77
78 byte[] emptyBytes = new byte[0];
79 bytes.put( EMPTY, emptyBytes );
80 files.put( EMPTY, createTempFile( emptyBytes, 0 ) );
81 sums.put( EMPTY, emptyChecksums );
82
83 byte[] patternBytes = writeBytes( new byte[] { 0, 1, 2, 4, 8, 16, 32, 64, 127, -1, -2, -4, -8, -16, -32, -64, -127 }, 1000 );
84 bytes.put( PATTERN, patternBytes );
85 files.put( PATTERN, createTempFile( patternBytes, 1 ) );
86 sums.put( PATTERN, patternChecksums );
87
88 byte[] textBytes = writeBytes( "the quick brown fox jumps over the lazy dog\n".getBytes( StandardCharsets.UTF_8 ), 500 );
89 bytes.put( TEXT, textBytes );
90 files.put( TEXT, createTempFile( textBytes, 1 ) );
91 sums.put( TEXT, textChecksums );
92
93 }
94
95 @Test
96 public void testEquality()
97 throws Throwable
98 {
99 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