From df3eb7af36b6797e0c8e09a179191c329889da57 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Sat, 14 Oct 2023 06:17:57 +0800 Subject: Fix coverage Signed-off-by: Yuuta Liang --- src/main/model/asn1/TagClass.java | 26 ++++++------------- src/main/model/pki/cert/TbsCertificate.java | 6 ++--- src/test/model/asn1/TagClassTest.java | 8 +++--- src/test/model/ca/TemplateTest.java | 33 +++++++++++++++++++++++++ src/test/model/pki/cert/TbsCertificateTest.java | 3 +++ 5 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/main/model/asn1/TagClass.java b/src/main/model/asn1/TagClass.java index 83dd4e9..2693512 100644 --- a/src/main/model/asn1/TagClass.java +++ b/src/main/model/asn1/TagClass.java @@ -7,10 +7,14 @@ package model.asn1; * This class also represents the value to the two highest bits of DER-encoded tag values. */ public enum TagClass { - UNIVERSAL(Values.UNIVERSAL), - APPLICATION(Values.APPLICATION), - PRIVATE(Values.PRIVATE), - CONTEXT_SPECIFIC(Values.CONTENT_SPECIFIC); + // 0b00000000 + UNIVERSAL((byte) 0x0), + // 0b01000000 + APPLICATION((byte) 0x40), + // 0b11000000 + PRIVATE((byte) -64), + // 0b10000000 + CONTEXT_SPECIFIC((byte) -128); private final Byte val; @@ -25,18 +29,4 @@ public enum TagClass { public Byte getVal() { return val; } - - /** - * The constants of high-two-bit values for Tag DER encoding. - */ - public static final class Values { - // 0b00000000 - public static final Byte UNIVERSAL = 0x0; - // 0b01000000 - public static final Byte APPLICATION = 0x40; - // 0b11000000 - public static final Byte PRIVATE = -64; - // 0b10000000 - public static final Byte CONTENT_SPECIFIC = -128; - } } diff --git a/src/main/model/pki/cert/TbsCertificate.java b/src/main/model/pki/cert/TbsCertificate.java index 84cf0ba..26b30f4 100644 --- a/src/main/model/pki/cert/TbsCertificate.java +++ b/src/main/model/pki/cert/TbsCertificate.java @@ -171,7 +171,7 @@ public class TbsCertificate extends ASN1Object { } else { // Enforce the extensions tag - nothing else should be here. if (Integer.compareUnsigned(getLength(), (encoded.getIndex() - i)) != 0) { - new Tag(encoded).enforce(new Tag(TagClass.CONTEXT_SPECIFIC, true, 3)); + throw new ParseException("Unexpected objects after."); } this.extensions = null; } @@ -204,9 +204,7 @@ public class TbsCertificate extends ASN1Object { */ private void enforceVersion() throws ParseException { if (version != null - && (version.getLong() != VERSION_V1 - && version.getLong() != VERSION_V2 - && version.getLong() != VERSION_V3)) { + && (version.getLong() < VERSION_V1 || version.getLong() > VERSION_V3)) { throw new ParseException("Illegal certificate version: " + version.getLong()); } if (extensions != null && (version == null || version.getLong() != VERSION_V3)) { diff --git a/src/test/model/asn1/TagClassTest.java b/src/test/model/asn1/TagClassTest.java index e7d6531..e3f9987 100644 --- a/src/test/model/asn1/TagClassTest.java +++ b/src/test/model/asn1/TagClassTest.java @@ -7,9 +7,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class TagClassTest { @Test void testConstructor() { - assertEquals(TagClass.Values.UNIVERSAL, TagClass.UNIVERSAL.getVal()); - assertEquals(TagClass.Values.PRIVATE, TagClass.PRIVATE.getVal()); - assertEquals(TagClass.Values.CONTENT_SPECIFIC, TagClass.CONTEXT_SPECIFIC.getVal()); - assertEquals(TagClass.Values.APPLICATION, TagClass.APPLICATION.getVal()); + assertEquals((byte) 0, TagClass.UNIVERSAL.getVal()); + assertEquals((byte) -64, TagClass.PRIVATE.getVal()); + assertEquals((byte) -128, TagClass.CONTEXT_SPECIFIC.getVal()); + assertEquals((byte) 0x40, TagClass.APPLICATION.getVal()); } } diff --git a/src/test/model/ca/TemplateTest.java b/src/test/model/ca/TemplateTest.java index 0ca7434..4014599 100644 --- a/src/test/model/ca/TemplateTest.java +++ b/src/test/model/ca/TemplateTest.java @@ -1,6 +1,12 @@ package model.ca; +import model.asn1.ASN1Object; +import model.asn1.ObjectIdentifier; +import model.asn1.PrintableString; import model.asn1.exceptions.ParseException; +import model.x501.AttributeTypeAndValue; +import model.x501.Name; +import model.x501.RelativeDistinguishedName; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -24,4 +30,31 @@ public class TemplateTest { assertNull(template.getSubject()); assertThrows(ParseException.class, () -> template.setSubject("*")); } + + @Test + void testSetters() throws ParseException { + Template template = new Template("123", true, null, 123); + + template.setName("114514"); + assertEquals("114514", template.getName()); + + template.setSubject(new Name(ASN1Object.TAG_SEQUENCE, null, new RelativeDistinguishedName[]{ + new RelativeDistinguishedName(ASN1Object.TAG_SET, null, new AttributeTypeAndValue[]{ + new AttributeTypeAndValue(ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, + ObjectIdentifier.OID_CN), + new PrintableString(PrintableString.TAG, null, "Test"))}), + new RelativeDistinguishedName(ASN1Object.TAG_SET, null, new AttributeTypeAndValue[]{ + new AttributeTypeAndValue(ASN1Object.TAG_SEQUENCE, null, + new ObjectIdentifier(ObjectIdentifier.TAG, null, + ObjectIdentifier.OID_C), + new PrintableString(PrintableString.TAG, null, "CA"))})})); + assertEquals("CN=Test,C=CA", template.getSubject().toString()); + + template.setEnabled(false); + assertFalse(template.isEnabled()); + + template.setValidity(233); + assertEquals(233, template.getValidity()); + } } diff --git a/src/test/model/pki/cert/TbsCertificateTest.java b/src/test/model/pki/cert/TbsCertificateTest.java index 972f59a..ee2a9c7 100644 --- a/src/test/model/pki/cert/TbsCertificateTest.java +++ b/src/test/model/pki/cert/TbsCertificateTest.java @@ -164,6 +164,9 @@ public class TbsCertificateTest { assertThrows(ParseException.class, () -> new TbsCertificate(new BytesReader(mutate(in, 8, 0x2, TbsCertificate.VERSION_V3 + 1)), false)); + assertThrows(ParseException.class, () -> + new TbsCertificate(new BytesReader(mutate(in, 8, 0x2, -10)), + false)); // Extensions exist, but no version final TbsCertificate certV1 = new TbsCertificate(new BytesReader(trimToTbs(TestConstants.CERT_V1)), false); -- cgit v1.2.3