示例#1
0
    def test_get_context(self):
        """Test extracting specific context encoded content.
        """
        if _debug: TestTagList._debug("test_get_context")

        tag_list_data = [
            ContextTag(0, xtob('00')),
            ContextTag(1, xtob('01')),
            OpeningTag(2),
            IntegerTag(3),
            OpeningTag(0),
            IntegerTag(4),
            ClosingTag(0),
            ClosingTag(2),
        ]
        taglist = TagList(tag_list_data)

        # known to be a simple context encoded element
        context_0 = taglist.get_context(0)
        if _debug: TestTagList._debug("    - context_0: %r", context_0)
        assert context_0 == tag_list_data[0]

        # known to be a simple context encoded list of element(s)
        context_2 = taglist.get_context(2)
        if _debug: TestTagList._debug("    - context_2: %r", context_2)
        assert context_2.tagList == tag_list_data[3:7]

        # known missing context
        context_3 = taglist.get_context(3)
        if _debug: TestTagList._debug("    - context_3: %r", context_3)
        assert taglist.get_context(3) is None
    def test_octet_string_copy(self):
        if _debug:
            TestOctetString._debug("test_octet_string_copy")

        obj1 = OctetString(xtob("01"))
        obj2 = OctetString(obj1)
        assert obj2.value == xtob("01")
示例#3
0
    def test_octet_string_octet_string(self):
        if _debug: TestOctetString._debug("test_octet_string_octet_string")

        obj = OctetString(xtob('01'))
        assert obj.value == xtob('01')
        assert str(obj) == "OctetString(X'01')"

        obj = OctetString(xtob('01020304'))
        assert obj.value == xtob('01020304')
        assert str(obj) == "OctetString(X'01020304')"
示例#4
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]
示例#5
0
    def test_remote_station_bytes(self):
        if _debug: TestRemoteStation._debug("test_remote_station_bytes")

        # multi-byte strings are hex encoded
        test_addr = RemoteStation(1, xtob('0102'))
        self.match_address(test_addr, 4, 1, 2, '0102')
        assert str(test_addr) == "1:0x0102"

        test_addr = RemoteStation(1, xtob('010203'))
        self.match_address(test_addr, 4, 1, 3, '010203')
        assert str(test_addr) == "1:0x010203"

        # match with an IPv4 address
        test_addr = RemoteStation(1, xtob('01020304bac0'))
        self.match_address(test_addr, 4, 1, 6, '01020304bac0')
        assert str(test_addr) == "1:1.2.3.4"
示例#6
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)
示例#7
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
示例#8
0
    def test_octet_string(self):
        if _debug: TestOctetString._debug("test_octet_string")

        obj = OctetString()
        assert obj.value == xtob('')

        with self.assertRaises(TypeError):
            OctetString(1)
示例#9
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
示例#10
0
    def test_application_tag(self):
        if _debug: TestApplicationTag._debug("test_application_tag")

        # application tag construction, encoding, and decoding
        tag = ApplicationTag(0, xtob(''))
        if _debug: TestApplicationTag._debug("    - tag: %r", tag_tuple(tag))

        with self.assertRaises(ValueError):
            tag = ApplicationTag(0)
示例#11
0
    def test_real_tag(self):
        if _debug: TestReal._debug("test_real_tag")

        tag = Tag(Tag.applicationTagClass, Tag.realAppTag, 1, xtob('3f800000'))
        obj = Real(tag)
        assert obj.value == 1.0

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

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            Real(tag)
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
示例#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(ValueError):
            Boolean(tag)

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(ValueError):
            Boolean(tag)
    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(ValueError):
            ObjectIdentifier(tag)

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(ValueError):
            ObjectIdentifier(tag)
示例#15
0
    def test_date_tag(self):
        if _debug: TestInteger._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(ValueError):
            Date(tag)

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(ValueError):
            Date(tag)
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
示例#17
0
def real_tag(x):
    """Convert a hex string to an real application tag."""
    if _debug: real_tag._debug("real_tag %r", x)

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

    return tag
示例#18
0
    def test_time_tag(self):
        if _debug: TestTime._debug("test_time_tag")

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

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

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            Time(tag)
示例#19
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
示例#20
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)
示例#21
0
    def test_enumerated_tag(self):
        if _debug: TestEnumerated._debug("test_enumerated_tag")

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

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

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            Enumerated(tag)
示例#22
0
def enumerated_tag(x):
    """Convert a hex string to an enumerated application tag."""
    if _debug: enumerated_tag._debug("enumerated_tag %r", x)

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

    return tag
示例#23
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
示例#24
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(ValueError):
            Unsigned(tag)

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(ValueError):
            Unsigned(tag)
示例#25
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)
示例#26
0
    def test_integer_tag(self):
        if _debug: TestInteger._debug("test_integer_tag")

        tag = Tag(Tag.applicationTagClass, Tag.integerAppTag, 1, xtob('01'))
        obj = Integer(tag)
        assert obj.value == 1

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

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            Integer(tag)
示例#27
0
def integer_tag(x):
    """Convert a hex string to an integer application tag."""
    if _debug: integer_tag._debug("integer_tag %r", x)

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

    return tag
示例#28
0
    def test_octet_string_tag(self):
        if _debug: TestOctetString._debug("test_octet_string_tag")

        tag = Tag(Tag.applicationTagClass, Tag.octetStringAppTag, 1, xtob('00'))
        obj = OctetString(tag)
        assert obj.value == xtob('00')

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

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(InvalidTag):
            OctetString(tag)
    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(ValueError):
            CharacterString(tag)

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

        tag = Tag(Tag.openingTagClass, 0)
        with self.assertRaises(ValueError):
            CharacterString(tag)
示例#30
0
    def test_local_station(self):
        if _debug: TestLocalStation._debug("test_local_station")

        # one parameter
        with self.assertRaises(TypeError):
            LocalStation()

        # test integer
        test_addr = LocalStation(1)
        self.match_address(test_addr, 2, None, 1, '01')
        assert str(test_addr) == "1"

        test_addr = LocalStation(254)
        self.match_address(test_addr, 2, None, 1, 'fe')
        assert str(test_addr) == "254"

        # test bad integer
        with self.assertRaises(ValueError):
            LocalStation(-1)
        with self.assertRaises(ValueError):
            LocalStation(256)

        # test bytes
        test_addr = LocalStation(xtob('01'))
        self.match_address(test_addr, 2, None, 1, '01')
        assert str(test_addr) == "1"

        test_addr = LocalStation(xtob('fe'))
        self.match_address(test_addr, 2, None, 1, 'fe')
        assert str(test_addr) == "254"

        # multi-byte strings are hex encoded
        test_addr = LocalStation(xtob('0102'))
        self.match_address(test_addr, 2, None, 2, '0102')
        assert str(test_addr) == "0x0102"

        test_addr = LocalStation(xtob('010203'))
        self.match_address(test_addr, 2, None, 3, '010203')
        assert str(test_addr) == "0x010203"

        # match with an IPv4 address
        test_addr = LocalStation(xtob('01020304bac0'))
        self.match_address(test_addr, 2, None, 6, '01020304bac0')
        assert str(test_addr) == "1.2.3.4"