aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/asn1/UtcTimeTest.java
diff options
context:
space:
mode:
authorYuuta Liang <yuutaw@students.cs.ubc.ca>2023-10-12 12:10:33 +0800
committerYuuta Liang <yuutaw@students.cs.ubc.ca>2023-10-12 12:10:33 +0800
commitd342a45d98c4795b3a3fe1aaef5236ad4a782b55 (patch)
treef4ebc0ad962b138d9371413fcc71c97a559df506 /src/test/model/asn1/UtcTimeTest.java
parente60c9c76243cfe0a408af98dc60bedb973e815db (diff)
downloadjca-d342a45d98c4795b3a3fe1aaef5236ad4a782b55.tar
jca-d342a45d98c4795b3a3fe1aaef5236ad4a782b55.tar.gz
jca-d342a45d98c4795b3a3fe1aaef5236ad4a782b55.tar.bz2
jca-d342a45d98c4795b3a3fe1aaef5236ad4a782b55.zip
Implement data structures from X.680, X.501, X.509, and PKCS#10, with X.690 encoding / decoding support
The implementation took four days, and it is still a little bit rough. Updated version should arrive soon. Signed-off-by: Yuuta Liang <yuutaw@students.cs.ubc.ca>
Diffstat (limited to 'src/test/model/asn1/UtcTimeTest.java')
-rw-r--r--src/test/model/asn1/UtcTimeTest.java121
1 files changed, 121 insertions, 0 deletions
diff --git a/src/test/model/asn1/UtcTimeTest.java b/src/test/model/asn1/UtcTimeTest.java
new file mode 100644
index 0000000..5ba7e13
--- /dev/null
+++ b/src/test/model/asn1/UtcTimeTest.java
@@ -0,0 +1,121 @@
+package model.asn1;
+
+import model.asn1.exceptions.ParseException;
+import model.asn1.parsing.BytesReader;
+import org.junit.jupiter.api.Test;
+
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class UtcTimeTest {
+ @Test
+ void testConstructor() throws ParseException {
+ final ZonedDateTime now = ZonedDateTime.now();
+ assertEquals(now, new UtcTime(UtcTime.TAG, null, now).getTimestamp());
+
+ final ASN1Time parsed = new UtcTime(new BytesReader(new Byte[] {
+ 0x17, 13,
+ '1', '9', '0', '8', '1', '0', '1', '1', '4', '5', '1', '4', 'Z'
+ }), false);
+ assertEquals("190810114514Z",
+ parsed.toString());
+ assertEquals(ZonedDateTime.of(2019, 8, 10, 11, 45, 14,
+ 0, ZoneId.of("UTC")),
+ parsed.getTimestamp());
+ }
+
+ @Test
+ void testParse() throws ParseException {
+ ASN1Time parsed = new UtcTime(new BytesReader(new Byte[] {
+ 0x17, 13,
+ '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z'
+ }), false);
+
+ assertEquals(ZonedDateTime.of(2023, 9, 27, 11, 45, 14,
+ 0, ZoneId.of("UTC")),
+ parsed.getTimestamp());
+
+ // No seconds
+ parsed = new UtcTime(new BytesReader(new Byte[] {
+ 0x17, 11,
+ '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z'
+ }), false);
+ assertEquals(ZonedDateTime.of(2023, 9, 27, 11, 45, 0,
+ 0, ZoneId.of("UTC")),
+ parsed.getTimestamp());
+
+ // Length 0
+ assertThrows(ParseException.class, () ->
+ new UtcTime(new BytesReader(new Byte[]{
+ 0x17, 0
+ }), false));
+ // Early EOF
+ assertThrows(ParseException.class, () ->
+ new UtcTime(new BytesReader(new Byte[]{
+ 0x17, 13
+ }), false));
+ // No tailing Z
+ assertThrows(ParseException.class, () ->
+ new UtcTime(new BytesReader(new Byte[]{
+ 0x17, 12,
+ '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4'
+ }), false));
+ // Custom timezone
+ assertThrows(ParseException.class, () ->
+ new UtcTime(new BytesReader(new Byte[]{
+ 0x17, 18,
+ '2', '0', '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z', '-', '0', '8'
+ }), false));
+ // Invalid month / day / hour / minute / second
+ assertThrows(ParseException.class, () ->
+ new UtcTime(new BytesReader(new Byte[]{
+ 0x17, 13,
+ '2', '3', '1', '3', '2', '7', '1', '1', '4', '5', '1', '4', 'Z'
+ }), false));
+ assertThrows(ParseException.class, () ->
+ new UtcTime(new BytesReader(new Byte[]{
+ 0x17, 13,
+ '2', '3', '1', '0', '3', '2', '1', '1', '4', '5', '1', '4', 'Z'
+ }), false));
+ assertThrows(ParseException.class, () ->
+ new UtcTime(new BytesReader(new Byte[]{
+ 0x17, 13,
+ '2', '3', '1', '0', '3', '0', '2', '5', '4', '5', '1', '4', 'Z'
+ }), false));
+ assertThrows(ParseException.class, () ->
+ new UtcTime(new BytesReader(new Byte[]{
+ 0x17, 13,
+ '2', '3', '1', '0', '3', '0', '2', '4', '6', '1', '1', '4', 'Z'
+ }), false));
+ assertThrows(ParseException.class, () ->
+ new UtcTime(new BytesReader(new Byte[]{
+ 0x17, 13,
+ '2', '3', '1', '0', '3', '0', '2', '4', '6', '0', '6', '1', 'Z'
+ }), false));
+ }
+
+ @Test
+ void testEncode() throws ParseException {
+ assertEquals("230927114514Z", new UtcTime(new BytesReader(new Byte[] {
+ -95, 15,
+ 0x17, 13,
+ '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z'
+ }), true).toString());
+ // No seconds
+ assertEquals("2309271145Z", new UtcTime(new BytesReader(new Byte[] {
+ -95, 13,
+ 0x17, 11,
+ '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z'
+ }), true).toString());
+
+ // To byte array
+ assertArrayEquals(new Byte[] {
+ 0x17, 11,
+ '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z'
+ }, new UtcTime(UtcTime.TAG, null, ZonedDateTime.of(2023, 9,
+ 27, 11, 45, 0, 0, ZoneId.of("UTC")))
+ .encodeDER());
+ }
+}