@@ -313,6 +313,111 @@ class Example:
313313 ]
314314 )
315315
316+ def test_ok_sequenceof_size_restriction (self ) -> None :
317+ @asn1 .sequence
318+ @_comparable_dataclass
319+ class Example :
320+ a : Annotated [typing .List [int ], asn1 .Size (min = 1 , max = 4 )]
321+
322+ assert_roundtrips (
323+ [
324+ (
325+ Example (a = [1 , 2 , 3 , 4 ]),
326+ b"\x30 \x0e \x30 \x0c \x02 \x01 \x01 \x02 \x01 \x02 \x02 \x01 \x03 \x02 \x01 \x04 " ,
327+ )
328+ ]
329+ )
330+
331+ def test_ok_sequenceof_size_restriction_no_max (self ) -> None :
332+ @asn1 .sequence
333+ @_comparable_dataclass
334+ class Example :
335+ a : Annotated [typing .List [int ], asn1 .Size (min = 1 , max = None )]
336+
337+ assert_roundtrips (
338+ [
339+ (
340+ Example (a = [1 , 2 , 3 , 4 ]),
341+ b"\x30 \x0e \x30 \x0c \x02 \x01 \x01 \x02 \x01 \x02 \x02 \x01 \x03 \x02 \x01 \x04 " ,
342+ )
343+ ]
344+ )
345+
346+ def test_ok_sequenceof_size_restriction_exact (self ) -> None :
347+ @asn1 .sequence
348+ @_comparable_dataclass
349+ class Example :
350+ a : Annotated [typing .List [int ], asn1 .Size .exact (4 )]
351+
352+ assert_roundtrips (
353+ [
354+ (
355+ Example (a = [1 , 2 , 3 , 4 ]),
356+ b"\x30 \x0e \x30 \x0c \x02 \x01 \x01 \x02 \x01 \x02 \x02 \x01 \x03 \x02 \x01 \x04 " ,
357+ )
358+ ]
359+ )
360+
361+ def test_fail_sequenceof_size_too_big (self ) -> None :
362+ @asn1 .sequence
363+ @_comparable_dataclass
364+ class Example :
365+ a : Annotated [typing .List [int ], asn1 .Size (min = 1 , max = 2 )]
366+
367+ with pytest .raises (
368+ ValueError ,
369+ match = re .escape ("SEQUENCE OF has size 4, expected size in [1, 2]" ),
370+ ):
371+ asn1 .decode_der (
372+ Example ,
373+ b"\x30 \x0e \x30 \x0c \x02 \x01 \x01 \x02 \x01 \x02 \x02 \x01 \x03 \x02 \x01 \x04 " ,
374+ )
375+
376+ with pytest .raises (
377+ ValueError ,
378+ ):
379+ asn1 .encode_der (Example (a = [1 , 2 , 3 , 4 ]))
380+
381+ def test_fail_sequenceof_size_too_small (self ) -> None :
382+ @asn1 .sequence
383+ @_comparable_dataclass
384+ class Example :
385+ a : Annotated [typing .List [int ], asn1 .Size (min = 5 , max = 6 )]
386+
387+ with pytest .raises (
388+ ValueError ,
389+ match = re .escape ("SEQUENCE OF has size 4, expected size in [5, 6]" ),
390+ ):
391+ asn1 .decode_der (
392+ Example ,
393+ b"\x30 \x0e \x30 \x0c \x02 \x01 \x01 \x02 \x01 \x02 \x02 \x01 \x03 \x02 \x01 \x04 " ,
394+ )
395+
396+ with pytest .raises (
397+ ValueError ,
398+ ):
399+ asn1 .encode_der (Example (a = [1 , 2 , 3 , 4 ]))
400+
401+ def test_fail_sequenceof_size_not_exact (self ) -> None :
402+ @asn1 .sequence
403+ @_comparable_dataclass
404+ class Example :
405+ a : Annotated [typing .List [int ], asn1 .Size .exact (5 )]
406+
407+ with pytest .raises (
408+ ValueError ,
409+ match = re .escape ("SEQUENCE OF has size 4, expected size in [5, 5]" ),
410+ ):
411+ asn1 .decode_der (
412+ Example ,
413+ b"\x30 \x0e \x30 \x0c \x02 \x01 \x01 \x02 \x01 \x02 \x02 \x01 \x03 \x02 \x01 \x04 " ,
414+ )
415+
416+ with pytest .raises (
417+ ValueError ,
418+ ):
419+ asn1 .encode_der (Example (a = [1 , 2 , 3 , 4 ]))
420+
316421 def test_ok_sequence_with_optionals (self ) -> None :
317422 @asn1 .sequence
318423 @_comparable_dataclass
0 commit comments