File: test_utils.py

package info (click to toggle)
python-ipmi 0.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,132 kB
  • sloc: python: 12,645; makefile: 2
file content (168 lines) | stat: -rw-r--r-- 4,550 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pytest

from array import array

import pyipmi.msgs.device_messaging
from pyipmi.utils import (ByteBuffer, chunks, check_completion_code,
                          check_rsp_completion_code)
from pyipmi.errors import DecodingError, CompletionCodeError
from pyipmi.msgs import decode_message


def test_bytebuffer_init_from_list():
    buf = ByteBuffer([0xf8])
    assert buf.array == array('B', [0xf8])


def test_bytebuffer_init_from_tuple():
    buf = ByteBuffer((0xf8,))
    assert buf.array == array('B', [0xf8])


def test_bytebuffer_initi_fromstring():
    buf = ByteBuffer(b'\xf8')
    assert buf.array == array('B', [0xf8])


def test_bytebuffer_push_unsigned_int():
    buf = ByteBuffer((1, 0))
    buf.push_unsigned_int(255, 1)
    assert buf[0] == 1
    assert buf[1] == 0
    assert buf[2] == 255
    buf.push_unsigned_int(255, 2)
    assert buf[3] == 255
    assert buf[4] == 0
    buf.push_unsigned_int(256, 2)
    assert buf[5] == 0
    assert buf[6] == 1


def test_bytebuffer_pop_unsigned_int():
    buf = ByteBuffer((1, 0, 0, 0))
    assert buf.pop_unsigned_int(1) == 1

    buf = ByteBuffer((0, 1, 0, 0))
    assert buf.pop_unsigned_int(2) == 0x100

    buf = ByteBuffer((0, 0, 1, 0))
    assert buf.pop_unsigned_int(3) == 0x10000

    buf = ByteBuffer((0, 0, 0, 1))
    assert buf.pop_unsigned_int(4) == 0x1000000


def test_bytebuffer_pop_unsigned_int_error():
    with pytest.raises(DecodingError):
        buf = ByteBuffer((0, 0))
        buf.pop_unsigned_int(3)


def test_bytebuffer_push_string():
    buf = ByteBuffer()
    buf.push_string(b'0123')
    assert buf[0] == 0x30
    assert buf[1] == 0x31
    assert buf[2] == 0x32
    assert buf[3] == 0x33
    assert buf.tostring() == b'0123'

    buf = ByteBuffer()
    buf.push_string(b'\x00\xb4')
    assert buf.tostring() == b'\x00\xb4'


def test_bytebuffer_pop_string():
    buf = ByteBuffer(b'\x30\x31\x32\x33')
    assert buf.pop_string(2) == b'01'
    assert buf.tostring() == b'23'


def test_bytebuffer_tostring():
    buf = ByteBuffer(b'\x30\x31\x32\x33')
    assert buf.tostring() == b'0123'


def test_bytebuffer_pop_slice():
    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(1)
    assert buf.tostring() == b'123'
    assert cut.tostring() == b'0'

    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(2)
    assert buf.tostring() == b'23'
    assert cut.tostring() == b'01'

    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(3)
    assert buf.tostring() == b'3'
    assert cut.tostring() == b'012'

    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(4)
    assert buf.tostring() == b''
    assert cut.tostring() == b'0123'


def test_bytebuffer_pop_slice_error():
    with pytest.raises(DecodingError):
        buf = ByteBuffer(b'\x30\x31\x32\x33')
        buf.pop_slice(5)


def test_chunks():
    data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    result = list()
    for chunk in chunks(data, 2):
        result.extend(chunk)
        assert len(chunk) == 2
    assert result == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


def test_check_completion_code_ok():
    check_completion_code(0)


def test_check_specific_completion_code_ok():
    rsp = pyipmi.msgs.device_messaging.SetUserPasswordRsp()
    decode_message(rsp, b'\x00')
    check_rsp_completion_code(rsp)


def test_check_completion_code_desc():
    with pytest.raises(CompletionCodeError) as ex:
        check_completion_code(0xc1)
    assert ex.value.cc == 0xc1
    assert ex.value.cc_desc == "Invalid Command"


def test_check_completion_code_unknown_desc():
    with pytest.raises(CompletionCodeError) as ex:
        check_completion_code(0x81)
    assert ex.value.cc == 0x81
    assert ex.value.cc_desc == "Unknown error description"


def test_check_rsp_completion_code_desc():
    rsp = pyipmi.msgs.device_messaging.SetUserPasswordRsp()
    decode_message(rsp, b'\x81')
    with pytest.raises(CompletionCodeError) as ex:
        check_rsp_completion_code(rsp)
    assert rsp.cmdid == 71
    assert rsp.netfn == 6 | 1
    assert rsp.group_extension is None
    assert ex.value.cc == 0x81
    assert ex.value.cc_desc == "password test failed. Wrong password size was used."


def test_check_rsp_completion_code_unknown_desc():
    rsp = pyipmi.msgs.device_messaging.SetUserPasswordRsp()
    decode_message(rsp, b'\x42')
    with pytest.raises(CompletionCodeError) as ex:
        check_rsp_completion_code(rsp)
    assert ex.value.cc == 0x42
    assert ex.value.cc_desc == "Unknown error description"