예제 #1
0
def context_encode(tag):
    """Encode the object into a tag, encode it in a PDU, return the data."""
    if _debug: context_encode._debug("context_encode %r", tag)

    data = PDUData()
    tag.encode(data)
    return data.pduData
예제 #2
0
    def test_tag(self):
        if _debug: TestTag._debug("test_tag")

        # test tag construction
        tag = Tag()
        assert (tag.tagClass, tag.tagNumber) == (None, None)

        # must have a valid encoded tag to extract from the data
        data = PDUData(xtob(''))
        with self.assertRaises(DecodingError):
            tag = Tag(data)

        # must have two values, class and number
        with self.assertRaises(ValueError):
            tag = Tag(0)

        tag = Tag(0, 1)
        assert (tag.tagClass, tag.tagNumber) == (0, 1)

        tag = Tag(0, 1, 2)
        assert (tag.tagClass, tag.tagNumber, tag.tagLVT) == (0, 1, 2)

        # tag data must be bytes or bytearray
        with self.assertRaises(TypeError):
            tag = Tag(0, 1, 2, 3)
예제 #3
0
def context_decode(blob):
    """Build PDU from the string, decode the tag, convert to an object."""
    if _debug: context_decode._debug("context_decode %r", blob)

    data = PDUData(blob)
    tag = ContextTag(data)
    return tag
예제 #4
0
def opening_decode(blob):
    """Build PDU from the string, decode the tag, convert to an object."""
    if _debug: opening_decode._debug("opening_decode %r", blob)

    data = PDUData(blob)
    tag = OpeningTag(data)
    return tag
예제 #5
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
예제 #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
예제 #7
0
    def test_endec_0(self):
        """Test empty tag list encoding and decoding.
        """
        if _debug: TestTagList._debug("test_endec_0")

        taglist = TagList([])

        data = PDUData()
        taglist.encode(data)
        assert data.pduData == xtob('')

        taglist = TagList()
        taglist.decode(data)
        assert taglist.tagList == []
예제 #8
0
    def test_endec_3(self):
        """Test bracketed application tagged integer encoding and decoding."""
        if _debug: TestTagList._debug("test_endec_2")

        tag0 = OpeningTag(0)
        tag1 = IntegerTag(0x0102)
        tag2 = ClosingTag(0)
        taglist = TagList([tag0, tag1, tag2])

        data = PDUData()
        taglist.encode(data)
        assert data.pduData == xtob('0E3201020F')

        taglist = TagList()
        taglist.decode(data)
        assert taglist.tagList == [tag0, tag1, tag2]
예제 #9
0
    def test_endec_2(self):
        """Test short tag list encoding and decoding, context tags.
        """
        if _debug: TestTagList._debug("test_endec_2")

        tag0 = ContextTag(0, xtob('00'))
        tag1 = ContextTag(1, xtob('01'))
        taglist = TagList([tag0, tag1])

        data = PDUData()
        taglist.encode(data)
        assert data.pduData == xtob('09001901')

        taglist = TagList()
        taglist.decode(data)
        assert taglist.tagList == [tag0, tag1]
예제 #10
0
    def test_endec_1(self):
        """Test short tag list encoding and decoding, application tags.
        """
        if _debug: TestTagList._debug("test_endec_1")

        tag0 = IntegerTag(0x00)
        tag1 = IntegerTag(0x01)
        taglist = TagList([tag0, tag1])

        data = PDUData()
        taglist.encode(data)
        assert data.pduData == xtob('31003101')

        taglist = TagList()
        taglist.decode(data)
        assert taglist.tagList == [tag0, tag1]