samples/messagedigest/src-test/test/NewAPIToOldAPITest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:23 +0200
changeset 44 716af5f2ebd1
child 263 7e8e995065c5
permissions -rw-r--r--
Switching to freeform project
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 java.nio.ByteBuffer;
jtulach@44
     9
import java.security.MessageDigest;
jtulach@44
    10
import java.util.Arrays;
jtulach@44
    11
import java.util.Random;
jtulach@44
    12
import org.apidesign.api.security.Digest;
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 NewAPIToOldAPITest {
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 NewAPIToOldAPITest() {
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@44
    46
        MessageDigest md = MessageDigest.getInstance("MD5");
jtulach@44
    47
        byte[] res = md.digest(arr);
jtulach@44
    48
        resOld = res;
jtulach@44
    49
    }
jtulach@44
    50
jtulach@44
    51
    @Test
jtulach@44
    52
    public void generateHashUsingNewDigest() throws Exception {
jtulach@44
    53
        Digest d = Digest.getInstance("MD5");
jtulach@44
    54
        ByteBuffer bb = ByteBuffer.wrap(arr);
jtulach@44
    55
        byte[] res = d.digest(bb);
jtulach@44
    56
        resNew = res;
jtulach@44
    57
    }
jtulach@44
    58
    
jtulach@44
    59
    @Test
jtulach@44
    60
    public void compareTheHashes() throws Exception {
jtulach@44
    61
        if (!Arrays.equals(resOld, resNew)) {
jtulach@44
    62
            fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));
jtulach@44
    63
        }
jtulach@44
    64
    }
jtulach@44
    65
}