/* * Copyright (C) 2013 University of Dundee & Open Microscopy Environment. * All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package ome.services.blitz.test.utests; import ome.services.blitz.util.ChecksumAlgorithmMapper; import ome.util.checksum.ChecksumProvider; import ome.util.checksum.ChecksumProviderFactory; import ome.util.checksum.ChecksumProviderFactoryImpl; import ome.util.checksum.ChecksumType; import omero.model.ChecksumAlgorithm; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; /** * Test that the text of the {@link ChecksumAlgorithm} enumeration values correctly * indicates the bit width of hash codes from the corresponding checksum algorithm. * @author m.t.b.carroll@dundee.ac.uk * @since 5.0 */ public class ChecksumBitwidthTest { private ChecksumProviderFactory checksumProviderFactory; @BeforeMethod public void setupChecksumProviderFactory() { checksumProviderFactory = new ChecksumProviderFactoryImpl(); } /** * Test that the bit width suffixing the checksum algorithm name matches * the size of the checksums actually generated by the corresponding algorithm. */ @Test public void testBitWidthsSame() { for (final ChecksumAlgorithm algorithm : ChecksumAlgorithmMapper.getAllChecksumAlgorithms()) { final String algorithmName = algorithm.getValue().getValue(); final int expectedBitWidth = Integer.parseInt(algorithmName.substring(algorithmName.lastIndexOf('-') + 1)); final ChecksumType checksumType = ChecksumAlgorithmMapper.getChecksumType(algorithm); final ChecksumProvider checksumProvider = checksumProviderFactory.getProvider(checksumType); final int actualBitWidth = checksumProvider.checksumAsBytes().length << 3; Assert.assertEquals(actualBitWidth, expectedBitWidth, "bit width of checksum should match that suggested by checksum name " + algorithmName); } } @AfterMethod public void teardownChecksumProviderFactory() { checksumProviderFactory = null; } }