1 package net.sf.openrocket.motor;
3 import static org.junit.Assert.assertEquals;
5 import java.io.UnsupportedEncodingException;
6 import java.security.MessageDigest;
7 import java.security.NoSuchAlgorithmException;
9 import net.sf.openrocket.motor.MotorDigest.DataType;
10 import net.sf.openrocket.util.TextUtil;
12 import org.junit.Test;
15 public class MotorDigestTest {
17 private static final double[] timeArray = {
18 0.0, 0.123456789, 0.4115, Math.nextAfter(Math.nextAfter(1.4445, 0), 0)
21 private static final double[] massArray = {
25 private static final double[] thrustArray = {
26 0.0, 0.2345678, 9999.3335, 0.0
29 private static final int[] intData = {
31 0, 4, 0, 123, 412, 1445,
32 // Mass specific (0.1g)
35 5, 4, 0, 235, 9999334, 0
40 public void testMotorDigest() throws NoSuchAlgorithmException {
42 MessageDigest correct = MessageDigest.getInstance("MD5");
43 for (int value: intData) {
44 correct.update((byte) ((value >>> 24) & 0xFF));
45 correct.update((byte) ((value >>> 16) & 0xFF));
46 correct.update((byte) ((value >>> 8) & 0xFF));
47 correct.update((byte) (value & 0xFF));
50 MotorDigest motor = new MotorDigest();
51 motor.update(DataType.TIME_ARRAY, timeArray);
52 motor.update(DataType.MASS_SPECIFIC, massArray);
53 motor.update(DataType.FORCE_PER_TIME, thrustArray);
56 assertEquals(TextUtil.hexString(correct.digest()), motor.getDigest());
61 public void testCommentDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException {
63 assertEquals(md5("Hello world!"), MotorDigest.digestComment("Hello world! "));
64 assertEquals(md5("Hello world!"), MotorDigest.digestComment("\nHello\tworld!\n\r"));
65 assertEquals(md5("Hello world!"), MotorDigest.digestComment("Hello\r\r\r\nworld!"));
66 assertEquals(md5("Hello\u00e4 world!"), MotorDigest.digestComment("Hello\u00e4\r\r\nworld!"));
71 private static String md5(String source)
72 throws NoSuchAlgorithmException, UnsupportedEncodingException {
73 MessageDigest digest = MessageDigest.getInstance("MD5");
74 return TextUtil.hexString(digest.digest(source.getBytes("UTF-8")));