Skip to content

Commit 2ce5211

Browse files
committed
add tests for missing coverage
Signed-off-by: Facundo Tuesca <facundo.tuesca@trailofbits.com>
1 parent 3c6db8c commit 2ce5211

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

src/rust/src/declarative_asn1/types.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,12 @@ pub(crate) fn check_size_constraint(
550550
mod tests {
551551

552552
use asn1::SimpleAsn1Readable;
553-
use pyo3::IntoPyObject;
553+
use pyo3::{IntoPyObject, PyTypeInfo};
554554

555-
use super::{is_tag_valid_for_type, AnnotatedType, Annotation, Type};
555+
use super::{
556+
is_tag_valid_for_type, is_tag_valid_for_variant, AnnotatedType, Annotation, Encoding, Type,
557+
Variant,
558+
};
556559

557560
#[test]
558561
// Needed for coverage of `is_tag_valid_for_type(Type::Option(..))`, since
@@ -599,4 +602,41 @@ mod tests {
599602
));
600603
})
601604
}
605+
#[test]
606+
// Needed for coverage of
607+
// `is_tag_valid_for_variant(..., encoding=Encoding::Implicit)`, since
608+
// `is_tag_valid_for_variant` is never called with an implicit encoding.
609+
fn test_is_tag_valid_for_implicit_variant() {
610+
pyo3::Python::initialize();
611+
612+
pyo3::Python::attach(|py| {
613+
let ann_type = pyo3::Py::new(
614+
py,
615+
AnnotatedType {
616+
inner: pyo3::Py::new(py, Type::PyInt()).unwrap(),
617+
annotation: Annotation {
618+
default: None,
619+
encoding: None,
620+
size: None,
621+
}
622+
.into_pyobject(py)
623+
.unwrap()
624+
.unbind(),
625+
},
626+
)
627+
.unwrap();
628+
let variant = Variant {
629+
python_class: pyo3::types::PyInt::type_object(py).unbind(),
630+
ann_type,
631+
tag_name: None,
632+
};
633+
let encoding = pyo3::Py::new(py, Encoding::Implicit(3)).ok();
634+
assert!(is_tag_valid_for_variant(
635+
py,
636+
asn1::BigInt::TAG,
637+
&variant,
638+
&encoding
639+
));
640+
})
641+
}
602642
}

tests/hazmat/asn1/test_serialization.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,66 @@ class Example:
488488
]
489489
)
490490

491+
def test_ok_sequence_all_types_default(self) -> None:
492+
default_time = datetime.datetime(
493+
2019,
494+
12,
495+
16,
496+
3,
497+
2,
498+
10,
499+
tzinfo=datetime.timezone.utc,
500+
)
501+
default_oid = x509.ObjectIdentifier("1.3.6.1.4.1.343")
502+
503+
@asn1.sequence
504+
@_comparable_dataclass
505+
class Example:
506+
a: Annotated[int, asn1.Default(3)]
507+
b: Annotated[bytes, asn1.Default(b"\x00")]
508+
c: Annotated[
509+
asn1.PrintableString, asn1.Default(asn1.PrintableString("a"))
510+
]
511+
d: Annotated[
512+
asn1.UtcTime,
513+
asn1.Default(asn1.UtcTime(default_time)),
514+
]
515+
e: Annotated[
516+
asn1.GeneralizedTime,
517+
asn1.Default(asn1.GeneralizedTime(default_time)),
518+
]
519+
f: Annotated[typing.List[int], asn1.Default([1])]
520+
g: Annotated[
521+
asn1.BitString,
522+
asn1.Default(
523+
asn1.BitString(data=b"", padding_bits=0),
524+
),
525+
]
526+
h: Annotated[asn1.IA5String, asn1.Default(asn1.IA5String("a"))]
527+
i: Annotated[
528+
x509.ObjectIdentifier,
529+
asn1.Default(default_oid),
530+
]
531+
532+
assert_roundtrips(
533+
[
534+
(
535+
Example(
536+
a=3,
537+
b=b"\x00",
538+
c=asn1.PrintableString("a"),
539+
d=asn1.UtcTime(default_time),
540+
e=asn1.GeneralizedTime(default_time),
541+
f=[1],
542+
g=asn1.BitString(data=b"", padding_bits=0),
543+
h=asn1.IA5String("a"),
544+
i=default_oid,
545+
),
546+
b"\x30\x00",
547+
)
548+
]
549+
)
550+
491551
def test_ok_sequence_with_default_annotations(self) -> None:
492552
@asn1.sequence
493553
@_comparable_dataclass

0 commit comments

Comments
 (0)