Debian Codegraph @

test/tls/test_rdp.py

raw
1 # SPDX-License-Identifier: MPL-2.0
2
3 import unittest
4 import collections
5
6 from cryptodatahub.common.exception import InvalidValue
7 from cryptoparser.common.exception import NotEnoughData, InvalidType
8
9 from cryptoparser.tls.rdp import (
10 COTPConnectionConfirm,
11 COTPConnectionRequest,
12 RDPNegotiationRequest,
13 RDPNegotiationRequestFlags,
14 RDPNegotiationResponse,
15 RDPNegotiationResponseFlags,
16 RDPProtocol,
17 TPKT,
18 )
19
20
21 class TestTPKT(unittest.TestCase):
22 def setUp(self):
23 self.tpkt_dict = collections.OrderedDict([
24 ('version', b'\x03'),
25 ('reserved', b'\x00'),
26 ('packet_length', b'\x00\x14'),
27 ('message',
28 b'\x00\x01\x02\x03\x04\x05\x06\x07' +
29 b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' +
30 b'')
31 ])
32 self.tpkt_bytes = b''.join(self.tpkt_dict.values())
33
34 self.tpkt = TPKT(
35 version=3,
36 message=b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
37 )
38
39 def test_error(self):
40 with self.assertRaises(NotEnoughData) as context_manager:
41 TPKT.parse_exact_size(b'')
42 self.assertEqual(context_manager.exception.bytes_needed, TPKT.HEADER_SIZE)
43
44 with self.assertRaises(InvalidValue) as context_manager:
45 TPKT.parse_exact_size(b'\x01\x00\x00\x00')
46 self.assertEqual(context_manager.exception.value, 1)
47
48 with self.assertRaises(NotEnoughData) as context_manager:
49 TPKT.parse_exact_size(b'\x03\x00\x00\x05')
50 self.assertEqual(context_manager.exception.bytes_needed, 1)
51
52 def test_parse(self):
53 tpkt = TPKT.parse_exact_size(self.tpkt_bytes)
54 self.assertEqual(tpkt.version, 3)
55 self.assertEqual(tpkt.message, self.tpkt_dict['message'])
56
57 def test_compose(self):
58 self.assertEqual(self.tpkt.compose(), self.tpkt_bytes)
59
60
61 class TestCOTPConnectionRequest(unittest.TestCase):
62 def setUp(self):
63 self.cotp_connection_request_dict = collections.OrderedDict([
64 ('length', b'\x0e'),
65 ('type', b'\xe0'),
66 ('src_ref', b'\x01\x02'),
67 ('dst_ref', b'\x03\x04'),
68 ('class_option', b'\x00'),
69 ('user_data', b'\x00\x01\x02\x03\x04\x05\x06\x07'),
70 ])
71 self.cotp_connection_request_bytes = b''.join(self.cotp_connection_request_dict.values())
72
73 self.cotp_connection_request = COTPConnectionRequest(
74 src_ref=0x0102, dst_ref=0x0304, class_option=0x00,
75 user_data=b'\x00\x01\x02\x03\x04\x05\x06\x07'
76 )
77
78 def test_error(self):
79 with self.assertRaises(NotEnoughData) as context_manager:
80 COTPConnectionRequest.parse_exact_size(b'')
81 self.assertEqual(context_manager.exception.bytes_needed, COTPConnectionRequest.HEADER_SIZE)
82
83 with self.assertRaises(NotEnoughData) as context_manager:
84 COTPConnectionRequest.parse_exact_size(b'\x07\xe0\x00\x00\x00\x00\x00')
85 self.assertEqual(context_manager.exception.bytes_needed, 1)
86
87 with self.assertRaises(InvalidType) as context_manager:
88 COTPConnectionRequest.parse_exact_size(b'\x07\xf0\x00\x00\x00\x00\x00\x00')
89
90 with self.assertRaises(InvalidValue) as context_manager:
91 COTPConnectionRequest.parse_exact_size(b'\x07\xe0\x00\x00\x00\x00\x01\x00')
92 self.assertEqual(context_manager.exception.value, 1)
93
94 def test_parse(self):
95 cotp_connection_request = COTPConnectionRequest.parse_exact_size(self.cotp_connection_request_bytes)
96 self.assertEqual(cotp_connection_request.src_ref, self.cotp_connection_request.src_ref)
97 self.assertEqual(cotp_connection_request.dst_ref, self.cotp_connection_request.dst_ref)
98 self.assertEqual(cotp_connection_request.class_option, self.cotp_connection_request.class_option)
99 self.assertEqual(cotp_connection_request.user_data, self.cotp_connection_request.user_data)
100
101 def test_compose(self):
102 self.assertEqual(self.cotp_connection_request.compose(), self.cotp_connection_request_bytes)
103
104
105 class TestCOTPConnectionConfirm(unittest.TestCase):
106 def setUp(self):
107 self.cotp_connection_confirm_dict = collections.OrderedDict([
108 ('length', b'\x0e'),
109 ('type', b'\xd0'),
110 ('src_ref', b'\x01\x02'),
111 ('dst_ref', b'\x03\x04'),
112 ('class_option', b'\x00'),
113 ('user_data', b'\x00\x01\x02\x03\x04\x05\x06\x07'),
114 ])
115 self.cotp_connection_confirm_bytes = b''.join(self.cotp_connection_confirm_dict.values())
116
117 self.cotp_connection_confirm = COTPConnectionConfirm(
118 src_ref=0x0102, dst_ref=0x0304, class_option=0x00,
119 user_data=b'\x00\x01\x02\x03\x04\x05\x06\x07'
120 )
121
122 def test_parse(self):
123 cotp_connection_confirm = COTPConnectionConfirm.parse_exact_size(self.cotp_connection_confirm_bytes)
124 self.assertEqual(cotp_connection_confirm.src_ref, self.cotp_connection_confirm.src_ref)
125 self.assertEqual(cotp_connection_confirm.dst_ref, self.cotp_connection_confirm.dst_ref)
126 self.assertEqual(cotp_connection_confirm.class_option, self.cotp_connection_confirm.class_option)
127 self.assertEqual(cotp_connection_confirm.user_data, self.cotp_connection_confirm.user_data)
128
129 def test_compose(self):
130 self.assertEqual(self.cotp_connection_confirm.compose(), self.cotp_connection_confirm_bytes)
131
132
133 class TestRDPNegotiationRequest(unittest.TestCase):
134 def setUp(self):
135 self.rdp_negotiation_request_dict = collections.OrderedDict([
136 ('type', b'\x01'),
137 ('flags', b'\x03'),
138 ('length', b'\x08\x00'),
139 ('protocol', b'\x03\x00\x00\x00'),
140 ])
141 self.rdp_negotiation_request_bytes = b''.join(self.rdp_negotiation_request_dict.values())
142
143 self.rdp_negotiation_request = RDPNegotiationRequest(
144 flags={
145 RDPNegotiationRequestFlags.RESTRICTED_ADMIN_MODE_REQUIRED,
146 RDPNegotiationRequestFlags.REDIRECTED_AUTHENTICATION_MODE_REQUIRED
147 },
148 protocol={RDPProtocol.SSL, RDPProtocol.HYBRID}
149 )
150
151 def test_error(self):
152 with self.assertRaises(NotEnoughData) as context_manager:
153 RDPNegotiationRequest.parse_exact_size(b'\x01')
154 self.assertEqual(context_manager.exception.bytes_needed, RDPNegotiationRequest.PACKET_LENGTH - 1)
155
156 with self.assertRaises(InvalidType) as context_manager:
157 RDPNegotiationRequest.parse_exact_size(b'\x02\x03\x08\x00\x00\x00\x00\x00')
158
159 with self.assertRaises(NotEnoughData) as context_manager:
160 RDPNegotiationRequest.parse_exact_size(b'\x01\x03\x08\x00\x00\x00')
161 self.assertEqual(context_manager.exception.bytes_needed, 2)
162
163 with self.assertRaises(InvalidValue) as context_manager:
164 RDPNegotiationRequest.parse_exact_size(b'\x01\x03\x02\x00\x00\x00\x00\x00')
165 self.assertEqual(context_manager.exception.value, 2)
166
167 def test_parse(self):
168 rdp_negotiation_request = RDPNegotiationRequest.parse_exact_size(self.rdp_negotiation_request_bytes)
169 self.assertEqual(rdp_negotiation_request.flags, self.rdp_negotiation_request.flags)
170 self.assertEqual(rdp_negotiation_request.protocol, self.rdp_negotiation_request.protocol)
171
172 def test_compose(self):
173 self.assertEqual(self.rdp_negotiation_request.compose(), self.rdp_negotiation_request_bytes)
174
175
176 class TestRDPNegotiationResponse(unittest.TestCase):
177 def setUp(self):
178 self.rdp_negotiation_request_dict = collections.OrderedDict([
179 ('type', b'\x02'),
180 ('flags', b'\x03'),
181 ('length', b'\x08\x00'),
182 ('protocol', b'\x03\x00\x00\x00'),
183 ])
184 self.rdp_negotiation_request_bytes = b''.join(self.rdp_negotiation_request_dict.values())
185
186 self.rdp_negotiation_request = RDPNegotiationResponse(
187 flags={
188 RDPNegotiationResponseFlags.EXTENDED_CLIENT_DATA_SUPPORTED,
189 RDPNegotiationResponseFlags.DYNVC_GFX_PROTOCOL_SUPPORTED
190 },
191 protocol={RDPProtocol.SSL, RDPProtocol.HYBRID}
192 )
193
194 def test_parse(self):
195 rdp_negotiation_request = RDPNegotiationResponse.parse_exact_size(self.rdp_negotiation_request_bytes)
196 self.assertEqual(rdp_negotiation_request.flags, self.rdp_negotiation_request.flags)
197 self.assertEqual(rdp_negotiation_request.protocol, self.rdp_negotiation_request.protocol)
198
199 def test_compose(self):
200 self.assertEqual(self.rdp_negotiation_request.compose(), self.rdp_negotiation_request_bytes)