PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/client/client_test.py

https://code.google.com/
Python | 181 lines | 100 code | 41 blank | 40 comment | 1 complexity | 6022ddbea7ae28bc1ce7f391a91a24cb MD5 | raw file
Possible License(s): Apache-2.0
  1. #!/usr/bin/env python
  2. # Copyright 2010 Google Inc.
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Test for client."""
  15. from grr.client import conf
  16. from grr.client import conf as flags
  17. # Need to import client to add the flags.
  18. from grr.client import actions
  19. # Load all the standard actions
  20. from grr.client import client_actions
  21. from grr.client import comms
  22. from grr.lib import test_lib
  23. from grr.proto import jobs_pb2
  24. FLAGS = flags.FLAGS
  25. class MockAction(actions.ActionPlugin):
  26. in_protobuf = jobs_pb2.PrintStr
  27. out_protobuf = jobs_pb2.PrintStr
  28. def Run(self, message):
  29. self.SendReply(jobs_pb2.PrintStr(
  30. data="Received Message: %s. Data %s" % (message.data, "x" * 100)))
  31. class RaiseAction(actions.ActionPlugin):
  32. """A mock action which raises an error."""
  33. in_protobuf = jobs_pb2.PrintStr
  34. out_protobuf = jobs_pb2.PrintStr
  35. def Run(self, message):
  36. raise RuntimeError("I dont like %s" % message.data)
  37. class TestedContext(comms.GRRContext):
  38. """We test a simpler Context without crypto here."""
  39. def LoadCertificates(self):
  40. self.certs_loaded = True
  41. class BasicContextTests(test_lib.GRRBaseTest):
  42. """Test the GRR contexts."""
  43. session_id = "1234"
  44. to_test_context = TestedContext
  45. def setUp(self):
  46. self.context = self.to_test_context()
  47. self.context.LoadCertificates()
  48. def testHandleMessage(self):
  49. """Test handling of a normal request with a response."""
  50. # Push a request on it
  51. message = jobs_pb2.GrrMessage(
  52. name="MockAction",
  53. session_id=self.session_id,
  54. auth_state=jobs_pb2.GrrMessage.AUTHENTICATED,
  55. args=jobs_pb2.PrintStr(data="hello").SerializeToString(),
  56. request_id=1)
  57. self.context.HandleMessage(message)
  58. # Check the response - one data and one status
  59. message_list = self.context.Drain().job
  60. self.assertEqual(message_list[0].session_id, self.session_id)
  61. self.assertEqual(message_list[0].response_id, 1)
  62. self.assert_("hello" in message_list[0].args)
  63. self.assertEqual(message_list[1].response_id, 2)
  64. self.assertEqual(message_list[1].type, jobs_pb2.GrrMessage.STATUS)
  65. def testHandleError(self):
  66. """Test handling of a request which raises."""
  67. # Push a request on it
  68. message = jobs_pb2.GrrMessage(
  69. name="RaiseAction",
  70. session_id=self.session_id,
  71. auth_state=jobs_pb2.GrrMessage.AUTHENTICATED,
  72. request_id=1)
  73. self.context.HandleMessage(message)
  74. # Check the response - one data and one status
  75. message_list = self.context.Drain().job
  76. self.assertEqual(message_list[0].session_id, self.session_id)
  77. self.assertEqual(message_list[0].response_id, 1)
  78. status = jobs_pb2.GrrStatus()
  79. status.ParseFromString(message_list[0].args)
  80. self.assert_("RuntimeError" in status.error_message)
  81. self.assertNotEqual(status.status, jobs_pb2.GrrStatus.OK)
  82. def testUnauthenticated(self):
  83. """What happens if an unauthenticated message is sent to the client?
  84. RuntimeError needs to be issued, and the client needs to send a
  85. GrrStatus message with the traceback in it.
  86. """
  87. # Push a request on it
  88. message = jobs_pb2.GrrMessage(
  89. name="MockAction",
  90. session_id=self.session_id,
  91. auth_state=jobs_pb2.GrrMessage.UNAUTHENTICATED,
  92. request_id=1)
  93. self.context.HandleMessage(message)
  94. # We expect to receive an GrrStatus to indicate an exception was
  95. # raised:
  96. # Check the response - one data and one status
  97. message_list = self.context.Drain().job
  98. self.assertEqual(len(message_list), 1)
  99. self.assertEqual(message_list[0].session_id, self.session_id)
  100. self.assertEqual(message_list[0].response_id, 1)
  101. status = jobs_pb2.GrrStatus()
  102. status.ParseFromString(message_list[0].args)
  103. self.assert_("not Authenticated" in status.error_message)
  104. self.assert_("RuntimeError" in status.error_message)
  105. self.assertNotEqual(status.status, jobs_pb2.GrrStatus.OK)
  106. class TestedProcessSeparatedContext(comms.ProcessSeparatedContext):
  107. def LoadCertificates(self):
  108. pass
  109. class TestProcessSeparatedContext(BasicContextTests):
  110. """Test the process separated context."""
  111. to_test_context = TestedProcessSeparatedContext
  112. def setUp(self):
  113. comms.SlaveContext.LoadCertificates = lambda self: None
  114. BasicContextTests.setUp(self)
  115. def tearDown(self):
  116. self.context.Terminate()
  117. def testSegFault(self):
  118. """What happens if our slave crashes?"""
  119. # Push a request on it
  120. message = jobs_pb2.GrrMessage(
  121. name="KillSlave",
  122. session_id=self.session_id,
  123. auth_state=jobs_pb2.GrrMessage.AUTHENTICATED,
  124. request_id=1)
  125. self.context.HandleMessage(message)
  126. # We expect to receive an GrrStatus to indicate an exception was
  127. # raised:
  128. # Check the response - one data and one status
  129. message_list = self.context.Drain().job
  130. self.assertEqual(len(message_list), 1)
  131. self.assertEqual(message_list[0].session_id, self.session_id)
  132. status = jobs_pb2.GrrStatus()
  133. status.ParseFromString(message_list[0].args)
  134. self.assert_("Slave crashed" in status.error_message)
  135. self.assertNotEqual(status.status, jobs_pb2.GrrStatus.OK)
  136. def main(argv):
  137. test_lib.main(argv)
  138. if __name__ == "__main__":
  139. conf.StartMain(main)