1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
10 # pylint: disable=F0401
12 from mojo
import system
17 def _GetRandomBuffer(size
):
19 return bytearray(''.join(chr(random
.randint(0, 255)) for i
in xrange(size
)))
22 class BaseMojoTest(unittest
.TestCase
):
28 class CoreTest(BaseMojoTest
):
30 def testResults(self
):
31 self
.assertEquals(system
.RESULT_OK
, 0)
32 self
.assertLess(system
.RESULT_CANCELLED
, 0)
33 self
.assertLess(system
.RESULT_UNKNOWN
, 0)
34 self
.assertLess(system
.RESULT_INVALID_ARGUMENT
, 0)
35 self
.assertLess(system
.RESULT_DEADLINE_EXCEEDED
, 0)
36 self
.assertLess(system
.RESULT_NOT_FOUND
, 0)
37 self
.assertLess(system
.RESULT_ALREADY_EXISTS
, 0)
38 self
.assertLess(system
.RESULT_PERMISSION_DENIED
, 0)
39 self
.assertLess(system
.RESULT_RESOURCE_EXHAUSTED
, 0)
40 self
.assertLess(system
.RESULT_FAILED_PRECONDITION
, 0)
41 self
.assertLess(system
.RESULT_ABORTED
, 0)
42 self
.assertLess(system
.RESULT_OUT_OF_RANGE
, 0)
43 self
.assertLess(system
.RESULT_UNIMPLEMENTED
, 0)
44 self
.assertLess(system
.RESULT_INTERNAL
, 0)
45 self
.assertLess(system
.RESULT_UNAVAILABLE
, 0)
46 self
.assertLess(system
.RESULT_DATA_LOSS
, 0)
47 self
.assertLess(system
.RESULT_BUSY
, 0)
48 self
.assertLess(system
.RESULT_SHOULD_WAIT
, 0)
50 def testConstants(self
):
51 self
.assertGreaterEqual(system
.DEADLINE_INDEFINITE
, 0)
52 self
.assertGreaterEqual(system
.HANDLE_SIGNAL_NONE
, 0)
53 self
.assertGreaterEqual(system
.HANDLE_SIGNAL_READABLE
, 0)
54 self
.assertGreaterEqual(system
.HANDLE_SIGNAL_WRITABLE
, 0)
55 self
.assertGreaterEqual(system
.WRITE_MESSAGE_FLAG_NONE
, 0)
56 self
.assertGreaterEqual(system
.READ_MESSAGE_FLAG_NONE
, 0)
57 self
.assertGreaterEqual(system
.READ_MESSAGE_FLAG_MAY_DISCARD
, 0)
58 self
.assertGreaterEqual(system
.WRITE_DATA_FLAG_NONE
, 0)
59 self
.assertGreaterEqual(system
.WRITE_DATA_FLAG_ALL_OR_NONE
, 0)
60 self
.assertGreaterEqual(system
.READ_DATA_FLAG_NONE
, 0)
61 self
.assertGreaterEqual(system
.READ_DATA_FLAG_ALL_OR_NONE
, 0)
62 self
.assertGreaterEqual(system
.READ_DATA_FLAG_DISCARD
, 0)
63 self
.assertGreaterEqual(system
.READ_DATA_FLAG_QUERY
, 0)
64 self
.assertGreaterEqual(system
.MAP_BUFFER_FLAG_NONE
, 0)
66 def testGetTimeTicksNow(self
):
68 v1
= system
.GetTimeTicksNow()
70 v2
= system
.GetTimeTicksNow()
72 self
.assertGreater(v1
, 0)
73 self
.assertGreater(v2
, v1
+ 1000)
74 self
.assertGreater(1e6
* (pt2
- pt1
), v2
- v1
)
76 def _testHandlesCreation(self
, *args
):
78 self
.assertTrue(handle
.IsValid())
80 self
.assertFalse(handle
.IsValid())
82 def _TestMessageHandleCreation(self
, handles
):
83 self
._testHandlesCreation
(handles
.handle0
, handles
.handle1
)
85 def testCreateMessagePipe(self
):
86 self
._TestMessageHandleCreation
(system
.MessagePipe())
88 def testCreateMessagePipeWithNoneOptions(self
):
89 self
._TestMessageHandleCreation
(system
.MessagePipe(None))
91 def testCreateMessagePipeWithOptions(self
):
92 self
._TestMessageHandleCreation
(
93 system
.MessagePipe(system
.CreateMessagePipeOptions()))
95 def testWaitOverMessagePipe(self
):
96 handles
= system
.MessagePipe()
97 handle
= handles
.handle0
99 self
.assertEquals(system
.RESULT_OK
, handle
.Wait(
100 system
.HANDLE_SIGNAL_WRITABLE
, system
.DEADLINE_INDEFINITE
))
101 self
.assertEquals(system
.RESULT_DEADLINE_EXCEEDED
,
102 handle
.Wait(system
.HANDLE_SIGNAL_READABLE
, 0))
104 handles
.handle1
.WriteMessage()
109 system
.HANDLE_SIGNAL_READABLE
,
110 system
.DEADLINE_INDEFINITE
))
112 def testWaitOverManyMessagePipe(self
):
113 handles
= system
.MessagePipe()
114 handle0
= handles
.handle0
115 handle1
= handles
.handle1
120 [(handle0
, system
.HANDLE_SIGNAL_WRITABLE
),
121 (handle1
, system
.HANDLE_SIGNAL_WRITABLE
)],
122 system
.DEADLINE_INDEFINITE
))
124 system
.RESULT_DEADLINE_EXCEEDED
,
126 [(handle0
, system
.HANDLE_SIGNAL_READABLE
),
127 (handle1
, system
.HANDLE_SIGNAL_READABLE
)], 0))
129 handle0
.WriteMessage()
134 [(handle0
, system
.HANDLE_SIGNAL_READABLE
),
135 (handle1
, system
.HANDLE_SIGNAL_READABLE
)],
136 system
.DEADLINE_INDEFINITE
))
138 def testSendBytesOverMessagePipe(self
):
139 handles
= system
.MessagePipe()
140 data
= _GetRandomBuffer(DATA_SIZE
)
141 handles
.handle0
.WriteMessage(data
)
142 (res
, buffers
, next_message
) = handles
.handle1
.ReadMessage()
143 self
.assertEquals(system
.RESULT_RESOURCE_EXHAUSTED
, res
)
144 self
.assertEquals(None, buffers
)
145 self
.assertEquals((DATA_SIZE
, 0), next_message
)
146 result
= bytearray(DATA_SIZE
)
147 (res
, buffers
, next_message
) = handles
.handle1
.ReadMessage(result
)
148 self
.assertEquals(system
.RESULT_OK
, res
)
149 self
.assertEquals(None, next_message
)
150 self
.assertEquals((data
, []), buffers
)
152 def testSendEmptyDataOverMessagePipe(self
):
153 handles
= system
.MessagePipe()
154 handles
.handle0
.WriteMessage(None)
155 (res
, buffers
, next_message
) = handles
.handle1
.ReadMessage()
157 self
.assertEquals(system
.RESULT_OK
, res
)
158 self
.assertEquals(None, next_message
)
159 self
.assertEquals((None, []), buffers
)
161 def testSendHandleOverMessagePipe(self
):
162 handles
= system
.MessagePipe()
163 handles_to_send
= system
.MessagePipe()
164 handles
.handle0
.WriteMessage(handles
=[handles_to_send
.handle0
,
165 handles_to_send
.handle1
])
166 (res
, buffers
, next_message
) = handles
.handle1
.ReadMessage(
167 max_number_of_handles
=2)
169 self
.assertFalse(handles_to_send
.handle0
.IsValid())
170 self
.assertFalse(handles_to_send
.handle1
.IsValid())
171 self
.assertEquals(system
.RESULT_OK
, res
)
172 self
.assertEquals(None, next_message
)
173 self
.assertEquals(None, buffers
[0])
174 self
.assertEquals(2, len(buffers
[1]))
177 for handle
in handles
:
178 self
.assertTrue(handle
.IsValid())
179 (res
, buffers
, next_message
) = handle
.ReadMessage()
180 self
.assertEquals(system
.RESULT_SHOULD_WAIT
, res
)
182 for handle
in handles
:
183 handle
.WriteMessage()
185 for handle
in handles
:
186 (res
, buffers
, next_message
) = handle
.ReadMessage()
187 self
.assertEquals(system
.RESULT_OK
, res
)
189 def _TestDataHandleCreation(self
, handles
):
190 self
._testHandlesCreation
(
191 handles
.producer_handle
, handles
.consumer_handle
)
193 def testCreateDataPipe(self
):
194 self
._TestDataHandleCreation
(system
.DataPipe())
196 def testCreateDataPipeWithNoneOptions(self
):
197 self
._TestDataHandleCreation
(system
.DataPipe(None))
199 def testCreateDataPipeWithDefaultOptions(self
):
200 self
._TestDataHandleCreation
(
201 system
.DataPipe(system
.CreateDataPipeOptions()))
203 def testCreateDataPipeWithDiscardFlag(self
):
204 options
= system
.CreateDataPipeOptions()
205 options
.flags
= system
.CreateDataPipeOptions
.FLAG_MAY_DISCARD
206 self
._TestDataHandleCreation
(system
.DataPipe(options
))
208 def testCreateDataPipeWithElementSize(self
):
209 options
= system
.CreateDataPipeOptions()
210 options
.element_num_bytes
= 5
211 self
._TestDataHandleCreation
(system
.DataPipe(options
))
213 def testCreateDataPipeWithCapacity(self
):
214 options
= system
.CreateDataPipeOptions()
215 options
.element_capacity_num_bytes
= DATA_SIZE
216 self
._TestDataHandleCreation
(system
.DataPipe(options
))
218 def testCreateDataPipeWithIncorrectParameters(self
):
219 options
= system
.CreateDataPipeOptions()
220 options
.element_num_bytes
= 5
221 options
.capacity_num_bytes
= DATA_SIZE
222 with self
.assertRaises(system
.MojoException
) as cm
:
223 self
._TestDataHandleCreation
(system
.DataPipe(options
))
224 self
.assertEquals(system
.RESULT_INVALID_ARGUMENT
, cm
.exception
.mojo_result
)
226 def testSendEmptyDataOverDataPipe(self
):
227 pipes
= system
.DataPipe()
228 self
.assertEquals((system
.RESULT_OK
, 0), pipes
.producer_handle
.WriteData())
230 (system
.RESULT_OK
, None), pipes
.consumer_handle
.ReadData())
232 def testSendDataOverDataPipe(self
):
233 pipes
= system
.DataPipe()
234 data
= _GetRandomBuffer(DATA_SIZE
)
235 self
.assertEquals((system
.RESULT_OK
, DATA_SIZE
),
236 pipes
.producer_handle
.WriteData(data
))
237 self
.assertEquals((system
.RESULT_OK
, data
),
238 pipes
.consumer_handle
.ReadData(bytearray(DATA_SIZE
)))
240 def testTwoPhaseWriteOnDataPipe(self
):
241 pipes
= system
.DataPipe()
242 (res
, buf
) = pipes
.producer_handle
.BeginWriteData(DATA_SIZE
)
243 self
.assertEquals(system
.RESULT_OK
, res
)
244 self
.assertGreaterEqual(len(buf
.buffer), DATA_SIZE
)
245 data
= _GetRandomBuffer(DATA_SIZE
)
246 buf
.buffer[0:DATA_SIZE
] = data
247 self
.assertEquals(system
.RESULT_OK
, buf
.End(DATA_SIZE
))
248 self
.assertEquals((system
.RESULT_OK
, data
),
249 pipes
.consumer_handle
.ReadData(bytearray(DATA_SIZE
)))
251 def testTwoPhaseReadOnDataPipe(self
):
252 pipes
= system
.DataPipe()
253 data
= _GetRandomBuffer(DATA_SIZE
)
254 self
.assertEquals((system
.RESULT_OK
, DATA_SIZE
),
255 pipes
.producer_handle
.WriteData(data
))
256 (res
, buf
) = pipes
.consumer_handle
.BeginReadData()
257 self
.assertEquals(system
.RESULT_OK
, res
)
258 self
.assertEquals(DATA_SIZE
, len(buf
.buffer))
259 self
.assertEquals(data
, buf
.buffer)
260 self
.assertEquals(system
.RESULT_OK
, buf
.End(DATA_SIZE
))
262 def testCreateSharedBuffer(self
):
263 self
._testHandlesCreation
(system
.CreateSharedBuffer(DATA_SIZE
))
265 def testCreateSharedBufferWithNoneOptions(self
):
266 self
._testHandlesCreation
(system
.CreateSharedBuffer(DATA_SIZE
, None))
268 def testCreateSharedBufferWithDefaultOptions(self
):
269 self
._testHandlesCreation
(
270 system
.CreateSharedBuffer(
272 system
.CreateSharedBufferOptions()))
274 def testDuplicateSharedBuffer(self
):
275 handle
= system
.CreateSharedBuffer(DATA_SIZE
)
276 self
._testHandlesCreation
(handle
.Duplicate())
278 def testDuplicateSharedBufferWithNoneOptions(self
):
279 handle
= system
.CreateSharedBuffer(DATA_SIZE
)
280 self
._testHandlesCreation
(handle
.Duplicate(None))
282 def testDuplicateSharedBufferWithDefaultOptions(self
):
283 handle
= system
.CreateSharedBuffer(DATA_SIZE
)
284 self
._testHandlesCreation
(
285 handle
.Duplicate(system
.DuplicateSharedBufferOptions()))
287 def testSendBytesOverSharedBuffer(self
):
288 handle
= system
.CreateSharedBuffer(DATA_SIZE
)
289 duplicated
= handle
.Duplicate()
290 data
= _GetRandomBuffer(DATA_SIZE
)
291 (res1
, buf1
) = handle
.Map(0, DATA_SIZE
)
292 (res2
, buf2
) = duplicated
.Map(0, DATA_SIZE
)
293 self
.assertEquals(system
.RESULT_OK
, res1
)
294 self
.assertEquals(system
.RESULT_OK
, res2
)
295 self
.assertEquals(DATA_SIZE
, len(buf1
.buffer))
296 self
.assertEquals(DATA_SIZE
, len(buf2
.buffer))
297 self
.assertEquals(buf1
.buffer, buf2
.buffer)
299 buf1
.buffer[:] = data
300 self
.assertEquals(data
, buf1
.buffer)
301 self
.assertEquals(data
, buf2
.buffer)
302 self
.assertEquals(buf1
.buffer, buf2
.buffer)
305 if __name__
== '__main__':
306 suite
= unittest
.TestLoader().loadTestsFromTestCase(CoreTest
)
307 test_results
= unittest
.TextTestRunner(verbosity
=0).run(suite
)
308 if not test_results
.wasSuccessful():