-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_serializer.py
More file actions
121 lines (105 loc) · 3.83 KB
/
test_serializer.py
File metadata and controls
121 lines (105 loc) · 3.83 KB
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
"""Tests for Serializer."""
from pyipp import serializer
from pyipp import parser
from pyipp.const import DEFAULT_CHARSET, DEFAULT_CHARSET_LANGUAGE, DEFAULT_PROTO_VERSION
from pyipp.enums import IppOperation, IppTag
from . import load_fixture_binary
def test_construct_attribute_values() -> None:
"""Test the construct_attribute_values method."""
result = serializer.construct_attribute_values(
IppTag.INTEGER,
IppOperation.GET_PRINTER_ATTRIBUTES,
)
assert result == b"\x00\x04\x00\x00\x00\x0b"
result = serializer.construct_attribute_values(
IppTag.ENUM,
IppOperation.GET_PRINTER_ATTRIBUTES,
)
assert result == b"\x00\x04\x00\x00\x00\x0b"
result = serializer.construct_attribute_values(
IppTag.BOOLEAN,
"0",
)
assert result == b"\x00\x01\x01"
result = serializer.construct_attribute_values(
IppTag.URI,
"ipps://localhost:631",
)
assert result == b"\x00\x14ipps://localhost:631"
def test_construct_attribute() -> None:
"""Test the construct_attribute method."""
result = serializer.construct_attribute("attributes-charset", DEFAULT_CHARSET)
assert result == b"G\x00\x12attributes-charset\x00\x05utf-8"
result = serializer.construct_attribute(
"operations-supported",
[IppOperation.GET_PRINTER_ATTRIBUTES],
)
assert result == b"#\x00\x14operations-supported\x00\x04\x00\x00\x00\x0b"
def test_construct_attribute_no_tag_unmapped() -> None:
"""Test the construct_attribute method with no tag and unmapped attribute name."""
result = serializer.construct_attribute(
"no-tag-unmapped",
None,
)
assert result == b""
def test_encode_dict() -> None:
"""Test the encode_dict method."""
result = serializer.encode_dict(
{
"version": DEFAULT_PROTO_VERSION,
"operation": IppOperation.GET_PRINTER_ATTRIBUTES,
"request-id": 1,
"operation-attributes-tag": {
"attributes-charset": DEFAULT_CHARSET,
"attributes-natural-language": DEFAULT_CHARSET_LANGUAGE,
"printer-uri": "ipp://printer.example.com:361/ipp/print",
"requesting-user-name": "PythonIPP",
},
},
)
assert result == load_fixture_binary(
"serializer/get-printer-attributes-request-000.bin",
)
def test_encode_collections() -> None:
"""Test encoding collections."""
message = {
"version": DEFAULT_PROTO_VERSION,
"operation": IppOperation.VALIDATE_JOB,
"request-id": 1,
"operation-attributes-tag": {
"attributes-charset": DEFAULT_CHARSET,
"attributes-natural-language": DEFAULT_CHARSET_LANGUAGE,
"requesting-user-name": "PythonIPP",
"printer-uri": "ipp://printer.example.com:361/ipp/print",
},
"job-attributes-tag": {
"media-col": {
"media-bottom-margin": 0,
"media-left-margin": 0,
"media-right-margin": 0,
"media-size": {
"x-dimension": 10000,
"y-dimension": 14800,
},
"media-source": "photo",
"media-top-margin": 0,
"media-type": "photographic",
},
},
}
encoded = serializer.encode_dict(message)
parsed = parser.parse(encoded)
assert parsed["jobs"] == [
{
"ipp-attribute-fidelity": True,
"media-col": {
"media-bottom-margin": 0,
"media-left-margin": 0,
"media-right-margin": 0,
"media-size": {"x-dimension": 10000, "y-dimension": 14800},
"media-source": "photo",
"media-top-margin": 0,
"media-type": "photographic",
},
},
]