1212from __future__ import print_function
1313from __future__ import unicode_literals
1414
15+ import base64
1516from builtins import int
1617
1718import pytest
@@ -312,7 +313,7 @@ def test_error_object_identifier(self):
312313 pytest .raises (asn1 .Error , enc .write , 'foo' , asn1 .Numbers .ObjectIdentifier )
313314 pytest .raises (asn1 .Error , enc .write , 'foo.bar' , asn1 .Numbers .ObjectIdentifier )
314315
315- def test_default_encding (self ):
316+ def test_default_encoding (self ):
316317 " Check that the encoder implicitly chooses the correct asn1 type "
317318 def check_defaults (value , number ):
318319 default , explicit = asn1 .Encoder (), asn1 .Encoder ()
@@ -329,6 +330,20 @@ def check_defaults(value, number):
329330 check_defaults (u"unicode string \U0001f4a9 " , asn1 .Numbers .PrintableString )
330331 check_defaults (None , asn1 .Numbers .Null )
331332
333+ def test_context_no_tag_number (self ):
334+ enc = asn1 .Encoder ()
335+ enc .start ()
336+ with pytest .raises (asn1 .Error ):
337+ enc .write (b'\x00 \x01 \x02 \x03 \x04 ' , typ = asn1 .Types .Primitive , cls = asn1 .Classes .Context )
338+
339+ def test_context (self ):
340+ enc = asn1 .Encoder ()
341+ enc .start ()
342+ enc .write (b'\x00 \x01 \x02 \x03 \x04 ' , nr = 10 , typ = asn1 .Types .Primitive , cls = asn1 .Classes .Context )
343+ res = enc .output ()
344+ assert res == b'\x8a \x05 \x00 \x01 \x02 \x03 \x04 '
345+
346+
332347class TestDecoder (object ):
333348 """Test suite for ASN1 Decoder."""
334349
@@ -696,6 +711,242 @@ def test_big_negative_integer(self):
696711 assert val == - 668929531791034950848739021124816874
697712 assert dec .eof ()
698713
714+ def test_context (self ):
715+ encoded = 'tYHKgAETgwgBgDgJAGMS9aQGgAQBAAAChQUAh7Mfc6YGgAQBAAABhwx0ZXN0LnRlc3Quc2WIAgEhqQigBoAECtiCBIsBAawuM' \
716+ 'CyCDAIjYh+TlkBYdGMQQIMBAIQBAIUBAoYJFwkVAClUKwAAiAgAIvIQAG0Yj40JFwkUIylUKwAAjgIOEI8BAJEBAZIJRENQMk' \
717+ 'dHU04xlQEAlgmRI3cAUGBTA/CXAgAAmAEAmwMi8hCdCFOTKXBYgkMQngECnx8CgAGfIAgAIvIQAG0Yjw=='
718+ buf = base64 .b64decode (encoded )
719+
720+ dec = asn1 .Decoder ()
721+ dec .start (buf )
722+
723+ tag = dec .peek ()
724+ assert tag .typ == asn1 .Types .Constructed
725+ assert tag .cls == asn1 .Classes .Context
726+ assert tag .nr == 21
727+
728+ dec .enter ()
729+ tag , value = dec .read ()
730+ assert tag .typ == asn1 .Types .Primitive
731+ assert tag .cls == asn1 .Classes .Context
732+ assert tag .nr == 0
733+ assert value == b'\x13 '
734+
735+ tag , value = dec .read ()
736+ assert tag .typ == asn1 .Types .Primitive
737+ assert tag .cls == asn1 .Classes .Context
738+ assert tag .nr == 3
739+ assert value == b'\x01 \x80 \x38 \x09 \x00 \x63 \x12 \xf5 '
740+
741+ tag = dec .peek ()
742+ assert tag .typ == asn1 .Types .Constructed
743+ assert tag .cls == asn1 .Classes .Context
744+ assert tag .nr == 4
745+
746+ dec .enter ()
747+ tag , value = dec .read ()
748+ assert tag .typ == asn1 .Types .Primitive
749+ assert tag .cls == asn1 .Classes .Context
750+ assert tag .nr == 0
751+ assert value == b'\x01 \x00 \x00 \x02 '
752+ dec .leave ()
753+
754+ tag , value = dec .read ()
755+ assert tag .typ == asn1 .Types .Primitive
756+ assert tag .cls == asn1 .Classes .Context
757+ assert tag .nr == 5
758+ assert value == b'\x00 \x87 \xB3 \x1F \x73 '
759+
760+ tag = dec .peek ()
761+ assert tag .typ == asn1 .Types .Constructed
762+ assert tag .cls == asn1 .Classes .Context
763+ assert tag .nr == 6
764+
765+ dec .enter ()
766+ tag , value = dec .read ()
767+ assert tag .typ == asn1 .Types .Primitive
768+ assert tag .cls == asn1 .Classes .Context
769+ assert tag .nr == 0
770+ assert value == b'\x01 \x00 \x00 \x01 '
771+ dec .leave ()
772+
773+ tag , value = dec .read ()
774+ assert tag .typ == asn1 .Types .Primitive
775+ assert tag .cls == asn1 .Classes .Context
776+ assert tag .nr == 7
777+ assert value == b'test.test.se'
778+
779+ tag , value = dec .read ()
780+ assert tag .typ == asn1 .Types .Primitive
781+ assert tag .cls == asn1 .Classes .Context
782+ assert tag .nr == 8
783+ assert value == b'\x01 \x21 '
784+
785+ tag = dec .peek ()
786+ assert tag .typ == asn1 .Types .Constructed
787+ assert tag .cls == asn1 .Classes .Context
788+ assert tag .nr == 9
789+
790+ dec .enter ()
791+
792+ tag = dec .peek ()
793+ assert tag .typ == asn1 .Types .Constructed
794+ assert tag .cls == asn1 .Classes .Context
795+ assert tag .nr == 0
796+
797+ dec .enter ()
798+ tag , value = dec .read ()
799+ assert tag .typ == asn1 .Types .Primitive
800+ assert tag .cls == asn1 .Classes .Context
801+ assert tag .nr == 0
802+ assert value == b'\x0A \xD8 \x82 \x04 '
803+ dec .leave ()
804+ dec .leave ()
805+
806+ tag , value = dec .read ()
807+ assert tag .typ == asn1 .Types .Primitive
808+ assert tag .cls == asn1 .Classes .Context
809+ assert tag .nr == 11
810+ assert value == b'\x01 '
811+
812+ tag = dec .peek ()
813+ assert tag .typ == asn1 .Types .Constructed
814+ assert tag .cls == asn1 .Classes .Context
815+ assert tag .nr == 12
816+
817+ dec .enter ()
818+ tag = dec .peek ()
819+ assert tag .typ == asn1 .Types .Constructed
820+ assert tag .cls == asn1 .Classes .Universal
821+ assert tag .nr == 16
822+
823+ dec .enter ()
824+ tag , value = dec .read ()
825+ assert tag .typ == asn1 .Types .Primitive
826+ assert tag .cls == asn1 .Classes .Context
827+ assert tag .nr == 2
828+ assert value == b'\x02 \x23 \x62 \x1F \x93 \x96 \x40 \x58 \x74 \x63 \x10 \x40 '
829+
830+ tag , value = dec .read ()
831+ assert tag .typ == asn1 .Types .Primitive
832+ assert tag .cls == asn1 .Classes .Context
833+ assert tag .nr == 3
834+ assert value == b'\x00 '
835+
836+ tag , value = dec .read ()
837+ assert tag .typ == asn1 .Types .Primitive
838+ assert tag .cls == asn1 .Classes .Context
839+ assert tag .nr == 4
840+ assert value == b'\x00 '
841+
842+ tag , value = dec .read ()
843+ assert tag .typ == asn1 .Types .Primitive
844+ assert tag .cls == asn1 .Classes .Context
845+ assert tag .nr == 5
846+ assert value == b'\x02 '
847+
848+ tag , value = dec .read ()
849+ assert tag .typ == asn1 .Types .Primitive
850+ assert tag .cls == asn1 .Classes .Context
851+ assert tag .nr == 6
852+ assert value == b'\x17 \x09 \x15 \x00 \x29 \x54 \x2B \x00 \x00 '
853+
854+ tag , value = dec .read ()
855+ assert tag .typ == asn1 .Types .Primitive
856+ assert tag .cls == asn1 .Classes .Context
857+ assert tag .nr == 8
858+ assert value == b'\x00 \x22 \xF2 \x10 \x00 \x6D \x18 \x8F '
859+
860+ dec .leave ()
861+ dec .leave ()
862+
863+ tag , value = dec .read ()
864+ assert tag .typ == asn1 .Types .Primitive
865+ assert tag .cls == asn1 .Classes .Context
866+ assert tag .nr == 13
867+ assert value == b'\x17 \x09 \x14 \x23 \x29 \x54 \x2B \x00 \x00 '
868+
869+ tag , value = dec .read ()
870+ assert tag .typ == asn1 .Types .Primitive
871+ assert tag .cls == asn1 .Classes .Context
872+ assert tag .nr == 14
873+ assert value == b'\x0E \x10 '
874+
875+ tag , value = dec .read ()
876+ assert tag .typ == asn1 .Types .Primitive
877+ assert tag .cls == asn1 .Classes .Context
878+ assert tag .nr == 15
879+ assert value == b'\x00 '
880+
881+ tag , value = dec .read ()
882+ assert tag .typ == asn1 .Types .Primitive
883+ assert tag .cls == asn1 .Classes .Context
884+ assert tag .nr == 17
885+ assert value == b'\x01 '
886+
887+ tag , value = dec .read ()
888+ assert tag .typ == asn1 .Types .Primitive
889+ assert tag .cls == asn1 .Classes .Context
890+ assert tag .nr == 18
891+ assert value == b'DCP2GGSN1'
892+
893+ tag , value = dec .read ()
894+ assert tag .typ == asn1 .Types .Primitive
895+ assert tag .cls == asn1 .Classes .Context
896+ assert tag .nr == 21
897+ assert value == b'\x00 '
898+
899+ tag , value = dec .read ()
900+ assert tag .typ == asn1 .Types .Primitive
901+ assert tag .cls == asn1 .Classes .Context
902+ assert tag .nr == 22
903+ assert value == b'\x91 \x23 \x77 \x00 \x50 \x60 \x53 \x03 \xF0 '
904+
905+ tag , value = dec .read ()
906+ assert tag .typ == asn1 .Types .Primitive
907+ assert tag .cls == asn1 .Classes .Context
908+ assert tag .nr == 23
909+ assert value == b'\x00 \x00 '
910+
911+ tag , value = dec .read ()
912+ assert tag .typ == asn1 .Types .Primitive
913+ assert tag .cls == asn1 .Classes .Context
914+ assert tag .nr == 24
915+ assert value == b'\x00 '
916+
917+ tag , value = dec .read ()
918+ assert tag .typ == asn1 .Types .Primitive
919+ assert tag .cls == asn1 .Classes .Context
920+ assert tag .nr == 27
921+ assert value == b'\x22 \xF2 \x10 '
922+
923+ tag , value = dec .read ()
924+ assert tag .typ == asn1 .Types .Primitive
925+ assert tag .cls == asn1 .Classes .Context
926+ assert tag .nr == 29
927+ assert value == b'\x53 \x93 \x29 \x70 \x58 \x82 \x43 \x10 '
928+
929+ tag , value = dec .read ()
930+ assert tag .typ == asn1 .Types .Primitive
931+ assert tag .cls == asn1 .Classes .Context
932+ assert tag .nr == 30
933+ assert value == b'\x02 '
934+
935+ tag , value = dec .read ()
936+ assert tag .typ == asn1 .Types .Primitive
937+ assert tag .cls == asn1 .Classes .Context
938+ assert tag .nr == 31
939+ assert value == b'\x80 \x01 '
940+
941+ tag , value = dec .read ()
942+ assert tag .typ == asn1 .Types .Primitive
943+ assert tag .cls == asn1 .Classes .Context
944+ assert tag .nr == 32
945+ assert value == b'\x00 \x22 \xF2 \x10 \x00 \x6D \x18 \x8F '
946+
947+ assert dec .peek () is None
948+
949+
699950class TestEncoderDecoder (object ):
700951 """Test suite for ASN1 Encoder and Decoder."""
701952
0 commit comments