diff options
author | Yuuta Liang <yuutaw@students.cs.ubc.ca> | 2023-10-12 12:10:33 +0800 |
---|---|---|
committer | Yuuta Liang <yuutaw@students.cs.ubc.ca> | 2023-10-12 12:10:33 +0800 |
commit | d342a45d98c4795b3a3fe1aaef5236ad4a782b55 (patch) | |
tree | f4ebc0ad962b138d9371413fcc71c97a559df506 /src/test/model/x501/NameTest.java | |
parent | e60c9c76243cfe0a408af98dc60bedb973e815db (diff) | |
download | jca-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/NameTest.java')
-rw-r--r-- | src/test/model/x501/NameTest.java | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/src/test/model/x501/NameTest.java b/src/test/model/x501/NameTest.java new file mode 100644 index 0000000..c649798 --- /dev/null +++ b/src/test/model/x501/NameTest.java @@ -0,0 +1,167 @@ +package model.x501; + +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import model.TestConstants; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class NameTest { + @Test + void testConstructor() { + assertEquals("users", + TestConstants.NAME_1.getRdnSequence()[1].getArray()[0].getValue().toString()); + assertEquals("CN", + TestConstants.NAME_2.getRdnSequence()[2].getArray()[0].getValue().toString()); + } + + @Test + void testParse() throws ParseException { + assertEquals("CA", new Name(new BytesReader(new Byte[]{ + 0x30, 45, // Name + + 0x31, 14, // RDN[0] + 0x30, 12, // KV[0] + 0x6, 3, 0x55, 0x04, 0x03, // CN + 0x13, 5, 'y', 'u', 'u', 't', 'a', // yuuta + + 0x31, 14, // RDN[1] + 0x30, 12, // KV[0] + 0x6, 3, 0x55, 0x04, 0xB, // OU + 0x13, 5, 'u', 's', 'e', 'r', 's', // users + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'A' // CA + }), false).getRdnSequence()[2].getArray()[0].getValue().toString()); + + assertEquals("SN=Qwq", new Name(new BytesReader(new Byte[]{ + 0x30, 38, // Name + + 0x31, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 9, // RDN[1] + 0x30, 7, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }), false).getRdnSequence()[0].toString()); + } + + @Test + void testParseFail() { + assertThrows(ParseException.class, () -> new Name(new BytesReader(new Byte[]{ + 0x30, 38, // Name + + // Wrong tag here + 0x30, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 9, // RDN[1] + 0x30, 7, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }), false)); + assertThrows(ParseException.class, () -> new Name(new BytesReader(new Byte[]{ + 0x30, 38, // Name + + 0x31, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 9, // RDN[1] + // Wrong tag here + 0x31, 7, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }), false)); + assertThrows(ParseException.class, () -> new Name(new BytesReader(new Byte[]{ + 0x30, 38, // Name + + 0x31, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 9, // RDN[1] + 0x30, 7, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + // Wrong tag here + 0x30, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }), false)); + } + + @Test + void testEncode() { + assertArrayEquals(new Byte[]{ + 0x30, 45, // Name + + 0x31, 14, // RDN[0] + 0x30, 12, // KV[0] + 0x6, 3, 0x55, 0x04, 0x03, // CN + 0x13, 5, 'y', 'u', 'u', 't', 'a', // yuuta + + 0x31, 14, // RDN[1] + 0x30, 12, // KV[0] + 0x6, 3, 0x55, 0x04, 0xB, // OU + 0x13, 5, 'u', 's', 'e', 'r', 's', // users + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'A' // CA + }, TestConstants.NAME_1.encodeDER()); + + assertArrayEquals(new Byte[]{ + 0x30, 40, // Name + + 0x31, 12, // RDN[0] + 0x30, 10, // KV[0] + 0x6, 3, 0x55, 0x04, 0x04, // CN + 0x13, 3, 'Q', 'w', 'q', // Qwq + + 0x31, 11, // RDN[1] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0xA, // O + 0x13, 2, 'I', 'T', // IT + + 0x31, 11, // RDN[2] + 0x30, 9, // KV[0] + 0x6, 3, 0x55, 0x04, 0x6, // C + 0x13, 2, 'C', 'N' // CN + }, TestConstants.NAME_2.encodeDER()); + } + + @Test + void testToString() { + assertEquals("CN=yuuta,OU=users,C=CA", TestConstants.NAME_1.toString()); + assertEquals("SN=Qwq,O=IT,C=CN", TestConstants.NAME_2.toString()); + } +} |