create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / motor / MotorDigestTest.java
1 package net.sf.openrocket.motor;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.io.UnsupportedEncodingException;
6 import java.security.MessageDigest;
7 import java.security.NoSuchAlgorithmException;
8
9 import net.sf.openrocket.motor.MotorDigest.DataType;
10 import net.sf.openrocket.util.TextUtil;
11
12 import org.junit.Test;
13
14
15 public class MotorDigestTest {
16         
17         private static final double[] timeArray = {
18                 0.0, 0.123456789, 0.4115, Math.nextAfter(Math.nextAfter(1.4445, 0), 0)
19         };
20         
21         private static final double[] massArray = {
22                 0.54321, 0.43211
23         };
24         
25         private static final double[] thrustArray = {
26                 0.0, 0.2345678, 9999.3335, 0.0
27         };
28         
29         private static final int[] intData = {
30                 // Time (ms)
31                 0, 4,   0, 123, 412, 1445,
32                 // Mass specific (0.1g)
33                 1, 2,   5432, 4321,
34                 // Thrust (mN)
35                 5, 4,   0, 235, 9999334, 0
36         };
37         
38
39         @Test
40         public void testMotorDigest() throws NoSuchAlgorithmException {
41                 
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));
48                 }
49                 
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);
54                 
55                 
56                 assertEquals(TextUtil.hexString(correct.digest()), motor.getDigest());
57         }
58         
59         
60         @Test
61         public void testCommentDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException {
62                 
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!"));
67                 
68         }
69         
70         
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")));
75         }
76 }