/usr/share/doc/python-protobuf.socketrpc/examples/tests/channel_test.py is in python-protobuf.socketrpc 1.3.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | #!/usr/bin/python
# Copyright (c) 2009 Las Cumbres Observatory (www.lcogt.net)
# Copyright (c) 2010 Jan Dittberner
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
'''
channel_test.py - unit tests for the protobuf.channel module
Authors: Eric Saunders (esaunders@lcogt.net)
Martin Norbury (mnorbury@lcogt.net)
Jan Dittberner (jan@dittberner.info)
May 2009, Nov 2010
'''
import unittest
# Add protobuf module to the classpath
import sys
sys.path.append('../../main')
# Import the class to test
import protobuf.socketrpc.channel as ch
import protobuf.socketrpc.error as error
# Import the fake stub classes used in testing to simulate sockets etc.
from fake import FakeSocketFactory, FakeSocket, FakeCallback, TestServiceImpl
# Import the RPC definition class and the test service class
import protobuf.socketrpc.rpc_pb2 as rpc_pb2
import test_pb2
class TestSocketRpcChannel(unittest.TestCase):
'''Unit tests for the protobuf.channel.SocketRpcChannel class.'''
def setUp(self):
# Create a channel connected to a fake socket
self.factory = FakeSocketFactory()
self.channel = ch.SocketRpcChannel(socketFactory=self.factory)
# Define a simple service request
self.service_request = test_pb2.Request()
self.service_request.str_data = 'The lord giveth'
self.serialized_request = self.service_request.SerializeToString()
# Define a service response
self.service_response = test_pb2.Response()
self.service_response.str_data = 'And the lord taketh away'
self.serialized_response = \
self.service_response.SerializePartialToString()
# Define an RPC request with the service request as payload
self.rpc_request = rpc_pb2.Request()
self.rpc_request.request_proto = self.serialized_request
def tearDown(self):
pass
def test___init__1(self):
self.assertEqual(self.channel.sockFactory, self.factory,
'Initialising channel with user-supplied factory')
def test___init__defaults(self):
self.assert_(self.channel.host, True)
self.assert_(self.channel.port, True)
def test_validateRequest(self):
self.rpc_request.service_name = "Dummy Service"
self.rpc_request.method_name = "Dummy Method"
self.assertEqual(self.channel.validateRequest(self.rpc_request), None,
'validateRequest - valid request provided')
def test_validateRequest_BAD_REQUEST_PROTO(self):
# A request with mandatory fields missing
self.rpc_request = rpc_pb2.Request()
self.assertRaises(error.BadRequestProtoError,
self.channel.validateRequest,
self.rpc_request)
def test_openSocket(self):
'''Test normal return from openSocket.'''
self.assert_(self.channel.openSocket, "openSocket returns something")
def test_openSocket_IO_ERROR(self):
'''Test exceptional return from openSocket (IO_ERROR).'''
# Fake socket primed to throw an unknown host exception
socket = FakeSocket()
socket.throwIOErrorException()
self.factory.setSocket(socket)
self.assertRaises(error.IOError, self.channel.openSocket,
'host', -1)
def test_openSocket_UNKNOWN_HOST(self):
'''Test exceptional return from openSocket (UNKNOWN_HOST).'''
self.assert_(self.channel.openSocket, "openSocket returns something")
# Fake socket primed to throw an unknown host exception
socket = FakeSocket()
socket.throwUnknownHostException()
self.factory.setSocket(socket)
self.assertRaises(error.UnknownHostError, self.channel.openSocket,
'host', -1)
def test_createRpcRequest(self):
'''Test createRpcRequest - normal usage.'''
# Instantiate the test service, and get a reference to the method
method_name = 'TestMethod'
service = TestServiceImpl()
method = service.DESCRIPTOR.FindMethodByName(method_name)
# Define a simple service request
service_request = test_pb2.Request()
service_request.str_data = 'The lord giveth'
serialized_request = service_request.SerializeToString()
# Define an RPC request with the service request as payload
expected_rpc = rpc_pb2.Request()
expected_rpc.request_proto = serialized_request
expected_rpc.service_name = service.DESCRIPTOR.full_name
expected_rpc.method_name = method_name
self.assertEqual(
self.channel.createRpcRequest(method, service_request),
expected_rpc, 'createRpcRequest - normal usage')
def test_sendRpcMessage(self):
'''Test sendRpcMessage - normal usage.'''
# Create a socket and service request
sock = self.factory.createSocket()
sent_request = self.rpc_request
sent_request.service_name = "Dummy service"
sent_request.method_name = "Dummy method"
# Call the method
self.channel.sendRpcMessage(sock, sent_request)
# Extract the output that was written to the socket
received_request = rpc_pb2.Request()
received_request.MergeFromString(sock.output_stream.stream_data)
self.assertEqual(received_request, sent_request,
'Request written to socket')
def test_sendRpcMessage_IOError(self):
'''Test sendRpcMessage - IOError.'''
# Create a socket with an IOError condition set
sock = self.factory.createSocket()
sock.throwIOErrorException()
# Create a service request
sent_request = self.rpc_request
sent_request.service_name = "Dummy service"
sent_request.method_name = "Dummy method"
self.assertRaises(error.IOError, self.channel.sendRpcMessage, sock,
sent_request)
def test_recvRpcMessage(self):
'''Test recvRpcMessage - normal usage.'''
# Create a socket and service request
msg = 'Message from server'
sock = self.factory.createSocket()
sock.withInputBytes(msg)
# Call the method
self.assertEqual(self.channel.recvRpcMessage(sock), msg,
'recvRpcMessage - normal usage')
def test_recvRpcMessage_ioerror(self):
'''Test recvRpcMessage - IOError.'''
# Create a socket and service request
msg = 'Message from server'
sock = self.factory.createSocket()
sock.withInputBytes(msg)
sock.throwIOErrorException()
# Call the method
self.assertRaises(error.IOError, self.channel.recvRpcMessage, sock)
def test_parseResponse(self):
'''Test parseResponse - normal usage.'''
resp_class = rpc_pb2.Response
expected_response = resp_class()
bytestream = expected_response.SerializeToString()
self.assertEqual(self.channel.parseResponse(bytestream, resp_class),
expected_response, 'parseResponse - normal usage')
def test_parseResponse_junk_input(self):
'''Test the correct error is raised after sending complete crap.'''
# Setup an arbitrary and broken bytestream
bytestream = 'ABCD'
resp_class = rpc_pb2.Response
self.assertRaises(error.BadResponseProtoError,
self.channel.parseResponse, bytestream, resp_class)
def testGoodRpc(self):
'''Test a good RPC call.'''
# Fake socket with prepared response
socket = FakeSocket()
socket.withResponseProto(self.service_response)
socketFactory = FakeSocketFactory()
socketFactory.setSocket(socket)
# Create channel
channel = ch.SocketRpcChannel("host", -1, socketFactory)
controller = channel.newController()
# Create the service
service = test_pb2.TestService_Stub(channel)
# Call RPC method
callback = FakeCallback()
service.TestMethod(controller, self.service_request, callback)
self.assertTrue(callback.invoked, 'Callback invoked')
self.assertEquals(self.service_response.str_data,
callback.response.str_data, 'Response message')
self.assertEquals(self.serialized_request,
socket.getRequest().request_proto,
'Request protocol serialisation')
self.assertEquals(service.DESCRIPTOR.full_name,
socket.getRequest().service_name, 'Service name')
self.assertEquals(service.DESCRIPTOR.methods[0].name,
socket.getRequest().method_name, 'Method name')
def testUnknownHostException(self):
'''Test unknown host.'''
# Fake socket primed to throw an unknown host exception
socket = FakeSocket()
socket.throwUnknownHostException()
socketFactory = FakeSocketFactory()
socketFactory.setSocket(socket)
# Create channel
channel = ch.SocketRpcChannel("host", -1, socketFactory)
controller = channel.newController()
# Create the service
service = test_pb2.TestService_Stub(channel)
# Call RPC method
callback = FakeCallback()
service.TestMethod(controller, self.service_request, callback)
self.assertFalse(callback.invoked, 'Callback invoked')
self.assertTrue(controller.failed())
self.assertEquals(rpc_pb2.UNKNOWN_HOST, controller.reason,
'Error reason')
def testIOErrorWhileCreatingSocket(self):
'''Test Error while creating socket.'''
# Fake socket primed to throw an unknown host exception
socket = FakeSocket()
socket.throwIOErrorException()
socketFactory = FakeSocketFactory()
socketFactory.setSocket(socket)
# Create channel
channel = ch.SocketRpcChannel("host", -1, socketFactory)
controller = channel.newController()
# Create the service
service = test_pb2.TestService_Stub(channel)
# Call RPC method
callback = FakeCallback()
service.TestMethod(controller, self.service_request, callback)
self.assertFalse(callback.invoked, 'Callback invoked')
self.assertTrue(controller.failed())
self.assertEquals(rpc_pb2.IO_ERROR, controller.reason, 'Error reason')
def testIncompleteRequest(self):
'''Test calling RPC with incomplete request.'''
# Create data
service_request = test_pb2.Request()
# Fake socket with prepared response
socket = FakeSocket()
socket.withResponseProto(self.service_response)
socketFactory = FakeSocketFactory()
socketFactory.setSocket(socket)
# Create channel
channel = ch.SocketRpcChannel("host", -1, socketFactory)
controller = channel.newController()
# Create the service
service = test_pb2.TestService_Stub(channel)
# Call RPC method
callback = FakeCallback()
service.TestMethod(controller, service_request, callback)
self.assertFalse(callback.invoked, 'Callback invoked')
self.assertEquals(rpc_pb2.BAD_REQUEST_PROTO, controller.reason)
self.assertTrue(controller.failed())
def testNoCallBack(self):
'''Test RPC failing to invoke callback.'''
# Fake socket with callback set to false
socket = FakeSocket()
socket.withNoResponse(False)
socketFactory = FakeSocketFactory()
socketFactory.setSocket(socket)
# Create channel
channel = ch.SocketRpcChannel("host", -1, socketFactory)
controller = channel.newController()
# Create the service
service = test_pb2.TestService_Stub(channel)
# Call RPC method
callback = FakeCallback()
service.TestMethod(controller, self.service_request, callback)
self.assertFalse(callback.invoked, 'Callback invoked')
self.assertEquals(self.serialized_request,
socket.getRequest().request_proto,
'Request protocol serialisation')
self.assertEquals(service.DESCRIPTOR.full_name,
socket.getRequest().service_name, 'Service name')
self.assertEquals(service.DESCRIPTOR.methods[0].name,
socket.getRequest().method_name, 'Method name')
def testBadResponse(self):
'''Test bad response from server.'''
# Fake socket with prepared response
socket = FakeSocket()
socket.withInputBytes("bad response")
socketFactory = FakeSocketFactory()
socketFactory.setSocket(socket)
# Create channel
channel = ch.SocketRpcChannel("host", -1, socketFactory)
controller = channel.newController()
# Create the service
service = test_pb2.TestService_Stub(channel)
# Call RPC method
callback = FakeCallback()
service.TestMethod(controller, self.service_request, callback)
# Verify request was sent and bad response received
self.assertFalse(callback.invoked, 'Callback invoked')
self.assertEquals(self.serialized_request,
socket.getRequest().request_proto,
'Request protocol serialisation')
self.assertTrue(controller.failed(), 'Controller failed')
self.assertEquals(rpc_pb2.BAD_RESPONSE_PROTO, controller.reason,
'Controller reason')
class Test__LifeCycle(unittest.TestCase):
'''Unit tests for the protobuf.channel._Lifecycle class.'''
def setUp(self):
# Create a channel connected to a fake socket
self.factory = FakeSocketFactory()
self.socket = FakeSocket()
self.channel = ch.SocketRpcChannel(socketFactory=self.factory)
self.controller = self.channel.newController()
self.lc = ch._LifeCycle(self.controller, self.channel)
self.factory.setSocket(self.socket)
# Define a simple service request
self.service_request = test_pb2.Request()
self.service_request.str_data = 'The lord giveth'
self.serialized_request = self.service_request.SerializeToString()
# Define an RPC request with the service request as payload
self.rpc_request = rpc_pb2.Request()
self.rpc_request.request_proto = self.serialized_request
def tearDown(self):
pass
def test___init__(self):
'''Test _LifeCycle constructor.'''
self.assertEqual(self.lc.controller, self.controller,
"Attribute 'controller' incorrectly initialized")
self.assertEqual(self.lc.channel, self.channel,
"Attribute 'channel' incorrectly initialized")
self.assertEqual(self.lc.sock, None,
"Attribute 'sock' incorrectly initialized")
self.assertEqual(self.lc.byte_stream, None,
"Attribute 'byte_stream' incorrectly initialized")
self.assertEqual(self.lc.rpcResponse, None,
"Attribute 'rpcResponse' incorrectly initialized")
self.assertEqual(self.lc.serviceResponse, None,
"Attribute 'serviceResponse' incorrectly initialized")
def test_tryToValidateRequest(self):
'''Test tryToValidateRequest - normal usage.'''
self.assertEquals(self.lc.tryToValidateRequest(self.rpc_request),
None, "tryToValidateRequest - valid request")
def test_tryToValidateRequest_con_error(self):
'''Test tryToValidateRequest - controller in error state.'''
self.assertEquals(self.lc.tryToValidateRequest(self.rpc_request),
None, "tryToValidateRequest - controller in error state")
def test_tryToValidateRequest_BAD_REQUEST_PROTO(self):
'''Test tryToValidateRequest - BadRequestProto error thrown.'''
# A request with mandatory fields missing
self.rpc_request = rpc_pb2.Request()
self.lc.tryToValidateRequest(self.rpc_request)
self.assertEquals(self.controller.reason, rpc_pb2.BAD_REQUEST_PROTO,
"tryToValidateRequest - invalid request")
self.assertEquals(self.controller.failed(), True,
"tryToValidateRequest - invalid request")
def test_tryToOpenSocket(self):
'''Test tryToOpenSocket - normal usage.'''
self.lc.tryToOpenSocket()
self.assert_(self.lc.sock)
def test_tryToOpenSocket_con_error(self):
'''Test tryToOpenSocket - controller in error state.'''
self.controller._fail = True
self.lc.tryToOpenSocket()
self.assertEquals(self.lc.sock, None,
"tryToOpenSocket - controller in error state")
def test_tryToOpenSocket_UNKNOWN_HOST(self):
'''Test tryToOpenSocket - UnknownHost error thrown.'''
self.socket.throwUnknownHostException()
self.lc.tryToOpenSocket()
self.assertEquals(self.lc.sock, None,
"tryToOpenSocket - UNKNOWN_HOST error")
self.assertEquals(self.controller.reason, rpc_pb2.UNKNOWN_HOST,
"tryToOpenSocket - UNKNOWN_HOST error")
self.assertEquals(self.controller.failed(), True,
"tryToOpenSocket - UNKNOWN_HOST error")
def test_tryToOpenSocket_IO_ERROR(self):
'''Test tryToOpenSocket - IOError error thrown.'''
self.socket.throwIOErrorException()
self.lc.tryToOpenSocket()
self.assertEquals(self.lc.sock, None,
"tryToOpenSocket - IO_ERROR error")
self.assertEquals(self.controller.reason, rpc_pb2.IO_ERROR,
"tryToOpenSocket - IO_ERROR error")
self.assertEquals(self.controller.failed(), True,
"tryToOpenSocket - IO_ERROR error")
def test_tryToSendRpcRequest(self):
'''Test tryToSendRpcRequest - normal usage.'''
# Instantiate the test service, and get a reference to the method
method_name = 'TestMethod'
service = TestServiceImpl()
method = service.DESCRIPTOR.FindMethodByName(method_name)
# Set the service and method names of the RPC request
self.rpc_request.service_name = service.DESCRIPTOR.full_name
self.rpc_request.method_name = method_name
# Add the socket instance to the lifecycle object
self.lc.sock = self.socket
self.assertEquals(
self.lc.tryToSendRpcRequest(method, self.rpc_request),
None, "tryToSendRpcRequest - normal return")
def test_tryToSendRpcRequest_IO_ERROR(self):
'''Test tryToSendRpcRequest - IOError error thrown.'''
# Instantiate the test service, and get a reference to the method
method_name = 'TestMethod'
service = TestServiceImpl()
method = service.DESCRIPTOR.FindMethodByName(method_name)
# Set the service and method names of the RPC request
self.rpc_request.service_name = service.DESCRIPTOR.full_name
self.rpc_request.method_name = method_name
# Set the exception, and add the socket instance to the
# lifecycle object
self.socket.throwIOErrorException()
self.lc.sock = self.socket
self.assertEquals(
self.lc.tryToSendRpcRequest(method, self.rpc_request),
None, "tryToSendRpcRequest - IO_ERROR")
self.assertEquals(self.controller.reason, rpc_pb2.IO_ERROR,
"tryToSendRpcRequest - IO_ERROR error")
self.assertEquals(self.controller.failed(), True,
"tryToSendRpcRequest - IO_ERROR error")
def test_tryToReceiveReply(self):
'''Test tryToReceiveReply - normal usage.'''
# Add some data to the socket
msg = 'Message from server'
self.socket.withInputBytes(msg)
self.lc.sock = self.socket
self.assertEquals(self.lc.tryToReceiveReply(), None,
"tryToReceiveReply - normal usage")
# Verify the socket has been closed
self.assert_(self.socket.input_stream.closed,
"tryToReceiveReply - normal usage")
def test_tryToReceiveReply_IOError(self):
'''Test tryToReceiveReply - IOError thrown.'''
# Add some data to the socket
msg = 'Message from server'
self.socket.withInputBytes(msg)
self.socket.throwIOErrorException()
self.lc.sock = self.socket
self.assertEquals(self.lc.tryToReceiveReply(), None,
"tryToReceiveReply - IO_ERROR error")
self.assertEquals(self.controller.reason, rpc_pb2.IO_ERROR,
"tryToReceiveReply - IO_ERROR error")
self.assertEquals(self.controller.failed(), True,
"tryToReceiveReply - IO_ERROR error")
# Verify the socket has been closed
self.assert_(self.socket.input_stream.closed,
"tryToReceiveReply - IO_ERROR error")
def test_tryToParseReply(self):
'''Test tryToParseReply - normal usage.'''
resp_class = rpc_pb2.Response
expected_response = resp_class()
self.lc.byte_stream = expected_response.SerializeToString()
self.assertEquals(self.lc.tryToParseReply(), None,
"tryToParseReply - normal usage")
def test_tryToParseReply_BAD_RESPONSE_PROTO(self):
'''Test tryToParseReply - BadResponseProto error thrown.'''
# Setup an arbitrary and broken bytestream
self.lc.byte_stream = 'ABCD'
self.assertEquals(self.lc.tryToParseReply(), None,
"tryToParseReply - BAD_RESPONSE_PROTO error")
self.assertEquals(self.controller.reason, rpc_pb2.BAD_RESPONSE_PROTO,
"tryToParseReply - BAD_RESPONSE_PROTO error")
self.assertEquals(self.controller.failed(), True,
"tryToParseReply - BAD_RESPONSE_PROTO error")
def test_tryToRetrieveServiceResponse(self):
'''Test tryToRetrieveServiceResponse - normal usage.'''
resp_class = rpc_pb2.Response
expected_response = resp_class()
self.lc.byte_stream = expected_response.SerializeToString()
self.lc.rpcResponse = expected_response
self.assertEquals(self.lc.tryToRetrieveServiceResponse(resp_class),
None, "tryToRetrieveServiceResponse - normal usage")
def test_tryToRetrieveServiceResponse_BAD_RESPONSE_PROTO(self):
'''tryToRetrieveServiceResponse - BadResponseProto
This error can never trigger, since all fields of an RPC
Response() object are optional!'''
pass
def test_tryToRunCallback(self):
'''Test tryToRunCallback - normal usage.'''
callback = FakeCallback()
self.lc.rpcResponse = rpc_pb2.Response()
self.assertEquals(self.lc.tryToRunCallback(callback), None,
"tryToRunCallback - normal usage")
def suite():
'''Return the test suite containing all tests from this module.'''
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestSocketRpcChannel))
suite.addTest(unittest.makeSuite(Test__LifeCycle))
return suite
if __name__ == '__main__':
unittest.main()
|