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.eclipse.aether.util.ChecksumUtils;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37
38 public class ChecksumUtilTest
39 {
40 private File emptyFile;
41
42 private File patternFile;
43
44 private File textFile;
45
46 private static Map<String, String> emptyFileChecksums = new HashMap<String, String>();
47
48 private static Map<String, String> patternFileChecksums = new HashMap<String, String>();
49
50 private static Map<String, String> textFileChecksums = new HashMap<String, String>();
51
52 private Map<File, Map<String, String>> sums = new HashMap<File, Map<String, String>>();
53
54 @BeforeClass
55 public static void beforeClass()
56 throws IOException
57 {
58 emptyFileChecksums.put( "MD5", "d41d8cd98f00b204e9800998ecf8427e" );
59 emptyFileChecksums.put( "SHA-1", "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
60 patternFileChecksums.put( "MD5", "14f01d6c7de7d4cf0a4887baa3528b5a" );
61 patternFileChecksums.put( "SHA-1", "feeeda19f626f9b0ef6cbf5948c1ec9531694295" );
62 textFileChecksums.put( "MD5", "12582d1a662cefe3385f2113998e43ed" );
63 textFileChecksums.put( "SHA-1", "a8ae272db549850eef2ff54376f8cac2770745ee" );
64 }
65
66 @Before
67 public void before()
68 throws IOException
69 {
70 sums.clear();
71
72 emptyFile = createTempFile( new byte[] {}, 0 );
73 sums.put( emptyFile, emptyFileChecksums );
74
75 patternFile =
76 createTempFile( new byte[] { 0, 1, 2, 4, 8, 16, 32, 64, 127, -1, -2, -4, -8, -16, -32, -64, -127 }, 1000 );
77 sums.put( patternFile, patternFileChecksums );
78
79 textFile = createTempFile( "the quick brown fox jumps over the lazy dog\n".getBytes( StandardCharsets.UTF_8 ), 500 );
80 sums.put( textFile, textFileChecksums );
81
82 }
83
84 @Test
85 public void testEquality()
86 throws Throwable
87 {
88 Map<String, Object> checksums = null;
89
90 for ( File file : new File[] { emptyFile, patternFile, textFile } )
91 {
92
93 checksums = ChecksumUtils.calc( file, Arrays.asList( "SHA-1", "MD5" ) );
94
95 for ( Entry<String, Object> entry : checksums.entrySet() )
96 {
97 if ( entry.getValue() instanceof Throwable )
98 {
99 throw (Throwable) entry.getValue();
100 }
101 String actual = entry.getValue().toString();
102 String expected = sums.get( file ).get( entry.getKey() );
103 assertEquals( String.format( "checksums do not match for '%s', algorithm '%s'", file.getName(),
104 entry.getKey() ), expected, actual );
105 }
106 assertTrue( "Could not delete file", file.delete() );
107 }
108 }
109
110 @Test
111 public void testFileHandleLeakage()
112 throws IOException
113 {
114 for ( File file : new File[] { emptyFile, patternFile, textFile } )
115 {
116 for ( int i = 0; i < 150; i++ )
117 {
118 ChecksumUtils.calc( file, Arrays.asList( "SHA-1", "MD5" ) );
119 }
120 assertTrue( "Could not delete file", file.delete() );
121 }
122
123 }
124
125 @Test
126 public void testRead()
127 throws IOException
128 {
129 for ( Map<String, String> checksums : sums.values() )
130 {
131 String sha1 = checksums.get( "SHA-1" );
132 String md5 = checksums.get( "MD5" );
133
134 File sha1File = createTempFile( sha1 );
135 File md5File = createTempFile( md5 );
136
137 assertEquals( sha1, ChecksumUtils.read( sha1File ) );
138 assertEquals( md5, ChecksumUtils.read( md5File ) );
139
140 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.sha1)", sha1File.delete() );
141 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksums.md5)", md5File.delete() );
142 }
143 }
144
145 @Test
146 public void testReadSpaces()
147 throws IOException
148 {
149 for ( Map<String, String> checksums : sums.values() )
150 {
151 String sha1 = checksums.get( "SHA-1" );
152 String md5 = checksums.get( "MD5" );
153
154 File sha1File = createTempFile( "sha1-checksum = " + sha1 );
155 File md5File = createTempFile( md5 + " test" );
156
157 assertEquals( sha1, ChecksumUtils.read( sha1File ) );
158 assertEquals( md5, ChecksumUtils.read( md5File ) );
159
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 testReadEmptyFile()
167 throws IOException
168 {
169 File file = createTempFile( "" );
170
171 assertEquals( "", ChecksumUtils.read( file ) );
172
173 assertTrue( "ChecksumUtils leaks file handles (cannot delete checksum.empty)", file.delete() );
174 }
175
176 @Test
177 public void testToHexString()
178 {
179 assertEquals( null, ChecksumUtils.toHexString( null ) );
180 assertEquals( "", ChecksumUtils.toHexString( new byte[] {} ) );
181 assertEquals( "00", ChecksumUtils.toHexString( new byte[] { 0 } ) );
182 assertEquals( "ff", ChecksumUtils.toHexString( new byte[] { -1 } ) );
183 assertEquals( "00017f", ChecksumUtils.toHexString( new byte[] { 0, 1, 127 } ) );
184 }
185
186 }