diff options
Diffstat (limited to 'guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java')
-rw-r--r-- | guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java | 73 |
1 files changed, 17 insertions, 56 deletions
diff --git a/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java b/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java index 2a9547b..a70466e 100644 --- a/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java +++ b/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java @@ -1,73 +1,34 @@ -/* - * Copyright (C) 2011 The Guava Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2011 Google Inc. All Rights Reserved. package com.google.common.hash; -import com.google.common.collect.ImmutableSet; - -import junit.framework.TestCase; - import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.Arrays; + +import junit.framework.TestCase; /** * Tests for the MessageDigestHashFunction. * - * @author Kurt Alfred Kluever + * @author kak@google.com (Kurt Alfred Kluever) */ public class MessageDigestHashFunctionTest extends TestCase { - private static final ImmutableSet<String> INPUTS = ImmutableSet.of("", "Z", "foobar"); - private static final ImmutableSet<String> ALGORITHMS = ImmutableSet.of( - "MD5", "SHA1", "SHA-1", "SHA-256", "SHA-512"); - - public void testHashing() { - for (String stringToTest : INPUTS) { - for (String algorithmToTest : ALGORITHMS) { - assertMessageDigestHashing(HashTestUtils.ascii(stringToTest), algorithmToTest); - } - } + public void testMd5Hashing() throws Exception { + assertMessageDigestHashing(HashTestUtils.ascii(""), "MD5"); + assertMessageDigestHashing(HashTestUtils.ascii("Z"), "MD5"); + assertMessageDigestHashing(HashTestUtils.ascii("foobar"), "MD5"); } - public void testToString() { - assertEquals("Hashing.md5()", Hashing.md5().toString()); - assertEquals("Hashing.sha1()", Hashing.sha1().toString()); - assertEquals("Hashing.sha256()", Hashing.sha256().toString()); - assertEquals("Hashing.sha512()", Hashing.sha512().toString()); + public void testSha1Hashing() throws Exception { + assertMessageDigestHashing(HashTestUtils.ascii(""), "SHA1"); + assertMessageDigestHashing(HashTestUtils.ascii("Z"), "SHA1"); + assertMessageDigestHashing(HashTestUtils.ascii("foobar"), "SHA1"); } - private static void assertMessageDigestHashing(byte[] input, String algorithmName) { - try { - MessageDigest digest = MessageDigest.getInstance(algorithmName); - assertEquals( - HashCodes.fromBytes(digest.digest(input)), - new MessageDigestHashFunction(algorithmName, algorithmName).hashBytes(input)); - for (int bytes = 4; bytes <= digest.getDigestLength(); bytes++) { - assertEquals( - HashCodes.fromBytes(Arrays.copyOf(digest.digest(input), bytes)), - new MessageDigestHashFunction(algorithmName, bytes, algorithmName).hashBytes(input)); - } - try { - int maxSize = digest.getDigestLength(); - new MessageDigestHashFunction(algorithmName, maxSize + 1, algorithmName); - fail(); - } catch (IllegalArgumentException expected) { - } - } catch (NoSuchAlgorithmException nsae) { - throw new AssertionError(nsae); - } + private static void assertMessageDigestHashing(byte[] input, String algorithmName) + throws NoSuchAlgorithmException { + HashTestUtils.assertEqualHashes( + MessageDigest.getInstance(algorithmName).digest(input), + new MessageDigestHashFunction(algorithmName).hashBytes(input).asBytes()); } } |