def test_bit_string_copy(self): if _debug: TestBitString._debug("test_bit_string_copy") sample_value = [0, 1, 0, 1] obj1 = BitString(sample_value) obj2 = BitString(obj1) assert obj2.value == sample_value
def test_bit_string_tag(self): if _debug: TestBitString._debug("test_bit_string_tag") tag = Tag(Tag.applicationTagClass, Tag.bitStringAppTag, 1, xtob('08')) obj = BitString(tag) if _debug: TestBitString._debug(" - obj.value: %r", obj.value) assert obj.value == [] tag = Tag(Tag.applicationTagClass, Tag.bitStringAppTag, 2, xtob('0102')) obj = BitString(tag) if _debug: TestBitString._debug(" - obj.value: %r", obj.value) assert obj.value == [0, 0, 0, 0, 0, 0, 1] tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob('')) with self.assertRaises(InvalidTag): BitString(tag) tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff')) with self.assertRaises(InvalidTag): BitString(tag) tag = Tag(Tag.openingTagClass, 0) with self.assertRaises(InvalidTag): BitString(tag)
def bit_string_decode(tag): """Decode an bit_string application tag into an bit_string.""" if _debug: bit_string_decode._debug("bit_string_decode %r", tag) obj = BitString(tag) if _debug: bit_string_decode._debug(" - obj: %r", obj) return obj
def test_bit_string(self): if _debug: TestBitString._debug("test_bit_string") obj = BitString() assert obj.value == [] assert str(obj) == "BitString()" obj = BitString([0]) assert obj.value == [0] assert str(obj) == "BitString(0)" obj = BitString([0, 1]) assert obj.value == [0, 1] assert str(obj) == "BitString(0,1)" with self.assertRaises(TypeError): BitString("some string") with self.assertRaises(TypeError): BitString(1.0)
def bit_string_endec(v, x): """Pass the value to BitString, construct a tag from the hex string, and compare results of encode and decoding each other.""" if _debug: bit_string_endec._debug("bit_string_endec %r %r", v, x) tag = bit_string_tag(x) if _debug: bit_string_endec._debug(" - tag: %r, %r", tag, tag.tagData) obj = BitString(v) if _debug: bit_string_endec._debug(" - obj: %r, %r", obj, obj.value) assert bit_string_encode(obj) == tag assert bit_string_decode(tag) == obj
def test_boolean_application_to_object(self): if _debug: TestApplicationTag._debug("test_boolean_application_to_object") # null obj_endec(Null(), '00') # boolean obj_endec(Boolean(True), '11') obj_endec(Boolean(False), '10') # unsigned obj_endec(Unsigned(0), '2100') obj_endec(Unsigned(1), '2101') obj_endec(Unsigned(127), '217F') obj_endec(Unsigned(128), '2180') # integer obj_endec(Integer(0), '3100') obj_endec(Integer(1), '3101') obj_endec(Integer(-1), '31FF') obj_endec(Integer(128), '320080') obj_endec(Integer(-128), '3180') # real obj_endec(Real(0), '4400000000') obj_endec(Real(1), '443F800000') obj_endec(Real(-1), '44BF800000') obj_endec(Real(73.5), '4442930000') # double obj_endec(Double(0), '55080000000000000000') obj_endec(Double(1), '55083FF0000000000000') obj_endec(Double(-1), '5508BFF0000000000000') obj_endec(Double(73.5), '55084052600000000000') # octet string obj_endec(OctetString(xtob('')), '60') obj_endec(OctetString(xtob('01')), '6101') obj_endec(OctetString(xtob('0102')), '620102') obj_endec(OctetString(xtob('010203040506')), '6506010203040506') # character string obj_endec(CharacterString(''), '7100') obj_endec(CharacterString('a'), '720061') obj_endec(CharacterString('abcde'), '7506006162636465') # bit string obj_endec(BitString([]), '8100') obj_endec(BitString([0]), '820700') obj_endec(BitString([1]), '820780') obj_endec(BitString([1, 1, 1, 1, 1]), '8203F8') obj_endec(BitString([1] * 10), '8306FFC0') # enumerated obj_endec(Enumerated(0), '9100') obj_endec(Enumerated(1), '9101') obj_endec(Enumerated(127), '917F') obj_endec(Enumerated(128), '9180') # date obj_endec(Date((1, 2, 3, 4)), 'A401020304') obj_endec(Date((255, 2, 3, 4)), 'A4FF020304') obj_endec(Date((1, 255, 3, 4)), 'A401FF0304') obj_endec(Date((1, 2, 255, 4)), 'A40102FF04') obj_endec(Date((1, 2, 3, 255)), 'A4010203FF') # time obj_endec(Time((1, 2, 3, 4)), 'B401020304') obj_endec(Time((255, 2, 3, 4)), 'B4FF020304') obj_endec(Time((1, 255, 3, 4)), 'B401FF0304') obj_endec(Time((1, 2, 255, 4)), 'B40102FF04') obj_endec(Time((1, 2, 3, 255)), 'B4010203FF') # object identifier obj_endec(ObjectIdentifier(0, 0), 'C400000000') obj_endec(ObjectIdentifier(1, 0), 'C400400000') obj_endec(ObjectIdentifier(0, 2), 'C400000002') obj_endec(ObjectIdentifier(3, 4), 'C400C00004')
def test_bit_string_statement(self): if _debug: TestExtendedTagStatements._debug("test_bit_string_statement") assert statement_to_tag("bit string 101") == tag_encode(BitString([1, 0, 1])) assert statement_to_tag("bit string 10111 context 9") == tag_encode(BitString([1, 0, 1, 1, 1]), context=9)
def bit_string_statement(value): if _debug: bit_string_statement._debug("bit_string_statement %r", value) return BitString([int(c) for c in value])