Debian Codegraph @

python/tests/proton_tests/message.py

raw
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19
20 from __future__ import absolute_import
21
22 from uuid import uuid4
23 from sys import version_info
24
25 from proton import *
26
27 from . import common
28
29
30 class Test(common.Test):
31
32 def setUp(self):
33 self.msg = Message()
34
35 def tearDown(self):
36 self.msg = None
37
38
39 class AccessorsTest(Test):
40
41 def _test(self, name, default, values):
42 d = getattr(self.msg, name)
43 assert d == default, (d, default)
44 for v in values:
45 setattr(self.msg, name, v)
46 gotten = getattr(self.msg, name)
47 assert gotten == v, gotten
48
49 def _test_symbol(self, name):
50 self._test(name, symbol(None), (symbol(u"abc.123.#$%"), symbol(u"hello.world")))
51
52 def _test_str(self, name):
53 self._test(name, None, (u"asdf", u"fdsa", u""))
54
55 def _test_time(self, name):
56 self._test(name, 0, (0, 123456789, 987654321))
57
58 def testId(self):
59 self._test("id", None, ("bytes", None, 123, u"string", uuid4()))
60
61 def testCorrelationId(self):
62 self._test("correlation_id", None, ("bytes", None, 123, u"string", uuid4()))
63
64 def testDurable(self):
65 self._test("durable", False, (True, False))
66
67 def testPriority(self):
68 self._test("priority", Message.DEFAULT_PRIORITY, range(0, 255))
69
70 def testTtl(self):
71 self._test("ttl", 0, range(12345, 54321))
72
73 def testFirstAcquirer(self):
74 self._test("first_acquirer", False, (True, False))
75
76 def testDeliveryCount(self):
77 self._test("delivery_count", 0, range(0, 1024))
78
79 def testUserId(self):
80 self._test("user_id", b"", (b"asdf", b"fdsa", b"asd\x00fdsa", b""))
81
82 def testAddress(self):
83 self._test_str("address")
84
85 def testSubject(self):
86 self._test_str("subject")
87
88 def testReplyTo(self):
89 self._test_str("reply_to")
90
91 def testContentType(self):
92 self._test_symbol("content_type")
93
94 def testContentEncoding(self):
95 self._test_symbol("content_encoding")
96
97 def testExpiryTime(self):
98 self._test_time("expiry_time")
99
100 def testCreationTime(self):
101 self._test_time("creation_time")
102
103 def testGroupId(self):
104 self._test_str("group_id")
105
106 def testGroupSequence(self):
107 self._test("group_sequence", 0, (0, 1, 10, 20, 4294967294, 4294967295))
108
109 def testReplyToGroupId(self):
110 self._test_str("reply_to_group_id")
111
112
113 long = int
114 unicode = str
115
116
117 class CodecTest(Test):
118
119 def testProperties(self):
120 self.msg.properties = {'key': 'value'}
121 data = self.msg.encode()
122
123 msg2 = Message()
124 msg2.decode(data)
125
126 assert msg2.properties['key'] == 'value', msg2.properties['key']
127
128 class MyStringSubclass(unicode):
129 def __repr__(self):
130 return 'MyStringSubclass(%s)' % unicode.__repr__(self)
131
132 def testStringSubclassPropertyKey(self):
133 self.msg.properties = {'abc': 123, CodecTest.MyStringSubclass('def'): 456}
134 data = self.msg.encode()
135
136 msg2 = Message()
137 msg2.decode(data)
138
139 assert msg2.properties == self.msg.properties
140 for k in msg2.properties:
141 assert type(k) is unicode, 'non-string key %s %s' % (k, type(k))
142
143 def _testNonStringPropertyKey(self, k):
144 self.msg.properties = {k: 'abc'}
145 try:
146 self.msg.encode()
147 except MessageException:
148 return
149 self.fail('Non-string property key: %s, key type: %s' % (self.msg, type(k)))
150
151 def testNonSequencePropertyKeys(self):
152 # AMQP non-string types
153 self._testNonStringPropertyKey(None)
154 self._testNonStringPropertyKey(True)
155 self._testNonStringPropertyKey(byte(1))
156 self._testNonStringPropertyKey(ubyte(2))
157 self._testNonStringPropertyKey(short(3))
158 self._testNonStringPropertyKey(ushort(4))
159 self._testNonStringPropertyKey(int32(5))
160 self._testNonStringPropertyKey(uint(6))
161 self._testNonStringPropertyKey(long(7))
162 self._testNonStringPropertyKey(ulong(8))
163 self._testNonStringPropertyKey(float32(9.0))
164 self._testNonStringPropertyKey(10.0)
165 self._testNonStringPropertyKey(decimal32(11))
166 self._testNonStringPropertyKey(decimal64(12))
167 self._testNonStringPropertyKey(timestamp(1234567890))
168 self._testNonStringPropertyKey(uuid4())
169
170 def testCharPropertyKey(self):
171 self._testNonStringPropertyKey(char('A'))
172
173 def testDecimal128PropertyKey(self):
174 self._testNonStringPropertyKey(decimal128(b'12345'))
175
176 def testSymbolPropertyKey(self):
177 self._testNonStringPropertyKey(symbol('abcdef'))
178
179 def testBinaryPropertyKey(self):
180 if version_info[0] > 2:
181 # Py3: Binary conversions not supported
182 self._testNonStringPropertyKey(b'abc123')
183 else:
184 # Py2: Check for correct key conversion
185 self.msg.properties = {'abc': 123, b'def': 456}
186 data = self.msg.encode()
187
188 msg2 = Message()
189 msg2.decode(data)
190
191 assert msg2.properties == self.msg.properties
192 for k in msg2.properties:
193 assert type(k) is unicode, 'non-string key %s %s' % (k, type(k))
194
195 def testAnnotationsSymbolicAndUlongKey(self, a=None):
196 if a is None:
197 a = {symbol('one'): 1, 'two': 2, ulong(3): 'three'}
198 self.msg.annotations = a
199 data = self.msg.encode()
200
201 msg2 = Message()
202 msg2.decode(data)
203 # both keys must be symbols
204 assert msg2.annotations == a
205
206 def testRoundTrip(self):
207 self.msg.id = "asdf"
208 self.msg.correlation_id = uuid4()
209 self.msg.ttl = 3
210 self.msg.priority = 100
211 self.msg.address = "address"
212 self.msg.subject = "subject"
213 self.msg.body = 'Hello World!'
214
215 data = self.msg.encode()
216
217 msg2 = Message()
218 msg2.decode(data)
219
220 assert self.msg.id == msg2.id, (self.msg.id, msg2.id)
221 assert self.msg.correlation_id == msg2.correlation_id, (self.msg.correlation_id, msg2.correlation_id)
222 assert self.msg.ttl == msg2.ttl, (self.msg.ttl, msg2.ttl)
223 assert self.msg.priority == msg2.priority, (self.msg.priority, msg2.priority)
224 assert self.msg.address == msg2.address, (self.msg.address, msg2.address)
225 assert self.msg.subject == msg2.subject, (self.msg.subject, msg2.subject)
226 assert self.msg.body == msg2.body, (self.msg.body, msg2.body)
227
228 def testExpiryEncodeAsNull(self):
229 self.msg.group_id = "A" # Force creation and expiry fields to be present
230 data = self.msg.encode()
231
232 decoder = Data()
233
234 # Skip past the headers
235 consumed = decoder.decode(data)
236 decoder.clear()
237 data = data[consumed:]
238
239 decoder.decode(data)
240 dproperties = decoder.get_py_described()
241 # Check we've got the correct described list
242 assert dproperties.descriptor == 0x73, (dproperties.descriptor)
243
244 properties = dproperties.value
245 assert properties[8] is None, properties[8]
246
247 def testCreationEncodeAsNull(self):
248 self.msg.group_id = "A" # Force creation and expiry fields to be present
249 data = self.msg.encode()
250
251 decoder = Data()
252
253 # Skip past the headers
254 consumed = decoder.decode(data)
255 decoder.clear()
256 data = data[consumed:]
257
258 decoder.decode(data)
259 dproperties = decoder.get_py_described()
260 # Check we've got the correct described list
261 assert dproperties.descriptor == 0x73, (dproperties.descriptor)
262
263 properties = dproperties.value
264 assert properties[9] is None, properties[9]
265
266 def testGroupSequenceEncodeAsNull(self):
267 self.msg.reply_to_group_id = "R" # Force group_id and group_sequence fields to be present
268 data = self.msg.encode()
269
270 decoder = Data()
271
272 # Skip past the headers
273 consumed = decoder.decode(data)
274 decoder.clear()
275 data = data[consumed:]
276
277 decoder.decode(data)
278 dproperties = decoder.get_py_described()
279 # Check we've got the correct described list
280 assert dproperties.descriptor == 0x73, (dproperties.descriptor)
281
282 properties = dproperties.value
283 assert properties[10] is None, properties[10]
284 assert properties[11] is None, properties[11]
285
286 def testGroupSequenceEncodeAsNonNull(self):
287 self.msg.group_id = "G"
288 self.msg.reply_to_group_id = "R" # Force group_id and group_sequence fields to be present
289 data = self.msg.encode()
290
291 decoder = Data()
292
293 # Skip past the headers
294 consumed = decoder.decode(data)
295 decoder.clear()
296 data = data[consumed:]
297
298 decoder.decode(data)
299 dproperties = decoder.get_py_described()
300 # Check we've got the correct described list
301 assert dproperties.descriptor == 0x73, (dproperties.descriptor)
302
303 properties = dproperties.value
304 assert properties[10] == 'G', properties[10]
305 assert properties[11] == 0, properties[11]
306
307 def testDefaultCreationExpiryDecode(self):
308 # This is a message with everything filled explicitly as null or zero in LIST32 HEADER and PROPERTIES lists
309 data = b'\x00\x53\x70\xd0\x00\x00\x00\x0a\x00\x00\x00\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xd0\x00\x00\x00\x12\x00\x00\x00\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x52\x00\x40'
310 msg2 = Message()
311 msg2.decode(data)
312 assert msg2.expiry_time == 0, (msg2.expiry_time)
313 assert msg2.creation_time == 0, (msg2.creation_time)
314
315 # The same message with LIST8s instead
316 data = b'\x00\x53\x70\xc0\x07\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xc0\x0f\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x52\x00\x40'
317 msg3 = Message()
318 msg3.decode(data)
319 assert msg2.expiry_time == 0, (msg2.expiry_time)
320 assert msg2.creation_time == 0, (msg2.creation_time)
321
322 # Minified message with zero length HEADER and PROPERTIES lists
323 data = b'\x00\x53\x70\x45' b'\x00\x53\x73\x45'
324 msg4 = Message()
325 msg4.decode(data)
326 assert msg2.expiry_time == 0, (msg2.expiry_time)
327 assert msg2.creation_time == 0, (msg2.creation_time)
328
329 def testDefaultPriorityEncode(self):
330 assert self.msg.priority == 4, (self.msg.priority)
331 self.msg.ttl = 0.003 # field after priority, so forces priority to be present
332 data = self.msg.encode()
333
334 decoder = Data()
335 decoder.decode(data)
336
337 dheaders = decoder.get_py_described()
338 # Check we've got the correct described list
339 assert dheaders.descriptor == 0x70, (dheaders.descriptor)
340
341 # Check that the priority field (second field) is encoded as null
342 headers = dheaders.value
343 assert headers[1] is None, (headers[1])
344
345 def testDefaultPriorityDecode(self):
346 # This is a message with everything filled explicitly as null or zero in LIST32 HEADER and PROPERTIES lists
347 data = b'\x00\x53\x70\xd0\x00\x00\x00\x0a\x00\x00\x00\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xd0\x00\x00\x00\x22\x00\x00\x00\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x83\x00\x00\x00\x00\x00\x00\x00\x00\x83\x00\x00\x00\x00\x00\x00\x00\x00\x40\x52\x00\x40'
348 msg2 = Message()
349 msg2.decode(data)
350 assert msg2.priority == 4, (msg2.priority)
351
352 # The same message with LIST8s instead
353 data = b'\x00\x53\x70\xc0\x07\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xc0\x1f\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x83\x00\x00\x00\x00\x00\x00\x00\x00\x83\x00\x00\x00\x00\x00\x00\x00\x00\x40\x52\x00\x40'
354 msg3 = Message()
355 msg3.decode(data)
356 assert msg3.priority == 4, (msg3.priority)
357
358 # Minified message with zero length HEADER and PROPERTIES lists
359 data = b'\x00\x53\x70\x45' b'\x00\x53\x73\x45'
360 msg4 = Message()
361 msg4.decode(data)
362 assert msg4.priority == 4, (msg4.priority)