aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/x501/AttributeTypeAndValueTest.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/x501/AttributeTypeAndValueTest.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/x501/AttributeTypeAndValueTest.java')
-rw-r--r--src/test/model/x501/AttributeTypeAndValueTest.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/test/model/x501/AttributeTypeAndValueTest.java b/src/test/model/x501/AttributeTypeAndValueTest.java
new file mode 100644
index 0000000..ea9c17e
--- /dev/null
+++ b/src/test/model/x501/AttributeTypeAndValueTest.java
@@ -0,0 +1,90 @@
+package model.x501;
+
+import model.asn1.*;
+import model.asn1.exceptions.ParseException;
+import model.asn1.parsing.BytesReader;
+import org.junit.jupiter.api.Test;
+
+import static model.asn1.ObjectIdentifier.*;
+import static org.junit.jupiter.api.Assertions.*;
+
+public class AttributeTypeAndValueTest {
+ @Test
+ void testConstructor() throws ParseException {
+ assertArrayEquals(OID_OU,
+ new AttributeTypeAndValue(
+ ASN1Object.TAG_SEQUENCE, null,
+ new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU),
+ new Null(Null.TAG, null))
+ .getType().getInts());
+ assertEquals("123",
+ new AttributeTypeAndValue(
+ ASN1Object.TAG_SEQUENCE, null,
+ new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU),
+ new PrintableString(PrintableString.TAG, null, "123"))
+ .getValue().toString());
+ }
+
+ @Test
+ void testParse() throws ParseException {
+ // C = IT
+ assertArrayEquals(OID_C, new AttributeTypeAndValue(new BytesReader(new Byte[]{
+ 0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54
+ }), false).getType().getInts());
+ assertEquals("IT", ((PrintableString) new AttributeTypeAndValue(new BytesReader(new Byte[]{
+ 0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54
+ }), false).getValue()).getString());
+
+ // CN = Test ed25519
+ assertArrayEquals(OID_CN, new AttributeTypeAndValue(new BytesReader(new Byte[]{
+ 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
+ 0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64,
+ 0x32, 0x35, 0x35, 0x31, 0x39
+ }), false).getType().getInts());
+ assertEquals("Test ed25519", ((ASN1String) new AttributeTypeAndValue(new BytesReader(new Byte[]{
+ 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
+ 0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64,
+ 0x32, 0x35, 0x35, 0x31, 0x39
+ }), false).getValue()).getString());
+ }
+
+ @Test
+ void testParseFail() {
+ // No type
+ assertThrows(ParseException.class, () -> new AttributeTypeAndValue(new BytesReader(new Byte[]{
+ 0x30, 0x0
+ }), false));
+ // No value
+ assertThrows(ParseException.class, () -> new AttributeTypeAndValue(new BytesReader(new Byte[]{
+ 0x30, 0x5, 0x6, 0x3, 0x55, 0x4, 0x6
+ }), false));
+ // Incorrect type tag (should be OID)
+ assertThrows(ParseException.class, () -> new AttributeTypeAndValue(new BytesReader(new Byte[]{
+ 0x30, 0x9, 0x7, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54
+ }), false));
+ }
+
+ @Test
+ void testEncode() throws ParseException {
+ assertArrayEquals(new Byte[]{
+ 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
+ 0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64,
+ 0x32, 0x35, 0x35, 0x31, 0x39
+ }, new AttributeTypeAndValue(ASN1Object.TAG_SEQUENCE, null,
+ new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_CN),
+ new UTF8String(UTF8String.TAG, null, "Test ed25519"))
+ .encodeDER());
+ }
+
+ @Test
+ void testToString() throws ParseException {
+ assertEquals("CN=Test ed25519", new AttributeTypeAndValue(new BytesReader(new Byte[]{
+ 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
+ 0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64,
+ 0x32, 0x35, 0x35, 0x31, 0x39
+ }), false).toString());
+ assertEquals("C=IT", new AttributeTypeAndValue(new BytesReader(new Byte[]{
+ 0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54
+ }), false).toString());
+ }
+}