Ejemplo n.º 1
0
    def test_boolean_application_to_context(self):
        if _debug:
            TestApplicationTag._debug("test_boolean_application_to_context")

        # create an application
        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0)
        if _debug: TestApplicationTag._debug("    - tag: %r", tag_tuple(tag))

        # convert it to context tagged, context 0
        ctag = tag.app_to_context(0)
        if _debug: TestApplicationTag._debug("    - ctag: %r", tag_tuple(ctag))

        # create a context tag with the same shape
        ttag = ContextTag(0, xtob('00'))
        if _debug: TestApplicationTag._debug("    - ttag: %r", tag_tuple(ttag))

        # check to see they are the same
        assert ctag == ttag

        # convert the context tag back to an application tag
        dtag = ctag.context_to_app(Tag.booleanAppTag)
        if _debug: TestApplicationTag._debug("    - dtag: %r", tag_tuple(dtag))

        # check to see it round-tripped
        assert dtag == tag
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
def obj_decode(blob):
    """Build PDU from the string, decode the tag, convert to an object."""
    if _debug: obj_decode._debug("obj_decode %r", blob)

    data = PDUData(blob)
    tag = Tag(data)
    obj = tag.app_to_object()
    return obj
Ejemplo n.º 4
0
def obj_decode(blob):
    """Build PDU from the string, decode the tag, convert to an object."""
    if _debug: obj_decode._debug("obj_decode %r", blob)

    data = PDUData(blob)
    tag = Tag(data)
    obj = tag.app_to_object()
    return obj
Ejemplo n.º 5
0
def obj_encode(obj):
    """Encode the object into a tag, encode it in a PDU, return the data."""
    if _debug: obj_encode._debug("obj_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    data = PDUData()
    tag.encode(data)
    return data.pduData
Ejemplo n.º 6
0
def obj_encode(obj):
    """Encode the object into a tag, encode it in a PDU, return the data."""
    if _debug: obj_encode._debug("obj_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    data = PDUData()
    tag.encode(data)
    return data.pduData
Ejemplo n.º 7
0
def tag_encode(obj, context=None):
    """Encode an atomic object into a tag."""
    if _debug: tag_encode._debug("tag_encode %r", obj)

    # encode it normally
    tag = Tag()
    obj.encode(tag)

    # check for context encoding
    if context is not None:
        tag = tag.app_to_context(context)

    if _debug: tag_encode._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 8
0
def IntegerTag(v):
    """Return an application encoded integer tag with the appropriate value.
    """
    obj = Integer(v)
    tag = Tag()
    obj.encode(tag)
    return tag
Ejemplo n.º 9
0
def boolean_tag(value):
    """Convert an integer to an boolean application tag."""
    if _debug: boolean_tag._debug("boolean_tag %r", value)

    tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, int(value), xtob(''))
    if _debug: boolean_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 10
0
    def test_character_string_tag(self):
        if _debug: TestCharacterString._debug("test_character_string_tag")

        tag = Tag(Tag.applicationTagClass, Tag.characterStringAppTag, 1, xtob('00'))
        obj = CharacterString(tag)
        assert obj.value == ''

        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
        with self.assertRaises(InvalidTag):
            CharacterString(tag)

        tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
        with self.assertRaises(InvalidTag):
            CharacterString(tag)

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            CharacterString(tag)
Ejemplo n.º 11
0
def tag(tag_class, tag_number, x):
    """Create a tag object with the given class, number, and data."""
    if _debug: tag._debug("tag %r %r %r", tag_class, tag_number, x)

    b = xtob(x)
    tag = Tag(tag_class, tag_number, len(b), b)
    if _debug: tag_tuple._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 12
0
def character_string_tag(x):
    """Convert a hex string to an character_string application tag."""
    if _debug: character_string_tag._debug("character_string_tag %r", x)

    b = xtob(x)
    tag = Tag(Tag.applicationTagClass, Tag.characterStringAppTag, len(b), b)
    if _debug: character_string_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 13
0
    def test_boolean_tag(self):
        if _debug: TestBoolean._debug("test_boolean_tag")

        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 1, xtob('01'))
        obj = Boolean(tag)
        assert obj.value == 1

        tag = Tag(Tag.applicationTagClass, Tag.integerAppTag, 0, xtob(''))
        with self.assertRaises(InvalidTag):
            Boolean(tag)

        tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
        with self.assertRaises(InvalidTag):
            Boolean(tag)

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            Boolean(tag)
Ejemplo n.º 14
0
def boolean_encode(obj):
    """Encode an Boolean object into a tag."""
    if _debug: boolean_encode._debug("boolean_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    if _debug: boolean_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 15
0
def octet_string_encode(obj):
    """Encode an OctetString object into a tag."""
    if _debug: octet_string_encode._debug("octet_string_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    if _debug: octet_string_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 16
0
    def test_date_tag(self):
        if _debug: TestDate._debug("test_date_tag")

        tag = Tag(Tag.applicationTagClass, Tag.dateAppTag, 4, xtob('01020304'))
        obj = Date(tag)
        assert obj.value == (1, 2, 3, 4)

        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
        with self.assertRaises(InvalidTag):
            Date(tag)

        tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
        with self.assertRaises(InvalidTag):
            Date(tag)

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            Date(tag)
Ejemplo n.º 17
0
def unsigned_encode(obj):
    """Encode an Unsigned object into a tag."""
    if _debug: unsigned_encode._debug("unsigned_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    if _debug: unsigned_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 18
0
def object_identifier_encode(obj):
    """Encode an ObjectIdentifier object into a tag."""
    if _debug: object_identifier_encode._debug("object_identifier_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    if _debug: object_identifier_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 19
0
    def test_unsigned_tag(self):
        if _debug: TestUnsigned._debug("test_unsigned_tag")

        tag = Tag(Tag.applicationTagClass, Tag.unsignedAppTag, 1, xtob('01'))
        obj = Unsigned(tag)
        assert obj.value == 1

        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
        with self.assertRaises(InvalidTag):
            Unsigned(tag)

        tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
        with self.assertRaises(InvalidTag):
            Unsigned(tag)

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            Unsigned(tag)
Ejemplo n.º 20
0
    def test_object_identifier_tag(self):
        if _debug: TestObjectIdentifier._debug("test_object_identifier_tag")

        tag = Tag(Tag.applicationTagClass, Tag.objectIdentifierAppTag, 1, xtob('06000003'))
        obj = ObjectIdentifier(tag)
        assert obj.value == ('pulseConverter', 3)

        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
        with self.assertRaises(InvalidTag):
            ObjectIdentifier(tag)

        tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
        with self.assertRaises(InvalidTag):
            ObjectIdentifier(tag)

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            ObjectIdentifier(tag)
Ejemplo n.º 21
0
def object_identifier_tag(x):
    """Convert a hex string to an object_identifier application tag."""
    if _debug: object_identifier_tag._debug("object_identifier_tag %r", x)

    b = xtob(x)
    tag = Tag(Tag.applicationTagClass, Tag.objectIdentifierAppTag, len(b), b)
    if _debug: object_identifier_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 22
0
def object_type_encode(obj):
    """Encode an ObjectType object into a tag."""
    if _debug: object_type_encode._debug("object_type_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    if _debug: object_type_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 23
0
def enumerated_encode(obj):
    """Encode an Enumerated object into a tag."""
    if _debug: enumerated_encode._debug("enumerated_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    if _debug: enumerated_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 24
0
    def test_object_type_tag(self):
        if _debug: TestObjectType._debug("test_object_type_tag")

        tag = Tag(Tag.applicationTagClass, Tag.enumeratedAppTag, 1, xtob('01'))
        obj = ObjectType(tag)
        assert obj.value == 'analogOutput'

        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
        with self.assertRaises(InvalidTag):
            ObjectType(tag)

        tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
        with self.assertRaises(InvalidTag):
            ObjectType(tag)

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            ObjectType(tag)
Ejemplo n.º 25
0
def null_tag(x):
    """Convert a hex string to an integer application tag."""
    if _debug: null_tag._debug("null_tag %r", x)

    b = xtob(x)
    tag = Tag(Tag.applicationTagClass, Tag.nullAppTag, len(b), b)
    if _debug: integer_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 26
0
def double_tag(x):
    """Convert a hex string to an double application tag."""
    if _debug: double_tag._debug("double_tag %r", x)

    b = xtob(x)
    tag = Tag(Tag.applicationTagClass, Tag.doubleAppTag, len(b), b)
    if _debug: double_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 27
0
def double_encode(obj):
    """Encode an Double object into a tag."""
    if _debug: double_encode._debug("double_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    if _debug: double_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 28
0
    def test_double_tag(self):
        if _debug: TestDouble._debug("test_double_tag")

        tag = Tag(Tag.applicationTagClass, Tag.doubleAppTag, 8, xtob('3ff0000000000000'))
        obj = Double(tag)
        assert obj.value == 1.0

        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
        with self.assertRaises(InvalidTag):
            Double(tag)

        tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
        with self.assertRaises(InvalidTag):
            Double(tag)

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            Double(tag)
Ejemplo n.º 29
0
def octet_string_tag(x):
    """Convert a hex string to an octet_string application tag."""
    if _debug: octet_string_tag._debug("octet_string_tag %r", x)

    b = xtob(x)
    tag = Tag(Tag.applicationTagClass, Tag.octetStringAppTag, len(b), b)
    if _debug: octet_string_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 30
0
def null_encode(obj):
    """Encode an Integer object into a tag."""
    if _debug: null_encode._debug("null_encode %r", obj)

    tag = Tag()
    obj.encode(tag)
    if _debug: null_encode._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 31
0
def unsigned_tag(x):
    """Convert a hex string to an unsigned application tag."""
    if _debug: unsigned_tag._debug("unsigned_tag %r", x)

    b = xtob(x)
    tag = Tag(Tag.applicationTagClass, Tag.unsignedAppTag, len(b), b)
    if _debug: unsigned_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 32
0
def object_type_tag(x):
    """Convert a hex string to an enumerated application tag."""
    if _debug: object_type_tag._debug("object_type_tag %r", x)

    b = xtob(x)
    tag = Tag(Tag.applicationTagClass, Tag.enumeratedAppTag, len(b), b)
    if _debug: object_type_endec._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 33
0
def statement_to_tag(line):
    """Parse a line of text and return the appropriate tag."""
    if _debug: statement_to_tag._debug("statement_to_tag %r", line)

    # look for a matching statement pattern
    for stmt_fn, stmt_re in statements:
        match = stmt_re.match(line)
        if match:
            break
    else:
        raise SyntaxError("syntax error: %r" % (line,))

    # extract the pieces captured by the pattern
    match_groups = match.groupdict()
    value = match_groups.get('value', None)
    context = match_groups.get('context', None)
    if _debug: statement_to_tag._debug("    - value: %r", value)
    if _debug: statement_to_tag._debug("    - context: %r", context)

    # let the function work on the value, skip blank lines
    element = stmt_fn(value)
    if not element:
        return None

    # check for element already a tag
    if isinstance(element, Tag):
        tag = element
        if context is not None:
            raise SyntaxError("syntax error: %r" % (line,))

    elif isinstance(element, Atomic):
        tag = Tag()
        element.encode(tag)
        if _debug: statement_to_tag._debug("    - encoded tag: %r", tag)

        if context is not None:
            tag = tag.app_to_context(int(context))
            if _debug: statement_to_tag._debug("    - with context: %r", tag)

    else:
        raise TypeError("element must be a tag or atomic")
    if _debug: statement_to_tag._debug("    - tag: %r", tag)

    return tag
Ejemplo n.º 34
0
    def test_boolean_application_to_context(self):
        if _debug: TestApplicationTag._debug("test_boolean_application_to_context")

        # create an application
        tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0)
        if _debug: TestApplicationTag._debug("    - tag: %r", tag_tuple(tag))

        # convert it to context tagged, context 0
        ctag = tag.app_to_context(0)
        if _debug: TestApplicationTag._debug("    - ctag: %r", tag_tuple(ctag))

        # create a context tag with the same shape
        ttag = ContextTag(0, xtob('00'))
        if _debug: TestApplicationTag._debug("    - ttag: %r", tag_tuple(ttag))

        # check to see they are the same
        assert ctag == ttag

        # convert the context tag back to an application tag
        dtag = ctag.context_to_app(Tag.booleanAppTag)
        if _debug: TestApplicationTag._debug("    - dtag: %r", tag_tuple(dtag))

        # check to see it round-tripped
        assert dtag == tag