File: header_test.py

package info (click to toggle)
python-xknx 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: python: 39,710; javascript: 8,556; makefile: 27; sh: 12
file content (63 lines) | stat: -rw-r--r-- 2,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Unit test for KNX/IP TunnellingAck objects."""

import pytest

from xknx.exceptions import CouldNotParseKNXIP
from xknx.knxip import DisconnectRequest, KNXIPHeader, KNXIPServiceType


class TestKNXIPHeader:
    """Test class for KNX/IP Header objects."""

    def test_from_knx(self) -> None:
        """Test parsing and streaming wrong Header (wrong length byte)."""
        raw = bytes((0x06, 0x10, 0x04, 0x21, 0x00, 0x0A))
        header = KNXIPHeader()
        assert header.from_knx(raw) == 6
        assert header.HEADERLENGTH == 6
        assert header.PROTOCOLVERSION == 16
        assert header.service_type_ident == KNXIPServiceType.TUNNELLING_ACK
        assert header.total_length == 10
        assert header.to_knx() == raw

    def test_set_length(self) -> None:
        """Test setting length."""
        header = KNXIPHeader()
        header.set_length(DisconnectRequest())
        # 6 (header) + 2 + 8 (HPAI length)
        assert header.total_length == 16

    def test_set_length_error(self) -> None:
        """Test setting length with wrong type."""
        header = KNXIPHeader()
        with pytest.raises(TypeError):
            header.set_length(2)

    def test_from_knx_wrong_header(self) -> None:
        """Test parsing and streaming wrong Header (wrong length)."""
        raw = bytes((0x06, 0x10, 0x04, 0x21, 0x00))
        header = KNXIPHeader()
        with pytest.raises(CouldNotParseKNXIP):
            header.from_knx(raw)

    def test_from_knx_wrong_header2(self) -> None:
        """Test parsing and streaming wrong Header (wrong length byte)."""
        raw = bytes((0x05, 0x10, 0x04, 0x21, 0x00, 0x0A))
        header = KNXIPHeader()
        with pytest.raises(CouldNotParseKNXIP):
            header.from_knx(raw)

    def test_from_knx_wrong_header3(self) -> None:
        """Test parsing and streaming wrong Header (wrong protocol version)."""
        raw = bytes((0x06, 0x11, 0x04, 0x21, 0x00, 0x0A))
        header = KNXIPHeader()
        with pytest.raises(CouldNotParseKNXIP):
            header.from_knx(raw)

    def test_from_knx_wrong_header4(self) -> None:
        """Test parsing and streaming wrong Header (unsupported service type)."""
        # 0x0000 as service type
        raw = bytes((0x06, 0x10, 0x00, 0x00, 0x00, 0x0A))
        header = KNXIPHeader()
        with pytest.raises(CouldNotParseKNXIP):
            header.from_knx(raw)