xref: /aosp_15_r20/external/openthread/tests/scripts/thread-cert/test_coap.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2016, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker#
29*cfb92d14SAndroid Build Coastguard Worker
30*cfb92d14SAndroid Build Coastguard Workerimport io
31*cfb92d14SAndroid Build Coastguard Workerimport random
32*cfb92d14SAndroid Build Coastguard Workerimport string
33*cfb92d14SAndroid Build Coastguard Workerimport unittest
34*cfb92d14SAndroid Build Coastguard Worker
35*cfb92d14SAndroid Build Coastguard Workerimport coap
36*cfb92d14SAndroid Build Coastguard Worker
37*cfb92d14SAndroid Build Coastguard Worker
38*cfb92d14SAndroid Build Coastguard Workerdef any_delta():
39*cfb92d14SAndroid Build Coastguard Worker    return random.getrandbits(4)
40*cfb92d14SAndroid Build Coastguard Worker
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Workerdef any_coap_option_type():
43*cfb92d14SAndroid Build Coastguard Worker    return random.getrandbits(4)
44*cfb92d14SAndroid Build Coastguard Worker
45*cfb92d14SAndroid Build Coastguard Worker
46*cfb92d14SAndroid Build Coastguard Workerdef any_value():
47*cfb92d14SAndroid Build Coastguard Worker    return random.getrandbits(8)
48*cfb92d14SAndroid Build Coastguard Worker
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Workerdef any_4bits_value_different_than_13_and_14():
51*cfb92d14SAndroid Build Coastguard Worker    value = None
52*cfb92d14SAndroid Build Coastguard Worker    while value is None:
53*cfb92d14SAndroid Build Coastguard Worker        value = random.getrandbits(4)
54*cfb92d14SAndroid Build Coastguard Worker        if value == 13 or value == 14:
55*cfb92d14SAndroid Build Coastguard Worker            value = None
56*cfb92d14SAndroid Build Coastguard Worker
57*cfb92d14SAndroid Build Coastguard Worker    return value
58*cfb92d14SAndroid Build Coastguard Worker
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Workerdef any_4bits_value_lower_or_equal_than_12():
61*cfb92d14SAndroid Build Coastguard Worker    value = None
62*cfb92d14SAndroid Build Coastguard Worker    while value is None:
63*cfb92d14SAndroid Build Coastguard Worker        value = random.getrandbits(4)
64*cfb92d14SAndroid Build Coastguard Worker        if value > 12:
65*cfb92d14SAndroid Build Coastguard Worker            value = None
66*cfb92d14SAndroid Build Coastguard Worker
67*cfb92d14SAndroid Build Coastguard Worker    return value
68*cfb92d14SAndroid Build Coastguard Worker
69*cfb92d14SAndroid Build Coastguard Worker
70*cfb92d14SAndroid Build Coastguard Workerdef any_bytearray(length):
71*cfb92d14SAndroid Build Coastguard Worker    return bytearray([random.getrandbits(8) for _ in range(length)])
72*cfb92d14SAndroid Build Coastguard Worker
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Workerdef any_version():
75*cfb92d14SAndroid Build Coastguard Worker    return random.getrandbits(2)
76*cfb92d14SAndroid Build Coastguard Worker
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Workerdef any_type():
79*cfb92d14SAndroid Build Coastguard Worker    return random.getrandbits(2)
80*cfb92d14SAndroid Build Coastguard Worker
81*cfb92d14SAndroid Build Coastguard Worker
82*cfb92d14SAndroid Build Coastguard Workerdef any_code():
83*cfb92d14SAndroid Build Coastguard Worker    return random.getrandbits(8)
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Worker
86*cfb92d14SAndroid Build Coastguard Workerdef any_message_id():
87*cfb92d14SAndroid Build Coastguard Worker    return random.getrandbits(16)
88*cfb92d14SAndroid Build Coastguard Worker
89*cfb92d14SAndroid Build Coastguard Worker
90*cfb92d14SAndroid Build Coastguard Workerdef any_token():
91*cfb92d14SAndroid Build Coastguard Worker    length = random.randint(0, 8)
92*cfb92d14SAndroid Build Coastguard Worker    return bytearray([random.getrandbits(8) for _ in range(length)])
93*cfb92d14SAndroid Build Coastguard Worker
94*cfb92d14SAndroid Build Coastguard Worker
95*cfb92d14SAndroid Build Coastguard Workerdef any_options():
96*cfb92d14SAndroid Build Coastguard Worker    return []
97*cfb92d14SAndroid Build Coastguard Worker
98*cfb92d14SAndroid Build Coastguard Worker
99*cfb92d14SAndroid Build Coastguard Workerdef any_payload(length=None):
100*cfb92d14SAndroid Build Coastguard Worker    length = length if length is not None else random.randint(0, 64)
101*cfb92d14SAndroid Build Coastguard Worker    return bytearray([random.getrandbits(8) for _ in range(length)])
102*cfb92d14SAndroid Build Coastguard Worker
103*cfb92d14SAndroid Build Coastguard Worker
104*cfb92d14SAndroid Build Coastguard Workerdef any_uri_path():
105*cfb92d14SAndroid Build Coastguard Worker    return "/" + random.choice(string.ascii_lowercase)
106*cfb92d14SAndroid Build Coastguard Worker
107*cfb92d14SAndroid Build Coastguard Worker
108*cfb92d14SAndroid Build Coastguard Workerclass TestCoapMessageOptionHeader(unittest.TestCase):
109*cfb92d14SAndroid Build Coastguard Worker
110*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_passed_on_value_when_read_extended_value_is_called_with_value_different_than_13_and_14(
111*cfb92d14SAndroid Build Coastguard Worker            self):
112*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
113*cfb92d14SAndroid Build Coastguard Worker        value = any_4bits_value_different_than_13_and_14()
114*cfb92d14SAndroid Build Coastguard Worker
115*cfb92d14SAndroid Build Coastguard Worker        # WHEN
116*cfb92d14SAndroid Build Coastguard Worker        actual_value = coap.CoapOptionHeader._read_extended_value(None, value)
117*cfb92d14SAndroid Build Coastguard Worker
118*cfb92d14SAndroid Build Coastguard Worker        # THEN
119*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(value, actual_value)
120*cfb92d14SAndroid Build Coastguard Worker
121*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_value_stored_in_first_byte_plus_13_when_read_extended_value_is_called_with_value_equal_13(
122*cfb92d14SAndroid Build Coastguard Worker            self):
123*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
124*cfb92d14SAndroid Build Coastguard Worker        value = 13
125*cfb92d14SAndroid Build Coastguard Worker        extended_value = any_value()
126*cfb92d14SAndroid Build Coastguard Worker
127*cfb92d14SAndroid Build Coastguard Worker        data = io.BytesIO(bytearray([extended_value]))
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Worker        # WHEN
130*cfb92d14SAndroid Build Coastguard Worker        actual_value = coap.CoapOptionHeader._read_extended_value(data, value)
131*cfb92d14SAndroid Build Coastguard Worker
132*cfb92d14SAndroid Build Coastguard Worker        # THEN
133*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(extended_value + 13, actual_value)
134*cfb92d14SAndroid Build Coastguard Worker
135*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_value_stored_in_first_byte_plus_269_when_read_extended_value_is_called_with_value_equal_14(
136*cfb92d14SAndroid Build Coastguard Worker            self):
137*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
138*cfb92d14SAndroid Build Coastguard Worker        value = 14
139*cfb92d14SAndroid Build Coastguard Worker        extended_value = any_value()
140*cfb92d14SAndroid Build Coastguard Worker
141*cfb92d14SAndroid Build Coastguard Worker        data = io.BytesIO(bytearray([any_value(), extended_value]))
142*cfb92d14SAndroid Build Coastguard Worker
143*cfb92d14SAndroid Build Coastguard Worker        # WHEN
144*cfb92d14SAndroid Build Coastguard Worker        actual_value = coap.CoapOptionHeader._read_extended_value(data, value)
145*cfb92d14SAndroid Build Coastguard Worker
146*cfb92d14SAndroid Build Coastguard Worker        # THEN
147*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(extended_value + 269, actual_value)
148*cfb92d14SAndroid Build Coastguard Worker
149*cfb92d14SAndroid Build Coastguard Worker    def test_should_create_CoapOptionHeader_when_from_bytes_classmethod_is_called(self):
150*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
151*cfb92d14SAndroid Build Coastguard Worker        delta = any_4bits_value_different_than_13_and_14()
152*cfb92d14SAndroid Build Coastguard Worker        length = any_4bits_value_different_than_13_and_14()
153*cfb92d14SAndroid Build Coastguard Worker
154*cfb92d14SAndroid Build Coastguard Worker        data = bytearray([delta << 4 | length])
155*cfb92d14SAndroid Build Coastguard Worker
156*cfb92d14SAndroid Build Coastguard Worker        # WHEN
157*cfb92d14SAndroid Build Coastguard Worker        option_header = coap.CoapOptionHeader.from_bytes(io.BytesIO(data))
158*cfb92d14SAndroid Build Coastguard Worker
159*cfb92d14SAndroid Build Coastguard Worker        # THEN
160*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(delta, option_header.delta)
161*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(length, option_header.length)
162*cfb92d14SAndroid Build Coastguard Worker
163*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_True_when_is_payload_marker_property_called_with_delta_and_length_equal_15(self):
164*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
165*cfb92d14SAndroid Build Coastguard Worker        delta = 15
166*cfb92d14SAndroid Build Coastguard Worker        length = 15
167*cfb92d14SAndroid Build Coastguard Worker
168*cfb92d14SAndroid Build Coastguard Worker        data = bytearray([delta << 4 | length])
169*cfb92d14SAndroid Build Coastguard Worker
170*cfb92d14SAndroid Build Coastguard Worker        # WHEN
171*cfb92d14SAndroid Build Coastguard Worker        option_header = coap.CoapOptionHeader.from_bytes(io.BytesIO(data))
172*cfb92d14SAndroid Build Coastguard Worker
173*cfb92d14SAndroid Build Coastguard Worker        # THEN
174*cfb92d14SAndroid Build Coastguard Worker        self.assertTrue(option_header.is_payload_marker)
175*cfb92d14SAndroid Build Coastguard Worker
176*cfb92d14SAndroid Build Coastguard Worker
177*cfb92d14SAndroid Build Coastguard Workerclass TestCoapOption(unittest.TestCase):
178*cfb92d14SAndroid Build Coastguard Worker
179*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_type_value_when_type_property_is_called(self):
180*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
181*cfb92d14SAndroid Build Coastguard Worker        _type = any_coap_option_type()
182*cfb92d14SAndroid Build Coastguard Worker
183*cfb92d14SAndroid Build Coastguard Worker        coap_opt = coap.CoapOption(_type, any_value())
184*cfb92d14SAndroid Build Coastguard Worker
185*cfb92d14SAndroid Build Coastguard Worker        # WHEN
186*cfb92d14SAndroid Build Coastguard Worker        actual_type = coap_opt.type
187*cfb92d14SAndroid Build Coastguard Worker
188*cfb92d14SAndroid Build Coastguard Worker        # THEN
189*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(_type, actual_type)
190*cfb92d14SAndroid Build Coastguard Worker
191*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_value_value_when_value_property_is_called(self):
192*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
193*cfb92d14SAndroid Build Coastguard Worker        value = any_value()
194*cfb92d14SAndroid Build Coastguard Worker
195*cfb92d14SAndroid Build Coastguard Worker        coap_opt = coap.CoapOption(any_coap_option_type(), value)
196*cfb92d14SAndroid Build Coastguard Worker
197*cfb92d14SAndroid Build Coastguard Worker        # WHEN
198*cfb92d14SAndroid Build Coastguard Worker        actual_value = coap_opt.value
199*cfb92d14SAndroid Build Coastguard Worker
200*cfb92d14SAndroid Build Coastguard Worker        # THEN
201*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(value, actual_value)
202*cfb92d14SAndroid Build Coastguard Worker
203*cfb92d14SAndroid Build Coastguard Worker
204*cfb92d14SAndroid Build Coastguard Workerclass TestCoapOptionsFactory(unittest.TestCase):
205*cfb92d14SAndroid Build Coastguard Worker
206*cfb92d14SAndroid Build Coastguard Worker    def test_should_create_list_of_CoapOption_from_bytearray_when_parse_method_is_called(self):
207*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
208*cfb92d14SAndroid Build Coastguard Worker        delta = any_4bits_value_lower_or_equal_than_12()
209*cfb92d14SAndroid Build Coastguard Worker        length = any_4bits_value_lower_or_equal_than_12()
210*cfb92d14SAndroid Build Coastguard Worker        value = any_bytearray(length)
211*cfb92d14SAndroid Build Coastguard Worker
212*cfb92d14SAndroid Build Coastguard Worker        data = bytearray([delta << 4 | length]) + value
213*cfb92d14SAndroid Build Coastguard Worker
214*cfb92d14SAndroid Build Coastguard Worker        factory = coap.CoapOptionsFactory()
215*cfb92d14SAndroid Build Coastguard Worker
216*cfb92d14SAndroid Build Coastguard Worker        # WHEN
217*cfb92d14SAndroid Build Coastguard Worker        coap_options = factory.parse(io.BytesIO(data), None)
218*cfb92d14SAndroid Build Coastguard Worker
219*cfb92d14SAndroid Build Coastguard Worker        # THEN
220*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(1, len(coap_options))
221*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(delta, coap_options[0].type)
222*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(value, coap_options[0].value)
223*cfb92d14SAndroid Build Coastguard Worker
224*cfb92d14SAndroid Build Coastguard Worker
225*cfb92d14SAndroid Build Coastguard Workerclass TestCoapCode(unittest.TestCase):
226*cfb92d14SAndroid Build Coastguard Worker
227*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_code_value_when_code_property_is_called(self):
228*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
229*cfb92d14SAndroid Build Coastguard Worker        code = any_code()
230*cfb92d14SAndroid Build Coastguard Worker
231*cfb92d14SAndroid Build Coastguard Worker        code_obj = coap.CoapCode(code)
232*cfb92d14SAndroid Build Coastguard Worker
233*cfb92d14SAndroid Build Coastguard Worker        # WHEN
234*cfb92d14SAndroid Build Coastguard Worker        actual_code = code_obj.code
235*cfb92d14SAndroid Build Coastguard Worker
236*cfb92d14SAndroid Build Coastguard Worker        # THEN
237*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(code, actual_code)
238*cfb92d14SAndroid Build Coastguard Worker
239*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_class_value_when_class_property_is_called(self):
240*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
241*cfb92d14SAndroid Build Coastguard Worker        code = any_code()
242*cfb92d14SAndroid Build Coastguard Worker
243*cfb92d14SAndroid Build Coastguard Worker        code_obj = coap.CoapCode(code)
244*cfb92d14SAndroid Build Coastguard Worker
245*cfb92d14SAndroid Build Coastguard Worker        # WHEN
246*cfb92d14SAndroid Build Coastguard Worker        actual_class = code_obj._class
247*cfb92d14SAndroid Build Coastguard Worker
248*cfb92d14SAndroid Build Coastguard Worker        # THEN
249*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual((code >> 5) & 0x7, actual_class)
250*cfb92d14SAndroid Build Coastguard Worker
251*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_detail_value_when_detail_property_is_called(self):
252*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
253*cfb92d14SAndroid Build Coastguard Worker        code = any_code()
254*cfb92d14SAndroid Build Coastguard Worker
255*cfb92d14SAndroid Build Coastguard Worker        code_obj = coap.CoapCode(code)
256*cfb92d14SAndroid Build Coastguard Worker
257*cfb92d14SAndroid Build Coastguard Worker        # WHEN
258*cfb92d14SAndroid Build Coastguard Worker        actual_detail = code_obj.detail
259*cfb92d14SAndroid Build Coastguard Worker
260*cfb92d14SAndroid Build Coastguard Worker        # THEN
261*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(code & 0x1F, actual_detail)
262*cfb92d14SAndroid Build Coastguard Worker
263*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_dotted_value_when_dotted_property_is_called(self):
264*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
265*cfb92d14SAndroid Build Coastguard Worker        code = any_code()
266*cfb92d14SAndroid Build Coastguard Worker
267*cfb92d14SAndroid Build Coastguard Worker        code_obj = coap.CoapCode(code)
268*cfb92d14SAndroid Build Coastguard Worker
269*cfb92d14SAndroid Build Coastguard Worker        # WHEN
270*cfb92d14SAndroid Build Coastguard Worker        actual_dotted = code_obj.dotted
271*cfb92d14SAndroid Build Coastguard Worker
272*cfb92d14SAndroid Build Coastguard Worker        # THEN
273*cfb92d14SAndroid Build Coastguard Worker        _class, detail = actual_dotted.split(".")
274*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(code, (int(_class) << 5) | int(detail))
275*cfb92d14SAndroid Build Coastguard Worker
276*cfb92d14SAndroid Build Coastguard Worker    def test_should_create_CoapCode_when_from_class_and_detail_classmethod_is_called(self):
277*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
278*cfb92d14SAndroid Build Coastguard Worker        code = any_code()
279*cfb92d14SAndroid Build Coastguard Worker
280*cfb92d14SAndroid Build Coastguard Worker        _class = (code >> 5) & 0x7
281*cfb92d14SAndroid Build Coastguard Worker        detail = code & 0x1F
282*cfb92d14SAndroid Build Coastguard Worker
283*cfb92d14SAndroid Build Coastguard Worker        # WHEN
284*cfb92d14SAndroid Build Coastguard Worker        actual_coap_obj = coap.CoapCode.from_class_and_detail(_class, detail)
285*cfb92d14SAndroid Build Coastguard Worker
286*cfb92d14SAndroid Build Coastguard Worker        # THEN
287*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(code, actual_coap_obj.code)
288*cfb92d14SAndroid Build Coastguard Worker
289*cfb92d14SAndroid Build Coastguard Worker    def test_should_create_CoapCode_when_from_dotted_string_classmethod_is_called(self):
290*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
291*cfb92d14SAndroid Build Coastguard Worker        code = any_code()
292*cfb92d14SAndroid Build Coastguard Worker
293*cfb92d14SAndroid Build Coastguard Worker        code_obj = coap.CoapCode(code)
294*cfb92d14SAndroid Build Coastguard Worker
295*cfb92d14SAndroid Build Coastguard Worker        # WHEN
296*cfb92d14SAndroid Build Coastguard Worker        actual_coap_obj = coap.CoapCode.from_dotted(code_obj.dotted)
297*cfb92d14SAndroid Build Coastguard Worker
298*cfb92d14SAndroid Build Coastguard Worker        # THEN
299*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(code, actual_coap_obj.code)
300*cfb92d14SAndroid Build Coastguard Worker
301*cfb92d14SAndroid Build Coastguard Worker
302*cfb92d14SAndroid Build Coastguard Workerclass TestCoapMessage(unittest.TestCase):
303*cfb92d14SAndroid Build Coastguard Worker
304*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_version_value_when_version_property_is_called(self):
305*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
306*cfb92d14SAndroid Build Coastguard Worker        version = any_version()
307*cfb92d14SAndroid Build Coastguard Worker
308*cfb92d14SAndroid Build Coastguard Worker        coap_message = coap.CoapMessage(
309*cfb92d14SAndroid Build Coastguard Worker            version,
310*cfb92d14SAndroid Build Coastguard Worker            any_type(),
311*cfb92d14SAndroid Build Coastguard Worker            any_code(),
312*cfb92d14SAndroid Build Coastguard Worker            any_message_id(),
313*cfb92d14SAndroid Build Coastguard Worker            any_token(),
314*cfb92d14SAndroid Build Coastguard Worker            any_options(),
315*cfb92d14SAndroid Build Coastguard Worker            any_payload(),
316*cfb92d14SAndroid Build Coastguard Worker        )
317*cfb92d14SAndroid Build Coastguard Worker
318*cfb92d14SAndroid Build Coastguard Worker        # WHEN
319*cfb92d14SAndroid Build Coastguard Worker        actual_version = coap_message.version
320*cfb92d14SAndroid Build Coastguard Worker
321*cfb92d14SAndroid Build Coastguard Worker        # THEN
322*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(version, actual_version)
323*cfb92d14SAndroid Build Coastguard Worker
324*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_type_value_when_type_property_is_called(self):
325*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
326*cfb92d14SAndroid Build Coastguard Worker        _type = any_type()
327*cfb92d14SAndroid Build Coastguard Worker
328*cfb92d14SAndroid Build Coastguard Worker        coap_message = coap.CoapMessage(
329*cfb92d14SAndroid Build Coastguard Worker            any_version(),
330*cfb92d14SAndroid Build Coastguard Worker            _type,
331*cfb92d14SAndroid Build Coastguard Worker            any_code(),
332*cfb92d14SAndroid Build Coastguard Worker            any_message_id(),
333*cfb92d14SAndroid Build Coastguard Worker            any_token(),
334*cfb92d14SAndroid Build Coastguard Worker            any_options(),
335*cfb92d14SAndroid Build Coastguard Worker            any_payload(),
336*cfb92d14SAndroid Build Coastguard Worker        )
337*cfb92d14SAndroid Build Coastguard Worker
338*cfb92d14SAndroid Build Coastguard Worker        # WHEN
339*cfb92d14SAndroid Build Coastguard Worker        actual_type = coap_message.type
340*cfb92d14SAndroid Build Coastguard Worker
341*cfb92d14SAndroid Build Coastguard Worker        # THEN
342*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(_type, actual_type)
343*cfb92d14SAndroid Build Coastguard Worker
344*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_code_value_when_code_property_is_called(self):
345*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
346*cfb92d14SAndroid Build Coastguard Worker        code = any_code()
347*cfb92d14SAndroid Build Coastguard Worker
348*cfb92d14SAndroid Build Coastguard Worker        coap_message = coap.CoapMessage(
349*cfb92d14SAndroid Build Coastguard Worker            any_version(),
350*cfb92d14SAndroid Build Coastguard Worker            any_type(),
351*cfb92d14SAndroid Build Coastguard Worker            code,
352*cfb92d14SAndroid Build Coastguard Worker            any_message_id(),
353*cfb92d14SAndroid Build Coastguard Worker            any_token(),
354*cfb92d14SAndroid Build Coastguard Worker            any_options(),
355*cfb92d14SAndroid Build Coastguard Worker            any_payload(),
356*cfb92d14SAndroid Build Coastguard Worker        )
357*cfb92d14SAndroid Build Coastguard Worker
358*cfb92d14SAndroid Build Coastguard Worker        # WHEN
359*cfb92d14SAndroid Build Coastguard Worker        actual_code = coap_message.code
360*cfb92d14SAndroid Build Coastguard Worker
361*cfb92d14SAndroid Build Coastguard Worker        # THEN
362*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(code, actual_code)
363*cfb92d14SAndroid Build Coastguard Worker
364*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_message_id_value_when_message_id_property_is_called(self):
365*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
366*cfb92d14SAndroid Build Coastguard Worker        message_id = any_message_id()
367*cfb92d14SAndroid Build Coastguard Worker
368*cfb92d14SAndroid Build Coastguard Worker        coap_message = coap.CoapMessage(
369*cfb92d14SAndroid Build Coastguard Worker            any_version(),
370*cfb92d14SAndroid Build Coastguard Worker            any_type(),
371*cfb92d14SAndroid Build Coastguard Worker            any_code(),
372*cfb92d14SAndroid Build Coastguard Worker            message_id,
373*cfb92d14SAndroid Build Coastguard Worker            any_token(),
374*cfb92d14SAndroid Build Coastguard Worker            any_options(),
375*cfb92d14SAndroid Build Coastguard Worker            any_payload(),
376*cfb92d14SAndroid Build Coastguard Worker        )
377*cfb92d14SAndroid Build Coastguard Worker
378*cfb92d14SAndroid Build Coastguard Worker        # WHEN
379*cfb92d14SAndroid Build Coastguard Worker        actual_message_id = coap_message.message_id
380*cfb92d14SAndroid Build Coastguard Worker
381*cfb92d14SAndroid Build Coastguard Worker        # THEN
382*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(message_id, actual_message_id)
383*cfb92d14SAndroid Build Coastguard Worker
384*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_token_value_when_token_property_is_called(self):
385*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
386*cfb92d14SAndroid Build Coastguard Worker        token = any_token()
387*cfb92d14SAndroid Build Coastguard Worker
388*cfb92d14SAndroid Build Coastguard Worker        coap_message = coap.CoapMessage(
389*cfb92d14SAndroid Build Coastguard Worker            any_version(),
390*cfb92d14SAndroid Build Coastguard Worker            any_type(),
391*cfb92d14SAndroid Build Coastguard Worker            any_code(),
392*cfb92d14SAndroid Build Coastguard Worker            any_message_id(),
393*cfb92d14SAndroid Build Coastguard Worker            token,
394*cfb92d14SAndroid Build Coastguard Worker            any_options(),
395*cfb92d14SAndroid Build Coastguard Worker            any_payload(),
396*cfb92d14SAndroid Build Coastguard Worker        )
397*cfb92d14SAndroid Build Coastguard Worker
398*cfb92d14SAndroid Build Coastguard Worker        # WHEN
399*cfb92d14SAndroid Build Coastguard Worker        actual_token = coap_message.token
400*cfb92d14SAndroid Build Coastguard Worker
401*cfb92d14SAndroid Build Coastguard Worker        # THEN
402*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(token, actual_token)
403*cfb92d14SAndroid Build Coastguard Worker
404*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_tkl_value_when_tkl_property_is_called(self):
405*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
406*cfb92d14SAndroid Build Coastguard Worker        token = any_token()
407*cfb92d14SAndroid Build Coastguard Worker
408*cfb92d14SAndroid Build Coastguard Worker        coap_message = coap.CoapMessage(
409*cfb92d14SAndroid Build Coastguard Worker            any_version(),
410*cfb92d14SAndroid Build Coastguard Worker            any_type(),
411*cfb92d14SAndroid Build Coastguard Worker            any_code(),
412*cfb92d14SAndroid Build Coastguard Worker            any_message_id(),
413*cfb92d14SAndroid Build Coastguard Worker            token,
414*cfb92d14SAndroid Build Coastguard Worker            any_options(),
415*cfb92d14SAndroid Build Coastguard Worker            any_payload(),
416*cfb92d14SAndroid Build Coastguard Worker        )
417*cfb92d14SAndroid Build Coastguard Worker
418*cfb92d14SAndroid Build Coastguard Worker        # WHEN
419*cfb92d14SAndroid Build Coastguard Worker        actual_tkl = coap_message.tkl
420*cfb92d14SAndroid Build Coastguard Worker
421*cfb92d14SAndroid Build Coastguard Worker        # THEN
422*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(len(token), actual_tkl)
423*cfb92d14SAndroid Build Coastguard Worker
424*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_options_value_when_options_property_is_called(self):
425*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
426*cfb92d14SAndroid Build Coastguard Worker        options = any_options()
427*cfb92d14SAndroid Build Coastguard Worker
428*cfb92d14SAndroid Build Coastguard Worker        coap_message = coap.CoapMessage(
429*cfb92d14SAndroid Build Coastguard Worker            any_version(),
430*cfb92d14SAndroid Build Coastguard Worker            any_type(),
431*cfb92d14SAndroid Build Coastguard Worker            any_code(),
432*cfb92d14SAndroid Build Coastguard Worker            any_message_id(),
433*cfb92d14SAndroid Build Coastguard Worker            any_token(),
434*cfb92d14SAndroid Build Coastguard Worker            options,
435*cfb92d14SAndroid Build Coastguard Worker            any_payload(),
436*cfb92d14SAndroid Build Coastguard Worker        )
437*cfb92d14SAndroid Build Coastguard Worker
438*cfb92d14SAndroid Build Coastguard Worker        # WHEN
439*cfb92d14SAndroid Build Coastguard Worker        actual_options = coap_message.options
440*cfb92d14SAndroid Build Coastguard Worker
441*cfb92d14SAndroid Build Coastguard Worker        # THEN
442*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(options, actual_options)
443*cfb92d14SAndroid Build Coastguard Worker
444*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_payload_value_when_payload_property_is_called(self):
445*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
446*cfb92d14SAndroid Build Coastguard Worker        payload = any_payload()
447*cfb92d14SAndroid Build Coastguard Worker
448*cfb92d14SAndroid Build Coastguard Worker        coap_message = coap.CoapMessage(
449*cfb92d14SAndroid Build Coastguard Worker            any_version(),
450*cfb92d14SAndroid Build Coastguard Worker            any_type(),
451*cfb92d14SAndroid Build Coastguard Worker            any_code(),
452*cfb92d14SAndroid Build Coastguard Worker            any_message_id(),
453*cfb92d14SAndroid Build Coastguard Worker            any_token(),
454*cfb92d14SAndroid Build Coastguard Worker            any_options(),
455*cfb92d14SAndroid Build Coastguard Worker            payload,
456*cfb92d14SAndroid Build Coastguard Worker        )
457*cfb92d14SAndroid Build Coastguard Worker
458*cfb92d14SAndroid Build Coastguard Worker        # WHEN
459*cfb92d14SAndroid Build Coastguard Worker        actual_payload = coap_message.payload
460*cfb92d14SAndroid Build Coastguard Worker
461*cfb92d14SAndroid Build Coastguard Worker        # THEN
462*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(payload, actual_payload)
463*cfb92d14SAndroid Build Coastguard Worker
464*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_uri_path_value_when_uri_path_property_is_called(self):
465*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
466*cfb92d14SAndroid Build Coastguard Worker        uri_path = any_uri_path()
467*cfb92d14SAndroid Build Coastguard Worker
468*cfb92d14SAndroid Build Coastguard Worker        coap_message = coap.CoapMessage(
469*cfb92d14SAndroid Build Coastguard Worker            any_version(),
470*cfb92d14SAndroid Build Coastguard Worker            any_type(),
471*cfb92d14SAndroid Build Coastguard Worker            any_code(),
472*cfb92d14SAndroid Build Coastguard Worker            any_message_id(),
473*cfb92d14SAndroid Build Coastguard Worker            any_token(),
474*cfb92d14SAndroid Build Coastguard Worker            any_options(),
475*cfb92d14SAndroid Build Coastguard Worker            any_payload(),
476*cfb92d14SAndroid Build Coastguard Worker            uri_path,
477*cfb92d14SAndroid Build Coastguard Worker        )
478*cfb92d14SAndroid Build Coastguard Worker
479*cfb92d14SAndroid Build Coastguard Worker        # WHEN
480*cfb92d14SAndroid Build Coastguard Worker        actual_uri_path = coap_message.uri_path
481*cfb92d14SAndroid Build Coastguard Worker
482*cfb92d14SAndroid Build Coastguard Worker        # THEN
483*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(uri_path, actual_uri_path)
484*cfb92d14SAndroid Build Coastguard Worker
485*cfb92d14SAndroid Build Coastguard Worker
486*cfb92d14SAndroid Build Coastguard Workerclass TestCoapMessageIdToUriPathBinder(unittest.TestCase):
487*cfb92d14SAndroid Build Coastguard Worker
488*cfb92d14SAndroid Build Coastguard Worker    def test_should_add_uri_path_to_binds_when_add_uri_path_for_method_is_called(self):
489*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
490*cfb92d14SAndroid Build Coastguard Worker        message_id = any_message_id()
491*cfb92d14SAndroid Build Coastguard Worker        token = any_token()
492*cfb92d14SAndroid Build Coastguard Worker        uri_path = any_uri_path()
493*cfb92d14SAndroid Build Coastguard Worker
494*cfb92d14SAndroid Build Coastguard Worker        binder = coap.CoapMessageIdToUriPathBinder()
495*cfb92d14SAndroid Build Coastguard Worker
496*cfb92d14SAndroid Build Coastguard Worker        # WHEN
497*cfb92d14SAndroid Build Coastguard Worker        binder.add_uri_path_for(message_id, token, uri_path)
498*cfb92d14SAndroid Build Coastguard Worker
499*cfb92d14SAndroid Build Coastguard Worker        # THEN
500*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(uri_path, binder.get_uri_path_for(message_id, token))
501*cfb92d14SAndroid Build Coastguard Worker
502*cfb92d14SAndroid Build Coastguard Worker    def test_should_raise_KeyError_when_get_uri_path_for_is_called_but_it_is_not_present_in_database(self):
503*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
504*cfb92d14SAndroid Build Coastguard Worker        message_id = any_message_id()
505*cfb92d14SAndroid Build Coastguard Worker        token = any_token()
506*cfb92d14SAndroid Build Coastguard Worker        any_uri_path()
507*cfb92d14SAndroid Build Coastguard Worker
508*cfb92d14SAndroid Build Coastguard Worker        binder = coap.CoapMessageIdToUriPathBinder()
509*cfb92d14SAndroid Build Coastguard Worker
510*cfb92d14SAndroid Build Coastguard Worker        # THEN
511*cfb92d14SAndroid Build Coastguard Worker        self.assertRaises(RuntimeError, binder.get_uri_path_for, message_id, token)
512*cfb92d14SAndroid Build Coastguard Worker
513*cfb92d14SAndroid Build Coastguard Worker
514*cfb92d14SAndroid Build Coastguard Workerclass TestCoapMessageFactory(unittest.TestCase):
515*cfb92d14SAndroid Build Coastguard Worker
516*cfb92d14SAndroid Build Coastguard Worker    def _create_dummy_payload_factory(self):
517*cfb92d14SAndroid Build Coastguard Worker
518*cfb92d14SAndroid Build Coastguard Worker        class DummyPayloadFactory:
519*cfb92d14SAndroid Build Coastguard Worker
520*cfb92d14SAndroid Build Coastguard Worker            def parse(self, data, message_info):
521*cfb92d14SAndroid Build Coastguard Worker                return data.read()
522*cfb92d14SAndroid Build Coastguard Worker
523*cfb92d14SAndroid Build Coastguard Worker        return DummyPayloadFactory()
524*cfb92d14SAndroid Build Coastguard Worker
525*cfb92d14SAndroid Build Coastguard Worker    def _create_coap_message_factory(self):
526*cfb92d14SAndroid Build Coastguard Worker        return coap.CoapMessageFactory(
527*cfb92d14SAndroid Build Coastguard Worker            options_factory=coap.CoapOptionsFactory(),
528*cfb92d14SAndroid Build Coastguard Worker            uri_path_based_payload_factories={"/a/as": self._create_dummy_payload_factory()},
529*cfb92d14SAndroid Build Coastguard Worker            message_id_to_uri_path_binder=coap.CoapMessageIdToUriPathBinder(),
530*cfb92d14SAndroid Build Coastguard Worker        )
531*cfb92d14SAndroid Build Coastguard Worker
532*cfb92d14SAndroid Build Coastguard Worker    def test_should_create_CoapMessage_from_solicit_request_data_when_parse_method_is_called(self):
533*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
534*cfb92d14SAndroid Build Coastguard Worker        data = bytearray([
535*cfb92d14SAndroid Build Coastguard Worker            0x42,
536*cfb92d14SAndroid Build Coastguard Worker            0x02,
537*cfb92d14SAndroid Build Coastguard Worker            0x00,
538*cfb92d14SAndroid Build Coastguard Worker            0xBD,
539*cfb92d14SAndroid Build Coastguard Worker            0x65,
540*cfb92d14SAndroid Build Coastguard Worker            0xee,
541*cfb92d14SAndroid Build Coastguard Worker            0xB1,
542*cfb92d14SAndroid Build Coastguard Worker            0x61,
543*cfb92d14SAndroid Build Coastguard Worker            0x02,
544*cfb92d14SAndroid Build Coastguard Worker            0x61,
545*cfb92d14SAndroid Build Coastguard Worker            0x73,
546*cfb92d14SAndroid Build Coastguard Worker            0xff,
547*cfb92d14SAndroid Build Coastguard Worker            0x01,
548*cfb92d14SAndroid Build Coastguard Worker            0x08,
549*cfb92d14SAndroid Build Coastguard Worker            0x16,
550*cfb92d14SAndroid Build Coastguard Worker            0x6E,
551*cfb92d14SAndroid Build Coastguard Worker            0x0A,
552*cfb92d14SAndroid Build Coastguard Worker            0x00,
553*cfb92d14SAndroid Build Coastguard Worker            0x00,
554*cfb92d14SAndroid Build Coastguard Worker            0x00,
555*cfb92d14SAndroid Build Coastguard Worker            0x00,
556*cfb92d14SAndroid Build Coastguard Worker            0x02,
557*cfb92d14SAndroid Build Coastguard Worker            0x04,
558*cfb92d14SAndroid Build Coastguard Worker            0x01,
559*cfb92d14SAndroid Build Coastguard Worker            0x02,
560*cfb92d14SAndroid Build Coastguard Worker        ])
561*cfb92d14SAndroid Build Coastguard Worker
562*cfb92d14SAndroid Build Coastguard Worker        factory = self._create_coap_message_factory()
563*cfb92d14SAndroid Build Coastguard Worker
564*cfb92d14SAndroid Build Coastguard Worker        # WHEN
565*cfb92d14SAndroid Build Coastguard Worker        coap_message = factory.parse(io.BytesIO(data), None)
566*cfb92d14SAndroid Build Coastguard Worker
567*cfb92d14SAndroid Build Coastguard Worker        # THEN
568*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(1, coap_message.version)
569*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(0, coap_message.type)
570*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(2, coap_message.tkl)
571*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(2, coap_message.code)
572*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(189, coap_message.message_id)
573*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(bytearray([0x65, 0xee]), coap_message.token)
574*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual("a", coap_message.options[0].value.decode("utf-8"))
575*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual("as", coap_message.options[1].value.decode("utf-8"))
576*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual("/a/as", coap_message.uri_path)
577*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(
578*cfb92d14SAndroid Build Coastguard Worker            bytearray([
579*cfb92d14SAndroid Build Coastguard Worker                0x01,
580*cfb92d14SAndroid Build Coastguard Worker                0x08,
581*cfb92d14SAndroid Build Coastguard Worker                0x16,
582*cfb92d14SAndroid Build Coastguard Worker                0x6E,
583*cfb92d14SAndroid Build Coastguard Worker                0x0A,
584*cfb92d14SAndroid Build Coastguard Worker                0x00,
585*cfb92d14SAndroid Build Coastguard Worker                0x00,
586*cfb92d14SAndroid Build Coastguard Worker                0x00,
587*cfb92d14SAndroid Build Coastguard Worker                0x00,
588*cfb92d14SAndroid Build Coastguard Worker                0x02,
589*cfb92d14SAndroid Build Coastguard Worker                0x04,
590*cfb92d14SAndroid Build Coastguard Worker                0x01,
591*cfb92d14SAndroid Build Coastguard Worker                0x02,
592*cfb92d14SAndroid Build Coastguard Worker            ]),
593*cfb92d14SAndroid Build Coastguard Worker            coap_message.payload,
594*cfb92d14SAndroid Build Coastguard Worker        )
595*cfb92d14SAndroid Build Coastguard Worker
596*cfb92d14SAndroid Build Coastguard Worker    def test_should_create_CoapMessage_from_solicit_response_data_when_parse_method_is_called(self):
597*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
598*cfb92d14SAndroid Build Coastguard Worker        data = bytearray([
599*cfb92d14SAndroid Build Coastguard Worker            0x62,
600*cfb92d14SAndroid Build Coastguard Worker            0x44,
601*cfb92d14SAndroid Build Coastguard Worker            0x00,
602*cfb92d14SAndroid Build Coastguard Worker            0xBD,
603*cfb92d14SAndroid Build Coastguard Worker            0x65,
604*cfb92d14SAndroid Build Coastguard Worker            0xee,
605*cfb92d14SAndroid Build Coastguard Worker            0xff,
606*cfb92d14SAndroid Build Coastguard Worker            0x04,
607*cfb92d14SAndroid Build Coastguard Worker            0x01,
608*cfb92d14SAndroid Build Coastguard Worker            0x00,
609*cfb92d14SAndroid Build Coastguard Worker            0x02,
610*cfb92d14SAndroid Build Coastguard Worker            0x02,
611*cfb92d14SAndroid Build Coastguard Worker            0x00,
612*cfb92d14SAndroid Build Coastguard Worker            0x00,
613*cfb92d14SAndroid Build Coastguard Worker            0x07,
614*cfb92d14SAndroid Build Coastguard Worker            0x09,
615*cfb92d14SAndroid Build Coastguard Worker            0x76,
616*cfb92d14SAndroid Build Coastguard Worker            0x80,
617*cfb92d14SAndroid Build Coastguard Worker            0x00,
618*cfb92d14SAndroid Build Coastguard Worker            0x01,
619*cfb92d14SAndroid Build Coastguard Worker            0x00,
620*cfb92d14SAndroid Build Coastguard Worker            0x00,
621*cfb92d14SAndroid Build Coastguard Worker            0x00,
622*cfb92d14SAndroid Build Coastguard Worker            0x00,
623*cfb92d14SAndroid Build Coastguard Worker            0x00,
624*cfb92d14SAndroid Build Coastguard Worker        ])
625*cfb92d14SAndroid Build Coastguard Worker
626*cfb92d14SAndroid Build Coastguard Worker        mid_binder = coap.CoapMessageIdToUriPathBinder()
627*cfb92d14SAndroid Build Coastguard Worker        mid_binder.add_uri_path_for(189, bytearray([0x65, 0xee]), "/a/as")
628*cfb92d14SAndroid Build Coastguard Worker
629*cfb92d14SAndroid Build Coastguard Worker        factory = coap.CoapMessageFactory(
630*cfb92d14SAndroid Build Coastguard Worker            options_factory=coap.CoapOptionsFactory(),
631*cfb92d14SAndroid Build Coastguard Worker            uri_path_based_payload_factories={"/a/as": self._create_dummy_payload_factory()},
632*cfb92d14SAndroid Build Coastguard Worker            message_id_to_uri_path_binder=mid_binder,
633*cfb92d14SAndroid Build Coastguard Worker        )
634*cfb92d14SAndroid Build Coastguard Worker
635*cfb92d14SAndroid Build Coastguard Worker        # WHEN
636*cfb92d14SAndroid Build Coastguard Worker        coap_message = factory.parse(io.BytesIO(data), None)
637*cfb92d14SAndroid Build Coastguard Worker
638*cfb92d14SAndroid Build Coastguard Worker        # THEN
639*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(1, coap_message.version)
640*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(2, coap_message.type)
641*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(2, coap_message.tkl)
642*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual("2.04", coap_message.code)
643*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(189, coap_message.message_id)
644*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(bytearray([0x65, 0xee]), coap_message.token)
645*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(None, coap_message.uri_path)
646*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(
647*cfb92d14SAndroid Build Coastguard Worker            bytearray([
648*cfb92d14SAndroid Build Coastguard Worker                0x04,
649*cfb92d14SAndroid Build Coastguard Worker                0x01,
650*cfb92d14SAndroid Build Coastguard Worker                0x00,
651*cfb92d14SAndroid Build Coastguard Worker                0x02,
652*cfb92d14SAndroid Build Coastguard Worker                0x02,
653*cfb92d14SAndroid Build Coastguard Worker                0x00,
654*cfb92d14SAndroid Build Coastguard Worker                0x00,
655*cfb92d14SAndroid Build Coastguard Worker                0x07,
656*cfb92d14SAndroid Build Coastguard Worker                0x09,
657*cfb92d14SAndroid Build Coastguard Worker                0x76,
658*cfb92d14SAndroid Build Coastguard Worker                0x80,
659*cfb92d14SAndroid Build Coastguard Worker                0x00,
660*cfb92d14SAndroid Build Coastguard Worker                0x01,
661*cfb92d14SAndroid Build Coastguard Worker                0x00,
662*cfb92d14SAndroid Build Coastguard Worker                0x00,
663*cfb92d14SAndroid Build Coastguard Worker                0x00,
664*cfb92d14SAndroid Build Coastguard Worker                0x00,
665*cfb92d14SAndroid Build Coastguard Worker                0x00,
666*cfb92d14SAndroid Build Coastguard Worker            ]),
667*cfb92d14SAndroid Build Coastguard Worker            coap_message.payload,
668*cfb92d14SAndroid Build Coastguard Worker        )
669*cfb92d14SAndroid Build Coastguard Worker
670*cfb92d14SAndroid Build Coastguard Worker
671*cfb92d14SAndroid Build Coastguard Workerif __name__ == "__main__":
672*cfb92d14SAndroid Build Coastguard Worker    unittest.main()
673