|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2014 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.util; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertArrayEquals; |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | + |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests {@link DigestUtils}. |
| 41 | + * |
| 42 | + * @author Curtis Rueden |
| 43 | + */ |
| 44 | +public class DigestUtilsTest { |
| 45 | + |
| 46 | + private static final byte[] CAFEBABE_SHA1 = { 20, 101, -38, -47, 38, -45, 43, |
| 47 | + -9, -86, 93, 59, -107, -91, -57, -61, 49, -51, -1, 52, -33 }; |
| 48 | + |
| 49 | + private static final byte[] CAFEBABE_MD5 = { 45, 27, -67, -30, -84, -84, 10, |
| 50 | + -3, 7, 100, 109, -104, 21, 79, 64, 46 }; |
| 51 | + |
| 52 | + private static final byte[] HELLO_WORLD_SHA1 = { 123, 80, 44, 58, 31, 72, |
| 53 | + -56, 96, -102, -30, 18, -51, -5, 99, -99, -18, 57, 103, 63, 94 }; |
| 54 | + |
| 55 | + private static final String HELLO_WORLD_SHA1_HEX = |
| 56 | + "7b502c3a1f48c8609ae212cdfb639dee39673f5e"; |
| 57 | + |
| 58 | + private static final String CAFEBABE_SHA1_HEX = |
| 59 | + "1465dad126d32bf7aa5d3b95a5c7c331cdff34df"; |
| 60 | + |
| 61 | + private static final String HELLO_WORLD_SHA1_BASE64 = |
| 62 | + "e1AsOh9IyGCa4hLN+2Od7jlnP14="; |
| 63 | + |
| 64 | + private static final String CAFEBABE_SHA1_BASE64 = |
| 65 | + "FGXa0SbTK/eqXTuVpcfDMc3/NN8="; |
| 66 | + |
| 67 | + /** Tests {@link DigestUtils#bytes(String)}. */ |
| 68 | + @Test |
| 69 | + public void testBytesString() { |
| 70 | + final String s = "Hello world"; |
| 71 | + final byte[] bytes = DigestUtils.bytes(s); |
| 72 | + final byte[] expected = |
| 73 | + { 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 }; |
| 74 | + assertArrayEquals(expected, bytes); |
| 75 | + } |
| 76 | + |
| 77 | + /** Tests {@link DigestUtils#bytes(int)}. */ |
| 78 | + @Test |
| 79 | + public void testBytesInt() { |
| 80 | + final byte[] bytes = DigestUtils.bytes(0xcafebabe); |
| 81 | + final byte[] expected = { -54, -2, -70, -66 }; |
| 82 | + assertArrayEquals(expected, bytes); |
| 83 | + } |
| 84 | + |
| 85 | + /** Tests {@link DigestUtils#hex(byte[])}. */ |
| 86 | + @Test |
| 87 | + public void testHex() { |
| 88 | + assertEquals("cafebabe", DigestUtils.hex(DigestUtils.bytes(0xcafebabe))); |
| 89 | + assertEquals("deadbeef", DigestUtils.hex(DigestUtils.bytes(0xdeadbeef))); |
| 90 | + assertEquals("00000000", DigestUtils.hex(DigestUtils.bytes(0x00000000))); |
| 91 | + assertEquals("ffffffff", DigestUtils.hex(DigestUtils.bytes(0xffffffff))); |
| 92 | + } |
| 93 | + |
| 94 | + /** Tests {@link DigestUtils#base64(byte[])}. */ |
| 95 | + @Test |
| 96 | + public void testBase64() { |
| 97 | + assertEquals("yv66vg==", DigestUtils.base64(DigestUtils.bytes(0xcafebabe))); |
| 98 | + assertEquals("3q2+7w==", DigestUtils.base64(DigestUtils.bytes(0xdeadbeef))); |
| 99 | + assertEquals("AAAAAA==", DigestUtils.base64(DigestUtils.bytes(0x00000000))); |
| 100 | + assertEquals("/////w==", DigestUtils.base64(DigestUtils.bytes(0xffffffff))); |
| 101 | + } |
| 102 | + |
| 103 | + /** Tests {@link DigestUtils#hash(String)}. */ |
| 104 | + @Test |
| 105 | + public void testHashString() { |
| 106 | + final byte[] hash = DigestUtils.hash("Hello world"); |
| 107 | + final byte[] expected = { -50, 89, -118, -92 }; |
| 108 | + assertArrayEquals(expected, hash); |
| 109 | + } |
| 110 | + |
| 111 | + /** Tests {@link DigestUtils#hash(byte[])}. */ |
| 112 | + @Test |
| 113 | + public void testHashBytes() { |
| 114 | + final byte[] bytes = DigestUtils.bytes("Hello world"); |
| 115 | + final byte[] hash = DigestUtils.hash(bytes); |
| 116 | + final byte[] expected = { -50, 89, -118, -92 }; |
| 117 | + assertArrayEquals(expected, hash); |
| 118 | + } |
| 119 | + |
| 120 | + /** Tests {@link DigestUtils#sha1(byte[])}. */ |
| 121 | + @Test |
| 122 | + public void testSHA1() { |
| 123 | + final byte[] bytes = DigestUtils.bytes(0xcafebabe); |
| 124 | + final byte[] sha1 = DigestUtils.sha1(bytes); |
| 125 | + assertArrayEquals(CAFEBABE_SHA1, sha1); |
| 126 | + } |
| 127 | + |
| 128 | + /** Tests {@link DigestUtils#md5(byte[])}. */ |
| 129 | + @Test |
| 130 | + public void testMD5() { |
| 131 | + final byte[] bytes = DigestUtils.bytes(0xcafebabe); |
| 132 | + final byte[] md5 = DigestUtils.md5(bytes); |
| 133 | + assertArrayEquals(CAFEBABE_MD5, md5); |
| 134 | + } |
| 135 | + |
| 136 | + /** Tests {@link DigestUtils#digest(String, byte[])}. */ |
| 137 | + @Test |
| 138 | + public void testDigest() { |
| 139 | + final byte[] bytes = DigestUtils.bytes(0xcafebabe); |
| 140 | + |
| 141 | + final byte[] sha1 = DigestUtils.digest("SHA-1", bytes); |
| 142 | + final byte[] expectedSHA1 = DigestUtils.sha1(bytes); |
| 143 | + assertArrayEquals(expectedSHA1, sha1); |
| 144 | + |
| 145 | + final byte[] md5 = DigestUtils.digest("MD5", bytes); |
| 146 | + final byte[] expectedMD5 = DigestUtils.md5(bytes); |
| 147 | + assertArrayEquals(expectedMD5, md5); |
| 148 | + } |
| 149 | + |
| 150 | + /** Tests {@link DigestUtils#best(String)}. */ |
| 151 | + @Test |
| 152 | + public void testBestString() { |
| 153 | + final byte[] best = DigestUtils.best("Hello world"); |
| 154 | + assertArrayEquals(HELLO_WORLD_SHA1, best); |
| 155 | + } |
| 156 | + |
| 157 | + /** Tests {@link DigestUtils#best(byte[])}. */ |
| 158 | + @Test |
| 159 | + public void testBestBytes() { |
| 160 | + final byte[] bytes = DigestUtils.bytes(0xcafebabe); |
| 161 | + final byte[] best = DigestUtils.best(bytes); |
| 162 | + assertArrayEquals(CAFEBABE_SHA1, best); |
| 163 | + } |
| 164 | + |
| 165 | + /** Tests {@link DigestUtils#bestHex(String)}. */ |
| 166 | + @Test |
| 167 | + public void testBestHexString() { |
| 168 | + assertEquals(HELLO_WORLD_SHA1_HEX, DigestUtils.bestHex("Hello world")); |
| 169 | + } |
| 170 | + |
| 171 | + /** Tests {@link DigestUtils#hex(byte[])}. */ |
| 172 | + @Test |
| 173 | + public void testBestHexBytes() { |
| 174 | + final byte[] bytes = DigestUtils.bytes(0xcafebabe); |
| 175 | + assertEquals(CAFEBABE_SHA1_HEX, DigestUtils.bestHex(bytes)); |
| 176 | + } |
| 177 | + |
| 178 | + /** Tests {@link DigestUtils#bestBase64(String)}. */ |
| 179 | + @Test |
| 180 | + public void testBestBase64String() { |
| 181 | + assertEquals(HELLO_WORLD_SHA1_BASE64, DigestUtils.bestBase64("Hello world")); |
| 182 | + } |
| 183 | + |
| 184 | + /** Tests {@link DigestUtils#bestBase64(byte[])}. */ |
| 185 | + @Test |
| 186 | + public void testBestBase64Bytes() { |
| 187 | + final byte[] bytes = DigestUtils.bytes(0xcafebabe); |
| 188 | + assertEquals(CAFEBABE_SHA1_BASE64, DigestUtils.bestBase64(bytes)); |
| 189 | + } |
| 190 | + |
| 191 | +} |
0 commit comments