Skip to content

Commit 9983093

Browse files
committed
Merge remote-tracking branch 'Julien00859/tag-repr'
2 parents 197f1d2 + 907c601 commit 9983093

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/asn1.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@
2828
__version__ = "2.7.1"
2929

3030

31-
class Numbers(IntEnum):
31+
class HexEnum(IntEnum):
32+
def __repr__(self):
33+
return '<{cls}.{name}: 0x{value:02x}>'.format(
34+
cls=type(self).__name__,
35+
name=self.name,
36+
value=self.value
37+
)
38+
39+
40+
class Numbers(HexEnum):
3241
Boolean = 0x01
3342
Integer = 0x02
3443
BitString = 0x03
@@ -46,12 +55,12 @@ class Numbers(IntEnum):
4655
UnicodeString = 0x1e
4756

4857

49-
class Types(IntEnum):
58+
class Types(HexEnum):
5059
Constructed = 0x20
5160
Primitive = 0x00
5261

5362

54-
class Classes(IntEnum):
63+
class Classes(HexEnum):
5564
Universal = 0x00
5665
Application = 0x40
5766
Context = 0x80
@@ -573,6 +582,19 @@ def _read_tag(self): # type: () -> Tag
573582
nr = (nr << 7) | (byte & 0x7f)
574583
if not byte & 0x80:
575584
break
585+
try:
586+
typ = Types(typ)
587+
except ValueError:
588+
pass
589+
try:
590+
cls = Classes(cls)
591+
except ValueError:
592+
pass
593+
if cls == Classes.Universal:
594+
try:
595+
nr = Numbers(nr)
596+
except ValueError:
597+
pass
576598
return Tag(nr=nr, typ=typ, cls=cls)
577599

578600
def _read_length(self): # type: () -> int

tests/test_asn1.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ def test_boolean(self):
392392
dec.start(buf)
393393
tag = dec.peek()
394394
assert tag == (asn1.Numbers.Boolean, asn1.Types.Primitive, asn1.Classes.Universal)
395+
assert str(tag) == "Tag(nr=<Numbers.Boolean: 0x01>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
395396
tag, val = dec.read()
396397
assert isinstance(val, int)
397398
assert val
@@ -412,6 +413,7 @@ def test_integer(self):
412413
dec.start(buf)
413414
tag = dec.peek()
414415
assert tag == (asn1.Numbers.Integer, asn1.Types.Primitive, asn1.Classes.Universal)
416+
assert str(tag) == "Tag(nr=<Numbers.Integer: 0x02>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
415417
tag, val = dec.read()
416418
assert isinstance(val, int)
417419
assert val == 1
@@ -462,6 +464,7 @@ def test_octet_string(self):
462464
dec.start(buf)
463465
tag = dec.peek()
464466
assert tag == (asn1.Numbers.OctetString, asn1.Types.Primitive, asn1.Classes.Universal)
467+
assert str(tag) == "Tag(nr=<Numbers.OctetString: 0x04>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
465468
tag, val = dec.read()
466469
assert val == b'foo'
467470

@@ -471,6 +474,7 @@ def test_printable_string(self):
471474
dec.start(buf)
472475
tag = dec.peek()
473476
assert tag == (asn1.Numbers.PrintableString, asn1.Types.Primitive, asn1.Classes.Universal)
477+
assert str(tag) == "Tag(nr=<Numbers.PrintableString: 0x13>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
474478
tag, val = dec.read()
475479
assert val == u'foo'
476480

@@ -480,6 +484,7 @@ def test_bitstring(self):
480484
dec.start(buf)
481485
tag = dec.peek()
482486
assert tag == (asn1.Numbers.BitString, asn1.Types.Primitive, asn1.Classes.Universal)
487+
assert str(tag) == "Tag(nr=<Numbers.BitString: 0x03>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
483488
tag, val = dec.read()
484489
assert val == b'\x12\x34\x56'
485490

@@ -489,6 +494,7 @@ def test_bitstring_unused_bits(self):
489494
dec.start(buf)
490495
tag = dec.peek()
491496
assert tag == (asn1.Numbers.BitString, asn1.Types.Primitive, asn1.Classes.Universal)
497+
assert str(tag) == "Tag(nr=<Numbers.BitString: 0x03>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
492498
tag, val = dec.read()
493499
assert val == b'\x01\x23\x45'
494500

@@ -498,6 +504,7 @@ def test_unicode_printable_string(self):
498504
dec.start(buf)
499505
tag = dec.peek()
500506
assert tag == (asn1.Numbers.PrintableString, asn1.Types.Primitive, asn1.Classes.Universal)
507+
assert str(tag) == "Tag(nr=<Numbers.PrintableString: 0x13>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
501508
tag, val = dec.read()
502509
assert val == u'fooé'
503510

@@ -507,6 +514,7 @@ def test_null(self):
507514
dec.start(buf)
508515
tag = dec.peek()
509516
assert tag == (asn1.Numbers.Null, asn1.Types.Primitive, asn1.Classes.Universal)
517+
assert str(tag) == "Tag(nr=<Numbers.Null: 0x05>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
510518
tag, val = dec.read()
511519
assert val is None
512520

@@ -552,6 +560,7 @@ def test_enumerated(self):
552560
dec.start(buf)
553561
tag = dec.peek()
554562
assert tag == (asn1.Numbers.Enumerated, asn1.Types.Primitive, asn1.Classes.Universal)
563+
assert str(tag) == "Tag(nr=<Numbers.Enumerated: 0x0a>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
555564
tag, val = dec.read()
556565
assert isinstance(val, int)
557566
assert val == 1
@@ -562,6 +571,7 @@ def test_sequence(self):
562571
dec.start(buf)
563572
tag = dec.peek()
564573
assert tag == (asn1.Numbers.Sequence, asn1.Types.Constructed, asn1.Classes.Universal)
574+
assert str(tag) == "Tag(nr=<Numbers.Sequence: 0x10>, typ=<Types.Constructed: 0x20>, cls=<Classes.Universal: 0x00>)"
565575
dec.enter()
566576
tag, val = dec.read()
567577
assert val == 1
@@ -574,6 +584,7 @@ def test_sequence_of(self):
574584
dec.start(buf)
575585
tag = dec.peek()
576586
assert tag == (asn1.Numbers.Sequence, asn1.Types.Constructed, asn1.Classes.Universal)
587+
assert str(tag) == "Tag(nr=<Numbers.Sequence: 0x10>, typ=<Types.Constructed: 0x20>, cls=<Classes.Universal: 0x00>)"
577588
dec.enter()
578589
tag, val = dec.read()
579590
assert val == 1
@@ -586,6 +597,7 @@ def test_set(self):
586597
dec.start(buf)
587598
tag = dec.peek()
588599
assert tag == (asn1.Numbers.Set, asn1.Types.Constructed, asn1.Classes.Universal)
600+
assert str(tag) == "Tag(nr=<Numbers.Set: 0x11>, typ=<Types.Constructed: 0x20>, cls=<Classes.Universal: 0x00>)"
589601
dec.enter()
590602
tag, val = dec.read()
591603
assert val == 1
@@ -598,6 +610,7 @@ def test_set_of(self):
598610
dec.start(buf)
599611
tag = dec.peek()
600612
assert tag == (asn1.Numbers.Set, asn1.Types.Constructed, asn1.Classes.Universal)
613+
assert str(tag) == "Tag(nr=<Numbers.Set: 0x11>, typ=<Types.Constructed: 0x20>, cls=<Classes.Universal: 0x00>)"
601614
dec.enter()
602615
tag, val = dec.read()
603616
assert val == 1
@@ -610,6 +623,7 @@ def test_context(self):
610623
dec.start(buf)
611624
tag = dec.peek()
612625
assert tag == (1, asn1.Types.Constructed, asn1.Classes.Context)
626+
assert str(tag) == "Tag(nr=1, typ=<Types.Constructed: 0x20>, cls=<Classes.Context: 0x80>)"
613627
dec.enter()
614628
tag, val = dec.read()
615629
assert val == 1
@@ -620,6 +634,7 @@ def test_application(self):
620634
dec.start(buf)
621635
tag = dec.peek()
622636
assert tag == (1, asn1.Types.Constructed, asn1.Classes.Application)
637+
assert str(tag) == "Tag(nr=1, typ=<Types.Constructed: 0x20>, cls=<Classes.Application: 0x40>)"
623638
dec.enter()
624639
tag, val = dec.read()
625640
assert val == 1
@@ -630,6 +645,7 @@ def test_private(self):
630645
dec.start(buf)
631646
tag = dec.peek()
632647
assert tag == (1, asn1.Types.Constructed, asn1.Classes.Private)
648+
assert str(tag) == "Tag(nr=1, typ=<Types.Constructed: 0x20>, cls=<Classes.Private: 0xc0>)"
633649
dec.enter()
634650
tag, val = dec.read()
635651
assert val == 1

0 commit comments

Comments
 (0)