author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Fri, 25 Mar 2016 07:34:02 +0100 | |
changeset 411 | 9eb6379b97f0 |
parent 44 | 716af5f2ebd1 |
permissions | -rw-r--r-- |
jtulach@44 | 1 |
package test; |
jtulach@44 | 2 |
|
jtulach@44 | 3 |
import java.nio.ByteBuffer; |
jtulach@44 | 4 |
import java.security.MessageDigest; |
jtulach@44 | 5 |
import java.util.Arrays; |
jtulach@44 | 6 |
import java.util.Random; |
jtulach@44 | 7 |
import org.apidesign.api.security.Digest; |
jtulach@44 | 8 |
import org.junit.After; |
jtulach@44 | 9 |
import org.junit.BeforeClass; |
jtulach@44 | 10 |
import org.junit.Test; |
jtulach@44 | 11 |
import static org.junit.Assert.*; |
jtulach@44 | 12 |
|
jtulach@44 | 13 |
/** Compares that the MessageDigest and Digest yield the same results for |
jtulach@44 | 14 |
* default provider. |
jtulach@44 | 15 |
* |
jtulach@44 | 16 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
jtulach@44 | 17 |
*/ |
jtulach@44 | 18 |
public class NewAPIToOldAPITest { |
jtulach@44 | 19 |
private static byte[] arr; |
jtulach@44 | 20 |
private static long time; |
jtulach@44 | 21 |
private static byte[] resOld; |
jtulach@44 | 22 |
private static byte[] resNew; |
jtulach@44 | 23 |
|
jtulach@44 | 24 |
public NewAPIToOldAPITest() { |
jtulach@44 | 25 |
} |
jtulach@44 | 26 |
|
jtulach@44 | 27 |
@BeforeClass |
jtulach@44 | 28 |
public static void setUp() { |
jtulach@44 | 29 |
time = System.currentTimeMillis(); |
jtulach@44 | 30 |
Random r = new Random(time); |
jtulach@44 | 31 |
arr = new byte[r.nextInt(1024)]; |
jtulach@44 | 32 |
r.nextBytes(arr); |
jtulach@44 | 33 |
} |
jtulach@44 | 34 |
|
jtulach@44 | 35 |
@After |
jtulach@44 | 36 |
public void tearDown() { |
jtulach@44 | 37 |
} |
jtulach@44 | 38 |
|
jtulach@44 | 39 |
@Test |
jtulach@44 | 40 |
public void generateHashUsingMessageDigest() throws Exception { |
jtulach@44 | 41 |
MessageDigest md = MessageDigest.getInstance("MD5"); |
jtulach@44 | 42 |
byte[] res = md.digest(arr); |
jtulach@44 | 43 |
resOld = res; |
jtulach@44 | 44 |
} |
jtulach@44 | 45 |
|
jtulach@44 | 46 |
@Test |
jtulach@44 | 47 |
public void generateHashUsingNewDigest() throws Exception { |
jtulach@263 | 48 |
if (Boolean.getBoolean("no.failures") && Boolean.getBoolean("no.md5")) return; |
jtulach@44 | 49 |
Digest d = Digest.getInstance("MD5"); |
jtulach@44 | 50 |
ByteBuffer bb = ByteBuffer.wrap(arr); |
jtulach@44 | 51 |
byte[] res = d.digest(bb); |
jtulach@44 | 52 |
resNew = res; |
jtulach@44 | 53 |
} |
jtulach@44 | 54 |
|
jtulach@44 | 55 |
@Test |
jtulach@44 | 56 |
public void compareTheHashes() throws Exception { |
jtulach@263 | 57 |
if (Boolean.getBoolean("no.failures") && Boolean.getBoolean("no.md5")) return; |
jtulach@44 | 58 |
if (!Arrays.equals(resOld, resNew)) { |
jtulach@44 | 59 |
fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew)); |
jtulach@44 | 60 |
} |
jtulach@44 | 61 |
} |
jtulach@44 | 62 |
} |