author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Fri, 25 Mar 2016 07:34:02 +0100 | |
changeset 411 | 9eb6379b97f0 |
parent 51 | 130e099942d8 |
permissions | -rw-r--r-- |
jtulach@44 | 1 |
/* |
jtulach@44 | 2 |
* To change this template, choose Tools | Templates |
jtulach@44 | 3 |
* and open the template in the editor. |
jtulach@44 | 4 |
*/ |
jtulach@44 | 5 |
|
jtulach@44 | 6 |
package test; |
jtulach@44 | 7 |
|
jtulach@44 | 8 |
import org.apidesign.api.security.*; |
jtulach@44 | 9 |
import java.nio.ByteBuffer; |
jtulach@44 | 10 |
import java.security.MessageDigest; |
jtulach@44 | 11 |
import java.util.Arrays; |
jtulach@44 | 12 |
import java.util.Random; |
jtulach@44 | 13 |
import org.junit.After; |
jtulach@44 | 14 |
import org.junit.BeforeClass; |
jtulach@44 | 15 |
import org.junit.Test; |
jtulach@44 | 16 |
import static org.junit.Assert.*; |
jtulach@44 | 17 |
|
jtulach@44 | 18 |
/** Compares that the MessageDigest and Digest yield the same results for |
jtulach@44 | 19 |
* default provider. |
jtulach@44 | 20 |
* |
jtulach@44 | 21 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
jtulach@44 | 22 |
*/ |
jtulach@44 | 23 |
public class OldAPIToNewAPITest { |
jtulach@44 | 24 |
private static byte[] arr; |
jtulach@44 | 25 |
private static long time; |
jtulach@44 | 26 |
private static byte[] resOld; |
jtulach@44 | 27 |
private static byte[] resNew; |
jtulach@44 | 28 |
|
jtulach@44 | 29 |
public OldAPIToNewAPITest() { |
jtulach@44 | 30 |
} |
jtulach@44 | 31 |
|
jtulach@44 | 32 |
@BeforeClass |
jtulach@44 | 33 |
public static void setUp() { |
jtulach@44 | 34 |
time = System.currentTimeMillis(); |
jtulach@44 | 35 |
Random r = new Random(time); |
jtulach@44 | 36 |
arr = new byte[r.nextInt(1024)]; |
jtulach@44 | 37 |
r.nextBytes(arr); |
jtulach@44 | 38 |
} |
jtulach@44 | 39 |
|
jtulach@44 | 40 |
@After |
jtulach@44 | 41 |
public void tearDown() { |
jtulach@44 | 42 |
} |
jtulach@44 | 43 |
|
jtulach@44 | 44 |
@Test |
jtulach@44 | 45 |
public void generateHashUsingMessageDigest() throws Exception { |
jtulach@263 | 46 |
if (Boolean.getBoolean("no.failures") && Boolean.getBoolean("no.md5")) return; |
jtulach@51 | 47 |
// BEGIN: day.end.bridges.BridgeToOldRegister |
jtulach@44 | 48 |
// The java.security.Providers cannot be registered in META-INF/services |
jtulach@44 | 49 |
// that is why one needs to either configure various properties or |
jtulach@44 | 50 |
// make some dummy call that will initialize our bridge class. |
jtulach@44 | 51 |
// Then the bridge class registers itself as a MessageDigest provider |
jtulach@44 | 52 |
// in its constructor. |
jtulach@44 | 53 |
// |
jtulach@44 | 54 |
// This is the call: |
jtulach@44 | 55 |
Digest initialize = Digest.getInstance("MD5"); |
jtulach@51 | 56 |
// END: day.end.bridges.BridgeToOldRegister |
jtulach@44 | 57 |
|
jtulach@44 | 58 |
MessageDigest md = MessageDigest.getInstance("cnt"); |
jtulach@44 | 59 |
byte[] res = md.digest(arr); |
jtulach@44 | 60 |
resOld = res; |
jtulach@44 | 61 |
} |
jtulach@44 | 62 |
|
jtulach@44 | 63 |
|
jtulach@44 | 64 |
@Test |
jtulach@44 | 65 |
public void generateHashUsingNewDigest() throws Exception { |
jtulach@44 | 66 |
Digest d = Digest.getInstance("cnt"); |
jtulach@44 | 67 |
ByteBuffer bb = ByteBuffer.wrap(arr); |
jtulach@44 | 68 |
byte[] res = d.digest(bb); |
jtulach@44 | 69 |
resNew = res; |
jtulach@44 | 70 |
} |
jtulach@44 | 71 |
|
jtulach@44 | 72 |
|
jtulach@44 | 73 |
|
jtulach@44 | 74 |
@Test |
jtulach@44 | 75 |
public void compareTheHashes() throws Exception { |
jtulach@263 | 76 |
if (Boolean.getBoolean("no.failures") && Boolean.getBoolean("no.md5")) return; |
jtulach@44 | 77 |
if (!Arrays.equals(resOld, resNew)) { |
jtulach@44 | 78 |
fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew)); |
jtulach@44 | 79 |
} |
jtulach@44 | 80 |
} |
jtulach@44 | 81 |
} |