View Javadoc
1   package org.eclipse.aether.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 static final String EMPTY = "EMPTY";
41      private static final String PATTERN = "PATTERN";
42      private static final String TEXT = "TEXT";
43  
44      private Map<String, File> files = new HashMap<>(3);
45  
46      private Map<String, byte[]> bytes = new HashMap<>(3);
47  
48      private static Map<String, String> emptyChecksums = new HashMap<>();
49  
50      private static Map<String, String> patternChecksums = new HashMap<>();
51  
52      private static Map<String, String> textChecksums = new HashMap<>();
53  
54      private Map<String, Map<String, String>> sums = new HashMap<>();
55  
56      @BeforeClass
57      public static void beforeClass()
58      {
59          emptyChecksums.put( "MD5", "d41d8cd98f00b204e9800998ecf8427e" );
60          emptyChecksums.put( "SHA-1", "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
61          emptyChecksums.put( "SHA-256", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" );
62          emptyChecksums.put( "SHA-512", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" );
63          patternChecksums.put( "MD5", "14f01d6c7de7d4cf0a4887baa3528b5a" );
64          patternChecksums.put( "SHA-1", "feeeda19f626f9b0ef6cbf5948c1ec9531694295" );
65          patternChecksums.put( "SHA-256", "81d480a714840ab206dc8de62ca6119036f65499ad9e2e227c2465551bed684d" );
66          patternChecksums.put( "SHA-512", "931aa34118d9a85b9514e0224046d736a5bd7e2b2f366505fe1ad07ed85e1a4ac0cbc18e9b9a7fe36ce835be2a18cb571202a4975d182553faff336970eb0b7e" );
67          textChecksums.put( "MD5", "12582d1a662cefe3385f2113998e43ed" );
68          textChecksums.put( "SHA-1", "a8ae272db549850eef2ff54376f8cac2770745ee" );
69          textChecksums.put( "SHA-256", "35829adced2979761ba521dc2bb7d166e92ebed7342319d041398e509d481a46" );
70          textChecksums.put( "SHA-512", "2d6d19570b26080fa88101af2256ce3dae63512b06864cd36a05371c81d6dbd0ec226dd75f22e8d46a9582e1fc40ee6e7a02d43c852f3c92255982b835db6e7c" );
71      }
72  
73      @Before
74      public void before()
75          throws IOException
76      {
77          sums.clear();
78  
79          byte[] emptyBytes = new byte[0];
80          bytes.put( EMPTY, emptyBytes );
81          files.put( EMPTY, createTempFile( emptyBytes, 0 ) );
82          sums.put( EMPTY, emptyChecksums );
83  
84          byte[] patternBytes = writeBytes( new byte[] { 0, 1, 2, 4, 8, 16, 32, 64, 127, -1, -2, -4, -8, -16, -32, -64, -127 }, 1000 );
85          bytes.put( PATTERN, patternBytes );
86          files.put( PATTERN, createTempFile( patternBytes, 1 ) );
87          sums.put( PATTERN, patternChecksums );
88  
89          byte[] textBytes = writeBytes( "the quick brown fox jumps over the lazy dog\n".getBytes( StandardCharsets.UTF_8 ), 500 );
90          bytes.put( TEXT, textBytes );
91          files.put( TEXT, createTempFile( textBytes, 1 ) );
92          sums.put( TEXT, textChecksums );
93  
94      }
95  
96      @Test
97      public void testEquality()
98          throws Throwable
99      {
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         assertNull( 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