001package org.eclipse.aether.connector.basic; 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.junit.Assert.*; 023 024import java.io.File; 025import java.io.IOException; 026import java.net.URI; 027import java.nio.ByteBuffer; 028import java.nio.charset.StandardCharsets; 029import java.security.NoSuchAlgorithmException; 030import java.util.ArrayList; 031import java.util.List; 032import java.util.Map; 033 034import org.eclipse.aether.internal.test.util.TestFileUtils; 035import org.eclipse.aether.spi.connector.layout.RepositoryLayout; 036import org.junit.Before; 037import org.junit.Test; 038 039public class ChecksumCalculatorTest 040{ 041 042 private static final String SHA1 = "SHA-1"; 043 044 private static final String MD5 = "MD5"; 045 046 private File file; 047 048 private ChecksumCalculator newCalculator( String... algos ) 049 { 050 List<RepositoryLayout.Checksum> checksums = new ArrayList<RepositoryLayout.Checksum>(); 051 for ( String algo : algos ) 052 { 053 checksums.add( new RepositoryLayout.Checksum( algo, URI.create( "irrelevant" ) ) ); 054 } 055 return ChecksumCalculator.newInstance( file, checksums ); 056 } 057 058 private ByteBuffer toBuffer( String data ) 059 { 060 return ByteBuffer.wrap( data.getBytes( StandardCharsets.UTF_8 ) ); 061 } 062 063 @Before 064 public void init() 065 throws Exception 066 { 067 file = TestFileUtils.createTempFile( "Hello World!" ); 068 } 069 070 @Test 071 public void testNoOffset() 072 { 073 ChecksumCalculator calculator = newCalculator( SHA1, MD5 ); 074 calculator.init( 0 ); 075 calculator.update( toBuffer( "Hello World!" ) ); 076 Map<String, Object> digests = calculator.get(); 077 assertNotNull( digests ); 078 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) ); 079 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) ); 080 assertEquals( 2, digests.size() ); 081 } 082 083 @Test 084 public void testWithOffset() 085 { 086 ChecksumCalculator calculator = newCalculator( SHA1, MD5 ); 087 calculator.init( 6 ); 088 calculator.update( toBuffer( "World!" ) ); 089 Map<String, Object> digests = calculator.get(); 090 assertNotNull( digests ); 091 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) ); 092 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) ); 093 assertEquals( 2, digests.size() ); 094 } 095 096 @Test 097 public void testWithExcessiveOffset() 098 { 099 ChecksumCalculator calculator = newCalculator( SHA1, MD5 ); 100 calculator.init( 100 ); 101 calculator.update( toBuffer( "World!" ) ); 102 Map<String, Object> digests = calculator.get(); 103 assertNotNull( digests ); 104 assertTrue( digests.get( SHA1 ) instanceof IOException ); 105 assertTrue( digests.get( MD5 ) instanceof IOException ); 106 assertEquals( 2, digests.size() ); 107 } 108 109 @Test 110 public void testUnknownAlgorithm() 111 { 112 ChecksumCalculator calculator = newCalculator( "unknown", SHA1 ); 113 calculator.init( 0 ); 114 calculator.update( toBuffer( "Hello World!" ) ); 115 Map<String, Object> digests = calculator.get(); 116 assertNotNull( digests ); 117 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) ); 118 assertTrue( digests.get( "unknown" ) instanceof NoSuchAlgorithmException ); 119 assertEquals( 2, digests.size() ); 120 } 121 122 @Test 123 public void testNoInitCall() 124 { 125 ChecksumCalculator calculator = newCalculator( SHA1, MD5 ); 126 calculator.update( toBuffer( "Hello World!" ) ); 127 Map<String, Object> digests = calculator.get(); 128 assertNotNull( digests ); 129 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) ); 130 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) ); 131 assertEquals( 2, digests.size() ); 132 } 133 134 @Test 135 public void testRestart() 136 { 137 ChecksumCalculator calculator = newCalculator( SHA1, MD5 ); 138 calculator.init( 0 ); 139 calculator.update( toBuffer( "Ignored" ) ); 140 calculator.init( 0 ); 141 calculator.update( toBuffer( "Hello World!" ) ); 142 Map<String, Object> digests = calculator.get(); 143 assertNotNull( digests ); 144 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) ); 145 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) ); 146 assertEquals( 2, digests.size() ); 147 } 148 149 @Test 150 public void testRestartAfterError() 151 { 152 ChecksumCalculator calculator = newCalculator( SHA1, MD5 ); 153 calculator.init( 100 ); 154 calculator.init( 0 ); 155 calculator.update( toBuffer( "Hello World!" ) ); 156 Map<String, Object> digests = calculator.get(); 157 assertNotNull( digests ); 158 assertEquals( "2ef7bde608ce5404e97d5f042f95f89f1c232871", digests.get( SHA1 ) ); 159 assertEquals( "ed076287532e86365e841e92bfc50d8c", digests.get( MD5 ) ); 160 assertEquals( 2, digests.size() ); 161 } 162 163}